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
impl TopOrderAndLevels
Sourcepub fn of(graph: &Graph) -> Result<TopOrderAndLevels, GraphError>
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.
Sourcepub fn order(&self) -> &[u32]
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).
Sourcepub fn level_of(&self, node: u32) -> u32
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.
Sourcepub fn level_count(&self) -> u32
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
impl Clone for TopOrderAndLevels
Source§fn clone(&self) -> TopOrderAndLevels
fn clone(&self) -> TopOrderAndLevels
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more