Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. #include "Dijkstra.h"
  2. #include <queue>
  3.  
  4.  
  5.  
  6. using namespace std;
  7.  
  8. vector< vector<pair<int, int> > > Dijsktra::setup_adjList(int num) {
  9. vector< vector<pair<int, int> > > adjList;
  10. numVectors = num;
  11. // creating 4 vertices, so we pass in num as the # of vertices
  12. int i = 0;
  13. while (i < num)
  14. {
  15. // make a vector representing a adjacency row, and add it to the adjList
  16. vector<pair<int, int> > row;
  17. adjList.push_back(row);
  18. i++;
  19. }
  20. return adjList;
  21. }
  22. // every adjList[i] contains all the adjacent vertices of i in this format:
  23. // <vertex of friend, edge weight>
  24. vector< vector<pair<int, int> > > Dijsktra::fill_adjList(vector< vector<pair<int, int> > > adjList, int a, int b, int c) {
  25.  
  26. // create our edges that will actually be in our Adj List
  27. // adjList[VERTEX].push_back(make_pair(EDGE, WEIGHT));
  28. // make_pair is used to pair the neighbor EDGE and its WEIGHT
  29. adjList[a].push_back(make_pair(b, c));
  30.  
  31.  
  32.  
  33.  
  34. // return the adjacency list, which is passed to the next fill_adjList call for the next adjList[i]
  35. return adjList;
  36. }
  37.  
  38. // prints shortest paths from src to all other vertices
  39. void Dijsktra::shortestPath(int src, vector< vector<pair<int, int> > > adjList, int num)
  40. {
  41. // creates a priority queue to store vertices that are being processed
  42. priority_queue< pair<int, int>, vector <pair<int, int>>, greater<pair<int, int>> > priorityQueue;
  43.  
  44. // create a vector for distances and initialize all distances as infinity (INT_MAX)
  45. // using num as number of verticies (number of distances we need to calculate)
  46. vector<int> dist(num, INT_MAX);
  47.  
  48. // push source in priority queue and set its distance = 0
  49. priorityQueue.push(make_pair(0, src));
  50. dist[src] = 0;
  51.  
  52. // looping till priority queue becomes empty (or all distances are not finalized)
  53. while (!priorityQueue.empty())
  54. {
  55. // the first vertex in the priorityQueue is the min distance vertex, pop it from the priorityQueue
  56. // capture int u as the label, we have to do this since the priorityQueue is sorted by distances (we'd lose the vertex label)
  57. int u = priorityQueue.top().second;
  58. priorityQueue.pop();
  59.  
  60. // vector i is used to iterate through all adjacent vertices of a vertex
  61. vector< pair<int, int> >::iterator i;
  62. for (i = adjList[u].begin(); i != adjList[u].end(); ++i)
  63. {
  64. // capture adjacent vertex label v, and weight it's weight
  65. int v = (*i).first;
  66. int weight = (*i).second;
  67.  
  68. // if there is a shorter path to v through u, update the distance of v
  69. if (dist[v] > dist[u] + weight)
  70. {
  71. dist[v] = dist[u] + weight;
  72. priorityQueue.push(make_pair(dist[v], v));
  73. }
  74. }
  75. }
  76. // public distVector captures dist so we do not have to pass it in to printDist()
  77. distVector = dist;
  78.  
  79. }
  80. void Dijsktra::printDist() {
  81.  
  82. // print shortest distances stored in dist[]
  83. char alpha[10] = { 'A','B','C','D','E','F','G','H','I','J' }; // used to coorilate vertex index to Node labels
  84. cout << " Vertex \t Distance from Source \t\n";
  85. for (int i = 0; i < 4; ++i)
  86. cout << " " << alpha[i] << " \t\t\t " << distVector[i] << "\t" << "\n";
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement