Professional Documents
Culture Documents
ipynb - Colaboratory
Reg No : URK20CS2065
Ex. No: 6
Date : 15/02/2022
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():
Function dijkstra():
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
*********************************************************
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
*********************************************************
Result:
Thus the program to get the shortest route using Dijkstra's Algorithm has been written and tested
for various inputs.
https://colab.research.google.com/drive/1z83mtui77c7F17sbBkILJb0jK7EyTRkt#scrollTo=Z-_udfS8X-3N&printMode=true 3/3