test/gomory_hu_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Tue, 15 Mar 2011 19:54:11 +0100
changeset 938 a07b6b27fe69
parent 596 293551ad254f
child 1008 d216e1c8b3fa
permissions -rw-r--r--
Change the default scaling factor in CostScaling (#417)
alpar@877
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@877
     2
 *
alpar@877
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@877
     4
 *
alpar@877
     5
 * Copyright (C) 2003-2010
alpar@877
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@877
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@877
     8
 *
alpar@877
     9
 * Permission to use, modify and distribute this software is granted
alpar@877
    10
 * provided that this copyright notice appears in all copies. For
alpar@877
    11
 * precise terms see the accompanying LICENSE file.
alpar@877
    12
 *
alpar@877
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@877
    14
 * express or implied, and with no claim as to its suitability for any
alpar@877
    15
 * purpose.
alpar@877
    16
 *
alpar@877
    17
 */
alpar@877
    18
tapolcai@543
    19
#include <iostream>
tapolcai@543
    20
tapolcai@543
    21
#include "test_tools.h"
tapolcai@543
    22
#include <lemon/smart_graph.h>
kpeter@596
    23
#include <lemon/concepts/graph.h>
kpeter@596
    24
#include <lemon/concepts/maps.h>
tapolcai@543
    25
#include <lemon/lgf_reader.h>
alpar@545
    26
#include <lemon/gomory_hu.h>
tapolcai@543
    27
#include <cstdlib>
tapolcai@543
    28
tapolcai@543
    29
using namespace std;
tapolcai@543
    30
using namespace lemon;
tapolcai@543
    31
tapolcai@543
    32
typedef SmartGraph Graph;
tapolcai@543
    33
tapolcai@543
    34
char test_lgf[] =
tapolcai@543
    35
  "@nodes\n"
tapolcai@543
    36
  "label\n"
tapolcai@543
    37
  "0\n"
tapolcai@543
    38
  "1\n"
tapolcai@543
    39
  "2\n"
tapolcai@543
    40
  "3\n"
tapolcai@543
    41
  "4\n"
tapolcai@543
    42
  "@arcs\n"
tapolcai@543
    43
  "     label capacity\n"
tapolcai@543
    44
  "0 1  0     1\n"
tapolcai@543
    45
  "1 2  1     1\n"
tapolcai@543
    46
  "2 3  2     1\n"
tapolcai@543
    47
  "0 3  4     5\n"
tapolcai@543
    48
  "0 3  5     10\n"
tapolcai@543
    49
  "0 3  6     7\n"
tapolcai@543
    50
  "4 2  7     1\n"
tapolcai@543
    51
  "@attributes\n"
tapolcai@543
    52
  "source 0\n"
tapolcai@543
    53
  "target 3\n";
alpar@877
    54
kpeter@596
    55
void checkGomoryHuCompile()
kpeter@596
    56
{
kpeter@596
    57
  typedef int Value;
kpeter@596
    58
  typedef concepts::Graph Graph;
kpeter@596
    59
kpeter@596
    60
  typedef Graph::Node Node;
kpeter@596
    61
  typedef Graph::Edge Edge;
kpeter@596
    62
  typedef concepts::ReadMap<Edge, Value> CapMap;
kpeter@596
    63
  typedef concepts::ReadWriteMap<Node, bool> CutMap;
kpeter@596
    64
kpeter@596
    65
  Graph g;
kpeter@596
    66
  Node n;
kpeter@596
    67
  CapMap cap;
kpeter@596
    68
  CutMap cut;
kpeter@596
    69
  Value v;
kpeter@596
    70
  int d;
kpeter@596
    71
kpeter@596
    72
  GomoryHu<Graph, CapMap> gh_test(g, cap);
kpeter@596
    73
  const GomoryHu<Graph, CapMap>&
kpeter@596
    74
    const_gh_test = gh_test;
kpeter@596
    75
kpeter@596
    76
  gh_test.run();
kpeter@596
    77
kpeter@596
    78
  n = const_gh_test.predNode(n);
kpeter@596
    79
  v = const_gh_test.predValue(n);
kpeter@596
    80
  d = const_gh_test.rootDist(n);
kpeter@596
    81
  v = const_gh_test.minCutValue(n, n);
kpeter@596
    82
  v = const_gh_test.minCutMap(n, n, cut);
kpeter@596
    83
}
kpeter@596
    84
tapolcai@543
    85
GRAPH_TYPEDEFS(Graph);
tapolcai@543
    86
typedef Graph::EdgeMap<int> IntEdgeMap;
tapolcai@543
    87
typedef Graph::NodeMap<bool> BoolNodeMap;
tapolcai@543
    88
tapolcai@543
    89
int cutValue(const Graph& graph, const BoolNodeMap& cut,
alpar@877
    90
             const IntEdgeMap& capacity) {
tapolcai@543
    91
tapolcai@543
    92
  int sum = 0;
tapolcai@543
    93
  for (EdgeIt e(graph); e != INVALID; ++e) {
tapolcai@543
    94
    Node s = graph.u(e);
tapolcai@543
    95
    Node t = graph.v(e);
tapolcai@543
    96
tapolcai@543
    97
    if (cut[s] != cut[t]) {
tapolcai@543
    98
      sum += capacity[e];
tapolcai@543
    99
    }
tapolcai@543
   100
  }
tapolcai@543
   101
  return sum;
tapolcai@543
   102
}
tapolcai@543
   103
tapolcai@543
   104
tapolcai@543
   105
int main() {
tapolcai@543
   106
  Graph graph;
tapolcai@543
   107
  IntEdgeMap capacity(graph);
tapolcai@543
   108
tapolcai@543
   109
  std::istringstream input(test_lgf);
tapolcai@543
   110
  GraphReader<Graph>(graph, input).
tapolcai@543
   111
    edgeMap("capacity", capacity).run();
tapolcai@543
   112
alpar@545
   113
  GomoryHu<Graph> ght(graph, capacity);
tapolcai@543
   114
  ght.run();
tapolcai@543
   115
tapolcai@543
   116
  for (NodeIt u(graph); u != INVALID; ++u) {
tapolcai@543
   117
    for (NodeIt v(graph); v != u; ++v) {
tapolcai@543
   118
      Preflow<Graph, IntEdgeMap> pf(graph, capacity, u, v);
tapolcai@543
   119
      pf.runMinCut();
tapolcai@543
   120
      BoolNodeMap cm(graph);
tapolcai@543
   121
      ght.minCutMap(u, v, cm);
tapolcai@543
   122
      check(pf.flowValue() == ght.minCutValue(u, v), "Wrong cut 1");
kpeter@596
   123
      check(cm[u] != cm[v], "Wrong cut 2");
kpeter@596
   124
      check(pf.flowValue() == cutValue(graph, cm, capacity), "Wrong cut 3");
alpar@544
   125
alpar@544
   126
      int sum=0;
alpar@545
   127
      for(GomoryHu<Graph>::MinCutEdgeIt a(ght, u, v);a!=INVALID;++a)
alpar@877
   128
        sum+=capacity[a];
alpar@544
   129
      check(sum == ght.minCutValue(u, v), "Problem with MinCutEdgeIt");
alpar@544
   130
alpar@544
   131
      sum=0;
alpar@545
   132
      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,true);n!=INVALID;++n)
alpar@544
   133
        sum++;
alpar@545
   134
      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,false);n!=INVALID;++n)
alpar@544
   135
        sum++;
alpar@544
   136
      check(sum == countNodes(graph), "Problem with MinCutNodeIt");
tapolcai@543
   137
    }
tapolcai@543
   138
  }
alpar@877
   139
tapolcai@543
   140
  return 0;
tapolcai@543
   141
}