- Type Parameters:
V- the graph vertex typeE- the graph edge type
The algorithm runs in two phases. Preprocessing decorates each edge with the minimum number of
edges still needed to reach a target. Enumeration then walks the decorated edges from the source
set, using the decoration to abandon any partial path that cannot complete within the budget. By
default a forward BFS from the source set is also run as part of preprocessing, so that edges
whose source endpoint is not reachable from any requested source within the budget are dropped
from the decoration up front. This can substantially reduce preprocessing cost on graphs where a
non-trivial fraction of the target-reachable subgraph is not reachable from the sources. See
setForwardPruning(boolean) for how to disable it.
- Author:
- Andrew Gainer-Dewar, Google LLC
-
Constructor Summary
ConstructorsConstructorDescriptionAllDirectedPaths(Graph<V, E> graph) Create a new instance.AllDirectedPaths(Graph<V, E> graph, PathValidator<V, E> pathValidator) Create a new instance with givenpathValidator. -
Method Summary
Modifier and TypeMethodDescriptiongetAllPaths(Set<V> sourceVertices, Set<V> targetVertices, boolean simplePathsOnly, Integer maxPathLength) Calculate (and return) all paths from the source vertices to the target vertices.getAllPaths(V sourceVertex, V targetVertex, boolean simplePathsOnly, Integer maxPathLength) Calculate (and return) all paths from the source vertex to the target vertex.booleanWhether the preprocessing step currently applies the forward-pruning optimisation.voidsetForwardPruning(boolean forwardPruning) Configure whether the preprocessing step applies the forward-pruning optimisation.
-
Constructor Details
-
AllDirectedPaths
Create a new instance.- Parameters:
graph- the input graph- Throws:
IllegalArgumentException- if the graph is not directed
-
AllDirectedPaths
Create a new instance with givenpathValidator. If non-null, thepathValidatorwill be used while searching for paths, validating the addition of any edge to a partial path. Zero-length paths will therefore not be subject topathValidator; length-1 paths will.- Parameters:
graph- the input graphpathValidator- validator for computed paths; may be null- Throws:
IllegalArgumentException- if the graph is not directed
-
-
Method Details
-
setForwardPruning
public void setForwardPruning(boolean forwardPruning) Configure whether the preprocessing step applies the forward-pruning optimisation.When forward pruning is enabled (the default),
getAllPaths(V, V, boolean, java.lang.Integer)first runs a forward BFS from the source set and uses the result to drop edges whose source endpoint is not reachable from any source within the bound, or whose forward-plus-backward length exceeds the bound. The prune is exact — it never drops an edge that could lie on a feasible source-to-target walk — and can be a large win when a substantial fraction of the graph is backward-reachable from the targets but not forward-reachable from the sources. On graphs where the prune never fires (e.g. small dense strongly-connected digraphs), the optimisation adds the cost of one extraO(V + E)BFS pergetAllPathscall.Setting forward pruning to
falserecovers the historical preprocessing behaviour exactly — useful when the cost of the extra BFS is known to dominate.- Parameters:
forwardPruning- whether to apply the forward-pruning preprocessing step
-
isForwardPruning
public boolean isForwardPruning()Whether the preprocessing step currently applies the forward-pruning optimisation.- Returns:
trueif forward pruning is enabled- See Also:
-
getAllPaths
public List<GraphPath<V,E>> getAllPaths(V sourceVertex, V targetVertex, boolean simplePathsOnly, Integer maxPathLength) Calculate (and return) all paths from the source vertex to the target vertex.- Parameters:
sourceVertex- the source vertextargetVertex- the target vertexsimplePathsOnly- if true, only search simple (non-self-intersecting) pathsmaxPathLength- maximum number of edges to allow in a path (if null, all paths are considered, which may be very slow due to potentially huge output)- Returns:
- all paths from the source vertex to the target vertex
-
getAllPaths
public List<GraphPath<V,E>> getAllPaths(Set<V> sourceVertices, Set<V> targetVertices, boolean simplePathsOnly, Integer maxPathLength) Calculate (and return) all paths from the source vertices to the target vertices.- Parameters:
sourceVertices- the source verticestargetVertices- the target verticessimplePathsOnly- if true, only search simple (non-self-intersecting) pathsmaxPathLength- maximum number of edges to allow in a path (if null, all paths are considered, which may be very slow due to potentially huge output)- Returns:
- list of all paths from the sources to the targets containing no more than maxPathLength edges
-