graphlib/iterators/
bfs.rs

1// Copyright 2019 Octavian Oncescu
2
3use crate::graph::Graph;
4use crate::vertex_id::VertexId;
5
6use hashbrown::HashSet;
7#[cfg(not(feature = "no_std"))]
8use std::collections::VecDeque;
9
10#[cfg(feature = "no_std")]
11extern crate alloc;
12#[cfg(feature = "no_std")]
13use alloc::collections::vec_deque::VecDeque;
14
15#[cfg(feature = "no_std")]
16use alloc::vec::Vec;
17
18#[cfg(feature = "no_std")]
19use core::fmt::Debug;
20
21#[cfg(not(feature = "no_std"))]
22use std::fmt::Debug;
23
24#[derive(Debug)]
25/// Breadth-First Iterator
26pub struct Bfs<'a, T> {
27    queue: VecDeque<VertexId>,
28    current_ptr: Option<VertexId>,
29    visited_set: HashSet<VertexId>,
30    roots_stack: Vec<VertexId>,
31    iterable: &'a Graph<T>,
32}
33
34impl<'a, T> Bfs<'a, T> {
35    pub fn new(graph: &'a Graph<T>) -> Bfs<'_, T> {
36        let mut roots_stack = Vec::with_capacity(graph.roots_count());
37
38        for v in graph.roots() {
39            roots_stack.push(v.clone());
40        }
41
42        let current_ptr = roots_stack.pop();
43
44        Bfs {
45            queue: VecDeque::with_capacity(graph.vertex_count()),
46            current_ptr,
47            visited_set: HashSet::with_capacity(graph.vertex_count()),
48            roots_stack,
49            iterable: graph,
50        }
51    }
52}
53
54impl<'a, T> Iterator for Bfs<'a, T> {
55    type Item = &'a VertexId;
56
57    fn next(&mut self) -> Option<Self::Item> {
58        loop {
59            if let Some(current_ptr) = &self.current_ptr {
60                // Yield current pointed value if
61                // it isn't in the visited stack.
62                if !self.visited_set.contains(current_ptr) {
63                    self.visited_set.insert(current_ptr.clone());
64                    return self.iterable.fetch_id_ref(current_ptr.as_ref());
65                }
66
67                // Iterate through current neighbors
68                // and check their visited status.
69                for n in self.iterable.out_neighbors(current_ptr.as_ref()) {
70                    if !self.visited_set.contains(n) {
71                        self.visited_set.insert(n.clone());
72                        self.queue.push_back(n.clone());
73
74                        return self.iterable.fetch_id_ref(n);
75                    }
76                }
77
78                // Move to next root if possible and yield it.
79                if self.queue.is_empty() {
80                    if let Some(next_root) = self.roots_stack.pop() {
81                        self.current_ptr = Some(next_root);
82                    } else {
83                        // Break execution if there are no more roots
84                        return None;
85                    }
86                } else {
87                    // Pop item from queue and set it
88                    // as the current pointer.
89                    self.current_ptr = self.queue.pop_front();
90                }
91            } else {
92                return None;
93            }
94        }
95    }
96}