4 #include <algorithm> |
4 #include <algorithm> |
5 #include <list> |
5 #include <list> |
6 #include <vector> |
6 #include <vector> |
7 //#include "pf_hiba.hh" |
7 //#include "pf_hiba.hh" |
8 //#include <marci_list_graph.hh> |
8 //#include <marci_list_graph.hh> |
9 #include <marci_graph_traits.hh> |
9 //#include <marci_graph_traits.hh> |
|
10 |
10 #include "reverse_bfs.hh" |
11 #include "reverse_bfs.hh" |
11 |
12 |
12 using namespace std; |
13 using namespace std; |
13 |
14 |
14 namespace marci { |
15 namespace marci { |
15 |
16 |
16 template <typename graph_type, typename T> |
17 template <typename graph_type, typename T> |
17 class preflow_push { |
18 class preflow_push { |
18 |
19 |
19 //Hasznos typedef-ek |
20 //Hasznos typedef-ek |
|
21 typedef typename graph_type::NodeIt NodeIt; |
|
22 typedef typename graph_type::EdgeIt EdgeIt; |
|
23 typedef typename graph_type::EachNodeIt EachNodeIt; |
|
24 typedef typename graph_type::EachEdgeIt EachEdgeIt; |
|
25 typedef typename graph_type::OutEdgeIt OutEdgeIt; |
|
26 typedef typename graph_type::InEdgeIt InEdgeIt; |
|
27 typedef typename graph_type::SymEdgeIt SymEdgeIt; |
|
28 |
|
29 |
|
30 |
|
31 /* |
20 typedef graph_traits<graph_type>::node_iterator node_iterator; |
32 typedef graph_traits<graph_type>::node_iterator node_iterator; |
21 typedef graph_traits<graph_type>::edge_iterator edge_iterator; |
33 typedef graph_traits<graph_type>::EdgeIt EdgeIt; |
22 typedef graph_traits<graph_type>::each_node_iterator each_node_iterator; |
34 typedef graph_traits<graph_type>::each_node_iterator each_node_iterator; |
23 typedef graph_traits<graph_type>::each_edge_iterator each_edge_iterator; |
35 typedef graph_traits<graph_type>::each_EdgeIt each_EdgeIt; |
24 typedef graph_traits<graph_type>::out_edge_iterator out_edge_iterator; |
36 typedef graph_traits<graph_type>::out_EdgeIt out_EdgeIt; |
25 typedef graph_traits<graph_type>::in_edge_iterator in_edge_iterator; |
37 typedef graph_traits<graph_type>::InEdgeIt InEdgeIt; |
26 typedef graph_traits<graph_type>::sym_edge_iterator sym_edge_iterator; |
38 typedef graph_traits<graph_type>::sym_EdgeIt sym_EdgeIt; |
|
39 */ |
27 |
40 |
28 //--------------------------------------------- |
41 //--------------------------------------------- |
29 //Parameters of the algorithm |
42 //Parameters of the algorithm |
30 //--------------------------------------------- |
43 //--------------------------------------------- |
31 //Fully examine an active node until excess becomes 0 |
44 //Fully examine an active node until excess becomes 0 |
41 //--------------------------------------------- |
54 //--------------------------------------------- |
42 |
55 |
43 private: |
56 private: |
44 //input |
57 //input |
45 graph_type& G; |
58 graph_type& G; |
46 node_iterator s; |
59 NodeIt s; |
47 node_iterator t; |
60 NodeIt t; |
48 edge_property_vector<graph_type, T> &capacity; |
61 typename graph_type::EdgeMap<T> &capacity; |
|
62 //typename graph_type::EdgeMap<T> &capacity; |
49 //output |
63 //output |
50 edge_property_vector<graph_type, T> preflow; |
64 //typename graph_type::EdgeMap<T> |
|
65 typename graph_type::EdgeMap<T> preflow; |
51 T maxflow_value; |
66 T maxflow_value; |
52 |
67 |
53 //auxiliary variables for computation |
68 //auxiliary variables for computation |
54 int number_of_nodes; |
69 int number_of_nodes; |
55 node_property_vector<graph_type, int> level; |
70 |
56 node_property_vector<graph_type, T> excess; |
71 |
|
72 typename graph_type::NodeMap<int> level; |
|
73 typename graph_type::NodeMap<T> excess; |
|
74 //node_property_vector<graph_type, int> level; |
|
75 //node_property_vector<graph_type, T> excess; |
57 |
76 |
58 //Number of nodes on each level |
77 //Number of nodes on each level |
59 vector<int> num_of_nodes_on_level; |
78 vector<int> num_of_nodes_on_level; |
60 |
79 |
61 //For the FIFO implementation |
80 //For the FIFO implementation |
62 list<node_iterator> fifo_nodes; |
81 list<NodeIt> fifo_nodes; |
63 //For 'highest label' implementation |
82 //For 'highest label' implementation |
64 int highest_active; |
83 int highest_active; |
65 //int second_highest_active; |
84 //int second_highest_active; |
66 vector< list<node_iterator> > active_nodes; |
85 vector< list<NodeIt> > active_nodes; |
67 |
86 |
68 public: |
87 public: |
69 |
88 |
70 //Constructing the object using the graph, source, sink and capacity vector |
89 //Constructing the object using the graph, source, sink and capacity vector |
71 preflow_push( |
90 preflow_push( |
72 graph_type& _G, |
91 graph_type& _G, |
73 node_iterator _s, |
92 NodeIt _s, |
74 node_iterator _t, |
93 NodeIt _t, |
75 edge_property_vector<graph_type, T>& _capacity) |
94 typename graph_type::EdgeMap<T> & _capacity) |
76 : G(_G), s(_s), t(_t), |
95 : G(_G), s(_s), t(_t), |
77 capacity(_capacity), |
96 capacity(_capacity), |
78 preflow(_G), |
97 preflow(_G), |
79 //Counting the number of nodes |
98 //Counting the number of nodes |
80 number_of_nodes(number_of(G.first_node())), |
99 //number_of_nodes(count(G.first<EachNodeIt>())), |
|
100 number_of_nodes(G.nodeNum()), |
|
101 |
81 level(_G), |
102 level(_G), |
82 excess(_G)//, |
103 excess(_G)//, |
83 // Default constructor: active_nodes() |
104 // Default constructor: active_nodes() |
84 { |
105 { |
85 //Simplest parameter settings |
106 //Simplest parameter settings |
104 } |
125 } |
105 |
126 |
106 //Returns the value of a maximal flow |
127 //Returns the value of a maximal flow |
107 T run(); |
128 T run(); |
108 |
129 |
109 edge_property_vector<graph_type, T> getmaxflow(){ |
130 typename graph_type::EdgeMap<T> getmaxflow(){ |
110 return preflow; |
131 return preflow; |
111 } |
132 } |
112 |
133 |
113 |
134 |
114 private: |
135 private: |
115 //For testing purposes only |
136 //For testing purposes only |
116 //Lists the node_properties |
137 //Lists the node_properties |
117 void write_property_vector(node_property_vector<graph_type, T> a, |
138 void write_property_vector(typename graph_type::NodeMap<T> a, |
|
139 //node_property_vector<graph_type, T> a, |
118 char* prop_name="property"){ |
140 char* prop_name="property"){ |
119 for(each_node_iterator i=G.first_node(); i.valid(); ++i) { |
141 for(EachNodeIt i=G.template first<EachNodeIt>(); i.valid(); ++i) { |
120 cout<<"Node id.: "<<G.id(i)<<", "<<prop_name<<" value: "<<a.get(i)<<endl; |
142 cout<<"Node id.: "<<G.id(i)<<", "<<prop_name<<" value: "<<a.get(i)<<endl; |
121 } |
143 } |
122 cout<<endl; |
144 cout<<endl; |
123 } |
145 } |
124 |
146 |
125 //Modifies the excess of the node and makes sufficient changes |
147 //Modifies the excess of the node and makes sufficient changes |
126 void modify_excess(const node_iterator& a ,T v){ |
148 void modify_excess(const NodeIt& a ,T v){ |
127 T old_value=excess.get(a); |
149 T old_value=excess.get(a); |
128 excess.put(a,old_value+v); |
150 excess.set(a,old_value+v); |
129 } |
151 } |
130 |
152 |
131 //This private procedure is supposed to modify the preflow on edge j |
153 //This private procedure is supposed to modify the preflow on edge j |
132 //by value v (which can be positive or negative as well) |
154 //by value v (which can be positive or negative as well) |
133 //and maintain the excess on the head and tail |
155 //and maintain the excess on the head and tail |
134 //Here we do not check whether this is possible or not |
156 //Here we do not check whether this is possible or not |
135 void modify_preflow(edge_iterator j, const T& v){ |
157 void modify_preflow(EdgeIt j, const T& v){ |
136 |
158 |
137 //Auxiliary variable |
159 //Auxiliary variable |
138 T old_value; |
160 T old_value; |
139 |
161 |
140 //Modifiyng the edge |
162 //Modifiyng the edge |
141 old_value=preflow.get(j); |
163 old_value=preflow.get(j); |
142 preflow.put(j,old_value+v); |
164 preflow.set(j,old_value+v); |
143 |
165 |
144 |
166 |
145 //Modifiyng the head |
167 //Modifiyng the head |
146 modify_excess(G.head(j),v); |
168 modify_excess(G.head(j),v); |
147 |
169 |
150 |
172 |
151 } |
173 } |
152 |
174 |
153 //Gives the active node to work with |
175 //Gives the active node to work with |
154 //(depending on the implementation to be used) |
176 //(depending on the implementation to be used) |
155 node_iterator get_active_node(){ |
177 NodeIt get_active_node(){ |
156 //cout<<highest_active<<endl; |
178 //cout<<highest_active<<endl; |
157 |
179 |
158 switch(implementation) { |
180 switch(implementation) { |
159 case impl_highest_label : { |
181 case impl_highest_label : { |
160 |
182 |
162 while( highest_active>=0 && active_nodes[highest_active].empty() ){ |
184 while( highest_active>=0 && active_nodes[highest_active].empty() ){ |
163 --highest_active; |
185 --highest_active; |
164 } |
186 } |
165 |
187 |
166 if( highest_active>=0) { |
188 if( highest_active>=0) { |
167 node_iterator a=active_nodes[highest_active].front(); |
189 NodeIt a=active_nodes[highest_active].front(); |
168 active_nodes[highest_active].pop_front(); |
190 active_nodes[highest_active].pop_front(); |
169 return a; |
191 return a; |
170 } |
192 } |
171 else { |
193 else { |
172 return node_iterator(); |
194 return NodeIt(); |
173 } |
195 } |
174 |
196 |
175 break; |
197 break; |
176 |
198 |
177 } |
199 } |
178 case impl_fifo : { |
200 case impl_fifo : { |
179 |
201 |
180 if( ! fifo_nodes.empty() ) { |
202 if( ! fifo_nodes.empty() ) { |
181 node_iterator a=fifo_nodes.front(); |
203 NodeIt a=fifo_nodes.front(); |
182 fifo_nodes.pop_front(); |
204 fifo_nodes.pop_front(); |
183 return a; |
205 return a; |
184 } |
206 } |
185 else { |
207 else { |
186 return node_iterator(); |
208 return NodeIt(); |
187 } |
209 } |
188 break; |
210 break; |
189 } |
211 } |
190 } |
212 } |
191 // |
213 // |
192 return node_iterator(); |
214 return NodeIt(); |
193 } |
215 } |
194 |
216 |
195 //Puts node 'a' among the active nodes |
217 //Puts node 'a' among the active nodes |
196 void make_active(const node_iterator& a){ |
218 void make_active(const NodeIt& a){ |
197 //s and t never become active |
219 //s and t never become active |
198 if (a!=s && a!= t){ |
220 if (a!=s && a!= t){ |
199 switch(implementation){ |
221 switch(implementation){ |
200 case impl_highest_label : |
222 case impl_highest_label : |
201 active_nodes[level.get(a)].push_back(a); |
223 active_nodes[level.get(a)].push_back(a); |
213 } |
235 } |
214 |
236 |
215 } |
237 } |
216 |
238 |
217 //Changes the level of node a and make sufficent changes |
239 //Changes the level of node a and make sufficent changes |
218 void change_level_to(node_iterator a, int new_value){ |
240 void change_level_to(NodeIt a, int new_value){ |
219 int seged = level.get(a); |
241 int seged = level.get(a); |
220 level.put(a,new_value); |
242 level.set(a,new_value); |
221 --num_of_nodes_on_level[seged]; |
243 --num_of_nodes_on_level[seged]; |
222 ++num_of_nodes_on_level[new_value]; |
244 ++num_of_nodes_on_level[new_value]; |
223 } |
245 } |
224 |
246 |
225 //Collection of things useful (or necessary) to do before running |
247 //Collection of things useful (or necessary) to do before running |
|
248 |
226 void preprocess(){ |
249 void preprocess(){ |
227 |
250 |
228 //--------------------------------------- |
251 //--------------------------------------- |
229 //Initialize parameters |
252 //Initialize parameters |
230 //--------------------------------------- |
253 //--------------------------------------- |
231 |
254 |
232 //Setting starting preflow, level and excess values to zero |
255 //Setting starting preflow, level and excess values to zero |
233 //This can be important, if the algorithm is run more then once |
256 //This can be important, if the algorithm is run more then once |
234 for(each_node_iterator i=G.first_node(); i.valid(); ++i) { |
257 for(EachNodeIt i=G.template first<EachNodeIt>(); i.valid(); ++i) { |
235 level.put(i,0); |
258 level.set(i,0); |
236 excess.put(i,0); |
259 excess.set(i,0); |
237 for(out_edge_iterator j=G.first_out_edge(i); j.valid(); ++j) |
260 for(OutEdgeIt j=G.template first<OutEdgeIt>(i); j.valid(); ++j) |
238 preflow.put(j, 0); |
261 preflow.set(j, 0); |
239 } |
262 } |
240 num_of_nodes_on_level[0]=number_of_nodes; |
263 num_of_nodes_on_level[0]=number_of_nodes; |
241 highest_active=0; |
264 highest_active=0; |
242 //--------------------------------------- |
265 //--------------------------------------- |
243 //Initialize parameters |
266 //Initialize parameters |
249 //------------------------------------ |
272 //------------------------------------ |
250 //Setting starting level values using reverse bfs |
273 //Setting starting level values using reverse bfs |
251 reverse_bfs<graph_type> rev_bfs(G,t); |
274 reverse_bfs<graph_type> rev_bfs(G,t); |
252 rev_bfs.run(); |
275 rev_bfs.run(); |
253 //write_property_vector(rev_bfs.dist,"rev_bfs"); |
276 //write_property_vector(rev_bfs.dist,"rev_bfs"); |
254 for(each_node_iterator i=G.first_node(); i.valid(); ++i) { |
277 for(EachNodeIt i=G.template first<EachNodeIt>(); i.valid(); ++i) { |
255 change_level_to(i,rev_bfs.dist(i)); |
278 change_level_to(i,rev_bfs.dist(i)); |
256 //level.put(i,rev_bfs.dist.get(i)); |
279 //level.put(i,rev_bfs.dist.get(i)); |
257 } |
280 } |
258 //------------------------------------ |
281 //------------------------------------ |
259 //This is the only part that uses BFS |
282 //This is the only part that uses BFS |
264 change_level_to(s,number_of_nodes); |
287 change_level_to(s,number_of_nodes); |
265 //level.put(s,number_of_nodes); |
288 //level.put(s,number_of_nodes); |
266 |
289 |
267 |
290 |
268 //we push as much preflow from s as possible to start with |
291 //we push as much preflow from s as possible to start with |
269 for(out_edge_iterator j=G.first_out_edge(s); j.valid(); ++j){ |
292 for(OutEdgeIt j=G.template first<OutEdgeIt>(s); j.valid(); ++j){ |
270 modify_preflow(j,capacity.get(j) ); |
293 modify_preflow(j,capacity.get(j) ); |
271 make_active(G.head(j)); |
294 make_active(G.head(j)); |
272 int lev=level.get(G.head(j)); |
295 int lev=level.get(G.head(j)); |
273 if(highest_active<lev){ |
296 if(highest_active<lev){ |
274 highest_active=lev; |
297 highest_active=lev; |
278 } |
301 } |
279 |
302 |
280 |
303 |
281 //If the preflow is less than the capacity on the given edge |
304 //If the preflow is less than the capacity on the given edge |
282 //then it is an edge in the residual graph |
305 //then it is an edge in the residual graph |
283 bool is_admissible_forward_edge(out_edge_iterator j, int& new_level){ |
306 bool is_admissible_forward_edge(OutEdgeIt j, int& new_level){ |
284 if (capacity.get(j)>preflow.get(j)){ |
307 if (capacity.get(j)>preflow.get(j)){ |
285 if(level.get(G.tail(j))==level.get(G.head(j))+1){ |
308 if(level.get(G.tail(j))==level.get(G.head(j))+1){ |
286 return true; |
309 return true; |
287 } |
310 } |
288 else{ |
311 else{ |
293 return false; |
316 return false; |
294 } |
317 } |
295 |
318 |
296 //If the preflow is greater than 0 on the given edge |
319 //If the preflow is greater than 0 on the given edge |
297 //then the edge reversd is an edge in the residual graph |
320 //then the edge reversd is an edge in the residual graph |
298 bool is_admissible_backward_edge(in_edge_iterator j, int& new_level){ |
321 bool is_admissible_backward_edge(InEdgeIt j, int& new_level){ |
299 if (0<preflow.get(j)){ |
322 if (0<preflow.get(j)){ |
300 if(level.get(G.tail(j))==level.get(G.head(j))-1){ |
323 if(level.get(G.tail(j))==level.get(G.head(j))-1){ |
301 return true; |
324 return true; |
302 } |
325 } |
303 else{ |
326 else{ |
316 T preflow_push<graph_type, T>::run() { |
339 T preflow_push<graph_type, T>::run() { |
317 |
340 |
318 preprocess(); |
341 preprocess(); |
319 |
342 |
320 T e,v; |
343 T e,v; |
321 node_iterator a; |
344 NodeIt a; |
322 while (a=get_active_node(), a.valid()){ |
345 while (a=get_active_node(), a.valid()){ |
323 //cout<<G.id(a)<<endl; |
346 //cout<<G.id(a)<<endl; |
324 //write_property_vector(excess,"excess"); |
347 //write_property_vector(excess,"excess"); |
325 //write_property_vector(level,"level"); |
348 //write_property_vector(level,"level"); |
326 |
349 |
327 //Initial value for the new level for the active node we are dealing with |
|
328 int new_level=2*number_of_nodes; |
|
329 |
350 |
330 bool go_to_next_node=false; |
351 bool go_to_next_node=false; |
331 e = excess.get(a); |
352 e = excess.get(a); |
332 while (!go_to_next_node){ |
353 while (!go_to_next_node){ |
333 |
354 |
|
355 //Initial value for the new level for the active node we are dealing with |
|
356 int new_level=2*number_of_nodes; |
|
357 //write_property_vector(excess,"excess"); |
|
358 //write_property_vector(level,"level"); |
|
359 //cout<<G.id(a)<<endl; |
334 //Out edges from node a |
360 //Out edges from node a |
335 { |
361 { |
336 out_edge_iterator j=G.first_out_edge(a); |
362 OutEdgeIt j=G.template first<OutEdgeIt>(a); |
337 while (j.valid() && e){ |
363 while (j.valid() && e){ |
338 |
364 |
339 if (is_admissible_forward_edge(j,new_level)){ |
365 if (is_admissible_forward_edge(j,new_level)){ |
340 v=min(e,capacity.get(j) - preflow.get(j)); |
366 v=min(e,capacity.get(j) - preflow.get(j)); |
341 e -= v; |
367 e -= v; |
348 ++j; |
374 ++j; |
349 } |
375 } |
350 } |
376 } |
351 //In edges to node a |
377 //In edges to node a |
352 { |
378 { |
353 in_edge_iterator j=G.first_in_edge(a); |
379 InEdgeIt j=G.template first<InEdgeIt>(a); |
354 while (j.valid() && e){ |
380 while (j.valid() && e){ |
355 if (is_admissible_backward_edge(j,new_level)){ |
381 if (is_admissible_backward_edge(j,new_level)){ |
356 v=min(e,preflow.get(j)); |
382 v=min(e,preflow.get(j)); |
357 e -= v; |
383 e -= v; |
358 //New node might become active |
384 //New node might become active |
370 if (0==e){ |
396 if (0==e){ |
371 //Saturating push |
397 //Saturating push |
372 go_to_next_node=true; |
398 go_to_next_node=true; |
373 } |
399 } |
374 else{//If there is still excess in node a |
400 else{//If there is still excess in node a |
375 |
401 |
|
402 //change_level_to(a,new_level+1); |
|
403 |
376 //Level remains empty |
404 //Level remains empty |
377 if (num_of_nodes_on_level[level.get(a)]==1){ |
405 if (num_of_nodes_on_level[level.get(a)]==1){ |
378 change_level_to(a,number_of_nodes); |
406 change_level_to(a,number_of_nodes); |
379 //go_to_next_node=True; |
407 //go_to_next_node=True; |
380 } |
408 } |
381 else{ |
409 else{ |
382 change_level_to(a,new_level+1); |
410 change_level_to(a,new_level+1); |
383 //increase_level(a); |
411 //increase_level(a); |
384 } |
412 } |
385 |
413 |
386 |
414 |
387 |
415 |
388 |
416 |
389 switch(node_examination){ |
417 switch(node_examination){ |
390 case examine_to_relabel: |
418 case examine_to_relabel: |