jacint@109: // -*- C++ -*- jacint@372: jacint@109: /* jacint@109: Heuristics: jacint@109: 2 phase jacint@109: gap jacint@109: list 'level_list' on the nodes on level i implemented by hand jacint@451: stack 'active' on the active nodes on level i jacint@109: runs heuristic 'highest label' for H1*n relabels jacint@113: runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label' jacint@109: jacint@451: Parameters H0 and H1 are initialized to 20 and 1. jacint@109: jacint@211: Constructors: jacint@109: jacint@372: Preflow(Graph, Node, Node, CapMap, FlowMap, bool) : bool must be false if jacint@372: FlowMap is not constant zero, and should be true if it is jacint@109: jacint@109: Members: jacint@109: jacint@211: void run() jacint@109: marci@468: Num flowValue() : returns the value of a maximum flow jacint@109: jacint@109: void minMinCut(CutMap& M) : sets M to the characteristic vector of the jacint@451: minimum min cut. M should be a map of bools initialized to false. ??Is it OK? jacint@109: jacint@109: void maxMinCut(CutMap& M) : sets M to the characteristic vector of the jacint@109: maximum min cut. M should be a map of bools initialized to false. jacint@109: jacint@109: void minCut(CutMap& M) : sets M to the characteristic vector of jacint@109: a min cut. M should be a map of bools initialized to false. jacint@109: jacint@109: */ jacint@109: jacint@211: #ifndef HUGO_PREFLOW_H jacint@211: #define HUGO_PREFLOW_H jacint@109: jacint@109: #define H0 20 jacint@109: #define H1 1 jacint@109: jacint@109: #include jacint@109: #include jacint@451: #include jacint@109: marci@471: #include marci@472: #include marci@472: #include marci@472: #include marci@472: #include marci@472: marci@471: jacint@109: namespace hugo { jacint@109: marci@468: template , marci@468: typename FlowMap=typename Graph::template EdgeMap > jacint@211: class Preflow { jacint@109: jacint@211: typedef typename Graph::Node Node; jacint@109: typedef typename Graph::NodeIt NodeIt; jacint@109: typedef typename Graph::OutEdgeIt OutEdgeIt; jacint@109: typedef typename Graph::InEdgeIt InEdgeIt; jacint@451: jacint@451: typedef typename std::vector > VecStack; jacint@451: typedef typename Graph::template NodeMap NNMap; jacint@451: typedef typename std::vector VecNode; jacint@451: marci@466: const Graph* g; jacint@211: Node s; jacint@211: Node t; marci@465: const CapMap* capacity; jacint@451: FlowMap* flow; jacint@451: int n; //the number of nodes of G marci@471: typedef ResGraphWrapper ResGW; marci@471: typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt; marci@471: typedef typename ResGW::Edge ResGWEdge; marci@471: //typedef typename ResGW::template NodeMap ReachedMap; marci@471: typedef typename Graph::template NodeMap ReachedMap; marci@471: ReachedMap level; marci@471: //level works as a bool map in augmenting path algorithms marci@471: //and is used by bfs for storing reached information. marci@471: //In preflow, it shows levels of nodes. marci@471: //typename Graph::template NodeMap level; marci@468: typename Graph::template NodeMap excess; jacint@451: jacint@109: public: jacint@451: jacint@451: enum flowEnum{ jacint@451: ZERO_FLOW=0, jacint@451: GEN_FLOW=1, jacint@451: PREFLOW=2 jacint@451: }; jacint@451: marci@465: Preflow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity, jacint@451: FlowMap& _flow) : marci@466: g(&_G), s(_s), t(_t), capacity(&_capacity), jacint@451: flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0) {} jacint@451: jacint@451: void run() { jacint@451: preflow( ZERO_FLOW ); jacint@451: } jacint@372: jacint@451: void preflow( flowEnum fe ) { jacint@451: preflowPhase0(fe); jacint@451: preflowPhase1(); jacint@451: } jacint@451: marci@471: void preflowPhase0( flowEnum fe ); jacint@451: marci@471: void preflowPhase1(); jacint@451: marci@471: bool augmentOnShortestPath(); jacint@109: marci@471: template bool augmentOnBlockingFlow(); jacint@451: marci@471: bool augmentOnBlockingFlow2(); jacint@109: marci@472: /// Returns the actual flow value. marci@472: /// More precisely, it returns the negative excess of s, thus marci@472: /// this works also for preflows. marci@472: Num flowValue() { marci@472: Num a=0; marci@472: FOR_EACH_INC_LOC(OutEdgeIt, e, *g, s) a+=(*flow)[e]; marci@472: FOR_EACH_INC_LOC(InEdgeIt, e, *g, s) a-=(*flow)[e]; marci@472: return a; jacint@451: } jacint@109: jacint@451: //should be used only between preflowPhase0 and preflowPhase1 jacint@451: template jacint@451: void actMinCut(_CutMap& M) { jacint@211: NodeIt v; marci@466: for(g->first(v); g->valid(v); g->next(v)) marci@466: if ( level[v] < n ) { marci@466: M.set(v,false); marci@466: } else { marci@466: M.set(v,true); marci@466: } jacint@211: } jacint@109: jacint@109: jacint@109: jacint@109: /* jacint@109: Returns the minimum min cut, by a bfs from s in the residual graph. jacint@109: */ jacint@109: template jacint@109: void minMinCut(_CutMap& M) { jacint@109: jacint@211: std::queue queue; jacint@109: jacint@109: M.set(s,true); jacint@109: queue.push(s); jacint@109: jacint@109: while (!queue.empty()) { jacint@211: Node w=queue.front(); jacint@109: queue.pop(); jacint@109: jacint@211: OutEdgeIt e; marci@466: for(g->first(e,w) ; g->valid(e); g->next(e)) { marci@466: Node v=g->head(e); jacint@451: if (!M[v] && (*flow)[e] < (*capacity)[e] ) { jacint@109: queue.push(v); jacint@109: M.set(v, true); jacint@109: } jacint@109: } jacint@109: jacint@211: InEdgeIt f; marci@466: for(g->first(f,w) ; g->valid(f); g->next(f)) { marci@466: Node v=g->tail(f); jacint@451: if (!M[v] && (*flow)[f] > 0 ) { jacint@109: queue.push(v); jacint@109: M.set(v, true); jacint@109: } jacint@109: } jacint@109: } jacint@109: } jacint@109: jacint@109: jacint@109: jacint@109: /* jacint@109: Returns the maximum min cut, by a reverse bfs jacint@109: from t in the residual graph. jacint@109: */ jacint@109: jacint@109: template jacint@109: void maxMinCut(_CutMap& M) { jacint@451: jacint@451: NodeIt v; marci@466: for(g->first(v) ; g->valid(v); g->next(v)) { jacint@451: M.set(v, true); jacint@451: } jacint@451: jacint@211: std::queue queue; jacint@109: jacint@451: M.set(t,false); jacint@109: queue.push(t); jacint@109: jacint@109: while (!queue.empty()) { jacint@211: Node w=queue.front(); jacint@109: queue.pop(); jacint@109: jacint@211: jacint@211: InEdgeIt e; marci@466: for(g->first(e,w) ; g->valid(e); g->next(e)) { marci@466: Node v=g->tail(e); jacint@451: if (M[v] && (*flow)[e] < (*capacity)[e] ) { jacint@109: queue.push(v); jacint@451: M.set(v, false); jacint@109: } jacint@109: } jacint@211: jacint@211: OutEdgeIt f; marci@466: for(g->first(f,w) ; g->valid(f); g->next(f)) { marci@466: Node v=g->head(f); jacint@451: if (M[v] && (*flow)[f] > 0 ) { jacint@109: queue.push(v); jacint@451: M.set(v, false); jacint@109: } jacint@109: } jacint@109: } jacint@109: } jacint@109: jacint@109: jacint@109: template jacint@109: void minCut(CutMap& M) { jacint@109: minMinCut(M); jacint@109: } jacint@109: marci@468: void resetTarget(Node _t) {t=_t;} marci@468: void resetSource(Node _s) {s=_s;} jacint@372: marci@468: void resetCap(const CapMap& _cap) { jacint@451: capacity=&_cap; jacint@451: } jacint@451: marci@468: void resetFlow(FlowMap& _flow) { jacint@451: flow=&_flow; jacint@372: } jacint@372: jacint@372: jacint@451: private: jacint@451: marci@469: int push(Node w, VecStack& active) { jacint@451: jacint@451: int lev=level[w]; marci@468: Num exc=excess[w]; jacint@451: int newlevel=n; //bound on the next level of w jacint@451: jacint@451: OutEdgeIt e; marci@466: for(g->first(e,w); g->valid(e); g->next(e)) { jacint@451: jacint@470: if ( (*flow)[e] >= (*capacity)[e] ) continue; marci@466: Node v=g->head(e); jacint@451: jacint@451: if( lev > level[v] ) { //Push is allowed now jacint@451: jacint@470: if ( excess[v]<=0 && v!=t && v!=s ) { jacint@451: int lev_v=level[v]; jacint@451: active[lev_v].push(v); jacint@451: } jacint@451: marci@468: Num cap=(*capacity)[e]; marci@468: Num flo=(*flow)[e]; marci@468: Num remcap=cap-flo; jacint@451: jacint@451: if ( remcap >= exc ) { //A nonsaturating push. jacint@451: jacint@451: flow->set(e, flo+exc); jacint@451: excess.set(v, excess[v]+exc); jacint@451: exc=0; jacint@451: break; jacint@451: jacint@451: } else { //A saturating push. jacint@451: flow->set(e, cap); jacint@451: excess.set(v, excess[v]+remcap); jacint@451: exc-=remcap; jacint@451: } jacint@451: } else if ( newlevel > level[v] ) newlevel = level[v]; jacint@451: } //for out edges wv jacint@451: jacint@451: if ( exc > 0 ) { jacint@451: InEdgeIt e; marci@466: for(g->first(e,w); g->valid(e); g->next(e)) { jacint@451: jacint@470: if( (*flow)[e] <= 0 ) continue; marci@466: Node v=g->tail(e); jacint@451: jacint@451: if( lev > level[v] ) { //Push is allowed now jacint@451: jacint@470: if ( excess[v]<=0 && v!=t && v!=s ) { jacint@451: int lev_v=level[v]; jacint@451: active[lev_v].push(v); jacint@451: } jacint@451: marci@468: Num flo=(*flow)[e]; jacint@451: jacint@451: if ( flo >= exc ) { //A nonsaturating push. jacint@451: jacint@451: flow->set(e, flo-exc); jacint@451: excess.set(v, excess[v]+exc); jacint@451: exc=0; jacint@451: break; jacint@451: } else { //A saturating push. jacint@451: jacint@451: excess.set(v, excess[v]+flo); jacint@451: exc-=flo; jacint@451: flow->set(e,0); jacint@451: } jacint@451: } else if ( newlevel > level[v] ) newlevel = level[v]; jacint@451: } //for in edges vw jacint@451: jacint@451: } // if w still has excess after the out edge for cycle jacint@451: jacint@451: excess.set(w, exc); jacint@451: jacint@451: return newlevel; jacint@451: } jacint@451: jacint@451: jacint@451: void preflowPreproc ( flowEnum fe, VecStack& active, jacint@451: VecNode& level_list, NNMap& left, NNMap& right ) { jacint@451: jacint@451: std::queue bfs_queue; jacint@451: jacint@451: switch ( fe ) { jacint@451: case ZERO_FLOW: jacint@451: { jacint@451: //Reverse_bfs from t, to find the starting level. jacint@451: level.set(t,0); jacint@451: bfs_queue.push(t); jacint@451: jacint@451: while (!bfs_queue.empty()) { jacint@451: jacint@451: Node v=bfs_queue.front(); jacint@451: bfs_queue.pop(); jacint@451: int l=level[v]+1; jacint@451: jacint@451: InEdgeIt e; marci@466: for(g->first(e,v); g->valid(e); g->next(e)) { marci@466: Node w=g->tail(e); jacint@451: if ( level[w] == n && w != s ) { jacint@451: bfs_queue.push(w); jacint@451: Node first=level_list[l]; marci@466: if ( g->valid(first) ) left.set(first,w); jacint@451: right.set(w,first); jacint@451: level_list[l]=w; jacint@451: level.set(w, l); jacint@451: } jacint@451: } jacint@451: } jacint@451: jacint@451: //the starting flow jacint@451: OutEdgeIt e; marci@466: for(g->first(e,s); g->valid(e); g->next(e)) jacint@451: { marci@468: Num c=(*capacity)[e]; jacint@470: if ( c <= 0 ) continue; marci@466: Node w=g->head(e); jacint@451: if ( level[w] < n ) { jacint@470: if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w); jacint@451: flow->set(e, c); jacint@451: excess.set(w, excess[w]+c); jacint@451: } jacint@451: } jacint@451: break; jacint@451: } jacint@451: jacint@451: case GEN_FLOW: jacint@451: case PREFLOW: jacint@451: { jacint@451: //Reverse_bfs from t in the residual graph, jacint@451: //to find the starting level. jacint@451: level.set(t,0); jacint@451: bfs_queue.push(t); jacint@451: jacint@451: while (!bfs_queue.empty()) { jacint@451: jacint@451: Node v=bfs_queue.front(); jacint@451: bfs_queue.pop(); jacint@451: int l=level[v]+1; jacint@451: jacint@451: InEdgeIt e; marci@466: for(g->first(e,v); g->valid(e); g->next(e)) { jacint@470: if ( (*capacity)[e] <= (*flow)[e] ) continue; marci@466: Node w=g->tail(e); jacint@451: if ( level[w] == n && w != s ) { jacint@451: bfs_queue.push(w); jacint@451: Node first=level_list[l]; marci@466: if ( g->valid(first) ) left.set(first,w); jacint@451: right.set(w,first); jacint@451: level_list[l]=w; jacint@451: level.set(w, l); jacint@451: } jacint@451: } jacint@451: jacint@451: OutEdgeIt f; marci@466: for(g->first(f,v); g->valid(f); g->next(f)) { jacint@470: if ( 0 >= (*flow)[f] ) continue; marci@466: Node w=g->head(f); jacint@451: if ( level[w] == n && w != s ) { jacint@451: bfs_queue.push(w); jacint@451: Node first=level_list[l]; marci@466: if ( g->valid(first) ) left.set(first,w); jacint@451: right.set(w,first); jacint@451: level_list[l]=w; jacint@451: level.set(w, l); jacint@451: } jacint@451: } jacint@451: } jacint@451: jacint@451: jacint@451: //the starting flow jacint@451: OutEdgeIt e; marci@466: for(g->first(e,s); g->valid(e); g->next(e)) jacint@451: { marci@468: Num rem=(*capacity)[e]-(*flow)[e]; jacint@470: if ( rem <= 0 ) continue; marci@466: Node w=g->head(e); jacint@451: if ( level[w] < n ) { jacint@470: if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w); jacint@451: flow->set(e, (*capacity)[e]); jacint@451: excess.set(w, excess[w]+rem); jacint@451: } jacint@451: } jacint@451: jacint@451: InEdgeIt f; marci@466: for(g->first(f,s); g->valid(f); g->next(f)) jacint@451: { jacint@470: if ( (*flow)[f] <= 0 ) continue; marci@466: Node w=g->tail(f); jacint@451: if ( level[w] < n ) { jacint@470: if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w); jacint@451: excess.set(w, excess[w]+(*flow)[f]); jacint@451: flow->set(f, 0); jacint@451: } jacint@451: } jacint@451: break; jacint@451: } //case PREFLOW jacint@451: } jacint@451: } //preflowPreproc jacint@451: jacint@451: jacint@451: marci@469: void relabel(Node w, int newlevel, VecStack& active, marci@469: VecNode& level_list, NNMap& left, marci@472: NNMap& right, int& b, int& k, bool what_heur ) marci@472: { jacint@451: marci@468: Num lev=level[w]; jacint@451: jacint@451: Node right_n=right[w]; jacint@451: Node left_n=left[w]; jacint@451: jacint@451: //unlacing starts marci@466: if ( g->valid(right_n) ) { marci@466: if ( g->valid(left_n) ) { jacint@451: right.set(left_n, right_n); jacint@451: left.set(right_n, left_n); jacint@451: } else { jacint@451: level_list[lev]=right_n; jacint@451: left.set(right_n, INVALID); jacint@451: } jacint@451: } else { marci@466: if ( g->valid(left_n) ) { jacint@451: right.set(left_n, INVALID); jacint@451: } else { jacint@451: level_list[lev]=INVALID; jacint@451: } jacint@451: } jacint@451: //unlacing ends jacint@451: marci@466: if ( !g->valid(level_list[lev]) ) { jacint@451: jacint@451: //gapping starts jacint@451: for (int i=lev; i!=k ; ) { jacint@451: Node v=level_list[++i]; marci@466: while ( g->valid(v) ) { jacint@451: level.set(v,n); jacint@451: v=right[v]; jacint@451: } jacint@451: level_list[i]=INVALID; jacint@451: if ( !what_heur ) { jacint@451: while ( !active[i].empty() ) { jacint@451: active[i].pop(); //FIXME: ezt szebben kene jacint@451: } jacint@451: } jacint@451: } jacint@451: jacint@451: level.set(w,n); jacint@451: b=lev-1; jacint@451: k=b; jacint@451: //gapping ends jacint@451: jacint@451: } else { jacint@451: jacint@451: if ( newlevel == n ) level.set(w,n); jacint@451: else { jacint@451: level.set(w,++newlevel); jacint@451: active[newlevel].push(w); jacint@451: if ( what_heur ) b=newlevel; jacint@451: if ( k < newlevel ) ++k; //now k=newlevel jacint@451: Node first=level_list[newlevel]; marci@466: if ( g->valid(first) ) left.set(first,w); jacint@451: right.set(w,first); jacint@451: left.set(w,INVALID); jacint@451: level_list[newlevel]=w; jacint@451: } jacint@451: } jacint@451: jacint@451: } //relabel marci@472: marci@472: marci@472: template marci@472: class DistanceMap { marci@472: protected: marci@472: const MapGraphWrapper* g; marci@472: typename MapGraphWrapper::template NodeMap dist; marci@472: public: marci@472: DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { } marci@472: void set(const typename MapGraphWrapper::Node& n, int a) { marci@472: dist.set(n, a); marci@472: } marci@472: int operator[](const typename MapGraphWrapper::Node& n) marci@472: { return dist[n]; } marci@472: // int get(const typename MapGraphWrapper::Node& n) const { marci@472: // return dist[n]; } marci@472: // bool get(const typename MapGraphWrapper::Edge& e) const { marci@472: // return (dist.get(g->tail(e))head(e))); } marci@472: bool operator[](const typename MapGraphWrapper::Edge& e) const { marci@472: return (dist[g->tail(e)]head(e)]); marci@472: } marci@472: }; jacint@451: marci@471: }; jacint@109: marci@471: marci@471: template marci@471: void Preflow::preflowPhase0( flowEnum fe ) marci@471: { marci@471: marci@471: int heur0=(int)(H0*n); //time while running 'bound decrease' marci@471: int heur1=(int)(H1*n); //time while running 'highest label' marci@471: int heur=heur1; //starting time interval (#of relabels) marci@471: int numrelabel=0; marci@471: marci@471: bool what_heur=1; marci@471: //It is 0 in case 'bound decrease' and 1 in case 'highest label' marci@471: marci@471: bool end=false; marci@471: //Needed for 'bound decrease', true means no active nodes are above bound b. marci@471: marci@471: int k=n-2; //bound on the highest level under n containing a node marci@471: int b=k; //bound on the highest level under n of an active node marci@471: marci@471: VecStack active(n); marci@471: marci@471: NNMap left(*g, INVALID); marci@471: NNMap right(*g, INVALID); marci@471: VecNode level_list(n,INVALID); marci@471: //List of the nodes in level ifirst(v); g->valid(v); g->next(v)) level.set(v,n); marci@471: //setting each node to level n marci@471: marci@471: switch ( fe ) { marci@471: case PREFLOW: marci@471: { marci@471: //counting the excess marci@471: NodeIt v; marci@471: for(g->first(v); g->valid(v); g->next(v)) { marci@471: Num exc=0; marci@471: marci@471: InEdgeIt e; marci@471: for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e]; marci@471: OutEdgeIt f; marci@471: for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f]; marci@471: marci@471: excess.set(v,exc); marci@471: marci@471: //putting the active nodes into the stack marci@471: int lev=level[v]; marci@471: if ( exc > 0 && lev < n && v != t ) active[lev].push(v); marci@471: } marci@471: break; marci@471: } marci@471: case GEN_FLOW: marci@471: { marci@471: //Counting the excess of t marci@471: Num exc=0; marci@471: marci@471: InEdgeIt e; marci@471: for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e]; marci@471: OutEdgeIt f; marci@471: for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f]; marci@471: marci@471: excess.set(t,exc); marci@471: marci@471: break; marci@471: } marci@471: default: marci@471: break; marci@471: } marci@471: marci@471: preflowPreproc( fe, active, level_list, left, right ); marci@471: //End of preprocessing marci@471: marci@471: marci@471: //Push/relabel on the highest level active nodes. marci@471: while ( true ) { marci@471: if ( b == 0 ) { marci@471: if ( !what_heur && !end && k > 0 ) { marci@471: b=k; marci@471: end=true; marci@471: } else break; marci@471: } marci@471: marci@471: if ( active[b].empty() ) --b; marci@471: else { marci@471: end=false; marci@471: Node w=active[b].top(); marci@471: active[b].pop(); marci@471: int newlevel=push(w,active); marci@471: if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list, marci@471: left, right, b, k, what_heur); marci@471: marci@471: ++numrelabel; marci@471: if ( numrelabel >= heur ) { marci@471: numrelabel=0; marci@471: if ( what_heur ) { marci@471: what_heur=0; marci@471: heur=heur0; marci@471: end=false; marci@471: } else { marci@471: what_heur=1; marci@471: heur=heur1; marci@471: b=k; marci@471: } marci@471: } marci@471: } marci@471: } marci@471: } marci@471: marci@471: marci@471: marci@471: template marci@471: void Preflow::preflowPhase1() marci@471: { marci@471: marci@471: int k=n-2; //bound on the highest level under n containing a node marci@471: int b=k; //bound on the highest level under n of an active node marci@471: marci@471: VecStack active(n); marci@471: level.set(s,0); marci@471: std::queue bfs_queue; marci@471: bfs_queue.push(s); marci@471: marci@471: while (!bfs_queue.empty()) { marci@471: marci@471: Node v=bfs_queue.front(); marci@471: bfs_queue.pop(); marci@471: int l=level[v]+1; marci@471: marci@471: InEdgeIt e; marci@471: for(g->first(e,v); g->valid(e); g->next(e)) { marci@471: if ( (*capacity)[e] <= (*flow)[e] ) continue; marci@471: Node u=g->tail(e); marci@471: if ( level[u] >= n ) { marci@471: bfs_queue.push(u); marci@471: level.set(u, l); marci@471: if ( excess[u] > 0 ) active[l].push(u); marci@471: } marci@471: } marci@471: marci@471: OutEdgeIt f; marci@471: for(g->first(f,v); g->valid(f); g->next(f)) { marci@471: if ( 0 >= (*flow)[f] ) continue; marci@471: Node u=g->head(f); marci@471: if ( level[u] >= n ) { marci@471: bfs_queue.push(u); marci@471: level.set(u, l); marci@471: if ( excess[u] > 0 ) active[l].push(u); marci@471: } marci@471: } marci@471: } marci@471: b=n-2; marci@471: marci@471: while ( true ) { marci@471: marci@471: if ( b == 0 ) break; marci@471: marci@471: if ( active[b].empty() ) --b; marci@471: else { marci@471: Node w=active[b].top(); marci@471: active[b].pop(); marci@471: int newlevel=push(w,active); marci@471: marci@471: //relabel marci@471: if ( excess[w] > 0 ) { marci@471: level.set(w,++newlevel); marci@471: active[newlevel].push(w); marci@471: b=newlevel; marci@471: } marci@471: } // if stack[b] is nonempty marci@471: } // while(true) marci@471: } marci@471: marci@471: marci@471: marci@472: template marci@472: bool Preflow::augmentOnShortestPath() marci@472: { marci@472: ResGW res_graph(*g, *capacity, *flow); marci@472: bool _augment=false; marci@472: marci@472: //ReachedMap level(res_graph); marci@472: FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); marci@472: BfsIterator bfs(res_graph, level); marci@472: bfs.pushAndSetReached(s); marci@472: marci@472: typename ResGW::template NodeMap pred(res_graph); marci@472: pred.set(s, INVALID); marci@472: marci@472: typename ResGW::template NodeMap free(res_graph); marci@472: marci@472: //searching for augmenting path marci@472: while ( !bfs.finished() ) { marci@472: ResGWOutEdgeIt e=bfs; marci@472: if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) { marci@472: Node v=res_graph.tail(e); marci@472: Node w=res_graph.head(e); marci@472: pred.set(w, e); marci@472: if (res_graph.valid(pred[v])) { marci@472: free.set(w, std::min(free[v], res_graph.resCap(e))); marci@472: } else { marci@472: free.set(w, res_graph.resCap(e)); marci@472: } marci@472: if (res_graph.head(e)==t) { _augment=true; break; } marci@472: } marci@472: marci@472: ++bfs; marci@472: } //end of searching augmenting path marci@471: marci@472: if (_augment) { marci@472: Node n=t; marci@472: Num augment_value=free[t]; marci@472: while (res_graph.valid(pred[n])) { marci@472: ResGWEdge e=pred[n]; marci@472: res_graph.augment(e, augment_value); marci@472: n=res_graph.tail(e); marci@472: } marci@472: } marci@471: marci@472: return _augment; marci@472: } marci@471: marci@471: marci@471: marci@471: marci@471: marci@471: jacint@109: marci@472: marci@472: marci@472: template marci@472: template marci@472: bool Preflow::augmentOnBlockingFlow() marci@472: { marci@472: typedef MutableGraph MG; marci@472: bool _augment=false; marci@472: marci@472: ResGW res_graph(*g, *capacity, *flow); marci@472: marci@472: //bfs for distances on the residual graph marci@472: //ReachedMap level(res_graph); marci@472: FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); marci@472: BfsIterator bfs(res_graph, level); marci@472: bfs.pushAndSetReached(s); marci@472: typename ResGW::template NodeMap marci@472: dist(res_graph); //filled up with 0's marci@472: marci@472: //F will contain the physical copy of the residual graph marci@472: //with the set of edges which are on shortest paths marci@472: MG F; marci@472: typename ResGW::template NodeMap marci@472: res_graph_to_F(res_graph); marci@472: { marci@472: typename ResGW::NodeIt n; marci@472: for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) { marci@472: res_graph_to_F.set(n, F.addNode()); marci@472: } marci@472: } marci@472: marci@472: typename MG::Node sF=res_graph_to_F[s]; marci@472: typename MG::Node tF=res_graph_to_F[t]; marci@472: typename MG::template EdgeMap original_edge(F); marci@472: typename MG::template EdgeMap residual_capacity(F); marci@472: marci@472: while ( !bfs.finished() ) { marci@472: ResGWOutEdgeIt e=bfs; marci@472: if (res_graph.valid(e)) { marci@472: if (bfs.isBNodeNewlyReached()) { marci@472: dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1); marci@472: typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]); marci@472: original_edge.update(); marci@472: original_edge.set(f, e); marci@472: residual_capacity.update(); marci@472: residual_capacity.set(f, res_graph.resCap(e)); marci@472: } else { marci@472: if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) { marci@472: typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]); marci@472: original_edge.update(); marci@472: original_edge.set(f, e); marci@472: residual_capacity.update(); marci@472: residual_capacity.set(f, res_graph.resCap(e)); marci@472: } marci@472: } marci@472: } marci@472: ++bfs; marci@472: } //computing distances from s in the residual graph marci@472: marci@472: bool __augment=true; marci@472: marci@472: while (__augment) { marci@472: __augment=false; marci@472: //computing blocking flow with dfs marci@472: DfsIterator< MG, typename MG::template NodeMap > dfs(F); marci@472: typename MG::template NodeMap pred(F); marci@472: pred.set(sF, INVALID); marci@472: //invalid iterators for sources marci@472: marci@472: typename MG::template NodeMap free(F); marci@472: marci@472: dfs.pushAndSetReached(sF); marci@472: while (!dfs.finished()) { marci@472: ++dfs; marci@472: if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) { marci@472: if (dfs.isBNodeNewlyReached()) { marci@472: typename MG::Node v=F.aNode(dfs); marci@472: typename MG::Node w=F.bNode(dfs); marci@472: pred.set(w, dfs); marci@472: if (F.valid(pred[v])) { marci@472: free.set(w, std::min(free[v], residual_capacity[dfs])); marci@472: } else { marci@472: free.set(w, residual_capacity[dfs]); marci@472: } marci@472: if (w==tF) { marci@472: __augment=true; marci@472: _augment=true; marci@472: break; marci@472: } marci@472: marci@472: } else { marci@472: F.erase(/*typename MG::OutEdgeIt*/(dfs)); marci@472: } marci@472: } marci@472: } marci@472: marci@472: if (__augment) { marci@472: typename MG::Node n=tF; marci@472: Num augment_value=free[tF]; marci@472: while (F.valid(pred[n])) { marci@472: typename MG::Edge e=pred[n]; marci@472: res_graph.augment(original_edge[e], augment_value); marci@472: n=F.tail(e); marci@472: if (residual_capacity[e]==augment_value) marci@472: F.erase(e); marci@472: else marci@472: residual_capacity.set(e, residual_capacity[e]-augment_value); marci@472: } marci@472: } marci@472: marci@472: } marci@472: marci@472: return _augment; marci@472: } marci@472: marci@472: marci@472: marci@472: marci@472: marci@472: marci@472: template marci@472: bool Preflow::augmentOnBlockingFlow2() marci@472: { marci@472: bool _augment=false; marci@472: marci@472: ResGW res_graph(*g, *capacity, *flow); marci@472: marci@472: //ReachedMap level(res_graph); marci@472: FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); marci@472: BfsIterator bfs(res_graph, level); marci@472: marci@472: bfs.pushAndSetReached(s); marci@472: DistanceMap dist(res_graph); marci@472: while ( !bfs.finished() ) { marci@472: ResGWOutEdgeIt e=bfs; marci@472: if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) { marci@472: dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1); marci@472: } marci@472: ++bfs; marci@472: } //computing distances from s in the residual graph marci@472: marci@472: //Subgraph containing the edges on some shortest paths marci@472: ConstMap true_map(true); marci@472: typedef SubGraphWrapper, marci@472: DistanceMap > FilterResGW; marci@472: FilterResGW filter_res_graph(res_graph, true_map, dist); marci@472: marci@472: //Subgraph, which is able to delete edges which are already marci@472: //met by the dfs marci@472: typename FilterResGW::template NodeMap marci@472: first_out_edges(filter_res_graph); marci@472: typename FilterResGW::NodeIt v; marci@472: for(filter_res_graph.first(v); filter_res_graph.valid(v); marci@472: filter_res_graph.next(v)) marci@472: { marci@472: typename FilterResGW::OutEdgeIt e; marci@472: filter_res_graph.first(e, v); marci@472: first_out_edges.set(v, e); marci@472: } marci@472: typedef ErasingFirstGraphWrapper > ErasingResGW; marci@472: ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges); marci@472: marci@472: bool __augment=true; marci@472: marci@472: while (__augment) { marci@472: marci@472: __augment=false; marci@472: //computing blocking flow with dfs marci@472: DfsIterator< ErasingResGW, marci@472: typename ErasingResGW::template NodeMap > marci@472: dfs(erasing_res_graph); marci@472: typename ErasingResGW:: marci@472: template NodeMap marci@472: pred(erasing_res_graph); marci@472: pred.set(s, INVALID); marci@472: //invalid iterators for sources marci@472: marci@472: typename ErasingResGW::template NodeMap marci@472: free1(erasing_res_graph); marci@472: marci@472: dfs.pushAndSetReached( marci@472: typename ErasingResGW::Node( marci@472: typename FilterResGW::Node( marci@472: typename ResGW::Node(s) marci@472: ) marci@472: ) marci@472: ); marci@472: while (!dfs.finished()) { marci@472: ++dfs; marci@472: if (erasing_res_graph.valid( marci@472: typename ErasingResGW::OutEdgeIt(dfs))) marci@472: { marci@472: if (dfs.isBNodeNewlyReached()) { marci@472: marci@472: typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs); marci@472: typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs); marci@472: marci@472: pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs)); marci@472: if (erasing_res_graph.valid(pred[v])) { marci@472: free1.set(w, std::min(free1[v], res_graph.resCap( marci@472: typename ErasingResGW::OutEdgeIt(dfs)))); marci@472: } else { marci@472: free1.set(w, res_graph.resCap( marci@472: typename ErasingResGW::OutEdgeIt(dfs))); marci@472: } marci@472: marci@472: if (w==t) { marci@472: __augment=true; marci@472: _augment=true; marci@472: break; marci@472: } marci@472: } else { marci@472: erasing_res_graph.erase(dfs); marci@472: } marci@472: } marci@472: } marci@472: marci@472: if (__augment) { marci@472: typename ErasingResGW::Node n=typename FilterResGW::Node(typename ResGW::Node(t)); marci@472: // typename ResGW::NodeMap a(res_graph); marci@472: // typename ResGW::Node b; marci@472: // Num j=a[b]; marci@472: // typename FilterResGW::NodeMap a1(filter_res_graph); marci@472: // typename FilterResGW::Node b1; marci@472: // Num j1=a1[b1]; marci@472: // typename ErasingResGW::NodeMap a2(erasing_res_graph); marci@472: // typename ErasingResGW::Node b2; marci@472: // Num j2=a2[b2]; marci@472: Num augment_value=free1[n]; marci@472: while (erasing_res_graph.valid(pred[n])) { marci@472: typename ErasingResGW::OutEdgeIt e=pred[n]; marci@472: res_graph.augment(e, augment_value); marci@472: n=erasing_res_graph.tail(e); marci@472: if (res_graph.resCap(e)==0) marci@472: erasing_res_graph.erase(e); marci@472: } marci@472: } marci@472: marci@472: } //while (__augment) marci@472: marci@472: return _augment; marci@472: } marci@472: marci@472: marci@472: marci@472: marci@174: } //namespace hugo jacint@109: jacint@451: #endif //HUGO_PREFLOW_H jacint@109: jacint@109: marci@174: marci@174: