COIN-OR::LEMON - Graph Library

source: lemon/test/max_matching_test.cc @ 637:b61354458b59

Last change on this file since 637:b61354458b59 was 637:b61354458b59, checked in by Peter Kovacs <kpeter@…>, 15 years ago

Imporvements for the matching algorithms (#264)

File size: 10.2 KB
Line 
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-2009
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/max_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
34using namespace std;
35using namespace lemon;
36
37GRAPH_TYPEDEFS(SmartGraph);
38
39
40const int lgfn = 3;
41const 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
115void 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_mat_test.mate(n);
142
143  MaxMatching<Graph>::Status stat =
144    const_mat_test.decomposition(n);
145  const_mat_test.barrier(n);
146 
147  ignore_unused_variable_warning(stat);
148}
149
150void checkMaxWeightedMatchingCompile()
151{
152  typedef concepts::Graph Graph;
153  typedef Graph::Node Node;
154  typedef Graph::Edge Edge;
155  typedef Graph::EdgeMap<int> WeightMap;
156
157  Graph g;
158  Node n;
159  Edge e;
160  WeightMap w(g);
161
162  MaxWeightedMatching<Graph> mat_test(g, w);
163  const MaxWeightedMatching<Graph>&
164    const_mat_test = mat_test;
165
166  mat_test.init();
167  mat_test.start();
168  mat_test.run();
169 
170  const_mat_test.matchingValue();
171  const_mat_test.matchingSize();
172  const_mat_test.matching(e);
173  const_mat_test.matching(n);
174  const_mat_test.mate(n);
175 
176  int k = 0;
177  const_mat_test.dualValue();
178  const_mat_test.nodeValue(n);
179  const_mat_test.blossomNum();
180  const_mat_test.blossomSize(k);
181  const_mat_test.blossomValue(k);
182}
183
184void checkMaxWeightedPerfectMatchingCompile()
185{
186  typedef concepts::Graph Graph;
187  typedef Graph::Node Node;
188  typedef Graph::Edge Edge;
189  typedef Graph::EdgeMap<int> WeightMap;
190
191  Graph g;
192  Node n;
193  Edge e;
194  WeightMap w(g);
195
196  MaxWeightedPerfectMatching<Graph> mat_test(g, w);
197  const MaxWeightedPerfectMatching<Graph>&
198    const_mat_test = mat_test;
199
200  mat_test.init();
201  mat_test.start();
202  mat_test.run();
203 
204  const_mat_test.matchingValue();
205  const_mat_test.matching(e);
206  const_mat_test.matching(n);
207  const_mat_test.mate(n);
208 
209  int k = 0;
210  const_mat_test.dualValue();
211  const_mat_test.nodeValue(n);
212  const_mat_test.blossomNum();
213  const_mat_test.blossomSize(k);
214  const_mat_test.blossomValue(k);
215}
216
217void checkMatching(const SmartGraph& graph,
218                   const MaxMatching<SmartGraph>& mm) {
219  int num = 0;
220
221  IntNodeMap comp_index(graph);
222  UnionFind<IntNodeMap> comp(comp_index);
223
224  int barrier_num = 0;
225
226  for (NodeIt n(graph); n != INVALID; ++n) {
227    check(mm.decomposition(n) == MaxMatching<SmartGraph>::EVEN ||
228          mm.matching(n) != INVALID, "Wrong Gallai-Edmonds decomposition");
229    if (mm.decomposition(n) == MaxMatching<SmartGraph>::ODD) {
230      ++barrier_num;
231    } else {
232      comp.insert(n);
233    }
234  }
235
236  for (EdgeIt e(graph); e != INVALID; ++e) {
237    if (mm.matching(e)) {
238      check(e == mm.matching(graph.u(e)), "Wrong matching");
239      check(e == mm.matching(graph.v(e)), "Wrong matching");
240      ++num;
241    }
242    check(mm.decomposition(graph.u(e)) != MaxMatching<SmartGraph>::EVEN ||
243          mm.decomposition(graph.v(e)) != MaxMatching<SmartGraph>::MATCHED,
244          "Wrong Gallai-Edmonds decomposition");
245
246    check(mm.decomposition(graph.v(e)) != MaxMatching<SmartGraph>::EVEN ||
247          mm.decomposition(graph.u(e)) != MaxMatching<SmartGraph>::MATCHED,
248          "Wrong Gallai-Edmonds decomposition");
249
250    if (mm.decomposition(graph.u(e)) != MaxMatching<SmartGraph>::ODD &&
251        mm.decomposition(graph.v(e)) != MaxMatching<SmartGraph>::ODD) {
252      comp.join(graph.u(e), graph.v(e));
253    }
254  }
255
256  std::set<int> comp_root;
257  int odd_comp_num = 0;
258  for (NodeIt n(graph); n != INVALID; ++n) {
259    if (mm.decomposition(n) != MaxMatching<SmartGraph>::ODD) {
260      int root = comp.find(n);
261      if (comp_root.find(root) == comp_root.end()) {
262        comp_root.insert(root);
263        if (comp.size(n) % 2 == 1) {
264          ++odd_comp_num;
265        }
266      }
267    }
268  }
269
270  check(mm.matchingSize() == num, "Wrong matching");
271  check(2 * num == countNodes(graph) - (odd_comp_num - barrier_num),
272         "Wrong matching");
273  return;
274}
275
276void checkWeightedMatching(const SmartGraph& graph,
277                   const SmartGraph::EdgeMap<int>& weight,
278                   const MaxWeightedMatching<SmartGraph>& mwm) {
279  for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
280    if (graph.u(e) == graph.v(e)) continue;
281    int rw = mwm.nodeValue(graph.u(e)) + mwm.nodeValue(graph.v(e));
282
283    for (int i = 0; i < mwm.blossomNum(); ++i) {
284      bool s = false, t = false;
285      for (MaxWeightedMatching<SmartGraph>::BlossomIt n(mwm, i);
286           n != INVALID; ++n) {
287        if (graph.u(e) == n) s = true;
288        if (graph.v(e) == n) t = true;
289      }
290      if (s == true && t == true) {
291        rw += mwm.blossomValue(i);
292      }
293    }
294    rw -= weight[e] * mwm.dualScale;
295
296    check(rw >= 0, "Negative reduced weight");
297    check(rw == 0 || !mwm.matching(e),
298          "Non-zero reduced weight on matching edge");
299  }
300
301  int pv = 0;
302  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
303    if (mwm.matching(n) != INVALID) {
304      check(mwm.nodeValue(n) >= 0, "Invalid node value");
305      pv += weight[mwm.matching(n)];
306      SmartGraph::Node o = graph.target(mwm.matching(n));
307      check(mwm.mate(n) == o, "Invalid matching");
308      check(mwm.matching(n) == graph.oppositeArc(mwm.matching(o)),
309            "Invalid matching");
310    } else {
311      check(mwm.mate(n) == INVALID, "Invalid matching");
312      check(mwm.nodeValue(n) == 0, "Invalid matching");
313    }
314  }
315
316  int dv = 0;
317  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
318    dv += mwm.nodeValue(n);
319  }
320
321  for (int i = 0; i < mwm.blossomNum(); ++i) {
322    check(mwm.blossomValue(i) >= 0, "Invalid blossom value");
323    check(mwm.blossomSize(i) % 2 == 1, "Even blossom size");
324    dv += mwm.blossomValue(i) * ((mwm.blossomSize(i) - 1) / 2);
325  }
326
327  check(pv * mwm.dualScale == dv * 2, "Wrong duality");
328
329  return;
330}
331
332void checkWeightedPerfectMatching(const SmartGraph& graph,
333                          const SmartGraph::EdgeMap<int>& weight,
334                          const MaxWeightedPerfectMatching<SmartGraph>& mwpm) {
335  for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
336    if (graph.u(e) == graph.v(e)) continue;
337    int rw = mwpm.nodeValue(graph.u(e)) + mwpm.nodeValue(graph.v(e));
338
339    for (int i = 0; i < mwpm.blossomNum(); ++i) {
340      bool s = false, t = false;
341      for (MaxWeightedPerfectMatching<SmartGraph>::BlossomIt n(mwpm, i);
342           n != INVALID; ++n) {
343        if (graph.u(e) == n) s = true;
344        if (graph.v(e) == n) t = true;
345      }
346      if (s == true && t == true) {
347        rw += mwpm.blossomValue(i);
348      }
349    }
350    rw -= weight[e] * mwpm.dualScale;
351
352    check(rw >= 0, "Negative reduced weight");
353    check(rw == 0 || !mwpm.matching(e),
354          "Non-zero reduced weight on matching edge");
355  }
356
357  int pv = 0;
358  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
359    check(mwpm.matching(n) != INVALID, "Non perfect");
360    pv += weight[mwpm.matching(n)];
361    SmartGraph::Node o = graph.target(mwpm.matching(n));
362    check(mwpm.mate(n) == o, "Invalid matching");
363    check(mwpm.matching(n) == graph.oppositeArc(mwpm.matching(o)),
364          "Invalid matching");
365  }
366
367  int dv = 0;
368  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
369    dv += mwpm.nodeValue(n);
370  }
371
372  for (int i = 0; i < mwpm.blossomNum(); ++i) {
373    check(mwpm.blossomValue(i) >= 0, "Invalid blossom value");
374    check(mwpm.blossomSize(i) % 2 == 1, "Even blossom size");
375    dv += mwpm.blossomValue(i) * ((mwpm.blossomSize(i) - 1) / 2);
376  }
377
378  check(pv * mwpm.dualScale == dv * 2, "Wrong duality");
379
380  return;
381}
382
383
384int main() {
385
386  for (int i = 0; i < lgfn; ++i) {
387    SmartGraph graph;
388    SmartGraph::EdgeMap<int> weight(graph);
389
390    istringstream lgfs(lgf[i]);
391    graphReader(graph, lgfs).
392      edgeMap("weight", weight).run();
393
394    MaxMatching<SmartGraph> mm(graph);
395    mm.run();
396    checkMatching(graph, mm);
397
398    MaxWeightedMatching<SmartGraph> mwm(graph, weight);
399    mwm.run();
400    checkWeightedMatching(graph, weight, mwm);
401
402    MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight);
403    bool perfect = mwpm.run();
404
405    check(perfect == (mm.matchingSize() * 2 == countNodes(graph)),
406          "Perfect matching found");
407
408    if (perfect) {
409      checkWeightedPerfectMatching(graph, weight, mwpm);
410    }
411  }
412
413  return 0;
414}
Note: See TracBrowser for help on using the repository browser.