2024-10-25 16:00:10 +00:00
|
|
|
from collections import deque
|
|
|
|
import heapq
|
|
|
|
import sys
|
|
|
|
|
2024-10-25 16:01:29 +00:00
|
|
|
|
|
|
|
# First implementation of johnson algorithm
|
2024-10-25 16:00:10 +00:00
|
|
|
class JohnsonGraph:
|
|
|
|
def __init__(self):
|
|
|
|
self.edges = []
|
|
|
|
self.graph = {}
|
2024-10-25 16:01:29 +00:00
|
|
|
|
|
|
|
# add vertices for a graph
|
2024-10-25 16:00:10 +00:00
|
|
|
def add_vertices(self, u):
|
|
|
|
self.graph[u] = []
|
2024-10-25 16:01:29 +00:00
|
|
|
|
|
|
|
# assign weights for each edges formed of the directed graph
|
2024-10-25 16:00:10 +00:00
|
|
|
def add_edge(self, u, v, w):
|
|
|
|
self.edges.append((u, v, w))
|
2024-10-25 16:01:29 +00:00
|
|
|
self.graph[u].append((v, w))
|
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-25 16:00:10 +00:00
|
|
|
def dijkstra(self, s):
|
|
|
|
no_v = len(self.graph)
|
2024-10-25 16:01:29 +00:00
|
|
|
distances = {vertex: sys.maxsize - 1 for vertex in self.graph}
|
|
|
|
pq = [(0, s)]
|
|
|
|
|
2024-10-25 16:00:10 +00:00
|
|
|
distances[s] = 0
|
|
|
|
while pq:
|
|
|
|
weight, v = heapq.heappop(pq)
|
2024-10-25 16:01:29 +00:00
|
|
|
|
2024-10-25 16:00:10 +00:00
|
|
|
if weight > distances[v]:
|
|
|
|
continue
|
2024-10-25 16:01:29 +00:00
|
|
|
|
2024-10-25 16:00:10 +00:00
|
|
|
for node, w in self.graph[v]:
|
2024-10-25 16:01:29 +00:00
|
|
|
if distances[v] + w < distances[node]:
|
|
|
|
distances[node] = distances[v] + w
|
|
|
|
heapq.heappush(pq, (distances[node], node))
|
2024-10-25 16:00:10 +00:00
|
|
|
return distances
|
|
|
|
|
2024-10-25 16:01:29 +00:00
|
|
|
# carry out the bellman ford algorithm for a node and estimate its distance vector
|
|
|
|
def bellman_ford(self, s):
|
2024-10-25 16:00:10 +00:00
|
|
|
no_v = len(self.graph)
|
2024-10-25 16:01:29 +00:00
|
|
|
distances = {vertex: sys.maxsize - 1 for vertex in self.graph}
|
2024-10-25 16:00:10 +00:00
|
|
|
distances[s] = 0
|
2024-10-25 16:01:29 +00:00
|
|
|
|
2024-10-25 16:00:10 +00:00
|
|
|
for u in self.graph:
|
|
|
|
for u, v, w in self.edges:
|
2024-10-25 16:01:29 +00:00
|
|
|
if distances[u] != sys.maxsize - 1 and distances[u] + w < distances[v]:
|
|
|
|
distances[v] = distances[u] + w
|
2024-10-25 16:00:10 +00:00
|
|
|
|
|
|
|
return distances
|
2024-10-25 16:01:29 +00:00
|
|
|
|
|
|
|
# perform the johnson algorithm to handle the negative weights that could not be handled by either the dijkstra
|
|
|
|
# or the bellman ford algorithm efficiently
|
2024-10-25 16:00:10 +00:00
|
|
|
def johnson_algo(self):
|
|
|
|
self.add_vertices("#")
|
|
|
|
for v in self.graph:
|
|
|
|
if v != "#":
|
|
|
|
self.add_edge("#", v, 0)
|
2024-10-25 16:01:29 +00:00
|
|
|
|
2024-10-25 16:00:10 +00:00
|
|
|
n = 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)):
|
|
|
|
u, v, weight = self.edges[i]
|
|
|
|
self.edges[i] = (u, v, weight + n[u] - n[v])
|
|
|
|
|
|
|
|
self.graph.pop("#")
|
|
|
|
self.edges = [(u, v, w) for u, v, w in self.edges if u != "#"]
|
2024-10-25 16:01:29 +00:00
|
|
|
|
2024-10-25 16:00:10 +00:00
|
|
|
for u in self.graph:
|
|
|
|
self.graph[u] = [(v, weight) for x, v, weight in self.edges if x == u]
|
2024-10-25 16:01:29 +00:00
|
|
|
|
2024-10-25 16:00:10 +00:00
|
|
|
distances = []
|
|
|
|
for u in self.graph:
|
|
|
|
new_dist = self.dijkstra(u)
|
|
|
|
for v in self.graph:
|
2024-10-25 16:01:29 +00:00
|
|
|
if new_dist[v] < sys.maxsize - 1:
|
2024-10-25 16:00:10 +00:00
|
|
|
new_dist[v] += n[v] - n[u]
|
|
|
|
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}")
|