test/gomory_hu_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 10 Jan 2011 09:34:50 +0100
changeset 1026 9312d6c89d02
parent 643 293551ad254f
child 1173 d216e1c8b3fa
permissions -rw-r--r--
Merge
alpar@956
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@956
     2
 *
alpar@956
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@956
     4
 *
alpar@956
     5
 * Copyright (C) 2003-2010
alpar@956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@956
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@956
     8
 *
alpar@956
     9
 * Permission to use, modify and distribute this software is granted
alpar@956
    10
 * provided that this copyright notice appears in all copies. For
alpar@956
    11
 * precise terms see the accompanying LICENSE file.
alpar@956
    12
 *
alpar@956
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@956
    14
 * express or implied, and with no claim as to its suitability for any
alpar@956
    15
 * purpose.
alpar@956
    16
 *
alpar@956
    17
 */
alpar@956
    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@956
    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;
kpeter@643
    71
kpeter@643
    72
  GomoryHu<Graph, CapMap> gh_test(g, cap);
kpeter@643
    73
  const GomoryHu<Graph, CapMap>&
kpeter@643
    74
    const_gh_test = gh_test;
kpeter@643
    75
kpeter@643
    76
  gh_test.run();
kpeter@643
    77
kpeter@643
    78
  n = const_gh_test.predNode(n);
kpeter@643
    79
  v = const_gh_test.predValue(n);
kpeter@643
    80
  d = const_gh_test.rootDist(n);
kpeter@643
    81
  v = const_gh_test.minCutValue(n, n);
kpeter@643
    82
  v = const_gh_test.minCutMap(n, n, cut);
kpeter@643
    83
}
kpeter@643
    84
tapolcai@590
    85
GRAPH_TYPEDEFS(Graph);
tapolcai@590
    86
typedef Graph::EdgeMap<int> IntEdgeMap;
tapolcai@590
    87
typedef Graph::NodeMap<bool> BoolNodeMap;
tapolcai@590
    88
tapolcai@590
    89
int cutValue(const Graph& graph, const BoolNodeMap& cut,
alpar@956
    90
             const IntEdgeMap& capacity) {
tapolcai@590
    91
tapolcai@590
    92
  int sum = 0;
tapolcai@590
    93
  for (EdgeIt e(graph); e != INVALID; ++e) {
tapolcai@590
    94
    Node s = graph.u(e);
tapolcai@590
    95
    Node t = graph.v(e);
tapolcai@590
    96
tapolcai@590
    97
    if (cut[s] != cut[t]) {
tapolcai@590
    98
      sum += capacity[e];
tapolcai@590
    99
    }
tapolcai@590
   100
  }
tapolcai@590
   101
  return sum;
tapolcai@590
   102
}
tapolcai@590
   103
tapolcai@590
   104
tapolcai@590
   105
int main() {
tapolcai@590
   106
  Graph graph;
tapolcai@590
   107
  IntEdgeMap capacity(graph);
tapolcai@590
   108
tapolcai@590
   109
  std::istringstream input(test_lgf);
tapolcai@590
   110
  GraphReader<Graph>(graph, input).
tapolcai@590
   111
    edgeMap("capacity", capacity).run();
tapolcai@590
   112
alpar@592
   113
  GomoryHu<Graph> ght(graph, capacity);
tapolcai@590
   114
  ght.run();
tapolcai@590
   115
tapolcai@590
   116
  for (NodeIt u(graph); u != INVALID; ++u) {
tapolcai@590
   117
    for (NodeIt v(graph); v != u; ++v) {
tapolcai@590
   118
      Preflow<Graph, IntEdgeMap> pf(graph, capacity, u, v);
tapolcai@590
   119
      pf.runMinCut();
tapolcai@590
   120
      BoolNodeMap cm(graph);
tapolcai@590
   121
      ght.minCutMap(u, v, cm);
tapolcai@590
   122
      check(pf.flowValue() == ght.minCutValue(u, v), "Wrong cut 1");
kpeter@643
   123
      check(cm[u] != cm[v], "Wrong cut 2");
kpeter@643
   124
      check(pf.flowValue() == cutValue(graph, cm, capacity), "Wrong cut 3");
alpar@591
   125
alpar@591
   126
      int sum=0;
alpar@592
   127
      for(GomoryHu<Graph>::MinCutEdgeIt a(ght, u, v);a!=INVALID;++a)
alpar@956
   128
        sum+=capacity[a];
alpar@591
   129
      check(sum == ght.minCutValue(u, v), "Problem with MinCutEdgeIt");
alpar@591
   130
alpar@591
   131
      sum=0;
alpar@592
   132
      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,true);n!=INVALID;++n)
alpar@591
   133
        sum++;
alpar@592
   134
      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,false);n!=INVALID;++n)
alpar@591
   135
        sum++;
alpar@591
   136
      check(sum == countNodes(graph), "Problem with MinCutNodeIt");
tapolcai@590
   137
    }
tapolcai@590
   138
  }
alpar@956
   139
tapolcai@590
   140
  return 0;
tapolcai@590
   141
}