Skip to main content

bfs_shortest_path

Function bfs_shortest_path 

Source
pub fn bfs_shortest_path(
    g: &Graph,
    start: usize,
    goal: usize,
) -> Option<Vec<usize>>
Expand description

Og’irliksiz grafda eng qisqa yo’l — oddiy BFS.

Og’irliklar teng bo’lsa, Dijkstra ortiqcha: BFS O(V+E) da javob beradi.

Qaytadi: start dan goal gacha bo’lgan yo’l (tugunlar ro’yxati) yoki None.

§Misol

use rust_algorithms::graph::{Graph, bfs_shortest_path};

let mut g = Graph::new_undirected(6);
for (a, b) in [(0, 1), (1, 2), (2, 5), (0, 3), (3, 4), (4, 5)] {
    g.add_unweighted_edge(a, b);
}
let yol = bfs_shortest_path(&g, 0, 5).unwrap();
assert_eq!(yol.len(), 4); // 0 → 1 → 2 → 5 (yoki 0 → 3 → 4 → 5)
assert_eq!(yol[0], 0);
assert_eq!(*yol.last().unwrap(), 5);