A DFS and BFS algorithm