Class GraphWalker<N>
- Type Parameters:
N
- the graph node type
Walker.inGraph()
).
Besides pre-order
, post-order
and breadth-first
traversals, also supports topologicalOrderFrom()
, detectCycleFrom()
and
stronglyConnectedComponentsFrom()
.
- Since:
- 4.3
-
Method Summary
Modifier and TypeMethodDescriptionbreadthFirstFrom
(Iterable<? extends N> startNodes) Starts fromstartNodes
and walks in breadth-first order.detectCycleFrom
(Iterable<? extends N> startNodes) Walking fromstartNodes
, detects if the graph has any cycle.detectCycleFrom
(N... startNodes) Walking fromstartNodes
, detects if the graph has any cycle.postOrderFrom
(Iterable<? extends N> startNodes) Starts fromstartNodes
and walks depth first in post-order (the reverse of a topological sort).preOrderFrom
(Iterable<? extends N> startNodes) Starts fromstartNodes
and walks depth first in pre-order.stronglyConnectedComponentsFrom
(Iterable<? extends N> startNodes) Walks the graph by starting fromstartNodes
, and returns a lazy stream of strongly connected components found in the graph.stronglyConnectedComponentsFrom
(N... startNodes) Walks the graph by starting fromstartNodes
, and returns a lazy stream of strongly connected components found in the graph.topologicalOrderFrom
(Iterable<? extends N> startNodes) Fully traverses the graph by starting fromstartNodes
, and returns an immutable list of nodes in topological order.topologicalOrderFrom
(N... startNodes) Fully traverses the graph by starting fromstartNodes
, and returns an immutable list of nodes in topological order.Methods inherited from class com.google.mu.util.graph.Walker
breadthFirstFrom, inBinaryTree, inGraph, inGraph, inTree, postOrderFrom, preOrderFrom
-
Method Details
-
preOrderFrom
Description copied from class:Walker
Starts fromstartNodes
and walks depth first in pre-order.The returned stream may be infinite if the graph or tree has infinite depth or infinite breadth, or both. The stream can still be short-circuited to consume a limited number of nodes during traversal.
- Specified by:
preOrderFrom
in classWalker<N>
-
postOrderFrom
Description copied from class:Walker
Starts fromstartNodes
and walks depth first in post-order (the reverse of a topological sort).The returned stream may be infinite if the graph or tree has infinite breadth. The stream can still be short-circuited to consume a limited number of nodes during traversal.
The stream may result in infinite loop when traversing through a node with infinite depth.
- Specified by:
postOrderFrom
in classWalker<N>
-
breadthFirstFrom
Description copied from class:Walker
Starts fromstartNodes
and walks in breadth-first order.The returned stream may be infinite if the graph or tree has infinite depth or infinite breadth, or both. The stream can still be short-circuited to consume a limited number of nodes during traversal.
- Specified by:
breadthFirstFrom
in classWalker<N>
-
detectCycleFrom
Walking fromstartNodes
, detects if the graph has any cycle.In the following cyclic graph, if starting from node
a
, the detected cyclic path will be:a -> b -> c -> e -> b
, withb -> c -> e -> b
being the cycle, anda -> b
the prefix path leading to the cycle.a -> b -> c -> d ^ / | / |/ e
This method will hang if the given graph is infinite without cycle (the sequence of natural numbers for instance).
- Parameters:
startNodes
- the entry point nodes to start walking the graph.- Returns:
- The stream of nodes starting from the first of
startNodes
that leads to a cycle, ending with nodes along a cyclic path. The last node will also be the starting point of the cycle. That is, ifA
andB
form a cycle, the stream ends withA -> B -> A
. If there is no cycle,Optional.empty()
is returned. - Since:
- 4.3
-
detectCycleFrom
Walking fromstartNodes
, detects if the graph has any cycle.In the following cyclic graph, if starting from node
a
, the detected cyclic path will be:a -> b -> c -> e -> b
, withb -> c -> e -> b
being the cycle, anda -> b
the prefix path leading to the cycle.a -> b -> c -> d ^ / | / |/ e
This method will hang if the given graph is infinite with no cycles (the sequence of natural numbers for instance).
- Parameters:
startNodes
- the entry point nodes to start walking the graph.- Returns:
- The stream of nodes starting from the first of
startNodes
that leads to a cycle, ending with nodes along a cyclic path. The last node will also be the starting point of the cycle. That is, ifA
andB
form a cycle, the stream ends withA -> B -> A
. If there is no cycle,Optional.empty()
is returned. - Since:
- 4.3
-
topologicalOrderFrom
Fully traverses the graph by starting fromstartNodes
, and returns an immutable list of nodes in topological order.Unlike the other
Walker
utilities, this method is not lazy: it has to traverse the entire graph in order to figure out the topological order.- Parameters:
startNodes
- the entry point nodes to start traversing the graph.- Throws:
CyclicGraphException
- if the graph has cycles.- Since:
- 4.3
-
topologicalOrderFrom
Fully traverses the graph by starting fromstartNodes
, and returns an immutable list of nodes in topological order.Unlike the other
Walker
utilities, this method is not lazy: it has to traverse the entire graph in order to figure out the topological order.- Parameters:
startNodes
- the entry point nodes to start traversing the graph.- Throws:
CyclicGraphException
- if the graph has cycles.- Since:
- 4.3
-
stronglyConnectedComponentsFrom
Walks the graph by starting fromstartNodes
, and returns a lazy stream of strongly connected components found in the graph.Implements the Tarjan algorithm in linear time (
O(V + E)
).The strongly connected components (represented by the list of nodes in each component) are returned in a lazy stream, in depth-first post order. If you need topological order from the start nodes, convert it using:
List<List<N>> components = Walker.inGraph(...) .stronglyConnectedComponentsFrom(...) .peek(Collections::reverse) // reverse order within each component .collect(toListAndThen(Collections::reverse)); // reverse order of the components
- Parameters:
startNodes
- the entry point nodes to start traversing the graph.- Since:
- 4.4
-
stronglyConnectedComponentsFrom
Walks the graph by starting fromstartNodes
, and returns a lazy stream of strongly connected components found in the graph.Implements the Tarjan algorithm in linear time (
O(V + E)
).The strongly connected components (represented by the list of nodes in each component) are returned in a lazy stream, in depth-first post order. If you need topological order from the start nodes, convert it using:
List<List<N>> components = Walker.inGraph(...) .stronglyConnectedComponentsFrom(...) .peek(Collections::reverse) // reverse order within each component .collect(toListAndThen(Collections::reverse)); // reverse order of the components
- Parameters:
startNodes
- the entry point nodes to start traversing the graph.- Since:
- 4.4
-