C Program to Find Shortest Path using Dijkstra’s Algorithm

This is a C Program to find Dijkstra algorithm. 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 minimum distance from source.

Here is source code of the C Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. // Number of vertices in the graph
  5. #define V 9
  6.  
  7. // A utility function to find the vertex with minimum distance value, from
  8. // the set of vertices not yet included in shortest path tree
  9. int minDistance(int dist[], int sptSet[]) {
  10.     // Initialize min value
  11.     int min = INT_MAX, min_index;
  12.     int v;
  13.     for (v = 0; v < V; v++)
  14.         if (sptSet[v] == 0 && dist[v] <= min)
  15.             min = dist[v], min_index = v;
  16.  
  17.     return min_index;
  18. }
  19.  
  20. // A utility function to print the constructed distance array
  21. void printSolution(int dist[], int n) {
  22.     printf("Vertex   Distance from Source\n");
  23.     int i;
  24.     for (i = 0; i < V; i++)
  25.         printf("%d \t\t %d\n", i, dist[i]);
  26. }
  27.  
  28. // Funtion that implements Dijkstra's single source shortest path algorithm
  29. // for a graph represented using adjacency matrix representation
  30. void dijkstra(int graph[V][V], int src) {
  31.     int dist[V]; // The output array.  dist[i] will hold the shortest
  32.     // distance from src to i
  33.  
  34.     int sptSet[V]; // sptSet[i] will 1 if vertex i is included in shortest
  35.     // path tree or shortest distance from src to i is finalized
  36.  
  37.     // Initialize all distances as INFINITE and stpSet[] as 0
  38.     int i, count, v;
  39.     for (i = 0; i < V; i++)
  40.         dist[i] = INT_MAX, sptSet[i] = 0;
  41.  
  42.     // Distance of source vertex from itself is always 0
  43.     dist[src] = 0;
  44.  
  45.     // Find shortest path for all vertices
  46.     for (count = 0; count < V - 1; count++) {
  47.         // Pick the minimum distance vertex from the set of vertices not
  48.         // yet processed. u is always equal to src in first iteration.
  49.         int u = minDistance(dist, sptSet);
  50.  
  51.         // Mark the picked vertex as processed
  52.         sptSet[u] = 1;
  53.  
  54.         // Update dist value of the adjacent vertices of the picked vertex.
  55.         for (v = 0; v < V; v++)
  56.  
  57.             // Update dist[v] only if is not in sptSet, there is an edge from
  58.             // u to v, and total weight of path from src to  v through u is
  59.             // smaller than current value of dist[v]
  60.             if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u]
  61.                     + graph[u][v] < dist[v])
  62.                 dist[v] = dist[u] + graph[u][v];
  63.     }
  64.  
  65.     // print the constructed distance array
  66.     printSolution(dist, V);
  67. }
  68.  
  69. // driver program to test above function
  70. int main() {
  71.     /* Let us create the example graph discussed above */
  72.     int graph[V][V] =  {{0, 4, 0, 0, 0, 0, 0, 8, 0},
  73.                         {4, 0, 8, 0, 0, 0, 0, 11, 0},
  74.                         {0, 8, 0, 7, 0, 4, 0, 0, 2},
  75.                         {0, 0, 7, 0, 9, 14, 0, 0, 0},
  76.                         {0, 0, 0, 9, 0, 10, 0, 0, 0},
  77.                         {0, 0, 4, 0, 10, 0, 2, 0, 0},
  78.                         {0, 0, 0, 14, 0, 2, 0, 1, 6},
  79.                         {8, 11, 0, 0, 0, 0, 1, 0, 7},
  80.                         {0, 0, 2, 0, 0, 0, 6, 7, 0}
  81.                        };
  82.  
  83.     dijkstra(graph, 0);
  84.  
  85.     return 0;
  86. }

Output:

$ gcc Dijkstra.c
$ ./a.out
 
Vertex   Distance from Source
0 		 0
1 		 4
2 		 12
3 		 19
4 		 21
5 		 11
6 		 9
7 		 8
8 		 14

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.