-
Notifications
You must be signed in to change notification settings - Fork 0
/
union_find.py
248 lines (188 loc) · 6.84 KB
/
union_find.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import time
from random import randint
# These data structures are also called disjoint sets.
##############################
### Quick Find Union Find. ###
##############################
class QuickFindUF:
# Initialize union-find data structure with N objects (0 to N – 1).
def __init__(self, N):
self.id = [0] * N
for i in range(N):
self.id[i] = i
# Return true if p and q are in the same component.
def connected(self, p, q):
if (p not in range(len(self.id)) or q not in range(len(self.id))):
raise IndexError('p and q should be in range [0, N)')
return self.id[p] == self.id[q]
# Add connection between p and q.
def union(self, p, q):
if (p not in range(len(self.id)) or q not in range(len(self.id))):
raise IndexError('p and q should be in range [0, N)')
pid = self.id[p]
qid = self.id[q]
for i in range(len(self.id)):
if (self.id[i] == pid):
self.id[i] = qid
# Experiments to check time complexity.
if __name__ == "__main__":
qf = QuickFindUF(500)
start_time = time.time()
for i in range(500):
i1 = randint(0, 499)
i2 = randint(0, 499)
qf.union(i1, i2)
print("Quick-find 500 takes %s seconds" % (time.time() - start_time))
# The following will take ~4 times longer.
qf = QuickFindUF(1000)
start_time = time.time()
for i in range(1000):
i1 = randint(0, 999)
i2 = randint(0, 999)
qf.union(i1, i2)
print("Quick-find 1000 takes %s seconds" % (time.time() - start_time))
###############################
### Quick Union Union Find. ###
###############################
class QuickUnionUF:
# Initialize union-find data structure with N objects (0 to N – 1).
def __init__(self, N):
self.id = [0] * N
for i in range(N):
self.id[i] = i
# Returns the depth of a node from its root.
# Used for analysis here, it's not in the slides.
def depth(self, i):
c = 0
while (i != self.id[i]):
i = self.id[i]
c += 1
return c
# Return true if p and q are in the same component.
def connected(self, p, q):
if (p not in range(len(self.id)) or q not in range(len(self.id))):
raise IndexError('p and q should be in range [0, N)')
return self.__root(p) == self.__root(q)
# Add connection between p and q.
def union(self, p , q):
if (p not in range(len(self.id)) or q not in range(len(self.id))):
raise IndexError('p and q should be in range [0, N)')
i = self.__root(p)
j = self.__root(q)
self.id[i] = j
###
### HELPER FUNCTIONS
###
def __root(self, i):
while (i != self.id[i]):
i = self.id[i]
return i
# Experiments to check time complexity.
if __name__ == "__main__":
qu = QuickUnionUF(5000)
start_time = time.time()
for i in range(5000):
i1 = randint(0, 4999)
i2 = randint(0, 4999)
qu.union(i1, i2)
print("Quick-union 5000 takes %s seconds" % (time.time() - start_time))
a = [None] * 5000
for i in range(5000):
a[i] = qu.depth(i)
print("Maximum depth -> " + str(max(a)))
print("Average depth -> " + str(sum(a) / len(a)))
# The following will take ~4 times longer in the worst case.
qu = QuickUnionUF(10000)
start_time = time.time()
for i in range(10000):
i1 = randint(0, 9999)
i2 = randint(0, 9999)
qu.union(i1, i2)
print("Quick-union 10000 takes %s seconds" % (time.time() - start_time))
########################################
### Weighted Quick Union Union Find. ###
########################################
class WeightedQuickUnionUF(QuickUnionUF):
# Initialize union-find data structure with N objects (0 to N – 1).
def __init__(self, N):
self.id = [0] * N
for i in range(N):
self.id[i] = i
self.sz = [1] * N
# Add connection between p and q.
def union(self, p , q):
if (p not in range(len(self.id)) or q not in range(len(self.id))):
raise IndexError('p and q should be in range [0, N)')
i = self._QuickUnionUF__root(p)
j = self._QuickUnionUF__root(q)
if (i == j):
return
if (self.sz[i] < self.sz[j]):
self.id[i] = j
self.sz[j] += self.sz[i]
else:
self.id[j] = i
self.sz[i] += self.sz[j]
# Experiments to check time complexity.
if __name__ == "__main__":
wqu = WeightedQuickUnionUF(5000)
start_time = time.time()
for i in range(5000):
i1 = randint(0, 4999)
i2 = randint(0, 4999)
wqu.union(i1, i2)
print("Weighted quick-union 5000 takes %s seconds" % (time.time() - start_time))
a = [None] * 5000
for i in range(5000):
a[i] = wqu.depth(i)
print("Maximum depth -> " + str(max(a)))
print("Average depth -> " + str(sum(a) / len(a)))
# The following will take ~2 times longer. (Linearithmic)
wqu = WeightedQuickUnionUF(10000)
start_time = time.time()
for i in range(10000):
i1 = randint(0, 9999)
i2 = randint(0, 9999)
wqu.union(i1, i2)
print("Weighted quick-union 10000 takes %s seconds" % (time.time() - start_time))
##############################################################
### Weighted Quick Union with Path Compression Union Find. ###
##############################################################
class WeightedQuickUnionPCUF(WeightedQuickUnionUF):
# Add connection between p and q.
def union(self, p , q):
if (p not in range(len(self.id)) or q not in range(len(self.id))):
raise IndexError('p and q should be in range [0, N)')
i = self._QuickUnionUF__root(p)
j = self._QuickUnionUF__root(q)
if (i == j):
return
if (self.sz[i] < self.sz[j]):
self.id[i] = j
self.sz[j] += self.sz[i]
else:
self.id[j] = i
self.sz[i] += self.sz[j]
# Experiments to check time complexity.
if __name__ == "__main__":
wqupc = WeightedQuickUnionPCUF(5000)
start_time = time.time()
for i in range(5000):
i1 = randint(0, 4999)
i2 = randint(0, 4999)
wqupc.union(i1, i2)
print("Weighted quick-union with path compression 5000 takes %s seconds"
% (time.time() - start_time))
a = [None] * 5000
for i in range(5000):
a[i] = wqupc.depth(i)
print("Maximum depth -> " + str(max(a)))
print("Average depth -> " + str(sum(a) / len(a)))
wqupc = WeightedQuickUnionPCUF(10000)
start_time = time.time()
for i in range(10000):
i1 = randint(0, 9999)
i2 = randint(0, 9999)
wqupc.union(i1, i2)
print("Weighted quick-union with path compression 10000 takes %s seconds"
% (time.time() - start_time))