|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectedu.uci.ics.jung.algorithms.shortestpath.ShortestPath
edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath
Calculates distances in a specified graph, using
Dijkstra's single-source-shortest-path algorithm. All edge weights
in the graph must be nonnegative; if any edge with negative weight is
found in the course of calculating distances, an
IllegalArgumentException
will be thrown.
(Note: this exception will only be thrown when such an edge would be
used to update a given tentative distance;
the algorithm does not check for negative-weight edges "up front".)
Distances and partial results are optionally cached (by this instance) for later reference. Thus, if the 10 closest vertices to a specified source vertex are known, calculating the 20 closest vertices does not require starting Dijkstra's algorithm over from scratch.
Distances are stored as double-precision values.
If a vertex is not reachable from the specified source vertex, no
distance is stored. This is new behavior with version 1.4;
the previous behavior was to store a value of
Double.POSITIVE_INFINITY
. This change gives the algorithm
an approximate complexity of O(kD log k), where k is either the number of
requested targets or the number of reachable vertices (whichever is smaller),
and D is the average degree of a vertex.
The elements in the maps returned by getDistanceMap
and getIncomingEdgeMap
are ordered (that is, returned
by the iterator) by nondecreasing distance from source
.
Users are cautioned that distances calculated should be assumed to
be invalidated by changes to the graph, and should invoke reset()
when appropriate so that the distances can be recalculated.
Constructor Summary | |
DijkstraShortestPath(Graph g)
|
|
DijkstraShortestPath(Graph g,
NumberEdgeValue nev)
|
|
DijkstraShortestPath(Graph g,
NumberEdgeValue nev,
boolean cached)
Creates an instance of DijkstraShortestPath for
the specified graph and the specified method of extracting weights
from edges. |
Method Summary | |
void |
enableCaching(boolean enable)
Specifies whether or not this instance of DijkstraShortestPath
should cache its results (final and partial) for future reference; |
java.lang.Number |
getDistance(Vertex source,
Vertex target)
Returns the length of a shortest path from the source to the target vertex, or null if the target is not reachable from the source. |
java.util.Map |
getDistanceMap(Vertex source)
Returns a LinkedHashMap which maps each vertex
in the graph (including the source vertex)
to its distance from the source vertex.
|
java.util.LinkedHashMap |
getDistanceMap(Vertex source,
int numDests)
Returns a LinkedHashMap which maps each of the closest
numDist vertices to the source vertex
in the graph (including the source vertex)
to its distance from the source vertex. |
Edge |
getIncomingEdge(Vertex source,
Vertex target)
Returns the last edge on a shortest path from source
to target , or null if target is not
reachable from source . |
java.util.Map |
getIncomingEdgeMap(Vertex source)
Returns a LinkedHashMap which maps each vertex
in the graph (including the source vertex)
to the last edge on the shortest path from the
source vertex.
|
java.util.LinkedHashMap |
getIncomingEdgeMap(Vertex source,
int numDests)
Returns a LinkedHashMap which maps each of the closest
numDist vertices to the source vertex
in the graph (including the source vertex)
to the incoming edge along the path from that vertex. |
java.util.List |
getPath(Vertex source,
Vertex target)
Returns a List of the edges on the shortest path from
source to target , in order of their
occurrence on this path.
|
void |
reset()
Clears all stored distances for this instance. |
void |
reset(Vertex source)
Clears all stored distances for the specified source vertex source . |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public DijkstraShortestPath(Graph g, NumberEdgeValue nev, boolean cached)
Creates an instance of DijkstraShortestPath
for
the specified graph and the specified method of extracting weights
from edges.
g
- the graph on which distances will be calculatedpublic DijkstraShortestPath(Graph g, NumberEdgeValue nev)
public DijkstraShortestPath(Graph g)
Method Detail |
public java.lang.Number getDistance(Vertex source, Vertex target)
IllegalArgumentException
.
getDistance
in interface Distance
getDistanceMap(Vertex)
,
getDistanceMap(Vertex,int)
public Edge getIncomingEdge(Vertex source, Vertex target)
Returns the last edge on a shortest path from source
to target
, or null if target
is not
reachable from source
.
If either vertex is not in the graph for which this instance
was created, throws IllegalArgumentException
.
public java.util.Map getDistanceMap(Vertex source)
Returns a LinkedHashMap
which maps each vertex
in the graph (including the source
vertex)
to its distance from the source
vertex.
The map's iterator will return the elements in order of
increasing distance from source
.
The size of the map returned will be the number of
vertices reachable from source
.
getDistanceMap
in interface Distance
source
- the vertex from which distances are measuredgetDistanceMap(Vertex,int)
,
getDistance(Vertex,Vertex)
public java.util.Map getIncomingEdgeMap(Vertex source)
Returns a LinkedHashMap
which maps each vertex
in the graph (including the source
vertex)
to the last edge on the shortest path from the
source
vertex.
The map's iterator will return the elements in order of
increasing distance from source
.
getIncomingEdgeMap
in class ShortestPath
source
- the vertex from which distances are measuredgetDistanceMap(Vertex,int)
,
getDistance(Vertex,Vertex)
public java.util.List getPath(Vertex source, Vertex target)
List
of the edges on the shortest path from
source
to target
, in order of their
occurrence on this path.
If either vertex is not in the graph for which this instance
was created, throws IllegalArgumentException
.
getPath
in class ShortestPath
public java.util.LinkedHashMap getDistanceMap(Vertex source, int numDests)
Returns a LinkedHashMap
which maps each of the closest
numDist
vertices to the source
vertex
in the graph (including the source
vertex)
to its distance from the source
vertex. Throws
an IllegalArgumentException
if source
is not in this instance's graph, or if numDests
is
either less than 1 or greater than the number of vertices in the
graph.
The size of the map returned will be the smaller of
numDests
and the number of vertices reachable from
source
.
source
- the vertex from which distances are measurednumDests
- the number of vertices for which to measure distancesgetDistanceMap(Vertex)
,
getDistance(Vertex,Vertex)
public java.util.LinkedHashMap getIncomingEdgeMap(Vertex source, int numDests)
Returns a LinkedHashMap
which maps each of the closest
numDist
vertices to the source
vertex
in the graph (including the source
vertex)
to the incoming edge along the path from that vertex. Throws
an IllegalArgumentException
if source
is not in this instance's graph, or if numDests
is
either less than 1 or greater than the number of vertices in the
graph.
source
- the vertex from which distances are measurednumDests
- the number of vertics for which to measure distances
getIncomingEdgeMap(Vertex)
,
getPath(Vertex,Vertex)
public void reset()
reset(Vertex)
may be appropriate instead.
reset(Vertex)
public void enableCaching(boolean enable)
DijkstraShortestPath
should cache its results (final and partial) for future reference;
enable
- true
if the results are to be cached, and
false
otherwisepublic void reset(Vertex source)
source
. Should be called whenever the stored distances
from this vertex are invalidated by changes to the graph.
reset()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |