Table of Contents

Class FrontierTraverser

Namespace
Graphity
Assembly
Graphity.Core.dll

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.

public abstract class FrontierTraverser
Inheritance
FrontierTraverser
Inherited Members

Examples

Here, calls are the calls between the methods of a program, and the search follows them from main for two levels:

private sealed class WithinTwoCalls(Graph calls) : FrontierTraverser(calls)
{
    public List<int> Reached { get; } = [];

    protected override void VisitNode(int method)
    {
        Reached.Add(method);
        if (CurrentLevel < 2)
        {
            EnqueueChildren(method);
        }
    }
}

private static List<int> RunWithinTwoCalls(Graph calls, int main)
{
    var withinTwoCalls = new WithinTwoCalls(calls);
    withinTwoCalls.Run(main);
    // withinTwoCalls.Reached: [0, 1, 2, 3, 4, 5] - main, parseArgs, run, load, save
    // and show
    return withinTwoCalls.Reached;
}

Remarks

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. When VisitNode(int) 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(ReadOnlySpan<int>) from several, such as the root nodes that 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 InvalidOperationException. 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 ReachableFrom(Graph, ReadOnlySpan<int>) 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.

Constructors

FrontierTraverser(Graph)

Creates a traverser for graph, with no node visited.

protected FrontierTraverser(Graph graph)

Parameters

graph Graph

The graph to search.

Remarks

Time: O(NodeCount).

Properties

CurrentLevel

Gets 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 int CurrentLevel { get; }

Property Value

int

Remarks

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

Time: O(1).

Graph

Gets the graph that this traverser searches.

protected Graph Graph { get; }

Property Value

Graph

Visited

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

protected BitArray Visited { get; }

Property Value

BitArray

Remarks

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.

Methods

Enqueue(int)

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

protected bool Enqueue(int node)

Parameters

node int

Node ID in 0..NodeCount-1.

Returns

bool

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

Remarks

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

Exceptions

ArgumentOutOfRangeException

node is out of range.

EnqueueChildren(int)

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

protected void EnqueueChildren(int node)

Parameters

node int

Node ID in 0..NodeCount-1.

Remarks

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

Exceptions

ArgumentOutOfRangeException

node is out of range.

IsVisited(int)

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

protected bool IsVisited(int node)

Parameters

node int

Node ID in 0..NodeCount-1.

Returns

bool

true if node is visited.

Remarks

Time: O(1).

Exceptions

ArgumentOutOfRangeException

node is out of range.

LeaveLevel(int)

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

protected virtual void LeaveLevel(int level)

Parameters

level int

The level just completed.

Remarks

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

Run()

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

public void Run()

Remarks

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.

Exceptions

InvalidOperationException

Called from a hook, during a run of this instance.

Run(int)

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

public void Run(int startNode)

Parameters

startNode int

Node ID in 0..NodeCount-1.

Remarks

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.

Exceptions

ArgumentOutOfRangeException

startNode is out of range.

InvalidOperationException

Called from a hook, during a run of this instance.

Run(ReadOnlySpan<int>)

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

public void Run(ReadOnlySpan<int> startNodes)

Parameters

startNodes ReadOnlySpan<int>

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

Remarks

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

Exceptions

ArgumentOutOfRangeException

A start node is out of range.

InvalidOperationException

Called from a hook, during a run of this instance.

Stop()

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

protected void Stop()

Remarks

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

VisitNode(int)

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

protected abstract void VisitNode(int node)

Parameters

node int

Node ID in 0..NodeCount-1.

Remarks

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.