test/bipartite_matching_test.cc
author ladanyi
Thu, 22 Mar 2007 06:36:50 +0000
changeset 2412 086fc76d591d
parent 2386 81b47fc5c444
child 2462 7a096a6bf53a
permissions -rw-r--r--
Append the svn revision number to the version string automatically.
alpar@2391
     1
/* -*- C++ -*-
alpar@2391
     2
 *
alpar@2391
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@2391
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
alpar@2391
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@2391
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@2391
     8
 *
alpar@2391
     9
 * Permission to use, modify and distribute this software is granted
alpar@2391
    10
 * provided that this copyright notice appears in all copies. For
alpar@2391
    11
 * precise terms see the accompanying LICENSE file.
alpar@2391
    12
 *
alpar@2391
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@2391
    14
 * express or implied, and with no claim as to its suitability for any
alpar@2391
    15
 * purpose.
alpar@2391
    16
 *
alpar@2391
    17
 */
alpar@2391
    18
deba@2040
    19
#include <cstdlib>
deba@2040
    20
#include <iostream>
deba@2040
    21
#include <sstream>
deba@2040
    22
deba@2137
    23
#include <lemon/smart_graph.h>
deba@2040
    24
deba@2040
    25
#include <lemon/bpugraph_adaptor.h>
deba@2040
    26
#include <lemon/bipartite_matching.h>
deba@2040
    27
deba@2040
    28
#include <lemon/graph_utils.h>
deba@2040
    29
deba@2051
    30
#include <lemon/maps.h>
deba@2051
    31
deba@2040
    32
#include "test_tools.h"
deba@2040
    33
deba@2040
    34
using namespace std;
deba@2040
    35
using namespace lemon;
deba@2040
    36
deba@2137
    37
typedef SmartBpUGraph Graph;
deba@2040
    38
BPUGRAPH_TYPEDEFS(Graph);
deba@2040
    39
deba@2386
    40
const int N = 10;
deba@2386
    41
const int M = 10;
deba@2386
    42
const int E = 52;
deba@2386
    43
const int C = 100;
deba@2137
    44
deba@2386
    45
const int sa[E] = { 6, 5, 6, 4, 1, 0, 9, 5, 2, 4, 4, 3, 5,
deba@2137
    46
                    2, 3, 8, 3, 4, 9, 6, 9, 4, 3, 1, 5, 8,
deba@2137
    47
                    4, 8, 9, 2, 2, 3, 0, 5, 2, 3, 6, 3, 8,
deba@2137
    48
                    8, 4, 0, 9, 9, 6, 2, 1, 2, 7, 1, 9, 4};
deba@2137
    49
deba@2386
    50
const int ta[E] = { 2, 7, 4, 8, 6, 3, 4, 1, 7, 7, 0, 1, 6,
deba@2137
    51
                    3, 2, 6, 8, 3, 5, 6, 3, 1, 8, 7, 2, 0,
deba@2137
    52
                    6, 9, 6, 7, 8, 3, 3, 4, 5, 8, 6, 4, 1,
deba@2137
    53
                    4, 3, 3, 8, 7, 7, 3, 7, 7, 3, 5, 1, 6};
deba@2137
    54
deba@2386
    55
const int wa[E] = { 3, 99, 85, 16, 79, 52, 83, 99, 62, 6, 42, 6, 95,
deba@2137
    56
                    13, 34, 9, 5, 38, 39, 75, 99, 12, 73, 35, 93, 43,
deba@2137
    57
                    54, 91, 45, 26, 77, 47, 11, 22, 50, 74, 37, 64, 91,
deba@2137
    58
                    60, 6, 92, 29, 46, 34, 84, 67, 34, 45, 0, 39, 47};
deba@2137
    59
deba@2137
    60
deba@2137
    61
int main() {
deba@2040
    62
  Graph graph;
deba@2040
    63
  vector<Node> aNodes;
deba@2040
    64
  vector<Node> bNodes;
deba@2051
    65
deba@2051
    66
  Graph::UEdgeMap<int> weight(graph);
deba@2051
    67
deba@2051
    68
  int max_cardinality;
deba@2051
    69
  int max_weight;
deba@2051
    70
  int max_cardinality_max_weight;
deba@2058
    71
  int min_cost_matching;
deba@2040
    72
deba@2386
    73
  for (int i = 0; i < N; ++i) {
deba@2040
    74
    Node node = graph.addANode();
deba@2040
    75
    aNodes.push_back(node);
deba@2040
    76
  }
deba@2386
    77
  for (int i = 0; i < M; ++i) {
deba@2040
    78
    Node node = graph.addBNode();
deba@2040
    79
    bNodes.push_back(node);
deba@2040
    80
  }
deba@2386
    81
  for (int i = 0; i < E; ++i) {
deba@2137
    82
    Node aNode = aNodes[sa[i]];
deba@2137
    83
    Node bNode = bNodes[ta[i]];
deba@2051
    84
    UEdge uedge = graph.addEdge(aNode, bNode);
deba@2137
    85
    weight[uedge] = wa[i];
deba@2040
    86
  }
deba@2040
    87
deba@2137
    88
deba@2040
    89
  {
deba@2040
    90
    MaxBipartiteMatching<Graph> bpmatch(graph);
deba@2040
    91
deba@2040
    92
    bpmatch.run();
deba@2040
    93
deba@2040
    94
    Graph::UEdgeMap<bool> mm(graph);
deba@2040
    95
    Graph::NodeMap<bool> cs(graph);
deba@2040
    96
    
deba@2040
    97
    check(bpmatch.coverSet(cs) == bpmatch.matching(mm), "INVALID PRIMAL-DUAL");
deba@2040
    98
    
deba@2040
    99
    for (UEdgeIt it(graph); it != INVALID; ++it) {
deba@2040
   100
      check(cs[graph.aNode(it)] || cs[graph.bNode(it)], "INVALID DUAL");
deba@2040
   101
    }
deba@2040
   102
    
deba@2040
   103
    for (ANodeIt it(graph); it != INVALID; ++it) {
deba@2040
   104
      int num = 0;
deba@2040
   105
      for (IncEdgeIt jt(graph, it); jt != INVALID; ++jt) {
deba@2040
   106
        if (mm[jt]) ++num;
deba@2051
   107
      }
deba@2040
   108
      check(num <= 1, "INVALID PRIMAL");
deba@2040
   109
    }
deba@2051
   110
    max_cardinality = bpmatch.matchingSize();
deba@2040
   111
  }
deba@2040
   112
deba@2040
   113
  {
deba@2058
   114
    Graph::UEdgeMap<bool> mm(graph);
deba@2058
   115
deba@2058
   116
    check(max_cardinality == maxBipartiteMatching(graph, mm),
deba@2058
   117
          "WRONG MATCHING");
deba@2058
   118
    
deba@2058
   119
    for (ANodeIt it(graph); it != INVALID; ++it) {
deba@2058
   120
      int num = 0;
deba@2058
   121
      for (IncEdgeIt jt(graph, it); jt != INVALID; ++jt) {
deba@2058
   122
        if (mm[jt]) ++num;
deba@2058
   123
      }
deba@2058
   124
      check(num <= 1, "INVALID PRIMAL");
deba@2058
   125
    }
deba@2058
   126
  }
deba@2058
   127
deba@2058
   128
  {
deba@2040
   129
    MaxBipartiteMatching<Graph> bpmatch(graph);
deba@2040
   130
deba@2040
   131
    bpmatch.greedyInit();
deba@2040
   132
    bpmatch.start();
deba@2040
   133
deba@2040
   134
    Graph::UEdgeMap<bool> mm(graph);
deba@2040
   135
    Graph::NodeMap<bool> cs(graph);
deba@2040
   136
deba@2040
   137
    check(bpmatch.coverSet(cs) == bpmatch.matching(mm), "INVALID PRIMAL-DUAL");
deba@2040
   138
    
deba@2040
   139
    for (UEdgeIt it(graph); it != INVALID; ++it) {
deba@2040
   140
      check(cs[graph.aNode(it)] || cs[graph.bNode(it)], "INVALID DUAL");
deba@2040
   141
    }
deba@2040
   142
    
deba@2040
   143
    for (ANodeIt it(graph); it != INVALID; ++it) {
deba@2040
   144
      int num = 0;
deba@2040
   145
      for (IncEdgeIt jt(graph, it); jt != INVALID; ++jt) {
deba@2040
   146
        if (mm[jt]) ++num;
deba@2040
   147
    }
deba@2040
   148
      check(num <= 1, "INVALID PRIMAL");
deba@2040
   149
    }
deba@2040
   150
  }
deba@2040
   151
deba@2040
   152
  {
deba@2040
   153
    MaxBipartiteMatching<Graph> bpmatch(graph);
deba@2040
   154
deba@2040
   155
    bpmatch.greedyInit();
deba@2040
   156
    while (bpmatch.simpleAugment());
deba@2040
   157
    
deba@2040
   158
    Graph::UEdgeMap<bool> mm(graph);
deba@2040
   159
    Graph::NodeMap<bool> cs(graph);
deba@2040
   160
    
deba@2040
   161
    check(bpmatch.coverSet(cs) == bpmatch.matching(mm), "INVALID PRIMAL-DUAL");
deba@2040
   162
    
deba@2040
   163
    for (UEdgeIt it(graph); it != INVALID; ++it) {
deba@2040
   164
      check(cs[graph.aNode(it)] || cs[graph.bNode(it)], "INVALID DUAL");
deba@2040
   165
    }
deba@2040
   166
    
deba@2040
   167
    for (ANodeIt it(graph); it != INVALID; ++it) {
deba@2040
   168
      int num = 0;
deba@2040
   169
      for (IncEdgeIt jt(graph, it); jt != INVALID; ++jt) {
deba@2040
   170
        if (mm[jt]) ++num;
deba@2040
   171
    }
deba@2040
   172
      check(num <= 1, "INVALID PRIMAL");
deba@2040
   173
    }
deba@2040
   174
  }
deba@2040
   175
deba@2040
   176
  {
deba@2051
   177
    MaxWeightedBipartiteMatching<Graph> bpmatch(graph, weight);
deba@2051
   178
deba@2051
   179
    bpmatch.init();
deba@2051
   180
deba@2051
   181
    max_weight = 0;
deba@2051
   182
    while (bpmatch.augment(true)) {
deba@2051
   183
    
deba@2051
   184
      Graph::UEdgeMap<bool> mm(graph);
deba@2051
   185
      Graph::NodeMap<int> pm(graph);
deba@2051
   186
      
deba@2051
   187
      bpmatch.matching(mm);
deba@2051
   188
      bpmatch.potential(pm);
deba@2051
   189
      
deba@2051
   190
      for (UEdgeIt it(graph); it != INVALID; ++it) {
deba@2051
   191
        if (mm[it]) {
deba@2058
   192
          check(pm[graph.aNode(it)] + pm[graph.bNode(it)] - weight[it] == 0,
deba@2051
   193
                "INVALID DUAL");
deba@2051
   194
        } else {
deba@2058
   195
          check(pm[graph.aNode(it)] + pm[graph.bNode(it)] - weight[it] >= 0,
deba@2051
   196
                "INVALID DUAL");
deba@2051
   197
        }
deba@2051
   198
      }
deba@2051
   199
deba@2051
   200
      for (ANodeIt it(graph); it != INVALID; ++it) {
deba@2051
   201
        int num = 0;
deba@2051
   202
        for (IncEdgeIt jt(graph, it); jt != INVALID; ++jt) {
deba@2051
   203
          if (mm[jt]) ++num;
deba@2051
   204
        }
deba@2051
   205
        check(num <= 1, "INVALID PRIMAL");
deba@2051
   206
      }
deba@2051
   207
      if (bpmatch.matchingValue() > max_weight) {
deba@2051
   208
        max_weight = bpmatch.matchingValue();
deba@2051
   209
      }
deba@2051
   210
    }
deba@2051
   211
  }
deba@2051
   212
deba@2051
   213
  {
deba@2058
   214
    Graph::UEdgeMap<bool> mm(graph);
deba@2058
   215
deba@2058
   216
    check(max_weight == maxWeightedBipartiteMatching(graph, weight, mm),
deba@2058
   217
          "WRONG MATCHING");
deba@2058
   218
    
deba@2058
   219
    for (ANodeIt it(graph); it != INVALID; ++it) {
deba@2058
   220
      int num = 0;
deba@2058
   221
      for (IncEdgeIt jt(graph, it); jt != INVALID; ++jt) {
deba@2058
   222
        if (mm[jt]) ++num;
deba@2058
   223
      }
deba@2058
   224
      check(num <= 1, "INVALID PRIMAL");
deba@2058
   225
    }
deba@2058
   226
  }
deba@2058
   227
deba@2058
   228
  {
deba@2051
   229
    MaxWeightedBipartiteMatching<Graph> bpmatch(graph, weight);
deba@2040
   230
deba@2040
   231
    bpmatch.run();
deba@2051
   232
    
deba@2051
   233
    Graph::UEdgeMap<bool> mm(graph);
deba@2051
   234
    Graph::NodeMap<int> pm(graph);
deba@2040
   235
deba@2051
   236
    bpmatch.matching(mm);
deba@2051
   237
    bpmatch.potential(pm);
deba@2040
   238
    
deba@2040
   239
    for (UEdgeIt it(graph); it != INVALID; ++it) {
deba@2051
   240
      if (mm[it]) {
deba@2058
   241
        check(pm[graph.aNode(it)] + pm[graph.bNode(it)] - weight[it] == 0,
deba@2051
   242
              "INVALID DUAL");
deba@2051
   243
      } else {
deba@2058
   244
        check(pm[graph.aNode(it)] + pm[graph.bNode(it)] - weight[it] >= 0,
deba@2051
   245
              "INVALID DUAL");
deba@2051
   246
      }
deba@2040
   247
    }
deba@2051
   248
deba@2040
   249
    for (ANodeIt it(graph); it != INVALID; ++it) {
deba@2040
   250
      int num = 0;
deba@2040
   251
      for (IncEdgeIt jt(graph, it); jt != INVALID; ++jt) {
deba@2040
   252
        if (mm[jt]) ++num;
deba@2040
   253
    }
deba@2040
   254
      check(num <= 1, "INVALID PRIMAL");
deba@2040
   255
    }
deba@2051
   256
    check(max_weight == bpmatch.matchingValue(), "WRONG WEIGHT");
deba@2051
   257
  }
deba@2051
   258
deba@2051
   259
  {
deba@2051
   260
    MaxWeightedBipartiteMatching<Graph> bpmatch(graph, weight);
deba@2051
   261
deba@2051
   262
    bpmatch.run(true);
deba@2051
   263
    
deba@2051
   264
    Graph::UEdgeMap<bool> mm(graph);
deba@2051
   265
    Graph::NodeMap<int> pm(graph);
deba@2051
   266
deba@2051
   267
    bpmatch.matching(mm);
deba@2051
   268
    bpmatch.potential(pm);
deba@2051
   269
    
deba@2051
   270
    for (UEdgeIt it(graph); it != INVALID; ++it) {
deba@2051
   271
      if (mm[it]) {
deba@2058
   272
        check(pm[graph.aNode(it)] + pm[graph.bNode(it)] - weight[it] == 0,
deba@2051
   273
              "INVALID DUAL");
deba@2051
   274
      } else {
deba@2058
   275
        check(pm[graph.aNode(it)] + pm[graph.bNode(it)] - weight[it] >= 0,
deba@2051
   276
              "INVALID DUAL");
deba@2051
   277
      }
deba@2051
   278
    }
deba@2051
   279
deba@2051
   280
    for (ANodeIt it(graph); it != INVALID; ++it) {
deba@2051
   281
      int num = 0;
deba@2051
   282
      for (IncEdgeIt jt(graph, it); jt != INVALID; ++jt) {
deba@2051
   283
        if (mm[jt]) ++num;
deba@2051
   284
    }
deba@2051
   285
      check(num <= 1, "INVALID PRIMAL");
deba@2051
   286
    }
deba@2051
   287
    check(max_cardinality == bpmatch.matchingSize(), "WRONG SIZE");
deba@2051
   288
    max_cardinality_max_weight = bpmatch.matchingValue();
deba@2051
   289
deba@2051
   290
  }
deba@2051
   291
deba@2051
   292
  {
deba@2058
   293
    Graph::UEdgeMap<bool> mm(graph);
deba@2051
   294
deba@2058
   295
    check(max_cardinality_max_weight == 
deba@2058
   296
          maxWeightedMaxBipartiteMatching(graph, weight, mm),
deba@2058
   297
          "WRONG MATCHING");
deba@2058
   298
    
deba@2058
   299
    for (ANodeIt it(graph); it != INVALID; ++it) {
deba@2058
   300
      int num = 0;
deba@2058
   301
      for (IncEdgeIt jt(graph, it); jt != INVALID; ++jt) {
deba@2058
   302
        if (mm[jt]) ++num;
deba@2058
   303
      }
deba@2058
   304
      check(num <= 1, "INVALID PRIMAL");
deba@2058
   305
    }
deba@2058
   306
  }
deba@2058
   307
deba@2058
   308
  Graph::UEdgeMap<int> cost(graph);
deba@2386
   309
  cost = subMap(constMap<UEdge>(C), weight);
deba@2058
   310
  {
deba@2051
   311
deba@2051
   312
    MinCostMaxBipartiteMatching<Graph> bpmatch(graph, cost);
deba@2051
   313
deba@2051
   314
    bpmatch.run();
deba@2051
   315
    
deba@2051
   316
    Graph::UEdgeMap<bool> mm(graph);
deba@2051
   317
    Graph::NodeMap<int> pm(graph);
deba@2051
   318
deba@2051
   319
    bpmatch.matching(mm);
deba@2051
   320
    bpmatch.potential(pm);
deba@2051
   321
    
deba@2137
   322
    min_cost_matching = bpmatch.matchingCost();
deba@2137
   323
    check(max_cardinality == bpmatch.matchingSize(), "WRONG SIZE");
deba@2386
   324
    check(max_cardinality * C - max_cardinality_max_weight 
deba@2137
   325
          == bpmatch.matchingCost(), "WRONG SIZE");
deba@2137
   326
deba@2051
   327
    for (UEdgeIt it(graph); it != INVALID; ++it) {
deba@2051
   328
      if (mm[it]) {
deba@2051
   329
        check(pm[graph.aNode(it)] + cost[it] - pm[graph.bNode(it)] == 0,
deba@2051
   330
              "INVALID DUAL");
deba@2051
   331
      } else {
deba@2051
   332
        check(pm[graph.aNode(it)] + cost[it] - pm[graph.bNode(it)] >= 0,
deba@2051
   333
              "INVALID DUAL");
deba@2051
   334
      }
deba@2051
   335
    }
deba@2051
   336
deba@2051
   337
    for (ANodeIt it(graph); it != INVALID; ++it) {
deba@2051
   338
      int num = 0;
deba@2051
   339
      for (IncEdgeIt jt(graph, it); jt != INVALID; ++jt) {
deba@2051
   340
        if (mm[jt]) ++num;
deba@2051
   341
    }
deba@2051
   342
      check(num <= 1, "INVALID PRIMAL");
deba@2051
   343
    }
deba@2051
   344
deba@2058
   345
    min_cost_matching = bpmatch.matchingCost();
deba@2051
   346
    check(max_cardinality == bpmatch.matchingSize(), "WRONG SIZE");
deba@2386
   347
    check(max_cardinality * C - max_cardinality_max_weight 
deba@2051
   348
          == bpmatch.matchingCost(), "WRONG SIZE");
deba@2051
   349
deba@2040
   350
  }
deba@2040
   351
deba@2058
   352
  {
deba@2058
   353
    Graph::UEdgeMap<bool> mm(graph);
deba@2058
   354
deba@2058
   355
    check(min_cost_matching == 
deba@2058
   356
          minCostMaxBipartiteMatching(graph, cost, mm),
deba@2058
   357
          "WRONG MATCHING");
deba@2058
   358
    
deba@2058
   359
    for (ANodeIt it(graph); it != INVALID; ++it) {
deba@2058
   360
      int num = 0;
deba@2058
   361
      for (IncEdgeIt jt(graph, it); jt != INVALID; ++jt) {
deba@2058
   362
        if (mm[jt]) ++num;
deba@2058
   363
      }
deba@2058
   364
      check(num <= 1, "INVALID PRIMAL");
deba@2058
   365
    }
deba@2058
   366
  }
deba@2058
   367
deba@2040
   368
  return 0;
deba@2040
   369
}