-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Graph list patch #4113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Graph list patch #4113
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c058b2b
new implementation for adjacency list graph
4018f40
add example code for undirected graph
b935b01
reduce length to 88 columns max to fix build errors7
e0ccc36
fix pre commit issues
82605e7
replace print_list method with __str__
6a6bbea
return object in add_edge method to enable fluent syntax
563b782
improve class docstring and include doctests
27f0f14
add end of file line
95cab10
fix pre-commit issues
f7d5c31
remove __str__ method
664cc02
trigger build
49109f9
Update graph_list.py
cclauss d13f756
Update graph_list.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,153 @@ | ||
#!/usr/bin/python | ||
|
||
# Author: OMKAR PATHAK | ||
# Author: OMKAR PATHAK, Nwachukwu Chidiebere | ||
|
||
# We can use Python's dictionary for constructing the graph. | ||
|
||
from pprint import pformat, pprint | ||
|
||
class AdjacencyList: | ||
def __init__(self): | ||
self.adj_list = {} | ||
|
||
def add_edge(self, from_vertex: int, to_vertex: int) -> None: | ||
# check if vertex is already present | ||
if from_vertex in self.adj_list: | ||
self.adj_list[from_vertex].append(to_vertex) | ||
class GraphAdjacencyList: | ||
""" | ||
Adjacency List type Graph Data Structure that accounts for directed and undirected | ||
Graphs. | ||
""" | ||
def __init__(self, directed=True): | ||
""" | ||
Initialize graph object indicating whether it's directed or undirected. Default | ||
is directed. | ||
|
||
Parameters | ||
---------- | ||
directed (bool): Indicates if graph is directed or undirected. Default is | ||
directed (True). | ||
""" | ||
self.adj_list = {} # dictionary of lists | ||
self.directed = directed | ||
|
||
def add_edge(self, source_vertex: int, destination_vertex: int) -> None: | ||
""" | ||
Connects vertices together. Creates and Edge from source vertex to destination | ||
vertex. | ||
Vertices will be created if not found in graph | ||
""" | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# For undirected graphs | ||
if not self.directed: | ||
# if both source vertex and destination vertex are both present in the | ||
# adjacency list, add destination vertex to source vertex list of adjacent | ||
# vertices and add source vertex to destination vertex list of adjacent | ||
# vertices. | ||
if source_vertex in self.adj_list and destination_vertex in self.adj_list: | ||
self.adj_list[source_vertex].append(destination_vertex) | ||
self.adj_list[destination_vertex].append(source_vertex) | ||
# if only source vertex is present in adjacency list, add destination vertex | ||
# to source vertex list of adjacent vertices, then create a new vertex with | ||
# destination vertex as key and assign a list containing the source vertex | ||
# as it's first adjacent vertex. | ||
elif source_vertex in self.adj_list: | ||
self.adj_list[source_vertex].append(destination_vertex) | ||
self.adj_list[destination_vertex] = [source_vertex] | ||
# if only destination vertex is present in adjacency list, add source vertex | ||
# to destination vertex list of adjacent vertices, then create a new vertex | ||
# with source vertex as key and assign a list containing the source vertex | ||
# as it's first adjacent vertex. | ||
elif destination_vertex in self.adj_list: | ||
self.adj_list[destination_vertex].append(source_vertex) | ||
self.adj_list[source_vertex] = [destination_vertex] | ||
# if both source vertex and destination vertex are not present in adjacency | ||
# list, create a new vertex with source vertex as key and assign a list | ||
# containing the destination vertex as it's first adjacent vertex also | ||
# create a new vertex with destination vertex as key and assign a list | ||
# containing the source vertex as it's first adjacent vertex. | ||
else: | ||
self.adj_list[source_vertex] = [destination_vertex] | ||
self.adj_list[destination_vertex] = [source_vertex] | ||
|
||
# For directed Graphs | ||
else: | ||
self.adj_list[from_vertex] = [to_vertex] | ||
# if both source vertex and destination vertex are present in adjacency | ||
# list, add destination vertex to source vertex list of adjacent vertices. | ||
if source_vertex in self.adj_list and destination_vertex in self.adj_list: | ||
self.adj_list[source_vertex].append(destination_vertex) | ||
# if only source vertex is present in adjacency list, add destination vertex | ||
# to source vertex list of adjacent vertices and create a new vertex with | ||
# destination vertex as key, which has no adjacent vertex. | ||
elif source_vertex in self.adj_list: | ||
self.adj_list[source_vertex].append(destination_vertex) | ||
self.adj_list[destination_vertex] = [] | ||
# if only destination vertex is present in adjacency list, create a new | ||
# vertex with source vertex as key and assign a list containing destination | ||
# vertex as first adjacent vertex. | ||
elif destination_vertex in self.adj_list: | ||
self.adj_list[source_vertex] = [destination_vertex] | ||
# if both source vertex and destination vertex are not present in adjacency | ||
# list, create a new vertex with source vertex as key and a list containing | ||
# destination vertex as it's first adjacent vertex. Then create a new vertex | ||
# with destination vertex as key, which has no adjacent vertex. | ||
else: | ||
self.adj_list[source_vertex] = [destination_vertex] | ||
self.adj_list[destination_vertex] = [] | ||
chidi-godwin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def print_list(self) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need doctests for both. |
||
for i in self.adj_list: | ||
print((i, "->", " -> ".join([str(j) for j in self.adj_list[i]]))) | ||
""" | ||
Displays adjacency list using python's pretty print function | ||
""" | ||
pprint(self.adj_list) | ||
|
||
def __repr__(self) -> str: | ||
return pformat(self.adj_list) | ||
|
||
|
||
if __name__ == "__main__": | ||
al = AdjacencyList() | ||
# directed graph | ||
al = GraphAdjacencyList() | ||
al.add_edge(0, 1) | ||
al.add_edge(0, 4) | ||
al.add_edge(4, 1) | ||
al.add_edge(4, 3) | ||
al.add_edge(1, 0) | ||
al.add_edge(1, 4) | ||
al.add_edge(1, 3) | ||
al.add_edge(1, 4) | ||
al.add_edge(1, 2) | ||
al.add_edge(2, 3) | ||
al.add_edge(3, 4) | ||
al.add_edge(3, 7) | ||
al.add_edge(3, 9) | ||
al.add_edge(4, 5) | ||
al.add_edge(4, 6) | ||
al.add_edge(2, 8) | ||
|
||
al.print_list() | ||
|
||
# OUTPUT: | ||
# 0 -> 1 -> 4 | ||
# 1 -> 0 -> 4 -> 3 -> 2 | ||
# 2 -> 3 | ||
# 3 -> 4 | ||
# 4 -> 1 -> 3 | ||
# {0: [1], | ||
# 1: [3, 4, 2], | ||
# 2: [8], | ||
# 3: [7, 9], | ||
# 4: [5, 6], | ||
# 5: [], | ||
# 6: [], | ||
# 7: [], | ||
# 8: [], | ||
# 9: []} | ||
|
||
# Undirected graph | ||
al2 = GraphAdjacencyList(False) | ||
al2.add_edge(0, 1) | ||
al2.add_edge(1, 3) | ||
al2.add_edge(1, 4) | ||
al2.add_edge(1, 2) | ||
al2.add_edge(3, 7) | ||
al2.add_edge(3, 9) | ||
al2.add_edge(4, 5) | ||
al2.add_edge(4, 6) | ||
al2.add_edge(2, 8) | ||
|
||
al2.print_list() | ||
|
||
# OUTPUT: | ||
# {0: [1], | ||
# 1: [0, 3, 4, 2], | ||
# 2: [1, 8], | ||
# 3: [1, 7, 9], | ||
# 4: [1, 5, 6], | ||
# 5: [4], | ||
# 6: [4], | ||
# 7: [3], | ||
# 8: [2], | ||
# 9: [3]} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.