Implement dfs.