alpar@726: // -*- C++ -*- alpar@726: #ifndef HUGO_MAX_FLOW_NO_STACK_H alpar@726: #define HUGO_MAX_FLOW_NO_STACK_H alpar@726: alpar@726: #include alpar@726: #include alpar@726: //#include alpar@726: alpar@726: #include alpar@726: #include alpar@726: #include alpar@726: alpar@726: /// \file alpar@726: /// \brief The same as max_flow.h, but without using stl stack for the active nodes. Only for test. alpar@726: /// \ingroup galgs alpar@726: alpar@726: namespace hugo { alpar@726: alpar@726: /// \addtogroup galgs alpar@726: /// @{ 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@726: ///functions \ref resetSource, \ref resetTarget, \ref resetCap and alpar@726: ///\ref resetFlow. Before any subsequent runs of any algorithm of alpar@726: ///the class \ref resetFlow should be called. alpar@726: 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@726: ///cuts, resp.) 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@726: ///\param FlowMap The flow map type. 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 > VecStack; 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@726: typedef ResGraphWrapper ResGW; alpar@726: //typedef ExpResGraphWrapper ResGW; alpar@726: typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt; alpar@726: typedef typename ResGW::Edge ResGWEdge; alpar@726: //typedef typename ResGW::template NodeMap ReachedMap; 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: alpar@726: /// Don not needle this flag only if necessary. alpar@726: StatusEnum status; alpar@726: alpar@726: // int number_of_augmentations; alpar@726: alpar@726: alpar@726: // template alpar@726: // class TrickyReachedMap { alpar@726: // protected: alpar@726: // IntMap* map; alpar@726: // int* number_of_augmentations; alpar@726: // public: alpar@726: // TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) : alpar@726: // map(&_map), number_of_augmentations(&_number_of_augmentations) { } alpar@726: // void set(const Node& n, bool b) { alpar@726: // if (b) alpar@726: // map->set(n, *number_of_augmentations); alpar@726: // else alpar@726: // map->set(n, *number_of_augmentations-1); alpar@726: // } alpar@726: // bool operator[](const Node& n) const { alpar@726: // return (*map)[n]==*number_of_augmentations; alpar@726: // } alpar@726: // }; alpar@726: alpar@726: ///Constructor alpar@726: alpar@726: ///\todo Document, please. alpar@726: /// alpar@726: MaxFlow(const Graph& _G, Node _s, Node _t, alpar@726: 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 alpar@726: ///is net 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: // VecStack active(n); 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 ifirst(v); g->valid(v); g->next(v)) level.set(v,n); alpar@726: //setting each node to level n alpar@726: alpar@726: if ( fe == NO_FLOW ) { alpar@726: EdgeIt e; alpar@726: for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0); alpar@726: } alpar@726: alpar@726: switch (fe) { //computing the excess alpar@726: case PRE_FLOW: alpar@726: { alpar@726: NodeIt v; alpar@726: for(g->first(v); g->valid(v); g->next(v)) { alpar@726: Num exc=0; alpar@726: alpar@726: InEdgeIt e; alpar@726: for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e]; alpar@726: OutEdgeIt f; alpar@726: for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f]; alpar@726: alpar@726: excess.set(v,exc); alpar@726: alpar@726: //putting the active nodes into the stack alpar@726: int lev=level[v]; alpar@726: if ( exc > 0 && lev < n && v != t ) alpar@726: { alpar@726: next.set(v,first[lev]); alpar@726: first[lev]=v; alpar@726: } alpar@726: // active[lev].push(v); alpar@726: } alpar@726: break; alpar@726: } alpar@726: case GEN_FLOW: alpar@726: { alpar@726: NodeIt v; alpar@726: for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0); alpar@726: alpar@726: Num exc=0; alpar@726: InEdgeIt e; alpar@726: for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e]; alpar@726: OutEdgeIt f; alpar@726: for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f]; alpar@726: excess.set(t,exc); alpar@726: break; alpar@726: } alpar@726: case ZERO_FLOW: alpar@726: case NO_FLOW: alpar@726: { alpar@726: NodeIt v; alpar@726: for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0); alpar@726: break; alpar@726: } alpar@726: } alpar@726: alpar@726: preflowPreproc(fe, next, first,/*active*/ level_list, left, right); alpar@726: //End of preprocessing alpar@726: alpar@726: alpar@726: //Push/relabel on the highest level active nodes. alpar@726: while ( true ) { alpar@726: if ( b == 0 ) { alpar@726: if ( !what_heur && !end && k > 0 ) { alpar@726: b=k; alpar@726: end=true; alpar@726: } else break; alpar@726: } alpar@726: alpar@726: if ( !g->valid(first[b])/*active[b].empty()*/ ) --b; alpar@726: else { alpar@726: end=false; alpar@726: Node w=first[b]; alpar@726: first[b]=next[w]; alpar@726: /* Node w=active[b].top(); alpar@726: active[b].pop();*/ alpar@726: int newlevel=push(w,/*active*/next, first); alpar@726: if ( excess[w] > 0 ) relabel(w, newlevel, /*active*/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: // VecStack active(n); 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@726: InEdgeIt e; alpar@726: for(g->first(e,v); g->valid(e); g->next(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: //active[l].push(u); alpar@726: } alpar@726: } alpar@726: } alpar@726: alpar@726: OutEdgeIt f; alpar@726: for(g->first(f,v); g->valid(f); g->next(f)) { alpar@726: if ( 0 >= (*flow)[f] ) continue; alpar@726: Node u=g->head(f); 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: //active[l].push(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@726: if ( !g->valid(first[b])/*active[b].empty()*/ ) --b; alpar@726: else { alpar@726: alpar@726: Node w=first[b]; alpar@726: first[b]=next[w]; alpar@726: /* Node w=active[b].top(); alpar@726: active[b].pop();*/ 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: //active[newlevel].push(w); alpar@726: b=newlevel; alpar@726: } alpar@726: } // if stack[b] is nonempty alpar@726: } // while(true) alpar@726: alpar@726: status=AFTER_PRE_FLOW_PHASE_2; alpar@726: } alpar@726: alpar@726: alpar@726: /// Returns the maximum value of a flow. alpar@726: alpar@726: /// Returns the maximum value of a flow, by counting the alpar@726: /// over-flow of the target node \ref t. alpar@726: /// It can be called already after running \ref preflowPhase1. alpar@726: Num flowValue() const { alpar@726: Num a=0; alpar@735: for(InEdgeIt e(*g,t);g->valid(e);g->next(e)) a+=(*flow)[e]; alpar@735: for(OutEdgeIt e(*g,t);g->valid(e);g->next(e)) a-=(*flow)[e]; alpar@726: alpar@726: //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan alpar@726: } 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: NodeIt v; alpar@726: switch (status) { alpar@726: case AFTER_PRE_FLOW_PHASE_1: alpar@726: for(g->first(v); g->valid(v); g->next(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@726: case AFTER_PRE_FLOW_PHASE_2: alpar@726: case AFTER_NOTHING: 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@726: OutEdgeIt e; alpar@726: for(g->first(e,w) ; g->valid(e); g->next(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@726: InEdgeIt f; alpar@726: for(g->first(f,w) ; g->valid(f); g->next(f)) { alpar@726: Node v=g->tail(f); alpar@726: if (!M[v] && (*flow)[f] > 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@726: NodeIt v; alpar@726: for(g->first(v) ; g->valid(v); g->next(v)) { alpar@726: M.set(v, true); alpar@726: } 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@726: InEdgeIt e; alpar@726: for(g->first(e,w) ; g->valid(e); g->next(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@726: OutEdgeIt f; alpar@726: for(g->first(f,w) ; g->valid(f); g->next(f)) { alpar@726: Node v=g->head(f); alpar@726: if (M[v] && (*flow)[f] > 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@726: ///Resets the source node to \c _s. alpar@726: alpar@726: ///Resets the source node to \c _s. alpar@726: /// alpar@726: void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; } alpar@726: alpar@726: ///Resets the target node to \c _t. alpar@726: alpar@726: ///Resets the target node to \c _t. alpar@726: /// alpar@726: void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; } alpar@726: alpar@726: /// Resets the edge map of the capacities to _cap. alpar@726: alpar@726: /// Resets the edge map of the capacities to _cap. alpar@726: /// alpar@726: void resetCap(const CapMap& _cap) alpar@726: { capacity=&_cap; status=AFTER_NOTHING; } alpar@726: alpar@726: /// Resets the edge map of the flows to _flow. alpar@726: alpar@726: /// Resets the edge map of the flows to _flow. alpar@726: /// alpar@726: void resetFlow(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@726: OutEdgeIt e; alpar@726: for(g->first(e,w); g->valid(e); g->next(e)) { alpar@726: 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@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: // int lev_v=level[v]; alpar@726: //active[lev_v].push(v); alpar@726: } alpar@726: alpar@726: Num cap=(*capacity)[e]; alpar@726: Num flo=(*flow)[e]; alpar@726: Num remcap=cap-flo; alpar@726: alpar@726: if ( remcap >= 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: 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@726: InEdgeIt e; alpar@726: for(g->first(e,w); g->valid(e); g->next(e)) { alpar@726: 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: //int lev_v=level[v]; alpar@726: //active[lev_v].push(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@726: alpar@726: return newlevel; alpar@726: } alpar@726: alpar@726: alpar@726: void preflowPreproc(FlowEnum fe, NNMap& next, VecFirst& first, alpar@726: VecNode& level_list, NNMap& left, NNMap& right) alpar@726: { alpar@726: std::queue bfs_queue; alpar@726: alpar@726: switch (fe) { alpar@726: case NO_FLOW: //flow is already set to const zero in this case alpar@726: case ZERO_FLOW: alpar@726: { alpar@726: //Reverse_bfs from t, to find the starting level. alpar@726: level.set(t,0); alpar@726: bfs_queue.push(t); 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@726: InEdgeIt e; alpar@726: for(g->first(e,v); g->valid(e); g->next(e)) { alpar@726: Node w=g->tail(e); alpar@726: if ( level[w] == n && w != s ) { alpar@726: bfs_queue.push(w); alpar@726: Node z=level_list[l]; alpar@726: if ( g->valid(z) ) left.set(z,w); alpar@726: right.set(w,z); alpar@726: level_list[l]=w; alpar@726: level.set(w, l); alpar@726: } alpar@726: } alpar@726: } alpar@726: alpar@726: //the starting flow alpar@726: OutEdgeIt e; alpar@726: for(g->first(e,s); g->valid(e); g->next(e)) alpar@726: { alpar@726: Num c=(*capacity)[e]; alpar@726: if ( c <= 0 ) continue; alpar@726: Node w=g->head(e); alpar@726: if ( level[w] < n ) { alpar@726: if ( excess[w] <= 0 && w!=t ) alpar@726: { alpar@726: next.set(w,first[level[w]]); alpar@726: first[level[w]]=w; alpar@726: //active[level[w]].push(w); alpar@726: } alpar@726: flow->set(e, c); alpar@726: excess.set(w, excess[w]+c); alpar@726: } alpar@726: } alpar@726: break; alpar@726: } alpar@726: alpar@726: case GEN_FLOW: alpar@726: case PRE_FLOW: alpar@726: { alpar@726: //Reverse_bfs from t in the residual graph, alpar@726: //to find the starting level. alpar@726: level.set(t,0); alpar@726: bfs_queue.push(t); 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@726: InEdgeIt e; alpar@726: for(g->first(e,v); g->valid(e); g->next(e)) { alpar@726: if ( (*capacity)[e] <= (*flow)[e] ) continue; alpar@726: Node w=g->tail(e); alpar@726: if ( level[w] == n && w != s ) { alpar@726: bfs_queue.push(w); alpar@726: Node z=level_list[l]; alpar@726: if ( g->valid(z) ) left.set(z,w); alpar@726: right.set(w,z); alpar@726: level_list[l]=w; alpar@726: level.set(w, l); alpar@726: } alpar@726: } alpar@726: alpar@726: OutEdgeIt f; alpar@726: for(g->first(f,v); g->valid(f); g->next(f)) { alpar@726: if ( 0 >= (*flow)[f] ) continue; alpar@726: Node w=g->head(f); alpar@726: if ( level[w] == n && w != s ) { alpar@726: bfs_queue.push(w); alpar@726: Node z=level_list[l]; alpar@726: if ( g->valid(z) ) left.set(z,w); alpar@726: right.set(w,z); alpar@726: level_list[l]=w; alpar@726: level.set(w, l); alpar@726: } alpar@726: } alpar@726: } alpar@726: alpar@726: alpar@726: //the starting flow alpar@726: OutEdgeIt e; alpar@726: for(g->first(e,s); g->valid(e); g->next(e)) alpar@726: { alpar@726: Num rem=(*capacity)[e]-(*flow)[e]; alpar@726: if ( rem <= 0 ) continue; alpar@726: Node w=g->head(e); alpar@726: if ( level[w] < n ) { alpar@726: if ( excess[w] <= 0 && w!=t ) alpar@726: { alpar@726: next.set(w,first[level[w]]); alpar@726: first[level[w]]=w; alpar@726: //active[level[w]].push(w); alpar@726: } alpar@726: flow->set(e, (*capacity)[e]); alpar@726: excess.set(w, excess[w]+rem); alpar@726: } alpar@726: } alpar@726: alpar@726: InEdgeIt f; alpar@726: for(g->first(f,s); g->valid(f); g->next(f)) alpar@726: { alpar@726: if ( (*flow)[f] <= 0 ) continue; alpar@726: Node w=g->tail(f); alpar@726: if ( level[w] < n ) { alpar@726: if ( excess[w] <= 0 && w!=t ) alpar@726: { alpar@726: next.set(w,first[level[w]]); alpar@726: first[level[w]]=w; alpar@726: //active[level[w]].push(w); alpar@726: } alpar@726: excess.set(w, excess[w]+(*flow)[f]); alpar@726: flow->set(f, 0); alpar@726: } alpar@726: } alpar@726: break; alpar@726: } //case PRE_FLOW alpar@726: } alpar@726: } //preflowPreproc alpar@726: 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: alpar@726: Num 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@726: if ( g->valid(right_n) ) { alpar@726: if ( g->valid(left_n) ) { 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@726: if ( g->valid(left_n) ) { 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@726: if ( !g->valid(level_list[lev]) ) { alpar@726: alpar@726: //gapping starts alpar@726: for (int i=lev; i!=k ; ) { alpar@726: Node v=level_list[++i]; alpar@726: while ( g->valid(v) ) { 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: while ( !active[i].empty() ) { alpar@726: active[i].pop(); //FIXME: ezt szebben kene alpar@726: } alpar@726: }*/ 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: // active[newlevel].push(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@726: if ( g->valid(z) ) 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 alpar@726: }; //class MaxFlow alpar@726: } //namespace hugo alpar@726: alpar@726: #endif //HUGO_MAX_FLOW_H alpar@726: alpar@726: alpar@726: alpar@726: