From b87b209c1458721c53e33e83663ef52eace2ee6e Mon Sep 17 00:00:00 2001 From: arcabuntu Date: Wed, 8 Oct 2025 10:22:09 +0200 Subject: [PATCH 1/3] added my name as instructed --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 8e9c5be..c7d262f 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. + +Name: Mehdi From 84ca4997c17bbff04928032fdce57cbbe47a0864 Mon Sep 17 00:00:00 2001 From: arcabuntu Date: Wed, 8 Oct 2025 10:54:21 +0200 Subject: [PATCH 2/3] replaced vec by vecdeque for the queue --- src/bfs.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/bfs.rs b/src/bfs.rs index 487fddc..3726cfe 100644 --- a/src/bfs.rs +++ b/src/bfs.rs @@ -1,4 +1,7 @@ -use std::collections::HashSet; +use std::collections::{ + HashSet, + VecDeque +}; /// A simple graph represented as an adjacency list #[derive(Debug, Clone)] @@ -17,31 +20,34 @@ impl Graph { pub fn add_edge(&mut self, from: usize, to: usize) { self.adjacency[from].push(to); } - pub fn num_nodes(&self) -> usize { self.adjacency.len() } } -/// Naive BFS implementation using Vec as a queue (intentionally slow) +/// Naive BFS implementation using VecDeque /// 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 queue = VecDeque::new(); let mut result = Vec::new(); - queue.push(start); + queue.push_back(start); visited.insert(start); while !queue.is_empty() { - // remove(0) is O(n) - this makes BFS slow! - let node = queue.remove(0); + let node = queue + .pop_front() + .expect( + "This used to be a remove(0), so I assume underlying value exists" + ) + ; result.push(node); if let Some(neighbors) = graph.adjacency.get(node) { for &neighbor in neighbors { if visited.insert(neighbor) { - queue.push(neighbor); + queue.push_back(neighbor); } } } From 383f06649f1279836a02e16146aa42f69e2476bc Mon Sep 17 00:00:00 2001 From: arcabuntu Date: Wed, 8 Oct 2025 11:06:25 +0200 Subject: [PATCH 3/3] added capacities --- src/bfs.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/bfs.rs b/src/bfs.rs index 3726cfe..be20b7b 100644 --- a/src/bfs.rs +++ b/src/bfs.rs @@ -28,9 +28,11 @@ impl Graph { /// Naive BFS implementation using VecDeque /// 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 = VecDeque::new(); - let mut result = Vec::new(); + let n_nodes = graph.num_nodes(); + + let mut visited = HashSet::with_capacity(n_nodes); + let mut queue = VecDeque::with_capacity(n_nodes); + let mut result = Vec::with_capacity(n_nodes); queue.push_back(start); visited.insert(start);