-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path547.py
115 lines (99 loc) · 3.88 KB
/
547.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# [ LeetCode ] 547. Number of Provinces
def solution(is_connected: list[list[int]]) -> int:
class UnionFind:
BASE_HEIGHT: int = 1
def __init__(self, size: int) -> None:
self.root: list[int] = [ vertex for vertex in range(size) ]
self.rank: list[int] = [
UnionFind.BASE_HEIGHT for _ in range(size)
]
def find(self, vertex: int) -> int:
if vertex == self.root[vertex]:
return vertex
self.root[vertex]: int = self.find(self.root[vertex])
return self.root[vertex]
def union(self, u: int, v: int) -> None:
u_root: int = self.find(u)
v_root: int = self.find(v)
if u_root != v_root:
u_rank: int = self.rank[u_root]
v_rank: int = self.rank[v_root]
if u_rank > v_rank:
self.root[v_root]: int = u_root
elif u_rank < v_rank:
self.root[u_root]: int = v_root
else:
self.root[v_root]: int = u_root
self.rank[u_root] += 1
union_find: UnionFind = UnionFind(len(is_connected))
for i in range(len(is_connected)-1):
for j in range(i + 1, len(is_connected)):
if is_connected[i][j] == 1:
union_find.union(i, j)
root: set[int] = set()
for vertex in range(len(is_connected)):
parent: int = union_find.find(vertex)
root.add(parent)
return len(root)
def another_solution(is_connected: list[list[int]]) -> int:
class UnionFind:
BASE_HEIGHT: int = 1
def __init__(self, size: int) -> None:
self.root: list[int] = [ vertex for vertex in range(size) ]
self.rank: list[int] = [
UnionFind.BASE_HEIGHT for _ in range(size)
]
self.count: int = size
def find(self, vertex: int) -> int:
if vertex == self.root[vertex]:
return vertex
self.root[vertex]: int = self.find(self.root[vertex])
return self.root[vertex]
def union(self, u: int, v: int) -> None:
u_root: int = self.find(u)
v_root: int = self.find(v)
if u_root != v_root:
u_rank: int = self.rank[u_root]
v_rank: int = self.rank[v_root]
if u_rank > v_rank:
self.root[v_root]: int = u_root
elif u_rank < v_rank:
self.root[u_root]: int = v_root
else:
self.root[v_root]: int = u_root
self.rank[u_root] += 1
self.count -= 1
def get_connected_components_count(self) -> int:
return self.count
union_find: UnionFind = UnionFind(len(is_connected))
for i in range(len(is_connected)-1):
for j in range(i + 1, len(is_connected)):
if is_connected[i][j] == 1:
union_find.union(i, j)
return union_find.get_connected_components_count()
if __name__ == "__main__":
cases: list[dict[str, dict[list[list[int]]] | int]] = [
{
"input": {
"is_connected": [
[1, 1, 0],
[1, 1, 0],
[0, 0, 1]
]
},
"output": 2
},
{
"input": {
"is_connected": [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
]
},
"output": 3
}
]
for case in cases:
assert case["output"] == solution(**case["input"])
assert case["output"] == another_solution(**case["input"])