Package org.graph4j.shortestpath
Class BFSSinglePairShortestPath
java.lang.Object
org.graph4j.GraphAlgorithm
org.graph4j.shortestpath.BFSSinglePairShortestPath
- All Implemented Interfaces:
SinglePairShortestPath
Determines the path with the fewest edges connecting two vertices. For
unweighted graphs, breadth-first search can be used to find the shortest path
between two vertices.
- Author:
- Cristian Frăsinaru
-
Field Summary
Fields inherited from class org.graph4j.GraphAlgorithm
directed, graph -
Constructor Summary
ConstructorsConstructorDescriptionBFSSinglePairShortestPath(Graph graph, int source, int target) Creates an algorithm to find the path with the fewest edges between two specified vertices.BFSSinglePairShortestPath(Graph graph, int source, int target, int[] forbiddenVertices) Creates an algorithm to find the path with the fewest edges between two specified vertices, not passing through some forbidden vertices. -
Method Summary
Modifier and TypeMethodDescriptionfindPath()Returns the shortest path between source and target.doubleReturns the weight of the shortest path from the source to the target, orDouble.POSITIVE_INFINITYif no path exists.intReturns the source vertex number.intReturns the target vertex number.Methods 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.SinglePairShortestPath
getGraph
-
Constructor Details
-
BFSSinglePairShortestPath
Creates an algorithm to find the path with the fewest edges between two specified vertices. If the input graph has weights on its edges, they are ignored.- Parameters:
graph- the input graph.source- the source vertex number.target- the target vertex number.
-
BFSSinglePairShortestPath
Creates an algorithm to find the path with the fewest edges between two specified vertices, not passing through some forbidden vertices.- Parameters:
graph- the input graph.source- the source vertex number.target- the target vertex number.forbiddenVertices- vertices that are not allowed in the path; can benullif there are no forbidden vertices.
-
-
Method Details
-
getSource
public int getSource()Description copied from interface:SinglePairShortestPathReturns the source vertex number.- Specified by:
getSourcein interfaceSinglePairShortestPath- Returns:
- the source vertex number.
-
getTarget
public int getTarget()Description copied from interface:SinglePairShortestPathReturns the target vertex number.- Specified by:
getTargetin interfaceSinglePairShortestPath- Returns:
- the target vertex number.
-
findPath
Description copied from interface:SinglePairShortestPathReturns the shortest path between source and target.- Specified by:
findPathin interfaceSinglePairShortestPath- Returns:
- the shortest path from the source to the target, or null if no path exists
-
getPathWeight
public double getPathWeight()Description copied from interface:SinglePairShortestPathReturns the weight of the shortest path from the source to the target, orDouble.POSITIVE_INFINITYif no path exists.- Specified by:
getPathWeightin interfaceSinglePairShortestPath- Returns:
- the weight of the shortest path from the source to the target, or
Double.POSITIVE_INFINITYif no path exists.
-