AbstractCreates 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).
the graph to search
Protected ReadonlygraphThe graph that this traverser searches.
Protected ReadonlyvisitedThe 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.
ProtectedcurrentThe 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).
ProtectedenqueuePuts 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).
node ID in 0..nodeCount-1
true if node was not visited and is now in the frontier
ProtectedenqueuePuts 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)).
node ID in 0..nodeCount-1
ProtectedisReturns whether node is visited: it has entered the frontier, or a subclass has marked it in
FrontierTraverser.visited.
Time: O(1).
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
true if node is visited
ProtectedleaveCalled 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.
the level just completed
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.
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.
node ID in 0..nodeCount-1
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.
node IDs, each in 0..nodeCount-1
ProtectedstopStops 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).
Protected AbstractvisitCalled 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.
node ID in 0..nodeCount-1
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
runmethods 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. WithenqueueChildren(node)in everyvisitNode(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
kis visited form levelk + 1. In a breadth-first search, those are the nodes at distancek + 1from 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,callsare the calls between the methods of a program, and the search follows them frommainfor two levels:When
visitNodereturns, 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 andrun(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.