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