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,20 +26,21 @@ 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 ) -> Vec < usize > {
29- let mut visited = HashSet :: new ( ) ;
29+ let mut visited = Vec :: from_iter ( ( 0 ..graph . num_nodes ( ) ) . map ( |_| false ) ) ;
3030 let mut queue = VecDeque :: with_capacity ( 1024 ) ;
3131 let mut result = Vec :: with_capacity ( graph. num_nodes ( ) ) ;
3232
3333 queue. push_back ( start) ;
34- visited. insert ( start) ;
34+ visited[ start] = true ;
3535
3636 while !queue. is_empty ( ) {
3737 let node = queue. pop_front ( ) . unwrap ( ) ;
3838 result. push ( node) ;
3939
4040 if let Some ( neighbors) = graph. adjacency . get ( node) {
4141 for & neighbor in neighbors {
42- if visited. insert ( neighbor) {
42+ if !visited[ neighbor] {
43+ visited[ neighbor] = true ;
4344 queue. push_back ( neighbor) ;
4445 }
4546 }
You can’t perform that action at this time.
0 commit comments