COIN-OR::LEMON - Graph Library

source: lemon-main/test/min_cost_arborescence_test.cc @ 1044:15d7c5eadaca

Last change on this file since 1044:15d7c5eadaca was 1008:d216e1c8b3fa, checked in by Alpar Juttner <alpar@…>, 12 years ago

Merge #453 to branches >=1.2

File size: 5.2 KB
RevLine 
[501]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
[501]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 <set>
21#include <vector>
22#include <iterator>
23
24#include <lemon/smart_graph.h>
25#include <lemon/min_cost_arborescence.h>
26#include <lemon/lgf_reader.h>
[625]27#include <lemon/concepts/digraph.h>
[501]28
29#include "test_tools.h"
30
31using namespace lemon;
32using namespace std;
33
34const char test_lgf[] =
35  "@nodes\n"
36  "label\n"
37  "0\n"
38  "1\n"
39  "2\n"
40  "3\n"
41  "4\n"
42  "5\n"
43  "6\n"
44  "7\n"
45  "8\n"
46  "9\n"
47  "@arcs\n"
48  "     label  cost\n"
49  "1 8  0      107\n"
50  "0 3  1      70\n"
51  "2 1  2      46\n"
52  "4 1  3      28\n"
53  "4 4  4      91\n"
54  "3 9  5      76\n"
55  "9 8  6      61\n"
56  "8 1  7      39\n"
57  "9 8  8      74\n"
58  "8 0  9      39\n"
59  "4 3  10     45\n"
60  "2 2  11     34\n"
61  "0 1  12     100\n"
62  "6 3  13     95\n"
63  "4 1  14     22\n"
64  "1 1  15     31\n"
65  "7 2  16     51\n"
66  "2 6  17     29\n"
67  "8 3  18     115\n"
68  "6 9  19     32\n"
69  "1 1  20     60\n"
70  "0 3  21     40\n"
71  "@attributes\n"
72  "source 0\n";
73
[625]74
75void checkMinCostArborescenceCompile()
76{
77  typedef double VType;
78  typedef concepts::Digraph Digraph;
79  typedef concepts::ReadMap<Digraph::Arc, VType> CostMap;
80  typedef Digraph::Node Node;
81  typedef Digraph::Arc Arc;
82  typedef concepts::WriteMap<Digraph::Arc, bool> ArbMap;
83  typedef concepts::ReadWriteMap<Digraph::Node, Digraph::Arc> PredMap;
84
85  typedef MinCostArborescence<Digraph, CostMap>::
86            SetArborescenceMap<ArbMap>::
87            SetPredMap<PredMap>::Create MinCostArbType;
88
89  Digraph g;
90  Node s, n;
91  Arc e;
92  VType c;
93  bool b;
[1007]94  ignore_unused_variable_warning(c,b);
[625]95  int i;
96  CostMap cost;
97  ArbMap arb;
98  PredMap pred;
99
100  MinCostArbType mcarb_test(g, cost);
101  const MinCostArbType& const_mcarb_test = mcarb_test;
102
103  mcarb_test
104    .arborescenceMap(arb)
105    .predMap(pred)
106    .run(s);
107
108  mcarb_test.init();
109  mcarb_test.addSource(s);
110  mcarb_test.start();
111  n = mcarb_test.processNextNode();
112  b = const_mcarb_test.emptyQueue();
113  i = const_mcarb_test.queueSize();
[877]114
[625]115  c = const_mcarb_test.arborescenceCost();
116  b = const_mcarb_test.arborescence(e);
117  e = const_mcarb_test.pred(n);
118  const MinCostArbType::ArborescenceMap &am =
119    const_mcarb_test.arborescenceMap();
120  const MinCostArbType::PredMap &pm =
121    const_mcarb_test.predMap();
122  b = const_mcarb_test.reached(n);
123  b = const_mcarb_test.processed(n);
[877]124
[625]125  i = const_mcarb_test.dualNum();
126  c = const_mcarb_test.dualValue();
127  i = const_mcarb_test.dualSize(i);
128  c = const_mcarb_test.dualValue(i);
[877]129
[625]130  ignore_unused_variable_warning(am);
131  ignore_unused_variable_warning(pm);
132}
133
[501]134int main() {
135  typedef SmartDigraph Digraph;
136  DIGRAPH_TYPEDEFS(Digraph);
137
138  typedef Digraph::ArcMap<double> CostMap;
139
140  Digraph digraph;
141  CostMap cost(digraph);
142  Node source;
143
144  std::istringstream is(test_lgf);
145  digraphReader(digraph, is).
146    arcMap("cost", cost).
147    node("source", source).run();
148
149  MinCostArborescence<Digraph, CostMap> mca(digraph, cost);
150  mca.run(source);
151
152  vector<pair<double, set<Node> > > dualSolution(mca.dualNum());
153
154  for (int i = 0; i < mca.dualNum(); ++i) {
155    dualSolution[i].first = mca.dualValue(i);
156    for (MinCostArborescence<Digraph, CostMap>::DualIt it(mca, i);
157         it != INVALID; ++it) {
158      dualSolution[i].second.insert(it);
159    }
160  }
161
162  for (ArcIt it(digraph); it != INVALID; ++it) {
163    if (mca.reached(digraph.source(it))) {
164      double sum = 0.0;
165      for (int i = 0; i < int(dualSolution.size()); ++i) {
166        if (dualSolution[i].second.find(digraph.target(it))
167            != dualSolution[i].second.end() &&
168            dualSolution[i].second.find(digraph.source(it))
169            == dualSolution[i].second.end()) {
170          sum += dualSolution[i].first;
171        }
172      }
173      if (mca.arborescence(it)) {
[625]174        check(sum == cost[it], "Invalid dual solution");
[501]175      }
[625]176      check(sum <= cost[it], "Invalid dual solution");
[501]177    }
178  }
179
180
[625]181  check(mca.dualValue() == mca.arborescenceCost(), "Invalid dual solution");
[501]182
[625]183  check(mca.reached(source), "Invalid arborescence");
[501]184  for (ArcIt a(digraph); a != INVALID; ++a) {
185    check(!mca.reached(digraph.source(a)) ||
[625]186          mca.reached(digraph.target(a)), "Invalid arborescence");
[501]187  }
188
189  for (NodeIt n(digraph); n != INVALID; ++n) {
190    if (!mca.reached(n)) continue;
191    int cnt = 0;
192    for (InArcIt a(digraph, n); a != INVALID; ++a) {
193      if (mca.arborescence(a)) {
[625]194        check(mca.pred(n) == a, "Invalid arborescence");
[501]195        ++cnt;
196      }
197    }
[625]198    check((n == source ? cnt == 0 : cnt == 1), "Invalid arborescence");
[501]199  }
200
201  Digraph::ArcMap<bool> arborescence(digraph);
[625]202  check(mca.arborescenceCost() ==
[501]203        minCostArborescence(digraph, cost, source, arborescence),
[625]204        "Wrong result of the function interface");
[501]205
206  return 0;
207}
Note: See TracBrowser for help on using the repository browser.