test/gomory_hu_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 17 Apr 2009 09:54:14 +0200
changeset 585 7ac52d6a268e
parent 533 e72bacfea6b7
child 588 293551ad254f
permissions -rw-r--r--
Extend and modify the interface of matching algorithms (#265)

- Rename decomposition() to status() in MaxMatching.
- Add a new query function statusMap() to MaxMatching.
- Add a new query function matchingMap() to all the three classes.
- Rename matchingValue() to matchingWeight() in the weighted
matching classes.
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.run();
tapolcai@531
    65
tapolcai@531
    66
  for (NodeIt u(graph); u != INVALID; ++u) {
tapolcai@531
    67
    for (NodeIt v(graph); v != u; ++v) {
tapolcai@531
    68
      Preflow<Graph, IntEdgeMap> pf(graph, capacity, u, v);
tapolcai@531
    69
      pf.runMinCut();
tapolcai@531
    70
      BoolNodeMap cm(graph);
tapolcai@531
    71
      ght.minCutMap(u, v, cm);
tapolcai@531
    72
      check(pf.flowValue() == ght.minCutValue(u, v), "Wrong cut 1");
tapolcai@531
    73
      check(cm[u] != cm[v], "Wrong cut 3");
tapolcai@531
    74
      check(pf.flowValue() == cutValue(graph, cm, capacity), "Wrong cut 2");
alpar@532
    75
alpar@532
    76
      int sum=0;
alpar@533
    77
      for(GomoryHu<Graph>::MinCutEdgeIt a(ght, u, v);a!=INVALID;++a)
alpar@532
    78
        sum+=capacity[a]; 
alpar@532
    79
      check(sum == ght.minCutValue(u, v), "Problem with MinCutEdgeIt");
alpar@532
    80
alpar@532
    81
      sum=0;
alpar@533
    82
      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,true);n!=INVALID;++n)
alpar@532
    83
        sum++;
alpar@533
    84
      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,false);n!=INVALID;++n)
alpar@532
    85
        sum++;
alpar@532
    86
      check(sum == countNodes(graph), "Problem with MinCutNodeIt");
tapolcai@531
    87
      
tapolcai@531
    88
    }
tapolcai@531
    89
  }
tapolcai@531
    90
  
tapolcai@531
    91
  return 0;
tapolcai@531
    92
}