Skip to content
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

Improved Graph Implementations #8730

Merged
merged 12 commits into from
May 31, 2023
Next Next commit
Improved Graph Implementations
Provides new implementation for graph_list.py and graph_matrix.py along with pytest suites for each. Fixes #8709
  • Loading branch information
nith2001 committed May 14, 2023
commit f520fb90895f30b53b3969997e42e93ff34e921d
224 changes: 107 additions & 117 deletions graphs/graph_list.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env python3
nith2001 marked this conversation as resolved.
Show resolved Hide resolved

# Author: OMKAR PATHAK, Nwachukwu Chidiebere
# Original Authors: OMKAR PATHAK, Nwachukwu Chidiebere
# Redesigned and reimplemented by Vikram Nithyanandam

# Use a Python dictionary to construct the graph.
# Use an adjacency list via Python dictionary to construct the graph.
from __future__ import annotations

from pprint import pformat
Expand All @@ -14,137 +15,126 @@
class GraphAdjacencyList(Generic[T]):
"""
Adjacency List type Graph Data Structure that accounts for directed and undirected
Graphs. Initialize graph object indicating whether it's directed or undirected.

Directed graph example:
>>> d_graph = GraphAdjacencyList()
>>> print(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: []}
>>> 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 example:
>>> 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]}
>>> char_graph = GraphAdjacencyList(directed=False)
>>> char_graph.add_edge('a', 'b')
{'a': ['b'], 'b': ['a']}
>>> char_graph.add_edge('b', 'c').add_edge('b', 'e').add_edge('b', 'f')
{'a': ['b'], 'b': ['a', 'c', 'e', 'f'], 'c': ['b'], 'e': ['b'], 'f': ['b']}
>>> char_graph
{'a': ['b'], 'b': ['a', 'c', 'e', 'f'], 'c': ['b'], 'e': ['b'], 'f': ['b']}
Graphs. Initialize graph object indicating whether it's directed or undirected.
"""

def __init__(self, directed: bool = True) -> None:
def __init__(
self, vertices: list[T] = [], edges: list[list[T]] = [], directed: bool = True
) -> None:
"""
Parameters:
directed: (bool) Indicates if graph is directed or undirected. Default is True.
"""

self.adj_list: dict[T, list[T]] = {} # dictionary of lists
self.adj_list: dict[T, list[T]] = {} # dictionary of lists of T
self.directed = directed

for vertex in vertices:
self.add_vertex(vertex)

for edge in edges:
if len(edge) != 2:
raise ValueError(f"Invalid input: {edge} is the wrong length.")
self.add_edge(edge[0], edge[1])

def add_vertex(self, vertex: T) -> GraphAdjacencyList[T]:
"""
Adds a vertex to the graph. If the given vertex already exists, a ValueError will
be thrown.
"""
if not self.contains_vertex(vertex):
self.adj_list[vertex] = []
else:
raise ValueError(f"Incorrect input: {vertex} is already in the graph.")

def add_edge(
self, source_vertex: T, destination_vertex: T
) -> GraphAdjacencyList[T]:
"""
Connects vertices together. Creates and Edge from source vertex to destination
vertex.
Vertices will be created if not found in graph
Creates an edge from source vertex to destination vertex. If any given vertex doesn't exist
or the edge already exists, a ValueError will be thrown.
"""

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
# vertices.
if source_vertex in self.adj_list and destination_vertex in self.adj_list:
self.adj_list[source_vertex].append(destination_vertex)
if (
self.contains_vertex(source_vertex)
and self.contains_vertex(destination_vertex)
and not self.contains_edge(source_vertex, destination_vertex)
):
self.adj_list[source_vertex].append(destination_vertex)
if not self.directed:
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]
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:
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:
raise ValueError(
f"Incorrect input: Either {source_vertex} or {destination_vertex} does not exist \
nith2001 marked this conversation as resolved.
Show resolved Hide resolved
OR the requested edge already exists between them."
)
return self

def remove_vertex(self, vertex: T) -> GraphAdjacencyList[T]:
"""
Removes the given vertex from the graph and deletes all incoming and outgoing edges from
the given vertex as well. If the given vertex does not exist, a ValueError will be thrown.
"""
if self.contains_vertex(vertex):
if not self.directed:
# If not directed, find all neighboring vertices and delete all references of
# edges connecting to the given vertex
for neighbor in self.adj_list[vertex]:
self.adj_list[neighbor].remove(vertex)
else:
self.adj_list[source_vertex] = [destination_vertex]
self.adj_list[destination_vertex] = []
# If directed, search all neighbors of all vertices and delete all references of
# edges connecting to the given vertex
for edge_list in self.adj_list.values():
if vertex in edge_list:
edge_list.remove(vertex)

return self
# Finally, delete the given vertex and all of its outgoing edge references
self.adj_list.pop(vertex)
else:
raise ValueError(f"Incorrect input: {vertex} does not exist in this graph.")

def remove_edge(
self, source_vertex: T, destination_vertex: T
) -> GraphAdjacencyList[T]:
"""
Removes the edge between the two vertices. If any given vertex doesn't exist
or the edge does not exist, a ValueError will be thrown.
"""
if (
self.contains_vertex(source_vertex)
and self.contains_vertex(destination_vertex)
and self.contains_edge(source_vertex, destination_vertex)
):
self.adj_list[source_vertex].remove(destination_vertex)
if not self.directed:
self.adj_list[destination_vertex].remove(source_vertex)
else:
raise ValueError(
f"Incorrect input: Either {source_vertex} or {destination_vertex} do not exist \
OR the requested edge does not exists between them."
)

def contains_vertex(self, vertex: T) -> bool:
"""
Returns True if the graph contains the vertex, False otherwise.
"""
return vertex in self.adj_list

def contains_edge(self, source_vertex: T, destination_vertex: T) -> bool:
"""
Returns True if the graph contains the edge from the source_vertex to the
destination_vertex, False otherwise. If any given vertex doesn't exist, a
ValueError will be thrown.
"""
if self.contains_vertex(source_vertex) and self.contains_vertex(
destination_vertex
):
return True if destination_vertex in self.adj_list[source_vertex] else False
else:
raise ValueError(
f"Incorrect input: Either {source_vertex} or {destination_vertex} does not exist."
)

def clear_graph(self) -> None:
self.adj_list: dict[T, list[T]] = {}

def __repr__(self) -> str:
return pformat(self.adj_list)
Loading