Graphity for TypeScript 3.0.0
    Preparing search index...

    Class DfsEdgeTraverserAbstract

    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, 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 {
    closingEdge = -1;

    protected override visitEdge(
    source: number,
    target: number,
    edgeIndex: number,
    state: TargetState,
    ): EdgeAction {
    if (state === TargetState.ON_STACK) {
    this.closingEdge = edgeIndex;
    return EdgeAction.STOP;
    }
    return super.visitEdge(source, target, edgeIndex, state);
    }
    }
    const firstCycle = new FirstCycle(dependencies);
    firstCycle.run();
    // firstCycle.closingEdge: 10 - the edge storage -> model

    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.

    Index
    • Creates 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).

      Parameters

      • graph: Graph

        the graph to search

      Returns DfsEdgeTraverser

    graph: Graph

    The graph that this traverser searches.

    • 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 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.

      Parameters

      • _node: number

        node ID in 0..nodeCount-1

      Returns boolean

      true to report the out-edges of node, false to skip them

    • 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 DfsEdgeTraverser.enterNode until its DfsEdgeTraverser.leaveNode. Outside a run, no node is on the path.

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1; for any other value the method returns false, also for one that is not an integer

      Returns boolean

      true if node is on the current path

    • Returns whether the search has reached node, in this run or in an earlier one.

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1; for any other value the method returns false, also for one that is not an integer

      Returns boolean

      true if DfsEdgeTraverser.enterNode has been called for 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 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.

      Parameters

      • _node: number

        node ID in 0..nodeCount-1

      Returns void

    • 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.

      Returns void

      Error if a depth-first path is longer than 2^30 - 5 nodes, or if called from a hook, during a run of this instance (Java: IllegalStateException)

    • 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: number

        node ID in 0..nodeCount-1

      Returns void

      RangeError if startNode is out of range

      Error if a depth-first path is longer than 2^30 - 5 nodes, or if called from a hook, during a run of this instance (Java: IllegalStateException)

    • 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.

      Parameters

      • startNodes: IntList

        node IDs, each in 0..nodeCount-1; not to be changed until the call returns

      Returns void

      RangeError if a start node is out of range

      Error if a depth-first path is longer than 2^30 - 5 nodes, or if called from a hook, during a run of this instance (Java: IllegalStateException)

    • 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 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).

      Parameters

      • _source: number

        the source of the edge, which is on the current path

      • _target: number

        the target of the edge

      • _edgeIndex: number

        edge index of the edge source -> target

      • state: TargetState

        the state of target

      Returns EdgeAction

      what the search does with the edge