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