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
// showWhen 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 hooksIf 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
impl FrontierTraverser
Sourcepub fn new(graph: &Graph) -> Self
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).
Sourcepub fn graph(&self) -> &Graph
pub fn graph(&self) -> &Graph
Returns the graph that this traverser searches - Java’s protected field graph.
Time: O(1).
Sourcepub fn is_visited(&self, node: u32) -> bool
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).
Sourcepub fn mark_visited(&mut self, node: u32)
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.
Sourcepub fn enqueue(&mut self, node: u32) -> bool
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.
Sourcepub fn enqueue_children(&mut self, node: u32)
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.
Sourcepub fn run<H: FrontierHooks + ?Sized>(&mut self, hooks: &mut H)
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.
Sourcepub fn run_from<H: FrontierHooks + ?Sized>(
&mut self,
start_node: u32,
hooks: &mut H,
)
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.
Sourcepub fn run_from_nodes<H: FrontierHooks + ?Sized>(
&mut self,
start_nodes: &[u32],
hooks: &mut H,
)
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
impl Clone for FrontierTraverser
Source§fn clone(&self) -> FrontierTraverser
fn clone(&self) -> FrontierTraverser
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more