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