jacint@101: // -*- C++ -*- jacint@101: /* jacint@101: preflow_hl3.h jacint@101: by jacint. jacint@101: jacint@109: Heuristics: jacint@101: jacint@109: suck gap : if there is a gap, then we put all jacint@109: nodes reachable from the relabelled node to level n jacint@109: 2 phase jacint@109: highest label jacint@101: jacint@109: The constructor runs the algorithm. jacint@101: jacint@109: Members: jacint@101: jacint@109: T maxFlow() : returns the value of a maximum flow jacint@101: jacint@109: T flowOnEdge(EdgeIt e) : for a fixed maximum flow x it returns x(e) jacint@101: jacint@109: FlowMap Flow() : returns the fixed maximum flow x jacint@101: jacint@109: void minCut(CutMap& M) : sets M to the characteristic vector of a jacint@101: minimum cut. M should be a map of bools initialized to false. jacint@101: jacint@109: void minMinCut(CutMap& M) : sets M to the characteristic vector of the jacint@101: minimum min cut. M should be a map of bools initialized to false. jacint@101: jacint@109: void maxMinCut(CutMap& M) : sets M to the characteristic vector of the jacint@101: maximum min cut. M should be a map of bools initialized to false. jacint@101: jacint@101: */ jacint@101: jacint@101: #ifndef PREFLOW_HL3_H jacint@101: #define PREFLOW_HL3_H jacint@101: jacint@101: #include jacint@101: #include jacint@101: #include jacint@101: jacint@109: #include //for test jacint@109: alpar@105: namespace hugo { jacint@101: jacint@101: template , jacint@109: typename CapMap=typename Graph::EdgeMap > jacint@101: class preflow_hl3 { jacint@101: jacint@101: typedef typename Graph::NodeIt NodeIt; jacint@101: typedef typename Graph::EdgeIt EdgeIt; jacint@101: typedef typename Graph::EachNodeIt EachNodeIt; jacint@101: typedef typename Graph::OutEdgeIt OutEdgeIt; jacint@101: typedef typename Graph::InEdgeIt InEdgeIt; jacint@101: jacint@101: Graph& G; jacint@101: NodeIt s; jacint@101: NodeIt t; jacint@101: FlowMap flow; jacint@101: CapMap& capacity; jacint@101: T value; jacint@101: jacint@101: public: jacint@101: jacint@109: double time; //for test jacint@109: jacint@101: preflow_hl3(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity) : jacint@109: G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity) { jacint@109: jacint@101: bool phase=0; jacint@101: int n=G.nodeNum(); jacint@101: int b=n-2; jacint@101: /* jacint@101: b is a bound on the highest level of the stack. jacint@101: In the beginning it is at most n-2. jacint@101: */ jacint@101: jacint@109: typename Graph::NodeMap level(G,n); jacint@109: typename Graph::NodeMap excess(G); jacint@101: jacint@101: std::vector numb(n); jacint@101: /* jacint@101: The number of nodes on level i < n. It is jacint@101: initialized to n+1, because of the reverse_bfs-part. jacint@101: Needed only in phase 0. jacint@101: */ jacint@101: jacint@101: std::vector > stack(n); jacint@101: //Stack of the active nodes in level i < n. jacint@101: //We use it in both phases. jacint@101: jacint@101: jacint@101: /*Reverse_bfs from t, to find the starting level.*/ jacint@101: level.set(t,0); jacint@101: std::queue bfs_queue; jacint@101: bfs_queue.push(t); jacint@101: jacint@101: while (!bfs_queue.empty()) { jacint@101: jacint@101: NodeIt v=bfs_queue.front(); jacint@101: bfs_queue.pop(); jacint@101: int l=level.get(v)+1; jacint@101: jacint@101: for(InEdgeIt e=G.template first(v); e.valid(); ++e) { jacint@101: NodeIt w=G.tail(e); jacint@101: if ( level.get(w) == n ) { jacint@101: bfs_queue.push(w); jacint@101: ++numb[l]; jacint@101: level.set(w, l); jacint@101: } jacint@101: } jacint@101: } jacint@101: jacint@101: level.set(s,n); jacint@101: jacint@101: jacint@101: jacint@101: /* Starting flow. It is everywhere 0 at the moment. */ jacint@101: for(OutEdgeIt e=G.template first(s); e.valid(); ++e) jacint@101: { jacint@101: T c=capacity.get(e); jacint@101: if ( c == 0 ) continue; jacint@101: NodeIt w=G.head(e); jacint@101: if ( level.get(w) < n ) { jacint@101: 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@101: } jacint@101: jacint@101: /* jacint@101: End of preprocessing jacint@101: */ jacint@101: jacint@101: jacint@101: jacint@101: /* jacint@101: Push/relabel on the highest level active nodes. jacint@101: */ jacint@101: /*While there exists an active node.*/ jacint@101: while ( true ) { jacint@101: jacint@101: if ( b == 0 ) { jacint@101: if ( phase ) break; jacint@101: phase=1; jacint@109: time=currTime(); jacint@109: jacint@101: level.set(s,0); jacint@101: jacint@101: std::queue bfs_queue; jacint@101: bfs_queue.push(s); jacint@101: jacint@101: while (!bfs_queue.empty()) { jacint@101: jacint@101: NodeIt v=bfs_queue.front(); jacint@101: bfs_queue.pop(); jacint@101: int l=level.get(v)+1; jacint@101: jacint@101: for(InEdgeIt e=G.template first(v); e.valid(); ++e) { jacint@101: if ( capacity.get(e) == flow.get(e) ) continue; jacint@101: NodeIt u=G.tail(e); jacint@101: if ( level.get(u) == n ) { jacint@101: bfs_queue.push(u); jacint@101: level.set(u, l); jacint@101: if ( excess.get(u) > 0 ) stack[l].push(u); jacint@101: } jacint@101: } jacint@101: jacint@101: for(OutEdgeIt e=G.template first(v); e.valid(); ++e) { jacint@101: if ( 0 == flow.get(e) ) continue; jacint@101: NodeIt u=G.head(e); jacint@101: if ( level.get(u) == n ) { jacint@101: bfs_queue.push(u); jacint@101: level.set(u, l); jacint@101: if ( excess.get(u) > 0 ) stack[l].push(u); jacint@101: } jacint@101: } jacint@101: } jacint@101: jacint@101: b=n-2; jacint@101: } jacint@101: jacint@101: if ( stack[b].empty() ) --b; jacint@101: else { jacint@101: jacint@109: NodeIt w=stack[b].top(); jacint@101: stack[b].pop(); jacint@101: int lev=level.get(w); jacint@109: T exc=excess.get(w); jacint@109: int newlevel=n; //bound on the next level of w. jacint@101: jacint@101: for(OutEdgeIt e=G.template first(w); e.valid(); ++e) { jacint@101: jacint@101: if ( flow.get(e) == capacity.get(e) ) continue; jacint@101: NodeIt v=G.head(e); jacint@101: //e=wv jacint@101: jacint@101: if( lev > level.get(v) ) { jacint@101: /*Push is allowed now*/ jacint@101: jacint@101: if ( excess.get(v)==0 && v !=t && v!=s ) jacint@101: stack[level.get(v)].push(v); jacint@101: /*v becomes active.*/ jacint@101: jacint@109: T cap=capacity.get(e); jacint@109: T flo=flow.get(e); jacint@109: T remcap=cap-flo; jacint@101: jacint@101: if ( remcap >= exc ) { jacint@101: /*A nonsaturating push.*/ jacint@101: jacint@101: flow.set(e, flo+exc); jacint@101: excess.set(v, excess.get(v)+exc); jacint@101: exc=0; jacint@101: break; jacint@101: jacint@101: } else { jacint@101: /*A saturating push.*/ jacint@101: jacint@101: flow.set(e, cap ); jacint@101: excess.set(v, excess.get(v)+remcap); jacint@101: exc-=remcap; jacint@101: } jacint@101: } else if ( newlevel > level.get(v) ) newlevel = level.get(v); jacint@101: jacint@101: } //for out edges wv jacint@101: jacint@101: jacint@101: if ( exc > 0 ) { jacint@101: for( InEdgeIt e=G.template first(w); e.valid(); ++e) { jacint@101: jacint@101: if( flow.get(e) == 0 ) continue; jacint@101: NodeIt v=G.tail(e); jacint@101: //e=vw jacint@101: jacint@101: if( lev > level.get(v) ) { jacint@101: /*Push is allowed now*/ jacint@101: jacint@101: if ( excess.get(v)==0 && v !=t && v!=s ) jacint@101: stack[level.get(v)].push(v); jacint@101: /*v becomes active.*/ jacint@101: jacint@109: T flo=flow.get(e); jacint@101: jacint@101: if ( flo >= exc ) { jacint@101: /*A nonsaturating push.*/ jacint@101: jacint@101: flow.set(e, flo-exc); jacint@101: excess.set(v, excess.get(v)+exc); jacint@101: exc=0; jacint@101: break; jacint@101: } else { jacint@101: /*A saturating push.*/ jacint@101: jacint@101: excess.set(v, excess.get(v)+flo); jacint@101: exc-=flo; jacint@101: flow.set(e,0); jacint@101: } jacint@101: } else if ( newlevel > level.get(v) ) newlevel = level.get(v); jacint@101: jacint@101: } //for in edges vw jacint@101: jacint@101: } // if w still has excess after the out edge for cycle jacint@101: jacint@101: excess.set(w, exc); jacint@101: jacint@101: jacint@101: /* jacint@101: Relabel jacint@101: */ jacint@101: jacint@101: if ( exc > 0 ) { jacint@101: //now 'lev' is the old level of w jacint@101: jacint@101: if ( phase ) { jacint@101: level.set(w,++newlevel); jacint@101: stack[newlevel].push(w); jacint@101: b=newlevel; jacint@101: } else { jacint@101: jacint@101: if ( newlevel >= n-2 || --numb[lev] == 0 ) { jacint@101: jacint@101: level.set(w,n); jacint@101: jacint@101: if ( newlevel < n ) { jacint@101: jacint@101: std::queue bfs_queue; jacint@101: bfs_queue.push(w); jacint@101: jacint@101: while (!bfs_queue.empty()) { jacint@101: jacint@101: NodeIt v=bfs_queue.front(); jacint@101: bfs_queue.pop(); jacint@101: jacint@101: for(OutEdgeIt e=G.template first(v); e.valid(); ++e) { jacint@101: if ( capacity.get(e) == flow.get(e) ) continue; jacint@101: NodeIt u=G.head(e); jacint@101: if ( level.get(u) < n ) { jacint@101: bfs_queue.push(u); jacint@101: --numb[level.get(u)]; jacint@101: level.set(u, n); jacint@101: } jacint@101: } jacint@101: jacint@101: for(InEdgeIt e=G.template first(v); e.valid(); ++e) { jacint@101: if ( 0 == flow.get(e) ) continue; jacint@101: NodeIt u=G.tail(e); jacint@101: if ( level.get(u) < n ) { jacint@101: bfs_queue.push(u); jacint@101: --numb[level.get(u)]; jacint@101: level.set(u, n); jacint@101: } jacint@101: } jacint@101: } jacint@101: } jacint@101: b=n-1; jacint@101: jacint@101: } else { jacint@101: level.set(w,++newlevel); jacint@101: stack[newlevel].push(w); jacint@101: ++numb[newlevel]; jacint@101: b=newlevel; jacint@101: } jacint@101: } jacint@101: } jacint@101: jacint@101: jacint@101: jacint@101: } // if stack[b] is nonempty jacint@101: jacint@101: } // while(true) jacint@101: jacint@101: jacint@101: value = excess.get(t); jacint@101: /*Max flow value.*/ jacint@101: jacint@101: jacint@101: } //void run() jacint@101: jacint@101: jacint@101: jacint@101: jacint@101: jacint@101: /* jacint@101: Returns the maximum value of a flow. jacint@101: */ jacint@101: jacint@109: T maxFlow() { jacint@101: return value; jacint@101: } jacint@101: jacint@101: jacint@101: jacint@101: /* 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@101: */ jacint@101: jacint@109: T flowOnEdge(EdgeIt e) { jacint@101: return flow.get(e); jacint@101: } jacint@101: jacint@101: jacint@101: jacint@101: /* jacint@101: Returns the maximum flow x found by the algorithm. jacint@101: */ jacint@101: jacint@109: FlowMap Flow() { jacint@101: return flow; jacint@101: } jacint@101: jacint@101: jacint@101: jacint@101: jacint@101: /* jacint@101: Returns the minimum min cut, by a bfs from s in the residual graph. jacint@101: */ jacint@101: jacint@101: template jacint@109: void minCut(CutMap& M) { jacint@101: jacint@101: std::queue queue; jacint@101: jacint@101: M.set(s,true); jacint@101: queue.push(s); jacint@101: jacint@101: while (!queue.empty()) { jacint@101: NodeIt w=queue.front(); jacint@101: queue.pop(); jacint@101: jacint@101: for(OutEdgeIt e=G.template first(w) ; e.valid(); ++e) { jacint@101: NodeIt v=G.head(e); jacint@101: if (!M.get(v) && flow.get(e) < capacity.get(e) ) { jacint@101: queue.push(v); jacint@101: M.set(v, true); jacint@101: } jacint@101: } jacint@101: jacint@101: for(InEdgeIt e=G.template first(w) ; e.valid(); ++e) { jacint@101: NodeIt v=G.tail(e); jacint@101: if (!M.get(v) && flow.get(e) > 0 ) { jacint@101: queue.push(v); jacint@101: M.set(v, true); jacint@101: } jacint@101: } jacint@101: jacint@101: } jacint@101: jacint@101: } jacint@101: jacint@101: jacint@101: jacint@101: /* jacint@101: Returns the maximum min cut, by a reverse bfs jacint@101: from t in the residual graph. jacint@101: */ jacint@101: jacint@101: template jacint@109: void maxMinCut(CutMap& M) { jacint@101: jacint@101: std::queue queue; jacint@101: jacint@101: M.set(t,true); jacint@101: queue.push(t); jacint@101: jacint@101: while (!queue.empty()) { jacint@101: NodeIt w=queue.front(); jacint@101: queue.pop(); jacint@101: jacint@101: for(InEdgeIt e=G.template first(w) ; e.valid(); ++e) { jacint@101: NodeIt v=G.tail(e); jacint@101: if (!M.get(v) && flow.get(e) < capacity.get(e) ) { jacint@101: queue.push(v); jacint@101: M.set(v, true); jacint@101: } jacint@101: } jacint@101: jacint@101: for(OutEdgeIt e=G.template first(w) ; e.valid(); ++e) { jacint@101: NodeIt v=G.head(e); jacint@101: if (!M.get(v) && flow.get(e) > 0 ) { jacint@101: queue.push(v); jacint@101: M.set(v, true); jacint@101: } jacint@101: } jacint@101: } jacint@101: jacint@101: for(EachNodeIt v=G.template first() ; v.valid(); ++v) { jacint@101: M.set(v, !M.get(v)); jacint@101: } jacint@101: jacint@101: } jacint@101: jacint@101: jacint@101: jacint@101: template jacint@109: void minMinCut(CutMap& M) { jacint@109: minCut(M); jacint@101: } jacint@101: jacint@101: jacint@101: jacint@101: }; jacint@109: }//namespace jacint@101: #endif jacint@101: jacint@101: jacint@101: jacint@101: