-
Notifications
You must be signed in to change notification settings - Fork 2
/
union_find.py
139 lines (114 loc) · 2.66 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
# -*- coding: utf-8 -*-
"""
Undergraduates - S4
Graphs connectivity:
Union-find algorithms and applications
Here graphs are given as pairs (order, edge list)
"""
from algopy.timing import timing
def __loadGRAasList(filename):
f = open(filename)
_ = bool(int(f.readline())) # useless, always False!
order = int(f.readline())
L = []
for line in f.readlines():
edge = line.strip().split(' ')
L.append((int(edge[0]), int(edge[1])))
f.close()
return (order, L)
# Union-Find
def find(x, p):
while p[x] >= 0:
x = p[x]
return x
def union(x, y, p):
rx = find(x, p)
ry = find(y, p)
if rx != ry:
p[ry] = rx
return True
else:
return False
#@timing
def buildUnionFind(n, L):
'''
n: integer > 0
L: list of pairs (a, b) with a and b in [0, n[
'''
p = [-1]*n
for (x, y) in L:
union(x, y, p)
return p
# optimized versions
def find2(x, p):
rx = x
while p[rx] >= 0:
rx = p[rx]
while p[x] >= 0:
(p[x], x) = (rx, p[x])
return rx
def union2(x, y, p):
rx = find2(x, p)
ry = find2(y, p)
if rx != ry:
if p[rx] < p[ry]:
p[rx] = p[rx] + p[ry]
p[ry] = rx
else:
p[ry] = p[ry] + p[rx]
p[rx] = ry
return True
else:
return False
#@timing
def buildUnionFind2(n, L):
'''
n: integer > 0
L: list of pairs (a, b) with a and b in [0, n[
'''
p = [-1]*n
for (x, y) in L:
union2(x, y, p)
return p
# applications
def makeMeConnected(n, L):
"""
return the list of edges to add
"""
p = buildUnionFind2(n, L)
x = 0
while p[x] >= 0:
x += 1
L = []
for y in range(x+1, n):
if p[y] < 0:
L.append((x, y))
# x = y
return L
def makeComponentsUF(L, n):
p = buildUnionFind2(n, L)
cc = [None]*n
k = 0
for s in range(n) :
if p[s] < 0:
k += 1
cc[s] = k
for s in range(n):
cc[s] = cc[find2(s, p)]
return (cc, k)
# TODO: find a better version
def __nbVertexInComponentsUF(p):
CCsizes = []
for i in range(len(p)):
if p[i] < 0:
CCsizes.append(-p[i])
return CCsizes
def Moon(n, L):
p = buildUnionFind2(L, n)
nbVert = __nbVertexInComponentsUF(p)
k = len(nbVert)
ways = 0
for a in range(k):
for b in range(a+1, k):
ways += nbVert[a]*nbVert[b]
return ways