Skip to content

Commit e3db05b

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

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

graphs/johnson_graph.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import heapq
22
import sys
33

4-
#First implementation of johnson algorithm
5-
#Steps followed to implement this algorithm is given in the below link:
6-
#https://brilliant.org/wiki/johnsons-algorithm/
4+
5+
# First implementation of johnson algorithm
6+
# Steps followed to implement this algorithm is given in the below link:
7+
# https://brilliant.org/wiki/johnsons-algorithm/
78
class JohnsonGraph:
89
def __init__(self):
910
self.edges = []
@@ -20,9 +21,9 @@ def add_edge(self, u, v, w):
2021

2122
# perform a dijkstra algorithm on a directed graph
2223
def dijkstra(self, s):
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)
@@ -36,9 +37,9 @@ def dijkstra(self, s):
3637
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):
41-
distances = {vertex: sys.maxsize-1 for vertex in self.graph}
40+
# carry out the bellman ford algorithm for a node and estimate its distance vector
41+
def bellman_ford(self, s):
42+
distances = {vertex: sys.maxsize - 1 for vertex in self.graph}
4243
distances[s] = 0
4344

4445
for u in self.graph:
@@ -47,10 +48,10 @@ def bellman_ford(self, s):
4748
distances[v] = distances[u] + w
4849

4950
return distances
50-
51-
#perform the johnson algorithm to handle the negative weights that
51+
52+
# perform the johnson algorithm to handle the negative weights that
5253
# could not be handled by either the dijkstra
53-
#or the bellman ford algorithm efficiently
54+
# or the bellman ford algorithm efficiently
5455
def johnson_algo(self):
5556
self.add_vertices("#")
5657
for v in self.graph:

0 commit comments

Comments
 (0)