1.1 --- a/test/preflow_test.cc Thu Feb 28 18:13:48 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,277 +0,0 @@
1.4 -/* -*- mode: C++; indent-tabs-mode: nil; -*-
1.5 - *
1.6 - * This file is a part of LEMON, a generic C++ optimization library.
1.7 - *
1.8 - * Copyright (C) 2003-2010
1.9 - * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 - * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 - *
1.12 - * Permission to use, modify and distribute this software is granted
1.13 - * provided that this copyright notice appears in all copies. For
1.14 - * precise terms see the accompanying LICENSE file.
1.15 - *
1.16 - * This software is provided "AS IS" with no warranty of any kind,
1.17 - * express or implied, and with no claim as to its suitability for any
1.18 - * purpose.
1.19 - *
1.20 - */
1.21 -
1.22 -#include <iostream>
1.23 -
1.24 -#include "test_tools.h"
1.25 -#include <lemon/smart_graph.h>
1.26 -#include <lemon/preflow.h>
1.27 -#include <lemon/concepts/digraph.h>
1.28 -#include <lemon/concepts/maps.h>
1.29 -#include <lemon/lgf_reader.h>
1.30 -#include <lemon/elevator.h>
1.31 -
1.32 -using namespace lemon;
1.33 -
1.34 -char test_lgf[] =
1.35 - "@nodes\n"
1.36 - "label\n"
1.37 - "0\n"
1.38 - "1\n"
1.39 - "2\n"
1.40 - "3\n"
1.41 - "4\n"
1.42 - "5\n"
1.43 - "6\n"
1.44 - "7\n"
1.45 - "8\n"
1.46 - "9\n"
1.47 - "@arcs\n"
1.48 - " label capacity\n"
1.49 - "0 1 0 20\n"
1.50 - "0 2 1 0\n"
1.51 - "1 1 2 3\n"
1.52 - "1 2 3 8\n"
1.53 - "1 3 4 8\n"
1.54 - "2 5 5 5\n"
1.55 - "3 2 6 5\n"
1.56 - "3 5 7 5\n"
1.57 - "3 6 8 5\n"
1.58 - "4 3 9 3\n"
1.59 - "5 7 10 3\n"
1.60 - "5 6 11 10\n"
1.61 - "5 8 12 10\n"
1.62 - "6 8 13 8\n"
1.63 - "8 9 14 20\n"
1.64 - "8 1 15 5\n"
1.65 - "9 5 16 5\n"
1.66 - "@attributes\n"
1.67 - "source 1\n"
1.68 - "target 8\n";
1.69 -
1.70 -void checkPreflowCompile()
1.71 -{
1.72 - typedef int VType;
1.73 - typedef concepts::Digraph Digraph;
1.74 -
1.75 - typedef Digraph::Node Node;
1.76 - typedef Digraph::Arc Arc;
1.77 - typedef concepts::ReadMap<Arc,VType> CapMap;
1.78 - typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
1.79 - typedef concepts::WriteMap<Node,bool> CutMap;
1.80 -
1.81 - typedef Elevator<Digraph, Digraph::Node> Elev;
1.82 - typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
1.83 -
1.84 - Digraph g;
1.85 - Node n;
1.86 - Arc e;
1.87 - CapMap cap;
1.88 - FlowMap flow;
1.89 - CutMap cut;
1.90 - VType v;
1.91 - bool b;
1.92 - ignore_unused_variable_warning(v,b);
1.93 -
1.94 - typedef Preflow<Digraph, CapMap>
1.95 - ::SetFlowMap<FlowMap>
1.96 - ::SetElevator<Elev>
1.97 - ::SetStandardElevator<LinkedElev>
1.98 - ::Create PreflowType;
1.99 - PreflowType preflow_test(g, cap, n, n);
1.100 - const PreflowType& const_preflow_test = preflow_test;
1.101 -
1.102 - const PreflowType::Elevator& elev = const_preflow_test.elevator();
1.103 - preflow_test.elevator(const_cast<PreflowType::Elevator&>(elev));
1.104 - PreflowType::Tolerance tol = const_preflow_test.tolerance();
1.105 - preflow_test.tolerance(tol);
1.106 -
1.107 - preflow_test
1.108 - .capacityMap(cap)
1.109 - .flowMap(flow)
1.110 - .source(n)
1.111 - .target(n);
1.112 -
1.113 - preflow_test.init();
1.114 - preflow_test.init(cap);
1.115 - preflow_test.startFirstPhase();
1.116 - preflow_test.startSecondPhase();
1.117 - preflow_test.run();
1.118 - preflow_test.runMinCut();
1.119 -
1.120 - v = const_preflow_test.flowValue();
1.121 - v = const_preflow_test.flow(e);
1.122 - const FlowMap& fm = const_preflow_test.flowMap();
1.123 - b = const_preflow_test.minCut(n);
1.124 - const_preflow_test.minCutMap(cut);
1.125 -
1.126 - ignore_unused_variable_warning(fm);
1.127 -}
1.128 -
1.129 -int cutValue (const SmartDigraph& g,
1.130 - const SmartDigraph::NodeMap<bool>& cut,
1.131 - const SmartDigraph::ArcMap<int>& cap) {
1.132 -
1.133 - int c=0;
1.134 - for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
1.135 - if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
1.136 - }
1.137 - return c;
1.138 -}
1.139 -
1.140 -bool checkFlow(const SmartDigraph& g,
1.141 - const SmartDigraph::ArcMap<int>& flow,
1.142 - const SmartDigraph::ArcMap<int>& cap,
1.143 - SmartDigraph::Node s, SmartDigraph::Node t) {
1.144 -
1.145 - for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
1.146 - if (flow[e] < 0 || flow[e] > cap[e]) return false;
1.147 - }
1.148 -
1.149 - for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
1.150 - if (n == s || n == t) continue;
1.151 - int sum = 0;
1.152 - for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
1.153 - sum += flow[e];
1.154 - }
1.155 - for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
1.156 - sum -= flow[e];
1.157 - }
1.158 - if (sum != 0) return false;
1.159 - }
1.160 - return true;
1.161 -}
1.162 -
1.163 -void initFlowTest()
1.164 -{
1.165 - DIGRAPH_TYPEDEFS(SmartDigraph);
1.166 -
1.167 - SmartDigraph g;
1.168 - SmartDigraph::ArcMap<int> cap(g),iflow(g);
1.169 - Node s=g.addNode(); Node t=g.addNode();
1.170 - Node n1=g.addNode(); Node n2=g.addNode();
1.171 - Arc a;
1.172 - a=g.addArc(s,n1); cap[a]=20; iflow[a]=20;
1.173 - a=g.addArc(n1,n2); cap[a]=10; iflow[a]=0;
1.174 - a=g.addArc(n2,t); cap[a]=20; iflow[a]=0;
1.175 -
1.176 - Preflow<SmartDigraph> pre(g,cap,s,t);
1.177 - pre.init(iflow);
1.178 - pre.startFirstPhase();
1.179 - check(pre.flowValue() == 10, "The incorrect max flow value.");
1.180 - check(pre.minCut(s), "Wrong min cut (Node s).");
1.181 - check(pre.minCut(n1), "Wrong min cut (Node n1).");
1.182 - check(!pre.minCut(n2), "Wrong min cut (Node n2).");
1.183 - check(!pre.minCut(t), "Wrong min cut (Node t).");
1.184 -}
1.185 -
1.186 -
1.187 -int main() {
1.188 -
1.189 - typedef SmartDigraph Digraph;
1.190 -
1.191 - typedef Digraph::Node Node;
1.192 - typedef Digraph::NodeIt NodeIt;
1.193 - typedef Digraph::ArcIt ArcIt;
1.194 - typedef Digraph::ArcMap<int> CapMap;
1.195 - typedef Digraph::ArcMap<int> FlowMap;
1.196 - typedef Digraph::NodeMap<bool> CutMap;
1.197 -
1.198 - typedef Preflow<Digraph, CapMap> PType;
1.199 -
1.200 - Digraph g;
1.201 - Node s, t;
1.202 - CapMap cap(g);
1.203 - std::istringstream input(test_lgf);
1.204 - DigraphReader<Digraph>(g,input).
1.205 - arcMap("capacity", cap).
1.206 - node("source",s).
1.207 - node("target",t).
1.208 - run();
1.209 -
1.210 - PType preflow_test(g, cap, s, t);
1.211 - preflow_test.run();
1.212 -
1.213 - check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
1.214 - "The flow is not feasible.");
1.215 -
1.216 - CutMap min_cut(g);
1.217 - preflow_test.minCutMap(min_cut);
1.218 - int min_cut_value=cutValue(g,min_cut,cap);
1.219 -
1.220 - check(preflow_test.flowValue() == min_cut_value,
1.221 - "The max flow value is not equal to the three min cut values.");
1.222 -
1.223 - FlowMap flow(g);
1.224 - for(ArcIt e(g); e!=INVALID; ++e) flow[e] = preflow_test.flowMap()[e];
1.225 -
1.226 - int flow_value=preflow_test.flowValue();
1.227 -
1.228 - for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e];
1.229 - preflow_test.init(flow);
1.230 - preflow_test.startFirstPhase();
1.231 -
1.232 - CutMap min_cut1(g);
1.233 - preflow_test.minCutMap(min_cut1);
1.234 - min_cut_value=cutValue(g,min_cut1,cap);
1.235 -
1.236 - check(preflow_test.flowValue() == min_cut_value &&
1.237 - min_cut_value == 2*flow_value,
1.238 - "The max flow value or the min cut value is wrong.");
1.239 -
1.240 - preflow_test.startSecondPhase();
1.241 -
1.242 - check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
1.243 - "The flow is not feasible.");
1.244 -
1.245 - CutMap min_cut2(g);
1.246 - preflow_test.minCutMap(min_cut2);
1.247 - min_cut_value=cutValue(g,min_cut2,cap);
1.248 -
1.249 - check(preflow_test.flowValue() == min_cut_value &&
1.250 - min_cut_value == 2*flow_value,
1.251 - "The max flow value or the three min cut values were not doubled");
1.252 -
1.253 -
1.254 - preflow_test.flowMap(flow);
1.255 -
1.256 - NodeIt tmp1(g,s);
1.257 - ++tmp1;
1.258 - if ( tmp1 != INVALID ) s=tmp1;
1.259 -
1.260 - NodeIt tmp2(g,t);
1.261 - ++tmp2;
1.262 - if ( tmp2 != INVALID ) t=tmp2;
1.263 -
1.264 - preflow_test.source(s);
1.265 - preflow_test.target(t);
1.266 -
1.267 - preflow_test.run();
1.268 -
1.269 - CutMap min_cut3(g);
1.270 - preflow_test.minCutMap(min_cut3);
1.271 - min_cut_value=cutValue(g,min_cut3,cap);
1.272 -
1.273 -
1.274 - check(preflow_test.flowValue() == min_cut_value,
1.275 - "The max flow value or the three min cut values are incorrect.");
1.276 -
1.277 - initFlowTest();
1.278 -
1.279 - return 0;
1.280 -}