diff --git a/README.md b/README.md index 8e9c5be..713a08b 100644 --- a/README.md +++ b/README.md @@ -34,3 +34,5 @@ cargo codspeed run -m walltime ``` Note: You can also set the `CODSPEED_RUNNER_MODE` environment variable to `walltime` to avoid passing `-m walltime` every time. + +* Théodore Prévot diff --git a/src/bfs.rs b/src/bfs.rs index 487fddc..5dd8b3d 100644 --- a/src/bfs.rs +++ b/src/bfs.rs @@ -1,4 +1,4 @@ -use std::collections::HashSet; +use std::collections::{HashSet, VecDeque}; /// A simple graph represented as an adjacency list #[derive(Debug, Clone)] @@ -26,22 +26,22 @@ impl Graph { /// Naive BFS implementation using Vec as a queue (intentionally slow) /// Returns the order in which nodes were visited pub fn bfs_naive(graph: &Graph, start: usize) -> Vec { - let mut visited = HashSet::new(); - let mut queue = Vec::new(); // Using Vec instead of VecDeque - intentionally inefficient! - let mut result = Vec::new(); + let mut visited = Vec::from_iter((0..graph.num_nodes()).map(|_| false)); + let mut queue = VecDeque::with_capacity(1024); + let mut result = Vec::with_capacity(graph.num_nodes()); - queue.push(start); - visited.insert(start); + queue.push_back(start); + visited[start] = true; while !queue.is_empty() { - // remove(0) is O(n) - this makes BFS slow! - let node = queue.remove(0); + let node = queue.pop_front().unwrap(); result.push(node); if let Some(neighbors) = graph.adjacency.get(node) { for &neighbor in neighbors { - if visited.insert(neighbor) { - queue.push(neighbor); + if !visited[neighbor] { + visited[neighbor] = true; + queue.push_back(neighbor); } } }