Class DfsEdgeTraverser

java.lang.Object
com.kobayami.graphity.DfsEdgeTraverser

public abstract class DfsEdgeTraverser extends Object
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 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
    Modifier and Type
    Class
    Description
    static enum 
    What the search does with an edge that visitEdge reports.
    static enum 
    The state of the target of an edge when visitEdge reports the edge.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Graph
    The graph that this traverser searches.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates a traverser for graph, with no node visited.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected boolean
    enterNode(int node)
    Called when the search reaches node, before its out-edges: the preorder hook.
    protected final boolean
    isOnStack(int node)
    Returns whether node is on the stack of the search, that is, on the current path.
    protected final boolean
    isVisited(int node)
    Returns whether the search has reached node, in this run or in an earlier one.
    protected void
    leaveNode(int node)
    Called when the search leaves node, after all its out-edges have been reported and searched: the postorder hook.
    final void
    run()
    Searches from every node not visited yet, in ascending order, until every node is visited.
    final void
    run(int startNode)
    Searches from startNode, visiting the nodes it reaches that are not visited yet.
    final void
    run(IntList startNodes)
    Searches from each of startNodes in list order, skipping those visited already.
    protected final void
    Stops the run in progress: the run returns as soon as the hook that calls this method returns, without calling another hook.
    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.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • graph

      protected final Graph graph
      The graph that this traverser searches.
  • Constructor Details

    • DfsEdgeTraverser

      protected DfsEdgeTraverser(Graph graph)
      Creates a traverser for graph, with no node visited.

      Time: O(nodeCount).

      Parameters:
      graph - the graph to search
  • Method Details

    • enterNode

      protected boolean enterNode(int node)
      Called 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 visitEdge 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.

      Parameters:
      node - node ID in 0..nodeCount-1
      Returns:
      true to report the out-edges of node, false to skip them
      In the guide:
    • leaveNode

      protected void leaveNode(int node)
      Called 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 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.

      Parameters:
      node - node ID in 0..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 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 DESCEND for an unvisited target and SKIP for any other.

      Parameters:
      source - the source of the edge, which is on the current path
      target - the target of the edge
      edgeIndex - edge index of the edge source -> target
      state - the state of target
      Returns:
      what the search does with the edge
      In the guide:
    • isVisited

      protected final boolean isVisited(int node)
      Returns whether the search has reached node, in this run or in an earlier one.

      Time: O(1).

      Parameters:
      node - node ID in 0..nodeCount-1; for any other value the method returns false or throws IndexOutOfBoundsException
      Returns:
      true if enterNode(int) has been called for node
      In the guide:
    • isOnStack

      protected final boolean isOnStack(int node)
      Returns 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 enterNode(int) until its leaveNode(int). Outside a run, no node is on the path.

      Time: O(1).

      Parameters:
      node - node ID in 0..nodeCount-1; for any other value the method returns false or throws IndexOutOfBoundsException
      Returns:
      true if node is on the current path
      In the guide:
    • stop

      protected final void 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 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).

      In the guide:
    • run

      public final void 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 nodes
      IllegalStateException - if called from a hook, during a run of this instance
      In the guide:
    • run

      public final void run(int startNode)
      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.

      Parameters:
      startNode - node ID in 0..nodeCount-1
      Throws:
      IndexOutOfBoundsException - if startNode is out of range
      IllegalArgumentException - if a depth-first path is longer than 230 - 5 nodes
      IllegalStateException - if called from a hook, during a run of this instance
      In the guide:
    • run

      public final void run(IntList startNodes)
      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. STOP and stop() 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 in 0..nodeCount-1; not to be changed until the call returns
      Throws:
      IndexOutOfBoundsException - if a start node is out of range
      IllegalArgumentException - if a depth-first path is longer than 230 - 5 nodes
      IllegalStateException - if called from a hook, during a run of this instance
      In the guide: