Professional Documents
Culture Documents
20CS2018L Design and Analysis of Algorithms
20CS2018L Design and Analysis of Algorithms
20CS2018L Design and Analysis of Algorithms
ipynb - Colaboratory
Reg No : URK20CS2065
Ex. No: 8
Date : 09/03/2022
Aim:
To write a program to find all pairs shortest path using Floyd's algorithm.
Description:
The Floyd Warshall Algorithm is for solving the All Pairs Shortest Path problem. The problem is to
find shortest distances between every pair of vertices in a given edge weighted directed Graph.
Algorithm:
Function main():
Function floyd():
Program:
1 from sys import maxsize
2
3 def find_path(path,i,j,route):
4 if path[i][j]==i:
5 return
6 find path(path,i,path[i][j],route)
https://colab.research.google.com/drive/1i_oQlN8OEw2x5tdx1mFJTtiNWSlhoSc3#scrollTo=Hb72wUPpX9fQ&printMode=true 1/4
19/03/2022, 16:00 DAA EXP-8.ipynb - Colaboratory
_p (p , ,p [ ][j], )
7 route.append(path[i][j])
8
9
10 def print_path(path):
11 for i in range(tv):
12 for j in range(tv):
13 if i!=j and path[i][j]!=-1:
14 route=[i]
15 find_path(path,i,j,route)
16 route.append(j)
17 print(f"The shortest path from {i} —> {j} is", route)
18 else:
19 if path[i][j]==-1 and i!=j:
20 print(f"There is no path between {i} to {j}")
21
22
23 def floyd(g,tv,path):
24 c=g.copy()
25 for i in range(tv):
26 for j in range(tv):
27 if i==j:
28 path[i][j]=0
29 elif c[i][j]!=maxsize:
30 path[i][j]=i
31 else:
32 path[i][j]=-1
33 for k in range(tv):
34 for i in range(tv):
35 for j in range(tv):
36 if c[i][j]>c[i][k]+c[k][j]:
37 c[i][j]=c[i][k]+c[k][j]
38 path[i][j]=path[k][j]
39 print("\nShortest Distance Between Each vertices: \n")
40 for i in range(tv):
41 for j in range(tv):
42 if c[i][j]>=maxsize:
43 c[i][j]="I"
44 for i in c:
45 print(i)
46 print("\nThe PATH: ")
47 print("******************************************************************")
48 print_path(path)
49 print("******************************************************************")
50
51
52 def main():
53 global tv
54 tv = int(input("Enter the number of vertices: ")) # getting the total number of ver
55 graph = []
56 I=maxsize
57 for i in range(tv):
58 h d(i t("E t th di t b t ti ") lit())
https://colab.research.google.com/drive/1i_oQlN8OEw2x5tdx1mFJTtiNWSlhoSc3#scrollTo=Hb72wUPpX9fQ&printMode=true 2/4
19/03/2022, 16:00 DAA EXP-8.ipynb - Colaboratory
58 graph.append(input("Enter the distances between vertices: ").split())
59 for i in range(tv):
60 for j in range(tv):
61 if graph[i][j]=="I":
62 graph[i][j]=I
63 else:
64 graph[i][j]=int(graph[i][j])
65
66 path=[[0 for i in range(tv)] for i in range(tv)]
67 floyd(graph,tv,path)
68
69 if __name__=="__main__":
70 main()
71 # 0 4 5
72 # 2 0 inf
73 # inf·-3·0
74 # graph = [[0, 5, I, 10],
75 # [I, 0, 3, I],
76 # [I, I, 0, 1],
77 # [I, I, I, 0]
78 # ]
[0, 2, 5]
[2, 0, 7]
[-1, -3, 0]
The PATH:
******************************************************************
******************************************************************
Result:
Thus the program to get all pairs shortest paths using Floyd's Algorithm is written and tested for
various inputs.
https://colab.research.google.com/drive/1i_oQlN8OEw2x5tdx1mFJTtiNWSlhoSc3#scrollTo=Hb72wUPpX9fQ&printMode=true 3/4