Class DijkstraShortestPath<V,E>
- java.lang.Object
-
- edu.uci.ics.jung.algorithms.shortestpath.DijkstraDistance<V,E>
-
- edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath<V,E>
-
- All Implemented Interfaces:
Distance<V>,ShortestPath<V,E>
public class DijkstraShortestPath<V,E> extends DijkstraDistance<V,E> implements ShortestPath<V,E>
Calculates distances and shortest paths using Dijkstra's single-source-shortest-path algorithm. This is a lightweight extension of
DijkstraDistancethat also stores path information, so that the shortest paths can be reconstructed.The elements in the maps returned by
getIncomingEdgeMapare ordered (that is, returned by the iterator) by nondecreasing distance fromsource.- Author:
- Joshua O'Madadhain, Tom Nelson converted to jung2
- See Also:
DijkstraDistance
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classDijkstraShortestPath.SourcePathDataFor a given source vertex, holds the estimated and final distances, tentative and final assignments of incoming edges on the shortest path from the source vertex, and a priority queue (ordered by estimaed distance) of the vertices for which distances are unknown.-
Nested classes/interfaces inherited from class edu.uci.ics.jung.algorithms.shortestpath.DijkstraDistance
DijkstraDistance.SourceData, DijkstraDistance.VertexComparator<V>
-
-
Field Summary
-
Fields inherited from class edu.uci.ics.jung.algorithms.shortestpath.DijkstraDistance
cached, g, max_distance, max_targets, nev, sourceMap
-
-
Constructor Summary
Constructors Constructor Description DijkstraShortestPath(Graph<V,E> g)Creates an instance ofDijkstraShortestPathfor the specified unweighted graph (that is, all weights 1) which caches results locally.DijkstraShortestPath(Graph<V,E> g, boolean cached)Creates an instance ofDijkstraShortestPathfor the specified unweighted graph (that is, all weights 1) which caches results locally.DijkstraShortestPath(Graph<V,E> g, com.google.common.base.Function<E,? extends java.lang.Number> nev)Creates an instance ofDijkstraShortestPathfor the specified graph and the specified method of extracting weights from edges, which caches results locally.DijkstraShortestPath(Graph<V,E> g, com.google.common.base.Function<E,? extends java.lang.Number> nev, boolean cached)Creates an instance ofDijkstraShortestPathfor the specified graph and the specified method of extracting weights from edges, which caches results locally if and only ifcachedistrue.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description EgetIncomingEdge(V source, V target)Returns the last edge on a shortest path fromsourcetotarget, or null iftargetis not reachable fromsource.java.util.Map<V,E>getIncomingEdgeMap(V source)Returns aLinkedHashMapwhich maps each vertex in the graph (including thesourcevertex) to the last edge on the shortest path from thesourcevertex.java.util.LinkedHashMap<V,E>getIncomingEdgeMap(V source, int numDests)Returns aLinkedHashMapwhich maps each of the closestnumDestsvertices to thesourcevertex in the graph (including thesourcevertex) to the incoming edge along the path from that vertex.java.util.List<E>getPath(V source, V target)Returns aListof the edges on the shortest path fromsourcetotarget, in order of their occurrence on this path.protected DijkstraDistance.SourceDatagetSourceData(V source)-
Methods inherited from class edu.uci.ics.jung.algorithms.shortestpath.DijkstraDistance
enableCaching, getDistance, getDistanceMap, getDistanceMap, getDistanceMap, getEdgesToCheck, reset, reset, setMaxDistance, setMaxTargets, singleSourceShortestPath
-
-
-
-
Constructor Detail
-
DijkstraShortestPath
public DijkstraShortestPath(Graph<V,E> g, com.google.common.base.Function<E,? extends java.lang.Number> nev, boolean cached)
Creates an instance of
DijkstraShortestPathfor the specified graph and the specified method of extracting weights from edges, which caches results locally if and only ifcachedistrue.- Parameters:
g- the graph on which distances will be calculatednev- the class responsible for returning weights for edgescached- specifies whether the results are to be cached
-
DijkstraShortestPath
public DijkstraShortestPath(Graph<V,E> g, com.google.common.base.Function<E,? extends java.lang.Number> nev)
Creates an instance of
DijkstraShortestPathfor the specified graph and the specified method of extracting weights from edges, which caches results locally.- Parameters:
g- the graph on which distances will be calculatednev- the class responsible for returning weights for edges
-
DijkstraShortestPath
public DijkstraShortestPath(Graph<V,E> g)
Creates an instance of
DijkstraShortestPathfor the specified unweighted graph (that is, all weights 1) which caches results locally.- Parameters:
g- the graph on which distances will be calculated
-
DijkstraShortestPath
public DijkstraShortestPath(Graph<V,E> g, boolean cached)
Creates an instance of
DijkstraShortestPathfor the specified unweighted graph (that is, all weights 1) which caches results locally.- Parameters:
g- the graph on which distances will be calculatedcached- specifies whether the results are to be cached
-
-
Method Detail
-
getSourceData
protected DijkstraDistance.SourceData getSourceData(V source)
- Overrides:
getSourceDatain classDijkstraDistance<V,E>
-
getIncomingEdge
public E getIncomingEdge(V source, V target)
Returns the last edge on a shortest path from
sourcetotarget, or null iftargetis not reachable fromsource.If either vertex is not in the graph for which this instance was created, throws
IllegalArgumentException.- Parameters:
source- the vertex where the shortest path startstarget- the vertex where the shortest path ends- Returns:
- the last edge on a shortest path from
sourcetotargetor null iftargetis not reachable fromsource
-
getIncomingEdgeMap
public java.util.Map<V,E> getIncomingEdgeMap(V source)
Returns a
LinkedHashMapwhich maps each vertex in the graph (including thesourcevertex) to the last edge on the shortest path from thesourcevertex. The map's iterator will return the elements in order of increasing distance fromsource.- Specified by:
getIncomingEdgeMapin interfaceShortestPath<V,E>- Parameters:
source- the vertex from which distances are measured- Returns:
- a map from vertices to the last edge on the shortest path to that vertex
starting from
source - See Also:
DijkstraDistance.getDistanceMap(Object,int),DijkstraDistance.getDistance(Object,Object)
-
getPath
public java.util.List<E> getPath(V source, V target)
Returns aListof the edges on the shortest path fromsourcetotarget, in order of their occurrence on this path. If either vertex is not in the graph for which this instance was created, throwsIllegalArgumentException.- Parameters:
source- the starting vertex for the path to generatetarget- the ending vertex for the path to generate- Returns:
- the edges on the shortest path from
sourcetotarget, in order of their occurrence
-
getIncomingEdgeMap
public java.util.LinkedHashMap<V,E> getIncomingEdgeMap(V source, int numDests)
Returns a
LinkedHashMapwhich maps each of the closestnumDestsvertices to thesourcevertex in the graph (including thesourcevertex) to the incoming edge along the path from that vertex. Throws anIllegalArgumentExceptionifsourceis not in this instance's graph, or ifnumDestsis either less than 1 or greater than the number of vertices in the graph.- Parameters:
source- the vertex from which distances are measurednumDests- the number of vertices for which to measure distances- Returns:
- a map from each of the closest
numDestsvertices to the last edge on the shortest path to that vertex starting fromsource - See Also:
getIncomingEdgeMap(Object),getPath(Object,Object)
-
-