jacint@72: // -*- C++ -*- jacint@72: /* jacint@83: preflow_push_hl.h jacint@72: by jacint. jacint@72: Runs the highest label variant of the preflow push algorithm with jacint@97: running time O(n^2\sqrt(m)), and with the 'empty level' heuristic. jacint@97: jacint@97: 'A' is a parameter for the empty_level heuristic jacint@72: jacint@72: Member functions: jacint@72: jacint@72: void run() : runs the algorithm jacint@72: jacint@72: The following functions should be used after run() was already run. jacint@72: jacint@72: T maxflow() : returns the value of a maximum flow jacint@72: jacint@83: T flowonedge(EdgeIt e) : for a fixed maximum flow x it returns x(e) jacint@72: jacint@97: FlowMap allflow() : returns the fixed maximum flow x jacint@72: jacint@97: void mincut(CutMap& M) : sets M to the characteristic vector of a jacint@97: minimum cut. M should be a map of bools initialized to false. jacint@97: jacint@97: void min_mincut(CutMap& M) : sets M to the characteristic vector of the jacint@97: minimum min cut. M should be a map of bools initialized to false. jacint@97: jacint@97: void max_mincut(CutMap& M) : sets M to the characteristic vector of the jacint@97: maximum min cut. M should be a map of bools initialized to false. jacint@97: jacint@72: */ jacint@72: jacint@72: #ifndef PREFLOW_PUSH_HL_H jacint@72: #define PREFLOW_PUSH_HL_H jacint@72: jacint@88: #define A 1 jacint@88: jacint@72: #include jacint@72: #include jacint@97: #include jacint@72: jacint@72: namespace marci { jacint@72: jacint@97: template , typename CapMap=typename Graph::EdgeMap, jacint@97: typename IntMap=typename Graph::NodeMap, typename TMap=typename Graph::NodeMap > jacint@72: class preflow_push_hl { jacint@72: jacint@72: typedef typename Graph::NodeIt NodeIt; jacint@72: typedef typename Graph::EdgeIt EdgeIt; jacint@72: typedef typename Graph::EachNodeIt EachNodeIt; jacint@72: typedef typename Graph::OutEdgeIt OutEdgeIt; jacint@72: typedef typename Graph::InEdgeIt InEdgeIt; jacint@72: jacint@72: Graph& G; jacint@72: NodeIt s; jacint@72: NodeIt t; jacint@97: FlowMap flow; jacint@97: CapMap& capacity; jacint@72: T value; jacint@97: jacint@72: public: jacint@72: jacint@97: preflow_push_hl(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity) : jacint@97: G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity) { } jacint@72: jacint@72: jacint@97: jacint@97: jacint@72: void run() { jacint@72: jacint@84: int n=G.nodeNum(); jacint@83: int b=n-2; jacint@83: /* jacint@83: b is a bound on the highest level of an active node. jacint@83: */ jacint@72: jacint@97: IntMap level(G,n); jacint@97: TMap excess(G); jacint@97: jacint@97: std::vector numb(n); jacint@97: /* jacint@97: The number of nodes on level i < n. It is jacint@97: initialized to n+1, because of the reverse_bfs-part. jacint@97: */ jacint@97: jacint@83: std::vector > stack(2*n-1); jacint@83: //Stack of the active nodes in level i. jacint@72: jacint@72: jacint@72: /*Reverse_bfs from t, to find the starting level.*/ jacint@97: level.set(t,0); jacint@97: std::queue bfs_queue; jacint@97: bfs_queue.push(t); jacint@97: jacint@97: while (!bfs_queue.empty()) { jacint@97: jacint@97: NodeIt v=bfs_queue.front(); jacint@97: bfs_queue.pop(); jacint@97: int l=level.get(v)+1; jacint@97: jacint@97: for(InEdgeIt e=G.template first(v); e.valid(); ++e) { jacint@97: NodeIt w=G.tail(e); jacint@97: if ( level.get(w) == n ) { jacint@97: bfs_queue.push(w); jacint@97: ++numb[l]; jacint@97: level.set(w, l); jacint@97: } jacint@83: } jacint@97: } jacint@97: jacint@97: level.set(s,n); jacint@72: jacint@72: jacint@72: jacint@83: /* Starting flow. It is everywhere 0 at the moment. */ jacint@83: for(OutEdgeIt e=G.template first(s); e.valid(); ++e) jacint@72: { jacint@97: if ( capacity.get(e) == 0 ) continue; jacint@97: NodeIt w=G.head(e); jacint@97: if ( w!=s ) { jacint@97: if ( excess.get(w) == 0 && w!=t ) stack[level.get(w)].push(w); jacint@97: flow.set(e, capacity.get(e)); jacint@97: excess.set(w, excess.get(w)+capacity.get(e)); jacint@83: } jacint@72: } jacint@97: jacint@72: /* jacint@72: End of preprocessing jacint@72: */ jacint@72: jacint@72: jacint@72: /* jacint@84: Push/relabel on the highest level active nodes. jacint@72: */ jacint@85: /*While there exists an active node.*/ jacint@72: while (b) { jacint@97: if ( stack[b].empty() ) { jacint@72: --b; jacint@97: continue; jacint@97: } jacint@72: jacint@97: NodeIt w=stack[b].top(); //w is a highest label active node. jacint@97: stack[b].pop(); jacint@97: int lev=level.get(w); jacint@97: int exc=excess.get(w); jacint@97: int newlevel=2*n-2; //In newlevel we bound the next level of w. jacint@72: jacint@97: // if ( level.get(w) < n ) { //Nem tudom ez mukodik-e jacint@72: for(OutEdgeIt e=G.template first(w); e.valid(); ++e) { jacint@84: jacint@97: if ( flow.get(e) == capacity.get(e) ) continue; jacint@97: NodeIt v=G.head(e); jacint@97: //e=wv jacint@97: jacint@97: if( lev > level.get(v) ) { jacint@97: /*Push is allowed now*/ jacint@97: jacint@97: if ( excess.get(v)==0 && v != s && v !=t ) jacint@97: stack[level.get(v)].push(v); jacint@97: /*v becomes active.*/ jacint@97: jacint@97: int cap=capacity.get(e); jacint@97: int flo=flow.get(e); jacint@97: int remcap=cap-flo; jacint@97: jacint@97: if ( remcap >= exc ) { jacint@97: /*A nonsaturating push.*/ jacint@97: jacint@97: flow.set(e, flo+exc); jacint@97: excess.set(v, excess.get(v)+exc); jacint@97: exc=0; jacint@97: break; jacint@97: jacint@97: } else { jacint@97: /*A saturating push.*/ jacint@97: jacint@97: flow.set(e, cap ); jacint@97: excess.set(v, excess.get(v)+remcap); jacint@97: exc-=remcap; jacint@97: } jacint@97: } else if ( newlevel > level.get(v) ) newlevel = level.get(v); jacint@97: jacint@97: } //for out edges wv jacint@97: jacint@97: jacint@97: if ( exc > 0 ) { jacint@97: for( InEdgeIt e=G.template first(w); e.valid(); ++e) { jacint@97: jacint@97: if( flow.get(e) == 0 ) continue; jacint@97: NodeIt v=G.tail(e); jacint@97: //e=vw jacint@97: jacint@97: if( lev > level.get(v) ) { jacint@97: /*Push is allowed now*/ jacint@97: jacint@97: if ( excess.get(v)==0 && v != s && v !=t) jacint@97: stack[level.get(v)].push(v); jacint@97: /*v becomes active.*/ jacint@97: jacint@97: int flo=flow.get(e); jacint@97: jacint@97: if ( flo >= exc ) { jacint@97: /*A nonsaturating push.*/ jacint@97: jacint@97: flow.set(e, flo-exc); jacint@97: excess.set(v, excess.get(v)+exc); jacint@97: exc=0; jacint@97: break; jacint@97: } else { jacint@97: /*A saturating push.*/ jacint@97: jacint@97: excess.set(v, excess.get(v)+flo); jacint@97: exc-=flo; jacint@97: flow.set(e,0); jacint@97: } jacint@97: } else if ( newlevel > level.get(v) ) newlevel = level.get(v); jacint@97: jacint@97: } //for in edges vw jacint@97: jacint@97: } // if w still has excess after the out edge for cycle jacint@97: jacint@97: excess.set(w, exc); jacint@97: jacint@72: jacint@97: jacint@72: jacint@97: /* jacint@97: Relabel jacint@97: */ jacint@97: jacint@97: if ( exc > 0 ) { jacint@97: //now 'lev' is the old level of w jacint@97: level.set(w,++newlevel); jacint@97: jacint@97: if ( lev < n ) { jacint@97: --numb[lev]; jacint@97: jacint@97: if ( !numb[lev] && lev < A*n ) { //If the level of w gets empty. jacint@97: jacint@97: for (EachNodeIt v=G.template first(); v.valid() ; ++v) { jacint@97: if (level.get(v) > lev && level.get(v) < n ) level.set(v,n); jacint@85: } jacint@97: for (int i=lev+1 ; i!=n ; ++i) numb[i]=0; jacint@97: if ( newlevel < n ) newlevel=n; jacint@97: } else { jacint@97: if ( newlevel < n ) ++numb[newlevel]; jacint@97: } jacint@97: } jacint@72: jacint@97: stack[newlevel].push(w); jacint@97: b=newlevel; jacint@85: jacint@97: } jacint@97: jacint@85: } // while(b) jacint@97: jacint@97: jacint@72: value = excess.get(t); jacint@72: /*Max flow value.*/ jacint@72: jacint@72: jacint@72: } //void run() jacint@72: jacint@72: jacint@72: jacint@72: jacint@72: jacint@72: /* jacint@72: Returns the maximum value of a flow. jacint@72: */ jacint@72: jacint@72: T maxflow() { jacint@72: return value; jacint@72: } jacint@72: jacint@72: jacint@72: jacint@72: /* jacint@72: For the maximum flow x found by the algorithm, it returns the flow value on Edge e, i.e. x(e). jacint@72: */ jacint@72: jacint@97: T flowonedge(const EdgeIt e) { jacint@72: return flow.get(e); jacint@72: } jacint@72: jacint@72: jacint@72: jacint@72: /* jacint@72: Returns the maximum flow x found by the algorithm. jacint@72: */ jacint@72: jacint@97: FlowMap allflow() { jacint@72: return flow; jacint@72: } jacint@72: jacint@72: jacint@72: jacint@97: jacint@72: /* jacint@97: Returns the minimum min cut, by a bfs from s in the residual graph. jacint@72: */ jacint@72: jacint@97: template jacint@97: void mincut(CutMap& M) { jacint@72: jacint@72: std::queue queue; jacint@72: jacint@97: M.set(s,true); jacint@97: queue.push(s); jacint@97: jacint@97: while (!queue.empty()) { jacint@97: NodeIt w=queue.front(); jacint@97: queue.pop(); jacint@97: jacint@97: for(OutEdgeIt e=G.template first(w) ; e.valid(); ++e) { jacint@97: NodeIt v=G.head(e); jacint@97: if (!M.get(v) && flow.get(e) < capacity.get(e) ) { jacint@97: queue.push(v); jacint@97: M.set(v, true); jacint@97: } jacint@97: } jacint@97: jacint@97: for(InEdgeIt e=G.template first(w) ; e.valid(); ++e) { jacint@97: NodeIt v=G.tail(e); jacint@97: if (!M.get(v) && flow.get(e) > 0 ) { jacint@97: queue.push(v); jacint@97: M.set(v, true); jacint@97: } jacint@97: } jacint@97: jacint@97: } jacint@97: } jacint@97: jacint@97: jacint@97: jacint@97: /* jacint@97: Returns the maximum min cut, by a reverse bfs jacint@97: from t in the residual graph. jacint@97: */ jacint@97: jacint@97: template jacint@97: void max_mincut(CutMap& M) { jacint@97: jacint@97: std::queue queue; jacint@97: jacint@97: M.set(t,true); jacint@72: queue.push(t); jacint@72: jacint@72: while (!queue.empty()) { jacint@72: NodeIt w=queue.front(); jacint@72: queue.pop(); jacint@72: jacint@72: for(InEdgeIt e=G.template first(w) ; e.valid(); ++e) { jacint@72: NodeIt v=G.tail(e); jacint@97: if (!M.get(v) && flow.get(e) < capacity.get(e) ) { jacint@72: queue.push(v); jacint@97: M.set(v, true); jacint@72: } jacint@97: } jacint@72: jacint@72: for(OutEdgeIt e=G.template first(w) ; e.valid(); ++e) { jacint@72: NodeIt v=G.head(e); jacint@97: if (!M.get(v) && flow.get(e) > 0 ) { jacint@72: queue.push(v); jacint@97: M.set(v, true); jacint@72: } jacint@97: } jacint@72: } jacint@72: jacint@97: for(EachNodeIt v=G.template first() ; v.valid(); ++v) { jacint@97: M.set(v, !M.get(v)); jacint@97: } jacint@97: jacint@72: } jacint@97: jacint@97: jacint@97: jacint@97: template jacint@97: void min_mincut(CutMap& M) { jacint@97: mincut(M); jacint@97: } jacint@97: jacint@97: jacint@97: jacint@72: }; jacint@72: }//namespace marci jacint@72: #endif jacint@72: jacint@72: jacint@72: jacint@72: