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