Skip to main content

Crate bfs

Crate bfs 

Source
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 source to every vertex reachable from it.
bfs_order
Return the visitation order of BFS starting from source.