3 //hogy kell azt megcsinalni hogy a konstruktorban a FlowMap-nek legyen default erteke (a 0 map)
9 The same as preflow.h, the only difference is that here
10 we start from a given flow.
13 The constructor runs the algorithm:
15 template <typename Graph, typename T,
16 typename Graph_EdgeMap_T_1=typename Graph::EdgeMap<T>,
17 typename Graph_EdgeMap_T_2=typename Graph::EdgeMap<T> >
18 PreflowAug(Graph G, G_NodeIt s, G_NodeIt t, G_EdgeMap_T_1 capacity,
19 G_EdgeMap_T_2 flow, bool flow_type)
21 'capacity' must be non-negative, if flow_type=0 then
22 'flow' must be a flow, otherwise it must be a preflow.
27 T maxFlow() : returns the value of a maximum flow
29 T flow(G_EdgeIt e) : for a fixed maximum flow x it returns x(e)
31 G_EdgeMap_T_2 flow() : returns the fixed maximum flow x
33 void minMinCut(G_NodeMap_bool& M) :
34 sets M to the characteristic vector of the
35 minimum min cut. M must be initialized to false.
37 void maxMinCut(G_NodeMap_bool& M) :
38 sets M to the characteristic vector of the
39 maximum min cut. M must be initialized to false.
41 void minCut(G_NodeMap_bool& M) :
42 sets M to the characteristic vector of a
43 min cut. M must be initialized to false.
55 #include <iostream> //for error handling
57 #include <time_measure.h>
61 template <typename Graph, typename T,
62 typename CapMap=typename Graph::EdgeMap<T>,
63 typename FlowMap=typename Graph::EdgeMap<T> >
66 typedef typename Graph::NodeIt NodeIt;
67 typedef typename Graph::EdgeIt EdgeIt;
68 typedef typename Graph::EachNodeIt EachNodeIt;
69 typedef typename Graph::EachEdgeIt EachEdgeIt;
70 typedef typename Graph::OutEdgeIt OutEdgeIt;
71 typedef typename Graph::InEdgeIt InEdgeIt;
83 PreflowAug(Graph& _G, NodeIt _s, NodeIt _t,
84 CapMap& _capacity, FlowMap& __flow, bool _flow_type ) :
85 G(_G), s(_s), t(_t), capacity(_capacity), _flow(__flow),
90 bool phase=0; //phase 0 is the 1st phase, phase 1 is the 2nd
92 int heur0=(int)(H0*n); //time while running 'bound decrease'
93 int heur1=(int)(H1*n); //time while running 'highest label'
94 int heur=heur1; //starting time interval (#of relabels)
97 what_heur is 0 in case 'bound decrease'
98 and 1 in case 'highest label'
102 Needed for 'bound decrease', 'true'
103 means no active nodes are above bound b.
106 int k=n-2; //bound on the highest level under n containing a node
107 int b=k; //bound on the highest level under n of an active node
109 typename Graph::NodeMap<int> level(G,n);
110 typename Graph::NodeMap<T> excess(G);
112 std::vector<NodeIt> active(n);
113 typename Graph::NodeMap<NodeIt> next(G);
114 //Stack of the active nodes in level i < n.
115 //We use it in both phases.
117 typename Graph::NodeMap<NodeIt> left(G);
118 typename Graph::NodeMap<NodeIt> right(G);
119 std::vector<NodeIt> level_list(n);
121 List of the nodes in level i<n.
124 /*Reverse_bfs from t, to find the starting level.*/
126 std::queue<NodeIt> bfs_queue;
130 while (!bfs_queue.empty()) {
132 NodeIt v=bfs_queue.front();
134 int l=level.get(v)+1;
136 for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
137 if ( capacity.get(e) == _flow.get(e) ) continue;
139 if ( level.get(w) == n && w != s ) {
141 NodeIt first=level_list[l];
142 if ( first.valid() ) left.set(first,w);
149 for(OutEdgeIt e=G.template first<OutEdgeIt>(v); e.valid(); ++e) {
150 if ( 0 == _flow.get(e) ) continue;
152 if ( level.get(w) == n && w != s ) {
154 NodeIt first=level_list[l];
155 if ( first != 0 ) left.set(first,w);
167 /*The starting excess.*/
169 for(EachEdgeIt e=G.template first<EachEdgeIt>(); e.valid(); ++e) {
170 if ( _flow.get(e) > 0 ) {
174 excess.set(u, excess.get(u)-flo);
175 excess.set(v, excess.get(v)+flo);
179 for(EachNodeIt v=G.template first<EachNodeIt>(); v.valid(); ++v) {
180 if ( excess.get(v) < 0 ) {
181 std::cerr<<"It is not a pre_flow."<<std::endl;
184 next.set(v,active[level.get(v)]);
185 active[level.get(v)]=v;}
190 /* Starting flow. It is everywhere 0 at the moment. */
191 for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e)
193 T c=capacity.get(e)-_flow.get(e);
194 if ( c == 0 ) continue;
196 if ( level.get(w) < n ) {
197 if ( excess.get(w) == 0 && w!=t ) {
198 next.set(w,active[level.get(w)]);
199 active[level.get(w)]=w;
201 _flow.set(e, capacity.get(e));
202 excess.set(w, excess.get(w)+c);
213 Push/relabel on the highest level active nodes.
220 if ( !what_heur && !end && k > 0 ) {
227 std::queue<NodeIt> bfs_queue;
230 while (!bfs_queue.empty()) {
232 NodeIt v=bfs_queue.front();
234 int l=level.get(v)+1;
236 for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
237 if ( capacity.get(e) == _flow.get(e) ) continue;
239 if ( level.get(u) >= n ) {
242 if ( excess.get(u) > 0 ) {
243 next.set(u,active[l]);
249 for(OutEdgeIt e=G.template first<OutEdgeIt>(v); e.valid(); ++e) {
250 if ( 0 == _flow.get(e) ) continue;
252 if ( level.get(u) >= n ) {
255 if ( excess.get(u) > 0 ) {
256 next.set(u,active[l]);
268 if ( active[b] == 0 ) --b;
273 active[b]=next.get(w);
274 int lev=level.get(w);
276 int newlevel=n; //bound on the next level of w
278 for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
280 if ( _flow.get(e) == capacity.get(e) ) continue;
284 if( lev > level.get(v) ) {
285 /*Push is allowed now*/
287 if ( excess.get(v)==0 && v!=t && v!=s ) {
288 int lev_v=level.get(v);
289 next.set(v,active[lev_v]);
293 T cap=capacity.get(e);
297 if ( remcap >= exc ) {
298 /*A nonsaturating push.*/
300 _flow.set(e, flo+exc);
301 excess.set(v, excess.get(v)+exc);
306 /*A saturating push.*/
309 excess.set(v, excess.get(v)+remcap);
312 } else if ( newlevel > level.get(v) ){
313 newlevel = level.get(v);
320 for( InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
322 if( _flow.get(e) == 0 ) continue;
326 if( lev > level.get(v) ) {
327 /*Push is allowed now*/
329 if ( excess.get(v)==0 && v!=t && v!=s ) {
330 int lev_v=level.get(v);
331 next.set(v,active[lev_v]);
338 /*A nonsaturating push.*/
340 _flow.set(e, flo-exc);
341 excess.set(v, excess.get(v)+exc);
345 /*A saturating push.*/
347 excess.set(v, excess.get(v)+flo);
351 } else if ( newlevel > level.get(v) ) {
352 newlevel = level.get(v);
356 } // if w still has excess after the out edge for cycle
366 //now 'lev' is the old level of w
369 level.set(w,++newlevel);
370 next.set(w,active[newlevel]);
375 NodeIt right_n=right.get(w);
376 NodeIt left_n=left.get(w);
378 if ( right_n != 0 ) {
380 right.set(left_n, right_n);
381 left.set(right_n, left_n);
383 level_list[lev]=right_n;
384 left.set(right_n, 0);
388 right.set(left_n, 0);
397 if ( level_list[lev]==0 ) {
399 for (int i=lev; i!=k ; ) {
400 NodeIt v=level_list[++i];
406 if ( !what_heur ) active[i]=0;
415 if ( newlevel == n ) level.set(w,n);
417 level.set(w,++newlevel);
418 next.set(w,active[newlevel]);
420 if ( what_heur ) b=newlevel;
421 if ( k < newlevel ) ++k;
422 NodeIt first=level_list[newlevel];
423 if ( first != 0 ) left.set(first,w);
426 level_list[newlevel]=w;
432 if ( relabel >= heur ) {
450 } // if stack[b] is nonempty
455 value = excess.get(t);
465 Returns the maximum value of a flow.
475 For the maximum flow x found by the algorithm,
476 it returns the flow value on edge e, i.e. x(e).
491 void flow(FlowMap& __flow ) {
492 for(EachNodeIt v=G.template first<EachNodeIt>(); v.valid(); ++v)
493 __flow.set(v,_flow.get(v));
499 Returns the minimum min cut, by a bfs from s in the residual graph.
502 template<typename _CutMap>
503 void minMinCut(_CutMap& M) {
505 std::queue<NodeIt> queue;
510 while (!queue.empty()) {
511 NodeIt w=queue.front();
514 for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
516 if (!M.get(v) && _flow.get(e) < capacity.get(e) ) {
522 for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
524 if (!M.get(v) && _flow.get(e) > 0 ) {
535 Returns the maximum min cut, by a reverse bfs
536 from t in the residual graph.
539 template<typename _CutMap>
540 void maxMinCut(_CutMap& M) {
542 std::queue<NodeIt> queue;
547 while (!queue.empty()) {
548 NodeIt w=queue.front();
551 for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
553 if (!M.get(v) && _flow.get(e) < capacity.get(e) ) {
559 for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
561 if (!M.get(v) && _flow.get(e) > 0 ) {
568 for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v) {
576 template<typename CutMap>
577 void minCut(CutMap& M) {