Analysis and Design of Algorithms Lab
[BCSL404]
4th Sem AI-ML
Lab 4: Design and implement C/C++ Program to find shortest
paths from a given vertex in a weighted connected graph to
other vertices using Dijkstra's algorithm.
By,
Vandana U
Asst. Professor
Dept. of AI-DS
Aim:To find shortest path using Dijikstra’s
algorithm.
Definition: Dijikstra’s algorithm - For a given source vertex(node) in
the graph, the algorithm finds the path with lowest cost between
that vertex and every other vertex. It can also be used for finding
cost of shortest paths from a single vertex to a single destination
vertex by stopping the algorithm once the shortest path to the
destination vertex has been determined.
Efficiency:1) 2)-graph represented by weighted matrix and priority
queue as unordered array
2)O(│E│log│v│)-graph represented by adjacency lists and priority
queue as min-heap
Algorithm:
Algorithm: Dijikstra(G,s)
//Dijikstra’s algorithm for single source shortest path
//input:A weighted connected graph with non negative weights and its vertex s
//output:The length dv of a shortest path from s to v and penultimate vertex pv for every vertex v in V
Initialize(Q)
for every vertex v in V do
dv<-∞;Pv<-null
Insert(Q,v,dv)
Ds<-0; Decrease(Q,s,ds);VT<-ǿ
for i<- 0 to │V│-1 do
u*<-DeleteMin(Q)
VT<-VT U{u*}
For every vertex u in V-VT that is adjacent to u* do
If du*+w(u*,u)<du
du<- du*+w(u*,u); pu<-u*
Decrease(Q,u,du)
Defines a constant INF with a value
#include<stdio.h> of 999, which represents infinity (a
#define INF 999 large value used to indicate no direct
path between nodes).
void dijkstra(int c[10][10],int n,int s,int d[10])
{ 4 parameters are passed:
int v[10],min,u,i,j; Adjacency matrix of size 10x10
Number of nodes ‘n’
for(i=1;i<=n;i++) Source vertex ‘s’
{ A single dimensional array ‘d’ to keep
track of shortest distance.
d[i]=c[s][i];
v[i]=0; Declares variables. v is an array to keep
} track of visited nodes.
v[s]=1;
Take the distance between source to a node ‘i’ as from
Marks the source node as visited. the adjacency matrix.
Initially mark all the nodes as unvisited
for(i=1;i<=n;i++) Initially all the minimum values are set to infinity.
{
min=INF; Relaxation steps:
for(j=1;j<=n;j++) If node j is unvisited and the path through u is shorter
if(v[j]==0 && d[j]<min) than the current distance, update min as d[j].
Updates the distance to node j through node u
{
min=d[j]; Sets u to the index of the node with the smallest
u=j; distance.
}
v[u]=1; Mark the given node as visited.
for(j=1;j<=n;j++)
if(v[j]==0 && (d[u]+c[u][j])<d[j])
d[j]=d[u]+c[u][j]; Relaxation steps:
} If node j is unvisited and the path through u is
} shorter than the current distance, update d[j].
Updates the distance to node j through node u
int main()
{
Enter the number of nodes.
int c[10][10],d[10],i,j,s,sum,n;
printf("\nEnter n value:");
scanf("%d",&n); Cost Adjacency Matrix
printf("\nEnter the graph data:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&c[i][j]); Entering the source node
printf("\nEnter the souce node:");
scanf("%d",&s);
Function call : Adjacency matrix, number of nodes,
dijkstra(c,n,s,d); source node and shortest distance array.
for(i=1;i<=n;i++)
printf("\nShortest distance from %d to %d is %d",s,i,d[i]);
return 0;
Display the shortest distance
}
Case 1 :
Enter n value:6
Enter the graph data:
0 15 10 999 45 999
999 0 15 999 20 999
20 999 0 20 999 999
999 10 999 0 35 999
999 999 999 30 0 999
OUTPUT
999 999 999 4 999 0
Enter the souce node:2
Case 2 :
Shortest distance from 2 to 1 is 35
Values as per your choice.
Shortest distance from 2 to 2 is 0
Shortest distance from 2 to 3 is 15
Shortest distance from 2 to 4 is 35
Shortest distance from 2 to 5 is 20
Shortest distance from 2 to 6 is 999
THANK YOU