2024-10-25 16:00:10 +00:00
|
|
|
import heapq
|
|
|
|
import sys
|
|
|
|
|
2024-10-25 16:12:54 +00:00
|
|
|
|
|
|
|
# First implementation of johnson algorithm
|
|
|
|
# Steps followed to implement this algorithm is given in the below link:
|
|
|
|
# https://brilliant.org/wiki/johnsons-algorithm/
|
2024-10-25 16:00:10 +00:00
|
|
|
class JohnsonGraph:
|
2024-10-25 16:17:05 +00:00
|
|
|
def __init__(self) -> None:
|
2024-10-28 02:28:21 +00:00
|
|
|
"""
|
|
|
|
Initializes an empty graph with no edges.
|
|
|
|
"""
|
|
|
|
self.edges: list[tuple[str, str, int]] = []
|
|
|
|
self.graph: dict[str, list[tuple[str, int]]] = {}
|
2024-10-25 16:01:29 +00:00
|
|
|
|
|
|
|
# add vertices for a graph
|
2024-10-28 02:56:35 +00:00
|
|
|
def add_vertices(self, vertex: str) -> None:
|
2024-10-28 02:28:21 +00:00
|
|
|
"""
|
|
|
|
Adds a vertex `u` to the graph with an empty adjacency list.
|
|
|
|
"""
|
2024-10-28 02:56:35 +00:00
|
|
|
self.graph[vertex] = []
|
2024-10-25 16:01:29 +00:00
|
|
|
|
|
|
|
# assign weights for each edges formed of the directed graph
|
2024-10-28 02:56:35 +00:00
|
|
|
def add_edge(self, vertex_a: str, vertex_b: str, weight: int) -> None:
|
2024-10-28 02:28:21 +00:00
|
|
|
"""
|
|
|
|
Adds a directed edge from vertex `u` to vertex `v` with weight `w`.
|
|
|
|
"""
|
2024-10-28 02:56:35 +00:00
|
|
|
self.edges.append((vertex_a, vertex_b, weight))
|
|
|
|
self.graph[vertex_a].append((vertex_b, weight))
|
2024-10-25 16:00:10 +00:00
|
|
|
|
2024-10-25 16:01:29 +00:00
|
|
|
# perform a dijkstra algorithm on a directed graph
|
2024-10-28 02:56:35 +00:00
|
|
|
def dijkstra(self, start: str) -> dict:
|
2024-10-28 02:28:21 +00:00
|
|
|
"""
|
2024-10-28 02:43:44 +00:00
|
|
|
Computes the shortest path from vertex `s`
|
2024-10-28 02:43:18 +00:00
|
|
|
to all other vertices using Dijkstra's algorithm.
|
2024-10-28 02:28:21 +00:00
|
|
|
"""
|
2024-10-25 16:19:06 +00:00
|
|
|
distances = {vertex: sys.maxsize - 1 for vertex in self.graph}
|
2024-10-28 02:56:35 +00:00
|
|
|
pq = [(0, start)]
|
|
|
|
distances[start] = 0
|
2024-10-25 16:00:10 +00:00
|
|
|
while pq:
|
2024-10-28 02:56:35 +00:00
|
|
|
weight, vertex = heapq.heappop(pq)
|
2024-10-25 16:01:29 +00:00
|
|
|
|
2024-10-28 02:56:35 +00:00
|
|
|
if weight > distances[vertex]:
|
2024-10-25 16:00:10 +00:00
|
|
|
continue
|
2024-10-25 16:01:29 +00:00
|
|
|
|
2024-10-28 02:56:35 +00:00
|
|
|
for node, node_weight in self.graph[vertex]:
|
|
|
|
if distances[vertex] + node_weight < distances[node]:
|
|
|
|
distances[node] = distances[vertex] + node_weight
|
2024-10-25 16:01:29 +00:00
|
|
|
heapq.heappush(pq, (distances[node], node))
|
2024-10-25 16:00:10 +00:00
|
|
|
return distances
|
|
|
|
|
2024-10-25 16:19:06 +00:00
|
|
|
# carry out the bellman ford algorithm for a node and estimate its distance vector
|
2024-10-28 02:56:35 +00:00
|
|
|
def bellman_ford(self, start: str) -> dict:
|
2024-10-28 02:28:21 +00:00
|
|
|
"""
|
2024-10-28 02:43:44 +00:00
|
|
|
Computes the shortest path from vertex `s`
|
2024-10-28 02:43:18 +00:00
|
|
|
to all other vertices using the Bellman-Ford algorithm.
|
2024-10-28 02:28:21 +00:00
|
|
|
"""
|
2024-10-25 16:19:06 +00:00
|
|
|
distances = {vertex: sys.maxsize - 1 for vertex in self.graph}
|
2024-10-28 02:56:35 +00:00
|
|
|
distances[start] = 0
|
2024-10-25 16:01:29 +00:00
|
|
|
|
2024-10-28 02:56:35 +00:00
|
|
|
for vertex_a in self.graph:
|
|
|
|
for vertex_a, vertex_b, weight in self.edges:
|
2024-10-28 02:57:01 +00:00
|
|
|
if (
|
|
|
|
distances[vertex_a] != sys.maxsize - 1
|
|
|
|
and distances[vertex_a] + weight < distances[vertex_b]
|
|
|
|
):
|
2024-10-28 02:56:35 +00:00
|
|
|
distances[vertex_b] = distances[vertex_a] + weight
|
2024-10-25 16:00:10 +00:00
|
|
|
|
|
|
|
return distances
|
2024-10-25 16:12:54 +00:00
|
|
|
|
|
|
|
# perform the johnson algorithm to handle the negative weights that
|
2024-10-25 16:04:37 +00:00
|
|
|
# could not be handled by either the dijkstra
|
2024-10-25 16:19:06 +00:00
|
|
|
# or the bellman ford algorithm efficiently
|
2024-10-28 02:17:56 +00:00
|
|
|
def johnson_algo(self) -> list[dict]:
|
2024-10-28 02:28:21 +00:00
|
|
|
"""
|
2024-10-28 02:43:44 +00:00
|
|
|
Computes the shortest paths between
|
2024-10-28 02:43:18 +00:00
|
|
|
all pairs of vertices using Johnson's algorithm.
|
2024-10-28 02:28:21 +00:00
|
|
|
"""
|
2024-10-25 16:00:10 +00:00
|
|
|
self.add_vertices("#")
|
2024-10-28 02:56:35 +00:00
|
|
|
for vertex in self.graph:
|
|
|
|
if vertex != "#":
|
|
|
|
self.add_edge("#", vertex, 0)
|
2024-10-25 16:01:29 +00:00
|
|
|
|
2024-10-28 02:56:35 +00:00
|
|
|
hash_path = self.bellman_ford("#")
|
2024-10-25 16:01:29 +00:00
|
|
|
|
2024-10-25 16:00:10 +00:00
|
|
|
for i in range(len(self.edges)):
|
2024-10-28 02:56:35 +00:00
|
|
|
vertex_a, vertex_b, weight = self.edges[i]
|
2024-10-28 02:57:01 +00:00
|
|
|
self.edges[i] = (
|
|
|
|
vertex_a,
|
|
|
|
vertex_b,
|
|
|
|
weight + hash_path[vertex_a] - hash_path[vertex_b],
|
|
|
|
)
|
2024-10-28 03:07:05 +00:00
|
|
|
self.edges[i] = (
|
|
|
|
vertex_a,
|
|
|
|
vertex_b,
|
|
|
|
weight + hash_path[vertex_a] - hash_path[vertex_b],
|
|
|
|
)
|
2024-10-25 16:00:10 +00:00
|
|
|
|
|
|
|
self.graph.pop("#")
|
2024-10-28 02:57:01 +00:00
|
|
|
self.edges = [
|
|
|
|
(vertex1, vertex2, node_weight)
|
|
|
|
for vertex1, vertex2, node_weight in self.edges
|
|
|
|
if vertex1 != "#"
|
|
|
|
]
|
2024-10-28 03:02:29 +00:00
|
|
|
filtered_edges = []
|
|
|
|
for vertex1, vertex2, node_weight in self.edges:
|
|
|
|
if vertex1 != "#":
|
|
|
|
filtered_edges.append((vertex1, vertex2, node_weight))
|
|
|
|
self.edges = filtered_edges
|
2024-10-25 16:01:29 +00:00
|
|
|
|
2024-10-28 02:56:35 +00:00
|
|
|
for vertex in self.graph:
|
2024-10-28 02:57:01 +00:00
|
|
|
self.graph[vertex] = [
|
|
|
|
(vertex2, node_weight)
|
|
|
|
for vertex1, vertex2, node_weight in self.edges
|
|
|
|
if vertex1 == vertex
|
|
|
|
]
|
2024-10-25 16:01:29 +00:00
|
|
|
|
2024-10-28 03:02:29 +00:00
|
|
|
filtered_neighbors = []
|
|
|
|
for vertex1, vertex2, node_weight in self.edges:
|
|
|
|
if vertex1 == vertex:
|
|
|
|
filtered_neighbors.append((vertex2, node_weight))
|
|
|
|
self.graph[vertex] = filtered_neighbors
|
2024-10-28 03:07:05 +00:00
|
|
|
|
2024-10-25 16:00:10 +00:00
|
|
|
distances = []
|
2024-10-28 02:56:35 +00:00
|
|
|
for vertex1 in self.graph:
|
|
|
|
new_dist = self.dijkstra(vertex1)
|
|
|
|
for vertex2 in self.graph:
|
|
|
|
if new_dist[vertex2] < sys.maxsize - 1:
|
|
|
|
new_dist[vertex2] += hash_path[vertex1] - hash_path[vertex2]
|
2024-10-25 16:00:10 +00:00
|
|
|
distances.append(new_dist)
|
|
|
|
return distances
|
2024-10-25 16:01:29 +00:00
|
|
|
|
|
|
|
|
2024-10-25 16:00:10 +00:00
|
|
|
g = JohnsonGraph()
|
2024-10-25 16:01:29 +00:00
|
|
|
# this a complete connected graph
|
2024-10-25 16:00:10 +00:00
|
|
|
g.add_vertices("A")
|
|
|
|
g.add_vertices("B")
|
|
|
|
g.add_vertices("C")
|
|
|
|
g.add_vertices("D")
|
|
|
|
g.add_vertices("E")
|
|
|
|
|
|
|
|
g.add_edge("A", "B", 1)
|
|
|
|
g.add_edge("A", "C", 3)
|
|
|
|
g.add_edge("B", "D", 4)
|
|
|
|
g.add_edge("D", "E", 2)
|
|
|
|
g.add_edge("E", "C", -2)
|
|
|
|
|
|
|
|
|
|
|
|
optimal_paths = g.johnson_algo()
|
|
|
|
print("Print all optimal paths of a graph using Johnson Algorithm")
|
|
|
|
for i, row in enumerate(optimal_paths):
|
2024-10-25 16:01:29 +00:00
|
|
|
print(f"{i}: {row}")
|