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