jacint@836: // -*- C++ -*- jacint@836: #ifndef HUGO_PREFLOW_H jacint@836: #define HUGO_PREFLOW_H jacint@836: jacint@836: #include jacint@836: #include jacint@836: jacint@836: #include jacint@836: #include jacint@836: jacint@836: /// \file jacint@836: /// \ingroup flowalgs jacint@836: jacint@836: namespace hugo { jacint@836: jacint@836: /// \addtogroup flowalgs jacint@836: /// @{ jacint@836: alpar@851: ///%Preflow algorithms class. jacint@836: jacint@836: ///This class provides an implementation of the \e preflow \e jacint@836: ///algorithm producing a flow of maximum value in a directed jacint@836: ///graph. The preflow algorithms are the fastest max flow algorithms alpar@851: ///up to now. The \e source node, the \e target node, the \e jacint@836: ///capacity of the edges and the \e starting \e flow value of the jacint@836: ///edges should be passed to the algorithm through the jacint@836: ///constructor. It is possible to change these quantities using the jacint@836: ///functions \ref setSource, \ref setTarget, \ref setCap and \ref jacint@836: ///setFlow. jacint@836: /// alpar@851: ///After running \ref phase1() or \ref preflow(), the actual flow jacint@836: ///value can be obtained by calling \ref flowValue(). The minimum alpar@851: ///value cut can be written into a bool node map by alpar@851: ///calling \ref minCut(). (\ref minMinCut() and \ref maxMinCut() writes jacint@836: ///the inclusionwise minimum and maximum of the minimum value cuts, jacint@836: ///resp.) jacint@836: /// jacint@836: ///\param Graph The directed graph type the algorithm runs on. jacint@836: ///\param Num The number type of the capacities and the flow values. jacint@836: ///\param CapMap The capacity map type. jacint@836: ///\param FlowMap The flow map type. jacint@836: /// jacint@836: ///\author Jacint Szabo jacint@836: template , jacint@836: typename FlowMap=typename Graph::template EdgeMap > jacint@836: class Preflow { jacint@836: protected: jacint@836: typedef typename Graph::Node Node; jacint@836: typedef typename Graph::NodeIt NodeIt; jacint@836: typedef typename Graph::EdgeIt EdgeIt; jacint@836: typedef typename Graph::OutEdgeIt OutEdgeIt; jacint@836: typedef typename Graph::InEdgeIt InEdgeIt; jacint@836: jacint@836: typedef typename Graph::template NodeMap NNMap; jacint@836: typedef typename std::vector VecNode; jacint@836: jacint@836: const Graph* g; jacint@836: Node s; jacint@836: Node t; jacint@836: const CapMap* capacity; jacint@836: FlowMap* flow; jacint@836: int n; //the number of nodes of G jacint@836: jacint@836: typename Graph::template NodeMap level; jacint@836: typename Graph::template NodeMap excess; jacint@836: jacint@836: // constants used for heuristics jacint@836: static const int H0=20; jacint@836: static const int H1=1; jacint@836: jacint@836: public: jacint@836: jacint@836: ///Indicates the property of the starting flow map. jacint@836: jacint@836: ///Indicates the property of the starting flow map. The meanings are as follows: jacint@836: ///- \c ZERO_FLOW: constant zero flow jacint@836: ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to jacint@836: ///the sum of the out-flows in every node except the \e source and jacint@836: ///the \e target. jacint@836: ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at jacint@836: ///least the sum of the out-flows in every node except the \e source. jacint@836: ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be jacint@836: ///set to the constant zero flow in the beginning of the algorithm in this case. jacint@836: /// jacint@836: enum FlowEnum{ jacint@836: NO_FLOW, jacint@836: ZERO_FLOW, jacint@836: GEN_FLOW, jacint@836: PRE_FLOW jacint@836: }; jacint@836: jacint@836: ///Indicates the state of the preflow algorithm. jacint@836: jacint@836: ///Indicates the state of the preflow algorithm. The meanings are as follows: jacint@836: ///- \c AFTER_NOTHING: before running the algorithm or at an unspecified state. jacint@836: ///- \c AFTER_PREFLOW_PHASE_1: right after running \c phase1 jacint@836: ///- \c AFTER_PREFLOW_PHASE_2: after running \ref phase2() jacint@836: /// jacint@836: enum StatusEnum { jacint@836: AFTER_NOTHING, jacint@836: AFTER_PREFLOW_PHASE_1, jacint@836: AFTER_PREFLOW_PHASE_2 jacint@836: }; jacint@836: jacint@836: protected: jacint@836: FlowEnum flow_prop; jacint@836: StatusEnum status; // Do not needle this flag only if necessary. jacint@836: jacint@836: public: jacint@836: ///The constructor of the class. jacint@836: jacint@836: ///The constructor of the class. jacint@836: ///\param _G The directed graph the algorithm runs on. jacint@836: ///\param _s The source node. jacint@836: ///\param _t The target node. jacint@836: ///\param _capacity The capacity of the edges. jacint@836: ///\param _flow The flow of the edges. jacint@836: ///Except the graph, all of these parameters can be reset by jacint@836: ///calling \ref setSource, \ref setTarget, \ref setCap and \ref jacint@836: ///setFlow, resp. jacint@836: Preflow(const Graph& _G, Node _s, Node _t, jacint@836: const CapMap& _capacity, FlowMap& _flow) : jacint@836: g(&_G), s(_s), t(_t), capacity(&_capacity), jacint@836: flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0), jacint@836: flow_prop(NO_FLOW), status(AFTER_NOTHING) { } jacint@836: jacint@836: jacint@836: jacint@836: ///Runs the preflow algorithm. jacint@836: alpar@851: ///Runs the preflow algorithm. alpar@851: /// jacint@836: void run() { jacint@836: phase1(flow_prop); jacint@836: phase2(); jacint@836: } jacint@836: jacint@836: ///Runs the preflow algorithm. jacint@836: jacint@836: ///Runs the preflow algorithm. jacint@836: ///\pre The starting flow map must be jacint@836: /// - a constant zero flow if \c fp is \c ZERO_FLOW, jacint@836: /// - an arbitrary flow if \c fp is \c GEN_FLOW, jacint@836: /// - an arbitrary preflow if \c fp is \c PRE_FLOW, jacint@836: /// - any map if \c fp is NO_FLOW. jacint@836: ///If the starting flow map is a flow or a preflow then jacint@836: ///the algorithm terminates faster. jacint@836: void run(FlowEnum fp) { jacint@836: flow_prop=fp; jacint@836: run(); jacint@836: } jacint@836: jacint@836: ///Runs the first phase of the preflow algorithm. jacint@836: jacint@836: ///The preflow algorithm consists of two phases, this method runs the jacint@836: ///first phase. After the first phase the maximum flow value and a jacint@836: ///minimum value cut can already be computed, though a maximum flow jacint@836: ///is not yet obtained. So after calling this method \ref flowValue jacint@836: ///and \ref minCut gives proper results. alpar@851: ///\warning \ref minMinCut and \ref maxMinCut do not jacint@836: ///give minimum value cuts unless calling \ref phase2. jacint@836: ///\pre The starting flow must be jacint@836: /// - a constant zero flow if \c fp is \c ZERO_FLOW, jacint@836: /// - an arbitary flow if \c fp is \c GEN_FLOW, jacint@836: /// - an arbitary preflow if \c fp is \c PRE_FLOW, jacint@836: /// - any map if \c fp is NO_FLOW. jacint@836: void phase1(FlowEnum fp) jacint@836: { jacint@836: flow_prop=fp; jacint@836: phase1(); jacint@836: } jacint@836: jacint@836: jacint@836: ///Runs the first phase of the preflow algorithm. jacint@836: jacint@836: ///The preflow algorithm consists of two phases, this method runs the jacint@836: ///first phase. After the first phase the maximum flow value and a jacint@836: ///minimum value cut can already be computed, though a maximum flow jacint@836: ///is not yet obtained. So after calling this method \ref flowValue jacint@836: ///and \ref actMinCut gives proper results. alpar@851: ///\warning \ref minCut, \ref minMinCut and \ref maxMinCut do not jacint@836: ///give minimum value cuts unless calling \ref phase2. jacint@836: void phase1() jacint@836: { jacint@836: int heur0=(int)(H0*n); //time while running 'bound decrease' jacint@836: int heur1=(int)(H1*n); //time while running 'highest label' jacint@836: int heur=heur1; //starting time interval (#of relabels) jacint@836: int numrelabel=0; jacint@836: jacint@836: bool what_heur=1; jacint@836: //It is 0 in case 'bound decrease' and 1 in case 'highest label' jacint@836: jacint@836: bool end=false; jacint@836: //Needed for 'bound decrease', true means no active jacint@836: //nodes are above bound b. jacint@836: jacint@836: int k=n-2; //bound on the highest level under n containing a node jacint@836: int b=k; //bound on the highest level under n of an active node jacint@836: jacint@836: VecNode first(n, INVALID); jacint@836: NNMap next(*g, INVALID); jacint@836: jacint@836: NNMap left(*g, INVALID); jacint@836: NNMap right(*g, INVALID); jacint@836: VecNode level_list(n,INVALID); jacint@836: //List of the nodes in level i 0 ) { jacint@836: b=k; jacint@836: end=true; jacint@836: } else break; jacint@836: } jacint@836: jacint@836: if ( first[b]==INVALID ) --b; jacint@836: else { jacint@836: end=false; jacint@836: Node w=first[b]; jacint@836: first[b]=next[w]; jacint@836: int newlevel=push(w, next, first); jacint@836: if ( excess[w] > 0 ) relabel(w, newlevel, first, next, level_list, jacint@836: left, right, b, k, what_heur); jacint@836: jacint@836: ++numrelabel; jacint@836: if ( numrelabel >= heur ) { jacint@836: numrelabel=0; jacint@836: if ( what_heur ) { jacint@836: what_heur=0; jacint@836: heur=heur0; jacint@836: end=false; jacint@836: } else { jacint@836: what_heur=1; jacint@836: heur=heur1; jacint@836: b=k; jacint@836: } jacint@836: } jacint@836: } jacint@836: } jacint@836: flow_prop=PRE_FLOW; jacint@836: status=AFTER_PREFLOW_PHASE_1; jacint@836: } jacint@836: // Heuristics: jacint@836: // 2 phase jacint@836: // gap jacint@836: // list 'level_list' on the nodes on level i implemented by hand jacint@836: // stack 'active' on the active nodes on level i jacint@836: // runs heuristic 'highest label' for H1*n relabels jacint@836: // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label' jacint@836: // Parameters H0 and H1 are initialized to 20 and 1. jacint@836: jacint@836: jacint@836: ///Runs the second phase of the preflow algorithm. jacint@836: jacint@836: ///The preflow algorithm consists of two phases, this method runs jacint@836: ///the second phase. After calling \ref phase1 and then jacint@836: ///\ref phase2 the methods \ref flowValue, \ref minCut, jacint@836: ///\ref minMinCut and \ref maxMinCut give proper results. jacint@836: ///\pre \ref phase1 must be called before. jacint@836: void phase2() jacint@836: { jacint@836: jacint@836: int k=n-2; //bound on the highest level under n containing a node jacint@836: int b=k; //bound on the highest level under n of an active node jacint@836: jacint@836: jacint@836: VecNode first(n, INVALID); jacint@836: NNMap next(*g, INVALID); jacint@836: level.set(s,0); jacint@836: std::queue bfs_queue; jacint@836: bfs_queue.push(s); jacint@836: jacint@836: while ( !bfs_queue.empty() ) { jacint@836: jacint@836: Node v=bfs_queue.front(); jacint@836: bfs_queue.pop(); jacint@836: int l=level[v]+1; jacint@836: jacint@836: for(InEdgeIt e(*g,v); e!=INVALID; ++e) { jacint@836: if ( (*capacity)[e] <= (*flow)[e] ) continue; jacint@836: Node u=g->tail(e); jacint@836: if ( level[u] >= n ) { jacint@836: bfs_queue.push(u); jacint@836: level.set(u, l); jacint@836: if ( excess[u] > 0 ) { jacint@836: next.set(u,first[l]); jacint@836: first[l]=u; jacint@836: } jacint@836: } jacint@836: } jacint@836: jacint@836: for(OutEdgeIt e(*g,v); e!=INVALID; ++e) { jacint@836: if ( 0 >= (*flow)[e] ) continue; jacint@836: Node u=g->head(e); jacint@836: if ( level[u] >= n ) { jacint@836: bfs_queue.push(u); jacint@836: level.set(u, l); jacint@836: if ( excess[u] > 0 ) { jacint@836: next.set(u,first[l]); jacint@836: first[l]=u; jacint@836: } jacint@836: } jacint@836: } jacint@836: } jacint@836: b=n-2; jacint@836: jacint@836: while ( true ) { jacint@836: jacint@836: if ( b == 0 ) break; jacint@836: if ( first[b]==INVALID ) --b; jacint@836: else { jacint@836: Node w=first[b]; jacint@836: first[b]=next[w]; jacint@836: int newlevel=push(w,next, first); jacint@836: jacint@836: //relabel jacint@836: if ( excess[w] > 0 ) { jacint@836: level.set(w,++newlevel); jacint@836: next.set(w,first[newlevel]); jacint@836: first[newlevel]=w; jacint@836: b=newlevel; jacint@836: } jacint@836: } jacint@836: } // while(true) jacint@836: flow_prop=GEN_FLOW; jacint@836: status=AFTER_PREFLOW_PHASE_2; jacint@836: } jacint@836: jacint@836: /// Returns the value of the maximum flow. jacint@836: jacint@836: /// Returns the value of the maximum flow by returning the excess jacint@836: /// of the target node \ref t. This value equals to the value of jacint@836: /// the maximum flow already after running \ref phase1. jacint@836: Num flowValue() const { jacint@836: return excess[t]; jacint@836: } jacint@836: jacint@836: jacint@836: ///Returns a minimum value cut. jacint@836: jacint@836: ///Sets \c M to the characteristic vector of a minimum value jacint@836: ///cut. This method can be called both after running \ref jacint@836: ///phase1 and \ref phase2. It is much faster after marci@849: ///\ref phase1. \pre M should be a bool-valued node-map. \pre jacint@836: ///If \ref mincut is called after \ref phase2 then M should jacint@836: ///be initialized to false. jacint@836: template jacint@836: void minCut(_CutMap& M) const { jacint@836: switch ( status ) { jacint@836: case AFTER_PREFLOW_PHASE_1: jacint@836: for(NodeIt v(*g); v!=INVALID; ++v) { jacint@836: if (level[v] < n) { jacint@836: M.set(v, false); jacint@836: } else { jacint@836: M.set(v, true); jacint@836: } jacint@836: } jacint@836: break; jacint@836: case AFTER_PREFLOW_PHASE_2: jacint@836: minMinCut(M); jacint@836: break; jacint@836: case AFTER_NOTHING: jacint@836: break; jacint@836: } jacint@836: } jacint@836: jacint@836: ///Returns the inclusionwise minimum of the minimum value cuts. jacint@836: jacint@836: ///Sets \c M to the characteristic vector of the minimum value cut jacint@836: ///which is inclusionwise minimum. It is computed by processing a jacint@836: ///bfs from the source node \c s in the residual graph. \pre M jacint@836: ///should be a node map of bools initialized to false. \pre \ref jacint@836: ///phase2 should already be run. jacint@836: template jacint@836: void minMinCut(_CutMap& M) const { jacint@836: jacint@836: std::queue queue; jacint@836: M.set(s,true); jacint@836: queue.push(s); jacint@836: jacint@836: while (!queue.empty()) { jacint@836: Node w=queue.front(); jacint@836: queue.pop(); jacint@836: jacint@836: for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) { jacint@836: Node v=g->head(e); jacint@836: if (!M[v] && (*flow)[e] < (*capacity)[e] ) { jacint@836: queue.push(v); jacint@836: M.set(v, true); jacint@836: } jacint@836: } jacint@836: jacint@836: for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) { jacint@836: Node v=g->tail(e); jacint@836: if (!M[v] && (*flow)[e] > 0 ) { jacint@836: queue.push(v); jacint@836: M.set(v, true); jacint@836: } jacint@836: } jacint@836: } jacint@836: } jacint@836: jacint@836: ///Returns the inclusionwise maximum of the minimum value cuts. jacint@836: jacint@836: ///Sets \c M to the characteristic vector of the minimum value cut jacint@836: ///which is inclusionwise maximum. It is computed by processing a jacint@836: ///backward bfs from the target node \c t in the residual graph. jacint@836: ///\pre \ref phase2() or preflow() should already be run. jacint@836: template jacint@836: void maxMinCut(_CutMap& M) const { jacint@836: jacint@836: for(NodeIt v(*g) ; v!=INVALID; ++v) M.set(v, true); jacint@836: jacint@836: std::queue queue; jacint@836: jacint@836: M.set(t,false); jacint@836: queue.push(t); jacint@836: jacint@836: while (!queue.empty()) { jacint@836: Node w=queue.front(); jacint@836: queue.pop(); jacint@836: jacint@836: for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) { jacint@836: Node v=g->tail(e); jacint@836: if (M[v] && (*flow)[e] < (*capacity)[e] ) { jacint@836: queue.push(v); jacint@836: M.set(v, false); jacint@836: } jacint@836: } jacint@836: jacint@836: for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) { jacint@836: Node v=g->head(e); jacint@836: if (M[v] && (*flow)[e] > 0 ) { jacint@836: queue.push(v); jacint@836: M.set(v, false); jacint@836: } jacint@836: } jacint@836: } jacint@836: } jacint@836: jacint@836: ///Sets the source node to \c _s. jacint@836: jacint@836: ///Sets the source node to \c _s. jacint@836: /// jacint@836: void setSource(Node _s) { jacint@836: s=_s; jacint@836: if ( flow_prop != ZERO_FLOW ) flow_prop=NO_FLOW; jacint@836: status=AFTER_NOTHING; jacint@836: } jacint@836: jacint@836: ///Sets the target node to \c _t. jacint@836: jacint@836: ///Sets the target node to \c _t. jacint@836: /// jacint@836: void setTarget(Node _t) { jacint@836: t=_t; jacint@836: if ( flow_prop == GEN_FLOW ) flow_prop=PRE_FLOW; jacint@836: status=AFTER_NOTHING; jacint@836: } jacint@836: jacint@836: /// Sets the edge map of the capacities to _cap. jacint@836: jacint@836: /// Sets the edge map of the capacities to _cap. jacint@836: /// jacint@836: void setCap(const CapMap& _cap) { jacint@836: capacity=&_cap; jacint@836: status=AFTER_NOTHING; jacint@836: } jacint@836: jacint@836: /// Sets the edge map of the flows to _flow. jacint@836: jacint@836: /// Sets the edge map of the flows to _flow. jacint@836: /// jacint@836: void setFlow(FlowMap& _flow) { jacint@836: flow=&_flow; jacint@836: flow_prop=NO_FLOW; jacint@836: status=AFTER_NOTHING; jacint@836: } jacint@836: jacint@836: jacint@836: private: jacint@836: jacint@836: int push(Node w, NNMap& next, VecNode& first) { jacint@836: jacint@836: int lev=level[w]; jacint@836: Num exc=excess[w]; jacint@836: int newlevel=n; //bound on the next level of w jacint@836: jacint@836: for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) { jacint@836: if ( (*flow)[e] >= (*capacity)[e] ) continue; jacint@836: Node v=g->head(e); jacint@836: jacint@836: if( lev > level[v] ) { //Push is allowed now jacint@836: jacint@836: if ( excess[v]<=0 && v!=t && v!=s ) { jacint@836: next.set(v,first[level[v]]); jacint@836: first[level[v]]=v; jacint@836: } jacint@836: jacint@836: Num cap=(*capacity)[e]; jacint@836: Num flo=(*flow)[e]; jacint@836: Num remcap=cap-flo; jacint@836: jacint@836: if ( remcap >= exc ) { //A nonsaturating push. jacint@836: jacint@836: flow->set(e, flo+exc); jacint@836: excess.set(v, excess[v]+exc); jacint@836: exc=0; jacint@836: break; jacint@836: jacint@836: } else { //A saturating push. jacint@836: flow->set(e, cap); jacint@836: excess.set(v, excess[v]+remcap); jacint@836: exc-=remcap; jacint@836: } jacint@836: } else if ( newlevel > level[v] ) newlevel = level[v]; jacint@836: } //for out edges wv jacint@836: jacint@836: if ( exc > 0 ) { jacint@836: for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) { jacint@836: jacint@836: if( (*flow)[e] <= 0 ) continue; jacint@836: Node v=g->tail(e); jacint@836: jacint@836: if( lev > level[v] ) { //Push is allowed now jacint@836: jacint@836: if ( excess[v]<=0 && v!=t && v!=s ) { jacint@836: next.set(v,first[level[v]]); jacint@836: first[level[v]]=v; jacint@836: } jacint@836: jacint@836: Num flo=(*flow)[e]; jacint@836: jacint@836: if ( flo >= exc ) { //A nonsaturating push. jacint@836: jacint@836: flow->set(e, flo-exc); jacint@836: excess.set(v, excess[v]+exc); jacint@836: exc=0; jacint@836: break; jacint@836: } else { //A saturating push. jacint@836: jacint@836: excess.set(v, excess[v]+flo); jacint@836: exc-=flo; jacint@836: flow->set(e,0); jacint@836: } jacint@836: } else if ( newlevel > level[v] ) newlevel = level[v]; jacint@836: } //for in edges vw jacint@836: jacint@836: } // if w still has excess after the out edge for cycle jacint@836: jacint@836: excess.set(w, exc); jacint@836: jacint@836: return newlevel; jacint@836: } jacint@836: jacint@836: jacint@836: jacint@836: void preflowPreproc(VecNode& first, NNMap& next, jacint@836: VecNode& level_list, NNMap& left, NNMap& right) jacint@836: { jacint@836: for(NodeIt v(*g); v!=INVALID; ++v) level.set(v,n); jacint@836: std::queue bfs_queue; jacint@836: jacint@836: if ( flow_prop == GEN_FLOW || flow_prop == PRE_FLOW ) { jacint@836: //Reverse_bfs from t in the residual graph, jacint@836: //to find the starting level. jacint@836: level.set(t,0); jacint@836: bfs_queue.push(t); jacint@836: jacint@836: while ( !bfs_queue.empty() ) { jacint@836: jacint@836: Node v=bfs_queue.front(); jacint@836: bfs_queue.pop(); jacint@836: int l=level[v]+1; jacint@836: jacint@836: for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) { jacint@836: if ( (*capacity)[e] <= (*flow)[e] ) continue; jacint@836: Node w=g->tail(e); jacint@836: if ( level[w] == n && w != s ) { jacint@836: bfs_queue.push(w); jacint@836: Node z=level_list[l]; jacint@836: if ( z!=INVALID ) left.set(z,w); jacint@836: right.set(w,z); jacint@836: level_list[l]=w; jacint@836: level.set(w, l); jacint@836: } jacint@836: } jacint@836: jacint@836: for(OutEdgeIt e(*g,v) ; e!=INVALID; ++e) { jacint@836: if ( 0 >= (*flow)[e] ) continue; jacint@836: Node w=g->head(e); jacint@836: if ( level[w] == n && w != s ) { jacint@836: bfs_queue.push(w); jacint@836: Node z=level_list[l]; jacint@836: if ( z!=INVALID ) left.set(z,w); jacint@836: right.set(w,z); jacint@836: level_list[l]=w; jacint@836: level.set(w, l); jacint@836: } jacint@836: } jacint@836: } //while jacint@836: } //if jacint@836: jacint@836: jacint@836: switch (flow_prop) { jacint@836: case NO_FLOW: jacint@836: for(EdgeIt e(*g); e!=INVALID; ++e) flow->set(e,0); jacint@836: case ZERO_FLOW: jacint@836: for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0); jacint@836: jacint@836: //Reverse_bfs from t, to find the starting level. jacint@836: level.set(t,0); jacint@836: bfs_queue.push(t); jacint@836: jacint@836: while ( !bfs_queue.empty() ) { jacint@836: jacint@836: Node v=bfs_queue.front(); jacint@836: bfs_queue.pop(); jacint@836: int l=level[v]+1; jacint@836: jacint@836: for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) { jacint@836: Node w=g->tail(e); jacint@836: if ( level[w] == n && w != s ) { jacint@836: bfs_queue.push(w); jacint@836: Node z=level_list[l]; jacint@836: if ( z!=INVALID ) left.set(z,w); jacint@836: right.set(w,z); jacint@836: level_list[l]=w; jacint@836: level.set(w, l); jacint@836: } jacint@836: } jacint@836: } jacint@836: jacint@836: //the starting flow jacint@836: for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) { jacint@836: Num c=(*capacity)[e]; jacint@836: if ( c <= 0 ) continue; jacint@836: Node w=g->head(e); jacint@836: if ( level[w] < n ) { jacint@836: if ( excess[w] <= 0 && w!=t ) { //putting into the stack jacint@836: next.set(w,first[level[w]]); jacint@836: first[level[w]]=w; jacint@836: } jacint@836: flow->set(e, c); jacint@836: excess.set(w, excess[w]+c); jacint@836: } jacint@836: } jacint@836: break; jacint@836: jacint@836: case GEN_FLOW: jacint@836: for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0); jacint@836: { jacint@836: Num exc=0; jacint@836: for(InEdgeIt e(*g,t) ; e!=INVALID; ++e) exc+=(*flow)[e]; jacint@836: for(OutEdgeIt e(*g,t) ; e!=INVALID; ++e) exc-=(*flow)[e]; jacint@836: excess.set(t,exc); jacint@836: } jacint@836: jacint@836: //the starting flow jacint@836: for(OutEdgeIt e(*g,s); e!=INVALID; ++e) { jacint@836: Num rem=(*capacity)[e]-(*flow)[e]; jacint@836: if ( rem <= 0 ) continue; jacint@836: Node w=g->head(e); jacint@836: if ( level[w] < n ) { jacint@836: if ( excess[w] <= 0 && w!=t ) { //putting into the stack jacint@836: next.set(w,first[level[w]]); jacint@836: first[level[w]]=w; jacint@836: } jacint@836: flow->set(e, (*capacity)[e]); jacint@836: excess.set(w, excess[w]+rem); jacint@836: } jacint@836: } jacint@836: jacint@836: for(InEdgeIt e(*g,s); e!=INVALID; ++e) { jacint@836: if ( (*flow)[e] <= 0 ) continue; jacint@836: Node w=g->tail(e); jacint@836: if ( level[w] < n ) { jacint@836: if ( excess[w] <= 0 && w!=t ) { jacint@836: next.set(w,first[level[w]]); jacint@836: first[level[w]]=w; jacint@836: } jacint@836: excess.set(w, excess[w]+(*flow)[e]); jacint@836: flow->set(e, 0); jacint@836: } jacint@836: } jacint@836: break; jacint@836: jacint@836: case PRE_FLOW: jacint@836: //the starting flow jacint@836: for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) { jacint@836: Num rem=(*capacity)[e]-(*flow)[e]; jacint@836: if ( rem <= 0 ) continue; jacint@836: Node w=g->head(e); jacint@836: if ( level[w] < n ) flow->set(e, (*capacity)[e]); jacint@836: } jacint@836: jacint@836: for(InEdgeIt e(*g,s) ; e!=INVALID; ++e) { jacint@836: if ( (*flow)[e] <= 0 ) continue; jacint@836: Node w=g->tail(e); jacint@836: if ( level[w] < n ) flow->set(e, 0); jacint@836: } jacint@836: jacint@836: //computing the excess jacint@836: for(NodeIt w(*g); w!=INVALID; ++w) { jacint@836: Num exc=0; jacint@836: for(InEdgeIt e(*g,w); e!=INVALID; ++e) exc+=(*flow)[e]; jacint@836: for(OutEdgeIt e(*g,w); e!=INVALID; ++e) exc-=(*flow)[e]; jacint@836: excess.set(w,exc); jacint@836: jacint@836: //putting the active nodes into the stack jacint@836: int lev=level[w]; jacint@836: if ( exc > 0 && lev < n && Node(w) != t ) { jacint@836: next.set(w,first[lev]); jacint@836: first[lev]=w; jacint@836: } jacint@836: } jacint@836: break; jacint@836: } //switch jacint@836: } //preflowPreproc jacint@836: jacint@836: jacint@836: void relabel(Node w, int newlevel, VecNode& first, NNMap& next, jacint@836: VecNode& level_list, NNMap& left, jacint@836: NNMap& right, int& b, int& k, bool what_heur ) jacint@836: { jacint@836: jacint@836: int lev=level[w]; jacint@836: jacint@836: Node right_n=right[w]; jacint@836: Node left_n=left[w]; jacint@836: jacint@836: //unlacing starts jacint@836: if ( right_n!=INVALID ) { jacint@836: if ( left_n!=INVALID ) { jacint@836: right.set(left_n, right_n); jacint@836: left.set(right_n, left_n); jacint@836: } else { jacint@836: level_list[lev]=right_n; jacint@836: left.set(right_n, INVALID); jacint@836: } jacint@836: } else { jacint@836: if ( left_n!=INVALID ) { jacint@836: right.set(left_n, INVALID); jacint@836: } else { jacint@836: level_list[lev]=INVALID; jacint@836: } jacint@836: } jacint@836: //unlacing ends jacint@836: jacint@836: if ( level_list[lev]==INVALID ) { jacint@836: jacint@836: //gapping starts jacint@836: for (int i=lev; i!=k ; ) { jacint@836: Node v=level_list[++i]; jacint@836: while ( v!=INVALID ) { jacint@836: level.set(v,n); jacint@836: v=right[v]; jacint@836: } jacint@836: level_list[i]=INVALID; jacint@836: if ( !what_heur ) first[i]=INVALID; jacint@836: } jacint@836: jacint@836: level.set(w,n); jacint@836: b=lev-1; jacint@836: k=b; jacint@836: //gapping ends jacint@836: jacint@836: } else { jacint@836: jacint@836: if ( newlevel == n ) level.set(w,n); jacint@836: else { jacint@836: level.set(w,++newlevel); jacint@836: next.set(w,first[newlevel]); jacint@836: first[newlevel]=w; jacint@836: if ( what_heur ) b=newlevel; jacint@836: if ( k < newlevel ) ++k; //now k=newlevel jacint@836: Node z=level_list[newlevel]; jacint@836: if ( z!=INVALID ) left.set(z,w); jacint@836: right.set(w,z); jacint@836: left.set(w,INVALID); jacint@836: level_list[newlevel]=w; jacint@836: } jacint@836: } jacint@836: } //relabel jacint@836: jacint@836: }; jacint@836: } //namespace hugo jacint@836: jacint@836: #endif //HUGO_PREFLOW_H jacint@836: jacint@836: jacint@836: jacint@836: