Package org.graph4j.shortestpath
Class DijkstraShortestPathBase
java.lang.Object
org.graph4j.GraphAlgorithm
org.graph4j.shortestpath.DijkstraShortestPathBase
- All Implemented Interfaces:
SingleSourceShortestPath
- Direct Known Subclasses:
DijkstraShortestPathDefault,DijkstraShortestPathHeap
public abstract class DijkstraShortestPathBase
extends GraphAlgorithm
implements SingleSourceShortestPath
Dijkstra's algorithm finds the minimum cost paths between a vertex (called
source) and all the other vertices in a graph, with the condition that there
are no negative weighted edges. The cost of a path is the sum of its edges
weights.
If the graph contains a negative weighted edge, an exception will be thrown.
- Author:
- Cristian Frăsinaru
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int[]protected double[]protected intprotected int[]protected boolean[]protected final intprotected final int[]Fields inherited from class org.graph4j.GraphAlgorithm
directed, graph -
Constructor Summary
ConstructorsConstructorDescriptionDijkstraShortestPathBase(Graph graph, int source) Creates an algorithm to find all shortest paths starting in the source. -
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) protected abstract intfindPath(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.intprotected voidpostUpdate(int vi) protected voidMethods 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 -
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
-
DijkstraShortestPathBase
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
-
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.
-
preCompute
protected void preCompute() -
postUpdate
protected void postUpdate(int vi) -
findMinIndex
protected abstract int findMinIndex() -
compute
protected void compute(int target) -
createPathEndingIn
-