Function rustworkx_core::connectivity::bfs_undirected
source · pub fn bfs_undirected<G>(
graph: G,
start: G::NodeId,
discovered: &mut G::Map
) -> HashSet<G::NodeId>where
G: GraphProp + IntoNeighborsDirected + Visitable,
G::NodeId: Eq + Hash,
Expand description
Given an graph, a node in the graph, and a visit_map, return the set of nodes connected to the given node using breadth first search and treating all edges as undirected.
Arguments:
graph
- The graph object to run the algorithm onnode
- The node index to find the connected nodes fordiscovered()
- The visit map for the graph
Example
use std::iter::FromIterator;
use hashbrown::HashSet;
use petgraph::graph::Graph;
use petgraph::graph::node_index as ndx;
use petgraph::visit::Visitable;
use petgraph::Directed;
use rustworkx_core::connectivity::bfs_undirected;
let graph = Graph::<(), (), Directed>::from_edges(&[
(0, 1),
(1, 2),
(2, 3),
(3, 0),
(4, 5),
(5, 6),
(6, 7),
(7, 4),
]);
let node_idx = ndx(3);
let component = bfs_undirected(&graph, node_idx, &mut graph.visit_map());
let expected = HashSet::from_iter([ndx(0), ndx(1), ndx(3), ndx(2)]);
assert_eq!(expected, component);