From c058b2be3ccdbe3ef7a41b993e7494dbf92677b1 Mon Sep 17 00:00:00 2001 From: gnc Date: Fri, 8 Jan 2021 19:36:49 +0100 Subject: [PATCH 01/13] new implementation for adjacency list graph --- graphs/graph_list.py | 119 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 95 insertions(+), 24 deletions(-) diff --git a/graphs/graph_list.py b/graphs/graph_list.py index a812fecd961e..c9ffd557e586 100644 --- a/graphs/graph_list.py +++ b/graphs/graph_list.py @@ -1,44 +1,115 @@ #!/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 + """ + + # 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] = [] def print_list(self) -> None: - 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() + 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: []} From 4018f40700fd8606326fc4f62abd209d91755843 Mon Sep 17 00:00:00 2001 From: gnc Date: Fri, 8 Jan 2021 19:50:57 +0100 Subject: [PATCH 02/13] add example code for undirected graph --- graphs/graph_list.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/graphs/graph_list.py b/graphs/graph_list.py index c9ffd557e586..b6b7eb2a1bb0 100644 --- a/graphs/graph_list.py +++ b/graphs/graph_list.py @@ -89,6 +89,7 @@ def __repr__(self) -> str: if __name__ == "__main__": + # directed graph al = GraphAdjacencyList() al.add_edge(0, 1) al.add_edge(1, 3) @@ -113,3 +114,29 @@ def __repr__(self) -> str: # 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]} From b935b013d581ab6d16647037f6ec9961e747dda4 Mon Sep 17 00:00:00 2001 From: gnc Date: Fri, 8 Jan 2021 22:58:31 +0100 Subject: [PATCH 03/13] reduce length to 88 columns max to fix build errors7 --- graphs/graph_list.py | 63 ++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/graphs/graph_list.py b/graphs/graph_list.py index b6b7eb2a1bb0..87fbcf93fe1f 100644 --- a/graphs/graph_list.py +++ b/graphs/graph_list.py @@ -9,71 +9,82 @@ class GraphAdjacencyList: """ - Adjacency List type Graph Data Structure that accounts for directed and undirected Graphs. + 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. + 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) + 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 + Connects vertices together. Creates and Edge from source vertex to destination + vertex. Vertices will be created if not found in graph """ # 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 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. + # 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. + # 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. + # 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: - # if both source vertex and destination vertex are present in adjacency list, add destination vertex to - # source vertex list of adjacent vertices. + # 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 + # 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 + # 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 + # 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] = [] From e0ccc3697cefb0214dfca6e84afebf433238f04b Mon Sep 17 00:00:00 2001 From: gnc Date: Sat, 9 Jan 2021 22:05:30 +0100 Subject: [PATCH 04/13] fix pre commit issues --- graphs/graph_list.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/graphs/graph_list.py b/graphs/graph_list.py index 87fbcf93fe1f..80fa95833980 100644 --- a/graphs/graph_list.py +++ b/graphs/graph_list.py @@ -12,6 +12,7 @@ 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 @@ -20,7 +21,7 @@ def __init__(self, directed=True): Parameters ---------- directed (bool): Indicates if graph is directed or undirected. Default is - directed (True). + directed (True) """ self.adj_list = {} # dictionary of lists self.directed = directed @@ -70,21 +71,21 @@ def add_edge(self, source_vertex: int, destination_vertex: int) -> None: # 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. + # 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. + # 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. + # with destination vertex as key, which has no adjacent vertex else: self.adj_list[source_vertex] = [destination_vertex] self.adj_list[destination_vertex] = [] From 82605e75febcb1a885b96b3cc611c512f9dd263a Mon Sep 17 00:00:00 2001 From: gnc Date: Tue, 12 Jan 2021 10:04:20 +0100 Subject: [PATCH 05/13] replace print_list method with __str__ --- graphs/graph_list.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/graphs/graph_list.py b/graphs/graph_list.py index 80fa95833980..2033da886acf 100644 --- a/graphs/graph_list.py +++ b/graphs/graph_list.py @@ -90,11 +90,11 @@ def add_edge(self, source_vertex: int, destination_vertex: int) -> None: self.adj_list[source_vertex] = [destination_vertex] self.adj_list[destination_vertex] = [] - def print_list(self) -> None: + def __str__(self) -> str: """ Displays adjacency list using python's pretty print function """ - pprint(self.adj_list) + return pformat(self.adj_list) def __repr__(self) -> str: return pformat(self.adj_list) @@ -113,7 +113,7 @@ def __repr__(self) -> str: al.add_edge(4, 6) al.add_edge(2, 8) - al.print_list() + print(al) # OUTPUT: # {0: [1], @@ -139,7 +139,7 @@ def __repr__(self) -> str: al2.add_edge(4, 6) al2.add_edge(2, 8) - al2.print_list() + print(al2) # OUTPUT: # {0: [1], From 6a6bbeaee594bac96378fb71a46828223afa4769 Mon Sep 17 00:00:00 2001 From: gnc Date: Tue, 12 Jan 2021 10:07:53 +0100 Subject: [PATCH 06/13] return object in add_edge method to enable fluent syntax --- graphs/graph_list.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/graphs/graph_list.py b/graphs/graph_list.py index 2033da886acf..10e052377c62 100644 --- a/graphs/graph_list.py +++ b/graphs/graph_list.py @@ -26,7 +26,7 @@ def __init__(self, directed=True): self.adj_list = {} # dictionary of lists self.directed = directed - def add_edge(self, source_vertex: int, destination_vertex: int) -> None: + def add_edge(self, source_vertex: int, destination_vertex: int) -> object: """ Connects vertices together. Creates and Edge from source vertex to destination vertex. @@ -90,6 +90,8 @@ def add_edge(self, source_vertex: int, destination_vertex: int) -> None: self.adj_list[source_vertex] = [destination_vertex] self.adj_list[destination_vertex] = [] + return self + def __str__(self) -> str: """ Displays adjacency list using python's pretty print function From 563b782f402d72710289ca9fb1486a85ffccb153 Mon Sep 17 00:00:00 2001 From: gnc Date: Tue, 12 Jan 2021 11:27:14 +0100 Subject: [PATCH 07/13] improve class docstring and include doctests --- graphs/graph_list.py | 119 ++++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 59 deletions(-) diff --git a/graphs/graph_list.py b/graphs/graph_list.py index 10e052377c62..d923c4fad308 100644 --- a/graphs/graph_list.py +++ b/graphs/graph_list.py @@ -11,18 +11,68 @@ class GraphAdjacencyList: """ Adjacency List type Graph Data Structure that accounts for directed and undirected Graphs. + 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) + + Examples + -------- + + Directed Graph + >>> d_graph = GraphAdjacencyList() + >>> d_graph.add_edge(0, 1) + {0: [1], 1: []} + >>> d_graph.add_edge(1, 2).add_edge(1, 4).add_edge(1, 5) + {0: [1], 1: [2, 4, 5], 2: [], 4: [], 5: []} + >>> d_graph.add_edge(2, 0).add_edge(2, 6).add_edge(2, 7) + {0: [1], 1: [2, 4, 5], 2: [0, 6, 7], 4: [], 5: [], 6: [], 7: []} + >>> + >>> print(d_graph) + {0: [1], 1: [2, 4, 5], 2: [0, 6, 7], 4: [], 5: [], 6: [], 7: []} + >>> print(repr(d_graph)) + {0: [1], 1: [2, 4, 5], 2: [0, 6, 7], 4: [], 5: [], 6: [], 7: []} + + + Undirected Graph + >>> u_graph = GraphAdjacencyList(directed=False) + >>> u_graph.add_edge(0, 1) + {0: [1], 1: [0]} + >>> u_graph.add_edge(1, 2).add_edge(1, 4).add_edge(1, 5) + {0: [1], 1: [0, 2, 4, 5], 2: [1], 4: [1], 5: [1]} + >>> u_graph.add_edge(2, 0).add_edge(2, 6).add_edge(2, 7) + {0: [1, 2], 1: [0, 2, 4, 5], 2: [1, 0, 6, 7], 4: [1], 5: [1], 6: [2], 7: [2]} + >>> u_graph.add_edge(4, 5) + {0: [1, 2], + 1: [0, 2, 4, 5], + 2: [1, 0, 6, 7], + 4: [1, 5], + 5: [1, 4], + 6: [2], + 7: [2]} + >>> print(u_graph) + {0: [1, 2], + 1: [0, 2, 4, 5], + 2: [1, 0, 6, 7], + 4: [1, 5], + 5: [1, 4], + 6: [2], + 7: [2]} + >>> print(repr(u_graph)) + {0: [1, 2], + 1: [0, 2, 4, 5], + 2: [1, 0, 6, 7], + 4: [1, 5], + 5: [1, 4], + 6: [2], + 7: [2]} """ 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 @@ -103,54 +153,5 @@ def __repr__(self) -> str: if __name__ == "__main__": - # directed graph - al = GraphAdjacencyList() - al.add_edge(0, 1) - al.add_edge(1, 3) - al.add_edge(1, 4) - al.add_edge(1, 2) - 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) - - print(al) - - # OUTPUT: - # {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) - - print(al2) - - # 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]} + import doctest + doctest.testmod() \ No newline at end of file From 27f0f14c6623b2f29ac25139a7a5b57aeb0e0b9c Mon Sep 17 00:00:00 2001 From: gnc Date: Tue, 12 Jan 2021 11:28:11 +0100 Subject: [PATCH 08/13] add end of file line --- graphs/graph_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/graph_list.py b/graphs/graph_list.py index d923c4fad308..58ced22f2055 100644 --- a/graphs/graph_list.py +++ b/graphs/graph_list.py @@ -154,4 +154,4 @@ def __repr__(self) -> str: if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From 95cab103016df069f0b6f9e6a63e67e5ef2124a3 Mon Sep 17 00:00:00 2001 From: gnc Date: Tue, 12 Jan 2021 11:35:00 +0100 Subject: [PATCH 09/13] fix pre-commit issues --- graphs/graph_list.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/graphs/graph_list.py b/graphs/graph_list.py index 58ced22f2055..bcc3329d8729 100644 --- a/graphs/graph_list.py +++ b/graphs/graph_list.py @@ -4,7 +4,7 @@ # We can use Python's dictionary for constructing the graph. -from pprint import pformat, pprint +from pprint import pformat class GraphAdjacencyList: @@ -154,4 +154,5 @@ def __repr__(self) -> str: if __name__ == "__main__": import doctest + doctest.testmod() From f7d5c3143b6354a263d5541177c1346d5c345a6c Mon Sep 17 00:00:00 2001 From: gnc Date: Tue, 12 Jan 2021 12:30:50 +0100 Subject: [PATCH 10/13] remove __str__ method --- graphs/graph_list.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/graphs/graph_list.py b/graphs/graph_list.py index bcc3329d8729..1c3f16d6f29f 100644 --- a/graphs/graph_list.py +++ b/graphs/graph_list.py @@ -142,12 +142,6 @@ def add_edge(self, source_vertex: int, destination_vertex: int) -> object: return self - def __str__(self) -> str: - """ - Displays adjacency list using python's pretty print function - """ - return pformat(self.adj_list) - def __repr__(self) -> str: return pformat(self.adj_list) From 664cc028b53c8753201d38a7051bcba3b40af2bd Mon Sep 17 00:00:00 2001 From: gnc Date: Tue, 12 Jan 2021 13:01:32 +0100 Subject: [PATCH 11/13] trigger build From 49109f9793455b594bfe24ac7d55d460e2f291a5 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 12 Jan 2021 14:29:46 +0100 Subject: [PATCH 12/13] Update graph_list.py --- graphs/graph_list.py | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/graphs/graph_list.py b/graphs/graph_list.py index 1c3f16d6f29f..bba27de673bc 100644 --- a/graphs/graph_list.py +++ b/graphs/graph_list.py @@ -1,8 +1,8 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # Author: OMKAR PATHAK, Nwachukwu Chidiebere -# We can use Python's dictionary for constructing the graph. +# Use a Python dictionary to construct the graph. from pprint import pformat @@ -10,34 +10,24 @@ class GraphAdjacencyList: """ Adjacency List type Graph Data Structure that accounts for directed and undirected - Graphs. - Initialize graph object indicating whether it's directed or undirected. Default - is directed. + Graphs. Initialize graph object indicating whether it's directed or undirected. - Parameters - ---------- - directed: (bool) Indicates if graph is directed or undirected. Default is - directed (True) - - Examples - -------- - - Directed Graph + Directed graph example: >>> d_graph = GraphAdjacencyList() + >>> d_graph + {} >>> d_graph.add_edge(0, 1) {0: [1], 1: []} >>> d_graph.add_edge(1, 2).add_edge(1, 4).add_edge(1, 5) {0: [1], 1: [2, 4, 5], 2: [], 4: [], 5: []} >>> d_graph.add_edge(2, 0).add_edge(2, 6).add_edge(2, 7) {0: [1], 1: [2, 4, 5], 2: [0, 6, 7], 4: [], 5: [], 6: [], 7: []} - >>> >>> print(d_graph) {0: [1], 1: [2, 4, 5], 2: [0, 6, 7], 4: [], 5: [], 6: [], 7: []} >>> print(repr(d_graph)) {0: [1], 1: [2, 4, 5], 2: [0, 6, 7], 4: [], 5: [], 6: [], 7: []} - - Undirected Graph + Undirected graph example: >>> u_graph = GraphAdjacencyList(directed=False) >>> u_graph.add_edge(0, 1) {0: [1], 1: [0]} @@ -71,8 +61,12 @@ class GraphAdjacencyList: 7: [2]} """ - def __init__(self, directed=True): - + def __init__(self, directed: bool = True): + """ + Parameters: + directed: (bool) Indicates if graph is directed or undirected. Default is True. + """ + self.adj_list = {} # dictionary of lists self.directed = directed @@ -83,8 +77,7 @@ def add_edge(self, source_vertex: int, destination_vertex: int) -> object: Vertices will be created if not found in graph """ - # For undirected graphs - if not self.directed: + if not self.directed: # For undirected graphs # 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 @@ -114,9 +107,7 @@ def add_edge(self, source_vertex: int, destination_vertex: int) -> object: else: self.adj_list[source_vertex] = [destination_vertex] self.adj_list[destination_vertex] = [source_vertex] - - # For directed Graphs - else: + else: # For directed graphs # 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: From d13f7568efed2c85e6612d5b03f78e2974fcc60d Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 12 Jan 2021 14:34:31 +0100 Subject: [PATCH 13/13] Update graph_list.py --- graphs/graph_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/graph_list.py b/graphs/graph_list.py index bba27de673bc..bab6d6893a89 100644 --- a/graphs/graph_list.py +++ b/graphs/graph_list.py @@ -66,7 +66,7 @@ def __init__(self, directed: bool = True): Parameters: directed: (bool) Indicates if graph is directed or undirected. Default is True. """ - + self.adj_list = {} # dictionary of lists self.directed = directed