You are on page 1of 3

15/02/2022, 21:45 DAA EXP-6.

ipynb - Colaboratory

20CS2018L Design and Analysis of Algorithms

Reg No : URK20CS2065

Ex. No: 6

Date : 15/02/2022

Name : D ALAN DANIELS

Video Link : https://youtu.be/pB0qwFqGkg8


DIJKSTRA'S SHORTEST PATH ALGORITHM

Aim:

To write a program to get the shortest distance between the source node and every other node
using Dijkstra's shortest path algorithm.

Description:

Dijkstra's Algorithm is a pathfinding algorithm that generates every single route through the graph,
and then selects the route that has the lowest overall cost. This works by iteratively calculating a
distance for each node in the graph, starting from the start node, and continuing until we reach the
end node.

Algorithm:

Function main():

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


Input the distances between each cities in 2-d array. If there were no edges then assign -1 and
assign zero if the distance between the same vertex
Input the source vertex.
Pass the inputs to the function dijkstra.

Function dijkstra():

Initialise the lists dist and vis.


Interate from 0 to tv-1.
Calculate the index that has the shortest distance using the min_index() function.
Mark the index that is found from min_index() as True in the vis list.
Calculate the neighboring vertices and check if graph[u][v]!=0 and graph[u][v]!=-1 and
vis[v]==False and dist[v]>dist[u]+graph[u][v] if so assign dist[v]=dist[u]+graph[u][v].

Program:
1 from sys import maxsize
2
https://colab.research.google.com/drive/1z83mtui77c7F17sbBkILJb0jK7EyTRkt#scrollTo=Z-_udfS8X-3N&printMode=true 1/3
15/02/2022, 21:45 DAA EXP-6.ipynb - Colaboratory

3 def min_index(dist,vis):
4     min=maxsize
5     for i in range(tv):
6         if dist[i]<min and vis[i]==False:
7             min=dist[i]
8     return dist.index(min)
9
10 def dijkstra(graph,src):
11     dist=[maxsize]*tv #to store the distances
12     dist[src]=0 # distance b/w src to src must be 0
13     vis=[False]*tv #to keep track of the visited vertices
14
15     for i in range(tv): #since a vertex is removed for every interation, interate till t
16         u=min_index(dist,vis) #index of vertex that is not visited and has the least dis
17         vis[u]=True # the index that is returned is marked visited
18
19         for v in range(tv): #neighboring vertices calculation
20             if graph[u][v]!=0 and graph[u][v]!=-1 and vis[v]==False and dist[v]>dist[u]+
21                 dist[v]=dist[u]+graph[u][v]
22     return dist
23 def main():
24     global tv
25     tv=int(input("Enter the number of vertices: ")) #getting the total number of vertice
26     graph=[]
27     for i in range(tv):
28         graph.append(list(map(int,input("Enter the distances between vertices: ").split(
29
30     source=int(input("Enter the source vertex: "))
31     dist=dijkstra(graph,source)
32     print("*********************************************************")
33     for i in range(tv):
34         print("The shortest distance between",source,"and",i,"is",dist[i])
35     print("*********************************************************")
36 if __name__=="__main__":
37     main()
38 #     Enter the number of vertices: 5
39 # Enter the distances between vertices: 0 10 3 -1 -1
40 # Enter the distances between vertices: -1 0 1 2 -1
41 # Enter the distances between vertices: -1 4 0 8 2
42 # Enter the distances between vertices: -1 -1 -1 0 7
43 # Enter the distances between vertices: -1 -1 -1 9 0

Enter the number of vertices: 5

Enter the distances between vertices: 0 10 3 -1 -1

Enter the distances between vertices: -1 0 1 2 -1

Enter the distances between vertices: -1 4 0 8 2

Enter the distances between vertices: -1 -1 -1 0 7

Enter the distances between vertices: -1 -1 -1 9 0

Enter the source vertex: 0

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

The shortest distance between 0 and 0 is 0

The shortest distance between 0 and 1 is 7

h h t t di t b t 0 d 2 i 3
https://colab.research.google.com/drive/1z83mtui77c7F17sbBkILJb0jK7EyTRkt#scrollTo=Z-_udfS8X-3N&printMode=true 2/3
15/02/2022, 21:45 DAA EXP-6.ipynb - Colaboratory
The shortest distance between 0 and 2 is 3

The shortest distance between 0 and 3 is 9

The shortest distance between 0 and 4 is 5

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

Result:

Thus the program to get the shortest route using Dijkstra's Algorithm has been written and tested
for various inputs.

check 30s completed at 20:49

https://colab.research.google.com/drive/1z83mtui77c7F17sbBkILJb0jK7EyTRkt#scrollTo=Z-_udfS8X-3N&printMode=true 3/3

You might also like