|
| 1 | +# https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ |
| 2 | + |
| 3 | +from typing import List |
| 4 | + |
| 5 | +class Solution: |
| 6 | + def countComponents_bfs(self, n: int, edges: List[List[int]]) -> int: |
| 7 | + """ |
| 8 | + [Complexity] |
| 9 | + - TC: O(v + e) (모든 edge & node 한 번씩 탐색) |
| 10 | + - SC: O(v + e) (graph) |
| 11 | +
|
| 12 | + [Approach] |
| 13 | + BFS로 visited를 기록해가며 connected component를 센다. |
| 14 | + """ |
| 15 | + from collections import deque |
| 16 | + |
| 17 | + graph = [[] for _ in range(n)] |
| 18 | + for a, b in edges: |
| 19 | + graph[a].append(b) |
| 20 | + graph[b].append(a) |
| 21 | + |
| 22 | + visited, res = set(), 0 |
| 23 | + |
| 24 | + def bfs(start): |
| 25 | + q = deque([start]) |
| 26 | + visited.add(start) |
| 27 | + |
| 28 | + while q: |
| 29 | + pos = q.popleft() |
| 30 | + for npos in graph[pos]: |
| 31 | + if npos not in visited: |
| 32 | + q.append(npos) |
| 33 | + visited.add(npos) |
| 34 | + |
| 35 | + return |
| 36 | + |
| 37 | + for i in range(n): |
| 38 | + if i not in visited: |
| 39 | + bfs(i) |
| 40 | + res += 1 |
| 41 | + |
| 42 | + return res |
| 43 | + |
| 44 | + def countComponents_dfs(self, n: int, edges: List[List[int]]) -> int: |
| 45 | + """ |
| 46 | + [Complexity] |
| 47 | + - TC: O(v + e) (모든 edge & node 한 번씩 탐색) |
| 48 | + - SC: O(v + e) (graph) |
| 49 | +
|
| 50 | + [Approach] |
| 51 | + DFS로 visited를 기록해가며 connected component를 센다. |
| 52 | + """ |
| 53 | + graph = [[] for _ in range(n)] |
| 54 | + for a, b in edges: |
| 55 | + graph[a].append(b) |
| 56 | + graph[b].append(a) |
| 57 | + |
| 58 | + visited, res = set(), 0 |
| 59 | + |
| 60 | + def dfs(pos): |
| 61 | + # 이전에 visited 포함 여부 확인하므로 base condition 생략 가능 |
| 62 | + |
| 63 | + visited.add(pos) |
| 64 | + |
| 65 | + # recur |
| 66 | + for npos in graph[pos]: |
| 67 | + if npos not in visited: |
| 68 | + dfs(npos) |
| 69 | + |
| 70 | + return |
| 71 | + |
| 72 | + for i in range(n): |
| 73 | + if i not in visited: |
| 74 | + dfs(i) |
| 75 | + res += 1 |
| 76 | + |
| 77 | + return res |
| 78 | + |
| 79 | + def countComponents(self, n: int, edges: List[List[int]]) -> int: |
| 80 | + """ |
| 81 | + [Complexity] |
| 82 | + - TC: O(v + e * α(v)) (모든 edge & node 한 번씩 탐색) |
| 83 | + - SC: O(v) (parent, set(...)) |
| 84 | +
|
| 85 | + [Approach] |
| 86 | + edges를 iterate 하며 union-find 수행 후, parent의 종류의 개수를 세면 된다. |
| 87 | + parent의 종류의 개수를 셀 때는 다시 find_parent(x)로 찾아야 한다! |
| 88 | + """ |
| 89 | + |
| 90 | + def find_parent(x): |
| 91 | + if x != parent[x]: |
| 92 | + parent[x] = find_parent(parent[x]) |
| 93 | + return parent[x] |
| 94 | + |
| 95 | + def union_parent(x, y): |
| 96 | + px, py = find_parent(x), find_parent(y) |
| 97 | + |
| 98 | + if px < py: |
| 99 | + parent[py] = px |
| 100 | + else: |
| 101 | + parent[px] = py |
| 102 | + |
| 103 | + parent = [i for i in range(n)] |
| 104 | + |
| 105 | + for x, y in edges: |
| 106 | + union_parent(x, y) |
| 107 | + |
| 108 | + return len(set(find_parent(i) for i in range(n))) |
0 commit comments