Skip to content

Commit 989d84e

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

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

graphs/graphs_floyd_warshall.py

+39-36
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,80 @@
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-
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,
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,
1819
# next vertex[i][j] = next vertex[i][k].
19-
20+
2021
"""
2122
:param graph: 2D array calculated from weight[edge[i, j]]
22-
23+
2324
:param v: number of vertices
24-
25+
2526
:return: shortest distance between all vertex pairs
26-
27+
2728
distance[u][v] will contain the shortest distance from vertex u to v.
28-
29+
2930
# doctests:
30-
31+
3132
>>> graph = [[0, 3, float('inf')], [2, 0, float('inf')],[float('inf'), 7, 0]]
3233
>>> floyd_warshall(graph, 3)[0]
3334
[[0, 3, inf], [2, 0, inf], [9, 7, 0]]
34-
35+
3536
>>> graph = [[0, 1, 4],[float('inf'), 0, 2],[float('inf'), float('inf'), 0]]
3637
>>> floyd_warshall(graph, 3)[0]
3738
[[0, 1, 3], [inf, 0, 2], [inf, inf, 0]]
38-
39+
3940
>>> graph = [[0, 0, 0],[0, 0, 0],[0, 0, 0]]
4041
>>> floyd_warshall(graph, 3)[0]
4142
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
42-
43+
4344
#Graph with all edge weights = infinity
44-
45+
4546
>>> graph = [[float('inf'), float('inf'), float('inf')],
4647
... [float('inf'), float('inf'), float('inf')],
4748
... [float('inf'), float('inf'), float('inf')]]
4849
>>> floyd_warshall(graph, 3)[0]
4950
[[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]]
50-
51-
51+
52+
5253
#Handling negetive weighted graph:
53-
54+
5455
>>> graph = [[0, -2, float('inf')],[float('inf'), 0, 3],[4, float('inf'), 0]]
5556
>>> floyd_warshall(graph, 3)[0]
5657
[[0, -2, 1], [7, 0, 3], [4, 2, 0]]
57-
58-
58+
59+
5960
#Handling negetive weighted cycle:
60-
61+
6162
>>> graph = [[0, -1, float('inf')],[float('inf'), 0, -2],[-3, float('inf'), 0]]
6263
>>> floyd_warshall(graph, 3)[0]
6364
[[-6, -7, -9], [-5, -6, -8], [-9, -10, -12]]
64-
65-
65+
66+
6667
#Number of vertex in function argument should match number of vertex in graph:
67-
68+
6869
>>> graph = [[0, -1, float('inf')],[float('inf'), 0, -2]]
6970
>>> floyd_warshall(graph, 3)[0]
7071
Traceback (most recent call last):
7172
...
7273
IndexError: list index out of range
73-
74-
74+
75+
7576
#Graph data type should be a 2D list:
76-
77+
7778
>>> graph = "strings not allowed"
7879
>>> floyd_warshall(graph, 3)[0]
7980
Traceback (most recent call last):
@@ -87,22 +88,24 @@ def floyd_warshall(graph:list[list[float]], vertex:int) -> tuple:
8788
for j in range(vertex):
8889
dist[i][j] = graph[i][j]
8990
# check vertex k against all other vertices (i, j)
90-
91+
9192
for k in range(vertex):
9293
# looping through rows of graph array
93-
94+
9495
for i in range(vertex):
9596
# looping through columns of graph array
96-
97+
9798
for j in range(vertex):
9899
if (
99100
dist[i][k] != float("inf")
100101
and dist[k][j] != float("inf")
101102
and dist[i][k] + dist[k][j] < dist[i][j]
102-
):
103+
):
103104
dist[i][j] = dist[i][k] + dist[k][j]
104105
return dist, vertex
105106

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

0 commit comments

Comments
 (0)