[326] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
| 4 | * |
---|
[877] | 5 | * Copyright (C) 2003-2010 |
---|
[326] | 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> |
---|
[327] | 20 | #include <sstream> |
---|
[326] | 21 | #include <vector> |
---|
| 22 | #include <queue> |
---|
| 23 | #include <cstdlib> |
---|
| 24 | |
---|
[594] | 25 | #include <lemon/matching.h> |
---|
[327] | 26 | #include <lemon/smart_graph.h> |
---|
[590] | 27 | #include <lemon/concepts/graph.h> |
---|
| 28 | #include <lemon/concepts/maps.h> |
---|
[327] | 29 | #include <lemon/lgf_reader.h> |
---|
[590] | 30 | #include <lemon/math.h> |
---|
[327] | 31 | |
---|
[326] | 32 | #include "test_tools.h" |
---|
| 33 | |
---|
| 34 | using namespace std; |
---|
| 35 | using namespace lemon; |
---|
| 36 | |
---|
[327] | 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 | |
---|
[590] | 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(); |
---|
[877] | 137 | |
---|
[590] | 138 | const_mat_test.matchingSize(); |
---|
| 139 | const_mat_test.matching(e); |
---|
| 140 | const_mat_test.matching(n); |
---|
[593] | 141 | const MaxMatching<Graph>::MatchingMap& mmap = |
---|
| 142 | const_mat_test.matchingMap(); |
---|
| 143 | e = mmap[n]; |
---|
[590] | 144 | const_mat_test.mate(n); |
---|
| 145 | |
---|
[877] | 146 | MaxMatching<Graph>::Status stat = |
---|
[593] | 147 | const_mat_test.status(n); |
---|
[1083] | 148 | ::lemon::ignore_unused_variable_warning(stat); |
---|
[593] | 149 | const MaxMatching<Graph>::StatusMap& smap = |
---|
| 150 | const_mat_test.statusMap(); |
---|
| 151 | stat = smap[n]; |
---|
[590] | 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(); |
---|
[877] | 174 | |
---|
[593] | 175 | const_mat_test.matchingWeight(); |
---|
[590] | 176 | const_mat_test.matchingSize(); |
---|
| 177 | const_mat_test.matching(e); |
---|
| 178 | const_mat_test.matching(n); |
---|
[593] | 179 | const MaxWeightedMatching<Graph>::MatchingMap& mmap = |
---|
| 180 | const_mat_test.matchingMap(); |
---|
| 181 | e = mmap[n]; |
---|
[590] | 182 | const_mat_test.mate(n); |
---|
[877] | 183 | |
---|
[590] | 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(); |
---|
[877] | 211 | |
---|
[593] | 212 | const_mat_test.matchingWeight(); |
---|
[590] | 213 | const_mat_test.matching(e); |
---|
| 214 | const_mat_test.matching(n); |
---|
[593] | 215 | const MaxWeightedPerfectMatching<Graph>::MatchingMap& mmap = |
---|
| 216 | const_mat_test.matchingMap(); |
---|
| 217 | e = mmap[n]; |
---|
[590] | 218 | const_mat_test.mate(n); |
---|
[877] | 219 | |
---|
[590] | 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 | |
---|
[327] | 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) { |
---|
[593] | 238 | check(mm.status(n) == MaxMatching<SmartGraph>::EVEN || |
---|
[327] | 239 | mm.matching(n) != INVALID, "Wrong Gallai-Edmonds decomposition"); |
---|
[593] | 240 | if (mm.status(n) == MaxMatching<SmartGraph>::ODD) { |
---|
[327] | 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 | } |
---|
[593] | 253 | check(mm.status(graph.u(e)) != MaxMatching<SmartGraph>::EVEN || |
---|
| 254 | mm.status(graph.v(e)) != MaxMatching<SmartGraph>::MATCHED, |
---|
[327] | 255 | "Wrong Gallai-Edmonds decomposition"); |
---|
| 256 | |
---|
[593] | 257 | check(mm.status(graph.v(e)) != MaxMatching<SmartGraph>::EVEN || |
---|
| 258 | mm.status(graph.u(e)) != MaxMatching<SmartGraph>::MATCHED, |
---|
[327] | 259 | "Wrong Gallai-Edmonds decomposition"); |
---|
| 260 | |
---|
[593] | 261 | if (mm.status(graph.u(e)) != MaxMatching<SmartGraph>::ODD && |
---|
| 262 | mm.status(graph.v(e)) != MaxMatching<SmartGraph>::ODD) { |
---|
[327] | 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) { |
---|
[593] | 270 | if (mm.status(n) != MaxMatching<SmartGraph>::ODD) { |
---|
[327] | 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 | |
---|
[326] | 395 | int main() { |
---|
| 396 | |
---|
[327] | 397 | for (int i = 0; i < lgfn; ++i) { |
---|
| 398 | SmartGraph graph; |
---|
| 399 | SmartGraph::EdgeMap<int> weight(graph); |
---|
[326] | 400 | |
---|
[327] | 401 | istringstream lgfs(lgf[i]); |
---|
| 402 | graphReader(graph, lgfs). |
---|
| 403 | edgeMap("weight", weight).run(); |
---|
[326] | 404 | |
---|
[870] | 405 | bool perfect; |
---|
| 406 | { |
---|
| 407 | MaxMatching<SmartGraph> mm(graph); |
---|
| 408 | mm.run(); |
---|
| 409 | checkMatching(graph, mm); |
---|
| 410 | perfect = 2 * mm.matchingSize() == countNodes(graph); |
---|
| 411 | } |
---|
[326] | 412 | |
---|
[870] | 413 | { |
---|
| 414 | MaxWeightedMatching<SmartGraph> mwm(graph, weight); |
---|
| 415 | mwm.run(); |
---|
| 416 | checkWeightedMatching(graph, weight, mwm); |
---|
| 417 | } |
---|
[326] | 418 | |
---|
[870] | 419 | { |
---|
| 420 | MaxWeightedMatching<SmartGraph> mwm(graph, weight); |
---|
| 421 | mwm.init(); |
---|
| 422 | mwm.start(); |
---|
| 423 | checkWeightedMatching(graph, weight, mwm); |
---|
| 424 | } |
---|
[326] | 425 | |
---|
[870] | 426 | { |
---|
| 427 | MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight); |
---|
| 428 | bool result = mwpm.run(); |
---|
[326] | 429 | |
---|
[870] | 430 | check(result == perfect, "Perfect matching found"); |
---|
| 431 | if (perfect) { |
---|
| 432 | checkWeightedPerfectMatching(graph, weight, mwpm); |
---|
| 433 | } |
---|
| 434 | } |
---|
[594] | 435 | |
---|
[870] | 436 | { |
---|
| 437 | MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight); |
---|
| 438 | mwpm.init(); |
---|
| 439 | bool result = mwpm.start(); |
---|
[877] | 440 | |
---|
[870] | 441 | check(result == perfect, "Perfect matching found"); |
---|
| 442 | if (perfect) { |
---|
| 443 | checkWeightedPerfectMatching(graph, weight, mwpm); |
---|
| 444 | } |
---|
[326] | 445 | } |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | return 0; |
---|
| 449 | } |
---|