V
- the graph vertex typeE
- the graph edge typepublic class BellmanFordShortestPath<V,E> extends Object
Computes shortest paths from a single source vertex to all other vertices in a weighted graph. The Bellman-Ford algorithm supports negative edge weights.
Negative weight cycles are not allowed and will be reported by the algorithm. This implies that
negative edge weights are not allowed in undirected graphs. In such cases the code will throw an
exception of type NegativeCycleDetectedException
which will contain the detected negative
weight cycle. Note that the algorithm will not report or find negative weight cycles which are
not reachable from the source vertex.
The running time is $O(|E||V|)$.
ShortestPathAlgorithm.SingleSourcePaths<V,E>
Modifier and Type | Field and Description |
---|---|
protected Comparator<Double> |
comparator |
protected Graph<V,E> |
graph
The underlying graph.
|
protected static String |
GRAPH_CONTAINS_A_NEGATIVE_WEIGHT_CYCLE
Error message for reporting the existence of a negative-weight cycle.
|
protected static String |
GRAPH_MUST_CONTAIN_THE_SINK_VERTEX
Error message for reporting that a sink vertex is missing.
|
protected static String |
GRAPH_MUST_CONTAIN_THE_SOURCE_VERTEX
Error message for reporting that a source vertex is missing.
|
protected int |
maxHops |
Constructor and Description |
---|
BellmanFordShortestPath(Graph<V,E> graph)
Construct a new instance.
|
BellmanFordShortestPath(Graph<V,E> graph,
double epsilon)
Construct a new instance.
|
BellmanFordShortestPath(Graph<V,E> graph,
double epsilon,
int maxHops)
Construct a new instance.
|
Modifier and Type | Method and Description |
---|---|
protected GraphPath<V,E> |
createEmptyPath(V source,
V sink)
Create an empty path.
|
static <V,E> GraphPath<V,E> |
findPathBetween(Graph<V,E> graph,
V source,
V sink)
Find a path between two vertices.
|
GraphPath<V,E> |
getPath(V source,
V sink)
Get a shortest path from a source vertex to a sink vertex.
|
ShortestPathAlgorithm.SingleSourcePaths<V,E> |
getPaths(V source)
Compute all shortest paths starting from a single source vertex.
|
double |
getPathWeight(V source,
V sink)
Get the weight of the shortest path from a source vertex to a sink vertex.
|
protected final Comparator<Double> comparator
protected final int maxHops
protected static final String GRAPH_CONTAINS_A_NEGATIVE_WEIGHT_CYCLE
protected static final String GRAPH_MUST_CONTAIN_THE_SOURCE_VERTEX
protected static final String GRAPH_MUST_CONTAIN_THE_SINK_VERTEX
protected final Graph<V,E> graph
public BellmanFordShortestPath(Graph<V,E> graph)
graph
- the input graphpublic BellmanFordShortestPath(Graph<V,E> graph, double epsilon)
graph
- the input graphepsilon
- tolerance when comparing floating point valuespublic BellmanFordShortestPath(Graph<V,E> graph, double epsilon, int maxHops)
graph
- the input graphepsilon
- tolerance when comparing floating point valuesmaxHops
- execute the algorithm for at most this many iterations. If this is smaller
than the number of vertices, then the negative cycle detection feature is disabled.IllegalArgumentException
- if the number of maxHops is not positivepublic GraphPath<V,E> getPath(V source, V sink)
source
- the source vertexsink
- the target vertexNegativeCycleDetectedException
- in case a negative weight cycle is detectedpublic ShortestPathAlgorithm.SingleSourcePaths<V,E> getPaths(V source)
getPaths
in interface ShortestPathAlgorithm<V,E>
source
- the source vertexNegativeCycleDetectedException
- in case a negative weight cycle is detectedpublic static <V,E> GraphPath<V,E> findPathBetween(Graph<V,E> graph, V source, V sink)
V
- the graph vertex typeE
- the graph edge typegraph
- the graph to be searchedsource
- the vertex at which the path should startsink
- the vertex at which the path should endpublic double getPathWeight(V source, V sink)
Double.POSITIVE_INFINITY
if no path exists.getPathWeight
in interface ShortestPathAlgorithm<V,E>
source
- the source vertexsink
- the sink vertexDouble.POSITIVE_INFINITY
if no path existsprotected final GraphPath<V,E> createEmptyPath(V source, V sink)
source
- the source vertexsink
- the sink vertexCopyright © 2019. All rights reserved.