jacint@20: /* jacint@20: preflow_push_hl.hh jacint@20: by jacint. jacint@20: Runs the highest label variant of the preflow push algorithm with jacint@20: running time O(n^2\sqrt(m)). jacint@20: jacint@20: Member functions: jacint@20: jacint@20: void run() : runs the algorithm jacint@20: jacint@20: The following functions should be used after run() was already run. jacint@20: jacint@20: T maxflow() : returns the value of a maximum flow jacint@20: jacint@20: T flowonedge(edge_iterator e) : for a fixed maximum flow x it returns x(e) jacint@20: jacint@20: edge_property_vector allflow() : returns the fixed maximum flow x jacint@20: jacint@20: node_property_vector mincut() : returns a jacint@20: characteristic vector of a minimum cut. (An empty level jacint@20: in the algorithm gives a minimum cut.) jacint@20: */ jacint@20: jacint@20: #ifndef PREFLOW_PUSH_HL_HH jacint@20: #define PREFLOW_PUSH_HL_HH jacint@20: jacint@20: #include jacint@20: #include jacint@20: #include jacint@20: jacint@20: #include jacint@20: #include jacint@20: #include jacint@20: jacint@20: namespace marci { jacint@20: jacint@20: template jacint@20: class preflow_push_hl { jacint@20: jacint@20: typedef typename graph_traits::node_iterator node_iterator; jacint@20: typedef typename graph_traits::edge_iterator edge_iterator; jacint@20: typedef typename graph_traits::each_node_iterator each_node_iterator; jacint@20: typedef typename graph_traits::out_edge_iterator out_edge_iterator; jacint@20: typedef typename graph_traits::in_edge_iterator in_edge_iterator; jacint@20: typedef typename graph_traits::each_edge_iterator each_edge_iterator; jacint@20: jacint@20: jacint@20: graph_type& G; jacint@20: node_iterator s; jacint@20: node_iterator t; jacint@20: edge_property_vector flow; jacint@20: edge_property_vector& capacity; jacint@20: T value; jacint@20: node_property_vector mincutvector; jacint@20: jacint@20: jacint@20: public: jacint@20: jacint@20: preflow_push_hl(graph_type& _G, node_iterator _s, node_iterator _t, edge_property_vector& _capacity) : G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity), mincutvector(_G, true) { } jacint@20: jacint@20: jacint@20: jacint@20: jacint@20: /* jacint@20: The run() function runs the highest label preflow-push, jacint@20: running time: O(n^2\sqrt(m)) jacint@20: */ jacint@20: void run() { jacint@20: jacint@20: node_property_vector level(G); //level of node jacint@20: node_property_vector excess(G); //excess of node jacint@20: jacint@20: int n=number_of(G.first_node()); //number of nodes jacint@20: int b=n; jacint@20: /*b is a bound on the highest level of an active node. In the beginning it is at most n-2.*/ jacint@20: jacint@20: std::vector > stack(2*n-1); //Stack of the active nodes in level i. jacint@20: jacint@20: jacint@20: jacint@20: jacint@20: /*Reverse_bfs from t, to find the starting level.*/ jacint@20: jacint@20: reverse_bfs bfs(G, t); jacint@20: bfs.run(); jacint@30: for(each_node_iterator v=G.first_node(); v.valid(); ++v) { jacint@20: level.put(v, bfs.dist(v)); jacint@20: //std::cout << "the level of " << v << " is " << bfs.dist(v); jacint@20: } jacint@20: jacint@20: /*The level of s is fixed to n*/ jacint@20: level.put(s,n); jacint@20: jacint@20: jacint@20: jacint@20: jacint@20: jacint@20: /* Starting flow. It is everywhere 0 at the moment. */ jacint@20: jacint@30: for(out_edge_iterator i=G.first_out_edge(s); i.valid(); ++i) jacint@20: { jacint@20: node_iterator w=G.head(i); jacint@20: flow.put(i, capacity.get(i)); jacint@20: stack[bfs.dist(w)].push(w); jacint@20: excess.put(w, capacity.get(i)); jacint@20: } jacint@20: jacint@20: jacint@20: /* jacint@20: End of preprocessing jacint@20: */ jacint@20: jacint@20: jacint@20: jacint@20: /* jacint@20: Push/relabel on the highest level active nodes. jacint@20: */ jacint@20: jacint@20: /*While there exists active node.*/ jacint@20: while (b) { jacint@20: jacint@20: /*We decrease the bound if there is no active node of level b.*/ jacint@20: if (stack[b].empty()) { jacint@20: --b; jacint@20: } else { jacint@20: jacint@20: node_iterator w=stack[b].top(); //w is the highest label active node. jacint@20: stack[b].pop(); //We delete w from the stack. jacint@20: jacint@20: int newlevel=2*n-2; //In newlevel we maintain the next level of w. jacint@20: jacint@30: for(out_edge_iterator e=G.first_out_edge(w); e.valid(); ++e) { jacint@20: node_iterator v=G.head(e); jacint@20: /*e is the edge wv.*/ jacint@20: jacint@20: if (flow.get(e) excess.get(w)) { jacint@20: /*A nonsaturating push.*/ jacint@20: jacint@20: if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); jacint@20: /*v becomes active.*/ jacint@20: jacint@20: flow.put(e, flow.get(e)+excess.get(w)); jacint@20: excess.put(v, excess.get(v)+excess.get(w)); jacint@20: excess.put(w,0); jacint@20: //std::cout << w << " " << v <<" elore elen nonsat pump " << std::endl; jacint@20: break; jacint@20: } else { jacint@20: /*A saturating push.*/ jacint@20: jacint@20: if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); jacint@20: /*v becomes active.*/ jacint@20: jacint@20: excess.put(v, excess.get(v)+capacity.get(e)-flow.get(e)); jacint@20: excess.put(w, excess.get(w)-capacity.get(e)+flow.get(e)); jacint@20: flow.put(e, capacity.get(e)); jacint@20: //std::cout << w<<" " < excess.get(w)) jacint@20: } // if(level.get(w)==level.get(v)+1) jacint@20: jacint@20: else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);} jacint@20: jacint@20: } //if (flow.get(e)0) { jacint@20: /*e is an edge of the residual graph */ jacint@20: jacint@20: if(level.get(w)==level.get(v)+1) { jacint@20: /*Push is allowed now*/ jacint@20: jacint@20: if (flow.get(e) > excess.get(w)) { jacint@20: /*A nonsaturating push.*/ jacint@20: jacint@20: if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); jacint@20: /*v becomes active.*/ jacint@20: jacint@20: flow.put(e, flow.get(e)-excess.get(w)); jacint@20: excess.put(v, excess.get(v)+excess.get(w)); jacint@20: excess.put(w,0); jacint@20: //std::cout << v << " " << w << " vissza elen nonsat pump " << std::endl; jacint@20: break; jacint@20: } else { jacint@20: /*A saturating push.*/ jacint@20: jacint@20: if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); jacint@20: /*v becomes active.*/ jacint@20: jacint@20: excess.put(v, excess.get(v)+flow.get(e)); jacint@20: excess.put(w, excess.get(w)-flow.get(e)); jacint@20: flow.put(e,0); jacint@20: //std::cout << v <<" " << w << " vissza elen sat pump " << std::endl; jacint@20: if (excess.get(w)==0) { break;} jacint@20: } //if (flow.get(e) > excess.get(v)) jacint@20: } //if(level.get(w)==level.get(v)+1) jacint@20: jacint@20: else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);} jacint@20: jacint@20: jacint@20: } //if (flow.get(e)>0) jacint@20: jacint@20: } //for jacint@20: jacint@20: jacint@20: if (excess.get(w)>0) { jacint@20: level.put(w,++newlevel); jacint@20: stack[newlevel].push(w); jacint@20: b=newlevel; jacint@20: //std::cout << "The new level of " << w << " is "<< newlevel < allflow() { jacint@20: return flow; jacint@20: } jacint@20: jacint@20: jacint@20: jacint@20: /* jacint@20: Returns a minimum cut by using a reverse bfs from t in the residual graph. jacint@20: */ jacint@20: jacint@20: node_property_vector mincut() { jacint@20: jacint@20: std::queue queue; jacint@20: jacint@20: mincutvector.put(t,false); jacint@20: queue.push(t); jacint@20: jacint@20: while (!queue.empty()) { jacint@20: node_iterator w=queue.front(); jacint@20: queue.pop(); jacint@20: jacint@30: for(in_edge_iterator e=G.first_in_edge(w) ; e.valid(); ++e) { jacint@20: node_iterator v=G.tail(e); jacint@20: if (mincutvector.get(v) && flow.get(e) < capacity.get(e) ) { jacint@20: queue.push(v); jacint@20: mincutvector.put(v, false); jacint@20: } jacint@20: } // for jacint@20: jacint@30: for(out_edge_iterator e=G.first_out_edge(w) ; e.valid(); ++e) { jacint@20: node_iterator v=G.head(e); jacint@20: if (mincutvector.get(v) && flow.get(e) > 0 ) { jacint@20: queue.push(v); jacint@20: mincutvector.put(v, false); jacint@20: } jacint@20: } // for jacint@20: jacint@20: } jacint@20: jacint@20: return mincutvector; jacint@20: jacint@20: } jacint@20: jacint@20: jacint@20: }; jacint@20: }//namespace marci jacint@20: #endif jacint@20: jacint@20: jacint@20: jacint@20: