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