Merge branch 'joel_graph' of https://github.com/joelkurien/Python into joel_graph

This commit is contained in:
Joelkurien 2024-10-28 14:43:33 +11:00
commit 81fb07bf82

View File

@ -32,7 +32,7 @@ class JohnsonGraph:
# assign weights for each edges formed of the directed graph # assign weights for each edges formed of the directed graph
def add_edge(self, vertex_a: str, vertex_b: str, weight: int) -> None: def add_edge(self, vertex_a: str, vertex_b: str, weight: int) -> None:
""" """
Adds a directed edge from vertex `vertex_a` Adds a directed edge from vertex `vertex_a`
to vertex `vertex_b` with weight `weight`. to vertex `vertex_b` with weight `weight`.
>>> g = JohnsonGraph() >>> g = JohnsonGraph()
>>> g.add_vertices("A") >>> g.add_vertices("A")
@ -49,7 +49,7 @@ class JohnsonGraph:
# perform a dijkstra algorithm on a directed graph # perform a dijkstra algorithm on a directed graph
def dijkstra(self, start: str) -> dict: def dijkstra(self, start: str) -> dict:
""" """
Computes the shortest path from vertex `start` Computes the shortest path from vertex `start`
to all other vertices using Dijkstra's algorithm. to all other vertices using Dijkstra's algorithm.
>>> g = JohnsonGraph() >>> g = JohnsonGraph()
>>> g.add_vertices("A") >>> g.add_vertices("A")
@ -80,7 +80,7 @@ class JohnsonGraph:
# carry out the bellman ford algorithm for a node and estimate its distance vector # carry out the bellman ford algorithm for a node and estimate its distance vector
def bellman_ford(self, start: str) -> dict: def bellman_ford(self, start: str) -> dict:
""" """
Computes the shortest path from vertex `start` to Computes the shortest path from vertex `start` to
all other vertices using the Bellman-Ford algorithm. all other vertices using the Bellman-Ford algorithm.
>>> g = JohnsonGraph() >>> g = JohnsonGraph()
>>> g.add_vertices("A") >>> g.add_vertices("A")
@ -111,7 +111,7 @@ class JohnsonGraph:
# or the bellman ford algorithm efficiently # or the bellman ford algorithm efficiently
def johnson_algo(self) -> list[dict]: def johnson_algo(self) -> list[dict]:
""" """
Computes the shortest paths between Computes the shortest paths between
all pairs of vertices using Johnson's algorithm all pairs of vertices using Johnson's algorithm
for a directed graph. for a directed graph.
>>> g = JohnsonGraph() >>> g = JohnsonGraph()
@ -159,10 +159,10 @@ class JohnsonGraph:
for vertex1 in self.graph: for vertex1 in self.graph:
new_dist = self.dijkstra(vertex1) new_dist = self.dijkstra(vertex1)
for vertex2 in self.graph: for vertex2 in self.graph:
if new_dist[vertex2] < sys.maxsize-1: if new_dist[vertex2] < sys.maxsize - 1:
new_dist[vertex2] += hash_path[vertex2] - hash_path[vertex1] new_dist[vertex2] += hash_path[vertex2] - hash_path[vertex1]
for key in new_dist: for key in new_dist:
if new_dist[key] == sys.maxsize-1: if new_dist[key] == sys.maxsize - 1:
new_dist[key] = None new_dist[key] = None
distances.append(new_dist) distances.append(new_dist)
return distances return distances