-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxc.hpp
352 lines (342 loc) · 7.25 KB
/
maxc.hpp
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#ifndef MAXC
#define MAXC
#include <iostream>
#include <algorithm>
#include <assert.h>
class Qmax
{
const bool* const* edgls;
int ALLSTP, lv;
const float Tlimit;
class Vtxs
{
class Vtx
{
int i, d;
public:
void setI(const int i)
{
this->i = i;
}
int getI() const
{
return i;
}
void setDeg(int d)
{
this->d = d;
}
int getDeg() const
{
return d;
}
};
Vtx *v;
int sz;
static bool descDeg(const Vtx vi, const Vtx vj)
{
return (vi.getDeg() > vj.getDeg());
}
public:
Vtxs(int size) : sz(0)
{
v = new Vtx[size];
}
~Vtxs () {}
void dispose()
{
if (v) delete [] v;
}
void sort()
{
std::sort(v, v+sz, descDeg);
}
void initCol();
void setDegs(Qmax&);
int size() const
{
return sz;
}
void push(const int i)
{
v[sz++].setI(i);
};
void pop()
{
sz--;
};
Vtx& at(const int i) const
{
return v[i];
};
Vtx& end() const
{
return v[sz - 1];
};
};
class ColorClass
{
int *i;
int sz;
public:
ColorClass() : sz(0), i(0) {}
ColorClass(const int sz) : sz(sz), i(0)
{
init(sz);
}
~ColorClass()
{
if (i) delete [] i;
}
void init(const int sz)
{
i = new int[sz];
rewind();
}
void push(const int i)
{
this->i[sz++] = i;
};
void pop()
{
sz--;
};
void rewind()
{
sz = 0;
};
int size() const
{
return sz;
}
int& at(const int i) const
{
return this->i[i];
}
ColorClass& operator=(const ColorClass& dh)
{
for (int j = 0; j < dh.sz; j++) i[j] = dh.i[j];
sz = dh.sz;
return *this;
}
};
Vtxs V;
ColorClass *C, _QMAX, Q;
class StpCnt
{
int i, j;
public:
StpCnt() : i(0), j(0) {}
void setI(const int i)
{
this->i = i;
}
int getI() const
{
return i;
}
void setJ(const int j)
{
this->j = j;
}
int getJ() const
{
return j;
}
void incI()
{
i++;
}
};
StpCnt *S;
bool isadjacent(const int i, const int j) const
{
return edgls[i][j];
}
bool cuttable(const int, const ColorClass&);
void cut(const Vtxs&, Vtxs&);
void coloration(Vtxs&);
void expand(Vtxs);
void recursiveExpand(Vtxs);
void degSort(Vtxs &R)
{
R.setDegs(*this);
R.sort();
}
public:
void maxc(int*&, int&);
Qmax(const bool* const*, const int, const float=0.025);
int steps() const
{
return ALLSTP;
}
~Qmax()
{
if (C) delete [] C;
if (S) delete [] S;
V.dispose();
};
};
Qmax::Qmax (const bool* const* graph, const int sz, const float tt) : ALLSTP(0), lv(1), Tlimit(tt), V(sz), Q(sz), _QMAX(sz)
{
assert(graph!=0 && sz>0);
for (int i=0; i < sz; i++) V.push(i);
edgls = graph;
C = new ColorClass[sz + 1];
for (int i=0; i < sz + 1; i++) C[i].init(sz + 1);
S = new StpCnt[sz + 1];
}
void Qmax::maxc(int* &Qmax, int &sz)
{
V.setDegs(*this);
V.sort();
V.initCol();
for (int i=0; i < V.size() + 1; i++)
{
S[i].setI(0);
S[i].setJ(0);
}
recursiveExpand(V);
Qmax = new int[_QMAX.size()];
for (int i=0; i<_QMAX.size(); i++)
{
Qmax[i] = _QMAX.at(i);
}
sz = _QMAX.size();
}
void Qmax::Vtxs::initCol()
{
const int maxDeg = v[0].getDeg();
for (int i = 0; i < maxDeg; i++)
v[i].setDeg(i + 1);
for (int i = maxDeg; i < sz; i++)
v[i].setDeg(maxDeg + 1);
}
void Qmax::Vtxs::setDegs(Qmax &m)
{
for (int i=0; i < sz; i++)
{
int d = 0;
for (int j=0; j < sz; j++)
if (m.isadjacent(v[i].getI(), v[j].getI())) d++;
v[i].setDeg(d);
}
}
bool Qmax::cuttable(const int pi, const ColorClass &A)
{
for (int i = 0; i < A.size(); i++)
if (isadjacent(pi, A.at(i)))
return true;
return false;
}
void Qmax::cut(const Vtxs &A, Vtxs &B)
{
for (int i = 0; i < A.size() - 1; i++)
{
if (isadjacent(A.end().getI(), A.at(i).getI()))
B.push(A.at(i).getI());
}
}
void Qmax::coloration(Vtxs &R)
{
int j = 0;
int maxno = 1;
int kmin = _QMAX.size() - Q.size() + 1;
C[1].rewind();
C[2].rewind();
int k = 1;
for (int i=0; i < R.size(); i++)
{
int pi = R.at(i).getI();
k = 1;
while (cuttable(pi, C[k]))
k++;
if (k > maxno)
{
maxno = k;
C[maxno + 1].rewind();
}
C[k].push(pi);
if (k < kmin)
{
R.at(j++).setI(pi);
}
}
if (j > 0) R.at(j-1).setDeg(0);
if (kmin <= 0) kmin = 1;
for (k = kmin; k <= maxno; k++)
for (int i = 0; i < C[k].size(); i++)
{
R.at(j).setI(C[k].at(i));
R.at(j++).setDeg(k);
}
}
void Qmax::expand(Vtxs R)
{
while (R.size())
{
if (Q.size() + R.end().getDeg() > _QMAX.size())
{
Q.push(R.end().getI());
Vtxs Rp(R.size());
cut(R, Rp);
if (Rp.size())
{
coloration(Rp);
ALLSTP++;
expand(Rp);
}
else if (Q.size() > _QMAX.size())
{
std::cout << "etape n = " << ALLSTP << " taille de clique max = " << Q.size() << std::endl;
_QMAX = Q;
}
Rp.dispose();
Q.pop();
}
else
{
return;
}
R.pop();
}
}
void Qmax::recursiveExpand(Vtxs R)
{
S[lv].setI(S[lv].getI() + S[lv - 1].getI() - S[lv].getJ());
S[lv].setJ(S[lv - 1].getI());
while (R.size())
{
if (Q.size() + R.end().getDeg() > _QMAX.size())
{
Q.push(R.end().getI());
Vtxs Rp(R.size());
cut(R, Rp);
if (Rp.size())
{
if ((float)S[lv].getI()/++ALLSTP < Tlimit)
{
degSort(Rp);
}
coloration(Rp);
S[lv].incI();
lv++;
recursiveExpand(Rp);
lv--;
}
else if (Q.size() > _QMAX.size())
{
std::cout << "etape n = " << ALLSTP << " taille de clique max= " << Q.size() << std::endl;
_QMAX = Q;
}
Rp.dispose();
Q.pop();
}
else
{
return;
}
R.pop();
}
}
#endif //MAXC