test/gomory_hu_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 06 Aug 2013 12:04:13 +0200
changeset 1075 115031ac8001
parent 596 293551ad254f
child 1008 d216e1c8b3fa
child 1083 3e711ee55d31
permissions -rw-r--r--
Further fixes for gcc version 3.3 and 4.3 (#470)
tapolcai@543
     1
#include <iostream>
tapolcai@543
     2
tapolcai@543
     3
#include "test_tools.h"
tapolcai@543
     4
#include <lemon/smart_graph.h>
kpeter@596
     5
#include <lemon/concepts/graph.h>
kpeter@596
     6
#include <lemon/concepts/maps.h>
tapolcai@543
     7
#include <lemon/lgf_reader.h>
alpar@545
     8
#include <lemon/gomory_hu.h>
tapolcai@543
     9
#include <cstdlib>
tapolcai@543
    10
tapolcai@543
    11
using namespace std;
tapolcai@543
    12
using namespace lemon;
tapolcai@543
    13
tapolcai@543
    14
typedef SmartGraph Graph;
tapolcai@543
    15
tapolcai@543
    16
char test_lgf[] =
tapolcai@543
    17
  "@nodes\n"
tapolcai@543
    18
  "label\n"
tapolcai@543
    19
  "0\n"
tapolcai@543
    20
  "1\n"
tapolcai@543
    21
  "2\n"
tapolcai@543
    22
  "3\n"
tapolcai@543
    23
  "4\n"
tapolcai@543
    24
  "@arcs\n"
tapolcai@543
    25
  "     label capacity\n"
tapolcai@543
    26
  "0 1  0     1\n"
tapolcai@543
    27
  "1 2  1     1\n"
tapolcai@543
    28
  "2 3  2     1\n"
tapolcai@543
    29
  "0 3  4     5\n"
tapolcai@543
    30
  "0 3  5     10\n"
tapolcai@543
    31
  "0 3  6     7\n"
tapolcai@543
    32
  "4 2  7     1\n"
tapolcai@543
    33
  "@attributes\n"
tapolcai@543
    34
  "source 0\n"
tapolcai@543
    35
  "target 3\n";
tapolcai@543
    36
  
kpeter@596
    37
void checkGomoryHuCompile()
kpeter@596
    38
{
kpeter@596
    39
  typedef int Value;
kpeter@596
    40
  typedef concepts::Graph Graph;
kpeter@596
    41
kpeter@596
    42
  typedef Graph::Node Node;
kpeter@596
    43
  typedef Graph::Edge Edge;
kpeter@596
    44
  typedef concepts::ReadMap<Edge, Value> CapMap;
kpeter@596
    45
  typedef concepts::ReadWriteMap<Node, bool> CutMap;
kpeter@596
    46
kpeter@596
    47
  Graph g;
kpeter@596
    48
  Node n;
kpeter@596
    49
  CapMap cap;
kpeter@596
    50
  CutMap cut;
kpeter@596
    51
  Value v;
kpeter@596
    52
  int d;
alpar@1007
    53
  ignore_unused_variable_warning(v,d);
kpeter@596
    54
kpeter@596
    55
  GomoryHu<Graph, CapMap> gh_test(g, cap);
kpeter@596
    56
  const GomoryHu<Graph, CapMap>&
kpeter@596
    57
    const_gh_test = gh_test;
kpeter@596
    58
kpeter@596
    59
  gh_test.run();
kpeter@596
    60
kpeter@596
    61
  n = const_gh_test.predNode(n);
kpeter@596
    62
  v = const_gh_test.predValue(n);
kpeter@596
    63
  d = const_gh_test.rootDist(n);
kpeter@596
    64
  v = const_gh_test.minCutValue(n, n);
kpeter@596
    65
  v = const_gh_test.minCutMap(n, n, cut);
kpeter@596
    66
}
kpeter@596
    67
tapolcai@543
    68
GRAPH_TYPEDEFS(Graph);
tapolcai@543
    69
typedef Graph::EdgeMap<int> IntEdgeMap;
tapolcai@543
    70
typedef Graph::NodeMap<bool> BoolNodeMap;
tapolcai@543
    71
tapolcai@543
    72
int cutValue(const Graph& graph, const BoolNodeMap& cut,
tapolcai@543
    73
	     const IntEdgeMap& capacity) {
tapolcai@543
    74
tapolcai@543
    75
  int sum = 0;
tapolcai@543
    76
  for (EdgeIt e(graph); e != INVALID; ++e) {
tapolcai@543
    77
    Node s = graph.u(e);
tapolcai@543
    78
    Node t = graph.v(e);
tapolcai@543
    79
tapolcai@543
    80
    if (cut[s] != cut[t]) {
tapolcai@543
    81
      sum += capacity[e];
tapolcai@543
    82
    }
tapolcai@543
    83
  }
tapolcai@543
    84
  return sum;
tapolcai@543
    85
}
tapolcai@543
    86
tapolcai@543
    87
tapolcai@543
    88
int main() {
tapolcai@543
    89
  Graph graph;
tapolcai@543
    90
  IntEdgeMap capacity(graph);
tapolcai@543
    91
tapolcai@543
    92
  std::istringstream input(test_lgf);
tapolcai@543
    93
  GraphReader<Graph>(graph, input).
tapolcai@543
    94
    edgeMap("capacity", capacity).run();
tapolcai@543
    95
alpar@545
    96
  GomoryHu<Graph> ght(graph, capacity);
tapolcai@543
    97
  ght.run();
tapolcai@543
    98
tapolcai@543
    99
  for (NodeIt u(graph); u != INVALID; ++u) {
tapolcai@543
   100
    for (NodeIt v(graph); v != u; ++v) {
tapolcai@543
   101
      Preflow<Graph, IntEdgeMap> pf(graph, capacity, u, v);
tapolcai@543
   102
      pf.runMinCut();
tapolcai@543
   103
      BoolNodeMap cm(graph);
tapolcai@543
   104
      ght.minCutMap(u, v, cm);
tapolcai@543
   105
      check(pf.flowValue() == ght.minCutValue(u, v), "Wrong cut 1");
kpeter@596
   106
      check(cm[u] != cm[v], "Wrong cut 2");
kpeter@596
   107
      check(pf.flowValue() == cutValue(graph, cm, capacity), "Wrong cut 3");
alpar@544
   108
alpar@544
   109
      int sum=0;
alpar@545
   110
      for(GomoryHu<Graph>::MinCutEdgeIt a(ght, u, v);a!=INVALID;++a)
alpar@544
   111
        sum+=capacity[a]; 
alpar@544
   112
      check(sum == ght.minCutValue(u, v), "Problem with MinCutEdgeIt");
alpar@544
   113
alpar@544
   114
      sum=0;
alpar@545
   115
      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,true);n!=INVALID;++n)
alpar@544
   116
        sum++;
alpar@545
   117
      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,false);n!=INVALID;++n)
alpar@544
   118
        sum++;
alpar@544
   119
      check(sum == countNodes(graph), "Problem with MinCutNodeIt");
tapolcai@543
   120
    }
tapolcai@543
   121
  }
tapolcai@543
   122
  
tapolcai@543
   123
  return 0;
tapolcai@543
   124
}