.
3 //run gyorsan tudna adni a minmincutot a 2 fazis elejen , ne vegyuk be konstruktorba egy cutmapet?
6 //majd marci megmondja betegyem-e bfs-t meg resgraphot
12 list 'level_list' on the nodes on level i implemented by hand
13 stack 'active' on the active nodes on level i implemented by hand
14 runs heuristic 'highest label' for H1*n relabels
15 runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
17 Parameters H0 and H1 are initialized to 20 and 10.
21 Preflow(Graph, Node, Node, CapMap, FlowMap, bool) : bool must be false if
22 FlowMap is not constant zero, and should be true if it is
28 T flowValue() : returns the value of a maximum flow
30 void minMinCut(CutMap& M) : sets M to the characteristic vector of the
31 minimum min cut. M should be a map of bools initialized to false.
33 void maxMinCut(CutMap& M) : sets M to the characteristic vector of the
34 maximum min cut. M should be a map of bools initialized to false.
36 void minCut(CutMap& M) : sets M to the characteristic vector of
37 a min cut. M should be a map of bools initialized to false.
43 #ifndef HUGO_PREFLOW_PROBA_H
44 #define HUGO_PREFLOW_PROBA_H
51 #include<graph_wrapper.h>
55 template <typename Graph, typename T,
56 typename CapMap=typename Graph::EdgeMap<T>,
57 typename FlowMap=typename Graph::EdgeMap<T> >
60 typedef typename Graph::Node Node;
61 typedef typename Graph::Edge Edge;
62 typedef typename Graph::NodeIt NodeIt;
63 typedef typename Graph::OutEdgeIt OutEdgeIt;
64 typedef typename Graph::InEdgeIt InEdgeIt;
69 const CapMap& capacity;
75 typedef ResGraphWrapper<const Graph, T, CapMap, FlowMap> ResGW;
76 typedef typename ResGW::OutEdgeIt ResOutEdgeIt;
77 typedef typename ResGW::InEdgeIt ResInEdgeIt;
78 typedef typename ResGW::Edge ResEdge;
81 PreflowProba(Graph& _G, Node _s, Node _t, CapMap& _capacity,
82 FlowMap& _flow, bool _constzero, bool _res ) :
83 G(_G), s(_s), t(_t), capacity(_capacity), flow(_flow), constzero(_constzero), res(_res) {}
88 ResGW res_graph(G, capacity, flow);
90 value=0; //for the subsequent runs
92 bool phase=0; //phase 0 is the 1st phase, phase 1 is the 2nd
94 int heur0=(int)(H0*n); //time while running 'bound decrease'
95 int heur1=(int)(H1*n); //time while running 'highest label'
96 int heur=heur1; //starting time interval (#of relabels)
99 what_heur is 0 in case 'bound decrease'
100 and 1 in case 'highest label'
104 Needed for 'bound decrease', 'true'
105 means no active nodes are above bound b.
108 int k=n-2; //bound on the highest level under n containing a node
109 int b=k; //bound on the highest level under n of an active node
111 typename Graph::NodeMap<int> level(G,n);
112 typename Graph::NodeMap<T> excess(G);
114 std::vector<Node> active(n-1,INVALID);
115 typename Graph::NodeMap<Node> next(G,INVALID);
116 //Stack of the active nodes in level i < n.
117 //We use it in both phases.
119 typename Graph::NodeMap<Node> left(G,INVALID);
120 typename Graph::NodeMap<Node> right(G,INVALID);
121 std::vector<Node> level_list(n,INVALID);
123 List of the nodes in level i<n.
129 /*Reverse_bfs from t, to find the starting level.*/
131 std::queue<Node> bfs_queue;
134 while (!bfs_queue.empty()) {
136 Node v=bfs_queue.front();
141 for(G.first(e,v); G.valid(e); G.next(e)) {
143 if ( level[w] == n && w != s ) {
145 Node first=level_list[l];
146 if ( G.valid(first) ) left.set(first,w);
156 for(G.first(e,s); G.valid(e); G.next(e))
159 if ( c == 0 ) continue;
161 if ( level[w] < n ) {
162 if ( excess[w] == 0 && w!=t ) {
163 next.set(w,active[level[w]]);
167 excess.set(w, excess[w]+c);
175 Reverse_bfs from t in the residual graph,
176 to find the starting level.
179 std::queue<Node> bfs_queue;
182 while (!bfs_queue.empty()) {
184 Node v=bfs_queue.front();
189 for(G.first(e,v); G.valid(e); G.next(e)) {
190 if ( capacity[e] == flow[e] ) continue;
192 if ( level[w] == n && w != s ) {
194 Node first=level_list[l];
195 if ( G.valid(first) ) left.set(first,w);
203 for(G.first(f,v); G.valid(f); G.next(f)) {
204 if ( 0 == flow[f] ) continue;
206 if ( level[w] == n && w != s ) {
208 Node first=level_list[l];
209 if ( G.valid(first) ) left.set(first,w);
222 for(G.first(v); G.valid(v); G.next(v)) {
226 for(G.first(e,v); G.valid(e); G.next(e)) exc+=flow[e];
228 for(G.first(f,v); G.valid(f); G.next(f)) exc-=flow[e];
232 //putting the active nodes into the stack
234 if ( exc > 0 && lev < n ) {
235 next.set(v,active[lev]);
243 for(G.first(e,s); G.valid(e); G.next(e))
245 T rem=capacity[e]-flow[e];
246 if ( rem == 0 ) continue;
248 if ( level[w] < n ) {
249 if ( excess[w] == 0 && w!=t ) {
250 next.set(w,active[level[w]]);
253 flow.set(e, capacity[e]);
254 excess.set(w, excess[w]+rem);
259 for(G.first(f,s); G.valid(f); G.next(f))
261 if ( flow[f] == 0 ) continue;
263 if ( level[w] < n ) {
264 if ( excess[w] == 0 && w!=t ) {
265 next.set(w,active[level[w]]);
268 excess.set(w, excess[w]+flow[f]);
284 Push/relabel on the highest level active nodes.
291 if ( !what_heur && !end && k > 0 ) {
297 std::queue<Node> bfs_queue;
300 while (!bfs_queue.empty()) {
302 Node v=bfs_queue.front();
308 for(res_graph.first(e,v); res_graph.valid(e);
310 Node u=res_graph.tail(e);
311 if ( level[u] >= n ) {
314 if ( excess[u] > 0 ) {
315 next.set(u,active[l]);
322 for(G.first(e,v); G.valid(e); G.next(e)) {
323 if ( capacity[e] == flow[e] ) continue;
325 if ( level[u] >= n ) {
328 if ( excess[u] > 0 ) {
329 next.set(u,active[l]);
336 for(G.first(f,v); G.valid(f); G.next(f)) {
337 if ( 0 == flow[f] ) continue;
339 if ( level[u] >= n ) {
342 if ( excess[u] > 0 ) {
343 next.set(u,active[l]);
356 if ( !G.valid(active[b]) ) --b;
364 int newlevel=n; //bound on the next level of w
367 for(G.first(e,w); G.valid(e); G.next(e)) {
369 if ( flow[e] == capacity[e] ) continue;
373 if( lev > level[v] ) {
374 /*Push is allowed now*/
376 if ( excess[v]==0 && v!=t && v!=s ) {
378 next.set(v,active[lev_v]);
386 if ( remcap >= exc ) {
387 /*A nonsaturating push.*/
389 flow.set(e, flo+exc);
390 excess.set(v, excess[v]+exc);
395 /*A saturating push.*/
398 excess.set(v, excess[v]+remcap);
401 } else if ( newlevel > level[v] ){
410 for(G.first(e,w); G.valid(e); G.next(e)) {
412 if( flow[e] == 0 ) continue;
416 if( lev > level[v] ) {
417 /*Push is allowed now*/
419 if ( excess[v]==0 && v!=t && v!=s ) {
421 next.set(v,active[lev_v]);
428 /*A nonsaturating push.*/
430 flow.set(e, flo-exc);
431 excess.set(v, excess[v]+exc);
435 /*A saturating push.*/
437 excess.set(v, excess[v]+flo);
441 } else if ( newlevel > level[v] ) {
446 } // if w still has excess after the out edge for cycle
456 //now 'lev' is the old level of w
459 level.set(w,++newlevel);
460 next.set(w,active[newlevel]);
465 Node right_n=right[w];
468 if ( G.valid(right_n) ) {
469 if ( G.valid(left_n) ) {
470 right.set(left_n, right_n);
471 left.set(right_n, left_n);
473 level_list[lev]=right_n;
474 left.set(right_n, INVALID);
477 if ( G.valid(left_n) ) {
478 right.set(left_n, INVALID);
480 level_list[lev]=INVALID;
485 if ( !G.valid(level_list[lev]) ) {
488 for (int i=lev; i!=k ; ) {
489 Node v=level_list[++i];
490 while ( G.valid(v) ) {
494 level_list[i]=INVALID;
495 if ( !what_heur ) active[i]=INVALID;
505 if ( newlevel == n ) level.set(w,n);
507 level.set(w,++newlevel);
508 next.set(w,active[newlevel]);
510 if ( what_heur ) b=newlevel;
511 if ( k < newlevel ) ++k; //now k=newlevel
512 Node first=level_list[newlevel];
513 if ( G.valid(first) ) left.set(first,w);
516 level_list[newlevel]=w;
522 if ( relabel >= heur ) {
540 } // if stack[b] is nonempty
555 Returns the maximum value of a flow.
569 void Flow(FlowMap& _flow ) {
571 for(G.first(v) ; G.valid(v); G.next(v))
572 _flow.set(v,flow[v]);
578 Returns the minimum min cut, by a bfs from s in the residual graph.
581 template<typename _CutMap>
582 void minMinCut(_CutMap& M) {
584 std::queue<Node> queue;
589 while (!queue.empty()) {
590 Node w=queue.front();
594 for(G.first(e,w) ; G.valid(e); G.next(e)) {
596 if (!M[v] && flow[e] < capacity[e] ) {
603 for(G.first(f,w) ; G.valid(f); G.next(f)) {
605 if (!M[v] && flow[f] > 0 ) {
616 Returns the maximum min cut, by a reverse bfs
617 from t in the residual graph.
620 template<typename _CutMap>
621 void maxMinCut(_CutMap& M) {
623 std::queue<Node> queue;
628 while (!queue.empty()) {
629 Node w=queue.front();
634 for(G.first(e,w) ; G.valid(e); G.next(e)) {
636 if (!M[v] && flow[e] < capacity[e] ) {
643 for(G.first(f,w) ; G.valid(f); G.next(f)) {
645 if (!M[v] && flow[f] > 0 ) {
653 for(G.first(v) ; G.valid(v); G.next(v)) {
661 template<typename CutMap>
662 void minCut(CutMap& M) {
667 void reset_target (Node _t) {t=_t;}
668 void reset_source (Node _s) {s=_s;}
670 template<typename _CapMap>
671 void reset_cap (_CapMap _cap) {capacity=_cap;}
673 template<typename _FlowMap>
674 void reset_cap (_FlowMap _flow, bool _constzero) {
676 constzero=_constzero;
685 #endif //PREFLOW_PROBA_H