test/min_cost_arborescence_test.cc
author Balazs Dezso <deba@google.com>
Fri, 22 Jan 2021 10:55:32 +0100
changeset 1208 c6aa2cc1af04
parent 1084 8b2d4e5d96e4
permissions -rw-r--r--
Factor out recursion from weighted matching algorithms (#638)
deba@501
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@501
     2
 *
deba@501
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@501
     4
 *
alpar@1092
     5
 * Copyright (C) 2003-2013
deba@501
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@501
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@501
     8
 *
deba@501
     9
 * Permission to use, modify and distribute this software is granted
deba@501
    10
 * provided that this copyright notice appears in all copies. For
deba@501
    11
 * precise terms see the accompanying LICENSE file.
deba@501
    12
 *
deba@501
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@501
    14
 * express or implied, and with no claim as to its suitability for any
deba@501
    15
 * purpose.
deba@501
    16
 *
deba@501
    17
 */
deba@501
    18
deba@501
    19
#include <iostream>
deba@501
    20
#include <set>
deba@501
    21
#include <vector>
deba@501
    22
#include <iterator>
deba@501
    23
deba@501
    24
#include <lemon/smart_graph.h>
deba@501
    25
#include <lemon/min_cost_arborescence.h>
deba@501
    26
#include <lemon/lgf_reader.h>
kpeter@625
    27
#include <lemon/concepts/digraph.h>
deba@501
    28
deba@501
    29
#include "test_tools.h"
deba@501
    30
deba@501
    31
using namespace lemon;
deba@501
    32
using namespace std;
deba@501
    33
deba@501
    34
const char test_lgf[] =
deba@501
    35
  "@nodes\n"
deba@501
    36
  "label\n"
deba@501
    37
  "0\n"
deba@501
    38
  "1\n"
deba@501
    39
  "2\n"
deba@501
    40
  "3\n"
deba@501
    41
  "4\n"
deba@501
    42
  "5\n"
deba@501
    43
  "6\n"
deba@501
    44
  "7\n"
deba@501
    45
  "8\n"
deba@501
    46
  "9\n"
deba@501
    47
  "@arcs\n"
deba@501
    48
  "     label  cost\n"
deba@501
    49
  "1 8  0      107\n"
deba@501
    50
  "0 3  1      70\n"
deba@501
    51
  "2 1  2      46\n"
deba@501
    52
  "4 1  3      28\n"
deba@501
    53
  "4 4  4      91\n"
deba@501
    54
  "3 9  5      76\n"
deba@501
    55
  "9 8  6      61\n"
deba@501
    56
  "8 1  7      39\n"
deba@501
    57
  "9 8  8      74\n"
deba@501
    58
  "8 0  9      39\n"
deba@501
    59
  "4 3  10     45\n"
deba@501
    60
  "2 2  11     34\n"
deba@501
    61
  "0 1  12     100\n"
deba@501
    62
  "6 3  13     95\n"
deba@501
    63
  "4 1  14     22\n"
deba@501
    64
  "1 1  15     31\n"
deba@501
    65
  "7 2  16     51\n"
deba@501
    66
  "2 6  17     29\n"
deba@501
    67
  "8 3  18     115\n"
deba@501
    68
  "6 9  19     32\n"
deba@501
    69
  "1 1  20     60\n"
deba@501
    70
  "0 3  21     40\n"
deba@501
    71
  "@attributes\n"
deba@501
    72
  "source 0\n";
deba@501
    73
kpeter@625
    74
kpeter@625
    75
void checkMinCostArborescenceCompile()
kpeter@625
    76
{
kpeter@625
    77
  typedef double VType;
kpeter@625
    78
  typedef concepts::Digraph Digraph;
kpeter@625
    79
  typedef concepts::ReadMap<Digraph::Arc, VType> CostMap;
kpeter@625
    80
  typedef Digraph::Node Node;
kpeter@625
    81
  typedef Digraph::Arc Arc;
kpeter@625
    82
  typedef concepts::WriteMap<Digraph::Arc, bool> ArbMap;
kpeter@625
    83
  typedef concepts::ReadWriteMap<Digraph::Node, Digraph::Arc> PredMap;
kpeter@625
    84
kpeter@625
    85
  typedef MinCostArborescence<Digraph, CostMap>::
kpeter@625
    86
            SetArborescenceMap<ArbMap>::
kpeter@625
    87
            SetPredMap<PredMap>::Create MinCostArbType;
kpeter@625
    88
kpeter@625
    89
  Digraph g;
kpeter@625
    90
  Node s, n;
kpeter@625
    91
  Arc e;
kpeter@625
    92
  VType c;
kpeter@625
    93
  bool b;
alpar@1083
    94
  ::lemon::ignore_unused_variable_warning(c,b);
kpeter@625
    95
  int i;
kpeter@625
    96
  CostMap cost;
kpeter@625
    97
  ArbMap arb;
kpeter@625
    98
  PredMap pred;
kpeter@625
    99
kpeter@625
   100
  MinCostArbType mcarb_test(g, cost);
kpeter@625
   101
  const MinCostArbType& const_mcarb_test = mcarb_test;
kpeter@625
   102
kpeter@625
   103
  mcarb_test
kpeter@625
   104
    .arborescenceMap(arb)
kpeter@625
   105
    .predMap(pred)
kpeter@625
   106
    .run(s);
kpeter@625
   107
kpeter@625
   108
  mcarb_test.init();
kpeter@625
   109
  mcarb_test.addSource(s);
kpeter@625
   110
  mcarb_test.start();
kpeter@625
   111
  n = mcarb_test.processNextNode();
kpeter@625
   112
  b = const_mcarb_test.emptyQueue();
kpeter@625
   113
  i = const_mcarb_test.queueSize();
alpar@877
   114
kpeter@625
   115
  c = const_mcarb_test.arborescenceCost();
kpeter@625
   116
  b = const_mcarb_test.arborescence(e);
kpeter@625
   117
  e = const_mcarb_test.pred(n);
kpeter@625
   118
  const MinCostArbType::ArborescenceMap &am =
kpeter@625
   119
    const_mcarb_test.arborescenceMap();
kpeter@625
   120
  const MinCostArbType::PredMap &pm =
kpeter@625
   121
    const_mcarb_test.predMap();
kpeter@625
   122
  b = const_mcarb_test.reached(n);
kpeter@625
   123
  b = const_mcarb_test.processed(n);
alpar@877
   124
kpeter@625
   125
  i = const_mcarb_test.dualNum();
kpeter@625
   126
  c = const_mcarb_test.dualValue();
kpeter@625
   127
  i = const_mcarb_test.dualSize(i);
kpeter@625
   128
  c = const_mcarb_test.dualValue(i);
alpar@877
   129
alpar@1083
   130
  ::lemon::ignore_unused_variable_warning(am);
alpar@1083
   131
  ::lemon::ignore_unused_variable_warning(pm);
kpeter@625
   132
}
kpeter@625
   133
deba@501
   134
int main() {
deba@501
   135
  typedef SmartDigraph Digraph;
deba@501
   136
  DIGRAPH_TYPEDEFS(Digraph);
deba@501
   137
deba@501
   138
  typedef Digraph::ArcMap<double> CostMap;
deba@501
   139
deba@501
   140
  Digraph digraph;
deba@501
   141
  CostMap cost(digraph);
deba@501
   142
  Node source;
deba@501
   143
deba@501
   144
  std::istringstream is(test_lgf);
deba@501
   145
  digraphReader(digraph, is).
deba@501
   146
    arcMap("cost", cost).
deba@501
   147
    node("source", source).run();
deba@501
   148
deba@501
   149
  MinCostArborescence<Digraph, CostMap> mca(digraph, cost);
deba@501
   150
  mca.run(source);
deba@501
   151
deba@501
   152
  vector<pair<double, set<Node> > > dualSolution(mca.dualNum());
deba@501
   153
deba@501
   154
  for (int i = 0; i < mca.dualNum(); ++i) {
deba@501
   155
    dualSolution[i].first = mca.dualValue(i);
deba@501
   156
    for (MinCostArborescence<Digraph, CostMap>::DualIt it(mca, i);
deba@501
   157
         it != INVALID; ++it) {
deba@501
   158
      dualSolution[i].second.insert(it);
deba@501
   159
    }
deba@501
   160
  }
deba@501
   161
deba@501
   162
  for (ArcIt it(digraph); it != INVALID; ++it) {
deba@501
   163
    if (mca.reached(digraph.source(it))) {
deba@501
   164
      double sum = 0.0;
deba@501
   165
      for (int i = 0; i < int(dualSolution.size()); ++i) {
deba@501
   166
        if (dualSolution[i].second.find(digraph.target(it))
deba@501
   167
            != dualSolution[i].second.end() &&
deba@501
   168
            dualSolution[i].second.find(digraph.source(it))
deba@501
   169
            == dualSolution[i].second.end()) {
deba@501
   170
          sum += dualSolution[i].first;
deba@501
   171
        }
deba@501
   172
      }
deba@501
   173
      if (mca.arborescence(it)) {
kpeter@625
   174
        check(sum == cost[it], "Invalid dual solution");
deba@501
   175
      }
kpeter@625
   176
      check(sum <= cost[it], "Invalid dual solution");
deba@501
   177
    }
deba@501
   178
  }
deba@501
   179
deba@501
   180
kpeter@625
   181
  check(mca.dualValue() == mca.arborescenceCost(), "Invalid dual solution");
deba@501
   182
kpeter@625
   183
  check(mca.reached(source), "Invalid arborescence");
deba@501
   184
  for (ArcIt a(digraph); a != INVALID; ++a) {
deba@501
   185
    check(!mca.reached(digraph.source(a)) ||
kpeter@625
   186
          mca.reached(digraph.target(a)), "Invalid arborescence");
deba@501
   187
  }
deba@501
   188
deba@501
   189
  for (NodeIt n(digraph); n != INVALID; ++n) {
deba@501
   190
    if (!mca.reached(n)) continue;
deba@501
   191
    int cnt = 0;
deba@501
   192
    for (InArcIt a(digraph, n); a != INVALID; ++a) {
deba@501
   193
      if (mca.arborescence(a)) {
kpeter@625
   194
        check(mca.pred(n) == a, "Invalid arborescence");
deba@501
   195
        ++cnt;
deba@501
   196
      }
deba@501
   197
    }
kpeter@625
   198
    check((n == source ? cnt == 0 : cnt == 1), "Invalid arborescence");
deba@501
   199
  }
deba@501
   200
deba@501
   201
  Digraph::ArcMap<bool> arborescence(digraph);
kpeter@625
   202
  check(mca.arborescenceCost() ==
deba@501
   203
        minCostArborescence(digraph, cost, source, arborescence),
kpeter@625
   204
        "Wrong result of the function interface");
deba@501
   205
deba@501
   206
  return 0;
deba@501
   207
}