-
Notifications
You must be signed in to change notification settings - Fork 7
/
combinatorics.cpp
284 lines (227 loc) · 5.54 KB
/
combinatorics.cpp
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// combinatorics.cpp
/*===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* Author: Vyacheslav Brover
*
* File Description:
* Combinatorics utilities
*
*/
#undef NDEBUG
#include "combinatorics.hpp"
#include "common.inc"
namespace Cmb_sp
{
bool next (vector<bool> &v)
{
const size_t s = v. size ();
size_t i = 0;
while (i < s && v [i])
{
v [i] = false;
i++;
}
if (i == s)
return false;
v [i] = true;
return true;
}
bool next (vector<size_t> &indexes,
const vector<size_t> &indexes_max)
{
ASSERT (indexes. size () == indexes_max. size ());
FFOR (size_t, i, indexes. size ())
{
ASSERT (indexes [i] <= indexes_max [i]);
if (indexes [i] < indexes_max [i])
{
indexes [i] ++;
return true;
}
indexes [i] = 0;
}
return false;
}
// SubsetSearch
SubsetSearch::SubsetSearch (size_t whole_size,
size_t subset_size)
: subset (subset_size + 1)
{
ASSERT (subset_size <= whole_size);
FOR (size_t, i, subset_size)
subset [i] = i;
subset [subset_size] = whole_size;
}
bool SubsetSearch::next ()
{
FFOR (size_t, i, subset. size () - 1)
if (subset [i] + 1 == subset [i + 1])
subset [i] = i;
else
{
subset [i] ++;
return true;
}
return false;
}
void SubsetSearch::complement (Vector<size_t> &disjoint) const
{
ASSERT (! subset. empty ());
ASSERT (subset. back () >= subset. size () - 1);
const size_t size = subset. back () - (subset. size () - 1);
disjoint. reserve (size);
disjoint. clear ();
size_t j = 0;
for (size_t i : subset)
{
for (; j < i; j++)
disjoint << j;
j++;
}
ASSERT (disjoint. size () == size);
}
// MinSubsetSearch
bool MinSubsetSearch::next ()
{
for (;;)
{
if (! subset. empty ()) // Previous invocation of next() has returned true
if (! advance_wide ())
return false;
while (! complete ())
if (! advance ())
return false;
// Check minimality
size_t i = 0;
while (i + 1 < subset. size ())
if (complete (subset [i]))
break;
else
i++;
if (i + 1 == subset. size ())
return true;
ASSERT (! subset. empty ());
}
}
bool MinSubsetSearch::advance ()
{
if (verbose (-1))
{
cout << "advance:";
saveText (cout);
cout << endl;
}
if (! n)
return false;
if (subset. empty ())
subset << 0;
else if (subset. back () + 1 < n)
{
ASSERT (subset. size () < n);
subset << subset. back () + 1;
}
else
return advance_wide ();
return true;
}
bool MinSubsetSearch::advance_wide ()
{
size_t limit = n;
while (! subset. empty ())
{
ASSERT (subset. back () + 1 <= limit);
if (subset. back () + 1 == limit)
{
subset. pop ();
limit--;
}
else
{
subset. back () ++;
return true;
}
}
return false;
}
namespace
{
struct ThisSubsetSearch : MinSubsetSearch
{
Vector<Vector<bool>> arr;
size_t whole {0};
ThisSubsetSearch (size_t n_arg,
ulong seed)
: MinSubsetSearch (n_arg)
, arr (n)
, whole (5) // PAR
{ Rand rand (seed);
for (Vector<bool>& vec : arr)
{
FOR (size_t, i, whole)
vec << (rand. getProb () > 0.2);
for (const bool b : vec)
cout << b;
cout << endl;
}
}
bool complete (size_t exclude = no_index) const final
{
FOR (size_t, i, whole)
{
bool intersect = true;
for (const size_t j : subset)
if (j != exclude)
if (! arr [j] [i])
{
intersect = false;
break;
}
if (intersect)
return false;
}
return true;
}
};
}
void MinSubsetSearch::test (ulong seed)
{
ThisSubsetSearch ss (5, seed); // PAR
ASSERT (! ss. complete ());
while (ss. next ())
{
cout << "Found:";
ss. saveText (cout);
cout << endl;
}
ASSERT (ss. subset. empty ());
}
// Permute
Permute::Permute (size_t n)
: vec (n)
{
FOR (size_t, i, n)
vec [i] = i;
}
} // namespace