# source:lemon-0.x/src/work/jacint/preflow_push_hl.h@88:93bb934b0794

Last change on this file since 88:93bb934b0794 was 88:93bb934b0794, checked in by jacint, 17 years ago

fast version

File size: 7.4 KB
RevLine
[72]1// -*- C++ -*-
2/*
[83]3preflow_push_hl.h
[72]4by jacint.
5Runs the highest label variant of the preflow push algorithm with
6running time O(n^2\sqrt(m)).
7
8Member functions:
9
10void run() : runs the algorithm
11
12 The following functions should be used after run() was already run.
13
14T maxflow() : returns the value of a maximum flow
15
[83]16T flowonedge(EdgeIt e) : for a fixed maximum flow x it returns x(e)
[72]17
[83]18Graph::EdgeMap<T> allflow() : returns the fixed maximum flow x
[72]19
[83]20Graph::NodeMap<bool> mincut() : returns a
[72]21     characteristic vector of a minimum cut. (An empty level
22     in the algorithm gives a minimum cut.)
23*/
24
25#ifndef PREFLOW_PUSH_HL_H
26#define PREFLOW_PUSH_HL_H
27
[88]28#define A 1
29
[72]30#include <vector>
31#include <stack>
32
[78]33#include <reverse_bfs.h>
[72]34
35namespace marci {
36
[78]37  template <typename Graph, typename T>
[72]38  class preflow_push_hl {
39
40    typedef typename Graph::NodeIt NodeIt;
41    typedef typename Graph::EdgeIt EdgeIt;
42    typedef typename Graph::EachNodeIt EachNodeIt;
43    typedef typename Graph::OutEdgeIt OutEdgeIt;
44    typedef typename Graph::InEdgeIt InEdgeIt;
45
46    Graph& G;
47    NodeIt s;
48    NodeIt t;
[78]49    typename Graph::EdgeMap<T> flow;
50    typename Graph::EdgeMap<T> capacity;
[72]51    T value;
[78]52    typename Graph::NodeMap<bool> mincutvector;
[72]53
54  public:
55
56    preflow_push_hl(Graph& _G, NodeIt _s, NodeIt _t,
[78]57                    typename Graph::EdgeMap<T>& _capacity) :
[83]58      G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity),
59      mincutvector(_G, true) { }
[72]60
61
62    /*
63      The run() function runs the highest label preflow-push,
64      running time: O(n^2\sqrt(m))
65    */
66    void run() {
67
[88]68      std::cout<<"A is "<<A<<" ";
69
[83]70      typename Graph::NodeMap<int> level(G);
[84]71      typename Graph::NodeMap<T> excess(G);
[85]72
[84]73      int n=G.nodeNum();
[83]74      int b=n-2;
75      /*
76        b is a bound on the highest level of an active node.
77        In the beginning it is at most n-2.
78      */
[72]79
[85]80      std::vector<int> numb(n);     //The number of nodes on level i < n.
[83]81      std::vector<std::stack<NodeIt> > stack(2*n-1);
82      //Stack of the active nodes in level i.
[72]83
84
85      /*Reverse_bfs from t, to find the starting level.*/
[78]86      reverse_bfs<Graph> bfs(G, t);
[72]87      bfs.run();
[83]88      for(EachNodeIt v=G.template first<EachNodeIt>(); v.valid(); ++v)
89        {
[85]90          int dist=bfs.dist(v);
91          level.set(v, dist);
92          ++numb[dist];
[83]93        }
[72]94
95      level.set(s,n);
96
97
[83]98      /* Starting flow. It is everywhere 0 at the moment. */
99      for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e)
[72]100        {
[83]101          if ( capacity.get(e) > 0 ) {
[85]103            if ( w!=s ) {
104              if ( excess.get(w) == 0 && w!=t ) stack[level.get(w)].push(w);
105              flow.set(e, capacity.get(e));
106              excess.set(w, excess.get(w)+capacity.get(e));
107            }
[83]108          }
[72]109        }
110
111      /*
112         End of preprocessing
113      */
114
115
116
117      /*
[84]118        Push/relabel on the highest level active nodes.
[72]119      */
120
[85]121      /*While there exists an active node.*/
[72]122      while (b) {
123
[85]124        /*We decrease the bound if there is no active node of level b.*/
[72]125        if (stack[b].empty()) {
126          --b;
127        } else {
128
[84]129          NodeIt w=stack[b].top();        //w is a highest label active node.
130          stack[b].pop();
[72]131
[84]132          int newlevel=2*n-2;             //In newlevel we bound the next level of w.
[72]133
134          for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
[84]135
136            if ( flow.get(e) < capacity.get(e) ) {
137              /*e is an edge of the residual graph */
[72]138
[84]139              NodeIt v=G.head(e);               /*e is the edge wv.*/
[72]140
[84]141              if( level.get(w) == level.get(v)+1 ) {
[72]142                /*Push is allowed now*/
143
[85]144                if ( excess.get(v)==0 && v != s && v !=t ) stack[level.get(v)].push(v);
145                /*v becomes active.*/
146
147                if ( capacity.get(e)-flow.get(e) > excess.get(w) ) {
[72]148                  /*A nonsaturating push.*/
149
150                  flow.set(e, flow.get(e)+excess.get(w));
151                  excess.set(v, excess.get(v)+excess.get(w));
152                  excess.set(w,0);
153                  break;
[85]154
[72]155                } else {
156                  /*A saturating push.*/
157
158                  excess.set(v, excess.get(v)+capacity.get(e)-flow.get(e));
159                  excess.set(w, excess.get(w)-capacity.get(e)+flow.get(e));
160                  flow.set(e, capacity.get(e));
[85]161                  if ( excess.get(w)==0 ) break;
162                  /*If w is not active any more, then we go on to the next node.*/
[72]163
[85]164                }
165              } else {
166                newlevel = newlevel < level.get(v) ? newlevel : level.get(v);
167              }
[72]168
[85]169            } //if the out edge wv is in the res graph
[72]170
[85]171          } //for out edges wv
[72]172
173
[85]174          if ( excess.get(w) > 0 ) {
175
176            for( InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
177              NodeIt v=G.tail(e);  /*e is the edge vw.*/
[72]178
[85]179              if( flow.get(e) > 0 ) {
180                /*e is an edge of the residual graph */
[72]181
[85]182                if( level.get(w)==level.get(v)+1 ) {
183                  /*Push is allowed now*/
[72]184
[85]185                  if ( excess.get(v)==0 && v != s && v !=t) stack[level.get(v)].push(v);
[72]186                  /*v becomes active.*/
187
[85]188                  if ( flow.get(e) > excess.get(w) ) {
189                    /*A nonsaturating push.*/
[72]190
[85]191                    flow.set(e, flow.get(e)-excess.get(w));
192                    excess.set(v, excess.get(v)+excess.get(w));
193                    excess.set(w,0);
194                    break;
195                  } else {
196                    /*A saturating push.*/
197
198                    excess.set(v, excess.get(v)+flow.get(e));
199                    excess.set(w, excess.get(w)-flow.get(e));
200                    flow.set(e,0);
201                    if ( excess.get(w)==0 ) break;
202                  }
203                } else {
204                  newlevel = newlevel < level.get(v) ? newlevel : level.get(v);
205                }
206
207              } //if in edge vw is in the res graph
[72]208
[85]209            } //for in edges vw
[72]210
[85]211          } // if w still has excess after the out edge for cycle
[72]212
213
[85]214          /*
215            Relabel
216          */
217
218          if ( excess.get(w) > 0 ) {
219
220            int oldlevel=level.get(w);
[72]221            level.set(w,++newlevel);
[85]222
223            if ( oldlevel < n ) {
224              --numb[oldlevel];
225
[88]226              if ( !numb[oldlevel] && oldlevel < A*n ) {  //If the level of w gets empty.
[85]227
228                for (EachNodeIt v=G.template first<EachNodeIt>(); v.valid() ; ++v) {
229                  if (level.get(v) > oldlevel && level.get(v) < n ) level.set(v,n);
230                }
231                for (int i=oldlevel+1 ; i!=n ; ++i) numb[i]=0;
232                if ( newlevel < n ) newlevel=n;
233              } else {
234                if ( newlevel < n ) ++numb[newlevel];
235              }
236            } else {
237            if ( newlevel < n ) ++numb[newlevel];
238            }
239
[72]240            stack[newlevel].push(w);
241            b=newlevel;
[85]242
[72]243          }
244
[85]245        } // if stack[b] is nonempty
[72]246
[85]247      } // while(b)
248
[72]249
250      value = excess.get(t);
251      /*Max flow value.*/
252
253
254    } //void run()
255
256
257
258
259
260    /*
261      Returns the maximum value of a flow.
262     */
263
264    T maxflow() {
265      return value;
266    }
267
268
269
270    /*
271      For the maximum flow x found by the algorithm, it returns the flow value on Edge e, i.e. x(e).
272    */
273
[88]274    T flowonedge(EdgeIt e) {
[72]275      return flow.get(e);
276    }
277
278
279
280    /*
281      Returns the maximum flow x found by the algorithm.
282    */
283
[78]284    typename Graph::EdgeMap<T> allflow() {
[72]285      return flow;
286    }
287
288
289
290    /*
291      Returns a minimum cut by using a reverse bfs from t in the residual graph.
292    */
293
[78]294    typename Graph::NodeMap<bool> mincut() {
[72]295
296      std::queue<NodeIt> queue;
297
298      mincutvector.set(t,false);
299      queue.push(t);
300
301      while (!queue.empty()) {
302        NodeIt w=queue.front();
303        queue.pop();
304
305        for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
306          NodeIt v=G.tail(e);
307          if (mincutvector.get(v) && flow.get(e) < capacity.get(e) ) {
308            queue.push(v);
309            mincutvector.set(v, false);
310          }
311        } // for
312
313        for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
315          if (mincutvector.get(v) && flow.get(e) > 0 ) {
316            queue.push(v);
317            mincutvector.set(v, false);
318          }
319        } // for
320
321      }
322
323      return mincutvector;
324
325    }
326  };
327}//namespace marci
328#endif
329
330
331
332
Note: See TracBrowser for help on using the repository browser.