1 | // -*- C++ -*- |
---|
2 | /* |
---|
3 | preflow_push_hl.h |
---|
4 | by jacint. |
---|
5 | Runs the highest label variant of the preflow push algorithm with |
---|
6 | running time O(n^2\sqrt(m)). |
---|
7 | |
---|
8 | Member functions: |
---|
9 | |
---|
10 | void run() : runs the algorithm |
---|
11 | |
---|
12 | The following functions should be used after run() was already run. |
---|
13 | |
---|
14 | T maxflow() : returns the value of a maximum flow |
---|
15 | |
---|
16 | T flowonedge(EdgeIt e) : for a fixed maximum flow x it returns x(e) |
---|
17 | |
---|
18 | Graph::EdgeMap<T> allflow() : returns the fixed maximum flow x |
---|
19 | |
---|
20 | Graph::NodeMap<bool> mincut() : returns a |
---|
21 | characteristic vector of a minimum cut. (An empty level |
---|
22 | in the algorithm gives a minimum cut.) |
---|
23 | */ |
---|
24 | |
---|
25 | #ifndef PREFLOW_PUSH_HL_H |
---|
26 | #define PREFLOW_PUSH_HL_H |
---|
27 | |
---|
28 | #include <algorithm> |
---|
29 | #include <vector> |
---|
30 | #include <stack> |
---|
31 | |
---|
32 | #include <list_graph.hh> |
---|
33 | #include <reverse_bfs.h> |
---|
34 | |
---|
35 | namespace marci { |
---|
36 | |
---|
37 | template <typename Graph, typename T> |
---|
38 | class preflow_push_hl { |
---|
39 | |
---|
40 | typedef typename Graph::NodeIt NodeIt; |
---|
41 | typedef typename Graph::EdgeIt EdgeIt; |
---|
42 | typedef typename Graph::EachNodeIt EachNodeIt; |
---|
43 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
44 | typedef typename Graph::InEdgeIt InEdgeIt; |
---|
45 | |
---|
46 | Graph& G; |
---|
47 | NodeIt s; |
---|
48 | NodeIt t; |
---|
49 | typename Graph::EdgeMap<T> flow; |
---|
50 | typename Graph::EdgeMap<T> capacity; |
---|
51 | T value; |
---|
52 | typename Graph::NodeMap<bool> mincutvector; |
---|
53 | |
---|
54 | public: |
---|
55 | |
---|
56 | preflow_push_hl(Graph& _G, NodeIt _s, NodeIt _t, |
---|
57 | typename Graph::EdgeMap<T>& _capacity) : |
---|
58 | G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity), |
---|
59 | mincutvector(_G, true) { } |
---|
60 | |
---|
61 | |
---|
62 | /* |
---|
63 | The run() function runs the highest label preflow-push, |
---|
64 | running time: O(n^2\sqrt(m)) |
---|
65 | */ |
---|
66 | void run() { |
---|
67 | |
---|
68 | typename Graph::NodeMap<int> level(G); |
---|
69 | typename Graph::NodeMap<T> excess(G); |
---|
70 | std::cout <<"excess s:"<<excess.get(s); //delme |
---|
71 | |
---|
72 | int n=G.nodeNum(); |
---|
73 | int b=n-2; |
---|
74 | /* |
---|
75 | b is a bound on the highest level of an active node. |
---|
76 | In the beginning it is at most n-2. |
---|
77 | */ |
---|
78 | |
---|
79 | std::vector<std::stack<NodeIt> > stack(2*n-1); |
---|
80 | //Stack of the active nodes in level i. |
---|
81 | |
---|
82 | |
---|
83 | /*Reverse_bfs from t, to find the starting level.*/ |
---|
84 | reverse_bfs<Graph> bfs(G, t); |
---|
85 | bfs.run(); |
---|
86 | for(EachNodeIt v=G.template first<EachNodeIt>(); v.valid(); ++v) |
---|
87 | { |
---|
88 | level.set(v, bfs.dist(v)); |
---|
89 | } |
---|
90 | |
---|
91 | level.set(s,n); |
---|
92 | |
---|
93 | |
---|
94 | /* Starting flow. It is everywhere 0 at the moment. */ |
---|
95 | for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e) |
---|
96 | { |
---|
97 | if ( capacity.get(e) > 0 ) { |
---|
98 | NodeIt w=G.head(e); |
---|
99 | if ( excess.get(w) == 0 && w!=t && w!=s ) stack[level.get(w)].push(w); |
---|
100 | flow.set(e, capacity.get(e)); |
---|
101 | excess.set(w, excess.get(w)+capacity.get(e)); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | /* |
---|
106 | End of preprocessing |
---|
107 | */ |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | /* |
---|
112 | Push/relabel on the highest level active nodes. |
---|
113 | */ |
---|
114 | |
---|
115 | /*While there exists active node.*/ |
---|
116 | while (b) { |
---|
117 | |
---|
118 | /*We decrease the bound if there is no active Node of level b.*/ |
---|
119 | if (stack[b].empty()) { |
---|
120 | --b; |
---|
121 | } else { |
---|
122 | |
---|
123 | NodeIt w=stack[b].top(); //w is a highest label active node. |
---|
124 | stack[b].pop(); |
---|
125 | |
---|
126 | int newlevel=2*n-2; //In newlevel we bound the next level of w. |
---|
127 | |
---|
128 | for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) { |
---|
129 | |
---|
130 | if ( flow.get(e) < capacity.get(e) ) { |
---|
131 | /*e is an edge of the residual graph */ |
---|
132 | |
---|
133 | NodeIt v=G.head(e); /*e is the edge wv.*/ |
---|
134 | |
---|
135 | if( level.get(w) > level.get(v)+1 ) { std::cout<<"HIBA1!";} //delme |
---|
136 | |
---|
137 | if( level.get(w) == level.get(v)+1 ) { |
---|
138 | /*Push is allowed now*/ |
---|
139 | |
---|
140 | if (capacity.get(e)-flow.get(e) > excess.get(w)) { |
---|
141 | /*A nonsaturating push.*/ |
---|
142 | |
---|
143 | if (excess.get(v)==0 && v != s && v !=t) stack[level.get(v)].push(v); |
---|
144 | /*v becomes active.*/ |
---|
145 | |
---|
146 | flow.set(e, flow.get(e)+excess.get(w)); |
---|
147 | excess.set(v, excess.get(v)+excess.get(w)); |
---|
148 | excess.set(w,0); |
---|
149 | //std::cout << w << " " << v <<" elore elen nonsat pump " << std::endl; |
---|
150 | break; |
---|
151 | } else { |
---|
152 | /*A saturating push.*/ |
---|
153 | |
---|
154 | if (excess.get(v)==0 && v != s&& v !=t) stack[level.get(v)].push(v); |
---|
155 | /*v becomes active.*/ |
---|
156 | |
---|
157 | excess.set(v, excess.get(v)+capacity.get(e)-flow.get(e)); |
---|
158 | excess.set(w, excess.get(w)-capacity.get(e)+flow.get(e)); |
---|
159 | flow.set(e, capacity.get(e)); |
---|
160 | //std::cout << w<<" " <<v<<" elore elen sat pump " << std::endl; |
---|
161 | if (excess.get(w)==0) break; |
---|
162 | /*If w is not active any more, then we go on to the next Node.*/ |
---|
163 | |
---|
164 | //std::cout<<++i; |
---|
165 | |
---|
166 | } // if (capacity.get(e)-flow.get(e) > excess.get(w)) |
---|
167 | } // if(level.get(w)==level.get(v)+1) |
---|
168 | |
---|
169 | else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);} |
---|
170 | |
---|
171 | } //if (flow.get(e)<capacity.get(e)) |
---|
172 | |
---|
173 | } //for(OutEdgeIt e=G.first_OutEdge(w); e.valid(); ++e) |
---|
174 | |
---|
175 | |
---|
176 | |
---|
177 | for(InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) { |
---|
178 | NodeIt v=G.tail(e); |
---|
179 | /*e is the Edge vw.*/ |
---|
180 | |
---|
181 | if (excess.get(w)==0) break; |
---|
182 | /*It may happen, that w became inactive in the first for cycle.*/ |
---|
183 | if(flow.get(e)>0) { |
---|
184 | /*e is an Edge of the residual graph */ |
---|
185 | |
---|
186 | if(level.get(w)==level.get(v)+1) { |
---|
187 | /*Push is allowed now*/ |
---|
188 | |
---|
189 | if (flow.get(e) > excess.get(w)) { |
---|
190 | /*A nonsaturating push.*/ |
---|
191 | |
---|
192 | if (excess.get(v)==0 && v != s&& v !=t) stack[level.get(v)].push(v); |
---|
193 | /*v becomes active.*/ |
---|
194 | |
---|
195 | flow.set(e, flow.get(e)-excess.get(w)); |
---|
196 | excess.set(v, excess.get(v)+excess.get(w)); |
---|
197 | excess.set(w,0); |
---|
198 | //std::cout << v << " " << w << " vissza elen nonsat pump " << std::endl; |
---|
199 | break; |
---|
200 | } else { |
---|
201 | /*A saturating push.*/ |
---|
202 | |
---|
203 | if (excess.get(v)==0 && v != s&& v !=t) stack[level.get(v)].push(v); |
---|
204 | /*v becomes active.*/ |
---|
205 | |
---|
206 | excess.set(v, excess.get(v)+flow.get(e)); |
---|
207 | excess.set(w, excess.get(w)-flow.get(e)); |
---|
208 | flow.set(e,0); |
---|
209 | //std::cout << v <<" " << w << " vissza elen sat pump " << std::endl; |
---|
210 | if (excess.get(w)==0) { break;} |
---|
211 | } //if (flow.get(e) > excess.get(v)) |
---|
212 | } //if(level.get(w)==level.get(v)+1) |
---|
213 | |
---|
214 | else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);} |
---|
215 | |
---|
216 | |
---|
217 | } //if (flow.get(e)>0) |
---|
218 | |
---|
219 | } //for |
---|
220 | |
---|
221 | |
---|
222 | if (excess.get(w)>0) { |
---|
223 | level.set(w,++newlevel); |
---|
224 | stack[newlevel].push(w); |
---|
225 | b=newlevel; |
---|
226 | //std::cout << "The new level of " << w << " is "<< newlevel <<std::endl; |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | } //else |
---|
231 | |
---|
232 | } //while(b) |
---|
233 | |
---|
234 | value = excess.get(t); |
---|
235 | /*Max flow value.*/ |
---|
236 | |
---|
237 | |
---|
238 | |
---|
239 | |
---|
240 | } //void run() |
---|
241 | |
---|
242 | |
---|
243 | |
---|
244 | |
---|
245 | |
---|
246 | /* |
---|
247 | Returns the maximum value of a flow. |
---|
248 | */ |
---|
249 | |
---|
250 | T maxflow() { |
---|
251 | return value; |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | |
---|
256 | /* |
---|
257 | For the maximum flow x found by the algorithm, it returns the flow value on Edge e, i.e. x(e). |
---|
258 | */ |
---|
259 | |
---|
260 | T flowonEdge(EdgeIt e) { |
---|
261 | return flow.get(e); |
---|
262 | } |
---|
263 | |
---|
264 | |
---|
265 | |
---|
266 | /* |
---|
267 | Returns the maximum flow x found by the algorithm. |
---|
268 | */ |
---|
269 | |
---|
270 | typename Graph::EdgeMap<T> allflow() { |
---|
271 | return flow; |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | |
---|
276 | /* |
---|
277 | Returns a minimum cut by using a reverse bfs from t in the residual graph. |
---|
278 | */ |
---|
279 | |
---|
280 | typename Graph::NodeMap<bool> mincut() { |
---|
281 | |
---|
282 | std::queue<NodeIt> queue; |
---|
283 | |
---|
284 | mincutvector.set(t,false); |
---|
285 | queue.push(t); |
---|
286 | |
---|
287 | while (!queue.empty()) { |
---|
288 | NodeIt w=queue.front(); |
---|
289 | queue.pop(); |
---|
290 | |
---|
291 | for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) { |
---|
292 | NodeIt v=G.tail(e); |
---|
293 | if (mincutvector.get(v) && flow.get(e) < capacity.get(e) ) { |
---|
294 | queue.push(v); |
---|
295 | mincutvector.set(v, false); |
---|
296 | } |
---|
297 | } // for |
---|
298 | |
---|
299 | for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) { |
---|
300 | NodeIt v=G.head(e); |
---|
301 | if (mincutvector.get(v) && flow.get(e) > 0 ) { |
---|
302 | queue.push(v); |
---|
303 | mincutvector.set(v, false); |
---|
304 | } |
---|
305 | } // for |
---|
306 | |
---|
307 | } |
---|
308 | |
---|
309 | return mincutvector; |
---|
310 | |
---|
311 | } |
---|
312 | }; |
---|
313 | }//namespace marci |
---|
314 | #endif |
---|
315 | |
---|
316 | |
---|
317 | |
---|
318 | |
---|