Class FrontierTraverser

java.lang.Object
com.kobayami.graphity.FrontierTraverser

public abstract class FrontierTraverser extends Object
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 visitNode(int) 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 enqueueChildren(int), or a single node with enqueue(int). 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. currentLevel() returns the level of the node being visited, and leaveLevel(int) 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 {
  final IntArrayList reached = new IntArrayList();

  WithinTwoCalls(Graph calls) { super(calls); }

  @Override
  protected void visitNode(int method) {
    reached.add(method);
    if (currentLevel() < 2) {
      enqueueChildren(method);
    }
  }
}
var 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 stop().

run() searches from every node in ascending order, run(int) from one node and run(IntList) from several, such as the root nodes that Graphs.rootNodes(BiAdjacentGraph) 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 visited.

A hook must not start another run of the same instance, which throws IllegalStateException. If a run ends with an exception 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(Graph, IntList) are ready-made breadth-first searches.

Instances are mutable and not safe to share between threads.

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.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Graph
    The graph that this traverser searches.
    protected final BitSet
    The visited nodes, one bit per node ID: a bit is set when its node enters the frontier.
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    protected final int
    Returns 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.
    protected final boolean
    enqueue(int node)
    Puts node into the frontier and marks it visited, unless it is visited already.
    protected final void
    enqueueChildren(int node)
    Puts the out-nodes of node that are not visited yet into the frontier, in ascending order, and marks them visited.
    protected final boolean
    isVisited(int node)
    Returns whether node is visited: it has entered the frontier, or a subclass has marked it in visited.
    protected void
    leaveLevel(int level)
    Called after the last node of a level has been visited, before the first node of the next level.
    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 and from the nodes enqueued before the call, visiting the nodes they reach that are not visited yet.
    final void
    run(IntList startNodes)
    Searches from startNodes and from the nodes enqueued before the call, visiting the nodes they reach that are not visited yet.
    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.
    protected abstract void
    visitNode(int node)
    Called for each node of the frontier, in the order in which the nodes entered it.

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

      protected final BitSet visited
      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 enqueue(int) or enqueueChildren(int). A node already in the frontier is visited all the same. A subclass must not clear a bit; what happens then is not specified.

  • Constructor Details

    • FrontierTraverser

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

      Time: O(nodeCount).

      Parameters:
      graph - the graph to search
  • Method Details

    • visitNode

      protected abstract void visitNode(int node)
      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, enqueue(int) 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 - node ID in 0..nodeCount-1
      In the guide:
    • enqueueChildren

      protected final void enqueueChildren(int node)
      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 enqueue(int) does.

      Time: O(outDegree(node)).

      Parameters:
      node - node ID in 0..nodeCount-1
      Throws:
      IndexOutOfBoundsException - if node is out of range
      In the guide:
    • enqueue

      protected final boolean enqueue(int node)
      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 - node ID in 0..nodeCount-1
      Returns:
      true if node was not visited and is now in the frontier
      Throws:
      IndexOutOfBoundsException - if node is out of range
      In the guide:
    • isVisited

      protected final boolean isVisited(int node)
      Returns whether node is visited: it has entered the frontier, or a subclass has marked it in visited.

      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 visited
      In the guide:
    • currentLevel

      protected final int currentLevel()
      Returns 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.

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

      Time: O(1).

      Returns:
      the current level
      In the guide:
    • leaveLevel

      protected void leaveLevel(int level)
      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 - the level just completed
      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.

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

      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.

      Throws:
      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 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 - node ID in 0..nodeCount-1
      Throws:
      IndexOutOfBoundsException - if startNode is out of range
      IllegalStateException - if called from a hook, during a run of this instance
      In the guide:
    • run

      public final void run(IntList startNodes)
      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(startNodes.size() + nodes visited), plus the hooks: O(startNodes.size() + reachable nodes + reachable edges) for a breadth-first search.

      Parameters:
      startNodes - node IDs, each in 0..nodeCount-1
      Throws:
      IndexOutOfBoundsException - if a start node is out of range
      IllegalStateException - if called from a hook, during a run of this instance
      In the guide: