1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
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/edmonds_karp.h>
24 #include <lemon/concepts/digraph.h>
25 #include <lemon/concepts/maps.h>
26 #include <lemon/lgf_reader.h>
28 using namespace lemon;
66 void checkEdmondKarpCompile() {
68 typedef concepts::Digraph Digraph;
70 typedef Digraph::Node Node;
71 typedef Digraph::Arc Arc;
72 typedef concepts::ReadMap<Arc,VType> CapMap;
73 typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
74 typedef concepts::WriteMap<Node,bool> CutMap;
84 ignore_unused_variable_warning(v,b);
85 typedef EdmondsKarp<Digraph, CapMap>
88 EKType ek_test(g, cap, n, n);
89 const EKType& const_ek_test = ek_test;
91 EKType::Tolerance tol = const_ek_test.tolerance();
92 ek_test.tolerance(tol);
103 v = const_ek_test.flowValue();
104 v = const_ek_test.flow(e);
106 const FlowMap& fm = const_ek_test.flowMap();
107 b = const_ek_test.minCut(n);
108 const_ek_test.minCutMap(cut);
110 ignore_unused_variable_warning(fm);
113 int cutValue (const SmartDigraph& g,
114 const SmartDigraph::NodeMap<bool>& cut,
115 const SmartDigraph::ArcMap<int>& cap) {
118 for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
119 if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
124 bool checkFlow(const SmartDigraph& g,
125 const SmartDigraph::ArcMap<int>& flow,
126 const SmartDigraph::ArcMap<int>& cap,
127 SmartDigraph::Node s, SmartDigraph::Node t) {
129 for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
130 if (flow[e] < 0 || flow[e] > cap[e]) return false;
133 for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
134 if (n == s || n == t) continue;
136 for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
139 for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
142 if (sum != 0) return false;
149 typedef SmartDigraph Digraph;
151 typedef Digraph::Node Node;
152 typedef Digraph::NodeIt NodeIt;
153 typedef Digraph::ArcIt ArcIt;
154 typedef Digraph::ArcMap<int> CapMap;
155 typedef Digraph::ArcMap<int> FlowMap;
156 typedef Digraph::NodeMap<bool> CutMap;
158 typedef EdmondsKarp<Digraph, CapMap> EKType;
163 std::istringstream input(test_lgf);
164 DigraphReader<Digraph>(g,input).
165 arcMap("capacity", cap).
170 EKType ek_test(g, cap, s, t);
173 check(checkFlow(g, ek_test.flowMap(), cap, s, t),
174 "The flow is not feasible.");
177 ek_test.minCutMap(min_cut);
178 int min_cut_value=cutValue(g,min_cut,cap);
180 check(ek_test.flowValue() == min_cut_value,
181 "The max flow value is not equal to the three min cut values.");
184 for(ArcIt e(g); e!=INVALID; ++e) flow[e] = ek_test.flowMap()[e];
186 int flow_value=ek_test.flowValue();
188 for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e];
189 ek_test.flowInit(flow);
193 ek_test.minCutMap(min_cut1);
194 min_cut_value=cutValue(g,min_cut1,cap);
196 check(ek_test.flowValue() == min_cut_value &&
197 min_cut_value == 2*flow_value,
198 "The max flow value or the min cut value is wrong.");
200 check(checkFlow(g, ek_test.flowMap(), cap, s, t),
201 "The flow is not feasible.");
204 ek_test.minCutMap(min_cut2);
205 min_cut_value=cutValue(g,min_cut2,cap);
207 check(ek_test.flowValue() == min_cut_value &&
208 min_cut_value == 2*flow_value,
209 "The max flow value or the three min cut values were not doubled.");
212 ek_test.flowMap(flow);
216 if ( tmp1 != INVALID ) s=tmp1;
220 if ( tmp2 != INVALID ) t=tmp2;
228 ek_test.minCutMap(min_cut3);
229 min_cut_value=cutValue(g,min_cut3,cap);
232 check(ek_test.flowValue() == min_cut_value,
233 "The max flow value or the three min cut values are incorrect.");