Experiment 4
Experiment 4
AIM: Implement Dijsktra‘s algorithm to compute the shortest path through a network
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
int main() {
int G[MAX][MAX], i, j, n, u;
dijkstra(G, n, u);
return 0;
}
distance[startnode] = 0;
visited[startnode] = 1;
count = 1;
visited[nextnode] = 1;
count++;
}
j = i;
do {
j = pred[j];
printf(" <- %d", j);
} while (j != startnode);
}
}
printf("\n");
}