Skip to main content

FrontierTraverser

Struct FrontierTraverser 

Source
pub struct FrontierTraverser { /* private fields */ }
Expand description

The base 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 - Java’s abstract class FrontierTraverser. A type of yours implements the hooks of FrontierHooks, and a run method starts the search with them.

The queue is called the frontier and works first in first out. The traverser calls visit_node 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 FrontierControl::enqueue_children, or a single node with FrontierControl::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 enqueue_children(node) in every visit_node(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. FrontierControl::current_level returns the level of the node being visited, and leave_level 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:

struct WithinTwoCalls {
    reached: Vec<u32>,
}

impl FrontierHooks for WithinTwoCalls {
    fn visit_node(&mut self, method: u32, control: &mut FrontierControl<'_>) {
        self.reached.push(method);
        if control.current_level() < 2 {
            control.enqueue_children(method);
        }
    }
}

let mut within_two_calls = WithinTwoCalls {
    reached: Vec::new(),
};
FrontierTraverser::new(&calls).run_from(main, &mut within_two_calls);
// within_two_calls.reached: [0, 1, 2, 3, 4, 5] - main, parseArgs, run, load, save and
// show

When visit_node 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 FrontierControl::stop.

run searches from every node in ascending order, run_from from one node and run_from_nodes from several, such as the root nodes that graphs::root_nodes returns. The visited nodes add up over the runs of a traverser: a node is visited at most once in the lifetime of the traverser, and a later run skips it. Nodes enqueued outside a run come first in the next run. A type of yours can also keep nodes out of the search by marking them with mark_visited.

A hook cannot start another run of the same traverser. Java throws IllegalStateException; in Rust, the borrow checker rejects the call at compile time.

ⓘ
use graphity_core::{FrontierControl, FrontierHooks, FrontierTraverser, GraphBuilder};

struct Nested<'t>(&'t mut FrontierTraverser);
impl FrontierHooks for Nested<'_> {
    fn visit_node(&mut self, _: u32, _: &mut FrontierControl<'_>) {}
}
let graph = GraphBuilder::new().build();
let mut traverser = FrontierTraverser::new(&graph);
let mut hooks = Nested(&mut traverser);
traverser.run(&mut hooks); // error: the traverser is already borrowed by the hooks

If a run ends with a panic from a hook or from the search, the state of the traverser is undefined: a later run may visit the rest of the frontier, for example. A new traverser is needed for further runs. BfsDistances and graphs::reachable_from_sources are ready-made breadth-first searches.

A traverser is mutable, and a run borrows it mutably, so the borrow rules keep two threads from running it at once - Java’s instances are 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 node_count bytes; the graph’s arrays are shared.

Implementations§

Source§

impl FrontierTraverser

Source

pub fn new(graph: &Graph) -> Self

Returns a traverser for graph, with no node visited - Java’s constructor. The graph’s arrays are shared.

Time: O(node_count).

Source

pub fn graph(&self) -> &Graph

Returns the graph that this traverser searches - Java’s protected field graph.

Time: O(1).

Source

pub fn is_visited(&self, node: u32) -> bool

Returns whether node is visited: it has entered the frontier, or it was marked with mark_visited - FrontierControl::is_visited outside a run.

node is a node ID in 0..node_count; for any other value the method returns false or panics with an index out of bounds message.

Time: O(1).

Source

pub fn mark_visited(&mut self, node: u32)

Marks node visited without putting it into the frontier, to keep it out of the search, also before the first run - what a Java subclass does with the protected bit set visited.

A marked node counts as visited and no longer enters the frontier, neither from a run method nor from enqueue or FrontierControl::enqueue_children. A node already in the frontier is visited all the same.

Time: O(1).

§Panics

With an index out of bounds message if node is not in 0..node_count.

Source

pub fn enqueue(&mut self, node: u32) -> bool

Puts node into the frontier and marks it visited, unless it is visited already, outside a run - Java’s protected enqueue called outside a run. Returns true if node was not visited and is now in the frontier.

It adds node to the next run: the nodes enqueued now 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).

§Panics

With an index out of bounds message if node is not in 0..node_count.

Source

pub fn enqueue_children(&mut self, node: u32)

Puts the out-nodes of node that are not visited yet into the frontier, in ascending order, and marks them visited, outside a run - Java’s protected enqueueChildren called outside a run.

It adds the nodes to the next run, as enqueue does: the nodes enqueued now come first in its level 0. During a run, a hook calls FrontierControl::enqueue_children.

Time: O(out_degree(node)).

§Panics

With an index out of bounds message if node is not in 0..node_count.

Source

pub fn run<H: FrontierHooks + ?Sized>(&mut self, hooks: &mut H)

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(node_count), plus the hooks: O(node_count + edge_count) for a breadth-first search.

Source

pub fn run_from<H: FrontierHooks + ?Sized>( &mut self, start_node: u32, hooks: &mut H, )

Searches from start_node and from the nodes enqueued before the call, visiting the nodes they reach that are not visited yet - Java’s overload run(int).

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

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

§Panics

With an index out of bounds message if start_node is not in 0..node_count.

Source

pub fn run_from_nodes<H: FrontierHooks + ?Sized>( &mut self, start_nodes: &[u32], hooks: &mut H, )

Searches from start_nodes and from the nodes enqueued before the call, visiting the nodes they reach that are not visited yet - Java’s overload run(IntList).

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

§Panics

With an index out of bounds message if a start node is not in 0..node_count.

Trait Implementations§

Source§

impl Clone for FrontierTraverser

Source§

fn clone(&self) -> FrontierTraverser

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FrontierTraverser

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.