AbstractCreates a traverser for graph, with no node visited.
Public, unlike Java's protected constructor: the class is abstract, so only a subclass can be instantiated, and a subclass without a constructor of its own stays instantiable.
Time: O(nodeCount).
the graph to search
Protected AbstractenterCalled when the search reaches node, before its out-nodes: the preorder hook.
The search calls it once per node: for a start node, or for an unvisited out-node of the node
it is searching. From this call on, node counts as visited and is on the current path.
Returning true lets the search go on with the out-nodes of node. Returning false skips
them, and DfsTraverser.leaveNode follows right away; out-nodes skipped this way stay
unvisited unless the search reaches them from another node.
node ID in 0..nodeCount-1
true to search from the out-nodes of node, false to skip them
ProtectedisReturns whether node is on the stack of the search, that is, on the current path.
The current path leads from the node where the search started to the node being searched. A node is on the path from its DfsTraverser.enterNode until its DfsTraverser.leaveNode. An edge from the node being searched to a node on the path closes a cycle. Outside a run, no node is on the path.
Time: O(1).
node ID in 0..nodeCount-1; for any other value the method returns false, also
for one that is not an integer
true if node is on the current path
ProtectedisReturns whether the search has reached node, in this run or in an earlier one.
Time: O(1).
node ID in 0..nodeCount-1; for any other value the method returns false, also
for one that is not an integer
true if DfsTraverser.enterNode has been called for node
ProtectedleaveCalled when the search leaves node, after it has searched from all its out-nodes: the
postorder hook. Does nothing unless overridden.
The search calls it once for every node it has visited, right after
DfsTraverser.enterNode if that returned false. From this call on, node is no longer
on the current path. After DfsTraverser.stop, it is not called for the nodes still on
the path.
node ID in 0..nodeCount-1
Searches from every node not visited yet, in ascending order, until every node is visited.
A node that no earlier search has reached when its turn comes starts a search of its own.
Time: O(nodeCount + edgeCount), plus the hooks.
Searches from startNode, visiting the nodes it reaches that are not visited yet.
If startNode is visited already, nothing happens.
Time: O(reachable nodes + reachable edges), plus the hooks.
node ID in 0..nodeCount-1
Searches from each of startNodes in list order, skipping those visited already.
The list is checked before the first hook, so a start node out of range leaves the traverser unchanged. DfsTraverser.stop ends the whole call, not only the search from one start node.
Time: O(number of start nodes + reachable nodes + reachable edges), plus the hooks.
node IDs, each in 0..nodeCount-1; not to be changed until the call returns
ProtectedstopStops the run in progress: the run returns as soon as the hook that calls this method returns, without calling another hook.
DfsTraverser.leaveNode is not called for the nodes on the current path, but they count as visited and as no longer on the path. A later run skips every node visited so far; it does not go on where the stopped run ended. Called outside a run, this method has no effect.
Time: O(1).
The base class of a depth-first search of your own over a Graph. A subclass overrides the hooks DfsTraverser.enterNode and DfsTraverser.leaveNode, and a
runmethod starts the search.The search visits each node it reaches once. It calls DfsTraverser.enterNode when it reaches a node (preorder) and DfsTraverser.leaveNode when it has searched from all out-nodes of the node (postorder). It takes the out-nodes of each node in ascending order, so the hooks come in the order of a textbook recursive depth-first search. The search itself runs without recursion and keeps its stack on the heap, so a deep graph does not overflow the call stack. Here,
callsare the calls between the methods of a program, and the postorder puts each method after the methods it calls, except along calls that close a cycle:Between its two hooks, a node is on the current path. The current path leads from the node where the search started to the node being searched. The hooks may call DfsTraverser.isVisited and DfsTraverser.isOnStack, end the search at a node by returning
falsefrom DfsTraverser.enterNode, and end the whole run with DfsTraverser.stop.run()searches from every node in ascending order,run(startNode)from one node andrun(startNodes)from several, such as the root nodes that Graphs.rootNodes returns. The visited nodes add up over the runs of an instance: a node is visited at most once in the lifetime of the instance, and a later run skips it. A hook must not start another run of the same instance, which throws anError(Java:IllegalStateException). If a run ends with an error from a hook or from the search, the state of the instance is undefined: nodes of the interrupted path may still count as on the path, for example. A new instance is needed for further runs.DfsEdgeTraverser also calls a hook for every edge, which suits searches that look at edges to nodes already visited. This class calls no such hook, which makes it the faster choice where node hooks suffice. FrontierTraverser searches breadth-first. Common searches are ready-made, such as TopOrder, EdgeTypes, BackEdges and Graphs.findCycle.
Instances are mutable.
Memory: 2 bits per node for the visited state, and up to 8 x nodeCount bytes for the stack, which grows with the deepest path searched.