Package org.graph4j.shortestpath
Class BellmanFordShortestPath
java.lang.Object
org.graph4j.GraphAlgorithm
org.graph4j.shortestpath.BellmanFordShortestPath
- All Implemented Interfaces:
SingleSourceShortestPath
Bellman-Ford-Moore's algorithm finds the shortest paths between a source
vertex and all the other vertices in a graph. It allows some of the edge
weights to be negative numbers, but no negative-weight cycles may exist.
The complexity of this implementation is O(nm), where m is the number of
edges and n the number of vertices.
- Author:
- Cristian Frăsinaru
-
Field Summary
Fields inherited from class org.graph4j.GraphAlgorithm
directed, graph -
Constructor Summary
ConstructorsConstructorDescriptionBellmanFordShortestPath(Graph graph, int source) Creates an algorithm to find all shortest paths starting in the source. -
Method Summary
Modifier and TypeMethodDescriptionfindPath(int target) Returns the shortest path between source and target.doublegetPathWeight(int target) Returns the weight of the shortest path from the source to the target.double[]Returns an array containing the weights of the shortest paths from the source to all vertices.intMethods inherited from class org.graph4j.GraphAlgorithm
getGraphMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.graph4j.shortestpath.SingleSourceShortestPath
computePath, getGraph
-
Constructor Details
-
BellmanFordShortestPath
Creates an algorithm to find all shortest paths starting in the source.- Parameters:
graph- the input graph.source- the source vertex number.
-
-
Method Details
-
getSource
public int getSource()- Specified by:
getSourcein interfaceSingleSourceShortestPath- Returns:
- the source of the paths
-
findPath
Description copied from interface:SingleSourceShortestPathReturns the shortest path between source and target. On the first invocation of this method, it computes all the shortest paths starting in source and then it returns the requested one. All the shortest paths are stored for later retrieval, so subsequent invocations will return the already computed paths.- Specified by:
findPathin interfaceSingleSourceShortestPath- Parameters:
target- the target vertex number.- Returns:
- the shortest path from the source to the target, or null if no path exists.
-
getPathWeight
public double getPathWeight(int target) Description copied from interface:SingleSourceShortestPathReturns the weight of the shortest path from the source to the target. If the algorithm is designed for unweighted graphs, his method returns the number of edges in the path.- Specified by:
getPathWeightin interfaceSingleSourceShortestPath- Parameters:
target- the number of the target vertex- Returns:
- the weight of the shortest path from the source to the target, or
Double.POSTIVE_INFINITYif no path exist.
-
getPathWeights
public double[] getPathWeights()Description copied from interface:SingleSourceShortestPathReturns an array containing the weights of the shortest paths from the source to all vertices. If the algorithm is designed for unweighted graphs, his method returns the number of edges in the each path.- Specified by:
getPathWeightsin interfaceSingleSourceShortestPath- Returns:
- an array containing the weights of the shortest paths from the source to all vertices.
-