consolidate the bfs and dfs aglorithms