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