-
Notifications
You must be signed in to change notification settings - Fork 0
/
disjointset.py
189 lines (161 loc) · 5.26 KB
/
disjointset.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
class DisjointSet:
def __init__(self, s, pathop=None):
self._root = set(s)
self._parent = dict(zip(s, s))
if pathop == 'compression':
self.find = self._compression
elif pathop == 'splitting':
self.find = self._splitting
elif pathop == 'halving':
self.find = self._halving
def _compression(self, x):
r = x
while r != self._parent[r]:
r = self._parent[r]
while x != r:
p = self._parent[x]
self._parent[x] = r
x = p
return x
def _splitting(self, x):
while x != self._parent[x]:
p = self._parent[x]
self._parent[x] = self._parent[p]
x = p
return x
def _halving(self, x):
while x != self._parent[x]:
p = self._parent[x]
self._parent[x] = self._parent[p]
x = self._parent[x]
return x
def find(self, x):
while x != self._parent[x]:
x = self._parent[x]
return x
def root_set(self):
return self._root.copy()
def make_set(self, x):
if x not in self._parent:
self._parent[x] = x
self._root.add(x)
return True
else:
return False
def joined(self, x, y):
return (self.find(x) == self.find(y))
def join(self, x, y):
rx, ry = self.find(x), self.find(y)
if rx == ry:
return False
if self._parent[rx] < self._parent[ry]:
rx, ry = ry, rx
self._parent[ry] = rx
self._root.remove(ry)
return True
def clear(self):
self._root = set()
for x in self._parent:
self._parent[x] = x
self._root.add(x)
def size(self):
return len(self._root)
def __len__(self):
return len(self._parent)
class DisjointWeightedSet(DisjointSet):
def __init__(self, s, pathop=None):
DisjointSet.__init__(self, s, pathop)
self._weight = dict.fromkeys(s, 1)
def make_set(self, x):
if x not in self._parent:
self._parent[x] = x
self._weight[x] = 1
self._root.add(x)
return True
else:
return False
def join(self, x, y):
rx, ry = self.find(x), self.find(y)
if rx == ry:
return False
if self._weight[rx] < self._weight[ry]:
rx, ry = ry, rx
self._parent[ry] = rx
self._weight[rx] += self._weight[ry]
self._root.remove(ry)
return True
def weight(self, x):
return self._weight[self.find(x)]
def clear(self):
DisjointSet.clear(self)
for x in self._parent:
self._weight[x] = 1
class DisjointRankedSet(DisjointSet):
def __init__(self, s, pathop=None):
DisjointSet.__init__(self, s, pathop)
self._rank = dict.fromkeys(s, 0)
def make_set(self, x):
if x not in self._parent:
self._parent[x] = x
self._rank[x] = 0
self._root.add(x)
return True
else:
return False
def join(self, x, y):
rx, ry = self.find(x), self.find(y)
if rx == ry:
return False
if self._rank[rx] == self._rank[ry]:
self._rank[rx] += 1
elif self._rank[rx] < self._rank[ry]:
rx, ry = ry, rx
self._parent[ry] = rx
self._root.remove(ry)
return True
def rank(self, x):
return self._rank[self.find(x)]
def clear(self):
DisjointSet.clear(self)
for x in self._parent:
self._rank[x] = 0
class DisjointCrossedSet(DisjointSet):
def join(self, x, y):
while self._parent[x] != self._parent[y]:
if self._parent[x] < self._parent[y]:
x, y = y, x
if y == self._parent[y]:
self._parent[y] = self._parent[x]
self._root.remove(y)
return True
t = self._parent[y]
self._parent[y] = self._parent[x]
y = t
return False
if __name__ == 'builtins':
import networkx as nx
from random import shuffle
grph = nx.erdos_renyi_graph(400, 0.1, seed=None, directed=False)
edg = list(grph.edges())
print("edges - ", len(edg))
cut_value, partition = nx.stoer_wagner(grph)
print("stoer_wagner - ", cut_value)
gmin_cut = float('inf')
for __ in range(400):
shuffle(edg)
ds = DisjointRankedSet(range(400), pathop='splitting')
i = 0
while ds.size() > 2 and i < len(edg):
ds.join(edg[i][0], edg[i][1])
i += 1
#print(ds.size(), i)
min_cut = 0
while i < len(edg):
if not ds.joined(edg[i][0], edg[i][1]):
min_cut += 1
if min_cut >= gmin_cut:
break
i += 1
if min_cut < gmin_cut:
gmin_cut = min_cut
print("karger - ", gmin_cut)