- Better Makefile.am. (I hope.)
- Some more tests
2 //The same as preflow.h, using ResGraphWrapper
3 #ifndef HUGO_PREFLOW_RES_H
4 #define HUGO_PREFLOW_RES_H
11 #include <graph_wrapper.h>
17 template <typename Graph, typename T,
18 typename CapMap=typename Graph::template EdgeMap<T>,
19 typename FlowMap=typename Graph::template EdgeMap<T> >
22 typedef typename Graph::Node Node;
23 typedef typename Graph::Edge Edge;
24 typedef typename Graph::NodeIt NodeIt;
25 typedef typename Graph::OutEdgeIt OutEdgeIt;
26 typedef typename Graph::InEdgeIt InEdgeIt;
31 const CapMap& capacity;
36 typedef ResGraphWrapper<const Graph, T, CapMap, FlowMap> ResGW;
37 typedef typename ResGW::OutEdgeIt ResOutEdgeIt;
38 typedef typename ResGW::InEdgeIt ResInEdgeIt;
39 typedef typename ResGW::Edge ResEdge;
42 PreflowRes(Graph& _G, Node _s, Node _t, CapMap& _capacity,
43 FlowMap& _flow, bool _constzero ) :
44 G(_G), s(_s), t(_t), capacity(_capacity), flow(_flow), constzero(_constzero) {}
49 ResGW res_graph(G, capacity, flow);
51 value=0; //for the subsequent runs
53 bool phase=0; //phase 0 is the 1st phase, phase 1 is the 2nd
55 int heur0=(int)(H0*n); //time while running 'bound decrease'
56 int heur1=(int)(H1*n); //time while running 'highest label'
57 int heur=heur1; //starting time interval (#of relabels)
60 what_heur is 0 in case 'bound decrease'
61 and 1 in case 'highest label'
65 Needed for 'bound decrease', 'true'
66 means no active nodes are above bound b.
69 int k=n-2; //bound on the highest level under n containing a node
70 int b=k; //bound on the highest level under n of an active node
72 typename Graph::template NodeMap<int> level(G,n);
73 typename Graph::template NodeMap<T> excess(G);
75 std::vector<Node> active(n-1,INVALID);
76 typename Graph::template NodeMap<Node> next(G,INVALID);
77 //Stack of the active nodes in level i < n.
78 //We use it in both phases.
80 typename Graph::template NodeMap<Node> left(G,INVALID);
81 typename Graph::template NodeMap<Node> right(G,INVALID);
82 std::vector<Node> level_list(n,INVALID);
84 List of the nodes in level i<n.
89 Reverse_bfs from t in the residual graph,
90 to find the starting level.
93 std::queue<Node> bfs_queue;
96 while (!bfs_queue.empty()) {
98 Node v=bfs_queue.front();
103 for(res_graph.first(e,v); res_graph.valid(e);
105 Node w=res_graph.tail(e);
106 if ( level[w] == n && w != s ) {
108 Node first=level_list[l];
109 if ( G.valid(first) ) left.set(first,w);
123 for(G.first(v); G.valid(v); G.next(v)) {
127 for(G.first(e,v); G.valid(e); G.next(e)) exc+=flow[e];
129 for(G.first(f,v); G.valid(f); G.next(f)) exc-=flow[f];
133 //putting the active nodes into the stack
135 if ( exc > 0 && lev < n ) {
136 next.set(v,active[lev]);
146 for(res_graph.first(e,s); res_graph.valid(e);
148 Node w=res_graph.head(e);
149 if ( level[w] < n ) {
150 if ( excess[w] == 0 && w!=t ) {
151 next.set(w,active[level[w]]);
154 T rem=res_graph.resCap(e);
155 excess.set(w, excess[w]+rem);
156 res_graph.augment(e, rem );
168 Push/relabel on the highest level active nodes.
175 if ( !what_heur && !end && k > 0 ) {
181 std::queue<Node> bfs_queue;
184 while (!bfs_queue.empty()) {
186 Node v=bfs_queue.front();
191 for(res_graph.first(e,v);
192 res_graph.valid(e); res_graph.next(e)) {
193 Node u=res_graph.tail(e);
194 if ( level[u] >= n ) {
197 if ( excess[u] > 0 ) {
198 next.set(u,active[l]);
211 if ( !G.valid(active[b]) ) --b;
219 int newlevel=n; //bound on the next level of w
222 for(res_graph.first(e,w); res_graph.valid(e); res_graph.next(e)) {
224 Node v=res_graph.head(e);
225 if( lev > level[v] ) {
226 /*Push is allowed now*/
228 if ( excess[v]==0 && v!=t && v!=s ) {
230 next.set(v,active[lev_v]);
234 T remcap=res_graph.resCap(e);
236 if ( remcap >= exc ) {
237 /*A nonsaturating push.*/
238 res_graph.augment(e, exc);
239 excess.set(v, excess[v]+exc);
244 /*A saturating push.*/
246 res_graph.augment(e, remcap);
247 excess.set(v, excess[v]+remcap);
250 } else if ( newlevel > level[v] ){
264 //now 'lev' is the old level of w
267 level.set(w,++newlevel);
268 next.set(w,active[newlevel]);
273 Node right_n=right[w];
276 if ( G.valid(right_n) ) {
277 if ( G.valid(left_n) ) {
278 right.set(left_n, right_n);
279 left.set(right_n, left_n);
281 level_list[lev]=right_n;
282 left.set(right_n, INVALID);
285 if ( G.valid(left_n) ) {
286 right.set(left_n, INVALID);
288 level_list[lev]=INVALID;
293 if ( !G.valid(level_list[lev]) ) {
296 for (int i=lev; i!=k ; ) {
297 Node v=level_list[++i];
298 while ( G.valid(v) ) {
302 level_list[i]=INVALID;
303 if ( !what_heur ) active[i]=INVALID;
313 if ( newlevel == n ) level.set(w,n);
315 level.set(w,++newlevel);
316 next.set(w,active[newlevel]);
318 if ( what_heur ) b=newlevel;
319 if ( k < newlevel ) ++k; //now k=newlevel
320 Node first=level_list[newlevel];
321 if ( G.valid(first) ) left.set(first,w);
324 level_list[newlevel]=w;
330 if ( relabel >= heur ) {
348 } // if stack[b] is nonempty
363 Returns the maximum value of a flow.
377 void Flow(FlowMap& _flow ) {
379 for(G.first(v) ; G.valid(v); G.next(v))
380 _flow.set(v,flow[v]);
386 Returns the minimum min cut, by a bfs from s in the residual graph.
389 template<typename _CutMap>
390 void minMinCut(_CutMap& M) {
392 std::queue<Node> queue;
397 while (!queue.empty()) {
398 Node w=queue.front();
402 for(G.first(e,w) ; G.valid(e); G.next(e)) {
404 if (!M[v] && flow[e] < capacity[e] ) {
411 for(G.first(f,w) ; G.valid(f); G.next(f)) {
413 if (!M[v] && flow[f] > 0 ) {
424 Returns the maximum min cut, by a reverse bfs
425 from t in the residual graph.
428 template<typename _CutMap>
429 void maxMinCut(_CutMap& M) {
431 std::queue<Node> queue;
436 while (!queue.empty()) {
437 Node w=queue.front();
442 for(G.first(e,w) ; G.valid(e); G.next(e)) {
444 if (!M[v] && flow[e] < capacity[e] ) {
451 for(G.first(f,w) ; G.valid(f); G.next(f)) {
453 if (!M[v] && flow[f] > 0 ) {
461 for(G.first(v) ; G.valid(v); G.next(v)) {
469 template<typename CutMap>
470 void minCut(CutMap& M) {
476 void resetTarget (Node _t) {t=_t;}
477 void resetSource (Node _s) {s=_s;}
479 void resetCap (CapMap _cap) {capacity=_cap;}
481 void resetFlow (FlowMap _flow, bool _constzero) {
483 constzero=_constzero;
491 #endif //HUGO_PREFLOW_RES_H