Package org.jgrapht.alg.shortestpath
Class BellmanFordIterator<V,E>
- java.lang.Object
-
- org.jgrapht.alg.shortestpath.BellmanFordIterator<V,E>
-
- All Implemented Interfaces:
java.util.Iterator<java.util.List<V>>
class BellmanFordIterator<V,E> extends java.lang.Object implements java.util.Iterator<java.util.List<V>>Helper class forBellmanFordShortestPath; not intended for general use.
-
-
Field Summary
Fields Modifier and Type Field Description private doubleepsilonprotected Graph<V,E>graphGraph on which shortest paths are searched.protected static java.lang.StringNEGATIVE_UNDIRECTED_EDGEError message.private java.util.List<V>prevImprovedVerticesVertices whose shortest path cost have been improved during the previous pass.private java.util.Map<V,BellmanFordPathElement<V,E>>prevVertexDataprotected VstartVertexStart vertex.private booleanstartVertexEncounteredprivate java.util.Map<V,BellmanFordPathElement<V,E>>vertexDataStores the vertices that have been seen during iteration and (optionally) some additional traversal info regarding each vertex.
-
Constructor Summary
Constructors Modifier Constructor Description protectedBellmanFordIterator(Graph<V,E> graph, V startVertex, double epsilon)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidassertBellmanFordIterator(Graph<V,E> graph, V startVertex)protected voidassertValidEdge(E edge)protected doublecalculatePathCost(V vertex, E edge)Costs taken into account are the weights stored inEdgeobjects.private BellmanFordPathElement<V,E>createSeenData(V vertex, E edge, double cost)The first time we see a vertex, make up a new entry for it.protected java.util.Iterator<E>edgesOfIterator(V vertex)Returns an iterator to loop over outgoing edgesEdgeof the vertex.private voidencounterStartVertex()BellmanFordPathElement<V,E>getPathElement(V endVertex)Returns the path element of the shortest path with less thannMaxHopsedges between the start vertex and the end vertex.protected BellmanFordPathElement<V,E>getPrevSeenData(V vertex)Access the data stored for a seen vertex in the previous pass.protected BellmanFordPathElement<V,E>getSeenData(V vertex)Access the data stored for a seen vertex in the current pass.booleanhasNext()protected booleanisSeenVertex(V vertex)Determines whether a vertex has been seen yet by this traversal.java.util.List<V>next()Returns the listCollectionof vertices whose path has been improved during the current pass.protected BellmanFordPathElement<V,E>putPrevSeenData(V vertex, BellmanFordPathElement<V,E> data)protected BellmanFordPathElement<V,E>putSeenData(V vertex, BellmanFordPathElement<V,E> data)Stores iterator-dependent data for a vertex that has been seen during the current pass.private voidrelaxVertex(V vertex, E edge)Upates data first time a vertex is reached by a path.private booleanrelaxVertexAgain(V vertex, E edge)Check if the cost of the best path so far reaching the specified vertex could be improved if the vertex is reached through the specified edge.voidremove()Unsupportedprivate voidsavePassData(java.util.List<V> improvedVertices)
-
-
-
Field Detail
-
NEGATIVE_UNDIRECTED_EDGE
protected static final java.lang.String NEGATIVE_UNDIRECTED_EDGE
Error message.- See Also:
- Constant Field Values
-
startVertex
protected V startVertex
Start vertex.
-
prevImprovedVertices
private java.util.List<V> prevImprovedVertices
Vertices whose shortest path cost have been improved during the previous pass.
-
prevVertexData
private java.util.Map<V,BellmanFordPathElement<V,E>> prevVertexData
-
startVertexEncountered
private boolean startVertexEncountered
-
vertexData
private java.util.Map<V,BellmanFordPathElement<V,E>> vertexData
Stores the vertices that have been seen during iteration and (optionally) some additional traversal info regarding each vertex.
-
epsilon
private double epsilon
-
-
Method Detail
-
getPathElement
public BellmanFordPathElement<V,E> getPathElement(V endVertex)
Returns the path element of the shortest path with less thannMaxHopsedges between the start vertex and the end vertex.- Parameters:
endVertex- end vertex.- Returns:
- .
-
hasNext
public boolean hasNext()
- Specified by:
hasNextin interfacejava.util.Iterator<V>- Returns:
trueif at least one path has been improved during the previous pass,falseotherwise.
-
next
public java.util.List<V> next()
Returns the listCollectionof vertices whose path has been improved during the current pass.- Specified by:
nextin interfacejava.util.Iterator<V>- See Also:
Iterator.next()
-
remove
public void remove()
Unsupported- Specified by:
removein interfacejava.util.Iterator<V>- See Also:
Iterator.remove()
-
assertValidEdge
protected void assertValidEdge(E edge)
- Parameters:
edge-- Throws:
java.lang.IllegalArgumentException- if the graph is undirected and the edge-weight is negative.
-
calculatePathCost
protected double calculatePathCost(V vertex, E edge)
Costs taken into account are the weights stored inEdgeobjects.- Parameters:
vertex- a vertex which has just been encountered.edge- the edge via which the vertex was encountered.- Returns:
- the cost obtained by concatenation.
- See Also:
Graph#getEdgeWeight(E)
-
edgesOfIterator
protected java.util.Iterator<E> edgesOfIterator(V vertex)
Returns an iterator to loop over outgoing edgesEdgeof the vertex.- Parameters:
vertex-- Returns:
- .
-
getPrevSeenData
protected BellmanFordPathElement<V,E> getPrevSeenData(V vertex)
Access the data stored for a seen vertex in the previous pass.- Parameters:
vertex- a vertex which has already been seen.- Returns:
- data associated with the seen vertex or
nullif no data was associated with the vertex.
-
getSeenData
protected BellmanFordPathElement<V,E> getSeenData(V vertex)
Access the data stored for a seen vertex in the current pass.- Parameters:
vertex- a vertex which has already been seen.- Returns:
- data associated with the seen vertex or
nullif no data was associated with the vertex.
-
isSeenVertex
protected boolean isSeenVertex(V vertex)
Determines whether a vertex has been seen yet by this traversal.- Parameters:
vertex- vertex in question.- Returns:
- true if vertex has already been seen.
-
putPrevSeenData
protected BellmanFordPathElement<V,E> putPrevSeenData(V vertex, BellmanFordPathElement<V,E> data)
- Parameters:
vertex-data-- Returns:
- .
-
putSeenData
protected BellmanFordPathElement<V,E> putSeenData(V vertex, BellmanFordPathElement<V,E> data)
Stores iterator-dependent data for a vertex that has been seen during the current pass.- Parameters:
vertex- a vertex which has been seen.data- data to be associated with the seen vertex.- Returns:
- previous value associated with specified vertex or
nullif no data was associated with the vertex.
-
createSeenData
private BellmanFordPathElement<V,E> createSeenData(V vertex, E edge, double cost)
The first time we see a vertex, make up a new entry for it.- Parameters:
vertex- a vertex which has just been encountered.edge- the edge via which the vertex was encountered.cost- cost of the created path element.- Returns:
- the new entry.
-
encounterStartVertex
private void encounterStartVertex()
-
relaxVertex
private void relaxVertex(V vertex, E edge)
Upates data first time a vertex is reached by a path.- Parameters:
vertex- a vertex which has just been encountered.edge- the edge via which the vertex was encountered.
-
relaxVertexAgain
private boolean relaxVertexAgain(V vertex, E edge)
Check if the cost of the best path so far reaching the specified vertex could be improved if the vertex is reached through the specified edge.- Parameters:
vertex- a vertex which has just been encountered.edge- the edge via which the vertex was encountered.- Returns:
trueif the cost has been improved,falseotherwise.
-
savePassData
private void savePassData(java.util.List<V> improvedVertices)
-
-