1# Number of vertices
2nV = 4
3INF = 999
4
5# Algorithm
6def floyd(G):
7 dist = list(map(lambda p: list(map(lambda q: q, p)), G))
8
9 # Adding vertices individually
10 for r in range(nV):
11 for p in range(nV):
12 for q in range(nV):
13 dist[p][q] = min(dist[p][q], dist[p][r] + dist[r][q])
14 sol(dist)
15
16# Printing the output
17def sol(dist):
18 for p in range(nV):
19 for q in range(nV):
20 if(dist[p][q] == INF):
21 print("INF", end=" ")
22 else:
23 print(dist[p][q], end=" ")
24 print(" ")
25
26G = [[0, 5, INF, INF],
27 [50, 0, 15, 5],
28 [30, INF, 0, 15],
29 [15, INF, 5, 0]]
30floyd(G)