Modifying bfs and dfs