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 Num 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
46 #include <graph_wrapper.h>
50 template <typename Graph, typename Num,
51 typename CapMap=typename Graph::template EdgeMap<Num>,
52 typename FlowMap=typename Graph::template EdgeMap<Num> >
55 typedef typename Graph::Node Node;
56 typedef typename Graph::NodeIt NodeIt;
57 typedef typename Graph::OutEdgeIt OutEdgeIt;
58 typedef typename Graph::InEdgeIt InEdgeIt;
60 typedef typename std::vector<std::stack<Node> > VecStack;
61 typedef typename Graph::template NodeMap<Node> NNMap;
62 typedef typename std::vector<Node> VecNode;
67 const CapMap* capacity;
69 int n; //the number of nodes of G
70 typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
71 typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
72 typedef typename ResGW::Edge ResGWEdge;
73 //typedef typename ResGW::template NodeMap<bool> ReachedMap;
74 typedef typename Graph::template NodeMap<int> ReachedMap;
76 //level works as a bool map in augmenting path algorithms
77 //and is used by bfs for storing reached information.
78 //In preflow, it shows levels of nodes.
79 //typename Graph::template NodeMap<int> level;
80 typename Graph::template NodeMap<Num> excess;
90 Preflow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
92 g(&_G), s(_s), t(_t), capacity(&_capacity),
93 flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0) {}
99 void preflow( flowEnum fe ) {
104 void preflowPhase0( flowEnum fe );
106 void preflowPhase1();
108 bool augmentOnShortestPath();
110 template<typename MutableGraph> bool augmentOnBlockingFlow();
112 bool augmentOnBlockingFlow2();
114 //Returns the maximum value of a flow.
119 //should be used only between preflowPhase0 and preflowPhase1
120 template<typename _CutMap>
121 void actMinCut(_CutMap& M) {
123 for(g->first(v); g->valid(v); g->next(v))
124 if ( level[v] < n ) {
134 Returns the minimum min cut, by a bfs from s in the residual graph.
136 template<typename _CutMap>
137 void minMinCut(_CutMap& M) {
139 std::queue<Node> queue;
144 while (!queue.empty()) {
145 Node w=queue.front();
149 for(g->first(e,w) ; g->valid(e); g->next(e)) {
151 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
158 for(g->first(f,w) ; g->valid(f); g->next(f)) {
160 if (!M[v] && (*flow)[f] > 0 ) {
171 Returns the maximum min cut, by a reverse bfs
172 from t in the residual graph.
175 template<typename _CutMap>
176 void maxMinCut(_CutMap& M) {
179 for(g->first(v) ; g->valid(v); g->next(v)) {
183 std::queue<Node> queue;
188 while (!queue.empty()) {
189 Node w=queue.front();
194 for(g->first(e,w) ; g->valid(e); g->next(e)) {
196 if (M[v] && (*flow)[e] < (*capacity)[e] ) {
203 for(g->first(f,w) ; g->valid(f); g->next(f)) {
205 if (M[v] && (*flow)[f] > 0 ) {
214 template<typename CutMap>
215 void minCut(CutMap& M) {
219 void resetTarget(Node _t) {t=_t;}
220 void resetSource(Node _s) {s=_s;}
222 void resetCap(const CapMap& _cap) {
226 void resetFlow(FlowMap& _flow) {
233 int push(Node w, VecStack& active) {
237 int newlevel=n; //bound on the next level of w
240 for(g->first(e,w); g->valid(e); g->next(e)) {
242 if ( (*flow)[e] >= (*capacity)[e] ) continue;
245 if( lev > level[v] ) { //Push is allowed now
247 if ( excess[v]<=0 && v!=t && v!=s ) {
249 active[lev_v].push(v);
252 Num cap=(*capacity)[e];
256 if ( remcap >= exc ) { //A nonsaturating push.
258 flow->set(e, flo+exc);
259 excess.set(v, excess[v]+exc);
263 } else { //A saturating push.
265 excess.set(v, excess[v]+remcap);
268 } else if ( newlevel > level[v] ) newlevel = level[v];
273 for(g->first(e,w); g->valid(e); g->next(e)) {
275 if( (*flow)[e] <= 0 ) continue;
278 if( lev > level[v] ) { //Push is allowed now
280 if ( excess[v]<=0 && v!=t && v!=s ) {
282 active[lev_v].push(v);
287 if ( flo >= exc ) { //A nonsaturating push.
289 flow->set(e, flo-exc);
290 excess.set(v, excess[v]+exc);
293 } else { //A saturating push.
295 excess.set(v, excess[v]+flo);
299 } else if ( newlevel > level[v] ) newlevel = level[v];
302 } // if w still has excess after the out edge for cycle
310 void preflowPreproc ( flowEnum fe, VecStack& active,
311 VecNode& level_list, NNMap& left, NNMap& right ) {
313 std::queue<Node> bfs_queue;
318 //Reverse_bfs from t, to find the starting level.
322 while (!bfs_queue.empty()) {
324 Node v=bfs_queue.front();
329 for(g->first(e,v); g->valid(e); g->next(e)) {
331 if ( level[w] == n && w != s ) {
333 Node first=level_list[l];
334 if ( g->valid(first) ) left.set(first,w);
344 for(g->first(e,s); g->valid(e); g->next(e))
346 Num c=(*capacity)[e];
347 if ( c <= 0 ) continue;
349 if ( level[w] < n ) {
350 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
352 excess.set(w, excess[w]+c);
361 //Reverse_bfs from t in the residual graph,
362 //to find the starting level.
366 while (!bfs_queue.empty()) {
368 Node v=bfs_queue.front();
373 for(g->first(e,v); g->valid(e); g->next(e)) {
374 if ( (*capacity)[e] <= (*flow)[e] ) continue;
376 if ( level[w] == n && w != s ) {
378 Node first=level_list[l];
379 if ( g->valid(first) ) left.set(first,w);
387 for(g->first(f,v); g->valid(f); g->next(f)) {
388 if ( 0 >= (*flow)[f] ) continue;
390 if ( level[w] == n && w != s ) {
392 Node first=level_list[l];
393 if ( g->valid(first) ) left.set(first,w);
404 for(g->first(e,s); g->valid(e); g->next(e))
406 Num rem=(*capacity)[e]-(*flow)[e];
407 if ( rem <= 0 ) continue;
409 if ( level[w] < n ) {
410 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
411 flow->set(e, (*capacity)[e]);
412 excess.set(w, excess[w]+rem);
417 for(g->first(f,s); g->valid(f); g->next(f))
419 if ( (*flow)[f] <= 0 ) continue;
421 if ( level[w] < n ) {
422 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
423 excess.set(w, excess[w]+(*flow)[f]);
434 void relabel(Node w, int newlevel, VecStack& active,
435 VecNode& level_list, NNMap& left,
436 NNMap& right, int& b, int& k, bool what_heur ) {
440 Node right_n=right[w];
444 if ( g->valid(right_n) ) {
445 if ( g->valid(left_n) ) {
446 right.set(left_n, right_n);
447 left.set(right_n, left_n);
449 level_list[lev]=right_n;
450 left.set(right_n, INVALID);
453 if ( g->valid(left_n) ) {
454 right.set(left_n, INVALID);
456 level_list[lev]=INVALID;
461 if ( !g->valid(level_list[lev]) ) {
464 for (int i=lev; i!=k ; ) {
465 Node v=level_list[++i];
466 while ( g->valid(v) ) {
470 level_list[i]=INVALID;
472 while ( !active[i].empty() ) {
473 active[i].pop(); //FIXME: ezt szebben kene
485 if ( newlevel == n ) level.set(w,n);
487 level.set(w,++newlevel);
488 active[newlevel].push(w);
489 if ( what_heur ) b=newlevel;
490 if ( k < newlevel ) ++k; //now k=newlevel
491 Node first=level_list[newlevel];
492 if ( g->valid(first) ) left.set(first,w);
495 level_list[newlevel]=w;
504 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
505 void Preflow<Graph, Num, CapMap, FlowMap>::preflowPhase0( flowEnum fe )
508 int heur0=(int)(H0*n); //time while running 'bound decrease'
509 int heur1=(int)(H1*n); //time while running 'highest label'
510 int heur=heur1; //starting time interval (#of relabels)
514 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
517 //Needed for 'bound decrease', true means no active nodes are above bound b.
519 int k=n-2; //bound on the highest level under n containing a node
520 int b=k; //bound on the highest level under n of an active node
524 NNMap left(*g, INVALID);
525 NNMap right(*g, INVALID);
526 VecNode level_list(n,INVALID);
527 //List of the nodes in level i<n, set to n.
530 for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
531 //setting each node to level n
536 //counting the excess
538 for(g->first(v); g->valid(v); g->next(v)) {
542 for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
544 for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
548 //putting the active nodes into the stack
550 if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
556 //Counting the excess of t
560 for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
562 for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
572 preflowPreproc( fe, active, level_list, left, right );
573 //End of preprocessing
576 //Push/relabel on the highest level active nodes.
579 if ( !what_heur && !end && k > 0 ) {
585 if ( active[b].empty() ) --b;
588 Node w=active[b].top();
590 int newlevel=push(w,active);
591 if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
592 left, right, b, k, what_heur);
595 if ( numrelabel >= heur ) {
613 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
614 void Preflow<Graph, Num, CapMap, FlowMap>::preflowPhase1()
617 int k=n-2; //bound on the highest level under n containing a node
618 int b=k; //bound on the highest level under n of an active node
622 std::queue<Node> bfs_queue;
625 while (!bfs_queue.empty()) {
627 Node v=bfs_queue.front();
632 for(g->first(e,v); g->valid(e); g->next(e)) {
633 if ( (*capacity)[e] <= (*flow)[e] ) continue;
635 if ( level[u] >= n ) {
638 if ( excess[u] > 0 ) active[l].push(u);
643 for(g->first(f,v); g->valid(f); g->next(f)) {
644 if ( 0 >= (*flow)[f] ) continue;
646 if ( level[u] >= n ) {
649 if ( excess[u] > 0 ) active[l].push(u);
659 if ( active[b].empty() ) --b;
661 Node w=active[b].top();
663 int newlevel=push(w,active);
666 if ( excess[w] > 0 ) {
667 level.set(w,++newlevel);
668 active[newlevel].push(w);
671 } // if stack[b] is nonempty
688 #endif //HUGO_PREFLOW_H