1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
4 | * |
---|
5 | * Copyright (C) 2003-2010 |
---|
6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
8 | * |
---|
9 | * Permission to use, modify and distribute this software is granted |
---|
10 | * provided that this copyright notice appears in all copies. For |
---|
11 | * precise terms see the accompanying LICENSE file. |
---|
12 | * |
---|
13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
14 | * express or implied, and with no claim as to its suitability for any |
---|
15 | * purpose. |
---|
16 | * |
---|
17 | */ |
---|
18 | |
---|
19 | #include <iostream> |
---|
20 | #include <sstream> |
---|
21 | #include <vector> |
---|
22 | #include <queue> |
---|
23 | #include <cstdlib> |
---|
24 | |
---|
25 | #include <lemon/matching.h> |
---|
26 | #include <lemon/smart_graph.h> |
---|
27 | #include <lemon/concepts/graph.h> |
---|
28 | #include <lemon/concepts/maps.h> |
---|
29 | #include <lemon/lgf_reader.h> |
---|
30 | #include <lemon/math.h> |
---|
31 | |
---|
32 | #include "test_tools.h" |
---|
33 | |
---|
34 | using namespace std; |
---|
35 | using namespace lemon; |
---|
36 | |
---|
37 | GRAPH_TYPEDEFS(SmartGraph); |
---|
38 | |
---|
39 | |
---|
40 | const int lgfn = 3; |
---|
41 | const std::string lgf[lgfn] = { |
---|
42 | "@nodes\n" |
---|
43 | "label\n" |
---|
44 | "0\n" |
---|
45 | "1\n" |
---|
46 | "2\n" |
---|
47 | "3\n" |
---|
48 | "4\n" |
---|
49 | "5\n" |
---|
50 | "6\n" |
---|
51 | "7\n" |
---|
52 | "@edges\n" |
---|
53 | " label weight\n" |
---|
54 | "7 4 0 984\n" |
---|
55 | "0 7 1 73\n" |
---|
56 | "7 1 2 204\n" |
---|
57 | "2 3 3 583\n" |
---|
58 | "2 7 4 565\n" |
---|
59 | "2 1 5 582\n" |
---|
60 | "0 4 6 551\n" |
---|
61 | "2 5 7 385\n" |
---|
62 | "1 5 8 561\n" |
---|
63 | "5 3 9 484\n" |
---|
64 | "7 5 10 904\n" |
---|
65 | "3 6 11 47\n" |
---|
66 | "7 6 12 888\n" |
---|
67 | "3 0 13 747\n" |
---|
68 | "6 1 14 310\n", |
---|
69 | |
---|
70 | "@nodes\n" |
---|
71 | "label\n" |
---|
72 | "0\n" |
---|
73 | "1\n" |
---|
74 | "2\n" |
---|
75 | "3\n" |
---|
76 | "4\n" |
---|
77 | "5\n" |
---|
78 | "6\n" |
---|
79 | "7\n" |
---|
80 | "@edges\n" |
---|
81 | " label weight\n" |
---|
82 | "2 5 0 710\n" |
---|
83 | "0 5 1 241\n" |
---|
84 | "2 4 2 856\n" |
---|
85 | "2 6 3 762\n" |
---|
86 | "4 1 4 747\n" |
---|
87 | "6 1 5 962\n" |
---|
88 | "4 7 6 723\n" |
---|
89 | "1 7 7 661\n" |
---|
90 | "2 3 8 376\n" |
---|
91 | "1 0 9 416\n" |
---|
92 | "6 7 10 391\n", |
---|
93 | |
---|
94 | "@nodes\n" |
---|
95 | "label\n" |
---|
96 | "0\n" |
---|
97 | "1\n" |
---|
98 | "2\n" |
---|
99 | "3\n" |
---|
100 | "4\n" |
---|
101 | "5\n" |
---|
102 | "6\n" |
---|
103 | "7\n" |
---|
104 | "@edges\n" |
---|
105 | " label weight\n" |
---|
106 | "6 2 0 553\n" |
---|
107 | "0 7 1 653\n" |
---|
108 | "6 3 2 22\n" |
---|
109 | "4 7 3 846\n" |
---|
110 | "7 2 4 981\n" |
---|
111 | "7 6 5 250\n" |
---|
112 | "5 2 6 539\n", |
---|
113 | }; |
---|
114 | |
---|
115 | void checkMaxMatchingCompile() |
---|
116 | { |
---|
117 | typedef concepts::Graph Graph; |
---|
118 | typedef Graph::Node Node; |
---|
119 | typedef Graph::Edge Edge; |
---|
120 | typedef Graph::EdgeMap<bool> MatMap; |
---|
121 | |
---|
122 | Graph g; |
---|
123 | Node n; |
---|
124 | Edge e; |
---|
125 | MatMap mat(g); |
---|
126 | |
---|
127 | MaxMatching<Graph> mat_test(g); |
---|
128 | const MaxMatching<Graph>& |
---|
129 | const_mat_test = mat_test; |
---|
130 | |
---|
131 | mat_test.init(); |
---|
132 | mat_test.greedyInit(); |
---|
133 | mat_test.matchingInit(mat); |
---|
134 | mat_test.startSparse(); |
---|
135 | mat_test.startDense(); |
---|
136 | mat_test.run(); |
---|
137 | |
---|
138 | const_mat_test.matchingSize(); |
---|
139 | const_mat_test.matching(e); |
---|
140 | const_mat_test.matching(n); |
---|
141 | const MaxMatching<Graph>::MatchingMap& mmap = |
---|
142 | const_mat_test.matchingMap(); |
---|
143 | e = mmap[n]; |
---|
144 | const_mat_test.mate(n); |
---|
145 | |
---|
146 | MaxMatching<Graph>::Status stat = |
---|
147 | const_mat_test.status(n); |
---|
148 | ::lemon::ignore_unused_variable_warning(stat); |
---|
149 | const MaxMatching<Graph>::StatusMap& smap = |
---|
150 | const_mat_test.statusMap(); |
---|
151 | stat = smap[n]; |
---|
152 | const_mat_test.barrier(n); |
---|
153 | } |
---|
154 | |
---|
155 | void checkMaxWeightedMatchingCompile() |
---|
156 | { |
---|
157 | typedef concepts::Graph Graph; |
---|
158 | typedef Graph::Node Node; |
---|
159 | typedef Graph::Edge Edge; |
---|
160 | typedef Graph::EdgeMap<int> WeightMap; |
---|
161 | |
---|
162 | Graph g; |
---|
163 | Node n; |
---|
164 | Edge e; |
---|
165 | WeightMap w(g); |
---|
166 | |
---|
167 | MaxWeightedMatching<Graph> mat_test(g, w); |
---|
168 | const MaxWeightedMatching<Graph>& |
---|
169 | const_mat_test = mat_test; |
---|
170 | |
---|
171 | mat_test.init(); |
---|
172 | mat_test.start(); |
---|
173 | mat_test.run(); |
---|
174 | |
---|
175 | const_mat_test.matchingWeight(); |
---|
176 | const_mat_test.matchingSize(); |
---|
177 | const_mat_test.matching(e); |
---|
178 | const_mat_test.matching(n); |
---|
179 | const MaxWeightedMatching<Graph>::MatchingMap& mmap = |
---|
180 | const_mat_test.matchingMap(); |
---|
181 | e = mmap[n]; |
---|
182 | const_mat_test.mate(n); |
---|
183 | |
---|
184 | int k = 0; |
---|
185 | const_mat_test.dualValue(); |
---|
186 | const_mat_test.nodeValue(n); |
---|
187 | const_mat_test.blossomNum(); |
---|
188 | const_mat_test.blossomSize(k); |
---|
189 | const_mat_test.blossomValue(k); |
---|
190 | } |
---|
191 | |
---|
192 | void checkMaxWeightedPerfectMatchingCompile() |
---|
193 | { |
---|
194 | typedef concepts::Graph Graph; |
---|
195 | typedef Graph::Node Node; |
---|
196 | typedef Graph::Edge Edge; |
---|
197 | typedef Graph::EdgeMap<int> WeightMap; |
---|
198 | |
---|
199 | Graph g; |
---|
200 | Node n; |
---|
201 | Edge e; |
---|
202 | WeightMap w(g); |
---|
203 | |
---|
204 | MaxWeightedPerfectMatching<Graph> mat_test(g, w); |
---|
205 | const MaxWeightedPerfectMatching<Graph>& |
---|
206 | const_mat_test = mat_test; |
---|
207 | |
---|
208 | mat_test.init(); |
---|
209 | mat_test.start(); |
---|
210 | mat_test.run(); |
---|
211 | |
---|
212 | const_mat_test.matchingWeight(); |
---|
213 | const_mat_test.matching(e); |
---|
214 | const_mat_test.matching(n); |
---|
215 | const MaxWeightedPerfectMatching<Graph>::MatchingMap& mmap = |
---|
216 | const_mat_test.matchingMap(); |
---|
217 | e = mmap[n]; |
---|
218 | const_mat_test.mate(n); |
---|
219 | |
---|
220 | int k = 0; |
---|
221 | const_mat_test.dualValue(); |
---|
222 | const_mat_test.nodeValue(n); |
---|
223 | const_mat_test.blossomNum(); |
---|
224 | const_mat_test.blossomSize(k); |
---|
225 | const_mat_test.blossomValue(k); |
---|
226 | } |
---|
227 | |
---|
228 | void checkMatching(const SmartGraph& graph, |
---|
229 | const MaxMatching<SmartGraph>& mm) { |
---|
230 | int num = 0; |
---|
231 | |
---|
232 | IntNodeMap comp_index(graph); |
---|
233 | UnionFind<IntNodeMap> comp(comp_index); |
---|
234 | |
---|
235 | int barrier_num = 0; |
---|
236 | |
---|
237 | for (NodeIt n(graph); n != INVALID; ++n) { |
---|
238 | check(mm.status(n) == MaxMatching<SmartGraph>::EVEN || |
---|
239 | mm.matching(n) != INVALID, "Wrong Gallai-Edmonds decomposition"); |
---|
240 | if (mm.status(n) == MaxMatching<SmartGraph>::ODD) { |
---|
241 | ++barrier_num; |
---|
242 | } else { |
---|
243 | comp.insert(n); |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | for (EdgeIt e(graph); e != INVALID; ++e) { |
---|
248 | if (mm.matching(e)) { |
---|
249 | check(e == mm.matching(graph.u(e)), "Wrong matching"); |
---|
250 | check(e == mm.matching(graph.v(e)), "Wrong matching"); |
---|
251 | ++num; |
---|
252 | } |
---|
253 | check(mm.status(graph.u(e)) != MaxMatching<SmartGraph>::EVEN || |
---|
254 | mm.status(graph.v(e)) != MaxMatching<SmartGraph>::MATCHED, |
---|
255 | "Wrong Gallai-Edmonds decomposition"); |
---|
256 | |
---|
257 | check(mm.status(graph.v(e)) != MaxMatching<SmartGraph>::EVEN || |
---|
258 | mm.status(graph.u(e)) != MaxMatching<SmartGraph>::MATCHED, |
---|
259 | "Wrong Gallai-Edmonds decomposition"); |
---|
260 | |
---|
261 | if (mm.status(graph.u(e)) != MaxMatching<SmartGraph>::ODD && |
---|
262 | mm.status(graph.v(e)) != MaxMatching<SmartGraph>::ODD) { |
---|
263 | comp.join(graph.u(e), graph.v(e)); |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | std::set<int> comp_root; |
---|
268 | int odd_comp_num = 0; |
---|
269 | for (NodeIt n(graph); n != INVALID; ++n) { |
---|
270 | if (mm.status(n) != MaxMatching<SmartGraph>::ODD) { |
---|
271 | int root = comp.find(n); |
---|
272 | if (comp_root.find(root) == comp_root.end()) { |
---|
273 | comp_root.insert(root); |
---|
274 | if (comp.size(n) % 2 == 1) { |
---|
275 | ++odd_comp_num; |
---|
276 | } |
---|
277 | } |
---|
278 | } |
---|
279 | } |
---|
280 | |
---|
281 | check(mm.matchingSize() == num, "Wrong matching"); |
---|
282 | check(2 * num == countNodes(graph) - (odd_comp_num - barrier_num), |
---|
283 | "Wrong matching"); |
---|
284 | return; |
---|
285 | } |
---|
286 | |
---|
287 | void checkWeightedMatching(const SmartGraph& graph, |
---|
288 | const SmartGraph::EdgeMap<int>& weight, |
---|
289 | const MaxWeightedMatching<SmartGraph>& mwm) { |
---|
290 | for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) { |
---|
291 | if (graph.u(e) == graph.v(e)) continue; |
---|
292 | int rw = mwm.nodeValue(graph.u(e)) + mwm.nodeValue(graph.v(e)); |
---|
293 | |
---|
294 | for (int i = 0; i < mwm.blossomNum(); ++i) { |
---|
295 | bool s = false, t = false; |
---|
296 | for (MaxWeightedMatching<SmartGraph>::BlossomIt n(mwm, i); |
---|
297 | n != INVALID; ++n) { |
---|
298 | if (graph.u(e) == n) s = true; |
---|
299 | if (graph.v(e) == n) t = true; |
---|
300 | } |
---|
301 | if (s == true && t == true) { |
---|
302 | rw += mwm.blossomValue(i); |
---|
303 | } |
---|
304 | } |
---|
305 | rw -= weight[e] * mwm.dualScale; |
---|
306 | |
---|
307 | check(rw >= 0, "Negative reduced weight"); |
---|
308 | check(rw == 0 || !mwm.matching(e), |
---|
309 | "Non-zero reduced weight on matching edge"); |
---|
310 | } |
---|
311 | |
---|
312 | int pv = 0; |
---|
313 | for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) { |
---|
314 | if (mwm.matching(n) != INVALID) { |
---|
315 | check(mwm.nodeValue(n) >= 0, "Invalid node value"); |
---|
316 | pv += weight[mwm.matching(n)]; |
---|
317 | SmartGraph::Node o = graph.target(mwm.matching(n)); |
---|
318 | check(mwm.mate(n) == o, "Invalid matching"); |
---|
319 | check(mwm.matching(n) == graph.oppositeArc(mwm.matching(o)), |
---|
320 | "Invalid matching"); |
---|
321 | } else { |
---|
322 | check(mwm.mate(n) == INVALID, "Invalid matching"); |
---|
323 | check(mwm.nodeValue(n) == 0, "Invalid matching"); |
---|
324 | } |
---|
325 | } |
---|
326 | |
---|
327 | int dv = 0; |
---|
328 | for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) { |
---|
329 | dv += mwm.nodeValue(n); |
---|
330 | } |
---|
331 | |
---|
332 | for (int i = 0; i < mwm.blossomNum(); ++i) { |
---|
333 | check(mwm.blossomValue(i) >= 0, "Invalid blossom value"); |
---|
334 | check(mwm.blossomSize(i) % 2 == 1, "Even blossom size"); |
---|
335 | dv += mwm.blossomValue(i) * ((mwm.blossomSize(i) - 1) / 2); |
---|
336 | } |
---|
337 | |
---|
338 | check(pv * mwm.dualScale == dv * 2, "Wrong duality"); |
---|
339 | |
---|
340 | return; |
---|
341 | } |
---|
342 | |
---|
343 | void checkWeightedPerfectMatching(const SmartGraph& graph, |
---|
344 | const SmartGraph::EdgeMap<int>& weight, |
---|
345 | const MaxWeightedPerfectMatching<SmartGraph>& mwpm) { |
---|
346 | for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) { |
---|
347 | if (graph.u(e) == graph.v(e)) continue; |
---|
348 | int rw = mwpm.nodeValue(graph.u(e)) + mwpm.nodeValue(graph.v(e)); |
---|
349 | |
---|
350 | for (int i = 0; i < mwpm.blossomNum(); ++i) { |
---|
351 | bool s = false, t = false; |
---|
352 | for (MaxWeightedPerfectMatching<SmartGraph>::BlossomIt n(mwpm, i); |
---|
353 | n != INVALID; ++n) { |
---|
354 | if (graph.u(e) == n) s = true; |
---|
355 | if (graph.v(e) == n) t = true; |
---|
356 | } |
---|
357 | if (s == true && t == true) { |
---|
358 | rw += mwpm.blossomValue(i); |
---|
359 | } |
---|
360 | } |
---|
361 | rw -= weight[e] * mwpm.dualScale; |
---|
362 | |
---|
363 | check(rw >= 0, "Negative reduced weight"); |
---|
364 | check(rw == 0 || !mwpm.matching(e), |
---|
365 | "Non-zero reduced weight on matching edge"); |
---|
366 | } |
---|
367 | |
---|
368 | int pv = 0; |
---|
369 | for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) { |
---|
370 | check(mwpm.matching(n) != INVALID, "Non perfect"); |
---|
371 | pv += weight[mwpm.matching(n)]; |
---|
372 | SmartGraph::Node o = graph.target(mwpm.matching(n)); |
---|
373 | check(mwpm.mate(n) == o, "Invalid matching"); |
---|
374 | check(mwpm.matching(n) == graph.oppositeArc(mwpm.matching(o)), |
---|
375 | "Invalid matching"); |
---|
376 | } |
---|
377 | |
---|
378 | int dv = 0; |
---|
379 | for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) { |
---|
380 | dv += mwpm.nodeValue(n); |
---|
381 | } |
---|
382 | |
---|
383 | for (int i = 0; i < mwpm.blossomNum(); ++i) { |
---|
384 | check(mwpm.blossomValue(i) >= 0, "Invalid blossom value"); |
---|
385 | check(mwpm.blossomSize(i) % 2 == 1, "Even blossom size"); |
---|
386 | dv += mwpm.blossomValue(i) * ((mwpm.blossomSize(i) - 1) / 2); |
---|
387 | } |
---|
388 | |
---|
389 | check(pv * mwpm.dualScale == dv * 2, "Wrong duality"); |
---|
390 | |
---|
391 | return; |
---|
392 | } |
---|
393 | |
---|
394 | |
---|
395 | int main() { |
---|
396 | |
---|
397 | for (int i = 0; i < lgfn; ++i) { |
---|
398 | SmartGraph graph; |
---|
399 | SmartGraph::EdgeMap<int> weight(graph); |
---|
400 | |
---|
401 | istringstream lgfs(lgf[i]); |
---|
402 | graphReader(graph, lgfs). |
---|
403 | edgeMap("weight", weight).run(); |
---|
404 | |
---|
405 | bool perfect; |
---|
406 | { |
---|
407 | MaxMatching<SmartGraph> mm(graph); |
---|
408 | mm.run(); |
---|
409 | checkMatching(graph, mm); |
---|
410 | perfect = 2 * mm.matchingSize() == countNodes(graph); |
---|
411 | } |
---|
412 | |
---|
413 | { |
---|
414 | MaxWeightedMatching<SmartGraph> mwm(graph, weight); |
---|
415 | mwm.run(); |
---|
416 | checkWeightedMatching(graph, weight, mwm); |
---|
417 | } |
---|
418 | |
---|
419 | { |
---|
420 | MaxWeightedMatching<SmartGraph> mwm(graph, weight); |
---|
421 | mwm.init(); |
---|
422 | mwm.start(); |
---|
423 | checkWeightedMatching(graph, weight, mwm); |
---|
424 | } |
---|
425 | |
---|
426 | { |
---|
427 | MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight); |
---|
428 | bool result = mwpm.run(); |
---|
429 | |
---|
430 | check(result == perfect, "Perfect matching found"); |
---|
431 | if (perfect) { |
---|
432 | checkWeightedPerfectMatching(graph, weight, mwpm); |
---|
433 | } |
---|
434 | } |
---|
435 | |
---|
436 | { |
---|
437 | MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight); |
---|
438 | mwpm.init(); |
---|
439 | bool result = mwpm.start(); |
---|
440 | |
---|
441 | check(result == perfect, "Perfect matching found"); |
---|
442 | if (perfect) { |
---|
443 | checkWeightedPerfectMatching(graph, weight, mwpm); |
---|
444 | } |
---|
445 | } |
---|
446 | } |
---|
447 | |
---|
448 | return 0; |
---|
449 | } |
---|