-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathunion.py
52 lines (47 loc) · 1.39 KB
/
union.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
"""
No longer used.
"""
class UnionFind():
"""
WeightedQuickUnion data structure for connectivity that also supplies a
reverse mapping in order to isolate each class.
"""
def __init__(self, vertices):
self.parent = {v:v for v in vertices}
self.size = {v:1 for v in vertices}
self.followers = {v:[v] for v in vertices}
def find(self, p):
"""
Helper method that finds the `boss` of a vertex.
"""
if p == self.parent[p]:
return p
self.parent[p] = self.find(self.parent[p])
return self.parent[p]
def connect(self, p, q):
"""
Connects vertices p and q.
"""
i, j = self.find(p), self.find(q)
if i == j:
return
if self.size[i] < self.size[j]:
self.parent[i] = j
self.size[j] += self.size[i]
self.followers[j] += self.followers[i]
self.followers.pop(i)
else:
self.parent[j] = i
self.size[i] += self.size[j]
self.followers[i] += self.followers[j]
self.followers.pop(j)
def is_connected(self, p, q):
"""
Checks whether vertices p and q are connected.
"""
return self.find(p) == self.find(q)
def classes(self):
"""
Returns a view inside the classes mapping.
"""
return self.followers