Class Algorithms<V>
java.lang.Object
com.github.yellowstonegames.path.Algorithms<V>
- Type Parameters:
V- the vertex type; oftenCoord
- Direct Known Subclasses:
DirectedGraphAlgorithms, UndirectedGraphAlgorithms
Most of the algorithms that operate on a
This class is not meant to be extended from outside of this package, so it uses a package-private implementation.
Graph are defined here, with some specific cases in subclasses.
The most-frequently-used algorithm here is probably findShortestPath(Object, Object, ObjectDeque, Heuristic),
or some overload of it; this is a standard A-Star algorithm backed by a priority queue that is very fast. Somewhat
surprisingly, it doesn't need indices like gdx-ai's IndexedAStarPathFinder needs, and this makes the code easier to
use. There's a good amount of other useful algorithms here, like containsCycle() and various non-A-Star search
methods, like breadthFirstSearch(Object, int, int) and depthFirstSearch(Object, int, int).
This class is not meant to be extended from outside of this package, so it uses a package-private implementation.
-
Method Summary
Modifier and TypeMethodDescriptionPerform a breadth first search starting from the specified vertex.breadthFirstSearch(V v, int maxVertices, int maxDepth) Perform a breadth first search starting from the specified vertex.booleanChecks whether there are any cycles in the graph using depth first searches.Perform a depth first search starting from the specified vertex.depthFirstSearch(V v, int maxVertices, int maxDepth) Perform a depth first search starting from the specified vertex.floatfindMinimumDistance(V start, V target) Find the length of the shortest path between the start and target vertices, using Dijkstra's algorithm implemented with a priority queue.floatfindMinimumDistance(V start, V target, Heuristic<V> heuristic) Find the length of the shortest path between the start and target vertices, using Dijkstra's algorithm implemented with a priority queue.com.github.tommyettinger.ds.ObjectDeque<V> findShortestPath(V start, V target) Find the shortest path between the start and target vertices, using Dijkstra's algorithm implemented with a priority queue.booleanfindShortestPath(V start, V target, com.github.tommyettinger.ds.ObjectDeque<V> path, Heuristic<V> heuristic) Find the shortest path between the start and target vertices, using the A* search algorithm with the provided heuristic, and implemented with a priority queue.com.github.tommyettinger.ds.ObjectDeque<V> findShortestPath(V start, V target, Heuristic<V> heuristic) Find the shortest path between the start and target vertices, using the A* search algorithm with the provided heuristic, and implemented with a priority queue.booleanisConnected(V start, V target) Checks whether there exists a path from the start vertex to target vertex, using Dijkstra's algorithm implemented with a priority queue.intGets the identifier for the last run of an algorithm; this should mostly be usable internally, but may be useful when different runs have affected a graph and aNode.lastRunIDmay need to be checked against this.voidsetRunID(int newID) Sets the identifier for the last run of an algorithm; this should mostly be usable internally, but may be useful when serializing and deserializing a Graph and you, for whatever reason, need to ensure previous runs of an algorithm on that Graph can be resumed.
-
Method Details
-
findShortestPath
Find the shortest path between the start and target vertices, using Dijkstra's algorithm implemented with a priority queue.- Parameters:
start- the starting vertextarget- the target vertex- Returns:
- a list of vertices from start to target containing the ordered vertices of a shortest path, including both the start and target vertices
-
findShortestPath
public com.github.tommyettinger.ds.ObjectDeque<V> findShortestPath(V start, V target, Heuristic<V> heuristic) Find the shortest path between the start and target vertices, using the A* search algorithm with the provided heuristic, and implemented with a priority queue.- Parameters:
start- the starting vertextarget- the target vertex- Returns:
- a list of vertices from start to target containing the ordered vertices of a shortest path, including both the start and target vertices
-
findShortestPath
public boolean findShortestPath(V start, V target, com.github.tommyettinger.ds.ObjectDeque<V> path, Heuristic<V> heuristic) Find the shortest path between the start and target vertices, using the A* search algorithm with the provided heuristic, and implemented with a priority queue. Fills path with a list of vertices from start to target containing the ordered vertices of a shortest path, including both the start and target vertices.- Parameters:
start- the starting vertextarget- the target vertexpath- the list of vertices to which the path vertices should be added- Returns:
- true if a path was found, or false if no path could be found
-
findMinimumDistance
Find the length of the shortest path between the start and target vertices, using Dijkstra's algorithm implemented with a priority queue.- Parameters:
start- the starting vertextarget- the target vertex- Returns:
- the sum of the weights in a shortest path from the starting vertex to the target vertex
If there is no path from the start vertex to the target vertex,
Float.MAX_VALUEis returned.
-
findMinimumDistance
Find the length of the shortest path between the start and target vertices, using Dijkstra's algorithm implemented with a priority queue.- Parameters:
start- the starting vertextarget- the target vertex- Returns:
- the sum of the weights in a shortest path from the starting vertex to the target vertex
If there is no path from the start vertex to the target vertex,
Float.MAX_VALUEis returned.
-
isConnected
Checks whether there exists a path from the start vertex to target vertex, using Dijkstra's algorithm implemented with a priority queue.- Parameters:
start- the starting vertextarget- the target vertex- Returns:
- whether there exists a path from the start vertex to target vertex
-
breadthFirstSearch
Perform a breadth first search starting from the specified vertex.- Parameters:
v- the vertex at which to start the searchmaxVertices- the maximum number of vertices to process before terminating the searchmaxDepth- the maximum edge distance (the number of edges in a shortest path between vertices) a vertex should have to be considered for processing. If a vertex has a distance larger than the maxDepth, it will not be added to the returned graph- Returns:
- a Graph object containing all the processed vertices, and the edges from which each vertex was encountered.
The vertices and edges in the returned graph will be in the order they were encountered in the search, and this will be
reflected in the iteration order of the collections returned by
Graph.getVertices()andGraph.getEdges().
-
breadthFirstSearch
Perform a breadth first search starting from the specified vertex.- Parameters:
v- the vertex at which to start the search- Returns:
- a Graph object containing all the processed vertices (all the vertices in this graph), and the edges from which each vertex was encountered.
The vertices and edges in the returned graph will be in the order they were encountered in the search, and this will be
reflected in the iteration order of the collections returned by
Graph.getVertices()andGraph.getEdges().
-
depthFirstSearch
Perform a depth first search starting from the specified vertex.- Parameters:
v- the vertex at which to start the searchmaxVertices- the maximum number of vertices to process before terminating the searchmaxDepth- the maximum edge distance (the number of edges in a shortest path between vertices) a vertex should have to be considered for processing. If a vertex has a distance larger than the maxDepth, it will not be added to the returned graph- Returns:
- a Graph object containing all the processed vertices, and the edges from which each vertex was encountered.
The vertices and edges in the returned graph will be in the order they were encountered in the search, and this will be
reflected in the iteration order of the collections returned by
Graph.getVertices()andGraph.getEdges().
-
depthFirstSearch
Perform a depth first search starting from the specified vertex.- Parameters:
v- the vertex at which to start the search- Returns:
- a Graph object containing all the processed vertices (all the vertices in this graph), and the edges from which each vertex was encountered.
The vertices and edges in the returned graph will be in the order they were encountered in the search, and this will be
reflected in the iteration order of the collections returned by
Graph.getVertices()andGraph.getEdges().
-
containsCycle
public boolean containsCycle()Checks whether there are any cycles in the graph using depth first searches.- Returns:
- true if the graph contains a cycle, false otherwise
-
lastRunID
public int lastRunID()Gets the identifier for the last run of an algorithm; this should mostly be usable internally, but may be useful when different runs have affected a graph and aNode.lastRunIDmay need to be checked against this.- Returns:
- an identifier for which algorithm ran last, as a probably-unique int
-
setRunID
public void setRunID(int newID) Sets the identifier for the last run of an algorithm; this should mostly be usable internally, but may be useful when serializing and deserializing a Graph and you, for whatever reason, need to ensure previous runs of an algorithm on that Graph can be resumed.- Parameters:
newID- an identifier for which algorithm ran last, as a probably-unique int
-