C++ Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time

«
»
This is a C++ Program to find the shortest path in linear time. This can be done by using Dijkstra’a Shortestpath algorithm.

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

Output:

advertisement
$ g++ LinearTimeShortestPath.cpp
$ a.out
 
Vertex   Distance from Source
0 		 0
1 		 4
2 		 12
3 		 19
4 		 21
5 		 11
6 		 9
7 		 8
8 		 14
 
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement

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

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter