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