COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/marci/edmonds_karp.h @ 305:6720705c9095

Last change on this file since 305:6720705c9095 was 303:1b377a730d02, checked in by marci, 16 years ago

konvergalunk, konvergalunk...

File size: 34.5 KB
Line 
1// -*- c++ -*-
2#ifndef HUGO_EDMONDS_KARP_H
3#define HUGO_EDMONDS_KARP_H
4
5#include <algorithm>
6#include <list>
7#include <iterator>
8
9#include <bfs_iterator.h>
10#include <invalid.h>
11#include <graph_wrapper.h>
12
13namespace hugo {
14
15  template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
16  class ResGraph {
17  public:
18    typedef typename Graph::Node Node;
19    typedef typename Graph::NodeIt NodeIt;
20  private:
21    typedef typename Graph::SymEdgeIt OldSymEdgeIt;
22    const Graph& G;
23    FlowMap& flow;
24    const CapacityMap& capacity;
25  public:
26    ResGraph(const Graph& _G, FlowMap& _flow,
27             const CapacityMap& _capacity) :
28      G(_G), flow(_flow), capacity(_capacity) { }
29
30    class Edge;
31    class OutEdgeIt;
32    friend class Edge;
33    friend class OutEdgeIt;
34
35    class Edge {
36      friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
37    protected:
38      const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
39      OldSymEdgeIt sym;
40    public:
41      Edge() { }
42      //Edge(const Edge& e) : resG(e.resG), sym(e.sym) { }
43      Number free() const {
44        if (resG->G.aNode(sym)==resG->G.tail(sym)) {
45          return (resG->capacity.get(sym)-resG->flow.get(sym));
46        } else {
47          return (resG->flow.get(sym));
48        }
49      }
50      bool valid() const { return sym.valid(); }
51      void augment(Number a) const {
52        if (resG->G.aNode(sym)==resG->G.tail(sym)) {
53          resG->flow.set(sym, resG->flow.get(sym)+a);
54          //resG->flow[sym]+=a;
55        } else {
56          resG->flow.set(sym, resG->flow.get(sym)-a);
57          //resG->flow[sym]-=a;
58        }
59      }
60    };
61
62    class OutEdgeIt : public Edge {
63      friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
64    public:
65      OutEdgeIt() { }
66      //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
67    private:
68      OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, Node v) {
69        resG=&_resG;
70        sym=resG->G.template first<OldSymEdgeIt>(v);
71        while( sym.valid() && !(free()>0) ) { ++sym; }
72      }
73    public:
74      OutEdgeIt& operator++() {
75        ++sym;
76        while( sym.valid() && !(free()>0) ) { ++sym; }
77        return *this;
78      }
79    };
80
81    void /*getF*/first(OutEdgeIt& e, Node v) const {
82      e=OutEdgeIt(*this, v);
83    }
84    void /*getF*/first(NodeIt& v) const { G./*getF*/first(v); }
85   
86    template< typename It >
87    It first() const {
88      It e;     
89      /*getF*/first(e);
90      return e;
91    }
92
93    template< typename It >
94    It first(Node v) const {
95      It e;
96      /*getF*/first(e, v);
97      return e;
98    }
99
100    Node tail(Edge e) const { return G.aNode(e.sym); }
101    Node head(Edge e) const { return G.bNode(e.sym); }
102
103    Node aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
104    Node bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
105
106    int id(Node v) const { return G.id(v); }
107
108    template <typename S>
109    class NodeMap {
110      typename Graph::NodeMap<S> node_map;
111    public:
112      NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
113      NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
114      void set(Node nit, S a) { node_map.set(nit, a); }
115      S get(Node nit) const { return node_map.get(nit); }
116      S& operator[](Node nit) { return node_map[nit]; }
117      const S& operator[](Node nit) const { return node_map[nit]; }
118    };
119
120  };
121
122
123  template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
124  class ResGraph2 {
125  public:
126    typedef typename Graph::Node Node;
127    typedef typename Graph::NodeIt NodeIt;
128  private:
129    //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
130    typedef typename Graph::OutEdgeIt OldOutEdgeIt;
131    typedef typename Graph::InEdgeIt OldInEdgeIt;
132   
133    const Graph& G;
134    FlowMap& flow;
135    const CapacityMap& capacity;
136  public:
137    ResGraph2(const Graph& _G, FlowMap& _flow,
138             const CapacityMap& _capacity) :
139      G(_G), flow(_flow), capacity(_capacity) { }
140
141    class Edge;
142    class OutEdgeIt;
143    friend class Edge;
144    friend class OutEdgeIt;
145
146    class Edge {
147      friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
148    protected:
149      const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
150      //OldSymEdgeIt sym;
151      OldOutEdgeIt out;
152      OldInEdgeIt in;
153      bool out_or_in; //true, iff out
154    public:
155      Edge() : out_or_in(true) { }
156      Number free() const {
157        if (out_or_in) {
158          return (resG->capacity.get(out)-resG->flow.get(out));
159        } else {
160          return (resG->flow.get(in));
161        }
162      }
163      bool valid() const {
164        return out_or_in && out.valid() || in.valid(); }
165      void augment(Number a) const {
166        if (out_or_in) {
167          resG->flow.set(out, resG->flow.get(out)+a);
168        } else {
169          resG->flow.set(in, resG->flow.get(in)-a);
170        }
171      }
172    };
173
174    class OutEdgeIt : public Edge {
175      friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
176    public:
177      OutEdgeIt() { }
178    private:
179      OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, Node v) {
180        resG=&_resG;
181        out=resG->G.template first<OldOutEdgeIt>(v);
182        while( out.valid() && !(free()>0) ) { ++out; }
183        if (!out.valid()) {
184          out_or_in=0;
185          in=resG->G.template first<OldInEdgeIt>(v);
186          while( in.valid() && !(free()>0) ) { ++in; }
187        }
188      }
189    public:
190      OutEdgeIt& operator++() {
191        if (out_or_in) {
192          Node v=resG->G.aNode(out);
193          ++out;
194          while( out.valid() && !(free()>0) ) { ++out; }
195          if (!out.valid()) {
196            out_or_in=0;
197            in=resG->G.template first<OldInEdgeIt>(v);
198            while( in.valid() && !(free()>0) ) { ++in; }
199          }
200        } else {
201          ++in;
202          while( in.valid() && !(free()>0) ) { ++in; }
203        }
204        return *this;
205      }
206    };
207
208    void /*getF*/first(OutEdgeIt& e, Node v) const {
209      e=OutEdgeIt(*this, v);
210    }
211    void /*getF*/first(NodeIt& v) const { G./*getF*/first(v); }
212   
213    template< typename It >
214    It first() const {
215      It e;
216      /*getF*/first(e);
217      return e;
218    }
219
220    template< typename It >
221    It first(Node v) const {
222      It e;
223      /*getF*/first(e, v);
224      return e;
225    }
226
227    Node tail(Edge e) const {
228      return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
229    Node head(Edge e) const {
230      return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
231
232    Node aNode(OutEdgeIt e) const {
233      return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
234    Node bNode(OutEdgeIt e) const {
235      return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
236
237    int id(Node v) const { return G.id(v); }
238
239    template <typename S>
240    class NodeMap {
241      typename Graph::NodeMap<S> node_map;
242    public:
243      NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
244      NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
245      void set(Node nit, S a) { node_map.set(nit, a); }
246      S get(Node nit) const { return node_map.get(nit); }
247    };
248  };
249
250
251  template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
252  class MaxFlow {
253  protected:
254    typedef typename Graph::Node Node;
255    typedef typename Graph::Edge Edge;
256    typedef typename Graph::EdgeIt EdgeIt;
257    typedef typename Graph::OutEdgeIt OutEdgeIt;
258    typedef typename Graph::InEdgeIt InEdgeIt;
259    const Graph* g;
260    Node s;
261    Node t;
262    FlowMap* flow;
263    const CapacityMap* capacity;
264    typedef ResGraphWrapper<const Graph, Number, FlowMap, CapacityMap > ResGW;
265    typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
266    typedef typename ResGW::Edge ResGWEdge;
267  public:
268
269    MaxFlow(const Graph& _g, Node _s, Node _t, FlowMap& _flow, const CapacityMap& _capacity) :
270      g(&_g), s(_s), t(_t), flow(&_flow), capacity(&_capacity) { }
271
272    bool augmentOnShortestPath() {
273      ResGW res_graph(*g, *flow, *capacity);
274      bool _augment=false;
275     
276      typedef typename ResGW::NodeMap<bool> ReachedMap;
277      BfsIterator5< ResGW, ReachedMap > bfs(res_graph);
278      bfs.pushAndSetReached(s);
279       
280      typename ResGW::NodeMap<ResGWEdge> pred(res_graph);
281      pred.set(s, INVALID);
282     
283      typename ResGW::NodeMap<Number> free(res_graph);
284       
285      //searching for augmenting path
286      while ( !bfs.finished() ) {
287        ResGWOutEdgeIt e=bfs;
288        if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
289          Node v=res_graph.tail(e);
290          Node w=res_graph.head(e);
291          pred.set(w, e);
292          if (res_graph.valid(pred[v])) {
293            free.set(w, std::min(free[v], res_graph.resCap(e)));
294          } else {
295            free.set(w, res_graph.resCap(e));
296          }
297          if (res_graph.head(e)==t) { _augment=true; break; }
298        }
299       
300        ++bfs;
301      } //end of searching augmenting path
302
303      if (_augment) {
304        Node n=t;
305        Number augment_value=free[t];
306        while (res_graph.valid(pred[n])) {
307          ResGWEdge e=pred[n];
308          res_graph.augment(e, augment_value);
309          n=res_graph.tail(e);
310        }
311      }
312
313      return _augment;
314    }
315
316    template<typename MapGraphWrapper>
317    class DistanceMap {
318    protected:
319      const MapGraphWrapper* g;
320      typename MapGraphWrapper::NodeMap<int> dist;
321    public:
322      DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
323      void set(const typename MapGraphWrapper::Node& n, int a) { dist[n]=a; }
324      int operator[](const typename MapGraphWrapper::Node& n)
325        { return dist[n]; }
326//       int get(const typename MapGraphWrapper::Node& n) const {
327//      return dist[n]; }
328//       bool get(const typename MapGraphWrapper::Edge& e) const {
329//      return (dist.get(g->tail(e))<dist.get(g->head(e))); }
330      bool operator[](const typename MapGraphWrapper::Edge& e) const {
331        return (dist[g->tail(e)]<dist[g->head(e)]);
332      }
333    };
334
335    template<typename MutableGraph> bool augmentOnBlockingFlow() {     
336      typedef MutableGraph MG;
337      bool _augment=false;
338
339      ResGW res_graph(*g, *flow, *capacity);
340
341      typedef typename ResGW::NodeMap<bool> ReachedMap;
342      BfsIterator5< ResGW, ReachedMap > bfs(res_graph);
343
344      bfs.pushAndSetReached(s);
345      //typename ResGW::NodeMap<int> dist(res_graph); //filled up with 0's
346      DistanceMap<ResGW> dist(res_graph);
347      while ( !bfs.finished() ) {
348        ResGWOutEdgeIt e=bfs;
349        if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
350          dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
351        }
352        ++bfs;
353      } //computing distances from s in the residual graph
354
355      MG F;
356      typedef SubGraphWrapper<ResGW, DistanceMap<ResGW> > FilterResGW;
357      FilterResGW filter_res_graph(res_graph, dist);
358      typename ResGW::NodeMap<typename MG::Node> res_graph_to_F(res_graph);
359      {
360        typename ResGW::NodeIt n;
361        for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
362          res_graph_to_F.set(n, F.addNode());
363        }
364      }
365
366      typename MG::Node sF=res_graph_to_F[s];
367      typename MG::Node tF=res_graph_to_F[t];
368      typename MG::EdgeMap<ResGWEdge> original_edge(F);
369      typename MG::EdgeMap<Number> residual_capacity(F);
370
371      //Making F to the graph containing the edges of the residual graph
372      //which are in some shortest paths
373      {
374        typename FilterResGW::EdgeIt e;
375        for(filter_res_graph.first(e); filter_res_graph.valid(e); filter_res_graph.next(e)) {
376          //if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
377          typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
378          original_edge.update();
379          original_edge.set(f, e);
380          residual_capacity.update();
381          residual_capacity.set(f, res_graph.resCap(e));
382          //}
383        }
384      }
385
386      bool __augment=true;
387
388      while (__augment) {
389        __augment=false;
390        //computing blocking flow with dfs
391        typedef typename TrivGraphWrapper<MG>::NodeMap<bool> BlockingReachedMap;
392        DfsIterator5< TrivGraphWrapper<MG>, BlockingReachedMap > dfs(F);
393        typename MG::NodeMap<typename MG::Edge> pred(F);
394        pred.set(sF, INVALID);
395        //invalid iterators for sources
396
397        typename MG::NodeMap<Number> free(F);
398
399        dfs.pushAndSetReached(sF);     
400        while (!dfs.finished()) {
401          ++dfs;
402          if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
403            if (dfs.isBNodeNewlyReached()) {
404              typename MG::Node v=F.aNode(dfs);
405              typename MG::Node w=F.bNode(dfs);
406              pred.set(w, dfs);
407              if (F.valid(pred[v])) {
408                free.set(w, std::min(free[v], residual_capacity[dfs]));
409              } else {
410                free.set(w, residual_capacity[dfs]);
411              }
412              if (w==tF) {
413                __augment=true;
414                _augment=true;
415                break;
416              }
417             
418            } else {
419              F.erase(/*typename MG::OutEdgeIt*/(dfs));
420            }
421          }
422        }
423
424        if (__augment) {
425          typename MG::Node n=tF;
426          Number augment_value=free[tF];
427          while (F.valid(pred[n])) {
428            typename MG::Edge e=pred[n];
429            res_graph.augment(original_edge[e], augment_value);
430            n=F.tail(e);
431            if (residual_capacity[e]==augment_value)
432              F.erase(e);
433            else
434              residual_capacity.set(e, residual_capacity[e]-augment_value);
435          }
436        }
437       
438      }
439           
440      return _augment;
441    }
442
443    template<typename MutableGraph> bool augmentOnBlockingFlow1() {     
444      typedef MutableGraph MG;
445      bool _augment=false;
446
447      ResGW res_graph(*g, *flow, *capacity);
448
449      //bfs for distances on the residual graph
450      typedef typename ResGW::NodeMap<bool> ReachedMap;
451      BfsIterator5< ResGW, ReachedMap > bfs(res_graph);
452      bfs.pushAndSetReached(s);
453      typename ResGW::NodeMap<int> dist(res_graph); //filled up with 0's
454
455      //F will contain the physical copy of the residual graph
456      //with the set of edges which are on shortest paths
457      MG F;
458      typename ResGW::NodeMap<typename MG::Node> res_graph_to_F(res_graph);
459      {
460        typename ResGW::NodeIt n;
461        for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
462          res_graph_to_F.set(n, F.addNode());
463        }
464      }
465
466      typename MG::Node sF=res_graph_to_F[s];
467      typename MG::Node tF=res_graph_to_F[t];
468      typename MG::EdgeMap<ResGWEdge> original_edge(F);
469      typename MG::EdgeMap<Number> residual_capacity(F);
470
471      while ( !bfs.finished() ) {
472        ResGWOutEdgeIt e=bfs;
473        if (res_graph.valid(e)) {
474          if (bfs.isBNodeNewlyReached()) {
475            dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
476            typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
477            original_edge.update();
478            original_edge.set(f, e);
479            residual_capacity.update();
480            residual_capacity.set(f, res_graph.resCap(e));
481          } else {
482            if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
483              typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
484              original_edge.update();
485              original_edge.set(f, e);
486              residual_capacity.update();
487              residual_capacity.set(f, res_graph.resCap(e));
488            }
489          }
490        }
491        ++bfs;
492      } //computing distances from s in the residual graph
493
494      bool __augment=true;
495
496      while (__augment) {
497        __augment=false;
498        //computing blocking flow with dfs
499        typedef typename TrivGraphWrapper<MG>::NodeMap<bool> BlockingReachedMap;
500        DfsIterator5< TrivGraphWrapper<MG>, BlockingReachedMap > dfs(F);
501        typename MG::NodeMap<typename MG::Edge> pred(F);
502        pred.set(sF, INVALID);
503        //invalid iterators for sources
504
505        typename MG::NodeMap<Number> free(F);
506
507        dfs.pushAndSetReached(sF);     
508        while (!dfs.finished()) {
509          ++dfs;
510          if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
511            if (dfs.isBNodeNewlyReached()) {
512              typename MG::Node v=F.aNode(dfs);
513              typename MG::Node w=F.bNode(dfs);
514              pred.set(w, dfs);
515              if (F.valid(pred[v])) {
516                free.set(w, std::min(free[v], residual_capacity[dfs]));
517              } else {
518                free.set(w, residual_capacity[dfs]);
519              }
520              if (w==tF) {
521                __augment=true;
522                _augment=true;
523                break;
524              }
525             
526            } else {
527              F.erase(/*typename MG::OutEdgeIt*/(dfs));
528            }
529          }
530        }
531
532        if (__augment) {
533          typename MG::Node n=tF;
534          Number augment_value=free[tF];
535          while (F.valid(pred[n])) {
536            typename MG::Edge e=pred[n];
537            res_graph.augment(original_edge[e], augment_value);
538            n=F.tail(e);
539            if (residual_capacity[e]==augment_value)
540              F.erase(e);
541            else
542              residual_capacity.set(e, residual_capacity[e]-augment_value);
543          }
544        }
545       
546      }
547           
548      return _augment;
549    }
550
551    bool augmentOnBlockingFlow2() {
552      bool _augment=false;
553
554      ResGW res_graph(*g, *flow, *capacity);
555
556      typedef typename ResGW::NodeMap<bool> ReachedMap;
557      BfsIterator5< ResGW, ReachedMap > bfs(res_graph);
558
559      bfs.pushAndSetReached(s);
560      DistanceMap<ResGW> dist(res_graph);
561      while ( !bfs.finished() ) {
562        ResGWOutEdgeIt e=bfs;
563        if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
564          dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
565        }
566        ++bfs;
567      } //computing distances from s in the residual graph
568
569      //Subgraph containing the edges on some shortest paths
570      typedef SubGraphWrapper<ResGW, DistanceMap<ResGW> > FilterResGW;
571      FilterResGW filter_res_graph(res_graph, dist);
572
573      //Subgraph, which is able to delete edges which are already
574      //met by the dfs
575      typename FilterResGW::NodeMap<typename FilterResGW::OutEdgeIt>
576        first_out_edges(filter_res_graph);
577      typename FilterResGW::NodeIt v;
578      for(filter_res_graph.first(v); filter_res_graph.valid(v);
579          filter_res_graph.next(v))
580      {
581        typename FilterResGW::OutEdgeIt e;
582        filter_res_graph.first(e, v);
583        first_out_edges.set(v, e);
584      }
585      typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
586        NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
587      ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
588
589      bool __augment=true;
590
591      while (__augment) {
592
593        __augment=false;
594        //computing blocking flow with dfs
595        typedef typename ErasingResGW::NodeMap<bool> BlockingReachedMap;
596        DfsIterator5< ErasingResGW, BlockingReachedMap >
597          dfs(erasing_res_graph);
598        typename ErasingResGW::NodeMap<typename ErasingResGW::OutEdgeIt>
599          pred(erasing_res_graph);
600        pred.set(s, INVALID);
601        //invalid iterators for sources
602
603        typename ErasingResGW::NodeMap<Number> free(erasing_res_graph);
604
605        dfs.pushAndSetReached(s);
606        while (!dfs.finished()) {
607          ++dfs;
608          if (erasing_res_graph.valid(
609                /*typename ErasingResGW::OutEdgeIt*/(dfs)))
610          {
611            if (dfs.isBNodeNewlyReached()) {
612         
613              typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
614              typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
615
616              pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
617              if (erasing_res_graph.valid(pred[v])) {
618                free.set(w, std::min(free[v], res_graph.resCap(dfs)));
619              } else {
620                free.set(w, res_graph.resCap(dfs));
621              }
622             
623              if (w==t) {
624                __augment=true;
625                _augment=true;
626                break;
627              }
628            } else {
629              erasing_res_graph.erase(dfs);
630            }
631          }
632        }       
633
634        if (__augment) {
635          typename ErasingResGW::Node n=t;
636          Number augment_value=free[n];
637          while (erasing_res_graph.valid(pred[n])) {
638            typename ErasingResGW::OutEdgeIt e=pred[n];
639            res_graph.augment(e, augment_value);
640            n=erasing_res_graph.tail(e);
641            if (res_graph.resCap(e)==0)
642              erasing_res_graph.erase(e);
643          }
644        }
645     
646      } //while (__augment)
647           
648      return _augment;
649    }
650
651    void run() {
652      //int num_of_augmentations=0;
653      while (augmentOnShortestPath()) {
654        //while (augmentOnBlockingFlow<MutableGraph>()) {
655        //std::cout << ++num_of_augmentations << " ";
656        //std::cout<<std::endl;
657      }
658    }
659
660    template<typename MutableGraph> void run() {
661      //int num_of_augmentations=0;
662      //while (augmentOnShortestPath()) {
663        while (augmentOnBlockingFlow<MutableGraph>()) {
664        //std::cout << ++num_of_augmentations << " ";
665        //std::cout<<std::endl;
666      }
667    }
668
669    Number flowValue() {
670      Number a=0;
671      OutEdgeIt e;
672      for(g->first(e, s); g->valid(e); g->next(e)) {
673        a+=(*flow)[e];
674      }
675      return a;
676    }
677
678  };
679
680
681//   template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
682//   class MaxMatching {
683//   public:
684//     typedef typename Graph::Node Node;
685//     typedef typename Graph::NodeIt NodeIt;
686//     typedef typename Graph::Edge Edge;
687//     typedef typename Graph::EdgeIt EdgeIt;
688//     typedef typename Graph::OutEdgeIt OutEdgeIt;
689//     typedef typename Graph::InEdgeIt InEdgeIt;
690
691//     typedef typename Graph::NodeMap<bool> SMap;
692//     typedef typename Graph::NodeMap<bool> TMap;
693//   private:
694//     const Graph* G;
695//     SMap* S;
696//     TMap* T;
697//     //Node s;
698//     //Node t;
699//     FlowMap* flow;
700//     const CapacityMap* capacity;
701//     typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
702//     typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
703//     typedef typename AugGraph::Edge AugEdge;
704//     typename Graph::NodeMap<int> used; //0
705
706//   public:
707//     MaxMatching(const Graph& _G, SMap& _S, TMap& _T, FlowMap& _flow, const CapacityMap& _capacity) :
708//       G(&_G), S(&_S), T(&_T), flow(&_flow), capacity(&_capacity), used(_G) { }
709//     bool augmentOnShortestPath() {
710//       AugGraph res_graph(*G, *flow, *capacity);
711//       bool _augment=false;
712     
713//       typedef typename AugGraph::NodeMap<bool> ReachedMap;
714//       BfsIterator5< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > bfs(res_graph);
715//       typename AugGraph::NodeMap<AugEdge> pred(res_graph);
716//       for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
717//      if ((S->get(s)) && (used.get(s)<1) ) {
718//        //Number u=0;
719//        //for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
720//        //u+=flow->get(e);
721//        //if (u<1) {
722//          bfs.pushAndSetReached(s);
723//          pred.set(s, AugEdge(INVALID));
724//          //}
725//      }
726//       }
727     
728//       typename AugGraph::NodeMap<Number> free(res_graph);
729       
730//       Node n;
731//       //searching for augmenting path
732//       while ( !bfs.finished() ) {
733//      AugOutEdgeIt e=bfs;
734//      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
735//        Node v=res_graph.tail(e);
736//        Node w=res_graph.head(e);
737//        pred.set(w, e);
738//        if (res_graph.valid(pred.get(v))) {
739//          free.set(w, std::min(free.get(v), res_graph.free(e)));
740//        } else {
741//          free.set(w, res_graph.free(e));
742//        }
743//        n=res_graph.head(e);
744//        if (T->get(n) && (used.get(n)<1) ) {
745//          //Number u=0;
746//          //for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
747//          //u+=flow->get(f);
748//          //if (u<1) {
749//            _augment=true;
750//            break;
751//            //}
752//        }
753//      }
754       
755//      ++bfs;
756//       } //end of searching augmenting path
757
758//       if (_augment) {
759//      //Node n=t;
760//      used.set(n, 1); //mind2 vegen jav
761//      Number augment_value=free.get(n);
762//      while (res_graph.valid(pred.get(n))) {
763//        AugEdge e=pred.get(n);
764//        res_graph.augment(e, augment_value);
765//        n=res_graph.tail(e);
766//      }
767//      used.set(n, 1); //mind2 vegen jav
768//       }
769
770//       return _augment;
771//     }
772
773// //     template<typename MutableGraph> bool augmentOnBlockingFlow() {     
774// //       bool _augment=false;
775
776// //       AugGraph res_graph(*G, *flow, *capacity);
777
778// //       typedef typename AugGraph::NodeMap<bool> ReachedMap;
779// //       BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
780
781
782
783
784
785// //       //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
786// //       for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
787// //   if (S->get(s)) {
788// //     Number u=0;
789// //     for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
790// //       u+=flow->get(e);
791// //     if (u<1) {
792// //       bfs.pushAndSetReached(s);
793// //       //pred.set(s, AugEdge(INVALID));
794// //     }
795// //   }
796// //       }
797
798
799
800
801// //       //bfs.pushAndSetReached(s);
802// //       typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
803// //       while ( !bfs.finished() ) {
804// //   AugOutEdgeIt e=bfs;
805// //   if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
806// //     dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
807// //   }
808       
809// //   ++bfs;
810// //       } //computing distances from s in the residual graph
811
812// //       MutableGraph F;
813// //       typename AugGraph::NodeMap<typename MutableGraph::Node>
814// //   res_graph_to_F(res_graph);
815// //       for(typename AugGraph::NodeIt n=res_graph.template first<typename AugGraph::NodeIt>(); res_graph.valid(n); res_graph.next(n)) {
816// //   res_graph_to_F.set(n, F.addNode());
817// //       }
818     
819// //       typename MutableGraph::Node sF=res_graph_to_F.get(s);
820// //       typename MutableGraph::Node tF=res_graph_to_F.get(t);
821
822// //       typename MutableGraph::EdgeMap<AugEdge> original_edge(F);
823// //       typename MutableGraph::EdgeMap<Number> residual_capacity(F);
824
825// //       //Making F to the graph containing the edges of the residual graph
826// //       //which are in some shortest paths
827// //       for(typename AugGraph::EdgeIt e=res_graph.template first<typename AugGraph::EdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
828// //   if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
829// //     typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
830// //     original_edge.update();
831// //     original_edge.set(f, e);
832// //     residual_capacity.update();
833// //     residual_capacity.set(f, res_graph.free(e));
834// //   }
835// //       }
836
837// //       bool __augment=true;
838
839// //       while (__augment) {
840// //   __augment=false;
841// //   //computing blocking flow with dfs
842// //   typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
843// //   DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
844// //   typename MutableGraph::NodeMap<typename MutableGraph::Edge> pred(F);
845// //   pred.set(sF, typename MutableGraph::Edge(INVALID));
846// //   //invalid iterators for sources
847
848// //   typename MutableGraph::NodeMap<Number> free(F);
849
850// //   dfs.pushAndSetReached(sF);     
851// //   while (!dfs.finished()) {
852// //     ++dfs;
853// //     if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
854// //       if (dfs.isBNodeNewlyReached()) {
855// //         typename MutableGraph::Node v=F.aNode(dfs);
856// //         typename MutableGraph::Node w=F.bNode(dfs);
857// //         pred.set(w, dfs);
858// //         if (F.valid(pred.get(v))) {
859// //           free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
860// //         } else {
861// //           free.set(w, residual_capacity.get(dfs));
862// //         }
863// //         if (w==tF) {
864// //           __augment=true;
865// //           _augment=true;
866// //           break;
867// //         }
868             
869// //       } else {
870// //         F.erase(typename MutableGraph::OutEdgeIt(dfs));
871// //       }
872// //     }
873// //   }
874
875// //   if (__augment) {
876// //     typename MutableGraph::Node n=tF;
877// //     Number augment_value=free.get(tF);
878// //     while (F.valid(pred.get(n))) {
879// //       typename MutableGraph::Edge e=pred.get(n);
880// //       res_graph.augment(original_edge.get(e), augment_value);
881// //       n=F.tail(e);
882// //       if (residual_capacity.get(e)==augment_value)
883// //         F.erase(e);
884// //       else
885// //         residual_capacity.set(e, residual_capacity.get(e)-augment_value);
886// //     }
887// //   }
888       
889// //       }
890           
891// //       return _augment;
892// //     }
893//     bool augmentOnBlockingFlow2() {
894//       bool _augment=false;
895
896//       //typedef ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> EAugGraph;
897//       typedef FilterGraphWrapper< ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > EAugGraph;
898//       typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt;
899//       typedef typename EAugGraph::Edge EAugEdge;
900
901//       EAugGraph res_graph(*G, *flow, *capacity);
902
903//       //typedef typename EAugGraph::NodeMap<bool> ReachedMap;
904//       BfsIterator5<
905//      ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>,
906//      /*typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt,*/
907//      ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<bool> > bfs(res_graph);
908
909
910//       //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
911//       for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
912//      if (S->get(s)) {
913//        Number u=0;
914//        for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
915//          u+=flow->get(e);
916//        if (u<1) {
917//          bfs.pushAndSetReached(s);
918//          //pred.set(s, AugEdge(INVALID));
919//        }
920//      }
921//       }
922
923     
924//       //bfs.pushAndSetReached(s);
925
926//       typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::
927//      NodeMap<int>& dist=res_graph.dist;
928
929//       while ( !bfs.finished() ) {
930//      typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt e=bfs;
931//      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
932//        dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
933//      }
934//      ++bfs; 
935//       } //computing distances from s in the residual graph
936
937//       bool __augment=true;
938
939//       while (__augment) {
940
941//      __augment=false;
942//      //computing blocking flow with dfs
943//      typedef typename EAugGraph::NodeMap<bool> BlockingReachedMap;
944//      DfsIterator5< EAugGraph/*, EAugOutEdgeIt*/, BlockingReachedMap >
945//        dfs(res_graph);
946//      typename EAugGraph::NodeMap<EAugEdge> pred(res_graph, INVALID);
947//      //pred.set(s, EAugEdge(INVALID));
948//      //invalid iterators for sources
949
950//      typename EAugGraph::NodeMap<Number> free(res_graph);
951
952
953//      //typename AugGraph::NodeMap<AugEdge> pred(res_graph);
954//       for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
955//      if (S->get(s)) {
956//        Number u=0;
957//        for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
958//          u+=flow->get(e);
959//        if (u<1) {
960//          dfs.pushAndSetReached(s);
961//          //pred.set(s, AugEdge(INVALID));
962//        }
963//      }
964//       }
965
966
967
968//       //dfs.pushAndSetReached(s);
969//       typename EAugGraph::Node n;
970//      while (!dfs.finished()) {
971//        ++dfs;
972//        if (res_graph.valid(EAugOutEdgeIt(dfs))) {
973//          if (dfs.isBNodeNewlyReached()) {
974         
975//            typename EAugGraph::Node v=res_graph.aNode(dfs);
976//            typename EAugGraph::Node w=res_graph.bNode(dfs);
977
978//            pred.set(w, EAugOutEdgeIt(dfs));
979//            if (res_graph.valid(pred.get(v))) {
980//              free.set(w, std::min(free.get(v), res_graph.free(dfs)));
981//            } else {
982//              free.set(w, res_graph.free(dfs));
983//            }
984             
985//            n=w;
986//            if (T->get(w)) {
987//              Number u=0;
988//              for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
989//                u+=flow->get(f);
990//              if (u<1) {
991//                __augment=true;
992//                _augment=true;
993//                break;
994//              }
995//            }
996//          } else {
997//            res_graph.erase(dfs);
998//          }
999//        }
1000
1001//      }
1002
1003//      if (__augment) {
1004//        // typename EAugGraph::Node n=t;
1005//        Number augment_value=free.get(n);
1006//        while (res_graph.valid(pred.get(n))) {
1007//          EAugEdge e=pred.get(n);
1008//          res_graph.augment(e, augment_value);
1009//          n=res_graph.tail(e);
1010//          if (res_graph.free(e)==0)
1011//            res_graph.erase(e);
1012//        }
1013//      }
1014     
1015//       }
1016           
1017//       return _augment;
1018//     }
1019//     void run() {
1020//       //int num_of_augmentations=0;
1021//       while (augmentOnShortestPath()) {
1022//      //while (augmentOnBlockingFlow<MutableGraph>()) {
1023//      //std::cout << ++num_of_augmentations << " ";
1024//      //std::cout<<std::endl;
1025//       }
1026//     }
1027// //     template<typename MutableGraph> void run() {
1028// //       //int num_of_augmentations=0;
1029// //       //while (augmentOnShortestPath()) {
1030// //   while (augmentOnBlockingFlow<MutableGraph>()) {
1031// //   //std::cout << ++num_of_augmentations << " ";
1032// //   //std::cout<<std::endl;
1033// //       }
1034// //     }
1035//     Number flowValue() {
1036//       Number a=0;
1037//       EdgeIt e;
1038//       for(G->/*getF*/first(e); G->valid(e); G->next(e)) {
1039//      a+=flow->get(e);
1040//       }
1041//       return a;
1042//     }
1043//   };
1044
1045
1046
1047
1048
1049 
1050// //   template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
1051// //   class MaxFlow2 {
1052// //   public:
1053// //     typedef typename Graph::Node Node;
1054// //     typedef typename Graph::Edge Edge;
1055// //     typedef typename Graph::EdgeIt EdgeIt;
1056// //     typedef typename Graph::OutEdgeIt OutEdgeIt;
1057// //     typedef typename Graph::InEdgeIt InEdgeIt;
1058// //   private:
1059// //     const Graph& G;
1060// //     std::list<Node>& S;
1061// //     std::list<Node>& T;
1062// //     FlowMap& flow;
1063// //     const CapacityMap& capacity;
1064// //     typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
1065// //     typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
1066// //     typedef typename AugGraph::Edge AugEdge;
1067// //     typename Graph::NodeMap<bool> SMap;
1068// //     typename Graph::NodeMap<bool> TMap;
1069// //   public:
1070// //     MaxFlow2(const Graph& _G, std::list<Node>& _S, std::list<Node>& _T, FlowMap& _flow, const CapacityMap& _capacity) : G(_G), S(_S), T(_T), flow(_flow), capacity(_capacity), SMap(_G), TMap(_G) {
1071// //       for(typename std::list<Node>::const_iterator i=S.begin();
1072// //     i!=S.end(); ++i) {
1073// //   SMap.set(*i, true);
1074// //       }
1075// //       for (typename std::list<Node>::const_iterator i=T.begin();
1076// //      i!=T.end(); ++i) {
1077// //   TMap.set(*i, true);
1078// //       }
1079// //     }
1080// //     bool augment() {
1081// //       AugGraph res_graph(G, flow, capacity);
1082// //       bool _augment=false;
1083// //       Node reached_t_node;
1084     
1085// //       typedef typename AugGraph::NodeMap<bool> ReachedMap;
1086// //       BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
1087// //       for(typename std::list<Node>::const_iterator i=S.begin();
1088// //     i!=S.end(); ++i) {
1089// //   bfs.pushAndSetReached(*i);
1090// //       }
1091// //       //bfs.pushAndSetReached(s);
1092       
1093// //       typename AugGraph::NodeMap<AugEdge> pred(res_graph);
1094// //       //filled up with invalid iterators
1095     
1096// //       typename AugGraph::NodeMap<Number> free(res_graph);
1097       
1098// //       //searching for augmenting path
1099// //       while ( !bfs.finished() ) {
1100// //   AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
1101// //   if (e.valid() && bfs.isBNodeNewlyReached()) {
1102// //     Node v=res_graph.tail(e);
1103// //     Node w=res_graph.head(e);
1104// //     pred.set(w, e);
1105// //     if (pred.get(v).valid()) {
1106// //       free.set(w, std::min(free.get(v), e.free()));
1107// //     } else {
1108// //       free.set(w, e.free());
1109// //     }
1110// //     if (TMap.get(res_graph.head(e))) {
1111// //       _augment=true;
1112// //       reached_t_node=res_graph.head(e);
1113// //       break;
1114// //     }
1115// //   }
1116       
1117// //   ++bfs;
1118// //       } //end of searching augmenting path
1119
1120// //       if (_augment) {
1121// //   Node n=reached_t_node;
1122// //   Number augment_value=free.get(reached_t_node);
1123// //   while (pred.get(n).valid()) {
1124// //     AugEdge e=pred.get(n);
1125// //     e.augment(augment_value);
1126// //     n=res_graph.tail(e);
1127// //   }
1128// //       }
1129
1130// //       return _augment;
1131// //     }
1132// //     void run() {
1133// //       while (augment()) { }
1134// //     }
1135// //     Number flowValue() {
1136// //       Number a=0;
1137// //       for(typename std::list<Node>::const_iterator i=S.begin();
1138// //     i!=S.end(); ++i) {
1139// //   for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
1140// //     a+=flow.get(e);
1141// //   }
1142// //   for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
1143// //     a-=flow.get(e);
1144// //   }
1145// //       }
1146// //       return a;
1147// //     }
1148// //   };
1149
1150
1151} // namespace hugo
1152
1153#endif //HUGO_EDMONDS_KARP_H
Note: See TracBrowser for help on using the repository browser.