Class DfsEdgeTraverser
- Namespace
- Graphity
- Assembly
- Graphity.Core.dll
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.
public abstract class DfsEdgeTraverser
- Inheritance
-
DfsEdgeTraverser
- Inherited Members
Examples
Here, dependencies are the dependencies between the packages of a program, and the search
stops at the first dependency that closes a cycle:
private sealed class FirstCycle(Graph dependencies) : DfsEdgeTraverser(dependencies)
{
public int ClosingEdge { get; private set; } = -1;
protected override EdgeAction VisitEdge(
int source, int target, int edgeIndex, TargetState state)
{
if (state == TargetState.OnStack)
{
ClosingEdge = edgeIndex;
return EdgeAction.Stop;
}
return base.VisitEdge(source, target, edgeIndex, state);
}
}
private static int RunFirstCycle(Graph dependencies)
{
var firstCycle = new FirstCycle(dependencies);
firstCycle.Run();
// firstCycle.ClosingEdge: 10 - the edge storage -> model
return firstCycle.ClosingEdge;
}
Remarks
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(int, int, int, TargetState), 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. 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 InvalidOperationException. 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.
Constructors
DfsEdgeTraverser(Graph)
Creates a traverser for graph, with no node visited.
protected DfsEdgeTraverser(Graph graph)
Parameters
graphGraphThe graph to search.
Remarks
Time: O(NodeCount).
Properties
Graph
Gets the graph that this traverser searches.
protected Graph Graph { get; }
Property Value
Methods
EnterNode(int)
Called when the search reaches node, before its out-edges: the preorder hook.
Returns true unless overridden.
protected virtual bool EnterNode(int node)
Parameters
nodeintNode ID in
0..NodeCount-1.
Returns
Remarks
The search calls it once per node: for a start node, or for the target of an edge for which
VisitEdge(int, int, int, TargetState) returned 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 LeaveNode(int) follows right away.
IsOnStack(int)
Returns whether node is on the stack of the search, that is, on the current
path.
protected bool IsOnStack(int node)
Parameters
nodeintNode ID in
0..NodeCount-1.
Returns
Remarks
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 its LeaveNode(int). Outside a run, no node is on the path.
Time: O(1).
Exceptions
- ArgumentOutOfRangeException
nodeis out of range.
IsVisited(int)
Returns whether the search has reached node, in this run or in an earlier one.
protected bool IsVisited(int node)
Parameters
nodeintNode ID in
0..NodeCount-1.
Returns
- bool
true if EnterNode(int) has been called for
node.
Remarks
Time: O(1).
Exceptions
- ArgumentOutOfRangeException
nodeis out of range.
LeaveNode(int)
Called when the search leaves node, after all its out-edges have been reported
and searched: the postorder hook. Does nothing unless overridden.
protected virtual void LeaveNode(int node)
Parameters
nodeintNode ID in
0..NodeCount-1.
Remarks
The search calls it once for every node it has visited, right after EnterNode(int) 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.
Run()
Searches from every node not visited yet, in ascending order, until every node is visited.
public void Run()
Remarks
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.
Exceptions
- ArgumentException
A depth-first path is longer than 2^30 - 29 nodes.
- InvalidOperationException
Called from a hook, during a run of this instance.
Run(int)
Searches from startNode, visiting the nodes it reaches that are not visited
yet.
public void Run(int startNode)
Parameters
startNodeintNode ID in
0..NodeCount-1.
Remarks
If startNode is visited already, nothing happens.
Time: O(reachable nodes + reachable edges), plus the hooks.
Exceptions
- ArgumentOutOfRangeException
startNodeis out of range.- ArgumentException
A depth-first path is longer than 2^30 - 29 nodes.
- InvalidOperationException
Called from a hook, during a run of this instance.
Run(ReadOnlySpan<int>)
Searches from each of startNodes in list order, skipping those visited already.
public void Run(ReadOnlySpan<int> startNodes)
Parameters
startNodesReadOnlySpan<int>Node IDs, each in
0..NodeCount-1; not to be changed until the call returns.
Remarks
The list is checked before the first hook, so a start node out of range leaves the traverser unchanged. Stop and Stop() end the whole call, not only the search from one start node.
Time: O(startNodes.Length + reachable nodes + reachable edges), plus the hooks.
Exceptions
- ArgumentOutOfRangeException
A start node is out of range.
- ArgumentException
A depth-first path is longer than 2^30 - 29 nodes.
- InvalidOperationException
Called from a hook, during a run of this instance.
Stop()
Stops the run in progress: the run returns as soon as the hook that calls this method returns, without calling another hook.
protected void Stop()
Remarks
It works from every hook; from VisitEdge(int, int, int, TargetState), it does the same as returning Stop. LeaveNode(int) is not called for the nodes on the current path, but they count as finished afterwards. This includes a node whose EnterNode(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).
VisitEdge(int, int, int, TargetState)
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.
protected virtual DfsEdgeTraverser.EdgeAction VisitEdge(int source, int target, int edgeIndex, DfsEdgeTraverser.TargetState state)
Parameters
sourceintThe source of the edge, which is on the current path.
targetintThe target of the edge.
edgeIndexintThe edge index of the edge
source -> target.stateDfsEdgeTraverser.TargetStateThe state of
target.
Returns
- DfsEdgeTraverser.EdgeAction
What the search does with the edge.
Remarks
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(int) 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 Descend for an unvisited
target and Skip for any other. A value outside the members of
DfsEdgeTraverser.EdgeAction makes the run throw ArgumentOutOfRangeException.