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