1 | /* -*- C++ -*- |
---|
2 | * src/test/preflow_test.cc - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
6 | * |
---|
7 | * Permission to use, modify and distribute this software is granted |
---|
8 | * provided that this copyright notice appears in all copies. For |
---|
9 | * precise terms see the accompanying LICENSE file. |
---|
10 | * |
---|
11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
12 | * express or implied, and with no claim as to its suitability for any |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #include <fstream> |
---|
18 | #include <string> |
---|
19 | |
---|
20 | #include "test_tools.h" |
---|
21 | #include <lemon/smart_graph.h> |
---|
22 | #include <lemon/dimacs.h> |
---|
23 | #include <lemon/preflow.h> |
---|
24 | #include <lemon/skeletons/graph.h> |
---|
25 | #include <lemon/skeletons/maps.h> |
---|
26 | |
---|
27 | using namespace lemon; |
---|
28 | |
---|
29 | void check_Preflow() |
---|
30 | { |
---|
31 | typedef int VType; |
---|
32 | typedef skeleton::StaticGraph Graph; |
---|
33 | |
---|
34 | typedef Graph::Node Node; |
---|
35 | typedef Graph::Edge Edge; |
---|
36 | typedef skeleton::ReadMap<Edge,VType> CapMap; |
---|
37 | typedef skeleton::ReadWriteMap<Edge,VType> FlowMap; |
---|
38 | typedef skeleton::ReadWriteMap<Node,bool> CutMap; |
---|
39 | |
---|
40 | typedef Preflow<Graph, int, CapMap, FlowMap> PType; |
---|
41 | |
---|
42 | Graph G; |
---|
43 | Node n; |
---|
44 | CapMap cap; |
---|
45 | FlowMap flow; |
---|
46 | CutMap cut; |
---|
47 | |
---|
48 | PType preflow_test(G,n,n,cap,flow); |
---|
49 | |
---|
50 | preflow_test.run(); |
---|
51 | preflow_test.flowValue(); |
---|
52 | preflow_test.setSource(n); |
---|
53 | preflow_test.setFlow(flow); |
---|
54 | |
---|
55 | preflow_test.phase1(PType::NO_FLOW); |
---|
56 | preflow_test.minCut(cut); |
---|
57 | |
---|
58 | preflow_test.phase2(); |
---|
59 | preflow_test.setTarget(n); |
---|
60 | preflow_test.setCap(cap); |
---|
61 | preflow_test.minMinCut(cut); |
---|
62 | preflow_test.maxMinCut(cut); |
---|
63 | } |
---|
64 | |
---|
65 | int cut_value ( SmartGraph& G, SmartGraph::NodeMap<bool>& cut, |
---|
66 | SmartGraph::EdgeMap<int>& cap) { |
---|
67 | |
---|
68 | int c=0; |
---|
69 | for(SmartGraph::EdgeIt e(G); e!=INVALID; ++e) { |
---|
70 | if (cut[G.tail(e)] && !cut[G.head(e)]) c+=cap[e]; |
---|
71 | } |
---|
72 | return c; |
---|
73 | } |
---|
74 | |
---|
75 | int main() { |
---|
76 | |
---|
77 | typedef SmartGraph Graph; |
---|
78 | |
---|
79 | typedef Graph::Node Node; |
---|
80 | typedef Graph::NodeIt NodeIt; |
---|
81 | typedef Graph::EdgeIt EdgeIt; |
---|
82 | typedef Graph::EdgeMap<int> CapMap; |
---|
83 | typedef Graph::EdgeMap<int> FlowMap; |
---|
84 | typedef Graph::NodeMap<bool> CutMap; |
---|
85 | |
---|
86 | typedef Preflow<Graph, int> PType; |
---|
87 | |
---|
88 | std::string f_name; |
---|
89 | if( getenv("srcdir") ) { |
---|
90 | f_name = std::string(getenv("srcdir")) + "/preflow_graph.dim"; |
---|
91 | } |
---|
92 | else { |
---|
93 | f_name = "preflow_graph.dim"; |
---|
94 | } |
---|
95 | |
---|
96 | std::ifstream file(f_name.c_str()); |
---|
97 | |
---|
98 | check(file, "Input file '" << f_name << "' not found."); |
---|
99 | |
---|
100 | Graph G; |
---|
101 | Node s, t; |
---|
102 | CapMap cap(G); |
---|
103 | readDimacs(file, G, cap, s, t); |
---|
104 | |
---|
105 | FlowMap flow(G,0); |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | PType preflow_test(G, s, t, cap, flow); |
---|
110 | preflow_test.run(PType::ZERO_FLOW); |
---|
111 | |
---|
112 | CutMap mincut(G,false); |
---|
113 | preflow_test.minCut(mincut); |
---|
114 | int min_cut_value=cut_value(G,mincut,cap); |
---|
115 | |
---|
116 | CutMap minmincut(G,false); |
---|
117 | preflow_test.minMinCut(minmincut); |
---|
118 | int min_min_cut_value=cut_value(G,minmincut,cap); |
---|
119 | |
---|
120 | CutMap maxmincut(G,false); |
---|
121 | preflow_test.maxMinCut(maxmincut); |
---|
122 | int max_min_cut_value=cut_value(G,maxmincut,cap); |
---|
123 | |
---|
124 | check(preflow_test.flowValue() == min_cut_value && |
---|
125 | min_cut_value == min_min_cut_value && |
---|
126 | min_min_cut_value == max_min_cut_value, |
---|
127 | "The max flow value is not equal to the three min cut values."); |
---|
128 | |
---|
129 | int flow_value=preflow_test.flowValue(); |
---|
130 | |
---|
131 | |
---|
132 | |
---|
133 | for(EdgeIt e(G); e!=INVALID; ++e) cap[e]=2*cap[e]; |
---|
134 | preflow_test.setCap(cap); |
---|
135 | |
---|
136 | preflow_test.phase1(PType::PRE_FLOW); |
---|
137 | |
---|
138 | CutMap mincut1(G,false); |
---|
139 | preflow_test.minCut(mincut1); |
---|
140 | min_cut_value=cut_value(G,mincut1,cap); |
---|
141 | |
---|
142 | check(preflow_test.flowValue() == min_cut_value && |
---|
143 | min_cut_value == 2*flow_value, |
---|
144 | "The max flow value or the min cut value is wrong."); |
---|
145 | |
---|
146 | preflow_test.phase2(); |
---|
147 | |
---|
148 | CutMap mincut2(G,false); |
---|
149 | preflow_test.minCut(mincut2); |
---|
150 | min_cut_value=cut_value(G,mincut2,cap); |
---|
151 | |
---|
152 | CutMap minmincut2(G,false); |
---|
153 | preflow_test.minMinCut(minmincut2); |
---|
154 | min_min_cut_value=cut_value(G,minmincut2,cap); |
---|
155 | |
---|
156 | preflow_test.maxMinCut(maxmincut); |
---|
157 | max_min_cut_value=cut_value(G,maxmincut,cap); |
---|
158 | |
---|
159 | check(preflow_test.flowValue() == min_cut_value && |
---|
160 | min_cut_value == min_min_cut_value && |
---|
161 | min_min_cut_value == max_min_cut_value && |
---|
162 | min_cut_value == 2*flow_value, |
---|
163 | "The max flow value or the three min cut values were not doubled"); |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | EdgeIt e(G); |
---|
168 | for( int i=1; i==10; ++i ) { |
---|
169 | flow.set(e,0); |
---|
170 | ++e; |
---|
171 | } |
---|
172 | |
---|
173 | preflow_test.setFlow(flow); |
---|
174 | |
---|
175 | NodeIt tmp1(G,s); |
---|
176 | ++tmp1; |
---|
177 | if ( tmp1 != INVALID ) s=tmp1; |
---|
178 | |
---|
179 | NodeIt tmp2(G,t); |
---|
180 | ++tmp2; |
---|
181 | if ( tmp2 != INVALID ) t=tmp2; |
---|
182 | |
---|
183 | preflow_test.setSource(s); |
---|
184 | preflow_test.setTarget(t); |
---|
185 | |
---|
186 | preflow_test.run(); |
---|
187 | |
---|
188 | CutMap mincut3(G,false); |
---|
189 | preflow_test.minCut(mincut3); |
---|
190 | min_cut_value=cut_value(G,mincut3,cap); |
---|
191 | |
---|
192 | CutMap minmincut3(G,false); |
---|
193 | preflow_test.minMinCut(minmincut3); |
---|
194 | min_min_cut_value=cut_value(G,minmincut3,cap); |
---|
195 | |
---|
196 | preflow_test.maxMinCut(maxmincut); |
---|
197 | max_min_cut_value=cut_value(G,maxmincut,cap); |
---|
198 | |
---|
199 | check(preflow_test.flowValue() == min_cut_value && |
---|
200 | min_cut_value == min_min_cut_value && |
---|
201 | min_min_cut_value == max_min_cut_value, |
---|
202 | "The max flow value or the three min cut values are incorrect."); |
---|
203 | } |
---|