-
Notifications
You must be signed in to change notification settings - Fork 18
/
Matching.cpp
621 lines (536 loc) · 12.4 KB
/
Matching.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include "Matching.h"
Matching::Matching(const Graph & G):
G(G),
outer(2*G.GetNumVertices()),
deep(2*G.GetNumVertices()),
shallow(2*G.GetNumVertices()),
tip(2*G.GetNumVertices()),
active(2*G.GetNumVertices()),
type(2*G.GetNumVertices()),
forest(2*G.GetNumVertices()),
root(2*G.GetNumVertices()),
blocked(2*G.GetNumVertices()),
dual(2*G.GetNumVertices()),
slack(G.GetNumEdges()),
mate(2*G.GetNumVertices()),
m(G.GetNumEdges()),
n(G.GetNumVertices()),
visited(2*G.GetNumVertices())
{
}
void Matching::Grow()
{
Reset();
//All unmatched vertices will be roots in a forest that will be grown
//The forest is grown by extending a unmatched vertex w through a matched edge u-v in a BFS fashion
while(!forestList.empty())
{
int w = outer[forestList.front()];
forestList.pop_front();
//w might be a blossom
//we have to explore all the connections from vertices inside the blossom to other vertices
for(list<int>::iterator it = deep[w].begin(); it != deep[w].end(); it++)
{
int u = *it;
int cont = false;
for(list<int>::const_iterator jt = G.AdjList(u).begin(); jt != G.AdjList(u).end(); jt++)
{
int v = *jt;
if(IsEdgeBlocked(u, v)) continue;
//u is even and v is odd
if(type[outer[v]] == ODD) continue;
//if v is unlabeled
if(type[outer[v]] != EVEN)
{
//We grow the alternating forest
int vm = mate[outer[v]];
forest[outer[v]] = u;
type[outer[v]] = ODD;
root[outer[v]] = root[outer[u]];
forest[outer[vm]] = v;
type[outer[vm]] = EVEN;
root[outer[vm]] = root[outer[u]];
if(!visited[outer[vm]])
{
forestList.push_back(vm);
visited[outer[vm]] = true;
}
}
//If v is even and u and v are on different trees
//we found an augmenting path
else if(root[outer[v]] != root[outer[u]])
{
Augment(u,v);
Reset();
cont = true;
break;
}
//If u and v are even and on the same tree
//we found a blossom
else if(outer[u] != outer[v])
{
int b = Blossom(u,v);
forestList.push_front(b);
visited[b] = true;
cont = true;
break;
}
}
if(cont) break;
}
}
//Check whether the matching is perfect
perfect = true;
for(int i = 0; i < n; i++)
if(mate[outer[i]] == -1)
perfect = false;
}
bool Matching::IsAdjacent(int u, int v)
{
return (G.AdjMat()[u][v] and not IsEdgeBlocked(u, v));
}
bool Matching::IsEdgeBlocked(int u, int v)
{
return GREATER(slack[ G.GetEdgeIndex(u, v) ], 0);
}
bool Matching::IsEdgeBlocked(int e)
{
return GREATER(slack[e], 0);
}
//Vertices will be selected in non-decreasing order of their degree
//Each time an unmatched vertex is selected, it is matched to its adjacent unmatched vertex of minimum degree
void Matching::Heuristic()
{
vector<int> degree(n, 0);
BinaryHeap B;
for(int i = 0; i < m; i++)
{
if(IsEdgeBlocked(i)) continue;
pair<int, int> p = G.GetEdge(i);
int u = p.first;
int v = p.second;
degree[u]++;
degree[v]++;
}
for(int i = 0; i < n; i++)
B.Insert(degree[i], i);
while(B.Size() > 0)
{
int u = B.DeleteMin();
if(mate[outer[u]] == -1)
{
int min = -1;
for(list<int>::const_iterator it = G.AdjList(u).begin(); it != G.AdjList(u).end(); it++)
{
int v = *it;
if(IsEdgeBlocked(u, v) or
(outer[u] == outer[v]) or
(mate[outer[v]] != -1) )
continue;
if(min == -1 or degree[v] < degree[min])
min = v;
}
if(min != -1)
{
mate[outer[u]] = min;
mate[outer[min]] = u;
}
}
}
}
//Destroys a blossom recursively
void Matching::DestroyBlossom(int t)
{
if((t < n) or
(blocked[t] and GREATER(dual[t], 0))) return;
for(list<int>::iterator it = shallow[t].begin(); it != shallow[t].end(); it++)
{
int s = *it;
outer[s] = s;
for(list<int>::iterator jt = deep[s].begin(); jt != deep[s].end(); jt++)
outer[*jt] = s;
DestroyBlossom(s);
}
active[t] = false;
blocked[t] = false;
AddFreeBlossomIndex(t);
mate[t] = -1;
}
void Matching::Expand(int u, bool expandBlocked = false)
{
int v = outer[mate[u]];
int index = m;
int p = -1, q = -1;
//Find the regular edge {p,q} of minimum index connecting u and its mate
//We use the minimum index to grant that the two possible blossoms u and v will use the same edge for a mate
for(list<int>::iterator it = deep[u].begin(); it != deep[u].end(); it++)
{
int di = *it;
for(list<int>::iterator jt = deep[v].begin(); jt != deep[v].end(); jt++)
{
int dj = *jt;
if(IsAdjacent(di, dj) and G.GetEdgeIndex(di, dj) < index)
{
index = G.GetEdgeIndex(di, dj);
p = di;
q = dj;
}
}
}
mate[u] = q;
mate[v] = p;
//If u is a regular vertex, we are done
if(u < n or (blocked[u] and not expandBlocked)) return;
bool found = false;
//Find the position t of the new tip of the blossom
for(list<int>::iterator it = shallow[u].begin(); it != shallow[u].end() and not found; )
{
int si = *it;
for(list<int>::iterator jt = deep[si].begin(); jt != deep[si].end() and not found; jt++)
{
if(*jt == p )
found = true;
}
it++;
if(not found)
{
shallow[u].push_back(si);
shallow[u].pop_front();
}
}
list<int>::iterator it = shallow[u].begin();
//Adjust the mate of the tip
mate[*it] = mate[u];
it++;
//
//Now we go through the odd circuit adjusting the new mates
while(it != shallow[u].end())
{
list<int>::iterator itnext = it;
itnext++;
mate[*it] = *itnext;
mate[*itnext] = *it;
itnext++;
it = itnext;
}
//We update the sets blossom, shallow, and outer since this blossom is being deactivated
for(list<int>::iterator it = shallow[u].begin(); it != shallow[u].end(); it++)
{
int s = *it;
outer[s] = s;
for(list<int>::iterator jt = deep[s].begin(); jt != deep[s].end(); jt++)
outer[*jt] = s;
}
active[u] = false;
AddFreeBlossomIndex(u);
//Expand the vertices in the blossom
for(list<int>::iterator it = shallow[u].begin(); it != shallow[u].end(); it++)
Expand(*it, expandBlocked);
}
//Augment the path root[u], ..., u, v, ..., root[v]
void Matching::Augment(int u, int v)
{
//We go from u and v to its respective roots, alternating the matching
int p = outer[u];
int q = outer[v];
int outv = q;
int fp = forest[p];
mate[p] = q;
mate[q] = p;
Expand(p);
Expand(q);
while(fp != -1)
{
q = outer[forest[p]];
p = outer[forest[q]];
fp = forest[p];
mate[p] = q;
mate[q] = p;
Expand(p);
Expand(q);
}
p = outv;
fp = forest[p];
while(fp != -1)
{
q = outer[forest[p]];
p = outer[forest[q]];
fp = forest[p];
mate[p] = q;
mate[q] = p;
Expand(p);
Expand(q);
}
}
void Matching::Reset()
{
for(int i = 0; i < 2*n; i++)
{
forest[i] = -1;
root[i] = i;
if(i >= n and active[i] and outer[i] == i)
DestroyBlossom(i);
}
visited.assign(2*n, 0);
forestList.clear();
for(int i = 0; i < n; i++)
{
if(mate[outer[i]] == -1)
{
type[outer[i]] = 2;
if(!visited[outer[i]])
forestList.push_back(i);
visited[outer[i]] = true;
}
else type[outer[i]] = 0;
}
}
int Matching::GetFreeBlossomIndex()
{
int i = free.back();
free.pop_back();
return i;
}
void Matching::AddFreeBlossomIndex(int i)
{
free.push_back(i);
}
void Matching::ClearBlossomIndices()
{
free.clear();
for(int i = n; i < 2*n; i++)
AddFreeBlossomIndex(i);
}
//Contracts the blossom w, ..., u, v, ..., w, where w is the first vertex that appears in the paths from u and v to their respective roots
int Matching::Blossom(int u, int v)
{
int t = GetFreeBlossomIndex();
vector<bool> isInPath(2*n, false);
//Find the tip of the blossom
int u_ = u;
while(u_ != -1)
{
isInPath[outer[u_]] = true;
u_ = forest[outer[u_]];
}
int v_ = outer[v];
while(not isInPath[v_])
v_ = outer[forest[v_]];
tip[t] = v_;
//Find the odd circuit, update shallow, outer, blossom and deep
//First we construct the set shallow (the odd circuit)
list<int> circuit;
u_ = outer[u];
circuit.push_front(u_);
while(u_ != tip[t])
{
u_ = outer[forest[u_]];
circuit.push_front(u_);
}
shallow[t].clear();
deep[t].clear();
for(list<int>::iterator it = circuit.begin(); it != circuit.end(); it++)
{
shallow[t].push_back(*it);
}
v_ = outer[v];
while(v_ != tip[t])
{
shallow[t].push_back(v_);
v_ = outer[forest[v_]];
}
//Now we construct deep and update outer
for(list<int>::iterator it = shallow[t].begin(); it != shallow[t].end(); it++)
{
u_ = *it;
outer[u_] = t;
for(list<int>::iterator jt = deep[u_].begin(); jt != deep[u_].end(); jt++)
{
deep[t].push_back(*jt);
outer[*jt] = t;
}
}
forest[t] = forest[tip[t]];
type[t] = EVEN;
root[t] = root[tip[t]];
active[t] = true;
outer[t] = t;
mate[t] = mate[tip[t]];
return t;
}
void Matching::UpdateDualCosts()
{
double e1 = 0, e2 = 0, e3 = 0;
int inite1 = false, inite2 = false, inite3 = false;
for(int i = 0; i < m; i++)
{
int u = G.GetEdge(i).first,
v = G.GetEdge(i).second;
if( (type[outer[u]] == EVEN and type[outer[v]] == UNLABELED) or (type[outer[v]] == EVEN and type[outer[u]] == UNLABELED) )
{
if(!inite1 or GREATER(e1, slack[i]))
{
e1 = slack[i];
inite1 = true;
}
}
else if( (outer[u] != outer[v]) and type[outer[u]] == EVEN and type[outer[v]] == EVEN )
{
if(!inite2 or GREATER(e2, slack[i]))
{
e2 = slack[i];
inite2 = true;
}
}
}
for(int i = n; i < 2*n; i++)
{
if(active[i] and i == outer[i] and type[outer[i]] == ODD and (!inite3 or GREATER(e3, dual[i])))
{
e3 = dual[i];
inite3 = true;
}
}
double e = 0;
if(inite1) e = e1;
else if(inite2) e = e2;
else if(inite3) e = e3;
if(GREATER(e, e2/2.0) and inite2)
e = e2/2.0;
if(GREATER(e, e3) and inite3)
e = e3;
for(int i = 0; i < 2*n; i++)
{
if(i != outer[i]) continue;
if(active[i] and type[outer[i]] == EVEN)
{
dual[i] += e;
}
else if(active[i] and type[outer[i]] == ODD)
{
dual[i] -= e;
}
}
for(int i = 0; i < m; i++)
{
int u = G.GetEdge(i).first,
v = G.GetEdge(i).second;
if(outer[u] != outer[v])
{
if(type[outer[u]] == EVEN and type[outer[v]] == EVEN)
slack[i] -= 2.0*e;
else if(type[outer[u]] == ODD and type[outer[v]] == ODD)
slack[i] += 2.0*e;
else if( (type[outer[v]] == UNLABELED and type[outer[u]] == EVEN) or (type[outer[u]] == UNLABELED and type[outer[v]] == EVEN) )
slack[i] -= e;
else if( (type[outer[v]] == UNLABELED and type[outer[u]] == ODD) or (type[outer[u]] == UNLABELED and type[outer[v]] == ODD) )
slack[i] += e;
}
}
for(int i = n; i < 2*n; i++)
{
if(GREATER(dual[i], 0))
{
blocked[i] = true;
}
else if(active[i] and blocked[i])
{
//The blossom is becoming unblocked
if(mate[i] == -1)
{
DestroyBlossom(i);
}
else
{
blocked[i] = false;
Expand(i);
}
}
}
}
pair< list<int>, double> Matching::SolveMinimumCostPerfectMatching(const vector<double> & cost)
{
SolveMaximumMatching();
if(!perfect)
throw "Error: The graph does not have a perfect matching";
Clear();
//Initialize slacks (reduced costs for the edges)
slack = cost;
PositiveCosts();
//If the matching on the compressed graph is perfect, we are done
perfect = false;
while(not perfect)
{
//Run an heuristic maximum matching algorithm
Heuristic();
//Grow a hungarian forest
Grow();
UpdateDualCosts();
//Set up the algorithm for a new grow step
Reset();
}
list<int> matching = RetrieveMatching();
double obj = 0;
for(list<int>::iterator it = matching.begin(); it != matching.end(); it++)
obj += cost[*it];
double dualObj = 0;
for(int i = 0; i < 2*n; i++)
{
if(i < n) dualObj += dual[i];
else if(blocked[i]) dualObj += dual[i];
}
return pair< list<int>, double >(matching, obj);
}
void Matching::PositiveCosts()
{
double minEdge = 0;
for(int i = 0; i < m ;i++)
if(GREATER(minEdge - slack[i], 0))
minEdge = slack[i];
for(int i = 0; i < m; i++)
slack[i] -= minEdge;
}
list<int> Matching::SolveMaximumMatching()
{
Clear();
Grow();
return RetrieveMatching();
}
//Sets up the algorithm for a new run
void Matching::Clear()
{
ClearBlossomIndices();
for(int i = 0; i < 2*n; i++)
{
outer[i] = i;
deep[i].clear();
if(i<n)
deep[i].push_back(i);
shallow[i].clear();
if(i < n) active[i] = true;
else active[i] = false;
type[i] = 0;
forest[i] = -1;
root[i] = i;
blocked[i] = false;
dual[i] = 0;
mate[i] = -1;
tip[i] = i;
}
slack.assign(m, 0);
}
list<int> Matching::RetrieveMatching()
{
list<int> matching;
for(int i = 0; i < 2*n; i++)
if(active[i] and mate[i] != -1 and outer[i] == i)
Expand(i, true);
for(int i = 0; i < m; i++)
{
int u = G.GetEdge(i).first;
int v = G.GetEdge(i).second;
if(mate[u] == v)
matching.push_back(i);
}
return matching;
}