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
ProtectedenterCalled when the search reaches node, before its out-edges: the preorder hook. Returns true
unless overridden.
The search calls it once per node: for a start node, or for the target of an edge for which
DfsEdgeTraverser.visitEdge returned EdgeAction.DESCEND while the target was
unvisited. From this call on, node counts as visited and is on the current path. Returning
false skips the out-edges of node: none of them is reported, and
DfsEdgeTraverser.leaveNode follows right away.
node ID in 0..nodeCount-1
true to report the out-edges 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 DfsEdgeTraverser.enterNode until its DfsEdgeTraverser.leaveNode. 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 DfsEdgeTraverser.enterNode has been called for node
ProtectedleaveCalled when the search leaves node, after all its out-edges have been reported and searched:
the postorder hook. Does nothing unless overridden.
The search calls it once for every node it has visited, right after
DfsEdgeTraverser.enterNode if that returned false. From this call on, node is no
longer on the current path. After the run is stopped, 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. EdgeAction.STOP and DfsEdgeTraverser.stop end 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.
It works from every hook; from DfsEdgeTraverser.visitEdge, it does the same as returning EdgeAction.STOP. DfsEdgeTraverser.leaveNode is not called for the nodes on the current path, but they count as finished afterwards. This includes a node whose DfsEdgeTraverser.enterNode called this method. 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).
ProtectedvisitCalled for each out-edge of a node that the search has entered, with the state of its target; returns what the search does with the edge.
The search reports the out-edges of source in edge index order, between enterNode(source)
and leaveNode(source). It reports no out-edge of a node whose enterNode returned false.
The state is that of the target when the edge is reported: an unvisited target that one edge
skips is reported as unvisited again by the next edge to it. Unless overridden, returns
EdgeAction.DESCEND for an unvisited target and EdgeAction.SKIP for any other. A
value that is not one of EdgeAction, such as undefined, makes the run throw an
Error (Java: a NullPointerException for null).
the source of the edge, which is on the current path
the target of the edge
edge index of the edge source -> target
the state of target
what the search does with the edge
The base class of a depth-first search of your own over a Graph, with a hook for every edge. The hook also sees the edges to nodes already visited.
Like DfsTraverser, it calls DfsEdgeTraverser.enterNode when the search reaches a node and DfsEdgeTraverser.leaveNode when it leaves the node. In between, it reports each out-edge of the node to DfsEdgeTraverser.visitEdge, in edge index order, with the state of the target: unvisited, on the current path or finished. The hook returns what the search does with the edge: descend into an unvisited target and search from it before the next edge, skip the edge, or stop the run; any hook can stop the run with DfsEdgeTraverser.stop.
By default, the search descends into every unvisited target, and the hooks come in the order of a textbook recursive depth-first search, as in DfsTraverser. The search itself runs without recursion and keeps its stack on the heap. An edge to a target on the current path closes a cycle. Here,
dependenciesare the dependencies between the packages of a program, and the search stops at the first dependency that closes a cycle:Graphs.findCycle returns such a cycle as a list of nodes. The state of a target does not tell a forward edge from a cross edge; EdgeTypes classifies every edge.
The runs work as in DfsTraverser. The visited nodes add up over the runs of an instance. A hook must not start another run of the same instance, which throws an
Error(Java:IllegalStateException). After a run that ends with an error, the state of the instance is undefined. A search that needs no hook per edge is faster on DfsTraverser.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.