Function rs_graph::algorithms::subgraph

source ·
pub fn subgraph<G, H, P>(g: G, predicate: P) -> Hwhere
    G: IndexDigraph,
    H: Digraph + Buildable,
    P: Fn(Item<'_, G>) -> bool,
Expand description

Return a subgraph.

The resulting graph contains all nodes and edges for which the predicate returns true.

Example

// Extract a bipartite subgraph.
use rs_graph::LinkedListGraph;
use rs_graph::traits::*;
use rs_graph::classes;
use rs_graph::algorithms::*;

let g: LinkedListGraph = classes::complete_graph(7);
let h: LinkedListGraph = subgraph(&g, |i| match i {
    Item::Node(u) => g.node_id(u) < 6,
    Item::Edge(e) => {
        let (u,v) = g.enodes(e);
        g.node_id(u) % 2 != g.node_id(v) % 2
    }
});

assert_eq!(h.num_nodes(), 6);
assert_eq!(h.num_edges(), 3*3);
for u in h.nodes() {
    let mut neighs = h.neighs(u).map(|(_,v)| h.node_id(v)).collect::<Vec<_>>();
    neighs.sort();
    assert_eq!(neighs, if h.node_id(u) % 2 == 0 { vec![1,3,5] } else { vec![0,2,4] });
}