Class DfsEdgeTraverser
Graph, with a hook for every
edge. The hook also sees the edges to nodes already visited.
Like DfsTraverser, it calls enterNode(int) when the search reaches a node and
leaveNode(int) when it leaves the node. In between, it reports each out-edge of the
node to 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 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, dependencies are the
dependencies between the packages of a program, and the search stops at the first dependency
that closes a cycle:
class FirstCycle extends DfsEdgeTraverser {
int closingEdge = -1;
FirstCycle(Graph dependencies) { super(dependencies); }
@Override
protected EdgeAction visitEdge(
int source, int target, int edgeIndex, TargetState state) {
if (state == TargetState.ON_STACK) {
closingEdge = edgeIndex;
return EdgeAction.STOP;
}
return super.visitEdge(source, target, edgeIndex, state);
}
}
var firstCycle = new FirstCycle(dependencies);
firstCycle.run();
// firstCycle.closingEdge: 10 - the edge storage -> model
Graphs.findCycle(Graph) 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
IllegalStateException. After a run that ends with an exception, the state of the
instance is undefined. A search that needs no hook per edge is faster on DfsTraverser.
Instances are mutable and not safe to share between threads.
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.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDfsEdgeTraverser(Graph graph) Creates a traverser forgraph, with no node visited. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanenterNode(int node) Called when the search reachesnode, before its out-edges: the preorder hook.protected final booleanisOnStack(int node) Returns whethernodeis on the stack of the search, that is, on the current path.protected final booleanisVisited(int node) Returns whether the search has reachednode, in this run or in an earlier one.protected voidleaveNode(int node) Called when the search leavesnode, after all its out-edges have been reported and searched: the postorder hook.final voidrun()Searches from every node not visited yet, in ascending order, until every node is visited.final voidrun(int startNode) Searches fromstartNode, visiting the nodes it reaches that are not visited yet.final voidSearches from each ofstartNodesin list order, skipping those visited already.protected final voidstop()Stops the run in progress: the run returns as soon as the hook that calls this method returns, without calling another hook.protected DfsEdgeTraverser.EdgeActionvisitEdge(int source, int target, int edgeIndex, DfsEdgeTraverser.TargetState state) Called 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.
-
Field Details
-
graph
-
-
Constructor Details
-
DfsEdgeTraverser
Creates a traverser forgraph, with no node visited.Time: O(nodeCount).
- Parameters:
graph- the graph to search
-
-
Method Details
-
enterNode
Called when the search reachesnode, before its out-edges: the preorder hook. Returnstrueunless overridden.The search calls it once per node: for a start node, or for the target of an edge for which
visitEdgereturnedDESCENDwhile the target was unvisited. From this call on,nodecounts as visited and is on the current path. Returningfalseskips the out-edges ofnode: none of them is reported, andleaveNode(int)follows right away.- Parameters:
node- node ID in0..nodeCount-1- Returns:
trueto report the out-edges ofnode,falseto skip them- In the guide:
-
leaveNode
Called when the search leavesnode, 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
enterNode(int)if that returnedfalse. From this call on,nodeis no longer on the current path. After the run is stopped, it is not called for the nodes still on the path.- Parameters:
node- node ID in0..nodeCount-1- In the guide:
-
visitEdge
protected DfsEdgeTraverser.EdgeAction visitEdge(int source, int target, int edgeIndex, DfsEdgeTraverser.TargetState state) Called 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
sourcein edge index order, betweenenterNode(source)andleaveNode(source). It reports no out-edge of a node whoseenterNodereturnedfalse. 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, returnsDESCENDfor an unvisited target andSKIPfor any other.- Parameters:
source- the source of the edge, which is on the current pathtarget- the target of the edgeedgeIndex- edge index of the edgesource -> targetstate- the state oftarget- Returns:
- what the search does with the edge
- In the guide:
-
isVisited
Returns whether the search has reachednode, in this run or in an earlier one.Time: O(1).
- Parameters:
node- node ID in0..nodeCount-1; for any other value the method returnsfalseor throwsIndexOutOfBoundsException- Returns:
trueifenterNode(int)has been called fornode- In the guide:
-
isOnStack
Returns whethernodeis 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
enterNode(int)until itsleaveNode(int). Outside a run, no node is on the path.Time: O(1).
- Parameters:
node- node ID in0..nodeCount-1; for any other value the method returnsfalseor throwsIndexOutOfBoundsException- Returns:
trueifnodeis on the current path- In the guide:
-
stop
Stops 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
visitEdge, it does the same as returningSTOP.leaveNode(int)is not called for the nodes on the current path, but they count as finished afterwards. This includes a node whoseenterNode(int)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. To cancel a run from another thread, let a hook check a flag of your own and call this method.Time: O(1).
- In the guide:
-
run
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.
- Throws:
IllegalArgumentException- if a depth-first path is longer than 230 - 5 nodesIllegalStateException- if called from a hook, during a run of this instance- In the guide:
-
run
Searches fromstartNode, visiting the nodes it reaches that are not visited yet.If
startNodeis visited already, nothing happens.Time: O(reachable nodes + reachable edges), plus the hooks.
- Parameters:
startNode- node ID in0..nodeCount-1- Throws:
IndexOutOfBoundsException- ifstartNodeis out of rangeIllegalArgumentException- if a depth-first path is longer than 230 - 5 nodesIllegalStateException- if called from a hook, during a run of this instance- In the guide:
-
run
Searches from each ofstartNodesin 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.
STOPandstop()end the whole call, not only the search from one start node.Time: O(startNodes.size() + reachable nodes + reachable edges), plus the hooks.
- Parameters:
startNodes- node IDs, each in0..nodeCount-1; not to be changed until the call returns- Throws:
IndexOutOfBoundsException- if a start node is out of rangeIllegalArgumentException- if a depth-first path is longer than 230 - 5 nodesIllegalStateException- if called from a hook, during a run of this instance- In the guide:
-