COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/marci/edmonds_karp.h @ 312:54e07057eb47

Last change on this file since 312:54e07057eb47 was 312:54e07057eb47, checked in by marci, 18 years ago

gw

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