Python - Union by Rank & DFS
Union Find Solution\n\nclass Solution:\n    def makeConnected(self, n: int, connections: List[List[int]]) -> int:\n        if len(connections) < n - 1:\n       
Union Find Solution\n```\nclass Solution:\n    def makeConnected(self, n: int, connections: List[List[int]]) -> int:\n        if len(connections) < n - 1:\n            return -1\n\n        parent_nodes = {i: i for i in range(n)}\n        rank_nodes = {i: 0 for i in range(n)}\n\n        def find(node):\n            if node != parent_nodes[node]:\n                parent_nodes[node] = find(parent_nodes[node])\n            return parent_nodes[node]\n\n        def merge(node_x, node_y):\n            parent_x = find(node_x)\n            parent_y = find(node_y)\n            if parent_x != parent_y:\n                rank_x = rank_nodes[parent_x]\n                rank_y = rank_nodes[parent_y]\n                if rank_x < rank_y:\n                    parent_nodes[parent_x] = parent_y\n                elif rank_x > rank_y:\n                    parent_nodes[parent_y] = parent_x\n                elif rank_x == rank_y:\n                    parent_nodes[parent_x] = parent_y\n                    rank_nodes[parent_y] += 1\n                return True\n            return False\n        \n        for u, v in connections:\n            if merge(u, v):\n                n -= 1\n           \n        return n - 1\n```\n\nDepth First Search Solution\n```\n\nclass Solution:\n    def makeConnected(self, n: int, connections: List[List[int]]) -> int:\n        if len(connections) + 1 < n:\n            return -1\n        \n        graph = collections.defaultdict(set)\n        \n        for u, v in connections:\n            graph[u].add(v)\n            graph[v].add(u)\n        \n        visited_nodes = set()\n        collection_of_nodes = 0\n        \n        def dfs(node):\n            visited_nodes.add(node)\n            for adj_node in graph[node]:\n                if adj_node not in visited_nodes:\n                    dfs(adj_node)\n        \n        for node in range(n):\n            if node not in visited_nodes:\n                dfs(node)\n                collection_of_nodes += 1\n        \n        return collection_of_nodes - 1\n```
['Depth-First Search', 'Union Find', 'Python']