Separated dfs algorithm