File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -27,15 +27,13 @@ impl Graph {
2727/// Returns the order in which nodes were visited
2828pub fn bfs_naive ( graph : & Graph , start : usize ) -> VecDeque < usize > {
2929 let mut visited = HashSet :: new ( ) ;
30- let mut queue = VecDeque :: new ( ) ; // Using Vec instead of VecDeque - intentionally inefficient!
31- let mut result = VecDeque :: new ( ) ;
30+ let mut queue = VecDeque :: with_capacity ( 256 ) ; // Using Vec instead of VecDeque - intentionally inefficient!
31+ let mut result = VecDeque :: with_capacity ( 256 ) ;
3232
3333 queue. push_back ( start) ;
3434 visited. insert ( start) ;
3535
36- while !queue. is_empty ( ) {
37- // remove(0) is O(n) - this makes BFS slow!
38- let node = queue. pop_front ( ) . unwrap ( ) ;
36+ while let Some ( node) = queue. pop_front ( ) {
3937 result. push_back ( node) ;
4038
4139 if let Some ( neighbors) = graph. adjacency . get ( node) {
You can’t perform that action at this time.
0 commit comments