Breadth First Search--->Python3
\n\n# 1. Breadth First Search--->O(N)\n\nclass Solution(object):\n    def makeConnected(self, n, connections):\n        if len(connections)<n-1:\n            re
\n\n# 1. Breadth First Search--->O(N)\n```\nclass Solution(object):\n    def makeConnected(self, n, connections):\n        if len(connections)<n-1:\n            return -1\n        graph=defaultdict(list)\n        for src,dst in connections:\n            graph[src].append(dst)\n            graph[dst].append(src)\n        visit=[False]*n\n        def bfs(start):\n            q=deque()\n            q.append(start)\n            while len(q)>0:\n                poping=q.popleft()\n                for i in graph[poping]:\n                    if not visit[i]:\n                        visit[i]=True\n                        q.append(i)\n        count=0\n        for i in range(n):\n            if not visit[i]:\n                count+=1\n                bfs(i)\n        return count-1\n\n```\n# please upvote me it would encourage me alot\n
['Python']