Class FrontierTraverser
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
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 -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedFrontierTraverser(Graph graph) Creates a traverser forgraph, with no node visited. -
Method Summary
Modifier and TypeMethodDescriptionprotected final intReturns the level of the node being visited: 0 for the start nodes,k + 1for the nodes that entered the frontier while levelkwas visited.protected final booleanenqueue(int node) Putsnodeinto the frontier and marks it visited, unless it is visited already.protected final voidenqueueChildren(int node) Puts the out-nodes ofnodethat are not visited yet into the frontier, in ascending order, and marks them visited.protected final booleanisVisited(int node) Returns whethernodeis visited: it has entered the frontier, or a subclass has marked it invisited.protected voidleaveLevel(int level) Called after the last node of a level has been visited, before the first node of the next level.final voidrun()Searches from every node not visited yet, in ascending order, until every node is visited.final voidrun(int startNode) Searches fromstartNodeand from the nodes enqueued before the call, visiting the nodes they reach that are not visited yet.final voidSearches fromstartNodesand from the nodes enqueued before the call, visiting the nodes they reach that are not visited yet.protected final voidstop()Stops the run in progress: the run returns as soon as the hook that calls this method returns, without calling another hook.protected abstract voidvisitNode(int node) Called for each node of the frontier, in the order in which the nodes entered it.
-
Field Details
-
graph
-
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
runmethod nor fromenqueue(int)orenqueueChildren(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
Creates a traverser forgraph, with no node visited.Time: O(nodeCount).
- Parameters:
graph- the graph to search
-
-
Method Details
-
visitNode
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 atnode. They join the next level and are visited after the rest of the current one.- Parameters:
node- node ID in0..nodeCount-1- In the guide:
-
enqueueChildren
Puts the out-nodes ofnodethat 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, asenqueue(int)does.Time: O(outDegree(node)).
- Parameters:
node- node ID in0..nodeCount-1- Throws:
IndexOutOfBoundsException- ifnodeis out of range- In the guide:
-
enqueue
Putsnodeinto the frontier and marks it visited, unless it is visited already.Called from a hook, it adds
nodeto the next level. Called outside a run, it addsnodeto the next run: the nodes enqueued then come first in its level 0, ahead of its start nodes, andrun()visits them before it starts from a node of its own.Time: O(1).
- Parameters:
node- node ID in0..nodeCount-1- Returns:
trueifnodewas not visited and is now in the frontier- Throws:
IndexOutOfBoundsException- ifnodeis out of range- In the guide:
-
isVisited
Returns whethernodeis visited: it has entered the frontier, or a subclass has marked it invisited.Time: O(1).
- Parameters:
node- node ID in0..nodeCount-1; for any other value the method returnsfalseor throwsIndexOutOfBoundsException- Returns:
trueifnodeis visited- In the guide:
-
currentLevel
Returns the level of the node being visited: 0 for the start nodes,k + 1for the nodes that entered the frontier while levelkwas 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
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
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
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
Searches fromstartNodeand 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
startNodeunless 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 in0..nodeCount-1- Throws:
IndexOutOfBoundsException- ifstartNodeis out of rangeIllegalStateException- if called from a hook, during a run of this instance- In the guide:
-
run
Searches fromstartNodesand 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 in0..nodeCount-1- Throws:
IndexOutOfBoundsException- if a start node is out of rangeIllegalStateException- if called from a hook, during a run of this instance- In the guide:
-