Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #include <queue>
  2. #include <algorithm>
  3. #include <string>
  4. #include <list>
  5.  
  6. /**
  7. * Returns an std::list of vertex keys that creates some shortest path between `start` and `end`.
  8. *
  9. * This list MUST include the key of the `start` vertex as the first vertex in the list, the key of
  10. * the `end` vertex as the last element in the list, and an ordered list of all vertices that must
  11. * be traveled along the shortest path.
  12. *
  13. * For example, the path a -> c -> e returns a list with three elements: "a", "c", "e".
  14. *
  15. * @param start The key for the starting vertex.
  16. * @param end The key for the ending vertex.
  17. */
  18. template <class V, class E>
  19. std::list<std::string> Graph<V,E>::shortestPath(const std::string start, const std::string end) {
  20. std::list<std::string> path;
  21. path.push_back(start);
  22.  
  23. std::queue<Vertex> q;
  24. Vertex v = vertexMap[start];
  25. v["label"] = "visited";
  26. q.push(v);
  27.  
  28. while (!q.empty()){
  29. v = q.front();
  30. q.pop();
  31.  
  32. std::list<E_byRef> & incident = incidentEdges(v);
  33.  
  34. for(E_byRef & edge :incident){
  35. Vertex adjacent = edge.get().dest();
  36. if (adjList["label"] != "visited"){
  37. edge["label"] = "discovered";
  38. adjacent["label"] = "visited";
  39. }
  40. }
  41.  
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
  48. return path;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement