Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. template <class V, class E>
  2. std::list<std::string> Graph<V,E>::shortestPath(const std::string start, const std::string end) {
  3. // TODO: Part 3
  4. std::list<std::string> path;
  5.  
  6. std::queue<std::string> path_;
  7. std::unordered_map<std::string, std::string> currentToOrigin;
  8. std::unordered_map<std::string, bool> visited;
  9.  
  10. for (std::pair<std::string, V_byRef> v : vertexMap) {
  11. visited.insert(std::pair<std::string, bool>(v.first, false));
  12. // std::cout << visited.at(v.first) << std::endl;
  13. }
  14.  
  15. // std::cout << "start bfs" << std::endl;
  16.  
  17. path_.push(start);
  18.  
  19. while (!(path_.empty())) {
  20. string v_ = path_.front();
  21. // V & v = *(new V(v_));
  22. path_.pop();
  23. bool & flag = visited.at(v_);
  24. flag = true;
  25.  
  26. // for (auto e : incidentEdges(v_)) {
  27. // if (visited.at((e.get()).dest().key()) == false) {
  28. // path_.push((e.get()).dest().key());
  29. // currentToOrigin.insert(std::pair<std::string, std::string>((e.get()).dest().key(), v_));
  30. // }
  31. // }
  32. for (std::pair<std::string, V_byRef> v : vertexMap) {
  33. if (isAdjacent(v_, v.first)) {
  34. if (visited.at(v.first) == false) {
  35. path_.push(v.first);
  36. currentToOrigin.insert(std::pair<std::string, std::string>(v.first, v_));
  37. }
  38. }
  39. }
  40. }
  41.  
  42. // std::cout << currentToOrigin.size() << std::endl;
  43. // std::cout << "here" << std::endl;
  44.  
  45. string currentV = end;
  46. while (currentV != start) {
  47. path.push_back(currentV);
  48. currentV = currentToOrigin.at(currentV);
  49. }
  50.  
  51. path.push_back(start);
  52. path.reverse();
  53.  
  54. return path;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement