pub fn layers<G>(
graph: G,
first_layer: Vec<G::NodeId>,
) -> impl Iterator<Item = Result<Vec<G::NodeId>, LayersError>>where
G: NodeIndexable + IntoNodeIdentifiers + IntoNeighborsDirected + IntoEdgesDirected,
<G as GraphBase>::NodeId: Debug + Copy + Eq + Hash,
Expand description
Return an iterator of graph layers
A layer is a subgraph whose nodes are disjoint, i.e., a layer has depth 1. The layers are constructed using a greedy algorithm.
Arguments:
graph
- The graph to get the layers fromfirst_layer
- A list of node ids for the first layer. This will be the first layer in the output
Will panic!
if a provided node is not in the graph.
use rustworkx_core::petgraph::prelude::*;
use rustworkx_core::dag_algo::layers;
use rustworkx_core::dictmap::*;
let edge_list = vec![
(0, 1),
(1, 2),
(2, 3),
(3, 4),
];
let graph = DiGraph::<u32, u32>::from_edges(&edge_list);
let layers: Vec<Vec<NodeIndex>> = layers(&graph, vec![0.into(),]).map(|layer| layer.unwrap()).collect();
let expected_layers: Vec<Vec<NodeIndex>> = vec![
vec![0.into(),],
vec![1.into(),],
vec![2.into(),],
vec![3.into(),],
vec![4.into()]
];
assert_eq!(layers, expected_layers)