Package squidpony.squidai.graph
Class Algorithms<V>
java.lang.Object
squidpony.squidai.graph.Algorithms<V>
- Type Parameters:
V
- the vertex type; oftenCoord
- Direct Known Subclasses:
DirectedGraphAlgorithms
,UndirectedGraphAlgorithms
public class Algorithms<V> extends Object
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, ArrayList, 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 detectCycle()
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.
- Author:
- earlygrey
-
Method Summary
Modifier and Type Method Description Graph<V>
breadthFirstSearch(V v)
Perform a breadth first search starting from the specified vertex.Graph<V>
breadthFirstSearch(V v, int maxVertices, int maxDepth)
Perform a breadth first search starting from the specified vertex.Graph<V>
depthFirstSearch(V v)
Perform a depth first search starting from the specified vertex.Graph<V>
depthFirstSearch(V v, int maxVertices, int maxDepth)
Perform a depth first search starting from the specified vertex.boolean
detectCycle()
Checks whether there are any cycles in the graph using depth first searches.double
findMinimumDistance(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.ArrayList<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.boolean
findShortestPath(V start, V target, ArrayList<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.ArrayList<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.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.lastRunID
may need to be checked against this.
-
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
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
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
-
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()
.
-
detectCycle
Checks whether there are any cycles in the graph using depth first searches.- Returns:
- true if the graph contains a cycle, false otherwise
-
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.lastRunID
may need to be checked against this.- Returns:
- an identifier for which algorithm ran last, as a probably-unique int
-