Skip to content

Commit 79ecb33

Browse files
authored
Update graphs_floyd_warshall.py
1 parent 0efe9a4 commit 79ecb33

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

graphs/graphs_floyd_warshall.py

+41-42
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,79 @@
11
# floyd_warshall.py
22
"""
3-
The problem is to find and return the shortest distance between all pairs of vertices in a
4-
weighted directed graph that can have negative edge weights.
3+
The problem is to find and return the shortest distance between all pairs of vertices
4+
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-
# 3. distance[i][j] = min(distance[i][j], distance[i][k] +distance[k][j]) for each possible pair i, j of vertices.
15-
16-
# 4. Step 3 repeated for k vertex in the graph.
17-
18-
# 5. Whenever distance[i][j] is given a new minimum value, next vertex[i][j] = next vertex[i][k].
19-
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,
18+
# next vertex[i][j] = next vertex[i][k].
19+
2020
"""
2121
:param graph: 2D array calculated from weight[edge[i, j]]
22-
22+
2323
:param v: number of vertices
24-
24+
2525
:return: shortest distance between all vertex pairs
26-
26+
2727
distance[u][v] will contain the shortest distance from vertex u to v.
28-
28+
2929
# doctests:
30-
30+
3131
>>> graph = [[0, 3, float('inf')], [2, 0, float('inf')],[float('inf'), 7, 0]]
3232
>>> floyd_warshall(graph, 3)[0]
3333
[[0, 3, inf], [2, 0, inf], [9, 7, 0]]
34-
34+
3535
>>> graph = [[0, 1, 4],[float('inf'), 0, 2],[float('inf'), float('inf'), 0]]
3636
>>> floyd_warshall(graph, 3)[0]
3737
[[0, 1, 3], [inf, 0, 2], [inf, inf, 0]]
38-
38+
3939
>>> graph = [[0, 0, 0],[0, 0, 0],[0, 0, 0]]
4040
>>> floyd_warshall(graph, 3)[0]
4141
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
42-
42+
4343
#Graph with all edge weights = infinity
44-
45-
>>> graph = [[float('inf'), float('inf'), float('inf')],[float('inf'), float('inf'), float('inf')],[float('inf'), float('inf'), float('inf')]]
44+
45+
>>> graph = [[float('inf'), float('inf'), float('inf')],
46+
... [float('inf'), float('inf'), float('inf')],
47+
... [float('inf'), float('inf'), float('inf')]]
4648
>>> floyd_warshall(graph, 3)[0]
4749
[[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]]
48-
49-
50+
51+
5052
#Handling negetive weighted graph:
51-
53+
5254
>>> graph = [[0, -2, float('inf')],[float('inf'), 0, 3],[4, float('inf'), 0]]
5355
>>> floyd_warshall(graph, 3)[0]
5456
[[0, -2, 1], [7, 0, 3], [4, 2, 0]]
55-
56-
57+
58+
5759
#Handling negetive weighted cycle:
58-
60+
5961
>>> graph = [[0, -1, float('inf')],[float('inf'), 0, -2],[-3, float('inf'), 0]]
6062
>>> floyd_warshall(graph, 3)[0]
6163
[[-6, -7, -9], [-5, -6, -8], [-9, -10, -12]]
62-
63-
64+
65+
6466
#Number of vertex in function arguement should match number of vertex in graph:
65-
67+
6668
>>> graph = [[0, -1, float('inf')],[float('inf'), 0, -2]]
6769
>>> floyd_warshall(graph, 3)[0]
6870
Traceback (most recent call last):
6971
...
7072
IndexError: list index out of range
71-
72-
73+
74+
7375
#Graph data type should be a 2D list:
74-
76+
7577
>>> graph = "strings not allowed"
7678
>>> floyd_warshall(graph, 3)[0]
7779
Traceback (most recent call last):
@@ -85,25 +87,22 @@ def floyd_warshall(graph: list[list[float]], vertex: int) -> tuple:
8587
for j in range(vertex):
8688
dist[i][j] = graph[i][j]
8789
# check vertex k against all other vertices (i, j)
88-
90+
8991
for k in range(vertex):
9092
# looping through rows of graph array
91-
93+
9294
for i in range(vertex):
9395
# looping through columns of graph array
94-
96+
9597
for j in range(vertex):
9698
if (
9799
dist[i][k] != float("inf")
98100
and dist[k][j] != float("inf")
99101
and dist[i][k] + dist[k][j] < dist[i][j]
100-
):
102+
):
101103
dist[i][j] = dist[i][k] + dist[k][j]
102-
103104
return dist, vertex
104105

105-
106-
if __name__ == "__main__":
106+
if __name__== "__main__":
107107
import doctest
108-
109108
doctest.testmod()

0 commit comments

Comments
 (0)