Easy To Understand - Python Solution Using DFS
The 2 Main Logic of This Implementation Are :-\n\t\'\'\'\n    For every 2 disconnected subGraph, we need 1 edge to connect them.\n    that means, return the num
# The 2 Main Logic of This Implementation Are :-\n\t\'\'\'\n    For every 2 disconnected subGraph, we need 1 edge to connect them.\n    that means, return the number of disconnected subGraph - 1 \n    \'\'\'\n    \n    \'\'\'\n    Perform DFS, on every node\n    If it\'s Un Visited, add one to the counter of disconnected subGraphs.\n    then visit it and perform DFS on that node.\n    \'\'\'\n\t\n\t\'\'\'  T.C = S.C = O( V + E )\'\'\'\n```\nclass Solution:\n    def makeConnected(self, n: int, connections: List[List[int]]) -> int:\n        \n        if len(connections) < n - 1: return -1\n        \n        def DO_DFS(isVisited, graph, node):\n            stack = [node]\n            while stack:\n                vertices = stack.pop()\n                if vertices not in isVisited:\n                    isVisited.add(vertices)\n                    \n                    for vertex in graph[vertices]:\n                        if vertex not in isVisited:\n                            stack.append(vertex)\n        \n        graph = [ set() for _ in range(n) ]\n        \n        for u, v in connections:\n            graph[u].add(v)\n            graph[v].add(u)\n            \n        isVisited = set()\n        disconnectedSubGraph = 0\n        \n        for node in range(n):\n            if node not in isVisited:\n                disconnectedSubGraph += 1\n                DO_DFS(isVisited, graph, node)\n                \n        return disconnectedSubGraph - 1\n        \n```
['Depth-First Search', 'Python']