Function connected_components

Source
pub fn connected_components<G>(graph: G) -> Vec<HashSet<G::NodeId>>
Expand description

Given a graph, return a list of sets of all the connected components.

Arguments:

  • graph - The graph object to run the algorithm on

ยงExample

use std::iter::FromIterator;
use hashbrown::HashSet;
use petgraph::graph::Graph;
use petgraph::graph::NodeIndex;
use petgraph::{Undirected, Directed};
use petgraph::graph::node_index as ndx;
use rustworkx_core::connectivity::connected_components;

let graph = Graph::<(), (), Undirected>::from_edges(&[
    (0, 1),
    (1, 2),
    (2, 3),
    (3, 0),
    (4, 5),
    (5, 6),
    (6, 7),
    (7, 4),
]);
let components = connected_components(&graph);
let exp1 = HashSet::from_iter([ndx(0), ndx(1), ndx(3), ndx(2)]);
let exp2 = HashSet::from_iter([ndx(7), ndx(5), ndx(4), ndx(6)]);
let expected = vec![exp1, exp2];
assert_eq!(expected, components);