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