Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::list<std::string> path;
- // no Dijkstra :(
- // initialization
- queue<string> q;
- unordered_map<string, bool> labels; // {vertex, flag}
- unordered_map<string, string> prev; // {vertex, previous vertex}
- labels[start] = true;
- q.push(start);
- while(!q.empty()){
- string v = q.front();
- q.pop();
- for(auto e : incidentEdges(v)){
- string w = e.get().dest().key();
- string source = e.get().source().key(); // something else is the source of adj
- if(labels.find(w) == labels.end()){ // unexplored
- labels[w] = true; // set to explored (discovery)
- prev[w] = v; // keep track of previous
- q.push(w); // add to queue
- }
- if(isAdjacent(source, v) && labels.find(source) == labels.end()){ // undirected
- labels[source] = true; // set to explored (discovery)
- prev[source] = v; // keep track of previous
- q.push(source); // add to queue
- }
- if(labels.find(end) != labels.end()){ // reached end vertex
- path.push_front(end); // add to path in reverse order
- string p = prev.at(end);
- while(p != start){
- path.push_front(p);
- p = prev.at(p);
- }
- path.push_front(start);
- return path;
- }
- }
- }
- return path;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement