Skip to main content

TopOrderAndLevels

Struct TopOrderAndLevels 

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

A topological order of a DAG (directed acyclic graph) with the level of each node. A root node has level 0, and every other node has one more than the highest level of its in-nodes.

The level of a node is the number of edges on a longest path to it from a root node, so no path connects two nodes of the same level. order lists the nodes level by level, level_of and levels return their levels, and level_count the number of levels.

Uses Kahn’s algorithm: of starts with the root nodes in ascending order, places a node once all its in-nodes are placed and takes the out-nodes of each node in ascending order, so a graph always gets the same order. For the order alone, top_order::of needs less memory.

use graphity_core::{GraphBuilder, TopOrderAndLevels};

let mut b = GraphBuilder::new();
b.add_nodes(4);
b.add_edge_pairs(&[0, 1, 0, 2, 1, 3, 2, 3]);
let levels = TopOrderAndLevels::of(&b.build()).unwrap();
assert_eq!(levels.order(), [0, 1, 2, 3]);
assert_eq!(levels.level_of(3), 2);
assert_eq!(levels.level_count(), 3);

Instances are immutable and safe to share between threads (Send and Sync).

Memory: 8 x node_count bytes.

Implementations§

Source§

impl TopOrderAndLevels

Source

pub fn of(graph: &Graph) -> Result<TopOrderAndLevels, GraphError>

Returns the topological order and the levels of graph.

On the graph of a BiAdjacentGraph, it reads the in-degrees instead of counting them in a pass over the edges.

Time: O(node_count + edge_count). Memory: 8 x node_count bytes for the result; while it runs, 8 x node_count bytes more.

§Errors

An InvalidArgument error, “graph contains a cycle”, if graph has a cycle, a self-loop included.

Source

pub fn order(&self) -> &[u32]

Returns the nodes in topological order, level by level: for every edge u -> v, u comes before v.

Time: O(1).

Source

pub fn levels(&self) -> &[u32]

Returns the level of every node, by node ID.

Time: O(1).

Source

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

Returns the level of node. Equivalent to levels()[node].

Time: O(1).

§Panics

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

Source

pub fn level_count(&self) -> u32

Returns the number of levels, or 0 for a graph without nodes. It equals the number of nodes on a longest path.

Time: O(1).

Trait Implementations§

Source§

impl Clone for TopOrderAndLevels

Source§

fn clone(&self) -> TopOrderAndLevels

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 TopOrderAndLevels

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Eq for TopOrderAndLevels

Source§

impl PartialEq for TopOrderAndLevels

Source§

fn eq(&self, other: &TopOrderAndLevels) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for TopOrderAndLevels

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.