Implement DFS to detect the connected components of the graph