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