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@72: running time O(n^2\sqrt(m)). 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@83: Graph::EdgeMap allflow() : returns the fixed maximum flow x jacint@72: jacint@83: Graph::NodeMap mincut() : returns a jacint@72: characteristic vector of a minimum cut. (An empty level jacint@72: in the algorithm gives a minimum cut.) jacint@72: */ jacint@72: jacint@72: #ifndef PREFLOW_PUSH_HL_H jacint@72: #define PREFLOW_PUSH_HL_H jacint@72: jacint@85: //#include jacint@72: #include jacint@72: #include jacint@72: jacint@78: #include jacint@72: jacint@72: namespace marci { jacint@72: jacint@78: template 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@78: typename Graph::EdgeMap flow; jacint@78: typename Graph::EdgeMap capacity; jacint@72: T value; jacint@78: typename Graph::NodeMap mincutvector; jacint@72: jacint@72: public: jacint@72: jacint@72: preflow_push_hl(Graph& _G, NodeIt _s, NodeIt _t, jacint@78: typename Graph::EdgeMap& _capacity) : jacint@83: G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity), jacint@83: mincutvector(_G, true) { } jacint@72: jacint@72: jacint@72: /* jacint@72: The run() function runs the highest label preflow-push, jacint@72: running time: O(n^2\sqrt(m)) jacint@72: */ jacint@72: void run() { jacint@72: jacint@83: typename Graph::NodeMap level(G); jacint@84: typename Graph::NodeMap excess(G); jacint@85: 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: In the beginning it is at most n-2. jacint@83: */ jacint@72: jacint@85: std::vector numb(n); //The number of nodes on level i < n. 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@78: reverse_bfs bfs(G, t); jacint@72: bfs.run(); jacint@83: for(EachNodeIt v=G.template first(); v.valid(); ++v) jacint@83: { jacint@85: int dist=bfs.dist(v); jacint@85: level.set(v, dist); jacint@85: ++numb[dist]; jacint@83: } jacint@72: jacint@72: level.set(s,n); 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@83: if ( capacity.get(e) > 0 ) { jacint@83: NodeIt w=G.head(e); jacint@85: if ( w!=s ) { jacint@85: if ( excess.get(w) == 0 && w!=t ) stack[level.get(w)].push(w); jacint@85: flow.set(e, capacity.get(e)); jacint@85: excess.set(w, excess.get(w)+capacity.get(e)); jacint@85: } jacint@83: } jacint@72: } jacint@72: jacint@72: /* jacint@72: End of preprocessing jacint@72: */ jacint@72: jacint@72: jacint@72: jacint@72: /* jacint@84: Push/relabel on the highest level active nodes. jacint@72: */ jacint@72: jacint@85: /*While there exists an active node.*/ jacint@72: while (b) { jacint@72: jacint@85: /*We decrease the bound if there is no active node of level b.*/ jacint@72: if (stack[b].empty()) { jacint@72: --b; jacint@72: } else { jacint@72: jacint@84: NodeIt w=stack[b].top(); //w is a highest label active node. jacint@84: stack[b].pop(); jacint@72: jacint@84: int newlevel=2*n-2; //In newlevel we bound the next level of w. jacint@72: jacint@72: for(OutEdgeIt e=G.template first(w); e.valid(); ++e) { jacint@84: jacint@84: if ( flow.get(e) < capacity.get(e) ) { jacint@84: /*e is an edge of the residual graph */ jacint@72: jacint@84: NodeIt v=G.head(e); /*e is the edge wv.*/ jacint@72: jacint@84: if( level.get(w) == level.get(v)+1 ) { jacint@72: /*Push is allowed now*/ jacint@72: jacint@85: if ( excess.get(v)==0 && v != s && v !=t ) stack[level.get(v)].push(v); jacint@85: /*v becomes active.*/ jacint@85: jacint@85: if ( capacity.get(e)-flow.get(e) > excess.get(w) ) { jacint@72: /*A nonsaturating push.*/ jacint@72: jacint@72: flow.set(e, flow.get(e)+excess.get(w)); jacint@72: excess.set(v, excess.get(v)+excess.get(w)); jacint@72: excess.set(w,0); jacint@72: break; jacint@85: jacint@72: } else { jacint@72: /*A saturating push.*/ jacint@72: jacint@72: excess.set(v, excess.get(v)+capacity.get(e)-flow.get(e)); jacint@72: excess.set(w, excess.get(w)-capacity.get(e)+flow.get(e)); jacint@72: flow.set(e, capacity.get(e)); jacint@85: if ( excess.get(w)==0 ) break; jacint@85: /*If w is not active any more, then we go on to the next node.*/ jacint@72: jacint@85: } jacint@85: } else { jacint@85: newlevel = newlevel < level.get(v) ? newlevel : level.get(v); jacint@85: } jacint@72: jacint@85: } //if the out edge wv is in the res graph jacint@72: jacint@85: } //for out edges wv jacint@72: jacint@72: jacint@85: if ( excess.get(w) > 0 ) { jacint@85: jacint@85: for( InEdgeIt e=G.template first(w); e.valid(); ++e) { jacint@85: NodeIt v=G.tail(e); /*e is the edge vw.*/ jacint@72: jacint@85: if( flow.get(e) > 0 ) { jacint@85: /*e is an edge of the residual graph */ jacint@72: jacint@85: if( level.get(w)==level.get(v)+1 ) { jacint@85: /*Push is allowed now*/ jacint@72: jacint@85: if ( excess.get(v)==0 && v != s && v !=t) stack[level.get(v)].push(v); jacint@72: /*v becomes active.*/ jacint@72: jacint@85: if ( flow.get(e) > excess.get(w) ) { jacint@85: /*A nonsaturating push.*/ jacint@72: jacint@85: flow.set(e, flow.get(e)-excess.get(w)); jacint@85: excess.set(v, excess.get(v)+excess.get(w)); jacint@85: excess.set(w,0); jacint@85: break; jacint@85: } else { jacint@85: /*A saturating push.*/ jacint@85: jacint@85: excess.set(v, excess.get(v)+flow.get(e)); jacint@85: excess.set(w, excess.get(w)-flow.get(e)); jacint@85: flow.set(e,0); jacint@85: if ( excess.get(w)==0 ) break; jacint@85: } jacint@85: } else { jacint@85: newlevel = newlevel < level.get(v) ? newlevel : level.get(v); jacint@85: } jacint@85: jacint@85: } //if in edge vw is in the res graph jacint@72: jacint@85: } //for in edges vw jacint@72: jacint@85: } // if w still has excess after the out edge for cycle jacint@72: jacint@72: jacint@85: /* jacint@85: Relabel jacint@85: */ jacint@85: jacint@85: if ( excess.get(w) > 0 ) { jacint@85: jacint@85: int oldlevel=level.get(w); jacint@72: level.set(w,++newlevel); jacint@85: jacint@85: if ( oldlevel < n ) { jacint@85: --numb[oldlevel]; jacint@85: jacint@85: if ( !numb[oldlevel] ) { //If the level of w gets empty. jacint@85: jacint@85: for (EachNodeIt v=G.template first(); v.valid() ; ++v) { jacint@85: if (level.get(v) > oldlevel && level.get(v) < n ) level.set(v,n); jacint@85: } jacint@85: for (int i=oldlevel+1 ; i!=n ; ++i) numb[i]=0; jacint@85: if ( newlevel < n ) newlevel=n; jacint@85: } else { jacint@85: if ( newlevel < n ) ++numb[newlevel]; jacint@85: } jacint@85: } else { jacint@85: if ( newlevel < n ) ++numb[newlevel]; jacint@85: } jacint@85: jacint@72: stack[newlevel].push(w); jacint@72: b=newlevel; jacint@85: jacint@72: } jacint@72: jacint@85: } // if stack[b] is nonempty jacint@72: jacint@85: } // while(b) jacint@85: jacint@72: 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@72: T flowonEdge(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@78: typename Graph::EdgeMap allflow() { jacint@72: return flow; jacint@72: } jacint@72: jacint@72: jacint@72: jacint@72: /* jacint@72: Returns a minimum cut by using a reverse bfs from t in the residual graph. jacint@72: */ jacint@72: jacint@78: typename Graph::NodeMap mincut() { jacint@72: jacint@72: std::queue queue; jacint@72: jacint@72: mincutvector.set(t,false); 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@72: if (mincutvector.get(v) && flow.get(e) < capacity.get(e) ) { jacint@72: queue.push(v); jacint@72: mincutvector.set(v, false); jacint@72: } jacint@72: } // for jacint@72: jacint@72: for(OutEdgeIt e=G.template first(w) ; e.valid(); ++e) { jacint@72: NodeIt v=G.head(e); jacint@72: if (mincutvector.get(v) && flow.get(e) > 0 ) { jacint@72: queue.push(v); jacint@72: mincutvector.set(v, false); jacint@72: } jacint@72: } // for jacint@72: jacint@72: } jacint@72: jacint@72: return mincutvector; jacint@72: jacint@72: } jacint@72: }; jacint@72: }//namespace marci jacint@72: #endif jacint@72: jacint@72: jacint@72: jacint@72: