File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -26,21 +26,20 @@ impl Graph {
2626/// Naive BFS implementation using Vec as a queue (intentionally slow)
2727/// Returns the order in which nodes were visited
2828pub fn bfs_naive ( graph : & Graph , start : usize ) -> VecDeque < usize > {
29- let mut visited = HashSet :: new ( ) ;
30- let mut queue = VecDeque :: with_capacity ( 256 ) ; // Using Vec instead of VecDeque - intentionally inefficient!
29+ let mut visited = [ false ; 256 ] ;
30+ let mut queue = VecDeque :: with_capacity ( 256 ) ;
3131 let mut result = VecDeque :: with_capacity ( 256 ) ;
3232
3333 queue. push_back ( start) ;
34- visited. insert ( start) ;
34+ visited[ start] = true ;
3535
3636 while let Some ( node) = queue. pop_front ( ) {
3737 result. push_back ( node) ;
3838
3939 if let Some ( neighbors) = graph. adjacency . get ( node) {
4040 for & neighbor in neighbors {
41- if visited. insert ( neighbor) {
42- queue. push_back ( neighbor) ;
43- }
41+ visited[ neighbor] = true ;
42+ queue. push_back ( neighbor) ;
4443 }
4544 }
4645 }
You can’t perform that action at this time.
0 commit comments