forked from giraldeau/evnav
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Francis Giraldeau <[email protected]>
- Loading branch information
Showing
6 changed files
with
249 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include "shortestpath.h" | ||
|
||
ShortestPath::ShortestPath(Graph &g, VertexId src) : | ||
m_graph(g), m_src(src) | ||
{ | ||
m_distTo[src] = 0; | ||
QList<VertexId> queue; | ||
QSet<VertexId> marked; | ||
|
||
marked.insert(src); | ||
queue.append(src); | ||
while(!queue.isEmpty()) { | ||
int v = queue.takeFirst(); | ||
for (Edge &e : g.adj(v)) { | ||
relax(e); | ||
if (!marked.contains(e.to())) { | ||
marked.insert(e.to()); | ||
queue.append(e.to()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
double ShortestPath::distTo(VertexId dst) | ||
{ | ||
if (m_distTo.contains(dst)) | ||
return m_distTo[dst]; | ||
return INFINITY; | ||
} | ||
|
||
bool ShortestPath::hasPathTo(VertexId dst) | ||
{ | ||
return m_distTo.contains(dst); | ||
} | ||
|
||
QList<Edge> ShortestPath::pathTo(VertexId dst) | ||
{ | ||
QList<Edge> path; | ||
if (hasPathTo(dst)) { | ||
Edge nullEdge{}; | ||
for (Edge &e = m_edgeTo[dst]; | ||
e != nullEdge; | ||
e = m_edgeTo[e.from()]) { | ||
path.push_front(e); | ||
} | ||
} | ||
return path; | ||
} | ||
|
||
void ShortestPath::relax(Edge &e) | ||
{ | ||
double old_dist = INFINITY; | ||
double new_dist = INFINITY; | ||
VertexId v = e.from(); | ||
VertexId w = e.to(); | ||
if (m_distTo.contains(v)) | ||
new_dist = m_distTo[v] + e.weight(); | ||
if (m_distTo.contains(w)) | ||
old_dist = m_distTo[w]; | ||
if (old_dist > new_dist) { | ||
m_distTo[w] = new_dist; | ||
m_edgeTo[w] = e; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef SHORTESTPATH_H | ||
#define SHORTESTPATH_H | ||
|
||
#include "graph.h" | ||
|
||
class ShortestPath | ||
{ | ||
public: | ||
// FIXME: the graph must exists for the lifespan of this instance. | ||
// convert graph to refcount for safety | ||
ShortestPath(Graph &graph, VertexId srcId); | ||
double distTo(VertexId dst); | ||
bool hasPathTo(VertexId dst); | ||
QList<Edge> pathTo(VertexId dst); | ||
|
||
private: | ||
void relax(Edge &e); | ||
|
||
Graph& m_graph; | ||
VertexId m_src; | ||
QMap<VertexId, double> m_distTo; | ||
QMap<VertexId, Edge> m_edgeTo; | ||
}; | ||
|
||
#endif // SHORTESTPATH_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters