alpar@726: // -*- C++ -*- jacint@749: #ifndef HUGO_MAX_FLOW_H jacint@749: #define HUGO_MAX_FLOW_H alpar@726: alpar@726: #include alpar@726: #include alpar@726: alpar@774: //#include alpar@726: #include alpar@726: #include alpar@726: alpar@726: /// \file alpar@758: /// \ingroup flowalgs alpar@726: alpar@726: namespace hugo { alpar@726: alpar@758: /// \addtogroup flowalgs alpar@758: /// @{ alpar@758: alpar@726: ///Maximum flow algorithms class. alpar@726: alpar@726: ///This class provides various algorithms for finding a flow of alpar@726: ///maximum value in a directed graph. The \e source node, the \e alpar@726: ///target node, the \e capacity of the edges and the \e starting \e alpar@726: ///flow value of the edges should be passed to the algorithm through the alpar@726: ///constructor. It is possible to change these quantities using the alpar@757: ///functions \ref setSource, \ref setTarget, \ref setCap and alpar@757: ///\ref setFlow. Before any subsequent runs of any algorithm of alpar@757: ///the class \ref setFlow should be called. alpar@758: /// alpar@726: ///After running an algorithm of the class, the actual flow value alpar@726: ///can be obtained by calling \ref flowValue(). The minimum alpar@726: ///value cut can be written into a \c node map of \c bools by alpar@726: ///calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes alpar@726: ///the inclusionwise minimum and maximum of the minimum value alpar@758: ///cuts, resp.) alpar@758: /// alpar@726: ///\param Graph The directed graph type the algorithm runs on. alpar@726: ///\param Num The number type of the capacities and the flow values. alpar@726: ///\param CapMap The capacity map type. alpar@758: ///\param FlowMap The flow map type. alpar@758: /// alpar@726: ///\author Marton Makai, Jacint Szabo alpar@726: template , alpar@726: typename FlowMap=typename Graph::template EdgeMap > alpar@726: class MaxFlow { alpar@726: protected: alpar@726: typedef typename Graph::Node Node; alpar@726: typedef typename Graph::NodeIt NodeIt; alpar@726: typedef typename Graph::EdgeIt EdgeIt; alpar@726: typedef typename Graph::OutEdgeIt OutEdgeIt; alpar@726: typedef typename Graph::InEdgeIt InEdgeIt; alpar@726: alpar@726: typedef typename std::vector VecFirst; alpar@726: typedef typename Graph::template NodeMap NNMap; alpar@726: typedef typename std::vector VecNode; alpar@726: alpar@726: const Graph* g; alpar@726: Node s; alpar@726: Node t; alpar@726: const CapMap* capacity; alpar@726: FlowMap* flow; alpar@726: int n; //the number of nodes of G alpar@774: // typedef ResGraphWrapper ResGW; alpar@726: //typedef ExpResGraphWrapper ResGW; alpar@774: // typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt; alpar@774: // typedef typename ResGW::Edge ResGWEdge; alpar@726: typedef typename Graph::template NodeMap ReachedMap; alpar@726: alpar@726: alpar@726: //level works as a bool map in augmenting path algorithms and is alpar@726: //used by bfs for storing reached information. In preflow, it alpar@726: //shows the levels of nodes. alpar@726: ReachedMap level; alpar@726: alpar@726: //excess is needed only in preflow alpar@726: typename Graph::template NodeMap excess; alpar@726: alpar@726: // constants used for heuristics alpar@726: static const int H0=20; alpar@726: static const int H1=1; alpar@726: alpar@726: public: alpar@726: alpar@726: ///Indicates the property of the starting flow. alpar@726: alpar@726: ///Indicates the property of the starting flow. The meanings are as follows: alpar@726: ///- \c ZERO_FLOW: constant zero flow alpar@726: ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to alpar@726: ///the sum of the out-flows in every node except the \e source and alpar@726: ///the \e target. alpar@726: ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at alpar@726: ///least the sum of the out-flows in every node except the \e source. alpar@726: ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be alpar@726: ///set to the constant zero flow in the beginning of the algorithm in this case. alpar@726: enum FlowEnum{ alpar@726: ZERO_FLOW, alpar@726: GEN_FLOW, alpar@726: PRE_FLOW, alpar@726: NO_FLOW alpar@726: }; alpar@726: alpar@726: enum StatusEnum { alpar@726: AFTER_NOTHING, alpar@726: AFTER_AUGMENTING, alpar@726: AFTER_FAST_AUGMENTING, alpar@726: AFTER_PRE_FLOW_PHASE_1, alpar@726: AFTER_PRE_FLOW_PHASE_2 alpar@726: }; alpar@726: jacint@749: /// Do not needle this flag only if necessary. alpar@726: StatusEnum status; alpar@726: alpar@774: // int number_of_augmentations; alpar@726: alpar@726: alpar@774: // template alpar@774: // class TrickyReachedMap { alpar@774: // protected: alpar@774: // IntMap* map; alpar@774: // int* number_of_augmentations; alpar@774: // public: alpar@774: // TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) : alpar@774: // map(&_map), number_of_augmentations(&_number_of_augmentations) { } alpar@774: // void set(const Node& n, bool b) { alpar@774: // if (b) alpar@774: // map->set(n, *number_of_augmentations); alpar@774: // else alpar@774: // map->set(n, *number_of_augmentations-1); alpar@774: // } alpar@774: // bool operator[](const Node& n) const { alpar@774: // return (*map)[n]==*number_of_augmentations; alpar@774: // } alpar@774: // }; alpar@726: alpar@726: ///Constructor alpar@726: alpar@726: ///\todo Document, please. alpar@726: /// alpar@726: MaxFlow(const Graph& _G, Node _s, Node _t, marci@745: const CapMap& _capacity, FlowMap& _flow) : alpar@726: g(&_G), s(_s), t(_t), capacity(&_capacity), alpar@726: flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0), alpar@726: status(AFTER_NOTHING) { } alpar@726: alpar@726: ///Runs a maximum flow algorithm. alpar@726: alpar@726: ///Runs a preflow algorithm, which is the fastest maximum flow alpar@726: ///algorithm up-to-date. The default for \c fe is ZERO_FLOW. alpar@726: ///\pre The starting flow must be alpar@726: /// - a constant zero flow if \c fe is \c ZERO_FLOW, alpar@726: /// - an arbitary flow if \c fe is \c GEN_FLOW, alpar@726: /// - an arbitary preflow if \c fe is \c PRE_FLOW, alpar@726: /// - any map if \c fe is NO_FLOW. alpar@726: void run(FlowEnum fe=ZERO_FLOW) { alpar@726: preflow(fe); alpar@726: } alpar@726: alpar@726: alpar@726: ///Runs a preflow algorithm. alpar@726: alpar@726: ///Runs a preflow algorithm. The preflow algorithms provide the alpar@726: ///fastest way to compute a maximum flow in a directed graph. alpar@726: ///\pre The starting flow must be alpar@726: /// - a constant zero flow if \c fe is \c ZERO_FLOW, alpar@726: /// - an arbitary flow if \c fe is \c GEN_FLOW, alpar@726: /// - an arbitary preflow if \c fe is \c PRE_FLOW, alpar@726: /// - any map if \c fe is NO_FLOW. alpar@726: /// alpar@726: ///\todo NO_FLOW should be the default flow. alpar@726: void preflow(FlowEnum fe) { alpar@726: preflowPhase1(fe); alpar@726: preflowPhase2(); alpar@726: } alpar@726: // Heuristics: alpar@726: // 2 phase alpar@726: // gap alpar@726: // list 'level_list' on the nodes on level i implemented by hand alpar@726: // stack 'active' on the active nodes on level i alpar@726: // runs heuristic 'highest label' for H1*n relabels alpar@726: // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label' alpar@726: // Parameters H0 and H1 are initialized to 20 and 1. alpar@726: alpar@726: ///Runs the first phase of the preflow algorithm. alpar@726: alpar@726: ///The preflow algorithm consists of two phases, this method runs the alpar@726: ///first phase. After the first phase the maximum flow value and a alpar@726: ///minimum value cut can already be computed, though a maximum flow jacint@749: ///is not yet obtained. So after calling this method \ref flowValue alpar@726: ///and \ref actMinCut gives proper results. alpar@726: ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not alpar@726: ///give minimum value cuts unless calling \ref preflowPhase2. alpar@726: ///\pre The starting flow must be alpar@726: /// - a constant zero flow if \c fe is \c ZERO_FLOW, alpar@726: /// - an arbitary flow if \c fe is \c GEN_FLOW, alpar@726: /// - an arbitary preflow if \c fe is \c PRE_FLOW, alpar@726: /// - any map if \c fe is NO_FLOW. alpar@726: void preflowPhase1(FlowEnum fe) alpar@726: { alpar@726: alpar@726: int heur0=(int)(H0*n); //time while running 'bound decrease' alpar@726: int heur1=(int)(H1*n); //time while running 'highest label' alpar@726: int heur=heur1; //starting time interval (#of relabels) alpar@726: int numrelabel=0; alpar@726: alpar@726: bool what_heur=1; alpar@726: //It is 0 in case 'bound decrease' and 1 in case 'highest label' alpar@726: alpar@726: bool end=false; alpar@726: //Needed for 'bound decrease', true means no active nodes are above bound alpar@726: //b. alpar@726: alpar@726: int k=n-2; //bound on the highest level under n containing a node alpar@726: int b=k; //bound on the highest level under n of an active node alpar@726: alpar@726: VecFirst first(n, INVALID); alpar@726: NNMap next(*g, INVALID); //maybe INVALID is not needed alpar@726: alpar@726: NNMap left(*g, INVALID); alpar@726: NNMap right(*g, INVALID); alpar@726: VecNode level_list(n,INVALID); alpar@726: //List of the nodes in level i 0 ) { alpar@726: b=k; alpar@726: end=true; alpar@726: } else break; alpar@726: } alpar@726: alpar@774: if ( first[b]==INVALID ) --b; alpar@726: else { alpar@726: end=false; alpar@726: Node w=first[b]; alpar@726: first[b]=next[w]; marci@745: int newlevel=push(w, next, first); marci@745: if ( excess[w] > 0 ) relabel(w, newlevel, next, first, level_list, alpar@726: left, right, b, k, what_heur); alpar@726: alpar@726: ++numrelabel; alpar@726: if ( numrelabel >= heur ) { alpar@726: numrelabel=0; alpar@726: if ( what_heur ) { alpar@726: what_heur=0; alpar@726: heur=heur0; alpar@726: end=false; alpar@726: } else { alpar@726: what_heur=1; alpar@726: heur=heur1; alpar@726: b=k; alpar@726: } alpar@726: } alpar@726: } alpar@726: } alpar@726: alpar@726: status=AFTER_PRE_FLOW_PHASE_1; alpar@726: } alpar@726: alpar@726: alpar@726: ///Runs the second phase of the preflow algorithm. alpar@726: alpar@726: ///The preflow algorithm consists of two phases, this method runs alpar@726: ///the second phase. After calling \ref preflowPhase1 and then alpar@726: ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut, alpar@726: ///\ref minMinCut and \ref maxMinCut give proper results. alpar@726: ///\pre \ref preflowPhase1 must be called before. alpar@726: void preflowPhase2() alpar@726: { alpar@726: alpar@726: int k=n-2; //bound on the highest level under n containing a node alpar@726: int b=k; //bound on the highest level under n of an active node alpar@726: alpar@726: alpar@726: VecFirst first(n, INVALID); alpar@726: NNMap next(*g, INVALID); //maybe INVALID is not needed alpar@726: level.set(s,0); alpar@726: std::queue bfs_queue; alpar@726: bfs_queue.push(s); alpar@726: alpar@726: while (!bfs_queue.empty()) { alpar@726: alpar@726: Node v=bfs_queue.front(); alpar@726: bfs_queue.pop(); alpar@726: int l=level[v]+1; alpar@726: alpar@774: for(InEdgeIt e(*g,v); e!=INVALID; ++e) { alpar@726: if ( (*capacity)[e] <= (*flow)[e] ) continue; alpar@726: Node u=g->tail(e); alpar@726: if ( level[u] >= n ) { alpar@726: bfs_queue.push(u); alpar@726: level.set(u, l); alpar@726: if ( excess[u] > 0 ) { alpar@726: next.set(u,first[l]); alpar@726: first[l]=u; alpar@726: } alpar@726: } alpar@726: } alpar@726: alpar@774: for(OutEdgeIt e(*g,v); e!=INVALID; ++e) { alpar@774: if ( 0 >= (*flow)[e] ) continue; alpar@774: Node u=g->head(e); alpar@726: if ( level[u] >= n ) { alpar@726: bfs_queue.push(u); alpar@726: level.set(u, l); alpar@726: if ( excess[u] > 0 ) { alpar@726: next.set(u,first[l]); alpar@726: first[l]=u; alpar@726: } alpar@726: } alpar@726: } alpar@726: } alpar@726: b=n-2; alpar@726: alpar@726: while ( true ) { alpar@726: alpar@726: if ( b == 0 ) break; alpar@726: alpar@774: if ( first[b]==INVALID ) --b; alpar@726: else { alpar@726: alpar@726: Node w=first[b]; alpar@726: first[b]=next[w]; alpar@726: int newlevel=push(w,next, first/*active*/); alpar@726: alpar@726: //relabel alpar@726: if ( excess[w] > 0 ) { alpar@726: level.set(w,++newlevel); alpar@726: next.set(w,first[newlevel]); alpar@726: first[newlevel]=w; alpar@726: b=newlevel; alpar@726: } jacint@749: } alpar@726: } // while(true) alpar@726: alpar@726: status=AFTER_PRE_FLOW_PHASE_2; alpar@726: } alpar@726: alpar@726: marci@761: /// Returns the value of the maximum flow. alpar@726: marci@761: /// Returns the excess of the target node \ref t. marci@761: /// After running \ref preflowPhase1, this is the value of marci@761: /// the maximum flow. alpar@726: /// It can be called already after running \ref preflowPhase1. alpar@726: Num flowValue() const { alpar@774: // Num a=0; alpar@774: // for(InEdgeIt e(*g,t);g->valid(e);g->next(e)) a+=(*flow)[e]; alpar@774: // for(OutEdgeIt e(*g,t);g->valid(e);g->next(e)) a-=(*flow)[e]; alpar@774: // return a; marci@761: return excess[t]; alpar@726: //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan alpar@726: } jacint@749: alpar@726: alpar@726: ///Returns a minimum value cut after calling \ref preflowPhase1. alpar@726: alpar@726: ///After the first phase of the preflow algorithm the maximum flow alpar@726: ///value and a minimum value cut can already be computed. This alpar@726: ///method can be called after running \ref preflowPhase1 for alpar@726: ///obtaining a minimum value cut. alpar@726: /// \warning Gives proper result only right after calling \ref alpar@726: /// preflowPhase1. alpar@726: /// \todo We have to make some status variable which shows the alpar@726: /// actual state alpar@726: /// of the class. This enables us to determine which methods are valid alpar@726: /// for MinCut computation alpar@726: template alpar@726: void actMinCut(_CutMap& M) const { alpar@726: switch (status) { alpar@774: case AFTER_PRE_FLOW_PHASE_1: alpar@774: for(NodeIt v(*g); v!=INVALID; ++v) { alpar@726: if (level[v] < n) { alpar@726: M.set(v, false); alpar@726: } else { alpar@726: M.set(v, true); alpar@726: } alpar@726: } alpar@726: break; alpar@774: case AFTER_PRE_FLOW_PHASE_2: alpar@774: case AFTER_NOTHING: alpar@774: case AFTER_AUGMENTING: alpar@774: case AFTER_FAST_AUGMENTING: alpar@726: minMinCut(M); alpar@726: break; alpar@726: } alpar@726: } alpar@726: alpar@726: ///Returns the inclusionwise minimum of the minimum value cuts. alpar@726: alpar@726: ///Sets \c M to the characteristic vector of the minimum value cut alpar@726: ///which is inclusionwise minimum. It is computed by processing alpar@726: ///a bfs from the source node \c s in the residual graph. alpar@726: ///\pre M should be a node map of bools initialized to false. alpar@726: ///\pre \c flow must be a maximum flow. alpar@726: template alpar@726: void minMinCut(_CutMap& M) const { alpar@726: std::queue queue; alpar@726: alpar@726: M.set(s,true); alpar@726: queue.push(s); alpar@726: alpar@726: while (!queue.empty()) { alpar@726: Node w=queue.front(); alpar@726: queue.pop(); alpar@726: alpar@774: for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) { alpar@726: Node v=g->head(e); alpar@726: if (!M[v] && (*flow)[e] < (*capacity)[e] ) { alpar@726: queue.push(v); alpar@726: M.set(v, true); alpar@726: } alpar@726: } alpar@726: alpar@774: for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) { alpar@774: Node v=g->tail(e); alpar@774: if (!M[v] && (*flow)[e] > 0 ) { alpar@726: queue.push(v); alpar@726: M.set(v, true); alpar@726: } alpar@726: } alpar@726: } alpar@726: } alpar@726: alpar@726: ///Returns the inclusionwise maximum of the minimum value cuts. alpar@726: alpar@726: ///Sets \c M to the characteristic vector of the minimum value cut alpar@726: ///which is inclusionwise maximum. It is computed by processing a alpar@726: ///backward bfs from the target node \c t in the residual graph. alpar@726: ///\pre M should be a node map of bools initialized to false. alpar@726: ///\pre \c flow must be a maximum flow. alpar@726: template alpar@726: void maxMinCut(_CutMap& M) const { alpar@726: alpar@774: for(NodeIt v(*g) ; v!=INVALID; ++v) M.set(v, true); alpar@726: alpar@726: std::queue queue; alpar@726: alpar@726: M.set(t,false); alpar@726: queue.push(t); alpar@726: alpar@726: while (!queue.empty()) { alpar@726: Node w=queue.front(); alpar@726: queue.pop(); alpar@726: alpar@774: for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) { alpar@726: Node v=g->tail(e); alpar@726: if (M[v] && (*flow)[e] < (*capacity)[e] ) { alpar@726: queue.push(v); alpar@726: M.set(v, false); alpar@726: } alpar@726: } alpar@726: alpar@774: for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) { alpar@774: Node v=g->head(e); alpar@774: if (M[v] && (*flow)[e] > 0 ) { alpar@726: queue.push(v); alpar@726: M.set(v, false); alpar@726: } alpar@726: } alpar@726: } alpar@726: } alpar@726: alpar@726: ///Returns a minimum value cut. alpar@726: alpar@726: ///Sets \c M to the characteristic vector of a minimum value cut. alpar@726: ///\pre M should be a node map of bools initialized to false. alpar@726: ///\pre \c flow must be a maximum flow. alpar@726: template alpar@726: void minCut(CutMap& M) const { minMinCut(M); } alpar@726: alpar@757: ///Sets the source node to \c _s. alpar@726: alpar@757: ///Sets the source node to \c _s. alpar@726: /// alpar@757: void setSource(Node _s) { s=_s; status=AFTER_NOTHING; } alpar@726: alpar@757: ///Sets the target node to \c _t. alpar@726: alpar@757: ///Sets the target node to \c _t. alpar@726: /// alpar@757: void setTarget(Node _t) { t=_t; status=AFTER_NOTHING; } alpar@726: alpar@757: /// Sets the edge map of the capacities to _cap. alpar@726: alpar@757: /// Sets the edge map of the capacities to _cap. alpar@726: /// alpar@757: void setCap(const CapMap& _cap) alpar@726: { capacity=&_cap; status=AFTER_NOTHING; } alpar@726: alpar@757: /// Sets the edge map of the flows to _flow. alpar@726: alpar@757: /// Sets the edge map of the flows to _flow. alpar@726: /// alpar@757: void setFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; } alpar@726: alpar@726: alpar@726: private: alpar@726: alpar@726: int push(Node w, NNMap& next, VecFirst& first) { alpar@726: alpar@726: int lev=level[w]; alpar@726: Num exc=excess[w]; alpar@726: int newlevel=n; //bound on the next level of w alpar@726: alpar@774: for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) { alpar@726: if ( (*flow)[e] >= (*capacity)[e] ) continue; alpar@726: Node v=g->head(e); alpar@726: alpar@726: if( lev > level[v] ) { //Push is allowed now alpar@774: alpar@726: if ( excess[v]<=0 && v!=t && v!=s ) { alpar@726: next.set(v,first[level[v]]); alpar@726: first[level[v]]=v; alpar@726: } alpar@726: alpar@726: Num cap=(*capacity)[e]; alpar@726: Num flo=(*flow)[e]; alpar@726: Num remcap=cap-flo; alpar@774: alpar@726: if ( remcap >= exc ) { //A nonsaturating push. alpar@774: alpar@726: flow->set(e, flo+exc); alpar@726: excess.set(v, excess[v]+exc); alpar@726: exc=0; alpar@726: break; alpar@726: alpar@726: } else { //A saturating push. alpar@726: flow->set(e, cap); alpar@726: excess.set(v, excess[v]+remcap); alpar@726: exc-=remcap; alpar@726: } alpar@726: } else if ( newlevel > level[v] ) newlevel = level[v]; alpar@726: } //for out edges wv alpar@726: alpar@726: if ( exc > 0 ) { alpar@774: for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) { alpar@774: alpar@726: if( (*flow)[e] <= 0 ) continue; alpar@726: Node v=g->tail(e); alpar@726: alpar@726: if( lev > level[v] ) { //Push is allowed now alpar@726: alpar@726: if ( excess[v]<=0 && v!=t && v!=s ) { alpar@726: next.set(v,first[level[v]]); alpar@726: first[level[v]]=v; alpar@726: } alpar@726: alpar@726: Num flo=(*flow)[e]; alpar@726: alpar@726: if ( flo >= exc ) { //A nonsaturating push. alpar@726: alpar@726: flow->set(e, flo-exc); alpar@726: excess.set(v, excess[v]+exc); alpar@726: exc=0; alpar@726: break; alpar@726: } else { //A saturating push. alpar@726: alpar@726: excess.set(v, excess[v]+flo); alpar@726: exc-=flo; alpar@726: flow->set(e,0); alpar@726: } alpar@726: } else if ( newlevel > level[v] ) newlevel = level[v]; alpar@726: } //for in edges vw alpar@726: alpar@726: } // if w still has excess after the out edge for cycle alpar@726: alpar@726: excess.set(w, exc); alpar@774: alpar@726: return newlevel; alpar@726: } alpar@774: alpar@774: alpar@774: alpar@726: void preflowPreproc(FlowEnum fe, NNMap& next, VecFirst& first, alpar@726: VecNode& level_list, NNMap& left, NNMap& right) alpar@726: { alpar@774: switch (fe) { //setting excess jacint@749: case NO_FLOW: alpar@774: for(EdgeIt e(*g); e!=INVALID; ++e) flow->set(e,0); alpar@774: for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0); alpar@774: break; alpar@774: case ZERO_FLOW: alpar@774: for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0); alpar@774: break; alpar@774: case GEN_FLOW: alpar@774: for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0); jacint@749: { alpar@774: Num exc=0; alpar@774: for(InEdgeIt e(*g,t) ; e!=INVALID; ++e) exc+=(*flow)[e]; alpar@774: for(OutEdgeIt e(*g,t) ; e!=INVALID; ++e) exc-=(*flow)[e]; alpar@774: excess.set(t,exc); jacint@749: } alpar@774: break; alpar@774: default: alpar@774: break; jacint@749: } alpar@774: alpar@774: for(NodeIt v(*g); v!=INVALID; ++v) level.set(v,n); jacint@749: //setting each node to level n jacint@749: alpar@726: std::queue bfs_queue; alpar@726: jacint@749: alpar@726: switch (fe) { jacint@749: case NO_FLOW: //flow is already set to const zero alpar@726: case ZERO_FLOW: alpar@774: //Reverse_bfs from t, to find the starting level. alpar@774: level.set(t,0); alpar@774: bfs_queue.push(t); alpar@774: alpar@774: while (!bfs_queue.empty()) { alpar@774: alpar@774: Node v=bfs_queue.front(); alpar@774: bfs_queue.pop(); alpar@774: int l=level[v]+1; alpar@774: alpar@774: for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) { alpar@774: Node w=g->tail(e); alpar@774: if ( level[w] == n && w != s ) { alpar@774: bfs_queue.push(w); alpar@774: Node z=level_list[l]; alpar@774: if ( z!=INVALID ) left.set(z,w); alpar@774: right.set(w,z); alpar@774: level_list[l]=w; alpar@774: level.set(w, l); alpar@726: } alpar@726: } alpar@726: } alpar@774: alpar@774: //the starting flow alpar@774: for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) alpar@774: { alpar@774: Num c=(*capacity)[e]; alpar@774: if ( c <= 0 ) continue; alpar@774: Node w=g->head(e); alpar@774: if ( level[w] < n ) { alpar@774: if ( excess[w] <= 0 && w!=t ) //putting into the stack alpar@774: { alpar@774: next.set(w,first[level[w]]); alpar@774: first[level[w]]=w; alpar@774: } alpar@774: flow->set(e, c); alpar@774: excess.set(w, excess[w]+c); jacint@749: } jacint@749: } alpar@774: break; alpar@774: case GEN_FLOW: alpar@774: //Reverse_bfs from t in the residual graph, alpar@774: //to find the starting level. alpar@774: level.set(t,0); alpar@774: bfs_queue.push(t); alpar@774: alpar@774: while (!bfs_queue.empty()) { alpar@774: alpar@774: Node v=bfs_queue.front(); alpar@774: bfs_queue.pop(); alpar@774: int l=level[v]+1; alpar@774: alpar@774: for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) { alpar@774: if ( (*capacity)[e] <= (*flow)[e] ) continue; alpar@774: Node w=g->tail(e); alpar@774: if ( level[w] == n && w != s ) { alpar@774: bfs_queue.push(w); alpar@774: Node z=level_list[l]; alpar@774: if ( z!=INVALID ) left.set(z,w); alpar@774: right.set(w,z); alpar@774: level_list[l]=w; alpar@774: level.set(w, l); alpar@726: } alpar@726: } alpar@774: alpar@774: for(OutEdgeIt e(*g,v) ; e!=INVALID; ++e) { alpar@774: if ( 0 >= (*flow)[e] ) continue; alpar@774: Node w=g->head(e); alpar@774: if ( level[w] == n && w != s ) { alpar@774: bfs_queue.push(w); alpar@774: Node z=level_list[l]; alpar@774: if ( z!=INVALID ) left.set(z,w); alpar@774: right.set(w,z); alpar@774: level_list[l]=w; alpar@774: level.set(w, l); alpar@774: } alpar@774: } alpar@774: } alpar@774: alpar@774: //the starting flow alpar@774: for(OutEdgeIt e(*g,s); e!=INVALID; ++e) alpar@774: { alpar@774: Num rem=(*capacity)[e]-(*flow)[e]; alpar@774: if ( rem <= 0 ) continue; alpar@774: Node w=g->head(e); alpar@774: if ( level[w] < n ) { alpar@774: if ( excess[w] <= 0 && w!=t ) //putting into the stack alpar@774: { alpar@774: next.set(w,first[level[w]]); alpar@774: first[level[w]]=w; alpar@774: } alpar@774: flow->set(e, (*capacity)[e]); alpar@774: excess.set(w, excess[w]+rem); alpar@774: } alpar@774: } alpar@774: alpar@774: for(InEdgeIt e(*g,s); e!=INVALID; ++e) alpar@774: { alpar@774: if ( (*flow)[e] <= 0 ) continue; alpar@774: Node w=g->tail(e); alpar@774: if ( level[w] < n ) { alpar@774: if ( excess[w] <= 0 && w!=t ) alpar@774: { alpar@774: next.set(w,first[level[w]]); alpar@774: first[level[w]]=w; alpar@774: } alpar@774: excess.set(w, excess[w]+(*flow)[e]); alpar@774: flow->set(e, 0); alpar@774: } alpar@774: } alpar@774: break; alpar@774: case PRE_FLOW: alpar@774: //Reverse_bfs from t in the residual graph, alpar@774: //to find the starting level. alpar@774: level.set(t,0); alpar@774: bfs_queue.push(t); alpar@774: alpar@774: while (!bfs_queue.empty()) { alpar@774: alpar@774: Node v=bfs_queue.front(); alpar@774: bfs_queue.pop(); alpar@774: int l=level[v]+1; alpar@774: alpar@774: for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) { alpar@774: if ( (*capacity)[e] <= (*flow)[e] ) continue; alpar@774: Node w=g->tail(e); alpar@774: if ( level[w] == n && w != s ) { alpar@774: bfs_queue.push(w); alpar@774: Node z=level_list[l]; alpar@774: if ( z!=INVALID ) left.set(z,w); alpar@774: right.set(w,z); alpar@774: level_list[l]=w; alpar@774: level.set(w, l); alpar@774: } alpar@774: } alpar@774: alpar@774: for(OutEdgeIt e(*g,v) ; e!=INVALID; ++e) { alpar@774: if ( 0 >= (*flow)[e] ) continue; alpar@774: Node w=g->head(e); alpar@774: if ( level[w] == n && w != s ) { alpar@774: bfs_queue.push(w); alpar@774: Node z=level_list[l]; alpar@774: if ( z!=INVALID ) left.set(z,w); alpar@774: right.set(w,z); alpar@774: level_list[l]=w; alpar@774: level.set(w, l); alpar@774: } alpar@774: } alpar@774: } alpar@774: alpar@774: alpar@774: //the starting flow alpar@774: for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) { alpar@774: Num rem=(*capacity)[e]-(*flow)[e]; alpar@774: if ( rem <= 0 ) continue; alpar@774: Node w=g->head(e); alpar@774: if ( level[w] < n ) { alpar@774: flow->set(e, (*capacity)[e]); alpar@774: excess.set(w, excess[w]+rem); alpar@774: } alpar@774: } alpar@774: alpar@774: for(InEdgeIt e(*g,s) ; e!=INVALID; ++e) { alpar@774: if ( (*flow)[e] <= 0 ) continue; alpar@774: Node w=g->tail(e); alpar@774: if ( level[w] < n ) { alpar@774: excess.set(w, excess[w]+(*flow)[e]); alpar@774: flow->set(e, 0); alpar@774: } alpar@774: } alpar@774: alpar@774: //computing the excess alpar@774: for(NodeIt w(*g); w!=INVALID; ++w) { alpar@774: Num exc=0; alpar@774: alpar@774: for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) exc+=(*flow)[e]; alpar@774: for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) exc-=(*flow)[e]; alpar@774: alpar@774: excess.set(w,exc); alpar@774: alpar@774: //putting the active nodes into the stack alpar@774: int lev=level[w]; alpar@774: if ( exc > 0 && lev < n && Node(w) != t ) alpar@774: ///\bug if ( exc > 0 && lev < n && w != t ) temporarily for working with wrappers. alpar@726: { alpar@774: next.set(w,first[lev]); alpar@774: first[lev]=w; alpar@726: } alpar@774: } alpar@774: break; alpar@774: } //switch alpar@726: } //preflowPreproc alpar@726: alpar@726: alpar@726: void relabel(Node w, int newlevel, NNMap& next, VecFirst& first, alpar@726: VecNode& level_list, NNMap& left, alpar@726: NNMap& right, int& b, int& k, bool what_heur ) alpar@726: { alpar@726: marci@773: int lev=level[w]; alpar@726: alpar@726: Node right_n=right[w]; alpar@726: Node left_n=left[w]; alpar@726: alpar@726: //unlacing starts alpar@774: if ( right_n!=INVALID ) { alpar@774: if ( left_n!=INVALID ) { alpar@726: right.set(left_n, right_n); alpar@726: left.set(right_n, left_n); alpar@726: } else { alpar@726: level_list[lev]=right_n; alpar@726: left.set(right_n, INVALID); alpar@726: } alpar@726: } else { alpar@774: if ( left_n!=INVALID ) { alpar@726: right.set(left_n, INVALID); alpar@726: } else { alpar@726: level_list[lev]=INVALID; alpar@726: } alpar@726: } alpar@726: //unlacing ends alpar@726: alpar@774: if ( level_list[lev]==INVALID ) { alpar@726: alpar@726: //gapping starts alpar@726: for (int i=lev; i!=k ; ) { alpar@726: Node v=level_list[++i]; alpar@774: while ( v!=INVALID ) { alpar@726: level.set(v,n); alpar@726: v=right[v]; alpar@726: } alpar@726: level_list[i]=INVALID; alpar@726: if ( !what_heur ) first[i]=INVALID; alpar@726: } alpar@726: alpar@726: level.set(w,n); alpar@726: b=lev-1; alpar@726: k=b; alpar@726: //gapping ends alpar@726: alpar@726: } else { alpar@726: alpar@726: if ( newlevel == n ) level.set(w,n); alpar@726: else { alpar@726: level.set(w,++newlevel); alpar@726: next.set(w,first[newlevel]); alpar@726: first[newlevel]=w; alpar@726: if ( what_heur ) b=newlevel; alpar@726: if ( k < newlevel ) ++k; //now k=newlevel alpar@726: Node z=level_list[newlevel]; alpar@774: if ( z!=INVALID ) left.set(z,w); alpar@726: right.set(w,z); alpar@726: left.set(w,INVALID); alpar@726: level_list[newlevel]=w; alpar@726: } alpar@726: } alpar@726: } //relabel jacint@749: jacint@749: void printexcess() {//// jacint@749: std::cout << "Excesses:" <id(v)) << ":" << excess[v]<id(v)) << ":" << level[v]<id(v)) << ":" << level[v]<