Experiment 4
AIM: Implement Dijsktra‘s algorithm to compute the shortest path through a network
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.
#include <stdio.h>
#define INFINITY 9999
#define MAX 10
void dijkstra(int G[MAX][MAX], int n, int startnode);
int main() {
int G[MAX][MAX], i, j, n, u;
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &G[i][j]);
}
}
printf("\nEnter the starting node: ");
scanf("%d", &u);
dijkstra(G, n, u);
return 0;
}
void dijkstra(int G[MAX][MAX], int n, int startnode) {
int cost[MAX][MAX], distance[MAX], pred[MAX];
int visited[MAX], count, mindistance, nextnode, i, j;
// Create the cost matrix
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (G[i][j] == 0) {
cost[i][j] = INFINITY;
} else {
cost[i][j] = G[i][j];
}
}
}
// Initialize pred[], distance[], and visited[]
for (i = 0; i < n; i++) {
distance[i] = cost[startnode][i];
pred[i] = startnode;
visited[i] = 0;
}
distance[startnode] = 0;
visited[startnode] = 1;
count = 1;
while (count < n - 1) {
mindistance = INFINITY;
// Find the next node with the minimum distance
for (i = 0; i < n; i++) {
if (distance[i] < mindistance && !visited[i]) {
mindistance = distance[i];
nextnode = i;
}
}
visited[nextnode] = 1;
// Update distance[] if a better path is found
for (i = 0; i < n; i++) {
if (!visited[i]) {
if (mindistance + cost[nextnode][i] < distance[i]) {
distance[i] = mindistance + cost[nextnode][i];
pred[i] = nextnode;
}
}
}
count++;
}
// Print the path and distance of each node
for (i = 0; i < n; i++) {
if (i != startnode) {
printf("\nDistance of node %d = %d", i, distance[i]);
printf("\nPath = %d", i);
j = i;
do {
j = pred[j];
printf(" <- %d", j);
} while (j != startnode);
}
}
printf("\n");
}