python, union find, dfs, bfs
union find:\n\npy\nclass UnionFind(object):\n    def __init__(self, n):\n        self.u = list(range(n))\n        \n    def union(self, a, b):\n        ra, rb =
union find:\n\n```py\nclass UnionFind(object):\n    def __init__(self, n):\n        self.u = list(range(n))\n        \n    def union(self, a, b):\n        ra, rb = self.find(a), self.find(b)\n        if ra != rb: self.u[ra] = rb\n    \n    def find(self, a):\n        while self.u[a] != a: a = self.u[a]\n        return a\n    \nclass Solution(object):\n    def findCircleNum(self, M):\n        """\n        :type M: List[List[int]]\n        :rtype: int\n        """\n        \n        if not M: return 0\n        s = len(M)\n        \n        uf = UnionFind(s)\n        for r in range(s):\n            for c in range(r,s):\n                if M[r][c] == 1: uf.union(r,c)\n                    \n        return len(set([uf.find(i) for i in range(s)]))\n```\n\nDFS:\n\n```py\nclass Solution(object):\n    def findCircleNum(self, M):\n        """\n        :type M: List[List[int]]\n        :rtype: int\n        """\n        \n        if not M: return 0\n        s = len(M)\n        seen = set()\n        \n        def dfs(p):\n            for q, adj in enumerate(M[p]):\n                if (adj == 1) and (q not in seen):\n                    seen.add(q)\n                    dfs(q)\n        \n        cnt = 0\n        for i in range(s):\n            if i not in seen: \n                dfs(i)\n                cnt += 1\n        \n        return cnt\n```\n\nBFS:\n\n```py\nclass Solution(object):\n    def findCircleNum(self, M):\n        """\n        :type M: List[List[int]]\n        :rtype: int\n        """\n        \n        if not M: return 0\n        s = len(M)\n        seen = set()\n        cnt = 0\n        for i in range(s):\n            if i not in seen:\n                q = [i]\n                while q:\n                    p = q.pop(0)\n                    if p not in seen:\n                        seen.add(p)\n                        q += [k for k,adj in enumerate(M[p]) if adj and (k not in seen)]\n                cnt += 1\n        \n        return cnt\n```\n
['Depth-First Search', 'Breadth-First Search', 'Union Find', 'Python']