finish dfs algorithm.