alpar@404
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
alpar@404
|
2 |
*
|
alpar@404
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
alpar@404
|
4 |
*
|
alpar@463
|
5 |
* Copyright (C) 2003-2009
|
alpar@404
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@404
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@404
|
8 |
*
|
alpar@404
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@404
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@404
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@404
|
12 |
*
|
alpar@404
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@404
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@404
|
15 |
* purpose.
|
alpar@404
|
16 |
*
|
alpar@404
|
17 |
*/
|
alpar@404
|
18 |
|
alpar@442
|
19 |
#include <iostream>
|
alpar@404
|
20 |
|
alpar@404
|
21 |
#include "test_tools.h"
|
alpar@404
|
22 |
#include <lemon/smart_graph.h>
|
alpar@404
|
23 |
#include <lemon/preflow.h>
|
alpar@404
|
24 |
#include <lemon/concepts/digraph.h>
|
alpar@404
|
25 |
#include <lemon/concepts/maps.h>
|
alpar@404
|
26 |
#include <lemon/lgf_reader.h>
|
kpeter@409
|
27 |
#include <lemon/elevator.h>
|
alpar@404
|
28 |
|
alpar@404
|
29 |
using namespace lemon;
|
alpar@404
|
30 |
|
alpar@442
|
31 |
char test_lgf[] =
|
alpar@442
|
32 |
"@nodes\n"
|
alpar@442
|
33 |
"label\n"
|
alpar@442
|
34 |
"0\n"
|
alpar@442
|
35 |
"1\n"
|
alpar@442
|
36 |
"2\n"
|
alpar@442
|
37 |
"3\n"
|
alpar@442
|
38 |
"4\n"
|
alpar@442
|
39 |
"5\n"
|
alpar@442
|
40 |
"6\n"
|
alpar@442
|
41 |
"7\n"
|
alpar@442
|
42 |
"8\n"
|
alpar@442
|
43 |
"9\n"
|
alpar@442
|
44 |
"@arcs\n"
|
alpar@442
|
45 |
" label capacity\n"
|
alpar@442
|
46 |
"0 1 0 20\n"
|
alpar@442
|
47 |
"0 2 1 0\n"
|
alpar@442
|
48 |
"1 1 2 3\n"
|
alpar@442
|
49 |
"1 2 3 8\n"
|
alpar@442
|
50 |
"1 3 4 8\n"
|
alpar@442
|
51 |
"2 5 5 5\n"
|
alpar@442
|
52 |
"3 2 6 5\n"
|
alpar@442
|
53 |
"3 5 7 5\n"
|
alpar@442
|
54 |
"3 6 8 5\n"
|
alpar@442
|
55 |
"4 3 9 3\n"
|
alpar@442
|
56 |
"5 7 10 3\n"
|
alpar@442
|
57 |
"5 6 11 10\n"
|
alpar@442
|
58 |
"5 8 12 10\n"
|
alpar@442
|
59 |
"6 8 13 8\n"
|
alpar@442
|
60 |
"8 9 14 20\n"
|
alpar@442
|
61 |
"8 1 15 5\n"
|
alpar@442
|
62 |
"9 5 16 5\n"
|
alpar@442
|
63 |
"@attributes\n"
|
alpar@442
|
64 |
"source 1\n"
|
alpar@442
|
65 |
"target 8\n";
|
alpar@442
|
66 |
|
kpeter@409
|
67 |
void checkPreflowCompile()
|
alpar@404
|
68 |
{
|
alpar@404
|
69 |
typedef int VType;
|
alpar@404
|
70 |
typedef concepts::Digraph Digraph;
|
alpar@404
|
71 |
|
alpar@404
|
72 |
typedef Digraph::Node Node;
|
alpar@404
|
73 |
typedef Digraph::Arc Arc;
|
alpar@404
|
74 |
typedef concepts::ReadMap<Arc,VType> CapMap;
|
alpar@404
|
75 |
typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
|
alpar@404
|
76 |
typedef concepts::WriteMap<Node,bool> CutMap;
|
alpar@404
|
77 |
|
kpeter@409
|
78 |
typedef Elevator<Digraph, Digraph::Node> Elev;
|
kpeter@409
|
79 |
typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
|
kpeter@409
|
80 |
|
alpar@404
|
81 |
Digraph g;
|
alpar@404
|
82 |
Node n;
|
alpar@404
|
83 |
Arc e;
|
alpar@404
|
84 |
CapMap cap;
|
alpar@404
|
85 |
FlowMap flow;
|
alpar@404
|
86 |
CutMap cut;
|
kpeter@632
|
87 |
VType v;
|
kpeter@632
|
88 |
bool b;
|
alpar@404
|
89 |
|
kpeter@632
|
90 |
typedef Preflow<Digraph, CapMap>
|
kpeter@632
|
91 |
::SetFlowMap<FlowMap>
|
kpeter@632
|
92 |
::SetElevator<Elev>
|
kpeter@632
|
93 |
::SetStandardElevator<LinkedElev>
|
kpeter@632
|
94 |
::Create PreflowType;
|
kpeter@632
|
95 |
PreflowType preflow_test(g, cap, n, n);
|
kpeter@632
|
96 |
const PreflowType& const_preflow_test = preflow_test;
|
alpar@404
|
97 |
|
kpeter@632
|
98 |
preflow_test
|
kpeter@632
|
99 |
.capacityMap(cap)
|
kpeter@632
|
100 |
.flowMap(flow)
|
kpeter@632
|
101 |
.source(n)
|
kpeter@632
|
102 |
.target(n);
|
alpar@404
|
103 |
|
alpar@404
|
104 |
preflow_test.init();
|
kpeter@407
|
105 |
preflow_test.init(cap);
|
alpar@404
|
106 |
preflow_test.startFirstPhase();
|
alpar@404
|
107 |
preflow_test.startSecondPhase();
|
alpar@404
|
108 |
preflow_test.run();
|
alpar@404
|
109 |
preflow_test.runMinCut();
|
alpar@404
|
110 |
|
kpeter@632
|
111 |
v = const_preflow_test.flowValue();
|
kpeter@632
|
112 |
v = const_preflow_test.flow(e);
|
kpeter@632
|
113 |
const FlowMap& fm = const_preflow_test.flowMap();
|
kpeter@632
|
114 |
b = const_preflow_test.minCut(n);
|
kpeter@632
|
115 |
const_preflow_test.minCutMap(cut);
|
kpeter@632
|
116 |
|
kpeter@632
|
117 |
ignore_unused_variable_warning(fm);
|
alpar@404
|
118 |
}
|
alpar@404
|
119 |
|
alpar@404
|
120 |
int cutValue (const SmartDigraph& g,
|
alpar@404
|
121 |
const SmartDigraph::NodeMap<bool>& cut,
|
alpar@404
|
122 |
const SmartDigraph::ArcMap<int>& cap) {
|
alpar@404
|
123 |
|
alpar@404
|
124 |
int c=0;
|
alpar@404
|
125 |
for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
|
alpar@404
|
126 |
if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
|
alpar@404
|
127 |
}
|
alpar@404
|
128 |
return c;
|
alpar@404
|
129 |
}
|
alpar@404
|
130 |
|
alpar@404
|
131 |
bool checkFlow(const SmartDigraph& g,
|
alpar@404
|
132 |
const SmartDigraph::ArcMap<int>& flow,
|
alpar@404
|
133 |
const SmartDigraph::ArcMap<int>& cap,
|
alpar@404
|
134 |
SmartDigraph::Node s, SmartDigraph::Node t) {
|
alpar@404
|
135 |
|
alpar@404
|
136 |
for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
|
alpar@404
|
137 |
if (flow[e] < 0 || flow[e] > cap[e]) return false;
|
alpar@404
|
138 |
}
|
alpar@404
|
139 |
|
alpar@404
|
140 |
for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
|
alpar@404
|
141 |
if (n == s || n == t) continue;
|
alpar@404
|
142 |
int sum = 0;
|
alpar@404
|
143 |
for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
|
alpar@404
|
144 |
sum += flow[e];
|
alpar@404
|
145 |
}
|
alpar@404
|
146 |
for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
|
alpar@404
|
147 |
sum -= flow[e];
|
alpar@404
|
148 |
}
|
alpar@404
|
149 |
if (sum != 0) return false;
|
alpar@404
|
150 |
}
|
alpar@404
|
151 |
return true;
|
alpar@404
|
152 |
}
|
alpar@404
|
153 |
|
alpar@1027
|
154 |
void initFlowTest()
|
alpar@1027
|
155 |
{
|
alpar@1027
|
156 |
DIGRAPH_TYPEDEFS(SmartDigraph);
|
alpar@1027
|
157 |
|
alpar@1027
|
158 |
SmartDigraph g;
|
alpar@1027
|
159 |
SmartDigraph::ArcMap<int> cap(g),iflow(g);
|
alpar@1027
|
160 |
Node s=g.addNode(); Node t=g.addNode();
|
alpar@1027
|
161 |
Node n1=g.addNode(); Node n2=g.addNode();
|
alpar@1027
|
162 |
Arc a;
|
alpar@1027
|
163 |
a=g.addArc(s,n1); cap[a]=20; iflow[a]=20;
|
alpar@1027
|
164 |
a=g.addArc(n1,n2); cap[a]=10; iflow[a]=0;
|
alpar@1027
|
165 |
a=g.addArc(n2,t); cap[a]=20; iflow[a]=0;
|
alpar@1027
|
166 |
|
alpar@1027
|
167 |
Preflow<SmartDigraph> pre(g,cap,s,t);
|
alpar@1027
|
168 |
pre.init(iflow);
|
alpar@1027
|
169 |
pre.startFirstPhase();
|
alpar@1027
|
170 |
check(pre.flowValue() == 10, "The incorrect max flow value.");
|
alpar@1027
|
171 |
check(pre.minCut(s), "Wrong min cut (Node s).");
|
alpar@1027
|
172 |
check(pre.minCut(n1), "Wrong min cut (Node n1).");
|
alpar@1027
|
173 |
check(!pre.minCut(n2), "Wrong min cut (Node n2).");
|
alpar@1027
|
174 |
check(!pre.minCut(t), "Wrong min cut (Node t).");
|
alpar@1027
|
175 |
}
|
alpar@1027
|
176 |
|
alpar@1027
|
177 |
|
alpar@404
|
178 |
int main() {
|
alpar@404
|
179 |
|
alpar@404
|
180 |
typedef SmartDigraph Digraph;
|
alpar@404
|
181 |
|
alpar@404
|
182 |
typedef Digraph::Node Node;
|
alpar@404
|
183 |
typedef Digraph::NodeIt NodeIt;
|
alpar@404
|
184 |
typedef Digraph::ArcIt ArcIt;
|
alpar@404
|
185 |
typedef Digraph::ArcMap<int> CapMap;
|
alpar@404
|
186 |
typedef Digraph::ArcMap<int> FlowMap;
|
alpar@404
|
187 |
typedef Digraph::NodeMap<bool> CutMap;
|
alpar@404
|
188 |
|
alpar@404
|
189 |
typedef Preflow<Digraph, CapMap> PType;
|
alpar@404
|
190 |
|
alpar@404
|
191 |
Digraph g;
|
alpar@404
|
192 |
Node s, t;
|
alpar@404
|
193 |
CapMap cap(g);
|
alpar@442
|
194 |
std::istringstream input(test_lgf);
|
alpar@442
|
195 |
DigraphReader<Digraph>(g,input).
|
alpar@404
|
196 |
arcMap("capacity", cap).
|
alpar@404
|
197 |
node("source",s).
|
alpar@404
|
198 |
node("target",t).
|
alpar@404
|
199 |
run();
|
alpar@404
|
200 |
|
alpar@404
|
201 |
PType preflow_test(g, cap, s, t);
|
alpar@404
|
202 |
preflow_test.run();
|
alpar@404
|
203 |
|
alpar@404
|
204 |
check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
|
alpar@404
|
205 |
"The flow is not feasible.");
|
alpar@404
|
206 |
|
alpar@404
|
207 |
CutMap min_cut(g);
|
alpar@404
|
208 |
preflow_test.minCutMap(min_cut);
|
alpar@404
|
209 |
int min_cut_value=cutValue(g,min_cut,cap);
|
alpar@404
|
210 |
|
alpar@404
|
211 |
check(preflow_test.flowValue() == min_cut_value,
|
alpar@404
|
212 |
"The max flow value is not equal to the three min cut values.");
|
alpar@404
|
213 |
|
alpar@404
|
214 |
FlowMap flow(g);
|
alpar@404
|
215 |
for(ArcIt e(g); e!=INVALID; ++e) flow[e] = preflow_test.flowMap()[e];
|
alpar@404
|
216 |
|
alpar@404
|
217 |
int flow_value=preflow_test.flowValue();
|
alpar@404
|
218 |
|
alpar@404
|
219 |
for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e];
|
kpeter@407
|
220 |
preflow_test.init(flow);
|
alpar@404
|
221 |
preflow_test.startFirstPhase();
|
alpar@404
|
222 |
|
alpar@404
|
223 |
CutMap min_cut1(g);
|
alpar@404
|
224 |
preflow_test.minCutMap(min_cut1);
|
alpar@404
|
225 |
min_cut_value=cutValue(g,min_cut1,cap);
|
alpar@404
|
226 |
|
alpar@404
|
227 |
check(preflow_test.flowValue() == min_cut_value &&
|
alpar@404
|
228 |
min_cut_value == 2*flow_value,
|
alpar@404
|
229 |
"The max flow value or the min cut value is wrong.");
|
alpar@404
|
230 |
|
alpar@404
|
231 |
preflow_test.startSecondPhase();
|
alpar@404
|
232 |
|
alpar@404
|
233 |
check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
|
alpar@404
|
234 |
"The flow is not feasible.");
|
alpar@404
|
235 |
|
alpar@404
|
236 |
CutMap min_cut2(g);
|
alpar@404
|
237 |
preflow_test.minCutMap(min_cut2);
|
alpar@404
|
238 |
min_cut_value=cutValue(g,min_cut2,cap);
|
alpar@404
|
239 |
|
alpar@404
|
240 |
check(preflow_test.flowValue() == min_cut_value &&
|
alpar@404
|
241 |
min_cut_value == 2*flow_value,
|
alpar@404
|
242 |
"The max flow value or the three min cut values were not doubled");
|
alpar@404
|
243 |
|
alpar@404
|
244 |
|
alpar@404
|
245 |
preflow_test.flowMap(flow);
|
alpar@404
|
246 |
|
alpar@404
|
247 |
NodeIt tmp1(g,s);
|
alpar@404
|
248 |
++tmp1;
|
alpar@404
|
249 |
if ( tmp1 != INVALID ) s=tmp1;
|
alpar@404
|
250 |
|
alpar@404
|
251 |
NodeIt tmp2(g,t);
|
alpar@404
|
252 |
++tmp2;
|
alpar@404
|
253 |
if ( tmp2 != INVALID ) t=tmp2;
|
alpar@404
|
254 |
|
alpar@404
|
255 |
preflow_test.source(s);
|
alpar@404
|
256 |
preflow_test.target(t);
|
alpar@404
|
257 |
|
alpar@404
|
258 |
preflow_test.run();
|
alpar@404
|
259 |
|
alpar@404
|
260 |
CutMap min_cut3(g);
|
alpar@404
|
261 |
preflow_test.minCutMap(min_cut3);
|
alpar@404
|
262 |
min_cut_value=cutValue(g,min_cut3,cap);
|
alpar@404
|
263 |
|
alpar@404
|
264 |
|
alpar@404
|
265 |
check(preflow_test.flowValue() == min_cut_value,
|
alpar@404
|
266 |
"The max flow value or the three min cut values are incorrect.");
|
alpar@404
|
267 |
|
alpar@1027
|
268 |
initFlowTest();
|
alpar@1027
|
269 |
|
alpar@404
|
270 |
return 0;
|
alpar@404
|
271 |
}
|