Advertisement
karbaev

STL Dijkstra

Nov 4th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. //Dijkstra's algorithm is simply BFS + priority queue and includes a relaxation step.
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <limits>
  6. #include <queue>
  7. using namespace std;
  8.  
  9. typedef vector<vector<pair<int,float> > > Graph;
  10. class Comparator
  11. {
  12. public:
  13.  int operator() ( const pair<int,float>& p1, const pair<int,float> &p2)
  14.  {
  15.  return p1.second>p2.second;
  16.  }
  17. };
  18.  
  19. void dijkstra(const Graph  &G,const int &source,const int &destination,vector<int> &path)
  20. {
  21. vector<float> d(G.size());
  22. vector<int> parent(G.size());
  23. for(unsigned int i = 0 ;i < G.size(); i++)
  24. {
  25.  d[i] = std::numeric_limits<float>::max();
  26.  parent[i] = -1;
  27. }
  28. priority_queue<pair<int,float>, vector<pair<int,float> >, Comparator> Q;
  29. d[source] = 0.0f;
  30. Q.push(make_pair(source,d[source]));
  31. while(!Q.empty())
  32. {
  33.  int u = Q.top().first;
  34.  if(u==destination) break;
  35.  Q.pop();
  36.  for(unsigned int i=0; i < G[u].size(); i++)
  37.  {
  38.   int v= G[u][i].first;
  39.   float w = G[u][i].second;
  40.   if(d[v] > d[u]+w)
  41.   {
  42.    d[v] = d[u]+w;
  43.    parent[v] = u;
  44.    Q.push(make_pair(v,d[v]));
  45.   }
  46.  }
  47. }
  48. path.clear();
  49. int p = destination;
  50. path.push_back(destination);
  51. while(p!=source)
  52. {
  53.  p = parent[p];
  54.  path.push_back(p);
  55. }
  56. }
  57.  
  58. int main()
  59. {
  60.     /* Graph
  61.     GRAPH TYPE = UNDIRECTED
  62.     NUMBER OF VERTICES = 6 indexed from 0 to 5
  63.     NUMBER OF EDGES = 9
  64.     edge 0->1 weight = 7
  65.     edge 0->2 weight = 9
  66.     edge 0->5 weight = 14
  67.     edge 1->2 weight = 10
  68.     edge 1->3 weight = 15
  69.     edge 2->5 weight = 2
  70.     edge 2->3 weight = 11
  71.     edge 3->4 weight = 6
  72.     edge 4->5 weight = 9
  73.     */
  74.     Graph g;
  75.     g.resize(6);
  76.     g[0].push_back(make_pair(1,7));
  77.     g[1].push_back(make_pair(0,7));
  78.  
  79.     g[0].push_back(make_pair(2,9));
  80.     g[2].push_back(make_pair(0,9));
  81.  
  82.     g[0].push_back(make_pair(5,14));
  83.     g[5].push_back(make_pair(0,14));
  84.  
  85.     g[1].push_back(make_pair(2,10));
  86.     g[2].push_back(make_pair(1,10));
  87.  
  88.     g[1].push_back(make_pair(3,15));
  89.     g[3].push_back(make_pair(1,15));
  90.  
  91.     g[2].push_back(make_pair(5,2));
  92.     g[5].push_back(make_pair(2,2));
  93.  
  94.     g[2].push_back(make_pair(3,11));
  95.     g[3].push_back(make_pair(2,11));
  96.  
  97.     g[3].push_back(make_pair(4,6));
  98.     g[4].push_back(make_pair(3,6));
  99.  
  100.     g[4].push_back(make_pair(5,9));
  101.     g[5].push_back(make_pair(4,9));
  102.     vector<int> path;
  103.     dijkstra(g,0,4,path);
  104.     for(int i=path.size()-1;i>=0;i--)
  105.         cout<<path[i]<<"->";
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement