-
Notifications
You must be signed in to change notification settings - Fork 1
/
union_find.py
executable file
·250 lines (222 loc) · 8.37 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
249
250
##Copyright (c) 2011 duncan g. smith
##
##Permission is hereby granted, free of charge, to any person obtaining a
##copy of this software and associated documentation files (the "Software"),
##to deal in the Software without restriction, including without limitation
##the rights to use, copy, modify, merge, publish, distribute, sublicense,
##and/or sell copies of the Software, and to permit persons to whom the
##Software is furnished to do so, subject to the following conditions:
##
##The above copyright notice and this permission notice shall be included
##in all copies or substantial portions of the Software.
##
##THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
##OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
##FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
##THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
##OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
##ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
##OTHER DEALINGS IN THE SOFTWARE.
from __future__ import division
class UnionFindSet(dict):
"""
A set-based union-find data structure that allows
the partition of equivalence classes as well as
merging of equivalence classes. All items that
are added to the union-find must be hashable.
"""
def __init__(self):
"""
Initialise the instance, setting the number
of equivalence classes, I{self.size}, to zero.
@note: len(uf) returns the number of items in
the union-find, uf
"""
self.size = 0
def add(self, item):
"""
Adds an item to the union-find, raising an
exception if the item is already present.
@type item: arbitrary hashable type
@param item: an item to be added
"""
if item in self:
raise ValueError, 'item already in union-find'
self[item] = set([item])
self.size += 1
def remove(self, item):
"""
Removes the item from the union-find by removing
it from its equivalence class and deleting its
entry.
@type item: arbitrary hashable type
@param item: an item to be removed
@raise KeyError: if item is not in union-find
"""
self[item].remove(item)
if not self[item]:
self.size -= 1
del self[item]
def union(self, item1, item2):
"""
Merges the equivalence classes that have I{item1}
and I{item2} as members. If the items are members
of the same equivalence class no change is made.
@type item1: arbitrary hashable type
@param item1: member of equivalence class
to be merged
@type item2: arbitrary hashable type
@param item2: member of equivalence class
to be merged
@rtype: C{bool}
@return: True if distinct equivalence classes
are merged, otherwise False
@raise KeyError: if item is not in union-find
"""
if self[item1] is self[item2]:
return False
else:
if len(self[item1]) < len(self[item2]):
item2, item1 = item1, item2
self[item1] |= self[item2]
for item in self[item2]:
self[item] = self[item1]
self.size -= 1
return True
def partition(self, items):
"""
Partitions the equivalence class containing I{items}
into two new equivalence classes containing I{items}
and the equivalence class's remaining items.
@type items: C{set}
@param items: new equivalence class
@raise ValueError: if I{items} is not a subset
of an existing equivalence class
"""
equiv_class = self[iter(items).next()]
new_class = equiv_class - items
if not len(new_class) + len(items) == len(equiv_class):
# items is not a subset of any existing equiv_class
raise ValueError, 'items is not a subset of any \
existing equivalence class'
for item in items:
self[item] = items
for item in new_class:
self[item] = new_class
self.size += 1
class UnionFindTree(dict):
"""
A tree-based union-find data structure that allows
the merging of equivalence classes only. All items
that are added to the union-find must be hashable.
This tree-based implementation tends to be faster
than the set-based implementation, but with the
restriction that partitioning of equivalence classes
is not possible.
"""
def __init__(self):
self.ranks = {}
self.size = 0
def copy(self):
acopy = self.__class__()
for item in self:
acopy.add(item)
for item in self:
acopy.union(item, self.find(item))
return acopy
def add(self, item):
"""
Adds an item to the union-find, raising an
exception if the item is already present.
@type item: arbitrary hashable type
@param item: an item to be added
"""
if item in self:
raise ValueError, 'item already in union-find'
self[item] = None
# ranks holds ranks for each forest {root:rank}
self.ranks[item] = 0
self.size += 1
def find(self, item):
"""
Returns the root of the tree containing
I{item}.
@type item: arbitrary hashable type
@param item: an item in the union-find
@rtype: arbitrary hashable type
@return: root of tree containining I{item}
@raise KeyError: if item is not in union-find
"""
cand = item
path = []
while True:
parent = self[cand]
if parent is None:
# cand is root
for node in path:
self[node] = cand
return cand
else:
path.append(cand)
cand = parent
def union(self, item1, item2):
"""
Merges the equivalence classes that have I{item1}
and I{item2} as members. If the items are members
of the same equivalence class no change is made.
@type item1: arbitrary hashable type
@param item1: member of equivalence class
to be merged
@type item2: arbitrary hashable type
@param item2: member of equivalence class
to be merged
@rtype: C{bool}
@return: True if distinct equivalence classes
are merged, otherwise False
@raise KeyError: if item is not in union-find
"""
root1, root2 = self.find(item1), self.find(item2)
if root1 == root2:
return False
else:
if self.ranks[root1] < self.ranks[root2]:
self[root1] = root2
elif self.ranks[root2] < self.ranks[root1]:
self[root2] = root1
else:
self[root1] = root2
self.ranks[root2] += 1
self.size -= 1
return True
def partition2tree(partition):
tree = UnionFindTree()
for part in partition:
part = iter(part)
first = part.next()
tree.add(first)
for element in part:
tree.add(element)
tree.union(tree.find(first), element)
return tree
def tree2file(tree, filename):
import dot
import digraph
g = digraph.DirectedGraph()
for node in tree:
g.addNode(node)
for node in tree:
parent = tree[node]
if parent:
g.addEdge((parent, node))
dot.graph2image(g, filename, True, filename[-3:])
def harmonize(trees):
res = UnionFindTree()
for tree in trees:
for node in tree:
try:
res.add(node)
except:
pass
root = tree.find(node)
res.union(root, node)
return res