Skip to content

Commit f58a653

Browse files
authored
Update graphs_floyd_warshall.py
1 parent e2cc729 commit f58a653

File tree

1 file changed

+37
-40
lines changed

1 file changed

+37
-40
lines changed

graphs/graphs_floyd_warshall.py

+37-40
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,79 @@
11
# floyd_warshall.py
22
"""
3-
The problem is to find and return the shortest distance between all pairs of vertices
3+
The problem is to find and return the shortest distance between all pairs of vertices
44
in a weighted directed graph that can have negative edge weights.
55
66
https://docs.python.org/3/library/doctest.html
77
88
"""
99

10-
11-
def floyd_warshall(graph: list[list[float]], vertex: int) -> tuple:
12-
# 1. For all edges from v to n, distance[i][j] = weight(edge(i, j)).
13-
14-
# 2. distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j]).
15-
16-
# 3. Step 2 is true for each pair of vertices.Repeat for k vertex in the graph.
17-
18-
# 4. Whenever distance[i][j] is given a new minimum value,
10+
def floyd_warshall(graph:list[list[float]], vertex:int) -> tuple:
11+
#1. For all edges from v to n, distance[i][j] = weight(edge(i, j)).
12+
13+
#2. distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j]).
14+
15+
#3. Step 2 is true for each pair of vertices.Repeat for k vertex in the graph.
16+
17+
#4. Whenever distance[i][j] is given a new minimum value,
1918
# next vertex[i][j] = next vertex[i][k].
20-
19+
2120
"""
2221
:param graph: 2D array calculated from weight[edge[i, j]]
23-
22+
2423
:param v: number of vertices
25-
24+
2625
:return: shortest distance between all vertex pairs
27-
26+
2827
distance[u][v] will contain the shortest distance from vertex u to v.
29-
28+
3029
# doctests:
31-
30+
3231
>>> graph = [[0, 3, float('inf')], [2, 0, float('inf')],[float('inf'), 7, 0]]
3332
>>> floyd_warshall(graph, 3)[0]
3433
[[0, 3, inf], [2, 0, inf], [9, 7, 0]]
35-
34+
3635
>>> graph = [[0, 1, 4],[float('inf'), 0, 2],[float('inf'), float('inf'), 0]]
3736
>>> floyd_warshall(graph, 3)[0]
3837
[[0, 1, 3], [inf, 0, 2], [inf, inf, 0]]
39-
38+
4039
>>> graph = [[0, 0, 0],[0, 0, 0],[0, 0, 0]]
4140
>>> floyd_warshall(graph, 3)[0]
4241
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
43-
42+
4443
#Graph with all edge weights = infinity
45-
44+
4645
>>> graph = [[float('inf'), float('inf'), float('inf')],
4746
... [float('inf'), float('inf'), float('inf')],
4847
... [float('inf'), float('inf'), float('inf')]]
4948
>>> floyd_warshall(graph, 3)[0]
5049
[[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]]
51-
52-
50+
51+
5352
#Handling negetive weighted graph:
54-
53+
5554
>>> graph = [[0, -2, float('inf')],[float('inf'), 0, 3],[4, float('inf'), 0]]
5655
>>> floyd_warshall(graph, 3)[0]
5756
[[0, -2, 1], [7, 0, 3], [4, 2, 0]]
58-
59-
57+
58+
6059
#Handling negetive weighted cycle:
61-
60+
6261
>>> graph = [[0, -1, float('inf')],[float('inf'), 0, -2],[-3, float('inf'), 0]]
6362
>>> floyd_warshall(graph, 3)[0]
6463
[[-6, -7, -9], [-5, -6, -8], [-9, -10, -12]]
65-
66-
67-
#Number of vertex in function arguement should match number of vertex in graph:
68-
64+
65+
66+
#Number of vertex in function argument should match number of vertex in graph:
67+
6968
>>> graph = [[0, -1, float('inf')],[float('inf'), 0, -2]]
7069
>>> floyd_warshall(graph, 3)[0]
7170
Traceback (most recent call last):
7271
...
7372
IndexError: list index out of range
74-
75-
73+
74+
7675
#Graph data type should be a 2D list:
77-
76+
7877
>>> graph = "strings not allowed"
7978
>>> floyd_warshall(graph, 3)[0]
8079
Traceback (most recent call last):
@@ -88,24 +87,22 @@ def floyd_warshall(graph: list[list[float]], vertex: int) -> tuple:
8887
for j in range(vertex):
8988
dist[i][j] = graph[i][j]
9089
# check vertex k against all other vertices (i, j)
91-
90+
9291
for k in range(vertex):
9392
# looping through rows of graph array
94-
93+
9594
for i in range(vertex):
9695
# looping through columns of graph array
97-
96+
9897
for j in range(vertex):
9998
if (
10099
dist[i][k] != float("inf")
101100
and dist[k][j] != float("inf")
102101
and dist[i][k] + dist[k][j] < dist[i][j]
103-
):
102+
):
104103
dist[i][j] = dist[i][k] + dist[k][j]
105104
return dist, vertex
106105

107-
108-
if __name__ == "__main__":
106+
if __name__== "__main__":
109107
import doctest
110-
111108
doctest.testmod()

0 commit comments

Comments
 (0)