finishing DFS and formatting output