Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. // A C++ program for Dijkstra's single source shortest path algorithm.
  2. // The program is for adjacency matrix representation of the graph
  3.  
  4. #include <limits.h>
  5. #include <stdio.h>
  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[])
  26. {
  27.     printf("Vertex \t\t 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. // Function 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 be 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.         // Pick the minimum distance vertex from the set of vertices not
  52.         // yet processed. u is always equal to src in the first iteration.
  53.         int u = minDistance(dist, sptSet);
  54.  
  55.         // Mark the picked vertex as processed
  56.         sptSet[u] = true;
  57.  
  58.         // Update dist value of the adjacent vertices of the picked vertex.
  59.         for (int v = 0; v < V; v++)
  60.  
  61.             // Update dist[v] only if is not in sptSet, there is an edge from
  62.             // u to v, and total weight of path from src to v through u is
  63.             // smaller than current value of dist[v]
  64.             if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX
  65.                 && dist[u] + graph[u][v] < dist[v])
  66.                 dist[v] = dist[u] + graph[u][v];
  67.     }
  68.  
  69.     // print the constructed distance array
  70.     printSolution(dist);
  71. }
  72.  
  73. // driver program to test above function
  74. int main()
  75. {
  76.     /* Let us create the example graph discussed above */
  77.     int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
  78.                         { 4, 0, 8, 0, 0, 0, 0, 11, 0 },
  79.                         { 0, 8, 0, 7, 0, 4, 0, 0, 2 },
  80.                         { 0, 0, 7, 0, 9, 14, 0, 0, 0 },
  81.                         { 0, 0, 0, 9, 0, 10, 0, 0, 0 },
  82.                         { 0, 0, 4, 14, 10, 0, 2, 0, 0 },
  83.                         { 0, 0, 0, 0, 0, 2, 0, 1, 6 },
  84.                         { 8, 11, 0, 0, 0, 0, 1, 0, 7 },
  85.                         { 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
  86.  
  87.     dijkstra(graph, 0);
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement