Class EppsteinShortestPathIterator<V,E>
- Type Parameters:
V- the graph vertex typeE- the graph edge type
This implementation can only be used for directed simple graphs. Also for this iterator to work correctly the graph must not be modified during iteration. Currently there are no means to ensure that, nor to fail-fast. The results of such modifications are undefined.
First the shortest paths tree in the edge reversed graph starting at sink is built. Thus
we get distances $d(v)$ from every vertex $v$ to sink. We then define a sidetrack edge to
be an edge, which is not in the shortest paths tree. The key observation is that every path
between the source and the sink can be solely determined by a sub-sequence of its
edges which are sidetracks.
Let $d(v)$ be the distance from $v$ to sink and $w()$ be the weight function for edges in
graph. If $e$ connects a pair of vertices $(u, w)$, the $\delta(e)$ is defined as
$w(e)+d(w)-d(u)$. Intuitively, $\delta(e)$ measures how much distance is lost by being
“sidetracked” along $e$ instead of taking a shortest path to sink.
The idea of the algorithm is to build a heap of sidetracks. This heap can be then traversed with
breadth-first search in order to retrieve the implicit representations of the paths between
source and sink.
This implementation has several improvements in comparison to the original description in the article:
- An outgoing edge of vertex $v$ is inserted in the paths graph iff it is reachable from the
source. - The cross edges in the paths graph are added only for those vertices which are reachable from the root vertex.
- Weights of the edges in the paths graph are mot maintained explicitly, because they are computed during its traversal.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classRepresents a path that is generated during the computations.private classVertex of the paths graph. -
Field Summary
FieldsModifier and TypeFieldDescriptionShortest paths tree in the edge reversed graphgraphrooted atsink.Underlying graph.private Map<V, EppsteinShortestPathIterator<V, E>.PathsGraphVertex> For each vertex $v$ ingraphmaintains the root of the balanced heap, which corresponds to it.private EppsteinShortestPathIterator<V,E>.PathsGraphVertex Vertex of the paths graph from which the BFS traversal is started.private Queue<EppsteinShortestPathIterator<V, E>.EppsteinGraphPath> Priority queue of the paths generated during the computation.private final VSink vertex.private final VSource vertex. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidAdds cross edges for every vertex $v$ reachable from the root of balanced heap ofsourcein the paths graph.private voidaddExtension(EppsteinShortestPathIterator<V, E>.EppsteinGraphPath path, EppsteinShortestPathIterator<V, E>.PathsGraphVertex extendingVertex, double weight) Adds an extension ofpathswithextendingVertexbeing its last element.private voidAdds all one-edge extension of thepathwrt the paths graph.private voidCreates the root vertex $r$ of the paths graph and connects it to the root of the balanced heap ofsource.private voidIf thegraphis denoted by $G$, then for every vertex $v$ reachable fromsourcein $G$ $D(G)$ contains balanced heaps of all outroots, which corresponds to vertices on the path from $v$ tosink.private voidGuides the building process of the paths graph.private doubleCalculates the $\delta(e)$ value for a given edgee.private Pair<EppsteinShortestPathIterator<V, E>.PathsGraphVertex, EppsteinShortestPathIterator<V, E>.PathsGraphVertex> Builds outroot and heapification of other sidetracks ofv.private EppsteinShortestPathIterator<V,E>.PathsGraphVertex getRestHeap(List<EppsteinShortestPathIterator<V, E>.PathsGraphVertex> vertices, int i, int size) Constructs an explicit tree-like representation of the binary heap contained inverticesstarting at positioni.booleanhasNext()private voidheapify(List<EppsteinShortestPathIterator<V, E>.PathsGraphVertex> vertices, int size) Builds a min-heap out of theverticeslistprivate EppsteinShortestPathIterator<V,E>.PathsGraphVertex insertPersistently(EppsteinShortestPathIterator<V, E>.PathsGraphVertex root, EppsteinShortestPathIterator<V, E>.PathsGraphVertex vertex) Insertsvertexinto the balanced heap rooted atrootin a persistent (non-destructive) way.private voidinsertVertex(V v, EppsteinShortestPathIterator<V, E>.PathsGraphVertex predecessorHeap) Guides the process of adding the sidetracks ofvto the paths graph.next()private voidsiftDown(List<EppsteinShortestPathIterator<V, E>.PathsGraphVertex> vertices, int i, int size) private voidswap(List<EppsteinShortestPathIterator<V, E>.PathsGraphVertex> vertices, int i, int j) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Iterator
forEachRemaining, remove
-
Field Details
-
graph
Underlying graph. -
source
Source vertex. -
sink
Sink vertex. -
pathsGraphRoot
Vertex of the paths graph from which the BFS traversal is started. -
distanceAndPredecessorMap
Shortest paths tree in the edge reversed graphgraphrooted atsink. -
pathsQueue
Priority queue of the paths generated during the computation. -
hMapping
For each vertex $v$ ingraphmaintains the root of the balanced heap, which corresponds to it.
-
-
Constructor Details
-
EppsteinShortestPathIterator
Constructs an instance of the algorithm for the givengraph,sourceandsink.- Parameters:
graph- graphsource- source vertexsink- sink vertex
-
-
Method Details
-
hasNext
public boolean hasNext() -
next
-
addOneEdgeExtension
Adds all one-edge extension of thepathwrt the paths graph.- Parameters:
path- path to put extensions of
-
addExtension
private void addExtension(EppsteinShortestPathIterator<V, E>.EppsteinGraphPath path, EppsteinShortestPathIterator<V, E>.PathsGraphVertex extendingVertex, double weight) Adds an extension ofpathswithextendingVertexbeing its last element.- Parameters:
path- path to put extension ofextendingVertex- vertex to extend path withweight- weight of the resulting path
-
buildPathsGraph
private void buildPathsGraph()Guides the building process of the paths graph. The process is divided into three stages. First the D(g) is constructed, then cross edges are added and finally the root vertex is created. -
buildDGraph
private void buildDGraph()If thegraphis denoted by $G$, then for every vertex $v$ reachable fromsourcein $G$ $D(G)$ contains balanced heaps of all outroots, which corresponds to vertices on the path from $v$ tosink. If there are no sidetracks on the path from $v$ tosink, the value $null$ is stored. An outroot is connected to its rest heap if the corresponding vertex has more than one sidetrack. -
addCrossEdges
private void addCrossEdges()Adds cross edges for every vertex $v$ reachable from the root of balanced heap ofsourcein the paths graph. If a sidetrack, which corresponds to $v$ connects some pair of vertices $(u,w)$, a cross edge from $v$ to the root of the balanced heap of $w$ is added. -
addPathGraphRoot
private void addPathGraphRoot()Creates the root vertex $r$ of the paths graph and connects it to the root of the balanced heap ofsource. -
insertVertex
Guides the process of adding the sidetracks ofvto the paths graph. First receives the outroot and root of the rest heap ofvby callinggetOutrootAndRestHeapRoot(Object). If the outroot if $null$ maps $v$ topredecessorHeapinhMapping. Otherwise inserts outroot of $v$ in the balanced heap rooted atpredecessorHeapand links it to the received rest heap root.- Parameters:
v- vertexpredecessorHeap- balanced heap root
-
insertPersistently
private EppsteinShortestPathIterator<V,E>.PathsGraphVertex insertPersistently(EppsteinShortestPathIterator<V, E>.PathsGraphVertex root, EppsteinShortestPathIterator<V, E>.PathsGraphVertex vertex) Insertsvertexinto the balanced heap rooted atrootin a persistent (non-destructive) way. Return root of the modified heap.- Parameters:
root- root of a balanced heapvertex- vertex to be inserted- Returns:
- root of the modified heap
-
getOutrootAndRestHeapRoot
private Pair<EppsteinShortestPathIterator<V,E>.PathsGraphVertex, getOutrootAndRestHeapRootEppsteinShortestPathIterator<V, E>.PathsGraphVertex> (V v) Builds outroot and heapification of other sidetracks ofv.- Parameters:
v- vertex- Returns:
- outroot and rest heap root
-
heapify
Builds a min-heap out of theverticeslist- Parameters:
vertices- verticessize- size of vertices
-
siftDown
private void siftDown(List<EppsteinShortestPathIterator<V, E>.PathsGraphVertex> vertices, int i, int size) -
getRestHeap
private EppsteinShortestPathIterator<V,E>.PathsGraphVertex getRestHeap(List<EppsteinShortestPathIterator<V, E>.PathsGraphVertex> vertices, int i, int size) Constructs an explicit tree-like representation of the binary heap contained inverticesstarting at positioni.- Parameters:
vertices- heapified verticesi- heap start positionsize- size of vertices- Returns:
- root of the built heap
-
swap
-
delta
Calculates the $\delta(e)$ value for a given edgee.- Parameters:
e- edge- Returns:
- value of $\delta(e)$
-