|
|
@ -21,7 +21,8 @@ inline V closestNode(const std::unordered_set<V>& q, const std::unordered_map<V,
|
|
|
|
const typename std::unordered_set<V>::const_iterator smallest_it =
|
|
|
|
const typename std::unordered_set<V>::const_iterator smallest_it =
|
|
|
|
std::min_element(q.begin(), q.end(),
|
|
|
|
std::min_element(q.begin(), q.end(),
|
|
|
|
[&dist](const V& v1, const V& v2)
|
|
|
|
[&dist](const V& v1, const V& v2)
|
|
|
|
{ return !(dist.find(v2) != dist.end() && ( (dist.find(v1) == dist.end()) || (dist.at(v1) > dist.at(v2)))); } );
|
|
|
|
{ return !( dist.find(v2) != dist.end() &&
|
|
|
|
|
|
|
|
((dist.find(v1) == dist.end()) || (dist.at(v1) > dist.at(v2))) ); } );
|
|
|
|
|
|
|
|
|
|
|
|
return *smallest_it;
|
|
|
|
return *smallest_it;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -30,15 +31,14 @@ template <typename V>
|
|
|
|
std::vector<V> pathFromPrevList(const V& dest, std::unordered_map<V, V> prev)
|
|
|
|
std::vector<V> pathFromPrevList(const V& dest, std::unordered_map<V, V> prev)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::vector<V> retval;
|
|
|
|
std::vector<V> retval;
|
|
|
|
|
|
|
|
if (prev.find(dest) == prev.end())
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
|
|
|
|
retval.push_back(dest);
|
|
|
|
V it = dest;
|
|
|
|
|
|
|
|
for (; prev.find(it) != prev.end() ; it = prev.at(it)) {
|
|
|
|
/// @bug This can be an endless loop
|
|
|
|
retval.push_back(it);
|
|
|
|
for (V it = dest; prev.find(it) != prev.end() ; /*it = prev.at(it)*/) {
|
|
|
|
|
|
|
|
V v = prev.at(it);
|
|
|
|
|
|
|
|
retval.push_back(v);
|
|
|
|
|
|
|
|
it = v;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retval.push_back(it);
|
|
|
|
|
|
|
|
|
|
|
|
std::reverse(retval.begin(), retval.end());
|
|
|
|
std::reverse(retval.begin(), retval.end());
|
|
|
|
return retval;
|
|
|
|
return retval;
|
|
|
@ -61,7 +61,6 @@ dijkstra_shortest_path_to(const Graph<V>& graph,
|
|
|
|
|
|
|
|
|
|
|
|
std::unordered_set<V> q;
|
|
|
|
std::unordered_set<V> q;
|
|
|
|
q.insert(source);
|
|
|
|
q.insert(source);
|
|
|
|
|
|
|
|
|
|
|
|
const std::vector<V>& s_n = graph.neighboursOf(source);
|
|
|
|
const std::vector<V>& s_n = graph.neighboursOf(source);
|
|
|
|
std::copy(s_n.begin(), s_n.end(), std::inserter(q, q.end()));
|
|
|
|
std::copy(s_n.begin(), s_n.end(), std::inserter(q, q.end()));
|
|
|
|
|
|
|
|
|
|
|
@ -72,23 +71,19 @@ dijkstra_shortest_path_to(const Graph<V>& graph,
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
for (V v : graph.neighboursOf(u)) {
|
|
|
|
for (V v : graph.neighboursOf(u)) {
|
|
|
|
|
|
|
|
|
|
|
|
const W d = distanceCompute(u, v);
|
|
|
|
const W d = distanceCompute(u, v);
|
|
|
|
const W alt = dist.at(u) + d;
|
|
|
|
const W alt = dist.at(u) + d;
|
|
|
|
const bool newNode = dist.find(v) == dist.end();
|
|
|
|
|
|
|
|
if (newNode) {
|
|
|
|
if (dist.find(v) == dist.end()) { // new node
|
|
|
|
dist.emplace(v, alt);
|
|
|
|
dist.emplace(v, alt);
|
|
|
|
prev.emplace(v, u);
|
|
|
|
prev.emplace(v, u);
|
|
|
|
q.insert(v);
|
|
|
|
q.insert(v);
|
|
|
|
} else {
|
|
|
|
} else if (alt < dist.at(v)) { // better route
|
|
|
|
const bool betterRoute = alt < dist.at(v);
|
|
|
|
|
|
|
|
if (betterRoute) {
|
|
|
|
|
|
|
|
dist[v] = alt;
|
|
|
|
dist[v] = alt;
|
|
|
|
prev[v] = u;
|
|
|
|
prev[v] = u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return pathFromPrevList(dest, prev);
|
|
|
|
return pathFromPrevList(dest, prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|