pub struct DfsEdgeTraverser { /* private fields */ }Expand description
The base of a depth-first search of your own over a Graph, with a hook for every edge - Java’s
abstract class DfsEdgeTraverser. The hook also sees the edges to nodes already visited.
Like DfsTraverser, it calls enter_node when
the search reaches a node and leave_node when it leaves the node. In
between, it reports each out-edge of the node to visit_edge, in
edge index order, with the state of the target: unvisited,
on the current path or finished. The hook returns
what the search does with the edge: descend into an unvisited target and
search from it before the next edge, skip the edge, or
stop the run; any hook can stop the run with DfsControl::stop.
By default, the search descends into every unvisited target, and the hooks come in the order of a
textbook recursive depth-first search, as in
DfsTraverser. The search itself runs without recursion and keeps its stack
on the heap. An edge to a target on the current path closes a cycle. Here, dependencies are the
dependencies between the packages of a program, and the search stops at the first dependency that
closes a cycle:
struct FirstCycle {
closing_edge: Option<u32>,
}
impl DfsEdgeHooks for FirstCycle {
fn visit_edge(
&mut self,
_source: u32,
_target: u32,
edge_index: u32,
state: TargetState,
_: &mut DfsControl<'_>,
) -> EdgeAction {
if state == TargetState::OnStack {
self.closing_edge = Some(edge_index);
return EdgeAction::Stop;
}
EdgeAction::default_for(state)
}
}
let mut first_cycle = FirstCycle { closing_edge: None };
DfsEdgeTraverser::new(&dependencies).run(&mut first_cycle);
// first_cycle.closing_edge: Some(10) - the edge storage -> modelgraphs::find_cycle returns such a cycle as a list of nodes. The
state of a target does not tell a forward edge from a cross edge; EdgeTypes
classifies every edge.
The runs work as in DfsTraverser. The visited nodes add up over the runs
of a traverser. A hook cannot start another run of the same traverser. Java throws
IllegalStateException; in Rust, the borrow checker rejects the call at compile time. After a run
that ends with a panic, the state of the traverser is undefined. A search that needs no hook per
edge is faster on DfsTraverser.
use graphity_core::{DfsControl, DfsEdgeHooks, DfsEdgeTraverser, GraphBuilder};
struct Nested<'t>(&'t mut DfsEdgeTraverser);
impl DfsEdgeHooks for Nested<'_> {
fn enter_node(&mut self, _: u32, _: &mut DfsControl<'_>) -> bool {
true
}
}
let graph = GraphBuilder::new().build();
let mut traverser = DfsEdgeTraverser::new(&graph);
let mut hooks = Nested(&mut traverser);
traverser.run(&mut hooks); // error: the traverser is already borrowed by the hooksA 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: 2 bits per node for the visited state, and up to 8 x node_count bytes for the stack, which grows with the deepest path searched; the graph’s arrays are shared.
Implementations§
Source§impl DfsEdgeTraverser
impl DfsEdgeTraverser
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 a run has reached node so far - DfsControl::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 is_on_stack(&self, node: u32) -> bool
pub fn is_on_stack(&self, node: u32) -> bool
Returns whether node is on the current path - DfsControl::is_on_stack outside a run. Outside
a run, no node is on the path.
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 run<H: DfsEdgeHooks + ?Sized>(&mut self, hooks: &mut H)
pub fn run<H: DfsEdgeHooks + ?Sized>(&mut self, hooks: &mut H)
Searches from every node not visited yet, in ascending order, until every node is visited.
A node that no earlier search has reached when its turn comes starts a search of its own.
Time: O(node_count + edge_count), plus the hooks.
§Panics
With an invalid argument message if a depth-first path is longer than 2^30 - 5 nodes.
Sourcepub fn run_from<H: DfsEdgeHooks + ?Sized>(
&mut self,
start_node: u32,
hooks: &mut H,
)
pub fn run_from<H: DfsEdgeHooks + ?Sized>( &mut self, start_node: u32, hooks: &mut H, )
Searches from start_node, visiting the nodes it reaches that are not visited yet - Java’s
overload run(int).
If start_node is visited already, nothing happens.
Time: O(reachable nodes + reachable edges), plus the hooks.
§Panics
With an index out of bounds message if start_node is not in 0..node_count; with an
invalid argument message if a depth-first path is longer than 2^30 - 5 nodes.
Sourcepub fn run_from_nodes<H: DfsEdgeHooks + ?Sized>(
&mut self,
start_nodes: &[u32],
hooks: &mut H,
)
pub fn run_from_nodes<H: DfsEdgeHooks + ?Sized>( &mut self, start_nodes: &[u32], hooks: &mut H, )
Searches from each of start_nodes in list order, skipping those visited already - Java’s
overload run(IntList).
The list is checked before the first hook, so a start node out of range leaves the traverser
unchanged. EdgeAction::Stop and DfsControl::stop end the whole call, not only the search
from one start node. The run borrows the list, so it cannot change until the call returns.
Time: O(start_nodes.len() + reachable nodes + reachable edges), plus the hooks.
§Panics
With an index out of bounds message if a start node is not in 0..node_count; with an
invalid argument message if a depth-first path is longer than 2^30 - 5 nodes.
Trait Implementations§
Source§impl Clone for DfsEdgeTraverser
impl Clone for DfsEdgeTraverser
Source§fn clone(&self) -> DfsEdgeTraverser
fn clone(&self) -> DfsEdgeTraverser
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more