test/gomory_hu_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 05 Aug 2013 14:03:29 +0200
changeset 1074 97d978243703
parent 877 141f9c0db4a3
parent 1007 7e368d9b67f7
child 1084 8b2d4e5d96e4
permissions -rw-r--r--
Fix unresolved doc references (#459)
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;
alpar@1007
    71
  ignore_unused_variable_warning(v,d);
kpeter@596
    72
kpeter@596
    73
  GomoryHu<Graph, CapMap> gh_test(g, cap);
kpeter@596
    74
  const GomoryHu<Graph, CapMap>&
kpeter@596
    75
    const_gh_test = gh_test;
kpeter@596
    76
kpeter@596
    77
  gh_test.run();
kpeter@596
    78
kpeter@596
    79
  n = const_gh_test.predNode(n);
kpeter@596
    80
  v = const_gh_test.predValue(n);
kpeter@596
    81
  d = const_gh_test.rootDist(n);
kpeter@596
    82
  v = const_gh_test.minCutValue(n, n);
kpeter@596
    83
  v = const_gh_test.minCutMap(n, n, cut);
kpeter@596
    84
}
kpeter@596
    85
tapolcai@543
    86
GRAPH_TYPEDEFS(Graph);
tapolcai@543
    87
typedef Graph::EdgeMap<int> IntEdgeMap;
tapolcai@543
    88
typedef Graph::NodeMap<bool> BoolNodeMap;
tapolcai@543
    89
tapolcai@543
    90
int cutValue(const Graph& graph, const BoolNodeMap& cut,
alpar@877
    91
             const IntEdgeMap& capacity) {
tapolcai@543
    92
tapolcai@543
    93
  int sum = 0;
tapolcai@543
    94
  for (EdgeIt e(graph); e != INVALID; ++e) {
tapolcai@543
    95
    Node s = graph.u(e);
tapolcai@543
    96
    Node t = graph.v(e);
tapolcai@543
    97
tapolcai@543
    98
    if (cut[s] != cut[t]) {
tapolcai@543
    99
      sum += capacity[e];
tapolcai@543
   100
    }
tapolcai@543
   101
  }
tapolcai@543
   102
  return sum;
tapolcai@543
   103
}
tapolcai@543
   104
tapolcai@543
   105
tapolcai@543
   106
int main() {
tapolcai@543
   107
  Graph graph;
tapolcai@543
   108
  IntEdgeMap capacity(graph);
tapolcai@543
   109
tapolcai@543
   110
  std::istringstream input(test_lgf);
tapolcai@543
   111
  GraphReader<Graph>(graph, input).
tapolcai@543
   112
    edgeMap("capacity", capacity).run();
tapolcai@543
   113
alpar@545
   114
  GomoryHu<Graph> ght(graph, capacity);
tapolcai@543
   115
  ght.run();
tapolcai@543
   116
tapolcai@543
   117
  for (NodeIt u(graph); u != INVALID; ++u) {
tapolcai@543
   118
    for (NodeIt v(graph); v != u; ++v) {
tapolcai@543
   119
      Preflow<Graph, IntEdgeMap> pf(graph, capacity, u, v);
tapolcai@543
   120
      pf.runMinCut();
tapolcai@543
   121
      BoolNodeMap cm(graph);
tapolcai@543
   122
      ght.minCutMap(u, v, cm);
tapolcai@543
   123
      check(pf.flowValue() == ght.minCutValue(u, v), "Wrong cut 1");
kpeter@596
   124
      check(cm[u] != cm[v], "Wrong cut 2");
kpeter@596
   125
      check(pf.flowValue() == cutValue(graph, cm, capacity), "Wrong cut 3");
alpar@544
   126
alpar@544
   127
      int sum=0;
alpar@545
   128
      for(GomoryHu<Graph>::MinCutEdgeIt a(ght, u, v);a!=INVALID;++a)
alpar@877
   129
        sum+=capacity[a];
alpar@544
   130
      check(sum == ght.minCutValue(u, v), "Problem with MinCutEdgeIt");
alpar@544
   131
alpar@544
   132
      sum=0;
alpar@545
   133
      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,true);n!=INVALID;++n)
alpar@544
   134
        sum++;
alpar@545
   135
      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,false);n!=INVALID;++n)
alpar@544
   136
        sum++;
alpar@544
   137
      check(sum == countNodes(graph), "Problem with MinCutNodeIt");
tapolcai@543
   138
    }
tapolcai@543
   139
  }
alpar@877
   140
tapolcai@543
   141
  return 0;
tapolcai@543
   142
}