Docs.
7 list 'level_list' on the nodes on level i implemented by hand
8 stack 'active' on the active nodes on level i
9 runs heuristic 'highest label' for H1*n relabels
10 runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
12 Parameters H0 and H1 are initialized to 20 and 1.
16 Preflow(Graph, Node, Node, CapMap, FlowMap, bool) : bool must be false if
17 FlowMap is not constant zero, and should be true if it is
23 T flowValue() : returns the value of a maximum flow
25 void minMinCut(CutMap& M) : sets M to the characteristic vector of the
26 minimum min cut. M should be a map of bools initialized to false. ??Is it OK?
28 void maxMinCut(CutMap& M) : sets M to the characteristic vector of the
29 maximum min cut. M should be a map of bools initialized to false.
31 void minCut(CutMap& M) : sets M to the characteristic vector of
32 a min cut. M should be a map of bools initialized to false.
36 #ifndef HUGO_PREFLOW_H
37 #define HUGO_PREFLOW_H
48 template <typename Graph, typename T,
49 typename CapMap=typename Graph::template EdgeMap<T>,
50 typename FlowMap=typename Graph::template EdgeMap<T> >
53 typedef typename Graph::Node Node;
54 typedef typename Graph::NodeIt NodeIt;
55 typedef typename Graph::OutEdgeIt OutEdgeIt;
56 typedef typename Graph::InEdgeIt InEdgeIt;
58 typedef typename std::vector<std::stack<Node> > VecStack;
59 typedef typename Graph::template NodeMap<Node> NNMap;
60 typedef typename std::vector<Node> VecNode;
67 int n; //the number of nodes of G
68 typename Graph::template NodeMap<int> level;
69 typename Graph::template NodeMap<T> excess;
80 Preflow(Graph& _G, Node _s, Node _t, CapMap& _capacity,
82 G(_G), s(_s), t(_t), capacity(&_capacity),
83 flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0) {}
89 void preflow( flowEnum fe ) {
94 void preflowPhase0( flowEnum fe ) {
96 int heur0=(int)(H0*n); //time while running 'bound decrease'
97 int heur1=(int)(H1*n); //time while running 'highest label'
98 int heur=heur1; //starting time interval (#of relabels)
102 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
105 //Needed for 'bound decrease', true means no active nodes are above bound b.
107 int k=n-2; //bound on the highest level under n containing a node
108 int b=k; //bound on the highest level under n of an active node
112 NNMap left(G,INVALID);
113 NNMap right(G,INVALID);
114 VecNode level_list(n,INVALID);
115 //List of the nodes in level i<n, set to n.
118 for(G.first(v); G.valid(v); G.next(v)) level.set(v,n);
119 //setting each node to level n
124 //counting the excess
126 for(G.first(v); G.valid(v); G.next(v)) {
130 for(G.first(e,v); G.valid(e); G.next(e)) exc+=(*flow)[e];
132 for(G.first(f,v); G.valid(f); G.next(f)) exc-=(*flow)[f];
136 //putting the active nodes into the stack
138 if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
144 //Counting the excess of t
148 for(G.first(e,t); G.valid(e); G.next(e)) exc+=(*flow)[e];
150 for(G.first(f,t); G.valid(f); G.next(f)) exc-=(*flow)[f];
158 preflowPreproc( fe, active, level_list, left, right );
159 //End of preprocessing
162 //Push/relabel on the highest level active nodes.
165 if ( !what_heur && !end && k > 0 ) {
171 if ( active[b].empty() ) --b;
174 Node w=active[b].top();
176 int newlevel=push(w,active);
177 if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
178 left, right, b, k, what_heur);
181 if ( numrelabel >= heur ) {
198 void preflowPhase1() {
200 int k=n-2; //bound on the highest level under n containing a node
201 int b=k; //bound on the highest level under n of an active node
205 std::queue<Node> bfs_queue;
208 while (!bfs_queue.empty()) {
210 Node v=bfs_queue.front();
215 for(G.first(e,v); G.valid(e); G.next(e)) {
216 if ( (*capacity)[e] == (*flow)[e] ) continue;
218 if ( level[u] >= n ) {
221 if ( excess[u] > 0 ) active[l].push(u);
226 for(G.first(f,v); G.valid(f); G.next(f)) {
227 if ( 0 == (*flow)[f] ) continue;
229 if ( level[u] >= n ) {
232 if ( excess[u] > 0 ) active[l].push(u);
242 if ( active[b].empty() ) --b;
244 Node w=active[b].top();
246 int newlevel=push(w,active);
249 if ( excess[w] > 0 ) {
250 level.set(w,++newlevel);
251 active[newlevel].push(w);
254 } // if stack[b] is nonempty
259 //Returns the maximum value of a flow.
264 //should be used only between preflowPhase0 and preflowPhase1
265 template<typename _CutMap>
266 void actMinCut(_CutMap& M) {
268 for(G.first(v); G.valid(v); G.next(v))
269 if ( level[v] < n ) M.set(v,false);
276 Returns the minimum min cut, by a bfs from s in the residual graph.
278 template<typename _CutMap>
279 void minMinCut(_CutMap& M) {
281 std::queue<Node> queue;
286 while (!queue.empty()) {
287 Node w=queue.front();
291 for(G.first(e,w) ; G.valid(e); G.next(e)) {
293 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
300 for(G.first(f,w) ; G.valid(f); G.next(f)) {
302 if (!M[v] && (*flow)[f] > 0 ) {
313 Returns the maximum min cut, by a reverse bfs
314 from t in the residual graph.
317 template<typename _CutMap>
318 void maxMinCut(_CutMap& M) {
321 for(G.first(v) ; G.valid(v); G.next(v)) {
325 std::queue<Node> queue;
330 while (!queue.empty()) {
331 Node w=queue.front();
336 for(G.first(e,w) ; G.valid(e); G.next(e)) {
338 if (M[v] && (*flow)[e] < (*capacity)[e] ) {
345 for(G.first(f,w) ; G.valid(f); G.next(f)) {
347 if (M[v] && (*flow)[f] > 0 ) {
356 template<typename CutMap>
357 void minCut(CutMap& M) {
362 void resetTarget (const Node _t) {t=_t;}
364 void resetSource (const Node _s) {s=_s;}
366 void resetCap (const CapMap& _cap) {
370 void resetFlow (FlowMap& _flow) {
377 int push(const Node w, VecStack& active) {
381 int newlevel=n; //bound on the next level of w
384 for(G.first(e,w); G.valid(e); G.next(e)) {
386 if ( (*flow)[e] == (*capacity)[e] ) continue;
389 if( lev > level[v] ) { //Push is allowed now
391 if ( excess[v]==0 && v!=t && v!=s ) {
393 active[lev_v].push(v);
396 T cap=(*capacity)[e];
400 if ( remcap >= exc ) { //A nonsaturating push.
402 flow->set(e, flo+exc);
403 excess.set(v, excess[v]+exc);
407 } else { //A saturating push.
409 excess.set(v, excess[v]+remcap);
412 } else if ( newlevel > level[v] ) newlevel = level[v];
417 for(G.first(e,w); G.valid(e); G.next(e)) {
419 if( (*flow)[e] == 0 ) continue;
422 if( lev > level[v] ) { //Push is allowed now
424 if ( excess[v]==0 && v!=t && v!=s ) {
426 active[lev_v].push(v);
431 if ( flo >= exc ) { //A nonsaturating push.
433 flow->set(e, flo-exc);
434 excess.set(v, excess[v]+exc);
437 } else { //A saturating push.
439 excess.set(v, excess[v]+flo);
443 } else if ( newlevel > level[v] ) newlevel = level[v];
446 } // if w still has excess after the out edge for cycle
454 void preflowPreproc ( flowEnum fe, VecStack& active,
455 VecNode& level_list, NNMap& left, NNMap& right ) {
457 std::queue<Node> bfs_queue;
462 //Reverse_bfs from t, to find the starting level.
466 while (!bfs_queue.empty()) {
468 Node v=bfs_queue.front();
473 for(G.first(e,v); G.valid(e); G.next(e)) {
475 if ( level[w] == n && w != s ) {
477 Node first=level_list[l];
478 if ( G.valid(first) ) left.set(first,w);
488 for(G.first(e,s); G.valid(e); G.next(e))
491 if ( c == 0 ) continue;
493 if ( level[w] < n ) {
494 if ( excess[w] == 0 && w!=t ) active[level[w]].push(w);
496 excess.set(w, excess[w]+c);
505 //Reverse_bfs from t in the residual graph,
506 //to find the starting level.
510 while (!bfs_queue.empty()) {
512 Node v=bfs_queue.front();
517 for(G.first(e,v); G.valid(e); G.next(e)) {
518 if ( (*capacity)[e] == (*flow)[e] ) continue;
520 if ( level[w] == n && w != s ) {
522 Node first=level_list[l];
523 if ( G.valid(first) ) left.set(first,w);
531 for(G.first(f,v); G.valid(f); G.next(f)) {
532 if ( 0 == (*flow)[f] ) continue;
534 if ( level[w] == n && w != s ) {
536 Node first=level_list[l];
537 if ( G.valid(first) ) left.set(first,w);
548 for(G.first(e,s); G.valid(e); G.next(e))
550 T rem=(*capacity)[e]-(*flow)[e];
551 if ( rem == 0 ) continue;
553 if ( level[w] < n ) {
554 if ( excess[w] == 0 && w!=t ) active[level[w]].push(w);
555 flow->set(e, (*capacity)[e]);
556 excess.set(w, excess[w]+rem);
561 for(G.first(f,s); G.valid(f); G.next(f))
563 if ( (*flow)[f] == 0 ) continue;
565 if ( level[w] < n ) {
566 if ( excess[w] == 0 && w!=t ) active[level[w]].push(w);
567 excess.set(w, excess[w]+(*flow)[f]);
578 void relabel( const Node w, int newlevel, VecStack& active,
579 VecNode& level_list, NNMap& left,
580 NNMap& right, int& b, int& k, const bool what_heur ) {
584 Node right_n=right[w];
588 if ( G.valid(right_n) ) {
589 if ( G.valid(left_n) ) {
590 right.set(left_n, right_n);
591 left.set(right_n, left_n);
593 level_list[lev]=right_n;
594 left.set(right_n, INVALID);
597 if ( G.valid(left_n) ) {
598 right.set(left_n, INVALID);
600 level_list[lev]=INVALID;
605 if ( !G.valid(level_list[lev]) ) {
608 for (int i=lev; i!=k ; ) {
609 Node v=level_list[++i];
610 while ( G.valid(v) ) {
614 level_list[i]=INVALID;
616 while ( !active[i].empty() ) {
617 active[i].pop(); //FIXME: ezt szebben kene
629 if ( newlevel == n ) level.set(w,n);
631 level.set(w,++newlevel);
632 active[newlevel].push(w);
633 if ( what_heur ) b=newlevel;
634 if ( k < newlevel ) ++k; //now k=newlevel
635 Node first=level_list[newlevel];
636 if ( G.valid(first) ) left.set(first,w);
639 level_list[newlevel]=w;
650 #endif //HUGO_PREFLOW_H