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