Expand description
Breadth-First Search (BFS) on a graph.
BFS visits neighbours of the current node before going deeper. It is typically used to compute shortest paths on unweighted graphs, to find connected components, or to perform level-order traversal.
This module exposes BFS as two helpers operating on an adjacency list:
bfs_distances— returns the shortest distance (in edges) from a source vertex to every reachable vertex. Unreachable vertices are absent from the result.bfs_order— returns the vertices in the order they are dequeued (i.e., the BFS visitation order).
The graph is passed as &[Vec<usize>], indexed by vertex. Vertex ids
are 0..n where n = graph.len().
Functions§
- bfs_
distances - Compute the shortest distance (in number of edges) from
sourceto every vertex reachable from it. - bfs_
order - Return the visitation order of BFS starting from
source.