Skip to content

Commit 18319a8

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent da0717b commit 18319a8

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

graphs/johnson_graph.py

+34-33
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,86 @@
22
import heapq
33
import sys
44

5-
#First implementation of johnson algorithm
5+
6+
# First implementation of johnson algorithm
67
class JohnsonGraph:
78
def __init__(self):
89
self.edges = []
910
self.graph = {}
10-
11-
#add vertices for a graph
11+
12+
# add vertices for a graph
1213
def add_vertices(self, u):
1314
self.graph[u] = []
14-
15-
#assign weights for each edges formed of the directed graph
15+
16+
# assign weights for each edges formed of the directed graph
1617
def add_edge(self, u, v, w):
1718
self.edges.append((u, v, w))
18-
self.graph[u].append((v,w))
19+
self.graph[u].append((v, w))
1920

20-
#perform a dijkstra algorithm on a directed graph
21+
# perform a dijkstra algorithm on a directed graph
2122
def dijkstra(self, s):
2223
no_v = len(self.graph)
23-
distances = {vertex: sys.maxsize-1 for vertex in self.graph}
24-
pq = [(0,s)]
25-
24+
distances = {vertex: sys.maxsize - 1 for vertex in self.graph}
25+
pq = [(0, s)]
26+
2627
distances[s] = 0
2728
while pq:
2829
weight, v = heapq.heappop(pq)
29-
30+
3031
if weight > distances[v]:
3132
continue
32-
33+
3334
for node, w in self.graph[v]:
34-
if distances[v]+w < distances[node]:
35-
distances[node] = distances[v]+w
36-
heapq.heappush(pq, (distances[node], node))
35+
if distances[v] + w < distances[node]:
36+
distances[node] = distances[v] + w
37+
heapq.heappush(pq, (distances[node], node))
3738
return distances
3839

39-
#carry out the bellman ford algorithm for a node and estimate its distance vector
40-
def bellman_ford(self, s):
40+
# carry out the bellman ford algorithm for a node and estimate its distance vector
41+
def bellman_ford(self, s):
4142
no_v = len(self.graph)
42-
distances = {vertex: sys.maxsize-1 for vertex in self.graph}
43+
distances = {vertex: sys.maxsize - 1 for vertex in self.graph}
4344
distances[s] = 0
44-
45+
4546
for u in self.graph:
4647
for u, v, w in self.edges:
47-
if distances[u] != sys.maxsize-1 and distances[u]+w<distances[v]:
48-
distances[v] = distances[u]+w
48+
if distances[u] != sys.maxsize - 1 and distances[u] + w < distances[v]:
49+
distances[v] = distances[u] + w
4950

5051
return distances
51-
52-
#perform the johnson algorithm to handle the negative weights that could not be handled by either the dijkstra
53-
#or the bellman ford algorithm efficiently
52+
53+
# perform the johnson algorithm to handle the negative weights that could not be handled by either the dijkstra
54+
# or the bellman ford algorithm efficiently
5455
def johnson_algo(self):
55-
5656
self.add_vertices("#")
5757
for v in self.graph:
5858
if v != "#":
5959
self.add_edge("#", v, 0)
60-
60+
6161
n = self.bellman_ford("#")
62-
62+
6363
for i in range(len(self.edges)):
6464
u, v, weight = self.edges[i]
6565
self.edges[i] = (u, v, weight + n[u] - n[v])
6666

6767
self.graph.pop("#")
6868
self.edges = [(u, v, w) for u, v, w in self.edges if u != "#"]
69-
69+
7070
for u in self.graph:
7171
self.graph[u] = [(v, weight) for x, v, weight in self.edges if x == u]
72-
72+
7373
distances = []
7474
for u in self.graph:
7575
new_dist = self.dijkstra(u)
7676
for v in self.graph:
77-
if new_dist[v] < sys.maxsize-1:
77+
if new_dist[v] < sys.maxsize - 1:
7878
new_dist[v] += n[v] - n[u]
7979
distances.append(new_dist)
8080
return distances
81-
81+
82+
8283
g = JohnsonGraph()
83-
#this a complete connected graph
84+
# this a complete connected graph
8485
g.add_vertices("A")
8586
g.add_vertices("B")
8687
g.add_vertices("C")
@@ -97,4 +98,4 @@ def johnson_algo(self):
9798
optimal_paths = g.johnson_algo()
9899
print("Print all optimal paths of a graph using Johnson Algorithm")
99100
for i, row in enumerate(optimal_paths):
100-
print(f"{i}: {row}")
101+
print(f"{i}: {row}")

0 commit comments

Comments
 (0)