test/min_cost_arborescence_test.cc
changeset 512 7f8560cb9d65
child 617 029a48052c67
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/min_cost_arborescence_test.cc	Tue Dec 02 23:33:47 2008 +0100
     1.3 @@ -0,0 +1,146 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#include <iostream>
    1.23 +#include <set>
    1.24 +#include <vector>
    1.25 +#include <iterator>
    1.26 +
    1.27 +#include <lemon/smart_graph.h>
    1.28 +#include <lemon/min_cost_arborescence.h>
    1.29 +#include <lemon/lgf_reader.h>
    1.30 +
    1.31 +#include "test_tools.h"
    1.32 +
    1.33 +using namespace lemon;
    1.34 +using namespace std;
    1.35 +
    1.36 +const char test_lgf[] =
    1.37 +  "@nodes\n"
    1.38 +  "label\n"
    1.39 +  "0\n"
    1.40 +  "1\n"
    1.41 +  "2\n"
    1.42 +  "3\n"
    1.43 +  "4\n"
    1.44 +  "5\n"
    1.45 +  "6\n"
    1.46 +  "7\n"
    1.47 +  "8\n"
    1.48 +  "9\n"
    1.49 +  "@arcs\n"
    1.50 +  "     label  cost\n"
    1.51 +  "1 8  0      107\n"
    1.52 +  "0 3  1      70\n"
    1.53 +  "2 1  2      46\n"
    1.54 +  "4 1  3      28\n"
    1.55 +  "4 4  4      91\n"
    1.56 +  "3 9  5      76\n"
    1.57 +  "9 8  6      61\n"
    1.58 +  "8 1  7      39\n"
    1.59 +  "9 8  8      74\n"
    1.60 +  "8 0  9      39\n"
    1.61 +  "4 3  10     45\n"
    1.62 +  "2 2  11     34\n"
    1.63 +  "0 1  12     100\n"
    1.64 +  "6 3  13     95\n"
    1.65 +  "4 1  14     22\n"
    1.66 +  "1 1  15     31\n"
    1.67 +  "7 2  16     51\n"
    1.68 +  "2 6  17     29\n"
    1.69 +  "8 3  18     115\n"
    1.70 +  "6 9  19     32\n"
    1.71 +  "1 1  20     60\n"
    1.72 +  "0 3  21     40\n"
    1.73 +  "@attributes\n"
    1.74 +  "source 0\n";
    1.75 +
    1.76 +int main() {
    1.77 +  typedef SmartDigraph Digraph;
    1.78 +  DIGRAPH_TYPEDEFS(Digraph);
    1.79 +
    1.80 +  typedef Digraph::ArcMap<double> CostMap;
    1.81 +
    1.82 +  Digraph digraph;
    1.83 +  CostMap cost(digraph);
    1.84 +  Node source;
    1.85 +
    1.86 +  std::istringstream is(test_lgf);
    1.87 +  digraphReader(digraph, is).
    1.88 +    arcMap("cost", cost).
    1.89 +    node("source", source).run();
    1.90 +
    1.91 +  MinCostArborescence<Digraph, CostMap> mca(digraph, cost);
    1.92 +  mca.run(source);
    1.93 +
    1.94 +  vector<pair<double, set<Node> > > dualSolution(mca.dualNum());
    1.95 +
    1.96 +  for (int i = 0; i < mca.dualNum(); ++i) {
    1.97 +    dualSolution[i].first = mca.dualValue(i);
    1.98 +    for (MinCostArborescence<Digraph, CostMap>::DualIt it(mca, i);
    1.99 +         it != INVALID; ++it) {
   1.100 +      dualSolution[i].second.insert(it);
   1.101 +    }
   1.102 +  }
   1.103 +
   1.104 +  for (ArcIt it(digraph); it != INVALID; ++it) {
   1.105 +    if (mca.reached(digraph.source(it))) {
   1.106 +      double sum = 0.0;
   1.107 +      for (int i = 0; i < int(dualSolution.size()); ++i) {
   1.108 +        if (dualSolution[i].second.find(digraph.target(it))
   1.109 +            != dualSolution[i].second.end() &&
   1.110 +            dualSolution[i].second.find(digraph.source(it))
   1.111 +            == dualSolution[i].second.end()) {
   1.112 +          sum += dualSolution[i].first;
   1.113 +        }
   1.114 +      }
   1.115 +      if (mca.arborescence(it)) {
   1.116 +        check(sum == cost[it], "INVALID DUAL");
   1.117 +      }
   1.118 +      check(sum <= cost[it], "INVALID DUAL");
   1.119 +    }
   1.120 +  }
   1.121 +
   1.122 +
   1.123 +  check(mca.dualValue() == mca.arborescenceValue(), "INVALID DUAL");
   1.124 +
   1.125 +  check(mca.reached(source), "INVALID ARBORESCENCE");
   1.126 +  for (ArcIt a(digraph); a != INVALID; ++a) {
   1.127 +    check(!mca.reached(digraph.source(a)) ||
   1.128 +          mca.reached(digraph.target(a)), "INVALID ARBORESCENCE");
   1.129 +  }
   1.130 +
   1.131 +  for (NodeIt n(digraph); n != INVALID; ++n) {
   1.132 +    if (!mca.reached(n)) continue;
   1.133 +    int cnt = 0;
   1.134 +    for (InArcIt a(digraph, n); a != INVALID; ++a) {
   1.135 +      if (mca.arborescence(a)) {
   1.136 +        check(mca.pred(n) == a, "INVALID ARBORESCENCE");
   1.137 +        ++cnt;
   1.138 +      }
   1.139 +    }
   1.140 +    check((n == source ? cnt == 0 : cnt == 1), "INVALID ARBORESCENCE");
   1.141 +  }
   1.142 +
   1.143 +  Digraph::ArcMap<bool> arborescence(digraph);
   1.144 +  check(mca.arborescenceValue() ==
   1.145 +        minCostArborescence(digraph, cost, source, arborescence),
   1.146 +        "WRONG FUNCTION");
   1.147 +
   1.148 +  return 0;
   1.149 +}