jacint@109: // -*- C++ -*- 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@109: stack 'active' on the active nodes on level i implemented by hand 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@109: Parameters H0 and H1 are initialized to 20 and 10. jacint@109: jacint@211: Constructors: jacint@109: jacint@211: Preflow(Graph, Node, Node, CapMap, FlowMap) jacint@109: jacint@109: Members: jacint@109: jacint@211: void run() jacint@109: jacint@211: T 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@109: minimum min cut. M should be a map of bools initialized to false. 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@109: jacint@109: namespace hugo { jacint@109: jacint@109: template , jacint@109: typename CapMap=typename Graph::EdgeMap > jacint@211: class Preflow { jacint@109: jacint@211: typedef typename Graph::Node Node; jacint@211: typedef typename Graph::Edge Edge; jacint@109: typedef typename Graph::NodeIt NodeIt; jacint@109: typedef typename Graph::OutEdgeIt OutEdgeIt; jacint@109: typedef typename Graph::InEdgeIt InEdgeIt; jacint@109: jacint@211: const Graph& G; jacint@211: Node s; jacint@211: Node t; jacint@211: FlowMap& flow; jacint@211: const CapMap& capacity; jacint@109: T value; jacint@109: jacint@109: public: marci@278: Preflow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity, marci@278: FlowMap& _flow ) : jacint@211: G(_G), s(_s), t(_t), flow(_flow), capacity(_capacity) {} jacint@211: jacint@211: jacint@211: void run() { jacint@109: jacint@109: bool phase=0; //phase 0 is the 1st phase, phase 1 is the 2nd jacint@109: int n=G.nodeNum(); jacint@109: int heur0=(int)(H0*n); //time while running 'bound decrease' jacint@109: int heur1=(int)(H1*n); //time while running 'highest label' jacint@109: int heur=heur1; //starting time interval (#of relabels) jacint@109: bool what_heur=1; jacint@109: /* jacint@109: what_heur is 0 in case 'bound decrease' jacint@109: and 1 in case 'highest label' jacint@109: */ jacint@109: bool end=false; jacint@109: /* jacint@109: Needed for 'bound decrease', 'true' jacint@109: means no active nodes are above bound b. jacint@109: */ jacint@109: int relabel=0; jacint@109: int k=n-2; //bound on the highest level under n containing a node jacint@109: int b=k; //bound on the highest level under n of an active node jacint@109: jacint@109: typename Graph::NodeMap level(G,n); jacint@109: typename Graph::NodeMap excess(G); jacint@109: jacint@211: std::vector active(n,INVALID); jacint@211: typename Graph::NodeMap next(G,INVALID); jacint@109: //Stack of the active nodes in level i < n. jacint@109: //We use it in both phases. jacint@109: jacint@211: typename Graph::NodeMap left(G,INVALID); jacint@211: typename Graph::NodeMap right(G,INVALID); jacint@211: std::vector level_list(n,INVALID); jacint@109: /* jacint@109: List of the nodes in level i bfs_queue; jacint@109: bfs_queue.push(t); jacint@109: jacint@109: while (!bfs_queue.empty()) { jacint@109: jacint@211: Node v=bfs_queue.front(); jacint@109: bfs_queue.pop(); jacint@211: int l=level[v]+1; jacint@109: jacint@211: InEdgeIt e; jacint@211: for(G.first(e,v); G.valid(e); G.next(e)) { jacint@211: Node w=G.tail(e); jacint@211: if ( level[w] == n && w != s ) { jacint@109: bfs_queue.push(w); jacint@211: Node first=level_list[l]; jacint@211: if ( G.valid(first) ) left.set(first,w); jacint@109: right.set(w,first); jacint@109: level_list[l]=w; jacint@109: level.set(w, l); jacint@109: } jacint@109: } jacint@109: } jacint@109: jacint@109: level.set(s,n); jacint@109: jacint@109: jacint@109: /* Starting flow. It is everywhere 0 at the moment. */ jacint@211: OutEdgeIt e; jacint@211: for(G.first(e,s); G.valid(e); G.next(e)) jacint@109: { jacint@211: T c=capacity[e]; jacint@109: if ( c == 0 ) continue; jacint@211: Node w=G.head(e); jacint@211: if ( level[w] < n ) { jacint@211: if ( excess[w] == 0 && w!=t ) { jacint@211: next.set(w,active[level[w]]); jacint@211: active[level[w]]=w; jacint@109: } jacint@109: flow.set(e, c); jacint@211: excess.set(w, excess[w]+c); jacint@109: } jacint@109: } jacint@109: jacint@109: /* jacint@109: End of preprocessing jacint@109: */ jacint@109: jacint@109: jacint@109: jacint@109: /* jacint@109: Push/relabel on the highest level active nodes. jacint@109: */ jacint@109: while ( true ) { jacint@109: jacint@109: if ( b == 0 ) { jacint@109: if ( phase ) break; jacint@109: jacint@109: if ( !what_heur && !end && k > 0 ) { jacint@109: b=k; jacint@109: end=true; jacint@109: } else { jacint@109: phase=1; jacint@109: level.set(s,0); jacint@211: std::queue bfs_queue; jacint@109: bfs_queue.push(s); jacint@109: jacint@109: while (!bfs_queue.empty()) { jacint@109: jacint@211: Node v=bfs_queue.front(); jacint@109: bfs_queue.pop(); jacint@211: int l=level[v]+1; jacint@109: jacint@211: InEdgeIt e; jacint@211: for(G.first(e,v); G.valid(e); G.next(e)) { jacint@211: if ( capacity[e] == flow[e] ) continue; jacint@211: Node u=G.tail(e); jacint@211: if ( level[u] >= n ) { jacint@109: bfs_queue.push(u); jacint@109: level.set(u, l); jacint@211: if ( excess[u] > 0 ) { jacint@109: next.set(u,active[l]); jacint@109: active[l]=u; jacint@109: } jacint@109: } jacint@109: } jacint@109: jacint@211: OutEdgeIt f; jacint@211: for(G.first(f,v); G.valid(f); G.next(f)) { jacint@211: if ( 0 == flow[f] ) continue; jacint@211: Node u=G.head(f); jacint@211: if ( level[u] >= n ) { jacint@109: bfs_queue.push(u); jacint@109: level.set(u, l); jacint@211: if ( excess[u] > 0 ) { jacint@109: next.set(u,active[l]); jacint@109: active[l]=u; jacint@109: } jacint@109: } jacint@109: } jacint@109: } jacint@109: b=n-2; jacint@109: } jacint@109: jacint@109: } jacint@109: jacint@109: jacint@211: if ( !G.valid(active[b]) ) --b; jacint@109: else { jacint@109: end=false; jacint@109: jacint@211: Node w=active[b]; jacint@211: active[b]=next[w]; jacint@211: int lev=level[w]; jacint@211: T exc=excess[w]; jacint@109: int newlevel=n; //bound on the next level of w jacint@109: jacint@211: OutEdgeIt e; jacint@211: for(G.first(e,w); G.valid(e); G.next(e)) { jacint@109: jacint@211: if ( flow[e] == capacity[e] ) continue; jacint@211: Node v=G.head(e); jacint@109: //e=wv jacint@109: jacint@211: if( lev > level[v] ) { jacint@109: /*Push is allowed now*/ jacint@109: jacint@211: if ( excess[v]==0 && v!=t && v!=s ) { jacint@211: int lev_v=level[v]; jacint@109: next.set(v,active[lev_v]); jacint@109: active[lev_v]=v; jacint@109: } jacint@109: jacint@211: T cap=capacity[e]; jacint@211: T flo=flow[e]; jacint@109: T remcap=cap-flo; jacint@109: jacint@109: if ( remcap >= exc ) { jacint@109: /*A nonsaturating push.*/ jacint@109: jacint@109: flow.set(e, flo+exc); jacint@211: excess.set(v, excess[v]+exc); jacint@109: exc=0; jacint@109: break; jacint@109: jacint@109: } else { jacint@109: /*A saturating push.*/ jacint@109: jacint@109: flow.set(e, cap); jacint@211: excess.set(v, excess[v]+remcap); jacint@109: exc-=remcap; jacint@109: } jacint@211: } else if ( newlevel > level[v] ){ jacint@211: newlevel = level[v]; jacint@109: } jacint@109: jacint@109: } //for out edges wv jacint@109: jacint@109: jacint@109: if ( exc > 0 ) { jacint@211: InEdgeIt e; jacint@211: for(G.first(e,w); G.valid(e); G.next(e)) { jacint@109: jacint@211: if( flow[e] == 0 ) continue; jacint@211: Node v=G.tail(e); jacint@109: //e=vw jacint@109: jacint@211: if( lev > level[v] ) { jacint@109: /*Push is allowed now*/ jacint@109: jacint@211: if ( excess[v]==0 && v!=t && v!=s ) { jacint@211: int lev_v=level[v]; jacint@109: next.set(v,active[lev_v]); jacint@109: active[lev_v]=v; jacint@109: } jacint@109: jacint@211: T flo=flow[e]; jacint@109: jacint@109: if ( flo >= exc ) { jacint@109: /*A nonsaturating push.*/ jacint@109: jacint@109: flow.set(e, flo-exc); jacint@211: excess.set(v, excess[v]+exc); jacint@109: exc=0; jacint@109: break; jacint@109: } else { jacint@109: /*A saturating push.*/ jacint@109: jacint@211: excess.set(v, excess[v]+flo); jacint@109: exc-=flo; jacint@109: flow.set(e,0); jacint@109: } jacint@211: } else if ( newlevel > level[v] ) { jacint@211: newlevel = level[v]; jacint@109: } jacint@109: } //for in edges vw jacint@109: jacint@109: } // if w still has excess after the out edge for cycle jacint@109: jacint@109: excess.set(w, exc); jacint@109: jacint@109: /* jacint@109: Relabel jacint@109: */ jacint@109: jacint@109: jacint@109: if ( exc > 0 ) { jacint@109: //now 'lev' is the old level of w jacint@109: jacint@109: if ( phase ) { jacint@109: level.set(w,++newlevel); jacint@109: next.set(w,active[newlevel]); jacint@109: active[newlevel]=w; jacint@109: b=newlevel; jacint@109: } else { jacint@109: //unlacing starts jacint@211: Node right_n=right[w]; jacint@211: Node left_n=left[w]; jacint@109: jacint@211: if ( G.valid(right_n) ) { jacint@211: if ( G.valid(left_n) ) { jacint@109: right.set(left_n, right_n); jacint@109: left.set(right_n, left_n); jacint@109: } else { jacint@109: level_list[lev]=right_n; jacint@211: left.set(right_n, INVALID); jacint@109: } jacint@109: } else { jacint@211: if ( G.valid(left_n) ) { jacint@211: right.set(left_n, INVALID); jacint@109: } else { jacint@211: level_list[lev]=INVALID; jacint@109: } jacint@109: } jacint@109: //unlacing ends jacint@109: jacint@109: //gapping starts jacint@211: if ( !G.valid(level_list[lev]) ) { jacint@109: jacint@109: for (int i=lev; i!=k ; ) { jacint@211: Node v=level_list[++i]; jacint@211: while ( G.valid(v) ) { jacint@109: level.set(v,n); jacint@211: v=right[v]; jacint@109: } jacint@211: level_list[i]=INVALID; jacint@211: if ( !what_heur ) active[i]=INVALID; jacint@109: } jacint@109: jacint@109: level.set(w,n); jacint@109: b=lev-1; jacint@109: k=b; jacint@109: //gapping ends jacint@109: } else { jacint@109: jacint@109: if ( newlevel == n ) level.set(w,n); jacint@109: else { jacint@109: level.set(w,++newlevel); jacint@109: next.set(w,active[newlevel]); jacint@109: active[newlevel]=w; jacint@109: if ( what_heur ) b=newlevel; jacint@109: if ( k < newlevel ) ++k; jacint@211: Node first=level_list[newlevel]; jacint@211: if ( G.valid(first) ) left.set(first,w); jacint@109: right.set(w,first); jacint@211: left.set(w,INVALID); jacint@109: level_list[newlevel]=w; jacint@109: } jacint@109: } jacint@109: jacint@109: jacint@109: ++relabel; jacint@109: if ( relabel >= heur ) { jacint@109: relabel=0; jacint@109: if ( what_heur ) { jacint@109: what_heur=0; jacint@109: heur=heur0; jacint@109: end=false; jacint@109: } else { jacint@109: what_heur=1; jacint@109: heur=heur1; jacint@109: b=k; jacint@109: } jacint@109: } jacint@109: } //phase 0 jacint@109: jacint@109: jacint@109: } // if ( exc > 0 ) jacint@109: jacint@109: jacint@109: } // if stack[b] is nonempty jacint@109: jacint@109: } // while(true) jacint@109: jacint@109: jacint@211: value = excess[t]; jacint@109: /*Max flow value.*/ jacint@109: jacint@109: } //void run() jacint@109: jacint@109: jacint@109: jacint@109: jacint@109: jacint@109: /* jacint@109: Returns the maximum value of a flow. jacint@109: */ jacint@109: jacint@211: T flowValue() { jacint@109: return value; jacint@109: } jacint@109: jacint@109: jacint@109: FlowMap Flow() { jacint@109: return flow; jacint@109: } jacint@109: jacint@109: jacint@109: jacint@109: void Flow(FlowMap& _flow ) { jacint@211: NodeIt v; jacint@211: for(G.first(v) ; G.valid(v); G.next(v)) jacint@211: _flow.set(v,flow[v]); 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: 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; jacint@211: for(G.first(e,w) ; G.valid(e); G.next(e)) { jacint@211: Node v=G.head(e); jacint@211: 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; jacint@211: for(G.first(f,w) ; G.valid(f); G.next(f)) { jacint@211: Node v=G.tail(f); jacint@211: 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@109: jacint@211: std::queue queue; jacint@109: jacint@109: M.set(t,true); 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; jacint@211: for(G.first(e,w) ; G.valid(e); G.next(e)) { jacint@211: Node v=G.tail(e); jacint@211: if (!M[v] && flow[e] < capacity[e] ) { jacint@109: queue.push(v); jacint@109: M.set(v, true); jacint@109: } jacint@109: } jacint@211: jacint@211: OutEdgeIt f; jacint@211: for(G.first(f,w) ; G.valid(f); G.next(f)) { jacint@211: Node v=G.head(f); jacint@211: 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@211: NodeIt v; jacint@211: for(G.first(v) ; G.valid(v); G.next(v)) { jacint@211: M.set(v, !M[v]); 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: jacint@109: jacint@109: }; jacint@109: marci@174: } //namespace hugo jacint@109: marci@174: #endif //PREFLOW_H jacint@109: jacint@109: marci@174: marci@174: