test/nagamochi_ibaraki_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 05 Jun 2012 12:06:28 +0200
changeset 1222 c40a9d94442d
child 1177 9a6c9d4ee77b
permissions -rw-r--r--
New features in time_measure.h (#442)
deba@1017
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@1017
     2
 *
deba@1017
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@1017
     4
 *
deba@1017
     5
 * Copyright (C) 2003-2010
deba@1017
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1017
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1017
     8
 *
deba@1017
     9
 * Permission to use, modify and distribute this software is granted
deba@1017
    10
 * provided that this copyright notice appears in all copies. For
deba@1017
    11
 * precise terms see the accompanying LICENSE file.
deba@1017
    12
 *
deba@1017
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1017
    14
 * express or implied, and with no claim as to its suitability for any
deba@1017
    15
 * purpose.
deba@1017
    16
 *
deba@1017
    17
 */
deba@1017
    18
deba@1017
    19
#include <sstream>
deba@1017
    20
deba@1017
    21
#include <lemon/smart_graph.h>
deba@1017
    22
#include <lemon/adaptors.h>
deba@1017
    23
#include <lemon/concepts/graph.h>
deba@1017
    24
#include <lemon/concepts/maps.h>
deba@1017
    25
#include <lemon/lgf_reader.h>
deba@1017
    26
#include <lemon/nagamochi_ibaraki.h>
deba@1017
    27
deba@1017
    28
#include "test_tools.h"
deba@1017
    29
deba@1017
    30
using namespace lemon;
deba@1017
    31
using namespace std;
deba@1017
    32
deba@1017
    33
const std::string lgf =
deba@1017
    34
  "@nodes\n"
deba@1017
    35
  "label\n"
deba@1017
    36
  "0\n"
deba@1017
    37
  "1\n"
deba@1017
    38
  "2\n"
deba@1017
    39
  "3\n"
deba@1017
    40
  "4\n"
deba@1017
    41
  "5\n"
deba@1017
    42
  "@edges\n"
deba@1017
    43
  "     cap1 cap2 cap3\n"
deba@1017
    44
  "0 1  1    1    1   \n"
deba@1017
    45
  "0 2  2    2    4   \n"
deba@1017
    46
  "1 2  4    4    4   \n"
deba@1017
    47
  "3 4  1    1    1   \n"
deba@1017
    48
  "3 5  2    2    4   \n"
deba@1017
    49
  "4 5  4    4    4   \n"
deba@1017
    50
  "2 3  1    6    6   \n";
deba@1017
    51
deba@1017
    52
void checkNagamochiIbarakiCompile()
deba@1017
    53
{
deba@1017
    54
  typedef int Value;
deba@1017
    55
  typedef concepts::Graph Graph;
deba@1017
    56
deba@1017
    57
  typedef Graph::Node Node;
deba@1017
    58
  typedef Graph::Edge Edge;
deba@1017
    59
  typedef concepts::ReadMap<Edge, Value> CapMap;
deba@1017
    60
  typedef concepts::WriteMap<Node, bool> CutMap;
deba@1017
    61
deba@1017
    62
  Graph g;
deba@1017
    63
  Node n;
deba@1017
    64
  CapMap cap;
deba@1017
    65
  CutMap cut;
deba@1017
    66
  Value v;
deba@1017
    67
  bool b;
deba@1017
    68
deba@1017
    69
  NagamochiIbaraki<Graph, CapMap> ni_test(g, cap);
deba@1017
    70
  const NagamochiIbaraki<Graph, CapMap>& const_ni_test = ni_test;
deba@1017
    71
deba@1017
    72
  ni_test.init();
deba@1017
    73
  ni_test.start();
deba@1017
    74
  b = ni_test.processNextPhase();
deba@1017
    75
  ni_test.run();
deba@1017
    76
deba@1017
    77
  v = const_ni_test.minCutValue();
deba@1017
    78
  v = const_ni_test.minCutMap(cut);
deba@1017
    79
}
deba@1017
    80
deba@1017
    81
template <typename Graph, typename CapMap, typename CutMap>
deba@1017
    82
typename CapMap::Value
deba@1017
    83
  cutValue(const Graph& graph, const CapMap& cap, const CutMap& cut)
deba@1017
    84
{
deba@1017
    85
  typename CapMap::Value sum = 0;
deba@1017
    86
  for (typename Graph::EdgeIt e(graph); e != INVALID; ++e) {
deba@1017
    87
    if (cut[graph.u(e)] != cut[graph.v(e)]) {
deba@1017
    88
      sum += cap[e];
deba@1017
    89
    }
deba@1017
    90
  }
deba@1017
    91
  return sum;
deba@1017
    92
}
deba@1017
    93
deba@1017
    94
int main() {
deba@1017
    95
  SmartGraph graph;
deba@1017
    96
  SmartGraph::EdgeMap<int> cap1(graph), cap2(graph), cap3(graph);
deba@1017
    97
  SmartGraph::NodeMap<bool> cut(graph);
deba@1017
    98
deba@1017
    99
  istringstream input(lgf);
deba@1017
   100
  graphReader(graph, input)
deba@1017
   101
    .edgeMap("cap1", cap1)
deba@1017
   102
    .edgeMap("cap2", cap2)
deba@1017
   103
    .edgeMap("cap3", cap3)
deba@1017
   104
    .run();
deba@1017
   105
deba@1017
   106
  {
deba@1017
   107
    NagamochiIbaraki<SmartGraph> ni(graph, cap1);
deba@1017
   108
    ni.run();
deba@1017
   109
    ni.minCutMap(cut);
deba@1017
   110
deba@1017
   111
    check(ni.minCutValue() == 1, "Wrong cut value");
deba@1017
   112
    check(ni.minCutValue() == cutValue(graph, cap1, cut), "Wrong cut value");
deba@1017
   113
  }
deba@1017
   114
  {
deba@1017
   115
    NagamochiIbaraki<SmartGraph> ni(graph, cap2);
deba@1017
   116
    ni.run();
deba@1017
   117
    ni.minCutMap(cut);
deba@1017
   118
deba@1017
   119
    check(ni.minCutValue() == 3, "Wrong cut value");
deba@1017
   120
    check(ni.minCutValue() == cutValue(graph, cap2, cut), "Wrong cut value");
deba@1017
   121
  }
deba@1017
   122
  {
deba@1017
   123
    NagamochiIbaraki<SmartGraph> ni(graph, cap3);
deba@1017
   124
    ni.run();
deba@1017
   125
    ni.minCutMap(cut);
deba@1017
   126
deba@1017
   127
    check(ni.minCutValue() == 5, "Wrong cut value");
deba@1017
   128
    check(ni.minCutValue() == cutValue(graph, cap3, cut), "Wrong cut value");
deba@1017
   129
  }
deba@1017
   130
  {
deba@1017
   131
    NagamochiIbaraki<SmartGraph>::SetUnitCapacity::Create ni(graph);
deba@1017
   132
    ni.run();
deba@1017
   133
    ni.minCutMap(cut);
deba@1017
   134
deba@1017
   135
    ConstMap<SmartGraph::Edge, int> cap4(1);
deba@1017
   136
    check(ni.minCutValue() == 1, "Wrong cut value");
deba@1017
   137
    check(ni.minCutValue() == cutValue(graph, cap4, cut), "Wrong cut value");
deba@1017
   138
  }
deba@1017
   139
deba@1017
   140
  return 0;
deba@1017
   141
}