test/arborescence_test.cc
author alpar
Tue, 18 Jul 2006 15:14:56 +0000
changeset 2152 ba87d27667cd
child 2180 d5544c9409e4
permissions -rw-r--r--
Bugfix
deba@2025
     1
#include <iostream>
deba@2025
     2
#include <set>
deba@2025
     3
#include <vector>
deba@2025
     4
#include <iterator>
deba@2025
     5
deba@2025
     6
#include <cmath>
deba@2025
     7
#include <cstdlib>
deba@2025
     8
deba@2025
     9
#include <lemon/smart_graph.h>
deba@2025
    10
#include <lemon/min_cost_arborescence.h>
deba@2025
    11
deba@2025
    12
#include <lemon/graph_utils.h>
deba@2025
    13
#include <lemon/time_measure.h>
deba@2025
    14
deba@2025
    15
#include <lemon/tolerance.h>
deba@2025
    16
deba@2025
    17
using namespace lemon;
deba@2025
    18
using namespace std;
deba@2025
    19
deba@2025
    20
int main(int argc, const char *argv[]) {
deba@2025
    21
  srand(time(0));
deba@2025
    22
  typedef SmartGraph Graph;
deba@2025
    23
  GRAPH_TYPEDEFS(Graph);
deba@2025
    24
deba@2025
    25
  typedef Graph::EdgeMap<double> CostMap;
deba@2025
    26
deba@2025
    27
  const int n = argc > 1 ? atoi(argv[1]) : 100;
deba@2025
    28
  const int e = argc > 2 ? atoi(argv[2]) : (int)(n * log(n));
deba@2025
    29
deba@2025
    30
  Graph graph;
deba@2025
    31
  CostMap cost(graph);
deba@2025
    32
  vector<Node> nodes;
deba@2025
    33
  
deba@2025
    34
  for (int i = 0; i < n; ++i) {
deba@2025
    35
    nodes.push_back(graph.addNode());
deba@2025
    36
  }
deba@2025
    37
deba@2025
    38
  for (int i = 0; i < e; ++i) {
deba@2025
    39
    int s = (int)(n * (double)rand() / (RAND_MAX + 1.0));
deba@2025
    40
    int t = (int)(n * (double)rand() / (RAND_MAX + 1.0));
deba@2025
    41
    double c = rand() / (1.0 + RAND_MAX) * 100.0 + 20.0;
deba@2025
    42
    Edge edge = graph.addEdge(nodes[s], nodes[t]);
deba@2025
    43
    cost[edge] = c;
deba@2025
    44
  }
deba@2025
    45
deba@2025
    46
  Node source = nodes[(int)(n * (double)rand() / (RAND_MAX + 1.0))];
deba@2025
    47
deba@2025
    48
  MinCostArborescence<Graph, CostMap> mca(graph, cost);
deba@2025
    49
  mca.run(source);
deba@2025
    50
deba@2025
    51
  vector<pair<double, set<Node> > > dualSolution(mca.dualSize());
deba@2025
    52
deba@2025
    53
  for (int i = 0; i < mca.dualSize(); ++i) {
deba@2025
    54
    dualSolution[i].first = mca.dualValue(i);
deba@2025
    55
    for (MinCostArborescence<Graph, CostMap>::DualIt it(mca, i); 
deba@2025
    56
         it != INVALID; ++it) {
deba@2025
    57
      dualSolution[i].second.insert(it);
deba@2025
    58
    }
deba@2025
    59
  }
deba@2025
    60
deba@2025
    61
  Tolerance<double> tolerance;
deba@2025
    62
deba@2025
    63
  for (EdgeIt it(graph); it != INVALID; ++it) {
deba@2025
    64
    if (mca.reached(graph.source(it))) {
deba@2025
    65
      double sum = 0.0;
deba@2025
    66
      for (int i = 0; i < (int)dualSolution.size(); ++i) {
deba@2025
    67
        if (dualSolution[i].second.find(graph.target(it)) 
deba@2025
    68
            != dualSolution[i].second.end() &&
deba@2025
    69
            dualSolution[i].second.find(graph.source(it)) 
deba@2025
    70
            == dualSolution[i].second.end()) {
deba@2025
    71
          sum += dualSolution[i].first;
deba@2025
    72
        }
deba@2025
    73
      }
deba@2025
    74
      if (mca.arborescence(it)) {
deba@2025
    75
        LEMON_ASSERT(!tolerance.less(sum, cost[it]), "INVALID DUAL");
deba@2025
    76
      }
deba@2025
    77
      LEMON_ASSERT(!tolerance.less(cost[it], sum), "INVALID DUAL");
deba@2025
    78
    }
deba@2025
    79
  }
deba@2025
    80
deba@2025
    81
deba@2025
    82
  LEMON_ASSERT(!tolerance.different(mca.dualValue(), mca.arborescenceValue()),
deba@2025
    83
               "INVALID DUAL");
deba@2025
    84
deba@2025
    85
deba@2025
    86
  LEMON_ASSERT(mca.reached(source), "INVALID ARBORESCENCE");
deba@2025
    87
  for (EdgeIt it(graph); it != INVALID; ++it) {
deba@2025
    88
    LEMON_ASSERT(!mca.reached(graph.source(it)) || 
deba@2025
    89
                 mca.reached(graph.target(it)), "INVALID ARBORESCENCE");
deba@2025
    90
  }
deba@2025
    91
deba@2025
    92
  for (NodeIt it(graph); it != INVALID; ++it) {
deba@2025
    93
    if (!mca.reached(it)) continue;
deba@2025
    94
    int cnt = 0;
deba@2025
    95
    for (InEdgeIt jt(graph, it); jt != INVALID; ++jt) {
deba@2025
    96
      if (mca.arborescence(jt)) {
deba@2025
    97
        ++cnt;
deba@2025
    98
      }
deba@2025
    99
    }
deba@2025
   100
    LEMON_ASSERT((it == source ? cnt == 0 : cnt == 1), "INVALID ARBORESCENCE");
deba@2025
   101
  }
deba@2025
   102
  
deba@2025
   103
  return 0;
deba@2025
   104
}