1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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;
89 ignore_unused_variable_warning(v,b);
91 typedef Preflow<Digraph, CapMap>
94 ::SetStandardElevator<LinkedElev>
96 PreflowType preflow_test(g, cap, n, n);
97 const PreflowType& const_preflow_test = preflow_test;
106 preflow_test.init(cap);
107 preflow_test.startFirstPhase();
108 preflow_test.startSecondPhase();
110 preflow_test.runMinCut();
112 v = const_preflow_test.flowValue();
113 v = const_preflow_test.flow(e);
114 const FlowMap& fm = const_preflow_test.flowMap();
115 b = const_preflow_test.minCut(n);
116 const_preflow_test.minCutMap(cut);
118 ignore_unused_variable_warning(fm);
121 int cutValue (const SmartDigraph& g,
122 const SmartDigraph::NodeMap<bool>& cut,
123 const SmartDigraph::ArcMap<int>& cap) {
126 for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
127 if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
132 bool checkFlow(const SmartDigraph& g,
133 const SmartDigraph::ArcMap<int>& flow,
134 const SmartDigraph::ArcMap<int>& cap,
135 SmartDigraph::Node s, SmartDigraph::Node t) {
137 for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
138 if (flow[e] < 0 || flow[e] > cap[e]) return false;
141 for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
142 if (n == s || n == t) continue;
144 for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
147 for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
150 if (sum != 0) return false;
157 DIGRAPH_TYPEDEFS(SmartDigraph);
160 SmartDigraph::ArcMap<int> cap(g),iflow(g);
161 Node s=g.addNode(); Node t=g.addNode();
162 Node n1=g.addNode(); Node n2=g.addNode();
164 a=g.addArc(s,n1); cap[a]=20; iflow[a]=20;
165 a=g.addArc(n1,n2); cap[a]=10; iflow[a]=0;
166 a=g.addArc(n2,t); cap[a]=20; iflow[a]=0;
168 Preflow<SmartDigraph> pre(g,cap,s,t);
170 pre.startFirstPhase();
171 check(pre.flowValue() == 10, "The incorrect max flow value.");
172 check(pre.minCut(s), "Wrong min cut (Node s).");
173 check(pre.minCut(n1), "Wrong min cut (Node n1).");
174 check(!pre.minCut(n2), "Wrong min cut (Node n2).");
175 check(!pre.minCut(t), "Wrong min cut (Node t).");
181 typedef SmartDigraph Digraph;
183 typedef Digraph::Node Node;
184 typedef Digraph::NodeIt NodeIt;
185 typedef Digraph::ArcIt ArcIt;
186 typedef Digraph::ArcMap<int> CapMap;
187 typedef Digraph::ArcMap<int> FlowMap;
188 typedef Digraph::NodeMap<bool> CutMap;
190 typedef Preflow<Digraph, CapMap> PType;
195 std::istringstream input(test_lgf);
196 DigraphReader<Digraph>(g,input).
197 arcMap("capacity", cap).
202 PType preflow_test(g, cap, s, t);
205 check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
206 "The flow is not feasible.");
209 preflow_test.minCutMap(min_cut);
210 int min_cut_value=cutValue(g,min_cut,cap);
212 check(preflow_test.flowValue() == min_cut_value,
213 "The max flow value is not equal to the three min cut values.");
216 for(ArcIt e(g); e!=INVALID; ++e) flow[e] = preflow_test.flowMap()[e];
218 int flow_value=preflow_test.flowValue();
220 for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e];
221 preflow_test.init(flow);
222 preflow_test.startFirstPhase();
225 preflow_test.minCutMap(min_cut1);
226 min_cut_value=cutValue(g,min_cut1,cap);
228 check(preflow_test.flowValue() == min_cut_value &&
229 min_cut_value == 2*flow_value,
230 "The max flow value or the min cut value is wrong.");
232 preflow_test.startSecondPhase();
234 check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
235 "The flow is not feasible.");
238 preflow_test.minCutMap(min_cut2);
239 min_cut_value=cutValue(g,min_cut2,cap);
241 check(preflow_test.flowValue() == min_cut_value &&
242 min_cut_value == 2*flow_value,
243 "The max flow value or the three min cut values were not doubled");
246 preflow_test.flowMap(flow);
250 if ( tmp1 != INVALID ) s=tmp1;
254 if ( tmp2 != INVALID ) t=tmp2;
256 preflow_test.source(s);
257 preflow_test.target(t);
262 preflow_test.minCutMap(min_cut3);
263 min_cut_value=cutValue(g,min_cut3,cap);
266 check(preflow_test.flowValue() == min_cut_value,
267 "The max flow value or the three min cut values are incorrect.");