Advertisement
Iam_Sandeep

All Pairs Shortest Path Floyd Warshall Algorithm

Jun 26th, 2022
939
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None
  1. #TC=O(v**3)
  2. class Solution:
  3.     def shortest_distance(self, mat):
  4.         v=len(mat)
  5.         for i in range(v):
  6.             for j in range(v):
  7.                 if mat[i][j]==-1:
  8.                     mat[i][j]=float('inf')
  9.         for k in range(v):
  10.             for i in range(v):
  11.                 for j in range(v):
  12.                     mat[i][j]=min(mat[i][k]+mat[k][j],mat[i][j])
  13.         for i in range(v):
  14.             for j in range(v):
  15.                 if mat[i][j]==float('inf'):
  16.                     mat[i][j]=-1
  17.         return mat
Advertisement
RAW Paste Data Copied
Advertisement