Skip to content

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 13 commits into from
Jan 12, 2021
Prev Previous commit
Next Next commit
reduce length to 88 columns max to fix build errors7
  • Loading branch information
gnc committed Jan 8, 2021
commit b935b013d581ab6d16647037f6ec9961e747dda4
63 changes: 37 additions & 26 deletions graphs/graph_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down