Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. #include <queue>
  2. #include <algorithm>
  3. #include <string>
  4. #include <list>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8.  
  9. inline void totalPaths(list<string> & currPath, vector<list<string>> & paths, unordered_map<string, list<string>> & predecessor, string prev, string end){
  10. list<string> options = predecessor[prev];
  11. if (find (options.begin(), options.end(), end) != options.end()){
  12. currPath.push_back(end);
  13. paths.push_back(currPath);
  14. return;
  15. }
  16. for (string option : options){
  17. currPath.push_back(option);
  18. totalPaths(currPath, paths, predecessor, option, end);
  19. currPath.pop_back();
  20. }
  21. }
  22.  
  23. /**
  24. * Returns an list of vertex keys that creates any shortest path between `start` and `end`.
  25. *
  26. * This list MUST include the key of the `start` vertex as the first vertex in the list, the key of
  27. * the `end` vertex as the last element in the list, and an ordered list of all vertices that must
  28. * be traveled along the shortest path.
  29. *
  30. * For example, the path a -> c -> e returns a list with three elements: "a", "c", "e".
  31. *
  32. * You should use undirected edges. Hint: There are no edge weights in the Graph.
  33. *
  34. * @param start The key for the starting vertex.
  35. * @param end The key for the ending vertex.
  36. */
  37. template <class V, class E>
  38. list<string> Graph<V,E>::shortestPath(const string start, const string end) {
  39. // TODO: Part 3
  40. unordered_map<string, bool> seen;
  41. unordered_map<string, list<string>> predecessor;
  42.  
  43. queue <string> bfs;
  44.  
  45. list<string> first;
  46. predecessor.emplace(start, first);
  47.  
  48. bfs.push(start);
  49.  
  50. while(!bfs.empty()){
  51. list<edgeListIter> edgesIts = adjList.at(bfs.front()); //takes next vertex from queue and gets all edges connected to it
  52. list<Edge> edges;
  53. for (edgeListIter it : edgesIts){
  54. edges.push_back(*it); //pushes back all incident edges to a vector
  55. }
  56. //below pushes any vertices not already seen into the queue
  57. for (Edge edge : edges){
  58. string s;
  59. string d;
  60.  
  61. if (seen.find(edge.dest().key()) == seen.end()){
  62. seen.emplace(edge.dest().key(), true);
  63. bfs.push(edge.dest().key());
  64. s = edge.source().key();
  65. d = edge.dest().key();
  66. }
  67.  
  68. if (seen.find(edge.source().key()) == seen.end()){
  69. seen.emplace(edge.source().key(), true);
  70. bfs.push(edge.source().key());
  71. s = edge.source().key();
  72. d = edge.dest().key();
  73. }
  74.  
  75.  
  76. if (predecessor.find(d) == predecessor.end()){
  77. list<string> previous;
  78. previous.push_back(s);
  79. predecessor.emplace(d, previous);
  80. }
  81. else
  82. {
  83. predecessor[d].push_back(s);
  84. }
  85.  
  86. if (predecessor.find(s) == predecessor.end()){
  87. list<string> previous;
  88. previous.push_back(d);
  89. predecessor.emplace(s, previous);
  90. }
  91. else
  92. {
  93. predecessor[s].push_back(d);
  94. }
  95. }
  96. bfs.pop();
  97. }
  98. // up to here, we now have predecessor that stores the edges as a map, now to figure out how to get all possible paths to end
  99. list<string> path1;
  100. path1.push_back(end);
  101. list<string> pred = predecessor[end];
  102.  
  103. auto it = predecessor.begin();
  104. while (it != predecessor.end()){
  105. list<string> nums = it->second;
  106. for (string num : nums){
  107. cout<<it->first << " " << num<<endl;
  108. // predecessor[num]
  109. }
  110. it++;
  111. }
  112.  
  113. vector<list<string>> allPaths;
  114. list<string> path;
  115. path.push_back(start);
  116. string one = start;
  117. string second = end;
  118. totalPaths(path, allPaths, predecessor, one, second);
  119. cout<<"PASSED TOTAL PATHS. Number of paths: " << allPaths.size()<<endl;
  120. list<string> testing = allPaths[0];
  121. auto testIt = testing.begin();
  122. for (testIt = testing.begin(); testIt!=testing.end(); testIt++){
  123. cout<<*testIt<<endl;
  124. }
  125. list<string> ret;
  126. ret = testing;
  127. // if (allPaths.size() == 0){
  128. // return ret;
  129. // }
  130. // ret = allPaths[0];
  131. // if (allPaths.size() == 1){
  132. // return ret;
  133. // }
  134. // for (unsigned i = 1; i < allPaths.size(); i++){
  135. // if (allPaths[i].size() < ret.size()){
  136. // ret = allPaths[i];
  137. // }
  138. // }
  139.  
  140. return ret;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement