Problem Statement:-: Prim's Algorithm For Minimum Spanning Tree
Problem Statement:-: Prim's Algorithm For Minimum Spanning Tree
PROBLEM STATEMENT:-
Write a program in c to find the shortest path for a graph using dijkstra’s algorithm.
THEORY:-
Dijkstra’s algorithm is very similar to Prim’s algorithm for minimum spanning tree. Like Prim’s MST, we
generate a SPT (shortest path tree) with given source as root. We maintain two sets, one set contains
vertices included in shortest path tree, other set includes vertices not yet included in shortest path tree.
At every step of the algorithm, we find a vertex which is in the other set (set of not yet included) and has
a minimum distance from the source.
Below are the detailed steps used in Dijkstra’s algorithm to find the shortest path from a single source
vertex to all other vertices in the given graph.
Algorithm
1) Create a set sptSet (shortest path tree set) that keeps track of vertices included in shortest path tree,
i.e., whose minimum distance from source is calculated and finalized. Initially, this set is empty.
2) Assign a distance value to all vertices in the input graph. Initialize all distance values as INFINITE.
Assign distance value as 0 for the source vertex so that it is picked first.
3) While sptSet doesn’t include all vertices
….a) Pick a vertex u which is not there in sptSet and has minimum distance value.
….b) Include u to sptSet.
….c) Update distance value of all adjacent vertices of u. To update the distance values, iterate through
all adjacent vertices. For every adjacent vertex v, if sum of distance value of u (from source) and weight
of edge u-v, is less than the distance value of v, then update the distance value of v.
ALGORITHM: -
Variable Listing: -
Steps for calling the function prototype-int dijk(int n,int v,int cost[10][10],int dist[]): -
Date: -
Page No.
[End of if]
STEP 3: - Read n
Date: -
Page No.
SOURCE CODE: -
#include<stdio.h>
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
flag[i]=0;
dist[i]=cost[v][i];
count=2;
while(count<=n)
min=99;
for(w=1;w<=n;w++)
min=dist[w],u=w;
flag[u]=1;
Date: -
Page No.
count++;
for(w=1;w<=n;w++)
dist[w]=dist[u]+cost[u][w];
int main()
int n,v,i,j,cost[10][10],dist[10];
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity;
Date: -
Page No.
scanf("%d",&v);
dij(n,v,cost,dist);
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%d\n",v,i,dist[i]);
return 0;
2345
5671
shortest path
5->1,cost=0
5->2,cost=3
5->3,cost=0
5->4,cost=0
--------------------------------
Date: -
Page No.
27934
61582
shortest path
6->1,cost=0
6->2,cost=-589060154
6->3,cost=9
6->4,cost=3
6->5,cost=0
--------------------------------
DISCUSSION: -
1) The code calculates shortest distance, but doesn’t calculate the path information. We can create a
parent array, update the parent array when distance is updated (like prim’s implementation) and use it
show the shortest path from source to different vertices.
2) The code is for undirected graph, same dijkstra function can be used for directed graphs also.
3) The code finds shortest distances from source to all vertices. If we are interested only in shortest
distance from the source to a single target, we can break the for the loop when the picked minimum
distance vertex is equal to target (Step 3.a of the algorithm).
4) Time Complexity of the implementation is O(V^2). If the input graph is represented using adjacency
list, it can be reduced to O(E log V) with the help of binary heap.
Date: -