jacint@21: /* jacint@21: preflow_push_max_flow_hh jacint@21: by jacint. jacint@21: Runs a preflow push algorithm with the modification, jacint@21: that we do not push on nodes with level at least n. jacint@21: Moreover, if a level gets empty, we put all nodes above that jacint@21: level to level n. Hence, in the end, we arrive at a maximum preflow jacint@21: with value of a max flow value. An empty level gives a minimum cut. jacint@21: jacint@21: Member functions: jacint@21: jacint@21: void run() : runs the algorithm jacint@21: jacint@21: The following functions should be used after run() was already run. jacint@21: jacint@21: T maxflow() : returns the value of a maximum flow jacint@21: jacint@21: node_property_vector mincut(): returns a jacint@21: characteristic vector of a minimum cut. jacint@21: */ jacint@21: jacint@21: #ifndef PREFLOW_PUSH_MAX_FLOW_HH jacint@21: #define PREFLOW_PUSH_MAX_FLOW_HH jacint@21: jacint@21: #include jacint@21: #include jacint@21: #include jacint@21: jacint@21: #include jacint@21: #include jacint@21: #include jacint@21: #include jacint@21: jacint@21: jacint@21: namespace marci { jacint@21: jacint@21: template jacint@21: class preflow_push_max_flow { jacint@21: jacint@21: typedef typename graph_traits::node_iterator node_iterator; jacint@21: typedef typename graph_traits::each_node_iterator each_node_iterator; jacint@21: typedef typename graph_traits::out_edge_iterator out_edge_iterator; jacint@21: typedef typename graph_traits::in_edge_iterator in_edge_iterator; jacint@21: jacint@21: graph_type& G; jacint@21: node_iterator s; jacint@21: node_iterator t; jacint@21: edge_property_vector& capacity; jacint@21: T value; jacint@21: node_property_vector mincutvector; jacint@21: jacint@21: jacint@21: jacint@21: public: jacint@21: jacint@21: preflow_push_max_flow(graph_type& _G, node_iterator _s, node_iterator _t, edge_property_vector& _capacity) : G(_G), s(_s), t(_t), capacity(_capacity), mincutvector(_G, false) { } jacint@21: jacint@21: jacint@21: /* jacint@21: The run() function runs a modified version of the highest label preflow-push, which only jacint@21: finds a maximum preflow, hence giving the value of a maximum flow. jacint@21: */ jacint@21: void run() { jacint@21: jacint@21: edge_property_vector flow(G, 0); //the flow value, 0 everywhere jacint@21: node_property_vector level(G); //level of node jacint@21: node_property_vector excess(G); //excess of node jacint@21: jacint@21: int n=number_of(G.first_node()); //number of nodes jacint@21: int b=n-2; jacint@21: /*b is a bound on the highest level of an active node. In the beginning it is at most n-2.*/ jacint@21: jacint@21: std::vector numb(n); //The number of nodes on level i < n. jacint@21: jacint@21: std::vector > stack(2*n-1); //Stack of the active nodes in level i. jacint@21: jacint@21: jacint@21: jacint@21: /*Reverse_bfs from t, to find the starting level.*/ jacint@21: jacint@21: reverse_bfs bfs(G, t); jacint@21: bfs.run(); jacint@30: for(each_node_iterator v=G.first_node(); v.valid(); ++v) jacint@21: { jacint@21: int dist=bfs.dist(v); jacint@21: level.put(v, dist); jacint@21: ++numb[dist]; jacint@21: } jacint@21: jacint@21: /*The level of s is fixed to n*/ jacint@21: level.put(s,n); jacint@21: jacint@21: jacint@21: /* Starting flow. It is everywhere 0 at the moment. */ jacint@21: jacint@30: for(out_edge_iterator i=G.first_out_edge(s); i.valid(); ++i) jacint@21: { jacint@21: node_iterator w=G.head(i); jacint@21: flow.put(i, capacity.get(i)); jacint@21: stack[bfs.dist(w)].push(w); jacint@21: excess.put(w, capacity.get(i)); jacint@21: } jacint@21: jacint@21: jacint@21: /* jacint@21: End of preprocessing jacint@21: */ jacint@21: jacint@21: jacint@21: jacint@21: jacint@21: /* jacint@21: Push/relabel on the highest level active nodes. jacint@21: */ jacint@21: jacint@21: /*While there exists an active node.*/ jacint@21: while (b) { jacint@21: jacint@21: /*We decrease the bound if there is no active node of level b.*/ jacint@21: if (stack[b].empty()) { jacint@21: --b; jacint@21: } else { jacint@21: jacint@21: node_iterator w=stack[b].top(); //w is the highest label active node. jacint@21: stack[b].pop(); //We delete w from the stack. jacint@21: jacint@21: int newlevel=2*n-2; //In newlevel we maintain the next level of w. jacint@21: jacint@30: for(out_edge_iterator e=G.first_out_edge(w); e.valid(); ++e) { jacint@21: node_iterator v=G.head(e); jacint@21: /*e is the edge wv.*/ jacint@21: jacint@21: if (flow.get(e) excess.get(w)) { jacint@21: /*A nonsaturating push.*/ jacint@21: jacint@21: if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); jacint@21: /*v becomes active.*/ jacint@21: jacint@21: flow.put(e, flow.get(e)+excess.get(w)); jacint@21: excess.put(v, excess.get(v)+excess.get(w)); jacint@21: excess.put(w,0); jacint@21: //std::cout << w << " " << v <<" elore elen nonsat pump " << std::endl; jacint@21: break; jacint@21: } else { jacint@21: /*A saturating push.*/ jacint@21: jacint@21: if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); jacint@21: /*v becomes active.*/ jacint@21: jacint@21: excess.put(v, excess.get(v)+capacity.get(e)-flow.get(e)); jacint@21: excess.put(w, excess.get(w)-capacity.get(e)+flow.get(e)); jacint@21: flow.put(e, capacity.get(e)); jacint@21: //std::cout << w <<" " << v <<" elore elen sat pump " << std::endl; jacint@21: if (excess.get(w)==0) break; jacint@21: /*If w is not active any more, then we go on to the next node.*/ jacint@21: jacint@21: } // if (capacity.get(e)-flow.get(e) > excess.get(w)) jacint@21: } // if (level.get(w)==level.get(v)+1) jacint@21: jacint@21: else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);} jacint@21: jacint@21: } //if (flow.get(e)0) { jacint@21: /*e is an edge of the residual graph */ jacint@21: jacint@21: if(level.get(w)==level.get(v)+1) { jacint@21: /*Push is allowed now*/ jacint@21: jacint@21: if (flow.get(e) > excess.get(w)) { jacint@21: /*A nonsaturating push.*/ jacint@21: jacint@21: if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); jacint@21: /*v becomes active.*/ jacint@21: jacint@21: flow.put(e, flow.get(e)-excess.get(w)); jacint@21: excess.put(v, excess.get(v)+excess.get(w)); jacint@21: excess.put(w,0); jacint@21: //std::cout << v << " " << w << " vissza elen nonsat pump " << std::endl; jacint@21: break; jacint@21: } else { jacint@21: /*A saturating push.*/ jacint@21: jacint@21: if (excess.get(v)==0 && v != s) stack[level.get(v)].push(v); jacint@21: /*v becomes active.*/ jacint@21: jacint@21: flow.put(e,0); jacint@21: excess.put(v, excess.get(v)+flow.get(e)); jacint@21: excess.put(w, excess.get(w)-flow.get(e)); jacint@21: //std::cout << v <<" " << w << " vissza elen sat pump " << std::endl; jacint@21: if (excess.get(w)==0) { break;} jacint@21: } //if (flow.get(e) > excess.get(v)) jacint@21: } //if(level.get(w)==level.get(v)+1) jacint@21: jacint@21: else {newlevel = newlevel < level.get(v) ? newlevel : level.get(v);} jacint@21: //std::cout << "Leveldecrease of node " << w << " to " << newlevel << std::endl; jacint@21: jacint@21: } //if (flow.get(e)>0) jacint@21: jacint@21: } //for in-edge jacint@21: jacint@21: jacint@21: jacint@21: jacint@21: /* jacint@21: Relabel jacint@21: */ jacint@21: if (excess.get(w)>0) { jacint@21: /*Now newlevel <= n*/ jacint@21: jacint@21: int l=level.get(w); //l is the old level of w. jacint@21: --numb[l]; jacint@21: jacint@21: if (newlevel == n) { jacint@21: level.put(w,n); jacint@21: jacint@21: } else { jacint@21: jacint@21: if (numb[l]) { jacint@21: /*If the level of w remains nonempty.*/ jacint@21: jacint@21: level.put(w,++newlevel); jacint@21: ++numb[newlevel]; jacint@21: stack[newlevel].push(w); jacint@21: b=newlevel; jacint@21: } else { jacint@21: /*If the level of w gets empty.*/ jacint@21: jacint@30: for (each_node_iterator v=G.first_node() ; v.valid() ; ++v) { jacint@21: if (level.get(v) >= l ) { jacint@21: level.put(v,n); jacint@21: } jacint@21: } jacint@21: jacint@21: for (int i=l+1 ; i!=n ; ++i) numb[i]=0; jacint@21: } //if (numb[l]) jacint@21: jacint@21: } // if (newlevel = n) jacint@21: jacint@21: } // if (excess.get(w)>0) jacint@21: jacint@21: jacint@21: } //else jacint@21: jacint@21: } //while(b) jacint@21: jacint@21: value=excess.get(t); jacint@21: /*Max flow value.*/ jacint@21: jacint@21: jacint@21: jacint@21: /* jacint@21: We find an empty level, e. The nodes above this level give jacint@21: a minimum cut. jacint@21: */ jacint@21: jacint@21: int e=1; jacint@21: jacint@21: while(e) { jacint@21: if(numb[e]) ++e; jacint@21: else break; jacint@21: } jacint@30: for (each_node_iterator v=G.first_node(); v.valid(); ++v) { jacint@21: if (level.get(v) > e) mincutvector.put(v, true); jacint@21: } jacint@21: jacint@21: jacint@21: } // void run() jacint@21: jacint@21: jacint@21: jacint@21: /* jacint@21: Returns the maximum value of a flow. jacint@21: */ jacint@21: jacint@21: T maxflow() { jacint@21: return value; jacint@21: } jacint@21: jacint@21: jacint@21: jacint@21: /* jacint@21: Returns a minimum cut. jacint@21: */ jacint@21: jacint@21: node_property_vector mincut() { jacint@21: return mincutvector; jacint@21: } jacint@21: jacint@21: jacint@21: }; jacint@21: }//namespace marci jacint@21: #endif jacint@21: jacint@21: jacint@21: jacint@21: jacint@21: