Advertisement
Eternoseeker

S-Ass3-FloydWarshall

Nov 24th, 2023
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | Source Code | 0 0
  1. # Number of vertices
  2. nV = 4
  3. INF = 999
  4. # Algorithm
  5. def floyd(G):
  6.     dist = list(map(lambda p: list(map(lambda q: q, p)), G))
  7.     # Adding vertices individually
  8.     for r in range(nV):
  9.         for p in range(nV):
  10.             for q in range(nV):
  11.                 dist[p][q] = min(dist[p][q], dist[p][r] + dist[r][q])
  12.     sol(dist)
  13.  
  14. # Printing the output
  15. def sol(dist):
  16.     for p in range(nV):
  17.         for q in range(nV):
  18.             if(dist[p][q] == INF):
  19.                 print("INF", end=" ")
  20.             else:
  21.                 print(dist[p][q], end="  ")
  22.         print(" ")
  23.  
  24. G = [[0, 5, INF, INF],
  25.          [50, 0, 15, 5],
  26.          [30, INF, 0, 15],
  27.          [15, INF, 5, 0]]
  28. print("The initial matrix is: ")
  29. sol(G)
  30. print("After Floyd Warshall Algorithm: ")
  31. floyd(G)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement