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;
90 typedef Preflow<Digraph, CapMap>
93 ::SetStandardElevator<LinkedElev>
95 PreflowType preflow_test(g, cap, n, n);
96 const PreflowType& const_preflow_test = preflow_test;
105 preflow_test.init(cap);
106 preflow_test.startFirstPhase();
107 preflow_test.startSecondPhase();
109 preflow_test.runMinCut();
111 v = const_preflow_test.flowValue();
112 v = const_preflow_test.flow(e);
113 const FlowMap& fm = const_preflow_test.flowMap();
114 b = const_preflow_test.minCut(n);
115 const_preflow_test.minCutMap(cut);
117 ignore_unused_variable_warning(fm);
120 int cutValue (const SmartDigraph& g,
121 const SmartDigraph::NodeMap<bool>& cut,
122 const SmartDigraph::ArcMap<int>& cap) {
125 for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
126 if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
131 bool checkFlow(const SmartDigraph& g,
132 const SmartDigraph::ArcMap<int>& flow,
133 const SmartDigraph::ArcMap<int>& cap,
134 SmartDigraph::Node s, SmartDigraph::Node t) {
136 for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
137 if (flow[e] < 0 || flow[e] > cap[e]) return false;
140 for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
141 if (n == s || n == t) continue;
143 for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
146 for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
149 if (sum != 0) return false;
156 DIGRAPH_TYPEDEFS(SmartDigraph);
159 SmartDigraph::ArcMap<int> cap(g),iflow(g);
160 Node s=g.addNode(); Node t=g.addNode();
161 Node n1=g.addNode(); Node n2=g.addNode();
163 a=g.addArc(s,n1); cap[a]=20; iflow[a]=20;
164 a=g.addArc(n1,n2); cap[a]=10; iflow[a]=0;
165 a=g.addArc(n2,t); cap[a]=20; iflow[a]=0;
167 Preflow<SmartDigraph> pre(g,cap,s,t);
169 pre.startFirstPhase();
170 check(pre.flowValue() == 10, "The incorrect max flow value.");
171 check(pre.minCut(s), "Wrong min cut (Node s).");
172 check(pre.minCut(n1), "Wrong min cut (Node n1).");
173 check(!pre.minCut(n2), "Wrong min cut (Node n2).");
174 check(!pre.minCut(t), "Wrong min cut (Node t).");
180 typedef SmartDigraph Digraph;
182 typedef Digraph::Node Node;
183 typedef Digraph::NodeIt NodeIt;
184 typedef Digraph::ArcIt ArcIt;
185 typedef Digraph::ArcMap<int> CapMap;
186 typedef Digraph::ArcMap<int> FlowMap;
187 typedef Digraph::NodeMap<bool> CutMap;
189 typedef Preflow<Digraph, CapMap> PType;
194 std::istringstream input(test_lgf);
195 DigraphReader<Digraph>(g,input).
196 arcMap("capacity", cap).
201 PType preflow_test(g, cap, s, t);
204 check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
205 "The flow is not feasible.");
208 preflow_test.minCutMap(min_cut);
209 int min_cut_value=cutValue(g,min_cut,cap);
211 check(preflow_test.flowValue() == min_cut_value,
212 "The max flow value is not equal to the three min cut values.");
215 for(ArcIt e(g); e!=INVALID; ++e) flow[e] = preflow_test.flowMap()[e];
217 int flow_value=preflow_test.flowValue();
219 for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e];
220 preflow_test.init(flow);
221 preflow_test.startFirstPhase();
224 preflow_test.minCutMap(min_cut1);
225 min_cut_value=cutValue(g,min_cut1,cap);
227 check(preflow_test.flowValue() == min_cut_value &&
228 min_cut_value == 2*flow_value,
229 "The max flow value or the min cut value is wrong.");
231 preflow_test.startSecondPhase();
233 check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
234 "The flow is not feasible.");
237 preflow_test.minCutMap(min_cut2);
238 min_cut_value=cutValue(g,min_cut2,cap);
240 check(preflow_test.flowValue() == min_cut_value &&
241 min_cut_value == 2*flow_value,
242 "The max flow value or the three min cut values were not doubled");
245 preflow_test.flowMap(flow);
249 if ( tmp1 != INVALID ) s=tmp1;
253 if ( tmp2 != INVALID ) t=tmp2;
255 preflow_test.source(s);
256 preflow_test.target(t);
261 preflow_test.minCutMap(min_cut3);
262 min_cut_value=cutValue(g,min_cut3,cap);
265 check(preflow_test.flowValue() == min_cut_value,
266 "The max flow value or the three min cut values are incorrect.");