Class Dijkstra<E>
java.lang.Object
rgu.transport.algorithms.search.Dijkstra<E>
- All Implemented Interfaces:
ReachabilityAlgorithm<E>, RoutingAlgorithm<E>, SpatialTemporalReachabilityAlgorithm<E>, SpatialTemporalRoutingAlgorithm<E>
public final class Dijkstra<E>
extends Object
implements RoutingAlgorithm<E>, ReachabilityAlgorithm<E>, SpatialTemporalRoutingAlgorithm<E>, SpatialTemporalReachabilityAlgorithm<E>
A thread-safe and generic implementation of Dijkstra's shortest path algorithm. Instances are
typically obtained by calling
ofInt(), ofLong(), ofDouble(),
ofDuration(), etc. depending on the desired edge type. The generic constructor
Dijkstra(Object, BinaryOperator, BiPredicate) method enables use of Dijkstra
on other types not listed. Dijkstra uses the BinaryMinHeap class.- Author:
- Lee A. Christie
-
Constructor Summary
ConstructorsConstructorDescriptionDijkstra(E zero, BinaryOperator<E> add, BiPredicate<E, E> lessThan) Creates an instance of Dijkstra which works on the specified generic type of edges. -
Method Summary
Modifier and TypeMethodDescription<V> Ecost(Explorable<V, E> graph, List<V> path) Reconstructs the total cost of a given path.Returns an instance of Dijkstra which works on distance edges.ofDouble()Returns an instance of Dijkstra which works on double edges.Returns an instance of Dijkstra which works on duration edges.ofInt()Returns an instance of Dijkstra which works on integer edges.ofLong()Returns an instance of Dijkstra which works on long edges.Returns an instance of Dijkstra which works on duration edges where edges are all negative but treated as positive.<V> List<V> path(Explorable<V, E> graph, V source, Predicate<V> target) Finds a route from the specified source to the specified target(s) by predicate which returns true if a given vertex is a target.<V> List<V> path(Explorable<V, E> graph, V source, Predicate<V> target, E maxCost) Finds a route from the specified source to the specified target(s) by predicate which returns true if a given vertex is a target, within a given maximum cost.<V> List<V> path(Explorable<V, E> graph, V source, V target) Finds a route from the specified source to the specified target.<V> List<V> path(Explorable<V, E> graph, V source, V target, E maxCost) Finds a route from the specified source to the specified target within a given maximum cost.reachable(Explorable<V, E> graph, V source) Returns the set of reachable vertices and the corresponding cost of each vertex's shortest path.reachable(Explorable<V, E> graph, V source, E maxCost) Returns the set of vertices reachable within a given maximum cost, and the corresponding cost of each vertex's shortest path.<V> voidreachable(Explorable<V, E> graph, V source, E maxCost, ReachabilityCallback<V, E> callback) Streams to a callback function the set of vertices reachable within a given maximum cost, and the corresponding cost of each vertex's shortest path.<V> voidreachable(Explorable<V, E> graph, V source, ReachabilityCallback<V, E> callback) Streams to a callback function the set of reachable vertices and the corresponding cost of each vertex's shortest path.<ST,S> List <ST> stPath(Explorable<ST, E> graph, ST source, Predicate<ST> target, E maxCost, SpatialTemporalMapping<S, E, ST> stMapping) Finds a route from the specified source to the specified target(s) by predicate which returns true if a given vertex is a target, within a given maximum cost.<ST,S> List <ST> stPath(Explorable<ST, E> graph, ST source, Predicate<ST> target, SpatialTemporalMapping<S, E, ST> stMapping) Finds a route from the specified source to the specified target(s) by predicate which returns true if a given vertex is a target.<ST,S> List <ST> stPath(Explorable<ST, E> graph, ST source, ST target, E maxCost, SpatialTemporalMapping<S, E, ST> stMapping) Finds a route from the specified source to the specified target within a given maximum cost.<ST,S> List <ST> stPath(Explorable<ST, E> graph, ST source, ST target, SpatialTemporalMapping<S, E, ST> stMapping) Finds a route from the specified source to the specified target.<ST,S> void stReachable(Explorable<ST, E> graph, ST source, E maxCost, ReachabilityCallback<ST, E> callback, SpatialTemporalMapping<S, E, ST> stMapping) Streams to a callback function the set of vertices reachable within a given maximum cost, and the corresponding cost of each vertex's shortest path.stReachable(Explorable<ST, E> graph, ST source, E maxCost, SpatialTemporalMapping<S, E, ST> stMapping) Returns the set of vertices reachable within a given maximum cost, and the corresponding cost of each vertex's shortest path.<ST,S> void stReachable(Explorable<ST, E> graph, ST source, ReachabilityCallback<ST, E> callback, SpatialTemporalMapping<S, E, ST> stMapping) Streams to a callback function the set of reachable vertices and the corresponding cost of each vertex's shortest path.stReachable(Explorable<ST, E> graph, ST source, SpatialTemporalMapping<S, E, ST> stMapping) Returns the set of reachable vertices and the corresponding cost of each vertex's shortest path.
-
Constructor Details
-
Dijkstra
Creates an instance of Dijkstra which works on the specified generic type of edges. Operators must be pure and the zero immutable for a valid thread-safe instance to be returned.- Parameters:
zero- an immutable instance of E which represents the constant zero, not nulladd- a pure binary operator which returns a + b, not nulllessThan- a pure predicate which tests whether a < b, not null
-
-
Method Details
-
ofLong
-
ofDouble
-
ofInt
-
ofDuration
-
ofNegativeDuration
-
ofDistance
-
cost
Reconstructs the total cost of a given path.- Specified by:
costin interfaceRoutingAlgorithm<E>- Type Parameters:
V- the vertex type- Parameters:
graph- the graph on which to route, not nullpath- a list of vertices describing each step in the path, no null- Returns:
- the total cost of the path
-
path
public <V> List<V> path(Explorable<V, E> graph, V source, V target) throws TargetUnreachableException, InterruptedExceptionFinds a route from the specified source to the specified target.- Specified by:
pathin interfaceRoutingAlgorithm<E>- Type Parameters:
V- the vertex type- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not nulltarget- the target vertex, in graph, not null- Returns:
- a list of vertices describing each step in the path
- Throws:
TargetUnreachableException- if no route could be found from source to targetInterruptedException- if the thread is interrupted
-
path
public <V> List<V> path(Explorable<V, E> graph, V source, V target, E maxCost) throws TargetUnreachableException, InterruptedExceptionFinds a route from the specified source to the specified target within a given maximum cost.- Specified by:
pathin interfaceRoutingAlgorithm<E>- Type Parameters:
V- the vertex type- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not nulltarget- the target vertex, in graph, not nullmaxCost- the maximum cost, considered unlimited if null- Returns:
- a list of vertices describing each step in the path
- Throws:
TargetUnreachableException- if no route could be found from source to targetInterruptedException- if the thread is interrupted
-
path
public <V> List<V> path(Explorable<V, E> graph, V source, Predicate<V> target) throws TargetUnreachableException, InterruptedExceptionFinds a route from the specified source to the specified target(s) by predicate which returns true if a given vertex is a target.- Specified by:
pathin interfaceRoutingAlgorithm<E>- Type Parameters:
V- the vertex type- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not nulltarget- the criteria to identify a target vertex, in graph, not null- Returns:
- a list of vertices describing each step in the path
- Throws:
TargetUnreachableException- if no route could be found from source to targetInterruptedException- if the thread is interrupted
-
path
public <V> List<V> path(Explorable<V, E> graph, V source, Predicate<V> target, E maxCost) throws TargetUnreachableException, InterruptedExceptionFinds a route from the specified source to the specified target(s) by predicate which returns true if a given vertex is a target, within a given maximum cost.- Specified by:
pathin interfaceRoutingAlgorithm<E>- Type Parameters:
V- the vertex type- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not nulltarget- the target vertex, in graph, not nullmaxCost- the maximum cost, considered unlimited if null- Returns:
- a list of vertices describing each step in the path
- Throws:
TargetUnreachableException- if no route could be found from source to targetInterruptedException- if the thread is interrupted
-
stPath
public <ST,S> List<ST> stPath(Explorable<ST, E> graph, ST source, ST target, SpatialTemporalMapping<S, throws TargetUnreachableException, InterruptedExceptionE, ST> stMapping) Finds a route from the specified source to the specified target.- Specified by:
stPathin interfaceSpatialTemporalRoutingAlgorithm<E>- Type Parameters:
ST- the type of the spatial-temporal vertexS- the type of the spatial component of the vertex- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not nulltarget- the target vertex, in graph, not nullstMapping- a spatial-temporal mapping- Returns:
- a list of vertices describing each step in the path
- Throws:
TargetUnreachableException- if no route could be found from source to targetInterruptedException- if the thread is interrupted
-
stPath
public <ST,S> List<ST> stPath(Explorable<ST, E> graph, ST source, ST target, E maxCost, SpatialTemporalMapping<S, throws TargetUnreachableException, InterruptedExceptionE, ST> stMapping) Finds a route from the specified source to the specified target within a given maximum cost.- Specified by:
stPathin interfaceSpatialTemporalRoutingAlgorithm<E>- Type Parameters:
ST- the type of the spatial-temporal vertexS- the type of the spatial component of the vertex- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not nulltarget- the target vertex, in graph, not nullmaxCost- the maximum cost, considered unlimited if nullstMapping- a spatial-temporal mapping- Returns:
- a list of vertices describing each step in the path
- Throws:
TargetUnreachableException- if no route could be found from source to targetInterruptedException- if the thread is interrupted
-
stPath
public <ST,S> List<ST> stPath(Explorable<ST, E> graph, ST source, Predicate<ST> target, SpatialTemporalMapping<S, throws TargetUnreachableException, InterruptedExceptionE, ST> stMapping) Finds a route from the specified source to the specified target(s) by predicate which returns true if a given vertex is a target.- Specified by:
stPathin interfaceSpatialTemporalRoutingAlgorithm<E>- Type Parameters:
ST- the type of the spatial-temporal vertexS- the type of the spatial component of the vertex- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not nulltarget- the criteria to identify a target vertex, in graph, not nullstMapping- a spatial-temporal mapping- Returns:
- a list of vertices describing each step in the path
- Throws:
TargetUnreachableException- if no route could be found from source to targetInterruptedException- if the thread is interrupted
-
stPath
public <ST,S> List<ST> stPath(Explorable<ST, E> graph, ST source, Predicate<ST> target, E maxCost, SpatialTemporalMapping<S, throws TargetUnreachableException, InterruptedExceptionE, ST> stMapping) Finds a route from the specified source to the specified target(s) by predicate which returns true if a given vertex is a target, within a given maximum cost.- Specified by:
stPathin interfaceSpatialTemporalRoutingAlgorithm<E>- Type Parameters:
ST- the type of the spatial-temporal vertexS- the type of the spatial component of the vertex- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not nulltarget- the target vertex, in graph, not nullmaxCost- the maximum cost, considered unlimited if nullstMapping- a spatial-temporal mapping- Returns:
- a list of vertices describing each step in the path
- Throws:
TargetUnreachableException- if no route could be found from source to targetInterruptedException- if the thread is interrupted
-
reachable
Returns the set of reachable vertices and the corresponding cost of each vertex's shortest path.- Specified by:
reachablein interfaceReachabilityAlgorithm<E>- Type Parameters:
V- the vertex type- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not null- Returns:
- a map of reachable vertices, with associated costs
- Throws:
InterruptedException- if the thread is interrupted
-
reachable
public <V> Map<V,E> reachable(Explorable<V, E> graph, V source, E maxCost) throws InterruptedExceptionReturns the set of vertices reachable within a given maximum cost, and the corresponding cost of each vertex's shortest path.- Specified by:
reachablein interfaceReachabilityAlgorithm<E>- Type Parameters:
V- the vertex type- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not nullmaxCost- the maximum cost, considered unlimited if null- Returns:
- a map of reachable vertices, with associated costs
- Throws:
InterruptedException- if the thread is interrupted
-
reachable
public <V> void reachable(Explorable<V, E> graph, V source, ReachabilityCallback<V, throws InterruptedExceptionE> callback) Streams to a callback function the set of reachable vertices and the corresponding cost of each vertex's shortest path.- Specified by:
reachablein interfaceReachabilityAlgorithm<E>- Type Parameters:
V- the vertex type- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not nullcallback- a callback which acts as a consumer of reachable vertices, with associated costs, but returnsReachabilityCallback.Action.BREAKif the algorithm should abandon the search orReachabilityCallback.Action.CONTINUEotherwise, not null- Throws:
InterruptedException- if the thread is interrupted
-
reachable
public <V> void reachable(Explorable<V, E> graph, V source, E maxCost, ReachabilityCallback<V, throws InterruptedExceptionE> callback) Streams to a callback function the set of vertices reachable within a given maximum cost, and the corresponding cost of each vertex's shortest path.- Specified by:
reachablein interfaceReachabilityAlgorithm<E>- Type Parameters:
V- the vertex type- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not nullmaxCost- the maximum cost, considered unlimited if nullcallback- a callback which acts as a consumer of reachable vertices, with associated costs, but returnsReachabilityCallback.Action.BREAKif the algorithm should abandon the search orReachabilityCallback.Action.CONTINUEotherwise, not null- Throws:
InterruptedException- if the thread is interrupted
-
stReachable
public <ST,S> Map<ST,E> stReachable(Explorable<ST, E> graph, ST source, SpatialTemporalMapping<S, throws InterruptedExceptionE, ST> stMapping) Returns the set of reachable vertices and the corresponding cost of each vertex's shortest path.- Specified by:
stReachablein interfaceSpatialTemporalReachabilityAlgorithm<E>- Type Parameters:
ST- the type of the spatial-temporal vertexS- the type of the spatial component of the vertex- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not nullstMapping- a spatial-temporal mapping- Returns:
- a map of reachable vertices, with associated costs
- Throws:
InterruptedException- if the thread is interrupted
-
stReachable
public <ST,S> Map<ST,E> stReachable(Explorable<ST, E> graph, ST source, E maxCost, SpatialTemporalMapping<S, throws InterruptedExceptionE, ST> stMapping) Returns the set of vertices reachable within a given maximum cost, and the corresponding cost of each vertex's shortest path.- Specified by:
stReachablein interfaceSpatialTemporalReachabilityAlgorithm<E>- Type Parameters:
ST- the type of the spatial-temporal vertexS- the type of the spatial component of the vertex- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not nullmaxCost- the maximum cost, considered unlimited if nullstMapping- a spatial-temporal mapping- Returns:
- a map of reachable vertices, with associated costs
- Throws:
InterruptedException- if the thread is interrupted
-
stReachable
public <ST,S> void stReachable(Explorable<ST, E> graph, ST source, ReachabilityCallback<ST, throws InterruptedExceptionE> callback, SpatialTemporalMapping<S, E, ST> stMapping) Streams to a callback function the set of reachable vertices and the corresponding cost of each vertex's shortest path.- Specified by:
stReachablein interfaceSpatialTemporalReachabilityAlgorithm<E>- Type Parameters:
ST- the type of the spatial-temporal vertexS- the type of the spatial component of the vertex- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not nullcallback- a callback which acts as a consumer of reachable vertices, with associated costs, but returnsReachabilityCallback.Action.BREAKif the algorithm should abandon the search orReachabilityCallback.Action.CONTINUEotherwise, not nullstMapping- a spatial-temporal mapping- Throws:
InterruptedException- if the thread is interrupted
-
stReachable
public <ST,S> void stReachable(Explorable<ST, E> graph, ST source, E maxCost, ReachabilityCallback<ST, throws InterruptedExceptionE> callback, SpatialTemporalMapping<S, E, ST> stMapping) Streams to a callback function the set of vertices reachable within a given maximum cost, and the corresponding cost of each vertex's shortest path.- Specified by:
stReachablein interfaceSpatialTemporalReachabilityAlgorithm<E>- Type Parameters:
ST- the type of the spatial-temporal vertexS- the type of the spatial component of the vertex- Parameters:
graph- the graph on which to route, not nullsource- the source vertex, in graph, not nullmaxCost- the maximum cost, considered unlimited if nullcallback- a callback which acts as a consumer of reachable vertices, with associated costs, but returnsReachabilityCallback.Action.BREAKif the algorithm should abandon the search orReachabilityCallback.Action.CONTINUEotherwise, not nullstMapping- a spatial-temporal mapping- Throws:
InterruptedException- if the thread is interrupted
-