Skip to main content

DfsEdgeTraverser

Struct DfsEdgeTraverser 

Source
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 -> model

graphs::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 hooks

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: 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

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

Source

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

Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> DfsEdgeTraverser

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 DfsEdgeTraverser

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.