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