Advertisement
Sourav_CSE

Dijkstra Cpp

Dec 3rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. // geekforgeeks
  2. // Program to find Dijkstra's shortest path using
  3. // priority_queue in STL
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6. # define INF 0x3f3f3f3f
  7.  
  8. // iPair ==> Integer Pair
  9. typedef pair<int, int> iPair;
  10.  
  11. // This class represents a directed graph using
  12. // adjacency list representation
  13. class Graph
  14. {
  15. int V; // No. of vertices
  16.  
  17. // In a weighted graph, we need to store vertex
  18. // and weight pair for every edge
  19. list< pair<int, int> > *adj;
  20.  
  21. public:
  22. Graph(int V); // Constructor
  23.  
  24. // function to add an edge to graph
  25. void addEdge(int u, int v, int w);
  26.  
  27. // prints shortest path from s
  28. void shortestPath(int s);
  29. };
  30.  
  31. // Allocates memory for adjacency list
  32. Graph::Graph(int V)
  33. {
  34. this->V = V;
  35. adj = new list<iPair> [V];
  36. }
  37.  
  38. void Graph::addEdge(int u, int v, int w)
  39. {
  40. adj[u].push_back(make_pair(v, w));
  41. adj[v].push_back(make_pair(u, w));
  42. }
  43.  
  44. // Prints shortest paths from src to all other vertices
  45. void Graph::shortestPath(int src)
  46. {
  47. // Create a priority queue to store vertices that
  48. // are being preprocessed. This is weird syntax in C++.
  49. // Refer below link for details of this syntax
  50. // https://www.geeksforgeeks.org/implement-min-heap-using-stl/
  51. priority_queue< iPair, vector <iPair> , greater<iPair> > pq;
  52.  
  53. // Create a vector for distances and initialize all
  54. // distances as infinite (INF)
  55. vector<int> dist(V, INF);
  56.  
  57. // Insert source itself in priority queue and initialize
  58. // its distance as 0.
  59. pq.push(make_pair(0, src));
  60. dist[src] = 0;
  61.  
  62. /* Looping till priority queue becomes empty (or all
  63. distances are not finalized) */
  64. while (!pq.empty())
  65. {
  66. // The first vertex in pair is the minimum distance
  67. // vertex, extract it from priority queue.
  68. // vertex label is stored in second of pair (it
  69. // has to be done this way to keep the vertices
  70. // sorted distance (distance must be first item
  71. // in pair)
  72. int u = pq.top().second;
  73. pq.pop();
  74.  
  75. // 'i' is used to get all adjacent vertices of a vertex
  76. list< pair<int, int> >::iterator i;
  77. for (i = adj[u].begin(); i != adj[u].end(); ++i)
  78. {
  79. // Get vertex label and weight of current adjacent
  80. // of u.
  81. int v = (*i).first;
  82. int weight = (*i).second;
  83.  
  84. // If there is shorted path to v through u.
  85. if (dist[v] > dist[u] + weight)
  86. {
  87. // Updating distance of v
  88. dist[v] = dist[u] + weight;
  89. pq.push(make_pair(dist[v], v));
  90. }
  91. }
  92. }
  93.  
  94. // Print shortest distances stored in dist[]
  95. printf("Vertex Distance from Source\n");
  96. for (int i = 0; i < V; ++i)
  97. printf("%d \t\t %d\n", i, dist[i]);
  98. }
  99.  
  100. // Driver program to test methods of graph class
  101. int main()
  102. {
  103. // create the graph given in above fugure
  104. int V = 9;
  105. Graph g(V);
  106.  
  107. // making above shown graph
  108. g.addEdge(0, 1, 4);
  109. g.addEdge(0, 7, 8);
  110. g.addEdge(1, 2, 8);
  111. g.addEdge(1, 7, 11);
  112. g.addEdge(2, 3, 7);
  113. g.addEdge(2, 8, 2);
  114. g.addEdge(2, 5, 4);
  115. g.addEdge(3, 4, 9);
  116. g.addEdge(3, 5, 14);
  117. g.addEdge(4, 5, 10);
  118. g.addEdge(5, 6, 2);
  119. g.addEdge(6, 7, 1);
  120. g.addEdge(6, 8, 6);
  121. g.addEdge(7, 8, 7);
  122.  
  123. g.shortestPath(0);
  124.  
  125. return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement