1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
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
22 #include "test_tools.h"
23 #include <lemon/smart_graph.h>
24 #include <lemon/preflow.h>
25 #include <lemon/concepts/digraph.h>
26 #include <lemon/concepts/maps.h>
27 #include <lemon/lgf_reader.h>
29 using namespace lemon;
34 typedef concepts::Digraph Digraph;
36 typedef Digraph::Node Node;
37 typedef Digraph::Arc Arc;
38 typedef concepts::ReadMap<Arc,VType> CapMap;
39 typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
40 typedef concepts::WriteMap<Node,bool> CutMap;
49 Preflow<Digraph, CapMap>::SetFlowMap<FlowMap>::Create preflow_test(g,cap,n,n);
51 preflow_test.capacityMap(cap);
52 flow = preflow_test.flowMap();
53 preflow_test.flowMap(flow);
54 preflow_test.source(n);
55 preflow_test.target(n);
58 preflow_test.init(cap);
59 preflow_test.startFirstPhase();
60 preflow_test.startSecondPhase();
62 preflow_test.runMinCut();
64 preflow_test.flowValue();
65 preflow_test.minCut(n);
66 preflow_test.minCutMap(cut);
71 int cutValue (const SmartDigraph& g,
72 const SmartDigraph::NodeMap<bool>& cut,
73 const SmartDigraph::ArcMap<int>& cap) {
76 for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
77 if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
82 bool checkFlow(const SmartDigraph& g,
83 const SmartDigraph::ArcMap<int>& flow,
84 const SmartDigraph::ArcMap<int>& cap,
85 SmartDigraph::Node s, SmartDigraph::Node t) {
87 for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
88 if (flow[e] < 0 || flow[e] > cap[e]) return false;
91 for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
92 if (n == s || n == t) continue;
94 for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
97 for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
100 if (sum != 0) return false;
107 typedef SmartDigraph Digraph;
109 typedef Digraph::Node Node;
110 typedef Digraph::NodeIt NodeIt;
111 typedef Digraph::ArcIt ArcIt;
112 typedef Digraph::ArcMap<int> CapMap;
113 typedef Digraph::ArcMap<int> FlowMap;
114 typedef Digraph::NodeMap<bool> CutMap;
116 typedef Preflow<Digraph, CapMap> PType;
119 if( getenv("srcdir") )
120 f_name = std::string(getenv("srcdir"));
122 f_name += "/test/preflow_graph.lgf";
124 std::ifstream file(f_name.c_str());
126 check(file, "Input file '" << f_name << "' not found.");
131 DigraphReader<Digraph>(g,file).
132 arcMap("capacity", cap).
137 PType preflow_test(g, cap, s, t);
140 check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
141 "The flow is not feasible.");
144 preflow_test.minCutMap(min_cut);
145 int min_cut_value=cutValue(g,min_cut,cap);
147 check(preflow_test.flowValue() == min_cut_value,
148 "The max flow value is not equal to the three min cut values.");
151 for(ArcIt e(g); e!=INVALID; ++e) flow[e] = preflow_test.flowMap()[e];
153 int flow_value=preflow_test.flowValue();
155 for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e];
156 preflow_test.init(flow);
157 preflow_test.startFirstPhase();
160 preflow_test.minCutMap(min_cut1);
161 min_cut_value=cutValue(g,min_cut1,cap);
163 check(preflow_test.flowValue() == min_cut_value &&
164 min_cut_value == 2*flow_value,
165 "The max flow value or the min cut value is wrong.");
167 preflow_test.startSecondPhase();
169 check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
170 "The flow is not feasible.");
173 preflow_test.minCutMap(min_cut2);
174 min_cut_value=cutValue(g,min_cut2,cap);
176 check(preflow_test.flowValue() == min_cut_value &&
177 min_cut_value == 2*flow_value,
178 "The max flow value or the three min cut values were not doubled");
181 preflow_test.flowMap(flow);
185 if ( tmp1 != INVALID ) s=tmp1;
189 if ( tmp2 != INVALID ) t=tmp2;
191 preflow_test.source(s);
192 preflow_test.target(t);
197 preflow_test.minCutMap(min_cut3);
198 min_cut_value=cutValue(g,min_cut3,cap);
201 check(preflow_test.flowValue() == min_cut_value,
202 "The max flow value or the three min cut values are incorrect.");