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