Graphity for TypeScript 3.0.0
    Preparing search index...

    Class FrontierTraverserAbstract

    The base class of a breadth-first search of your own over a Graph, or of any search that works off a queue of nodes and chooses which nodes enter it.

    The queue is called the frontier and works first in first out. The traverser calls FrontierTraverser.visitNode for each node in the order in which it entered the frontier. The run methods put the start nodes into the frontier. The hook puts in the nodes that come next: the out-nodes of a node with FrontierTraverser.enqueueChildren, or a single node with FrontierTraverser.enqueue. A node counts as visited from the moment it enters the frontier, so it enters the frontier at most once. From a node for which the hook enqueues nothing, the search goes no further. With enqueueChildren(node) in every visitNode(node), the traversal is a breadth-first search.

    The nodes come in levels: the start nodes form level 0, and the nodes that enter the frontier while level k is visited form level k + 1. In a breadth-first search, those are the nodes at distance k + 1 from the nearest start node. FrontierTraverser.currentLevel is the level of the node being visited, and FrontierTraverser.leaveLevel is called after the last node of each level. Here, calls are the calls between the methods of a program, and the search follows them from main for two levels:

    class WithinTwoCalls extends FrontierTraverser {
    readonly reached: number[] = [];

    protected visitNode(method: number): void {
    this.reached.push(method);
    if (this.currentLevel < 2) {
    this.enqueueChildren(method);
    }
    }
    }
    const withinTwoCalls = new WithinTwoCalls(calls);
    withinTwoCalls.run(main);
    // withinTwoCalls.reached: [0, 1, 2, 3, 4, 5] - main, parseArgs, run, load, save and
    // show

    When visitNode returns, the nodes it enqueued are still waiting in the frontier: unlike DfsTraverser, this traverser has no postorder. A hook can end the whole run with FrontierTraverser.stop.

    run() searches from every node in ascending order, run(startNode) from one node and run(startNodes) from several, such as the root nodes that Graphs.rootNodes returns. The visited nodes add up over the runs of an instance: a node is visited at most once in the lifetime of the instance, and a later run skips it. Nodes enqueued outside a run come first in the next run. A subclass can also keep nodes out of the search by marking them in FrontierTraverser.visited.

    A hook must not start another run of the same instance, which throws an Error (Java: IllegalStateException). If a run ends with an error from a hook or from the search, the state of the instance is undefined: a later run may visit the rest of the frontier, for example. A new instance is needed for further runs. BfsDistances and Graphs.reachableFrom are ready-made breadth-first searches.

    Instances are mutable.

    Memory: 1 bit per node for the visited nodes, and 4 to 6 bytes for every node that has entered the frontier, which keeps them all: up to 6 x nodeCount bytes.

    Index
    graph: Graph

    The graph that this traverser searches.

    visited: BitSet

    The visited nodes, one bit per node ID: a bit is set when its node enters the frontier.

    A subclass may set bits to keep nodes out of the search, also before the first run: a node whose bit is set counts as visited and no longer enters the frontier, neither from a run method nor from FrontierTraverser.enqueue or FrontierTraverser.enqueueChildren. A node already in the frontier is visited all the same. A subclass must not clear a bit; what happens then is not specified.

    • get currentLevel(): number

      The level of the node being visited: 0 for the start nodes, k + 1 for the nodes that entered the frontier while level k was visited (Java: currentLevel()).

      In FrontierTraverser.leaveLevel, it is the level just completed. Outside a run, the value has no meaning.

      Time: O(1).

      Returns number

    • Puts node into the frontier and marks it visited, unless it is visited already.

      Called from a hook, it adds node to the next level. Called outside a run, it adds node to the next run: the nodes enqueued then come first in its level 0, ahead of its start nodes, and run() visits them before it starts from a node of its own.

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns boolean

      true if node was not visited and is now in the frontier

      RangeError if node is out of range

    • Puts the out-nodes of node that are not visited yet into the frontier, in ascending order, and marks them visited.

      Called from visitNode(node), it makes the traversal a breadth-first search. Called outside a run, it adds the nodes to the next run, as FrontierTraverser.enqueue does.

      Time: O(outDegree(node)).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns void

      RangeError if node is out of range

    • Returns whether node is visited: it has entered the frontier, or a subclass has marked it in FrontierTraverser.visited.

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1; for a larger value the method returns false, and for a negative one or one that is not an integer it throws a RangeError

      Returns boolean

      true if node is visited

    • Called after the last node of a level has been visited, before the first node of the next level. Does nothing unless overridden.

      Nodes enqueued here join the next level, which then follows even if the completed level enqueued nothing.

      Parameters

      • _level: number

        the level just completed

      Returns void

    • Searches from every node not visited yet, in ascending order, until every node is visited.

      Nodes enqueued before the call come first, as a level 0 of their own. Then each node that no earlier search has reached when its turn comes starts a search of its own, with levels counted from 0 again.

      Time: O(nodeCount), plus the hooks: O(nodeCount + edgeCount) for a breadth-first search.

      Returns void

      Error if called from a hook, during a run of this instance (Java: IllegalStateException)

    • Searches from startNode and from the nodes enqueued before the call, visiting the nodes they reach that are not visited yet.

      Level 0 holds the nodes enqueued before the call, then startNode unless it is visited already.

      Time: O(nodes visited), plus the hooks: O(reachable nodes + reachable edges) for a breadth-first search.

      Parameters

      • startNode: number

        node ID in 0..nodeCount-1

      Returns void

      RangeError if startNode is out of range

      Error if called from a hook, during a run of this instance (Java: IllegalStateException)

    • Searches from startNodes and from the nodes enqueued before the call, visiting the nodes they reach that are not visited yet.

      Level 0 holds the nodes enqueued before the call, then the start nodes in list order, skipping those visited already. The list is checked first, so a start node out of range leaves the traverser unchanged.

      Time: O(number of start nodes + nodes visited), plus the hooks: O(number of start nodes + reachable nodes + reachable edges) for a breadth-first search.

      Parameters

      • startNodes: IntList

        node IDs, each in 0..nodeCount-1

      Returns void

      RangeError if a start node is out of range

      Error if called from a hook, during a run of this instance (Java: IllegalStateException)

    • Stops the run in progress: the run returns as soon as the hook that calls this method returns, without calling another hook.

      The nodes still waiting in the frontier are dropped from it, but stay marked visited, so no later run visits them. A later run skips every node visited so far. Called outside a run, this method has no effect.

      Time: O(1).

      Returns void

    • Called for each node of the frontier, in the order in which the nodes entered it.

      The hook puts the nodes that come next into the frontier: enqueueChildren(node) for a breadth-first search, FrontierTraverser.enqueue for single nodes, or nothing to end the search at node. They join the next level and are visited after the rest of the current one.

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns void