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@109: running time O(n^2\sqrt(m)). jacint@98: jacint@109: Heuristics: jacint@98: jacint@109: gap: we iterate through the nodes for finding the nodes above jacint@109: the gap and under level n. So it is quite slow. jacint@109: numb: we maintain the number of nodes in level i. jacint@109: highest label jacint@109: jacint@109: 'A' is a parameter for the gap, we only upgrade the nodes to level n, jacint@109: if the gap is under A*n. jacint@98: jacint@109: The constructor runs the algorithm. jacint@98: jacint@109: Members: jacint@98: jacint@109: T maxFlow() : returns the value of a maximum flow jacint@98: jacint@109: T flowOnEdge(EdgeIt e) : for a fixed maximum flow x it returns x(e) jacint@98: jacint@109: FlowMap Flow() : returns the fixed maximum flow x jacint@98: jacint@109: 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@109: void minMinCut(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@109: void maxMinCut(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@109: #define A .9 jacint@98: jacint@98: #include jacint@98: #include jacint@98: #include jacint@98: alpar@105: namespace hugo { jacint@98: jacint@98: template , jacint@109: typename CapMap=typename Graph::EdgeMap > 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@109: G(_G), s(_s), t(_t), flow(_G), capacity(_capacity) { jacint@98: 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: */ jacint@98: jacint@109: typename Graph::NodeMap level(G,n); jacint@109: typename Graph::NodeMap excess(G); jacint@109: jacint@101: std::vector numb(n); 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@109: 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@101: T c=capacity.get(e); jacint@101: if ( c == 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@101: flow.set(e, c); jacint@101: excess.set(w, excess.get(w)+c); jacint@101: } jacint@98: } jacint@109: jacint@98: /* jacint@98: End of preprocessing jacint@98: */ jacint@98: jacint@98: jacint@98: /* jacint@98: Push/relabel on the highest level active nodes. jacint@109: */ jacint@98: /*While there exists an active node.*/ jacint@98: while (b) { jacint@109: if ( stack[b].empty() ) { jacint@98: --b; jacint@109: continue; jacint@109: } jacint@109: jacint@109: NodeIt w=stack[b].top(); jacint@109: stack[b].pop(); jacint@109: int lev=level.get(w); jacint@109: T exc=excess.get(w); jacint@109: int newlevel=2*n; //In newlevel we bound the next level of w. jacint@109: 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@109: T cap=capacity.get(e); jacint@109: T flo=flow.get(e); jacint@109: T 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@109: T 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@109: jacint@109: excess.set(w, exc); jacint@109: jacint@109: jacint@109: jacint@109: jacint@109: /* jacint@109: Relabel jacint@109: */ jacint@109: jacint@109: if ( exc > 0 ) { jacint@109: //now 'lev' is the old level of w jacint@109: level.set(w,++newlevel); jacint@98: jacint@109: if ( lev < n ) { jacint@109: --numb[lev]; jacint@109: jacint@109: if ( !numb[lev] && lev < A*n ) { //If the level of w gets empty. jacint@109: jacint@109: for (EachNodeIt v=G.template first(); v.valid() ; ++v) { jacint@109: if (level.get(v) > lev && level.get(v) < n ) level.set(v,n); jacint@109: } jacint@109: for (int i=lev+1 ; i!=n ; ++i) numb[i]=0; jacint@109: if ( newlevel < n ) newlevel=n; jacint@109: } else { jacint@109: if ( newlevel < n ) ++numb[newlevel]; jacint@109: } jacint@109: } jacint@98: jacint@109: stack[newlevel].push(w); jacint@109: b=newlevel; jacint@109: jacint@109: } jacint@109: jacint@98: } // while(b) jacint@109: jacint@109: 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@109: T maxFlow() { jacint@98: return value; jacint@98: } jacint@98: jacint@98: jacint@98: jacint@98: /* jacint@109: For the maximum flow x found by the algorithm, jacint@109: it returns the flow value on edge e, i.e. x(e). jacint@98: */ jacint@98: jacint@109: T flowOnEdge(const 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@109: FlowMap Flow() { 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@109: 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@109: 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@109: } 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@109: void maxMinCut(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@109: void minMinCut(CutMap& M) { jacint@109: minCut(M); jacint@98: } jacint@98: jacint@98: jacint@98: jacint@98: }; jacint@109: }//namespace marci jacint@98: #endif jacint@98: jacint@98: jacint@98: jacint@98: