test/nagamochi_ibaraki_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 08 Mar 2013 01:12:05 +0100
changeset 1213 4e8787627db3
parent 1017 5087694945e4
child 1262 dd1443e4a34c
permissions -rw-r--r--
Improve images of the Connectivity section (#411)
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;
alpar@1177
    68
  ignore_unused_variable_warning(v,b);
deba@1017
    69
deba@1017
    70
  NagamochiIbaraki<Graph, CapMap> ni_test(g, cap);
deba@1017
    71
  const NagamochiIbaraki<Graph, CapMap>& const_ni_test = ni_test;
deba@1017
    72
deba@1017
    73
  ni_test.init();
deba@1017
    74
  ni_test.start();
deba@1017
    75
  b = ni_test.processNextPhase();
deba@1017
    76
  ni_test.run();
deba@1017
    77
deba@1017
    78
  v = const_ni_test.minCutValue();
deba@1017
    79
  v = const_ni_test.minCutMap(cut);
deba@1017
    80
}
deba@1017
    81
deba@1017
    82
template <typename Graph, typename CapMap, typename CutMap>
deba@1017
    83
typename CapMap::Value
deba@1017
    84
  cutValue(const Graph& graph, const CapMap& cap, const CutMap& cut)
deba@1017
    85
{
deba@1017
    86
  typename CapMap::Value sum = 0;
deba@1017
    87
  for (typename Graph::EdgeIt e(graph); e != INVALID; ++e) {
deba@1017
    88
    if (cut[graph.u(e)] != cut[graph.v(e)]) {
deba@1017
    89
      sum += cap[e];
deba@1017
    90
    }
deba@1017
    91
  }
deba@1017
    92
  return sum;
deba@1017
    93
}
deba@1017
    94
deba@1017
    95
int main() {
deba@1017
    96
  SmartGraph graph;
deba@1017
    97
  SmartGraph::EdgeMap<int> cap1(graph), cap2(graph), cap3(graph);
deba@1017
    98
  SmartGraph::NodeMap<bool> cut(graph);
deba@1017
    99
deba@1017
   100
  istringstream input(lgf);
deba@1017
   101
  graphReader(graph, input)
deba@1017
   102
    .edgeMap("cap1", cap1)
deba@1017
   103
    .edgeMap("cap2", cap2)
deba@1017
   104
    .edgeMap("cap3", cap3)
deba@1017
   105
    .run();
deba@1017
   106
deba@1017
   107
  {
deba@1017
   108
    NagamochiIbaraki<SmartGraph> ni(graph, cap1);
deba@1017
   109
    ni.run();
deba@1017
   110
    ni.minCutMap(cut);
deba@1017
   111
deba@1017
   112
    check(ni.minCutValue() == 1, "Wrong cut value");
deba@1017
   113
    check(ni.minCutValue() == cutValue(graph, cap1, cut), "Wrong cut value");
deba@1017
   114
  }
deba@1017
   115
  {
deba@1017
   116
    NagamochiIbaraki<SmartGraph> ni(graph, cap2);
deba@1017
   117
    ni.run();
deba@1017
   118
    ni.minCutMap(cut);
deba@1017
   119
deba@1017
   120
    check(ni.minCutValue() == 3, "Wrong cut value");
deba@1017
   121
    check(ni.minCutValue() == cutValue(graph, cap2, cut), "Wrong cut value");
deba@1017
   122
  }
deba@1017
   123
  {
deba@1017
   124
    NagamochiIbaraki<SmartGraph> ni(graph, cap3);
deba@1017
   125
    ni.run();
deba@1017
   126
    ni.minCutMap(cut);
deba@1017
   127
deba@1017
   128
    check(ni.minCutValue() == 5, "Wrong cut value");
deba@1017
   129
    check(ni.minCutValue() == cutValue(graph, cap3, cut), "Wrong cut value");
deba@1017
   130
  }
deba@1017
   131
  {
deba@1017
   132
    NagamochiIbaraki<SmartGraph>::SetUnitCapacity::Create ni(graph);
deba@1017
   133
    ni.run();
deba@1017
   134
    ni.minCutMap(cut);
deba@1017
   135
deba@1017
   136
    ConstMap<SmartGraph::Edge, int> cap4(1);
deba@1017
   137
    check(ni.minCutValue() == 1, "Wrong cut value");
deba@1017
   138
    check(ni.minCutValue() == cutValue(graph, cap4, cut), "Wrong cut value");
deba@1017
   139
  }
deba@1017
   140
deba@1017
   141
  return 0;
deba@1017
   142
}