test/gomory_hu_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 25 Feb 2009 11:10:57 +0000
changeset 533 e72bacfea6b7
parent 532 ccd2d3a3001e
child 534 d6b40ebb2617
permissions -rw-r--r--
Remane GomoryHuTree to GomoryHu (#66)
tapolcai@531
     1
#include <iostream>
tapolcai@531
     2
tapolcai@531
     3
#include "test_tools.h"
tapolcai@531
     4
#include <lemon/smart_graph.h>
tapolcai@531
     5
#include <lemon/lgf_reader.h>
alpar@533
     6
#include <lemon/gomory_hu.h>
tapolcai@531
     7
#include <cstdlib>
tapolcai@531
     8
tapolcai@531
     9
using namespace std;
tapolcai@531
    10
using namespace lemon;
tapolcai@531
    11
tapolcai@531
    12
typedef SmartGraph Graph;
tapolcai@531
    13
tapolcai@531
    14
char test_lgf[] =
tapolcai@531
    15
  "@nodes\n"
tapolcai@531
    16
  "label\n"
tapolcai@531
    17
  "0\n"
tapolcai@531
    18
  "1\n"
tapolcai@531
    19
  "2\n"
tapolcai@531
    20
  "3\n"
tapolcai@531
    21
  "4\n"
tapolcai@531
    22
  "@arcs\n"
tapolcai@531
    23
  "     label capacity\n"
tapolcai@531
    24
  "0 1  0     1\n"
tapolcai@531
    25
  "1 2  1     1\n"
tapolcai@531
    26
  "2 3  2     1\n"
tapolcai@531
    27
  "0 3  4     5\n"
tapolcai@531
    28
  "0 3  5     10\n"
tapolcai@531
    29
  "0 3  6     7\n"
tapolcai@531
    30
  "4 2  7     1\n"
tapolcai@531
    31
  "@attributes\n"
tapolcai@531
    32
  "source 0\n"
tapolcai@531
    33
  "target 3\n";
tapolcai@531
    34
  
tapolcai@531
    35
GRAPH_TYPEDEFS(Graph);
tapolcai@531
    36
typedef Graph::EdgeMap<int> IntEdgeMap;
tapolcai@531
    37
typedef Graph::NodeMap<bool> BoolNodeMap;
tapolcai@531
    38
tapolcai@531
    39
int cutValue(const Graph& graph, const BoolNodeMap& cut,
tapolcai@531
    40
	     const IntEdgeMap& capacity) {
tapolcai@531
    41
tapolcai@531
    42
  int sum = 0;
tapolcai@531
    43
  for (EdgeIt e(graph); e != INVALID; ++e) {
tapolcai@531
    44
    Node s = graph.u(e);
tapolcai@531
    45
    Node t = graph.v(e);
tapolcai@531
    46
tapolcai@531
    47
    if (cut[s] != cut[t]) {
tapolcai@531
    48
      sum += capacity[e];
tapolcai@531
    49
    }
tapolcai@531
    50
  }
tapolcai@531
    51
  return sum;
tapolcai@531
    52
}
tapolcai@531
    53
tapolcai@531
    54
tapolcai@531
    55
int main() {
tapolcai@531
    56
  Graph graph;
tapolcai@531
    57
  IntEdgeMap capacity(graph);
tapolcai@531
    58
tapolcai@531
    59
  std::istringstream input(test_lgf);
tapolcai@531
    60
  GraphReader<Graph>(graph, input).
tapolcai@531
    61
    edgeMap("capacity", capacity).run();
tapolcai@531
    62
alpar@533
    63
  GomoryHu<Graph> ght(graph, capacity);
tapolcai@531
    64
  ght.init();
tapolcai@531
    65
  ght.run();
tapolcai@531
    66
tapolcai@531
    67
  for (NodeIt u(graph); u != INVALID; ++u) {
tapolcai@531
    68
    for (NodeIt v(graph); v != u; ++v) {
tapolcai@531
    69
      Preflow<Graph, IntEdgeMap> pf(graph, capacity, u, v);
tapolcai@531
    70
      pf.runMinCut();
tapolcai@531
    71
      BoolNodeMap cm(graph);
tapolcai@531
    72
      ght.minCutMap(u, v, cm);
tapolcai@531
    73
      check(pf.flowValue() == ght.minCutValue(u, v), "Wrong cut 1");
tapolcai@531
    74
      check(cm[u] != cm[v], "Wrong cut 3");
tapolcai@531
    75
      check(pf.flowValue() == cutValue(graph, cm, capacity), "Wrong cut 2");
alpar@532
    76
alpar@532
    77
      int sum=0;
alpar@533
    78
      for(GomoryHu<Graph>::MinCutEdgeIt a(ght, u, v);a!=INVALID;++a)
alpar@532
    79
        sum+=capacity[a]; 
alpar@532
    80
      check(sum == ght.minCutValue(u, v), "Problem with MinCutEdgeIt");
alpar@532
    81
alpar@532
    82
      sum=0;
alpar@533
    83
      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,true);n!=INVALID;++n)
alpar@532
    84
        sum++;
alpar@533
    85
      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,false);n!=INVALID;++n)
alpar@532
    86
        sum++;
alpar@532
    87
      check(sum == countNodes(graph), "Problem with MinCutNodeIt");
tapolcai@531
    88
      
tapolcai@531
    89
    }
tapolcai@531
    90
  }
tapolcai@531
    91
  
tapolcai@531
    92
  return 0;
tapolcai@531
    93
}