Package org.graph4j.shortestpath
Class AStarAlgorithm
java.lang.Object
org.graph4j.GraphAlgorithm
org.graph4j.shortestpath.AStarAlgorithm
- All Implemented Interfaces:
SinglePairShortestPath
A* algorithm finds the shortest path from a specified source vertex to a
specified target vertex. The algorithm is guided by a heuristic function h(v)
that estimates the distance from the vertex v to the target.
Dijkstra's algorithm can be viewd as a special case of A* where no heuristic
function is available: h(v)=0, for all v.
- Author:
- Cristian Ivan, Cristian Frăsinaru
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int[]protected double[]protected intprotected int[]protected boolean[]protected final intprotected final intprotected final int[]Fields inherited from class org.graph4j.GraphAlgorithm
directed, graph -
Constructor Summary
ConstructorsConstructorDescriptionAStarAlgorithm(Graph graph, int source, int target, AStarEstimator heuristic) Creates an algorithm to find the shortest path between source and target. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcompute()protected PathcreatePathEndingIn(int vi) findPath()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
-
Field Details
-
source
protected final int source -
target
protected final int target -
vertices
protected final int[] vertices -
cost
protected double[] cost -
before
protected int[] before -
size
protected int[] size -
solved
protected boolean[] solved -
numSolved
protected int numSolved
-
-
Constructor Details
-
AStarAlgorithm
Creates an algorithm to find the shortest path between source and target.- Parameters:
graph- the input graph.source- the source vertex number.target- the target vertex number.heuristic- the estimated distance from a vertex to the target.
-
-
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.
-
compute
protected void compute() -
createPathEndingIn
-