Package org.graph4j.shortestpath
Class BFSSingleSourceShortestPath
java.lang.Object
org.graph4j.GraphAlgorithm
org.graph4j.shortestpath.BFSSingleSourceShortestPath
- All Implemented Interfaces:
SingleSourceShortestPath
Determines the shortest paths from a source vertex to all other vertices, in
an unweighted graph, using a breadth-first traversal.
- Author:
- Cristian Frăsinaru
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int[]protected double[]protected final intFields inherited from class org.graph4j.GraphAlgorithm
directed, graph -
Constructor Summary
ConstructorsConstructorDescriptionBFSSingleSourceShortestPath(Graph graph, int source) Creates an algorithm to find all shortest paths starting in the specified source, in an unweighted graph. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcompute(int target) computePath(int target) Attempts at finding the shortest path from the source to the target without computing all the paths.protected PathcreatePathEndingIn(int vi) findPath(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
getGraph
-
Field Details
-
source
protected final int source -
dist
protected double[] dist -
before
protected int[] before
-
-
Constructor Details
-
BFSSingleSourceShortestPath
Creates an algorithm to find all shortest paths starting in the specified source, in an unweighted graph. If the input graph has weights on its edges, they are ignored.- 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
-
computePath
Description copied from interface:SingleSourceShortestPathAttempts at finding the shortest path from the source to the target without computing all the paths. It may be implemented by starting the computation for all the shortest paths and stopping the algorithm when the shortest path to the target is found. If a implementation does not have this ability, it simply invokesgetPathinstead.- Specified by:
computePathin interfaceSingleSourceShortestPath- Parameters:
target- the target vertex number.- Returns:
- the shortest path from the source to the target.
-
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.
-
compute
protected void compute(int target) -
createPathEndingIn
-