jacint@714: // -*- C++ -*- jacint@714: #ifndef HUGO_MAX_FLOW_NO_STACK_H jacint@714: #define HUGO_MAX_FLOW_NO_STACK_H jacint@714: jacint@714: #include jacint@714: #include jacint@714: //#include jacint@714: jacint@714: #include jacint@714: #include jacint@714: #include jacint@714: #include jacint@714: #include jacint@714: jacint@714: /// \file jacint@714: /// \brief The same as max_flow.h, but without using stl stack for the active nodes. Only for test. jacint@714: /// \ingroup galgs jacint@714: jacint@714: namespace hugo { jacint@714: jacint@714: /// \addtogroup galgs jacint@714: /// @{ jacint@714: ///Maximum flow algorithms class. jacint@714: jacint@714: ///This class provides various algorithms for finding a flow of jacint@714: ///maximum value in a directed graph. The \e source node, the \e jacint@714: ///target node, the \e capacity of the edges and the \e starting \e jacint@714: ///flow value of the edges should be passed to the algorithm through the jacint@714: ///constructor. It is possible to change these quantities using the jacint@714: ///functions \ref resetSource, \ref resetTarget, \ref resetCap and jacint@714: ///\ref resetFlow. Before any subsequent runs of any algorithm of jacint@714: ///the class \ref resetFlow should be called. jacint@714: jacint@714: ///After running an algorithm of the class, the actual flow value jacint@714: ///can be obtained by calling \ref flowValue(). The minimum jacint@714: ///value cut can be written into a \c node map of \c bools by jacint@714: ///calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes jacint@714: ///the inclusionwise minimum and maximum of the minimum value jacint@714: ///cuts, resp.) jacint@714: ///\param Graph The directed graph type the algorithm runs on. jacint@714: ///\param Num The number type of the capacities and the flow values. jacint@714: ///\param CapMap The capacity map type. jacint@714: ///\param FlowMap The flow map type. jacint@714: ///\author Marton Makai, Jacint Szabo jacint@714: template , jacint@714: typename FlowMap=typename Graph::template EdgeMap > jacint@714: class MaxFlowNoStack { jacint@714: protected: jacint@714: typedef typename Graph::Node Node; jacint@714: typedef typename Graph::NodeIt NodeIt; jacint@714: typedef typename Graph::EdgeIt EdgeIt; jacint@714: typedef typename Graph::OutEdgeIt OutEdgeIt; jacint@714: typedef typename Graph::InEdgeIt InEdgeIt; jacint@714: jacint@714: // typedef typename std::vector > VecStack; jacint@714: typedef typename std::vector VecFirst; jacint@714: typedef typename Graph::template NodeMap NNMap; jacint@714: typedef typename std::vector VecNode; jacint@714: jacint@714: const Graph* g; jacint@714: Node s; jacint@714: Node t; jacint@714: const CapMap* capacity; jacint@714: FlowMap* flow; jacint@714: int n; //the number of nodes of G jacint@714: typedef ResGraphWrapper ResGW; jacint@714: //typedef ExpResGraphWrapper ResGW; jacint@714: typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt; jacint@714: typedef typename ResGW::Edge ResGWEdge; jacint@714: //typedef typename ResGW::template NodeMap ReachedMap; jacint@714: typedef typename Graph::template NodeMap ReachedMap; jacint@714: jacint@714: jacint@714: //level works as a bool map in augmenting path algorithms and is jacint@714: //used by bfs for storing reached information. In preflow, it jacint@714: //shows the levels of nodes. jacint@714: ReachedMap level; jacint@714: jacint@714: //excess is needed only in preflow jacint@714: typename Graph::template NodeMap excess; jacint@714: jacint@714: //fixme jacint@714: // protected: jacint@714: // MaxFlow() { } jacint@714: // void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity, jacint@714: // FlowMap& _flow) jacint@714: // { jacint@714: // g=&_G; jacint@714: // s=_s; jacint@714: // t=_t; jacint@714: // capacity=&_capacity; jacint@714: // flow=&_flow; jacint@714: // n=_G.nodeNum; jacint@714: // level.set (_G); //kellene vmi ilyesmi fv jacint@714: // excess(_G,0); //itt is jacint@714: // } jacint@714: jacint@714: // constants used for heuristics jacint@714: static const int H0=20; jacint@714: static const int H1=1; jacint@714: jacint@714: public: jacint@714: jacint@714: ///Indicates the property of the starting flow. jacint@714: jacint@714: ///Indicates the property of the starting flow. The meanings are as follows: jacint@714: ///- \c ZERO_FLOW: constant zero flow jacint@714: ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to jacint@714: ///the sum of the out-flows in every node except the \e source and jacint@714: ///the \e target. jacint@714: ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at jacint@714: ///least the sum of the out-flows in every node except the \e source. jacint@714: ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be jacint@714: ///set to the constant zero flow in the beginning of the algorithm in this case. jacint@714: enum FlowEnum{ jacint@714: ZERO_FLOW, jacint@714: GEN_FLOW, jacint@714: PRE_FLOW, jacint@714: NO_FLOW jacint@714: }; jacint@714: jacint@714: enum StatusEnum { jacint@714: AFTER_NOTHING, jacint@714: AFTER_AUGMENTING, jacint@714: AFTER_FAST_AUGMENTING, jacint@714: AFTER_PRE_FLOW_PHASE_1, jacint@714: AFTER_PRE_FLOW_PHASE_2 jacint@714: }; jacint@714: jacint@714: /// Don not needle this flag only if necessary. jacint@714: StatusEnum status; jacint@714: int number_of_augmentations; jacint@714: jacint@714: jacint@714: template jacint@714: class TrickyReachedMap { jacint@714: protected: jacint@714: IntMap* map; jacint@714: int* number_of_augmentations; jacint@714: public: jacint@714: TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) : jacint@714: map(&_map), number_of_augmentations(&_number_of_augmentations) { } jacint@714: void set(const Node& n, bool b) { jacint@714: if (b) jacint@714: map->set(n, *number_of_augmentations); jacint@714: else jacint@714: map->set(n, *number_of_augmentations-1); jacint@714: } jacint@714: bool operator[](const Node& n) const { jacint@714: return (*map)[n]==*number_of_augmentations; jacint@714: } jacint@714: }; jacint@714: jacint@714: ///Constructor jacint@714: jacint@714: ///\todo Document, please. jacint@714: /// alpar@719: MaxFlowNoStack(const Graph& _G, Node _s, Node _t, alpar@719: const CapMap& _capacity, FlowMap& _flow) : jacint@714: g(&_G), s(_s), t(_t), capacity(&_capacity), jacint@714: flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0), jacint@714: status(AFTER_NOTHING), number_of_augmentations(0) { } jacint@714: jacint@714: ///Runs a maximum flow algorithm. jacint@714: jacint@714: ///Runs a preflow algorithm, which is the fastest maximum flow jacint@714: ///algorithm up-to-date. The default for \c fe is ZERO_FLOW. jacint@714: ///\pre The starting flow must be jacint@714: /// - a constant zero flow if \c fe is \c ZERO_FLOW, jacint@714: /// - an arbitary flow if \c fe is \c GEN_FLOW, jacint@714: /// - an arbitary preflow if \c fe is \c PRE_FLOW, jacint@714: /// - any map if \c fe is NO_FLOW. jacint@714: void run(FlowEnum fe=ZERO_FLOW) { jacint@714: preflow(fe); jacint@714: } jacint@714: jacint@714: jacint@714: ///Runs a preflow algorithm. jacint@714: jacint@714: ///Runs a preflow algorithm. The preflow algorithms provide the jacint@714: ///fastest way to compute a maximum flow in a directed graph. jacint@714: ///\pre The starting flow must be jacint@714: /// - a constant zero flow if \c fe is \c ZERO_FLOW, jacint@714: /// - an arbitary flow if \c fe is \c GEN_FLOW, jacint@714: /// - an arbitary preflow if \c fe is \c PRE_FLOW, jacint@714: /// - any map if \c fe is NO_FLOW. jacint@714: /// jacint@714: ///\todo NO_FLOW should be the default flow. jacint@714: void preflow(FlowEnum fe) { jacint@714: preflowPhase1(fe); jacint@714: preflowPhase2(); jacint@714: } jacint@714: // Heuristics: jacint@714: // 2 phase jacint@714: // gap jacint@714: // list 'level_list' on the nodes on level i implemented by hand jacint@714: // stack 'active' on the active nodes on level i jacint@714: // runs heuristic 'highest label' for H1*n relabels jacint@714: // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label' jacint@714: // Parameters H0 and H1 are initialized to 20 and 1. jacint@714: jacint@714: ///Runs the first phase of the preflow algorithm. jacint@714: jacint@714: ///The preflow algorithm consists of two phases, this method runs the jacint@714: ///first phase. After the first phase the maximum flow value and a jacint@714: ///minimum value cut can already be computed, though a maximum flow jacint@714: ///is net yet obtained. So after calling this method \ref flowValue jacint@714: ///and \ref actMinCut gives proper results. jacint@714: ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not jacint@714: ///give minimum value cuts unless calling \ref preflowPhase2. jacint@714: ///\pre The starting flow must be jacint@714: /// - a constant zero flow if \c fe is \c ZERO_FLOW, jacint@714: /// - an arbitary flow if \c fe is \c GEN_FLOW, jacint@714: /// - an arbitary preflow if \c fe is \c PRE_FLOW, jacint@714: /// - any map if \c fe is NO_FLOW. jacint@714: void preflowPhase1(FlowEnum fe); jacint@714: jacint@714: ///Runs the second phase of the preflow algorithm. jacint@714: jacint@714: ///The preflow algorithm consists of two phases, this method runs jacint@714: ///the second phase. After calling \ref preflowPhase1 and then jacint@714: ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut, jacint@714: ///\ref minMinCut and \ref maxMinCut give proper results. jacint@714: ///\pre \ref preflowPhase1 must be called before. jacint@714: void preflowPhase2(); jacint@714: jacint@714: /// Starting from a flow, this method searches for an augmenting path jacint@714: /// according to the Edmonds-Karp algorithm jacint@714: /// and augments the flow on if any. jacint@714: /// The return value shows if the augmentation was succesful. jacint@714: bool augmentOnShortestPath(); jacint@714: bool augmentOnShortestPath2(); jacint@714: jacint@714: /// Starting from a flow, this method searches for an augmenting blocking jacint@714: /// flow according to Dinits' algorithm and augments the flow on if any. jacint@714: /// The blocking flow is computed in a physically constructed jacint@714: /// residual graph of type \c Mutablegraph. jacint@714: /// The return value show sif the augmentation was succesful. jacint@714: template bool augmentOnBlockingFlow(); jacint@714: jacint@714: /// The same as \c augmentOnBlockingFlow but the jacint@714: /// residual graph is not constructed physically. jacint@714: /// The return value shows if the augmentation was succesful. jacint@714: bool augmentOnBlockingFlow2(); jacint@714: jacint@714: /// Returns the maximum value of a flow. jacint@714: jacint@714: /// Returns the maximum value of a flow, by counting the jacint@714: /// over-flow of the target node \ref t. jacint@714: /// It can be called already after running \ref preflowPhase1. jacint@714: Num flowValue() const { jacint@714: Num a=0; jacint@714: FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e]; jacint@714: FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e]; jacint@714: return a; jacint@714: //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan jacint@714: } jacint@714: jacint@714: ///Returns a minimum value cut after calling \ref preflowPhase1. jacint@714: jacint@714: ///After the first phase of the preflow algorithm the maximum flow jacint@714: ///value and a minimum value cut can already be computed. This jacint@714: ///method can be called after running \ref preflowPhase1 for jacint@714: ///obtaining a minimum value cut. jacint@714: /// \warning Gives proper result only right after calling \ref jacint@714: /// preflowPhase1. jacint@714: /// \todo We have to make some status variable which shows the jacint@714: /// actual state jacint@714: /// of the class. This enables us to determine which methods are valid jacint@714: /// for MinCut computation jacint@714: template jacint@714: void actMinCut(_CutMap& M) const { jacint@714: NodeIt v; jacint@714: switch (status) { jacint@714: case AFTER_PRE_FLOW_PHASE_1: jacint@714: for(g->first(v); g->valid(v); g->next(v)) { jacint@714: if (level[v] < n) { jacint@714: M.set(v, false); jacint@714: } else { jacint@714: M.set(v, true); jacint@714: } jacint@714: } jacint@714: break; jacint@714: case AFTER_PRE_FLOW_PHASE_2: jacint@714: case AFTER_NOTHING: jacint@714: minMinCut(M); jacint@714: break; jacint@714: case AFTER_AUGMENTING: jacint@714: for(g->first(v); g->valid(v); g->next(v)) { jacint@714: if (level[v]) { jacint@714: M.set(v, true); jacint@714: } else { jacint@714: M.set(v, false); jacint@714: } jacint@714: } jacint@714: break; jacint@714: case AFTER_FAST_AUGMENTING: jacint@714: for(g->first(v); g->valid(v); g->next(v)) { jacint@714: if (level[v]==number_of_augmentations) { jacint@714: M.set(v, true); jacint@714: } else { jacint@714: M.set(v, false); jacint@714: } jacint@714: } jacint@714: break; jacint@714: } jacint@714: } jacint@714: jacint@714: ///Returns the inclusionwise minimum of the minimum value cuts. jacint@714: jacint@714: ///Sets \c M to the characteristic vector of the minimum value cut jacint@714: ///which is inclusionwise minimum. It is computed by processing jacint@714: ///a bfs from the source node \c s in the residual graph. jacint@714: ///\pre M should be a node map of bools initialized to false. jacint@714: ///\pre \c flow must be a maximum flow. jacint@714: template jacint@714: void minMinCut(_CutMap& M) const { jacint@714: std::queue queue; jacint@714: jacint@714: M.set(s,true); jacint@714: queue.push(s); jacint@714: jacint@714: while (!queue.empty()) { jacint@714: Node w=queue.front(); jacint@714: queue.pop(); jacint@714: jacint@714: OutEdgeIt e; jacint@714: for(g->first(e,w) ; g->valid(e); g->next(e)) { jacint@714: Node v=g->head(e); jacint@714: if (!M[v] && (*flow)[e] < (*capacity)[e] ) { jacint@714: queue.push(v); jacint@714: M.set(v, true); jacint@714: } jacint@714: } jacint@714: jacint@714: InEdgeIt f; jacint@714: for(g->first(f,w) ; g->valid(f); g->next(f)) { jacint@714: Node v=g->tail(f); jacint@714: if (!M[v] && (*flow)[f] > 0 ) { jacint@714: queue.push(v); jacint@714: M.set(v, true); jacint@714: } jacint@714: } jacint@714: } jacint@714: } jacint@714: jacint@714: ///Returns the inclusionwise maximum of the minimum value cuts. jacint@714: jacint@714: ///Sets \c M to the characteristic vector of the minimum value cut jacint@714: ///which is inclusionwise maximum. It is computed by processing a jacint@714: ///backward bfs from the target node \c t in the residual graph. jacint@714: ///\pre M should be a node map of bools initialized to false. jacint@714: ///\pre \c flow must be a maximum flow. jacint@714: template jacint@714: void maxMinCut(_CutMap& M) const { jacint@714: jacint@714: NodeIt v; jacint@714: for(g->first(v) ; g->valid(v); g->next(v)) { jacint@714: M.set(v, true); jacint@714: } jacint@714: jacint@714: std::queue queue; jacint@714: jacint@714: M.set(t,false); jacint@714: queue.push(t); jacint@714: jacint@714: while (!queue.empty()) { jacint@714: Node w=queue.front(); jacint@714: queue.pop(); jacint@714: jacint@714: InEdgeIt e; jacint@714: for(g->first(e,w) ; g->valid(e); g->next(e)) { jacint@714: Node v=g->tail(e); jacint@714: if (M[v] && (*flow)[e] < (*capacity)[e] ) { jacint@714: queue.push(v); jacint@714: M.set(v, false); jacint@714: } jacint@714: } jacint@714: jacint@714: OutEdgeIt f; jacint@714: for(g->first(f,w) ; g->valid(f); g->next(f)) { jacint@714: Node v=g->head(f); jacint@714: if (M[v] && (*flow)[f] > 0 ) { jacint@714: queue.push(v); jacint@714: M.set(v, false); jacint@714: } jacint@714: } jacint@714: } jacint@714: } jacint@714: jacint@714: ///Returns a minimum value cut. jacint@714: jacint@714: ///Sets \c M to the characteristic vector of a minimum value cut. jacint@714: ///\pre M should be a node map of bools initialized to false. jacint@714: ///\pre \c flow must be a maximum flow. jacint@714: template jacint@714: void minCut(CutMap& M) const { minMinCut(M); } jacint@714: jacint@714: ///Resets the source node to \c _s. jacint@714: jacint@714: ///Resets the source node to \c _s. jacint@714: /// jacint@714: void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; } jacint@714: jacint@714: ///Resets the target node to \c _t. jacint@714: jacint@714: ///Resets the target node to \c _t. jacint@714: /// jacint@714: void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; } jacint@714: jacint@714: /// Resets the edge map of the capacities to _cap. jacint@714: jacint@714: /// Resets the edge map of the capacities to _cap. jacint@714: /// alpar@719: void resetCap(const CapMap& _cap) alpar@719: { capacity=&_cap; status=AFTER_NOTHING; } jacint@714: jacint@714: /// Resets the edge map of the flows to _flow. jacint@714: jacint@714: /// Resets the edge map of the flows to _flow. jacint@714: /// jacint@714: void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; } jacint@714: jacint@714: jacint@714: private: jacint@714: jacint@714: int push(Node w, NNMap& next, VecFirst& first) { jacint@714: jacint@714: int lev=level[w]; jacint@714: Num exc=excess[w]; jacint@714: int newlevel=n; //bound on the next level of w jacint@714: jacint@714: OutEdgeIt e; jacint@714: for(g->first(e,w); g->valid(e); g->next(e)) { jacint@714: jacint@714: if ( (*flow)[e] >= (*capacity)[e] ) continue; jacint@714: Node v=g->head(e); jacint@714: jacint@714: if( lev > level[v] ) { //Push is allowed now jacint@714: jacint@714: if ( excess[v]<=0 && v!=t && v!=s ) { jacint@714: next.set(v,first[level[v]]); jacint@714: first[level[v]]=v; jacint@714: // int lev_v=level[v]; jacint@714: //active[lev_v].push(v); jacint@714: } jacint@714: jacint@714: Num cap=(*capacity)[e]; jacint@714: Num flo=(*flow)[e]; jacint@714: Num remcap=cap-flo; jacint@714: jacint@714: if ( remcap >= exc ) { //A nonsaturating push. jacint@714: jacint@714: flow->set(e, flo+exc); jacint@714: excess.set(v, excess[v]+exc); jacint@714: exc=0; jacint@714: break; jacint@714: jacint@714: } else { //A saturating push. jacint@714: flow->set(e, cap); jacint@714: excess.set(v, excess[v]+remcap); jacint@714: exc-=remcap; jacint@714: } jacint@714: } else if ( newlevel > level[v] ) newlevel = level[v]; jacint@714: } //for out edges wv jacint@714: jacint@714: if ( exc > 0 ) { jacint@714: InEdgeIt e; jacint@714: for(g->first(e,w); g->valid(e); g->next(e)) { jacint@714: jacint@714: if( (*flow)[e] <= 0 ) continue; jacint@714: Node v=g->tail(e); jacint@714: jacint@714: if( lev > level[v] ) { //Push is allowed now jacint@714: jacint@714: if ( excess[v]<=0 && v!=t && v!=s ) { jacint@714: next.set(v,first[level[v]]); jacint@714: first[level[v]]=v; jacint@714: //int lev_v=level[v]; jacint@714: //active[lev_v].push(v); jacint@714: } jacint@714: jacint@714: Num flo=(*flow)[e]; jacint@714: jacint@714: if ( flo >= exc ) { //A nonsaturating push. jacint@714: jacint@714: flow->set(e, flo-exc); jacint@714: excess.set(v, excess[v]+exc); jacint@714: exc=0; jacint@714: break; jacint@714: } else { //A saturating push. jacint@714: jacint@714: excess.set(v, excess[v]+flo); jacint@714: exc-=flo; jacint@714: flow->set(e,0); jacint@714: } jacint@714: } else if ( newlevel > level[v] ) newlevel = level[v]; jacint@714: } //for in edges vw jacint@714: jacint@714: } // if w still has excess after the out edge for cycle jacint@714: jacint@714: excess.set(w, exc); jacint@714: jacint@714: return newlevel; jacint@714: } jacint@714: jacint@714: jacint@714: void preflowPreproc(FlowEnum fe, NNMap& next, VecFirst& first, jacint@714: VecNode& level_list, NNMap& left, NNMap& right) jacint@714: { jacint@714: std::queue bfs_queue; jacint@714: jacint@714: switch (fe) { jacint@714: case NO_FLOW: //flow is already set to const zero in this case jacint@714: case ZERO_FLOW: jacint@714: { jacint@714: //Reverse_bfs from t, to find the starting level. jacint@714: level.set(t,0); jacint@714: bfs_queue.push(t); jacint@714: jacint@714: while (!bfs_queue.empty()) { jacint@714: jacint@714: Node v=bfs_queue.front(); jacint@714: bfs_queue.pop(); jacint@714: int l=level[v]+1; jacint@714: jacint@714: InEdgeIt e; jacint@714: for(g->first(e,v); g->valid(e); g->next(e)) { jacint@714: Node w=g->tail(e); jacint@714: if ( level[w] == n && w != s ) { jacint@714: bfs_queue.push(w); jacint@714: Node z=level_list[l]; jacint@714: if ( g->valid(z) ) left.set(z,w); jacint@714: right.set(w,z); jacint@714: level_list[l]=w; jacint@714: level.set(w, l); jacint@714: } jacint@714: } jacint@714: } jacint@714: jacint@714: //the starting flow jacint@714: OutEdgeIt e; jacint@714: for(g->first(e,s); g->valid(e); g->next(e)) jacint@714: { jacint@714: Num c=(*capacity)[e]; jacint@714: if ( c <= 0 ) continue; jacint@714: Node w=g->head(e); jacint@714: if ( level[w] < n ) { jacint@714: if ( excess[w] <= 0 && w!=t ) jacint@714: { jacint@714: next.set(w,first[level[w]]); jacint@714: first[level[w]]=w; jacint@714: //active[level[w]].push(w); jacint@714: } jacint@714: flow->set(e, c); jacint@714: excess.set(w, excess[w]+c); jacint@714: } jacint@714: } jacint@714: break; jacint@714: } jacint@714: jacint@714: case GEN_FLOW: jacint@714: case PRE_FLOW: jacint@714: { jacint@714: //Reverse_bfs from t in the residual graph, jacint@714: //to find the starting level. jacint@714: level.set(t,0); jacint@714: bfs_queue.push(t); jacint@714: jacint@714: while (!bfs_queue.empty()) { jacint@714: jacint@714: Node v=bfs_queue.front(); jacint@714: bfs_queue.pop(); jacint@714: int l=level[v]+1; jacint@714: jacint@714: InEdgeIt e; jacint@714: for(g->first(e,v); g->valid(e); g->next(e)) { jacint@714: if ( (*capacity)[e] <= (*flow)[e] ) continue; jacint@714: Node w=g->tail(e); jacint@714: if ( level[w] == n && w != s ) { jacint@714: bfs_queue.push(w); jacint@714: Node z=level_list[l]; jacint@714: if ( g->valid(z) ) left.set(z,w); jacint@714: right.set(w,z); jacint@714: level_list[l]=w; jacint@714: level.set(w, l); jacint@714: } jacint@714: } jacint@714: jacint@714: OutEdgeIt f; jacint@714: for(g->first(f,v); g->valid(f); g->next(f)) { jacint@714: if ( 0 >= (*flow)[f] ) continue; jacint@714: Node w=g->head(f); jacint@714: if ( level[w] == n && w != s ) { jacint@714: bfs_queue.push(w); jacint@714: Node z=level_list[l]; jacint@714: if ( g->valid(z) ) left.set(z,w); jacint@714: right.set(w,z); jacint@714: level_list[l]=w; jacint@714: level.set(w, l); jacint@714: } jacint@714: } jacint@714: } jacint@714: jacint@714: jacint@714: //the starting flow jacint@714: OutEdgeIt e; jacint@714: for(g->first(e,s); g->valid(e); g->next(e)) jacint@714: { jacint@714: Num rem=(*capacity)[e]-(*flow)[e]; jacint@714: if ( rem <= 0 ) continue; jacint@714: Node w=g->head(e); jacint@714: if ( level[w] < n ) { jacint@714: if ( excess[w] <= 0 && w!=t ) jacint@714: { jacint@714: next.set(w,first[level[w]]); jacint@714: first[level[w]]=w; jacint@714: //active[level[w]].push(w); jacint@714: } jacint@714: flow->set(e, (*capacity)[e]); jacint@714: excess.set(w, excess[w]+rem); jacint@714: } jacint@714: } jacint@714: jacint@714: InEdgeIt f; jacint@714: for(g->first(f,s); g->valid(f); g->next(f)) jacint@714: { jacint@714: if ( (*flow)[f] <= 0 ) continue; jacint@714: Node w=g->tail(f); jacint@714: if ( level[w] < n ) { jacint@714: if ( excess[w] <= 0 && w!=t ) jacint@714: { jacint@714: next.set(w,first[level[w]]); jacint@714: first[level[w]]=w; jacint@714: //active[level[w]].push(w); jacint@714: } jacint@714: excess.set(w, excess[w]+(*flow)[f]); jacint@714: flow->set(f, 0); jacint@714: } jacint@714: } jacint@714: break; jacint@714: } //case PRE_FLOW jacint@714: } jacint@714: } //preflowPreproc jacint@714: jacint@714: jacint@714: jacint@714: void relabel(Node w, int newlevel, NNMap& next, VecFirst& first, jacint@714: VecNode& level_list, NNMap& left, jacint@714: NNMap& right, int& b, int& k, bool what_heur ) jacint@714: { jacint@714: jacint@714: Num lev=level[w]; jacint@714: jacint@714: Node right_n=right[w]; jacint@714: Node left_n=left[w]; jacint@714: jacint@714: //unlacing starts jacint@714: if ( g->valid(right_n) ) { jacint@714: if ( g->valid(left_n) ) { jacint@714: right.set(left_n, right_n); jacint@714: left.set(right_n, left_n); jacint@714: } else { jacint@714: level_list[lev]=right_n; jacint@714: left.set(right_n, INVALID); jacint@714: } jacint@714: } else { jacint@714: if ( g->valid(left_n) ) { jacint@714: right.set(left_n, INVALID); jacint@714: } else { jacint@714: level_list[lev]=INVALID; jacint@714: } jacint@714: } jacint@714: //unlacing ends jacint@714: jacint@714: if ( !g->valid(level_list[lev]) ) { jacint@714: jacint@714: //gapping starts jacint@714: for (int i=lev; i!=k ; ) { jacint@714: Node v=level_list[++i]; jacint@714: while ( g->valid(v) ) { jacint@714: level.set(v,n); jacint@714: v=right[v]; jacint@714: } jacint@714: level_list[i]=INVALID; jacint@714: if ( !what_heur ) first[i]=INVALID; jacint@714: /*{ jacint@714: while ( !active[i].empty() ) { jacint@714: active[i].pop(); //FIXME: ezt szebben kene jacint@714: } jacint@714: }*/ jacint@714: } jacint@714: jacint@714: level.set(w,n); jacint@714: b=lev-1; jacint@714: k=b; jacint@714: //gapping ends jacint@714: jacint@714: } else { jacint@714: jacint@714: if ( newlevel == n ) level.set(w,n); jacint@714: else { jacint@714: level.set(w,++newlevel); jacint@714: next.set(w,first[newlevel]); jacint@714: first[newlevel]=w; jacint@714: // active[newlevel].push(w); jacint@714: if ( what_heur ) b=newlevel; jacint@714: if ( k < newlevel ) ++k; //now k=newlevel jacint@714: Node z=level_list[newlevel]; jacint@714: if ( g->valid(z) ) left.set(z,w); jacint@714: right.set(w,z); jacint@714: left.set(w,INVALID); jacint@714: level_list[newlevel]=w; jacint@714: } jacint@714: } jacint@714: jacint@714: } //relabel jacint@714: jacint@714: jacint@714: template jacint@714: class DistanceMap { jacint@714: protected: jacint@714: const MapGraphWrapper* g; jacint@714: typename MapGraphWrapper::template NodeMap dist; jacint@714: public: jacint@714: DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { } jacint@714: void set(const typename MapGraphWrapper::Node& n, int a) { jacint@714: dist.set(n, a); jacint@714: } jacint@714: int operator[](const typename MapGraphWrapper::Node& n) const { jacint@714: return dist[n]; jacint@714: } jacint@714: // int get(const typename MapGraphWrapper::Node& n) const { jacint@714: // return dist[n]; } jacint@714: // bool get(const typename MapGraphWrapper::Edge& e) const { jacint@714: // return (dist.get(g->tail(e))head(e))); } jacint@714: bool operator[](const typename MapGraphWrapper::Edge& e) const { jacint@714: return (dist[g->tail(e)]head(e)]); jacint@714: } jacint@714: }; jacint@714: jacint@714: }; jacint@714: jacint@714: jacint@714: template jacint@714: void MaxFlowNoStack::preflowPhase1(FlowEnum fe) jacint@714: { jacint@714: jacint@714: int heur0=(int)(H0*n); //time while running 'bound decrease' jacint@714: int heur1=(int)(H1*n); //time while running 'highest label' jacint@714: int heur=heur1; //starting time interval (#of relabels) jacint@714: int numrelabel=0; jacint@714: jacint@714: bool what_heur=1; jacint@714: //It is 0 in case 'bound decrease' and 1 in case 'highest label' jacint@714: jacint@714: bool end=false; jacint@714: //Needed for 'bound decrease', true means no active nodes are above bound jacint@714: //b. jacint@714: jacint@714: int k=n-2; //bound on the highest level under n containing a node jacint@714: int b=k; //bound on the highest level under n of an active node jacint@714: jacint@714: VecFirst first(n, INVALID); jacint@714: NNMap next(*g, INVALID); //maybe INVALID is not needed jacint@714: // VecStack active(n); jacint@714: jacint@714: NNMap left(*g, INVALID); jacint@714: NNMap right(*g, INVALID); jacint@714: VecNode level_list(n,INVALID); jacint@714: //List of the nodes in level ifirst(v); g->valid(v); g->next(v)) level.set(v,n); jacint@714: //setting each node to level n jacint@714: jacint@714: if ( fe == NO_FLOW ) { jacint@714: EdgeIt e; jacint@714: for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0); jacint@714: } jacint@714: jacint@714: switch (fe) { //computing the excess jacint@714: case PRE_FLOW: jacint@714: { jacint@714: NodeIt v; jacint@714: for(g->first(v); g->valid(v); g->next(v)) { jacint@714: Num exc=0; jacint@714: jacint@714: InEdgeIt e; jacint@714: for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e]; jacint@714: OutEdgeIt f; jacint@714: for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f]; jacint@714: jacint@714: excess.set(v,exc); jacint@714: jacint@714: //putting the active nodes into the stack jacint@714: int lev=level[v]; jacint@714: if ( exc > 0 && lev < n && v != t ) jacint@714: { jacint@714: next.set(v,first[lev]); jacint@714: first[lev]=v; jacint@714: } jacint@714: // active[lev].push(v); jacint@714: } jacint@714: break; jacint@714: } jacint@714: case GEN_FLOW: jacint@714: { jacint@714: NodeIt v; jacint@714: for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0); jacint@714: jacint@714: Num exc=0; jacint@714: InEdgeIt e; jacint@714: for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e]; jacint@714: OutEdgeIt f; jacint@714: for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f]; jacint@714: excess.set(t,exc); jacint@714: break; jacint@714: } jacint@714: case ZERO_FLOW: jacint@714: case NO_FLOW: jacint@714: { jacint@714: NodeIt v; jacint@714: for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0); jacint@714: break; jacint@714: } jacint@714: } jacint@714: jacint@714: preflowPreproc(fe, next, first,/*active*/ level_list, left, right); jacint@714: //End of preprocessing jacint@714: jacint@714: jacint@714: //Push/relabel on the highest level active nodes. jacint@714: while ( true ) { jacint@714: if ( b == 0 ) { jacint@714: if ( !what_heur && !end && k > 0 ) { jacint@714: b=k; jacint@714: end=true; jacint@714: } else break; jacint@714: } jacint@714: jacint@714: if ( !g->valid(first[b])/*active[b].empty()*/ ) --b; jacint@714: else { jacint@714: end=false; jacint@714: Node w=first[b]; jacint@714: first[b]=next[w]; jacint@714: /* Node w=active[b].top(); jacint@714: active[b].pop();*/ jacint@714: int newlevel=push(w,/*active*/next, first); jacint@714: if ( excess[w] > 0 ) relabel(w, newlevel, /*active*/next, first, level_list, jacint@714: left, right, b, k, what_heur); jacint@714: jacint@714: ++numrelabel; jacint@714: if ( numrelabel >= heur ) { jacint@714: numrelabel=0; jacint@714: if ( what_heur ) { jacint@714: what_heur=0; jacint@714: heur=heur0; jacint@714: end=false; jacint@714: } else { jacint@714: what_heur=1; jacint@714: heur=heur1; jacint@714: b=k; jacint@714: } jacint@714: } jacint@714: } jacint@714: } jacint@714: jacint@714: status=AFTER_PRE_FLOW_PHASE_1; jacint@714: } jacint@714: jacint@714: jacint@714: jacint@714: template jacint@714: void MaxFlowNoStack::preflowPhase2() jacint@714: { jacint@714: jacint@714: int k=n-2; //bound on the highest level under n containing a node jacint@714: int b=k; //bound on the highest level under n of an active node jacint@714: jacint@714: jacint@714: VecFirst first(n, INVALID); jacint@714: NNMap next(*g, INVALID); //maybe INVALID is not needed jacint@714: // VecStack active(n); jacint@714: level.set(s,0); jacint@714: std::queue bfs_queue; jacint@714: bfs_queue.push(s); jacint@714: jacint@714: while (!bfs_queue.empty()) { jacint@714: jacint@714: Node v=bfs_queue.front(); jacint@714: bfs_queue.pop(); jacint@714: int l=level[v]+1; jacint@714: jacint@714: InEdgeIt e; jacint@714: for(g->first(e,v); g->valid(e); g->next(e)) { jacint@714: if ( (*capacity)[e] <= (*flow)[e] ) continue; jacint@714: Node u=g->tail(e); jacint@714: if ( level[u] >= n ) { jacint@714: bfs_queue.push(u); jacint@714: level.set(u, l); jacint@714: if ( excess[u] > 0 ) { jacint@714: next.set(u,first[l]); jacint@714: first[l]=u; jacint@714: //active[l].push(u); jacint@714: } jacint@714: } jacint@714: } jacint@714: jacint@714: OutEdgeIt f; jacint@714: for(g->first(f,v); g->valid(f); g->next(f)) { jacint@714: if ( 0 >= (*flow)[f] ) continue; jacint@714: Node u=g->head(f); jacint@714: if ( level[u] >= n ) { jacint@714: bfs_queue.push(u); jacint@714: level.set(u, l); jacint@714: if ( excess[u] > 0 ) { jacint@714: next.set(u,first[l]); jacint@714: first[l]=u; jacint@714: //active[l].push(u); jacint@714: } jacint@714: } jacint@714: } jacint@714: } jacint@714: b=n-2; jacint@714: jacint@714: while ( true ) { jacint@714: jacint@714: if ( b == 0 ) break; jacint@714: jacint@714: if ( !g->valid(first[b])/*active[b].empty()*/ ) --b; jacint@714: else { jacint@714: jacint@714: Node w=first[b]; jacint@714: first[b]=next[w]; jacint@714: /* Node w=active[b].top(); jacint@714: active[b].pop();*/ jacint@714: int newlevel=push(w,next, first/*active*/); jacint@714: jacint@714: //relabel jacint@714: if ( excess[w] > 0 ) { jacint@714: level.set(w,++newlevel); jacint@714: next.set(w,first[newlevel]); jacint@714: first[newlevel]=w; jacint@714: //active[newlevel].push(w); jacint@714: b=newlevel; jacint@714: } jacint@714: } // if stack[b] is nonempty jacint@714: } // while(true) jacint@714: jacint@714: status=AFTER_PRE_FLOW_PHASE_2; jacint@714: } jacint@714: jacint@714: jacint@714: jacint@714: template jacint@714: bool MaxFlowNoStack::augmentOnShortestPath() jacint@714: { jacint@714: ResGW res_graph(*g, *capacity, *flow); jacint@714: bool _augment=false; jacint@714: jacint@714: //ReachedMap level(res_graph); jacint@714: FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); jacint@714: BfsIterator bfs(res_graph, level); jacint@714: bfs.pushAndSetReached(s); jacint@714: jacint@714: typename ResGW::template NodeMap pred(res_graph); jacint@714: pred.set(s, INVALID); jacint@714: jacint@714: typename ResGW::template NodeMap free(res_graph); jacint@714: jacint@714: //searching for augmenting path jacint@714: while ( !bfs.finished() ) { jacint@714: ResGWOutEdgeIt e=bfs; jacint@714: if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) { jacint@714: Node v=res_graph.tail(e); jacint@714: Node w=res_graph.head(e); jacint@714: pred.set(w, e); jacint@714: if (res_graph.valid(pred[v])) { jacint@714: free.set(w, std::min(free[v], res_graph.resCap(e))); jacint@714: } else { jacint@714: free.set(w, res_graph.resCap(e)); jacint@714: } jacint@714: if (res_graph.head(e)==t) { _augment=true; break; } jacint@714: } jacint@714: jacint@714: ++bfs; jacint@714: } //end of searching augmenting path jacint@714: jacint@714: if (_augment) { jacint@714: Node n=t; jacint@714: Num augment_value=free[t]; jacint@714: while (res_graph.valid(pred[n])) { jacint@714: ResGWEdge e=pred[n]; jacint@714: res_graph.augment(e, augment_value); jacint@714: n=res_graph.tail(e); jacint@714: } jacint@714: } jacint@714: jacint@714: status=AFTER_AUGMENTING; jacint@714: return _augment; jacint@714: } jacint@714: jacint@714: jacint@714: template jacint@714: bool MaxFlowNoStack::augmentOnShortestPath2() jacint@714: { jacint@714: ResGW res_graph(*g, *capacity, *flow); jacint@714: bool _augment=false; jacint@714: jacint@714: if (status!=AFTER_FAST_AUGMENTING) { jacint@714: FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); jacint@714: number_of_augmentations=1; jacint@714: } else { jacint@714: ++number_of_augmentations; jacint@714: } jacint@714: TrickyReachedMap jacint@714: tricky_reached_map(level, number_of_augmentations); jacint@714: //ReachedMap level(res_graph); jacint@714: // FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); jacint@714: BfsIterator > jacint@714: bfs(res_graph, tricky_reached_map); jacint@714: bfs.pushAndSetReached(s); jacint@714: jacint@714: typename ResGW::template NodeMap pred(res_graph); jacint@714: pred.set(s, INVALID); jacint@714: jacint@714: typename ResGW::template NodeMap free(res_graph); jacint@714: jacint@714: //searching for augmenting path jacint@714: while ( !bfs.finished() ) { jacint@714: ResGWOutEdgeIt e=bfs; jacint@714: if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) { jacint@714: Node v=res_graph.tail(e); jacint@714: Node w=res_graph.head(e); jacint@714: pred.set(w, e); jacint@714: if (res_graph.valid(pred[v])) { jacint@714: free.set(w, std::min(free[v], res_graph.resCap(e))); jacint@714: } else { jacint@714: free.set(w, res_graph.resCap(e)); jacint@714: } jacint@714: if (res_graph.head(e)==t) { _augment=true; break; } jacint@714: } jacint@714: jacint@714: ++bfs; jacint@714: } //end of searching augmenting path jacint@714: jacint@714: if (_augment) { jacint@714: Node n=t; jacint@714: Num augment_value=free[t]; jacint@714: while (res_graph.valid(pred[n])) { jacint@714: ResGWEdge e=pred[n]; jacint@714: res_graph.augment(e, augment_value); jacint@714: n=res_graph.tail(e); jacint@714: } jacint@714: } jacint@714: jacint@714: status=AFTER_FAST_AUGMENTING; jacint@714: return _augment; jacint@714: } jacint@714: jacint@714: jacint@714: template jacint@714: template jacint@714: bool MaxFlowNoStack::augmentOnBlockingFlow() jacint@714: { jacint@714: typedef MutableGraph MG; jacint@714: bool _augment=false; jacint@714: jacint@714: ResGW res_graph(*g, *capacity, *flow); jacint@714: jacint@714: //bfs for distances on the residual graph jacint@714: //ReachedMap level(res_graph); jacint@714: FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); jacint@714: BfsIterator bfs(res_graph, level); jacint@714: bfs.pushAndSetReached(s); jacint@714: typename ResGW::template NodeMap jacint@714: dist(res_graph); //filled up with 0's jacint@714: jacint@714: //F will contain the physical copy of the residual graph jacint@714: //with the set of edges which are on shortest paths jacint@714: MG F; jacint@714: typename ResGW::template NodeMap jacint@714: res_graph_to_F(res_graph); jacint@714: { jacint@714: typename ResGW::NodeIt n; jacint@714: for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) { jacint@714: res_graph_to_F.set(n, F.addNode()); jacint@714: } jacint@714: } jacint@714: jacint@714: typename MG::Node sF=res_graph_to_F[s]; jacint@714: typename MG::Node tF=res_graph_to_F[t]; jacint@714: typename MG::template EdgeMap original_edge(F); jacint@714: typename MG::template EdgeMap residual_capacity(F); jacint@714: jacint@714: while ( !bfs.finished() ) { jacint@714: ResGWOutEdgeIt e=bfs; jacint@714: if (res_graph.valid(e)) { jacint@714: if (bfs.isBNodeNewlyReached()) { jacint@714: dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1); jacint@714: typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], jacint@714: res_graph_to_F[res_graph.head(e)]); jacint@714: original_edge.update(); jacint@714: original_edge.set(f, e); jacint@714: residual_capacity.update(); jacint@714: residual_capacity.set(f, res_graph.resCap(e)); jacint@714: } else { jacint@714: if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) { jacint@714: typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], jacint@714: res_graph_to_F[res_graph.head(e)]); jacint@714: original_edge.update(); jacint@714: original_edge.set(f, e); jacint@714: residual_capacity.update(); jacint@714: residual_capacity.set(f, res_graph.resCap(e)); jacint@714: } jacint@714: } jacint@714: } jacint@714: ++bfs; jacint@714: } //computing distances from s in the residual graph jacint@714: jacint@714: bool __augment=true; jacint@714: jacint@714: while (__augment) { jacint@714: __augment=false; jacint@714: //computing blocking flow with dfs jacint@714: DfsIterator< MG, typename MG::template NodeMap > dfs(F); jacint@714: typename MG::template NodeMap pred(F); jacint@714: pred.set(sF, INVALID); jacint@714: //invalid iterators for sources jacint@714: jacint@714: typename MG::template NodeMap free(F); jacint@714: jacint@714: dfs.pushAndSetReached(sF); jacint@714: while (!dfs.finished()) { jacint@714: ++dfs; jacint@714: if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) { jacint@714: if (dfs.isBNodeNewlyReached()) { jacint@714: typename MG::Node v=F.aNode(dfs); jacint@714: typename MG::Node w=F.bNode(dfs); jacint@714: pred.set(w, dfs); jacint@714: if (F.valid(pred[v])) { jacint@714: free.set(w, std::min(free[v], residual_capacity[dfs])); jacint@714: } else { jacint@714: free.set(w, residual_capacity[dfs]); jacint@714: } jacint@714: if (w==tF) { jacint@714: __augment=true; jacint@714: _augment=true; jacint@714: break; jacint@714: } jacint@714: jacint@714: } else { jacint@714: F.erase(/*typename MG::OutEdgeIt*/(dfs)); jacint@714: } jacint@714: } jacint@714: } jacint@714: jacint@714: if (__augment) { jacint@714: typename MG::Node n=tF; jacint@714: Num augment_value=free[tF]; jacint@714: while (F.valid(pred[n])) { jacint@714: typename MG::Edge e=pred[n]; jacint@714: res_graph.augment(original_edge[e], augment_value); jacint@714: n=F.tail(e); jacint@714: if (residual_capacity[e]==augment_value) jacint@714: F.erase(e); jacint@714: else jacint@714: residual_capacity.set(e, residual_capacity[e]-augment_value); jacint@714: } jacint@714: } jacint@714: jacint@714: } jacint@714: jacint@714: status=AFTER_AUGMENTING; jacint@714: return _augment; jacint@714: } jacint@714: jacint@714: jacint@714: jacint@714: jacint@714: template jacint@714: bool MaxFlowNoStack::augmentOnBlockingFlow2() jacint@714: { jacint@714: bool _augment=false; jacint@714: jacint@714: ResGW res_graph(*g, *capacity, *flow); jacint@714: jacint@714: //ReachedMap level(res_graph); jacint@714: FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); jacint@714: BfsIterator bfs(res_graph, level); jacint@714: jacint@714: bfs.pushAndSetReached(s); jacint@714: DistanceMap dist(res_graph); jacint@714: while ( !bfs.finished() ) { jacint@714: ResGWOutEdgeIt e=bfs; jacint@714: if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) { jacint@714: dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1); jacint@714: } jacint@714: ++bfs; jacint@714: } //computing distances from s in the residual graph jacint@714: jacint@714: //Subgraph containing the edges on some shortest paths jacint@714: ConstMap true_map(true); jacint@714: typedef SubGraphWrapper, jacint@714: DistanceMap > FilterResGW; jacint@714: FilterResGW filter_res_graph(res_graph, true_map, dist); jacint@714: jacint@714: //Subgraph, which is able to delete edges which are already jacint@714: //met by the dfs jacint@714: typename FilterResGW::template NodeMap jacint@714: first_out_edges(filter_res_graph); jacint@714: typename FilterResGW::NodeIt v; jacint@714: for(filter_res_graph.first(v); filter_res_graph.valid(v); jacint@714: filter_res_graph.next(v)) jacint@714: { jacint@714: typename FilterResGW::OutEdgeIt e; jacint@714: filter_res_graph.first(e, v); jacint@714: first_out_edges.set(v, e); jacint@714: } jacint@714: typedef ErasingFirstGraphWrapper > ErasingResGW; jacint@714: ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges); jacint@714: jacint@714: bool __augment=true; jacint@714: jacint@714: while (__augment) { jacint@714: jacint@714: __augment=false; jacint@714: //computing blocking flow with dfs jacint@714: DfsIterator< ErasingResGW, jacint@714: typename ErasingResGW::template NodeMap > jacint@714: dfs(erasing_res_graph); jacint@714: typename ErasingResGW:: jacint@714: template NodeMap jacint@714: pred(erasing_res_graph); jacint@714: pred.set(s, INVALID); jacint@714: //invalid iterators for sources jacint@714: jacint@714: typename ErasingResGW::template NodeMap jacint@714: free1(erasing_res_graph); jacint@714: jacint@714: dfs.pushAndSetReached jacint@714: ///\bug hugo 0.2 jacint@714: (typename ErasingResGW::Node jacint@714: (typename FilterResGW::Node jacint@714: (typename ResGW::Node(s) jacint@714: ) jacint@714: ) jacint@714: ); jacint@714: while (!dfs.finished()) { jacint@714: ++dfs; jacint@714: if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs))) jacint@714: { jacint@714: if (dfs.isBNodeNewlyReached()) { jacint@714: jacint@714: typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs); jacint@714: typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs); jacint@714: jacint@714: pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs)); jacint@714: if (erasing_res_graph.valid(pred[v])) { jacint@714: free1.set jacint@714: (w, std::min(free1[v], res_graph.resCap jacint@714: (typename ErasingResGW::OutEdgeIt(dfs)))); jacint@714: } else { jacint@714: free1.set jacint@714: (w, res_graph.resCap jacint@714: (typename ErasingResGW::OutEdgeIt(dfs))); jacint@714: } jacint@714: jacint@714: if (w==t) { jacint@714: __augment=true; jacint@714: _augment=true; jacint@714: break; jacint@714: } jacint@714: } else { jacint@714: erasing_res_graph.erase(dfs); jacint@714: } jacint@714: } jacint@714: } jacint@714: jacint@714: if (__augment) { jacint@714: typename ErasingResGW::Node jacint@714: n=typename FilterResGW::Node(typename ResGW::Node(t)); jacint@714: // typename ResGW::NodeMap a(res_graph); jacint@714: // typename ResGW::Node b; jacint@714: // Num j=a[b]; jacint@714: // typename FilterResGW::NodeMap a1(filter_res_graph); jacint@714: // typename FilterResGW::Node b1; jacint@714: // Num j1=a1[b1]; jacint@714: // typename ErasingResGW::NodeMap a2(erasing_res_graph); jacint@714: // typename ErasingResGW::Node b2; jacint@714: // Num j2=a2[b2]; jacint@714: Num augment_value=free1[n]; jacint@714: while (erasing_res_graph.valid(pred[n])) { jacint@714: typename ErasingResGW::OutEdgeIt e=pred[n]; jacint@714: res_graph.augment(e, augment_value); jacint@714: n=erasing_res_graph.tail(e); jacint@714: if (res_graph.resCap(e)==0) jacint@714: erasing_res_graph.erase(e); jacint@714: } jacint@714: } jacint@714: jacint@714: } //while (__augment) jacint@714: jacint@714: status=AFTER_AUGMENTING; jacint@714: return _augment; jacint@714: } jacint@714: jacint@714: jacint@714: } //namespace hugo jacint@714: jacint@714: #endif //HUGO_MAX_FLOW_H jacint@714: jacint@714: jacint@714: jacint@714: