1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
21 #include "test_tools.h"
22 #include <lemon/smart_graph.h>
23 #include <lemon/preflow.h>
24 #include <lemon/concepts/digraph.h>
25 #include <lemon/concepts/maps.h>
26 #include <lemon/lgf_reader.h>
27 #include <lemon/elevator.h>
29 using namespace lemon;
67 void checkPreflowCompile()
70 typedef concepts::Digraph Digraph;
72 typedef Digraph::Node Node;
73 typedef Digraph::Arc Arc;
74 typedef concepts::ReadMap<Arc,VType> CapMap;
75 typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
76 typedef concepts::WriteMap<Node,bool> CutMap;
78 typedef Elevator<Digraph, Digraph::Node> Elev;
79 typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
90 typedef Preflow<Digraph, CapMap>
93 ::SetStandardElevator<LinkedElev>
95 PreflowType preflow_test(g, cap, n, n);
96 const PreflowType& const_preflow_test = preflow_test;
98 const PreflowType::Elevator& elev = const_preflow_test.elevator();
99 preflow_test.elevator(const_cast<PreflowType::Elevator&>(elev));
100 PreflowType::Tolerance tol = const_preflow_test.tolerance();
101 preflow_test.tolerance(tol);
110 preflow_test.init(cap);
111 preflow_test.startFirstPhase();
112 preflow_test.startSecondPhase();
114 preflow_test.runMinCut();
116 v = const_preflow_test.flowValue();
117 v = const_preflow_test.flow(e);
118 const FlowMap& fm = const_preflow_test.flowMap();
119 b = const_preflow_test.minCut(n);
120 const_preflow_test.minCutMap(cut);
122 ignore_unused_variable_warning(fm);
125 int cutValue (const SmartDigraph& g,
126 const SmartDigraph::NodeMap<bool>& cut,
127 const SmartDigraph::ArcMap<int>& cap) {
130 for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
131 if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
136 bool checkFlow(const SmartDigraph& g,
137 const SmartDigraph::ArcMap<int>& flow,
138 const SmartDigraph::ArcMap<int>& cap,
139 SmartDigraph::Node s, SmartDigraph::Node t) {
141 for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
142 if (flow[e] < 0 || flow[e] > cap[e]) return false;
145 for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
146 if (n == s || n == t) continue;
148 for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
151 for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
154 if (sum != 0) return false;
161 DIGRAPH_TYPEDEFS(SmartDigraph);
164 SmartDigraph::ArcMap<int> cap(g),iflow(g);
165 Node s=g.addNode(); Node t=g.addNode();
166 Node n1=g.addNode(); Node n2=g.addNode();
168 a=g.addArc(s,n1); cap[a]=20; iflow[a]=20;
169 a=g.addArc(n1,n2); cap[a]=10; iflow[a]=0;
170 a=g.addArc(n2,t); cap[a]=20; iflow[a]=0;
172 Preflow<SmartDigraph> pre(g,cap,s,t);
174 pre.startFirstPhase();
175 check(pre.flowValue() == 10, "The incorrect max flow value.");
176 check(pre.minCut(s), "Wrong min cut (Node s).");
177 check(pre.minCut(n1), "Wrong min cut (Node n1).");
178 check(!pre.minCut(n2), "Wrong min cut (Node n2).");
179 check(!pre.minCut(t), "Wrong min cut (Node t).");
185 typedef SmartDigraph Digraph;
187 typedef Digraph::Node Node;
188 typedef Digraph::NodeIt NodeIt;
189 typedef Digraph::ArcIt ArcIt;
190 typedef Digraph::ArcMap<int> CapMap;
191 typedef Digraph::ArcMap<int> FlowMap;
192 typedef Digraph::NodeMap<bool> CutMap;
194 typedef Preflow<Digraph, CapMap> PType;
199 std::istringstream input(test_lgf);
200 DigraphReader<Digraph>(g,input).
201 arcMap("capacity", cap).
206 PType preflow_test(g, cap, s, t);
209 check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
210 "The flow is not feasible.");
213 preflow_test.minCutMap(min_cut);
214 int min_cut_value=cutValue(g,min_cut,cap);
216 check(preflow_test.flowValue() == min_cut_value,
217 "The max flow value is not equal to the three min cut values.");
220 for(ArcIt e(g); e!=INVALID; ++e) flow[e] = preflow_test.flowMap()[e];
222 int flow_value=preflow_test.flowValue();
224 for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e];
225 preflow_test.init(flow);
226 preflow_test.startFirstPhase();
229 preflow_test.minCutMap(min_cut1);
230 min_cut_value=cutValue(g,min_cut1,cap);
232 check(preflow_test.flowValue() == min_cut_value &&
233 min_cut_value == 2*flow_value,
234 "The max flow value or the min cut value is wrong.");
236 preflow_test.startSecondPhase();
238 check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
239 "The flow is not feasible.");
242 preflow_test.minCutMap(min_cut2);
243 min_cut_value=cutValue(g,min_cut2,cap);
245 check(preflow_test.flowValue() == min_cut_value &&
246 min_cut_value == 2*flow_value,
247 "The max flow value or the three min cut values were not doubled");
250 preflow_test.flowMap(flow);
254 if ( tmp1 != INVALID ) s=tmp1;
258 if ( tmp2 != INVALID ) t=tmp2;
260 preflow_test.source(s);
261 preflow_test.target(t);
266 preflow_test.minCutMap(min_cut3);
267 min_cut_value=cutValue(g,min_cut3,cap);
270 check(preflow_test.flowValue() == min_cut_value,
271 "The max flow value or the three min cut values are incorrect.");