Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e767c24

Browse files
authoredJan 24, 2025··
Changed definition of G
Update floyd_warshall.py
1 parent 1ffd336 commit e767c24

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed
 

‎graphs/floyd_warshall.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@
1212
Wiki page:- <https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm>
1313
"""
1414

15-
1615
def floyd_warshall(graph, n):
1716
"""
1817
Returns the shortest distance between all pairs of nodes
1918
20-
>>> floyd_warshall(G1, 6)
19+
>>> INF = 999999
20+
>>> G = []
21+
>>> G.append([0, 2, INF, INF, INF, 3])
22+
>>> G.append([2, 0, 2, INF, INF, INF])
23+
>>> G.append([INF, 2, 0, 1, INF, INF])
24+
>>> G.append([INF, INF, 1, 0, 1, INF])
25+
>>> G.append([INF, INF, INF, 1, 0, 5])
26+
>>> G.append([3, INF, INF, INF, 5, 0])
27+
>>> floyd_warshall(G, 6)
2128
[\
2229
[0, 2, 4, 5, 6, 3], \
2330
[2, 0, 2, 3, 4, 5], \
@@ -35,19 +42,9 @@ def floyd_warshall(graph, n):
3542
distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j])
3643
return distance
3744

38-
39-
if __name__ == "__main__":
40-
INF = 999999
41-
G1 = [
42-
[0, 2, INF, INF, INF, 3],
43-
[2, 0, 2, INF, INF, INF],
44-
[INF, 2, 0, 1, INF, INF],
45-
[INF, INF, 1, 0, 1, INF],
46-
[INF, INF, INF, 1, 0, 5],
47-
[3, INF, INF, INF, 5, 0],
48-
]
45+
if __name__ == '__main__':
4946
"""
50-
Layout of G1:-
47+
Layout of G:-
5148
2 2 1 1 5
5249
(1) <-----> (2) <-----> (3) <-----> (4) <-----> (5) <-----> (6)
5350
/\\ /\\

0 commit comments

Comments
 (0)
Please sign in to comment.