20CS2018L Design and Analysis of Algorithms

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

19/03/2022, 16:00 DAA EXP-8.

ipynb - Colaboratory

20CS2018L Design and Analysis of Algorithms

Reg No : URK20CS2065

Ex. No: 8

Date : 09/03/2022

Name : D ALAN DANIELS

Video Link : https://youtu.be/1s8poUUTRGY


Floyd's All Pairs Shortest Path

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():

Input the number of vertices(tv) that are there in the graph.


Input the distances between each vertex in 2-d array. If there are no direct edges then assign
inf and put zero if the distance between the same vertex is to be assigned.
Initialize a path 2d list to store the intermediate paths.
Pass the inputs to the function floyd()

Function floyd():

Assign the contents of graph to variable c.


Iterate k from 0 to tv-1.
Iterate i from 0 to tv-1
Iterate j from 0 to tv-1
If c[i][j]>c[i][k]+c[k][j] then assign c[i][j]=c[i][k]+c[k][j] and path[i][j]=k.
Display the shortest path from each of the veritces and the path list.

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     #      ]

Enter the number of vertices: 3

Enter the distances between vertices: 0 4 5

Enter the distances between vertices: 2 0 I

Enter the distances between vertices: I -3 0

Shortest Distance Between Each vertices:

[0, 2, 5]

[2, 0, 7]

[-1, -3, 0]

The PATH:

******************************************************************

The shortest path from 0 —> 1 is [0, 2, 1]

The shortest path from 0 —> 2 is [0, 2]

The shortest path from 1 —> 0 is [1, 0]

The shortest path from 1 —> 2 is [1, 0, 2]

The shortest path from 2 —> 0 is [2, 1, 0]

The shortest path from 2 —> 1 is [2, 1]

******************************************************************

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

You might also like