src/work/edmonds_karp.hh
author jacint
Thu, 11 Mar 2004 23:31:13 +0000
changeset 173 de9849252e78
parent 155 8c6292ec54c6
permissions -rw-r--r--
*** empty log message ***
marci@59
     1
#ifndef EDMONDS_KARP_HH
marci@59
     2
#define EDMONDS_KARP_HH
marci@43
     3
marci@43
     4
#include <algorithm>
marci@75
     5
#include <list>
marci@75
     6
#include <iterator>
marci@43
     7
marci@43
     8
#include <bfs_iterator.hh>
marci@133
     9
//#include <time_measure.h>
marci@43
    10
alpar@107
    11
namespace hugo {
marci@43
    12
marci@75
    13
  template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@43
    14
  class ResGraph {
klao@127
    15
  public:
marci@43
    16
    typedef typename Graph::NodeIt NodeIt;
marci@43
    17
    typedef typename Graph::EachNodeIt EachNodeIt;
klao@127
    18
  private:
marci@43
    19
    typedef typename Graph::SymEdgeIt OldSymEdgeIt;
marci@43
    20
    const Graph& G;
marci@59
    21
    FlowMap& flow;
marci@59
    22
    const CapacityMap& capacity;
marci@43
    23
  public:
marci@59
    24
    ResGraph(const Graph& _G, FlowMap& _flow, 
marci@59
    25
	     const CapacityMap& _capacity) : 
marci@43
    26
      G(_G), flow(_flow), capacity(_capacity) { }
marci@43
    27
marci@43
    28
    class EdgeIt; 
marci@43
    29
    class OutEdgeIt; 
marci@43
    30
    friend class EdgeIt; 
marci@43
    31
    friend class OutEdgeIt; 
marci@43
    32
marci@43
    33
    class EdgeIt {
marci@75
    34
      friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
marci@43
    35
    protected:
marci@75
    36
      const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
marci@43
    37
      OldSymEdgeIt sym;
marci@43
    38
    public:
marci@43
    39
      EdgeIt() { } 
marci@43
    40
      //EdgeIt(const EdgeIt& e) : resG(e.resG), sym(e.sym) { }
marci@75
    41
      Number free() const { 
marci@43
    42
	if (resG->G.aNode(sym)==resG->G.tail(sym)) { 
marci@43
    43
	  return (resG->capacity.get(sym)-resG->flow.get(sym)); 
marci@43
    44
	} else { 
marci@43
    45
	  return (resG->flow.get(sym)); 
marci@43
    46
	}
marci@43
    47
      }
marci@43
    48
      bool valid() const { return sym.valid(); }
marci@75
    49
      void augment(Number a) const {
marci@43
    50
	if (resG->G.aNode(sym)==resG->G.tail(sym)) { 
marci@43
    51
	  resG->flow.set(sym, resG->flow.get(sym)+a);
marci@75
    52
	  //resG->flow[sym]+=a;
marci@43
    53
	} else { 
marci@43
    54
	  resG->flow.set(sym, resG->flow.get(sym)-a);
marci@75
    55
	  //resG->flow[sym]-=a;
marci@43
    56
	}
marci@43
    57
      }
marci@43
    58
    };
marci@43
    59
marci@43
    60
    class OutEdgeIt : public EdgeIt {
marci@75
    61
      friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
marci@43
    62
    public:
marci@43
    63
      OutEdgeIt() { }
marci@43
    64
      //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
marci@43
    65
    private:
marci@75
    66
      OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) { 
marci@43
    67
      	resG=&_resG;
marci@43
    68
	sym=resG->G.template first<OldSymEdgeIt>(v);
marci@43
    69
	while( sym.valid() && !(free()>0) ) { ++sym; }
marci@43
    70
      }
marci@43
    71
    public:
marci@43
    72
      OutEdgeIt& operator++() { 
marci@43
    73
	++sym; 
marci@43
    74
	while( sym.valid() && !(free()>0) ) { ++sym; }
marci@43
    75
	return *this; 
marci@43
    76
      }
marci@43
    77
    };
marci@43
    78
marci@64
    79
    void getFirst(OutEdgeIt& e, NodeIt v) const { 
marci@43
    80
      e=OutEdgeIt(*this, v); 
marci@43
    81
    }
marci@43
    82
    void getFirst(EachNodeIt& v) const { G.getFirst(v); }
marci@43
    83
    
marci@43
    84
    template< typename It >
marci@43
    85
    It first() const { 
marci@75
    86
      It e;      
marci@75
    87
      getFirst(e);
marci@75
    88
      return e; 
marci@75
    89
    }
marci@75
    90
marci@75
    91
    template< typename It >
marci@75
    92
    It first(NodeIt v) const { 
marci@75
    93
      It e;
marci@75
    94
      getFirst(e, v);
marci@75
    95
      return e; 
marci@75
    96
    }
marci@75
    97
marci@75
    98
    NodeIt tail(EdgeIt e) const { return G.aNode(e.sym); }
marci@75
    99
    NodeIt head(EdgeIt e) const { return G.bNode(e.sym); }
marci@75
   100
marci@75
   101
    NodeIt aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
marci@75
   102
    NodeIt bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
marci@75
   103
marci@75
   104
    int id(NodeIt v) const { return G.id(v); }
marci@75
   105
marci@75
   106
    template <typename S>
marci@75
   107
    class NodeMap {
marci@75
   108
      typename Graph::NodeMap<S> node_map; 
marci@75
   109
    public:
marci@75
   110
      NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
marci@75
   111
      NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
marci@75
   112
      void set(NodeIt nit, S a) { node_map.set(nit, a); }
marci@75
   113
      S get(NodeIt nit) const { return node_map.get(nit); }
marci@75
   114
      S& operator[](NodeIt nit) { return node_map[nit]; } 
marci@75
   115
      const S& operator[](NodeIt nit) const { return node_map[nit]; } 
marci@75
   116
    };
marci@75
   117
marci@75
   118
  };
marci@75
   119
marci@75
   120
marci@75
   121
  template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@75
   122
  class ResGraph2 {
klao@127
   123
  public:
marci@75
   124
    typedef typename Graph::NodeIt NodeIt;
marci@75
   125
    typedef typename Graph::EachNodeIt EachNodeIt;
klao@127
   126
  private:
marci@75
   127
    //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
marci@75
   128
    typedef typename Graph::OutEdgeIt OldOutEdgeIt;
marci@75
   129
    typedef typename Graph::InEdgeIt OldInEdgeIt;
marci@75
   130
    
marci@75
   131
    const Graph& G;
marci@75
   132
    FlowMap& flow;
marci@75
   133
    const CapacityMap& capacity;
marci@75
   134
  public:
marci@75
   135
    ResGraph2(const Graph& _G, FlowMap& _flow, 
marci@75
   136
	     const CapacityMap& _capacity) : 
marci@75
   137
      G(_G), flow(_flow), capacity(_capacity) { }
marci@75
   138
marci@75
   139
    class EdgeIt; 
marci@75
   140
    class OutEdgeIt; 
marci@75
   141
    friend class EdgeIt; 
marci@75
   142
    friend class OutEdgeIt; 
marci@75
   143
marci@75
   144
    class EdgeIt {
marci@75
   145
      friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
marci@75
   146
    protected:
marci@75
   147
      const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
marci@75
   148
      //OldSymEdgeIt sym;
marci@75
   149
      OldOutEdgeIt out;
marci@75
   150
      OldInEdgeIt in;
marci@148
   151
      bool out_or_in; //true, iff out
marci@75
   152
    public:
marci@148
   153
      EdgeIt() : out_or_in(true) { } 
marci@75
   154
      Number free() const { 
marci@75
   155
	if (out_or_in) { 
marci@75
   156
	  return (resG->capacity.get(out)-resG->flow.get(out)); 
marci@75
   157
	} else { 
marci@75
   158
	  return (resG->flow.get(in)); 
marci@75
   159
	}
marci@75
   160
      }
marci@75
   161
      bool valid() const { 
marci@75
   162
	return out_or_in && out.valid() || in.valid(); }
marci@75
   163
      void augment(Number a) const {
marci@75
   164
	if (out_or_in) { 
marci@75
   165
	  resG->flow.set(out, resG->flow.get(out)+a);
marci@75
   166
	} else { 
marci@75
   167
	  resG->flow.set(in, resG->flow.get(in)-a);
marci@75
   168
	}
marci@75
   169
      }
marci@75
   170
    };
marci@75
   171
marci@75
   172
    class OutEdgeIt : public EdgeIt {
marci@75
   173
      friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
marci@75
   174
    public:
marci@75
   175
      OutEdgeIt() { }
marci@75
   176
    private:
marci@75
   177
      OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) { 
marci@75
   178
      	resG=&_resG;
marci@75
   179
	out=resG->G.template first<OldOutEdgeIt>(v);
marci@75
   180
	while( out.valid() && !(free()>0) ) { ++out; }
marci@75
   181
	if (!out.valid()) {
marci@75
   182
	  out_or_in=0;
marci@75
   183
	  in=resG->G.template first<OldInEdgeIt>(v);
marci@75
   184
	  while( in.valid() && !(free()>0) ) { ++in; }
marci@75
   185
	}
marci@75
   186
      }
marci@75
   187
    public:
marci@75
   188
      OutEdgeIt& operator++() { 
marci@75
   189
	if (out_or_in) {
marci@75
   190
	  NodeIt v=resG->G.aNode(out);
marci@75
   191
	  ++out;
marci@75
   192
	  while( out.valid() && !(free()>0) ) { ++out; }
marci@75
   193
	  if (!out.valid()) {
marci@75
   194
	    out_or_in=0;
marci@75
   195
	    in=resG->G.template first<OldInEdgeIt>(v);
marci@75
   196
	    while( in.valid() && !(free()>0) ) { ++in; }
marci@75
   197
	  }
marci@75
   198
	} else {
marci@75
   199
	  ++in;
marci@75
   200
	  while( in.valid() && !(free()>0) ) { ++in; } 
marci@75
   201
	}
marci@75
   202
	return *this; 
marci@75
   203
      }
marci@75
   204
    };
marci@75
   205
marci@75
   206
    void getFirst(OutEdgeIt& e, NodeIt v) const { 
marci@75
   207
      e=OutEdgeIt(*this, v); 
marci@75
   208
    }
marci@75
   209
    void getFirst(EachNodeIt& v) const { G.getFirst(v); }
marci@75
   210
    
marci@75
   211
    template< typename It >
marci@75
   212
    It first() const { 
marci@43
   213
      It e;
marci@43
   214
      getFirst(e);
marci@43
   215
      return e; 
marci@43
   216
    }
marci@43
   217
marci@43
   218
    template< typename It >
marci@64
   219
    It first(NodeIt v) const { 
marci@43
   220
      It e;
marci@43
   221
      getFirst(e, v);
marci@43
   222
      return e; 
marci@43
   223
    }
marci@43
   224
marci@75
   225
    NodeIt tail(EdgeIt e) const { 
marci@75
   226
      return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
marci@75
   227
    NodeIt head(EdgeIt e) const { 
marci@75
   228
      return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
marci@43
   229
marci@75
   230
    NodeIt aNode(OutEdgeIt e) const { 
marci@75
   231
      return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
marci@75
   232
    NodeIt bNode(OutEdgeIt e) const { 
marci@75
   233
      return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
marci@43
   234
marci@64
   235
    int id(NodeIt v) const { return G.id(v); }
marci@43
   236
marci@69
   237
    template <typename S>
marci@43
   238
    class NodeMap {
marci@69
   239
      typename Graph::NodeMap<S> node_map; 
marci@43
   240
    public:
marci@75
   241
      NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
marci@75
   242
      NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
marci@75
   243
      void set(NodeIt nit, S a) { node_map.set(nit, a); }
marci@75
   244
      S get(NodeIt nit) const { return node_map.get(nit); }
marci@75
   245
    };
marci@75
   246
  };
marci@75
   247
marci@43
   248
marci@75
   249
  template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@59
   250
  class MaxFlow {
klao@127
   251
  public:
marci@43
   252
    typedef typename Graph::NodeIt NodeIt;
marci@43
   253
    typedef typename Graph::EdgeIt EdgeIt;
marci@43
   254
    typedef typename Graph::EachEdgeIt EachEdgeIt;
marci@43
   255
    typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@43
   256
    typedef typename Graph::InEdgeIt InEdgeIt;
klao@127
   257
klao@127
   258
  private:
marci@148
   259
    const Graph* G;
marci@64
   260
    NodeIt s;
marci@64
   261
    NodeIt t;
marci@148
   262
    FlowMap* flow;
marci@148
   263
    const CapacityMap* capacity;
marci@148
   264
    typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
marci@59
   265
    typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
marci@59
   266
    typedef typename AugGraph::EdgeIt AugEdgeIt;
marci@75
   267
marci@75
   268
    //AugGraph res_graph;    
marci@75
   269
    //typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@75
   270
    //typename AugGraph::NodeMap<AugEdgeIt> pred; 
marci@133
   271
    //typename AugGraph::NodeMap<Number> free;
marci@59
   272
  public:
marci@75
   273
    MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) : 
marci@148
   274
      G(&_G), s(_s), t(_t), flow(&_flow), capacity(&_capacity) //,  
marci@75
   275
      //res_graph(G, flow, capacity), pred(res_graph), free(res_graph) 
marci@75
   276
      { }
marci@133
   277
    bool augmentOnShortestPath() {
marci@148
   278
      AugGraph res_graph(*G, *flow, *capacity);
marci@59
   279
      bool _augment=false;
marci@59
   280
      
marci@59
   281
      typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@168
   282
      BfsIterator5< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > res_bfs(res_graph);
marci@59
   283
      res_bfs.pushAndSetReached(s);
marci@59
   284
	
marci@59
   285
      typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph); 
marci@75
   286
      //filled up with invalid iterators
marci@75
   287
      //pred.set(s, AugEdgeIt());
marci@59
   288
      
marci@133
   289
      typename AugGraph::NodeMap<Number> free(res_graph);
marci@59
   290
	
marci@59
   291
      //searching for augmenting path
marci@59
   292
      while ( !res_bfs.finished() ) { 
marci@141
   293
	AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
marci@148
   294
	if (res_graph.valid(e) && res_bfs.isBNodeNewlyReached()) {
marci@59
   295
	  NodeIt v=res_graph.tail(e);
marci@59
   296
	  NodeIt w=res_graph.head(e);
marci@59
   297
	  pred.set(w, e);
marci@148
   298
	  if (res_graph.valid(pred.get(v))) {
marci@168
   299
	    free.set(w, std::min(free.get(v), res_graph.free(e)));
marci@59
   300
	  } else {
marci@168
   301
	    free.set(w, res_graph.free(e)); 
marci@59
   302
	  }
marci@59
   303
	  if (res_graph.head(e)==t) { _augment=true; break; }
marci@59
   304
	}
marci@59
   305
	
marci@59
   306
	++res_bfs;
marci@59
   307
      } //end of searching augmenting path
marci@43
   308
marci@59
   309
      if (_augment) {
marci@59
   310
	NodeIt n=t;
marci@75
   311
	Number augment_value=free.get(t);
marci@148
   312
	while (res_graph.valid(pred.get(n))) { 
marci@59
   313
	  AugEdgeIt e=pred.get(n);
marci@168
   314
	  res_graph.augment(e, augment_value); 
marci@168
   315
	  //e.augment(augment_value); 
marci@59
   316
	  n=res_graph.tail(e);
marci@59
   317
	}
marci@59
   318
      }
marci@59
   319
marci@59
   320
      return _augment;
marci@59
   321
    }
marci@141
   322
marci@133
   323
    template<typename MutableGraph> bool augmentOnBlockingFlow() {
marci@133
   324
      bool _augment=false;
marci@133
   325
marci@148
   326
      AugGraph res_graph(*G, *flow, *capacity);
marci@133
   327
marci@133
   328
      typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@133
   329
      BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
marci@133
   330
marci@100
   331
      bfs.pushAndSetReached(s);
marci@133
   332
      typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
marci@100
   333
      while ( !bfs.finished() ) { 
marci@141
   334
	AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
marci@148
   335
	if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
marci@133
   336
	  dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
marci@100
   337
	}
marci@100
   338
	
marci@100
   339
	++bfs;
marci@133
   340
      } //computing distances from s in the residual graph
marci@100
   341
marci@100
   342
      MutableGraph F;
marci@133
   343
      typename AugGraph::NodeMap<NodeIt> res_graph_to_F(res_graph);
marci@148
   344
      for(typename AugGraph::EachNodeIt n=res_graph.template first<typename AugGraph::EachNodeIt>(); res_graph.valid(n); res_graph.next(n)) {
marci@133
   345
	res_graph_to_F.set(n, F.addNode());
marci@100
   346
      }
marci@133
   347
      
marci@133
   348
      typename MutableGraph::NodeIt sF=res_graph_to_F.get(s);
marci@133
   349
      typename MutableGraph::NodeIt tF=res_graph_to_F.get(t);
marci@133
   350
marci@133
   351
      typename MutableGraph::EdgeMap<AugEdgeIt> original_edge(F);
marci@148
   352
      typename MutableGraph::EdgeMap<Number> residual_capacity(F);
marci@133
   353
marci@133
   354
      //Making F to the graph containing the edges of the residual graph 
marci@133
   355
      //which are in some shortest paths
marci@148
   356
      for(typename AugGraph::EachEdgeIt e=res_graph.template first<typename AugGraph::EachEdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
marci@133
   357
	if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
marci@133
   358
	  typename MutableGraph::EdgeIt f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
marci@133
   359
	  original_edge.update();
marci@133
   360
	  original_edge.set(f, e);
marci@148
   361
	  residual_capacity.update();
marci@168
   362
	  residual_capacity.set(f, res_graph.free(e));
marci@133
   363
	} 
marci@133
   364
      }
marci@133
   365
marci@133
   366
      bool __augment=true;
marci@133
   367
marci@133
   368
      while (__augment) {
marci@133
   369
	__augment=false;
marci@133
   370
	//computing blocking flow with dfs
marci@133
   371
	typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
marci@133
   372
	DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
marci@133
   373
	typename MutableGraph::NodeMap<EdgeIt> pred(F); //invalid iterators
marci@133
   374
	typename MutableGraph::NodeMap<Number> free(F);
marci@133
   375
marci@133
   376
	dfs.pushAndSetReached(sF);      
marci@133
   377
	while (!dfs.finished()) {
marci@133
   378
	  ++dfs;
marci@148
   379
	  if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
marci@168
   380
	    if (dfs.isBNodeNewlyReached()) {
marci@168
   381
// 	      std::cout << "OutEdgeIt: " << dfs; 
marci@168
   382
// 	      std::cout << " aNode: " << F.aNode(dfs); 
marci@168
   383
// 	      std::cout << " bNode: " << F.bNode(dfs) << " ";
marci@133
   384
	  
marci@168
   385
	      typename MutableGraph::NodeIt v=F.aNode(dfs);
marci@168
   386
	      typename MutableGraph::NodeIt w=F.bNode(dfs);
marci@168
   387
	      pred.set(w, dfs);
marci@168
   388
	      if (F.valid(pred.get(v))) {
marci@168
   389
		free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
marci@168
   390
	      } else {
marci@168
   391
		free.set(w, residual_capacity.get(dfs)); 
marci@168
   392
	      }
marci@168
   393
	      if (w==tF) { 
marci@168
   394
		//std::cout << "AUGMENTATION"<<std::endl;
marci@168
   395
		__augment=true; 
marci@168
   396
		_augment=true;
marci@168
   397
		break; 
marci@168
   398
	      }
marci@168
   399
	      
marci@133
   400
	    } else {
marci@133
   401
	      F.erase(typename MutableGraph::OutEdgeIt(dfs));
marci@133
   402
	    }
marci@133
   403
	  } 
marci@100
   404
	}
marci@133
   405
marci@133
   406
	if (__augment) {
marci@133
   407
	  typename MutableGraph::NodeIt n=tF;
marci@133
   408
	  Number augment_value=free.get(tF);
marci@148
   409
	  while (F.valid(pred.get(n))) { 
marci@133
   410
	    typename MutableGraph::EdgeIt e=pred.get(n);
marci@168
   411
	    res_graph.augment(original_edge.get(e), augment_value); 
marci@168
   412
	    //original_edge.get(e).augment(augment_value); 
marci@133
   413
	    n=F.tail(e);
marci@148
   414
	    if (residual_capacity.get(e)==augment_value) 
marci@135
   415
	      F.erase(e); 
marci@135
   416
	    else 
marci@148
   417
	      residual_capacity.set(e, residual_capacity.get(e)-augment_value);
marci@133
   418
	  }
marci@133
   419
	}
marci@168
   420
	
marci@168
   421
      }
marci@168
   422
            
marci@168
   423
      return _augment;
marci@168
   424
    }
marci@168
   425
    bool augmentOnBlockingFlow2() {
marci@168
   426
      bool _augment=false;
marci@168
   427
marci@168
   428
      //typedef ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> EAugGraph;
marci@168
   429
      typedef FilterGraphWrapper< ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > EAugGraph;
marci@168
   430
      typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt;
marci@168
   431
      typedef typename EAugGraph::EdgeIt EAugEdgeIt;
marci@168
   432
marci@168
   433
      EAugGraph res_graph(*G, *flow, *capacity);
marci@168
   434
marci@168
   435
      //std::cout << "meg jo1" << std::endl;
marci@168
   436
marci@168
   437
      //typedef typename EAugGraph::NodeMap<bool> ReachedMap;
marci@168
   438
      BfsIterator4< 
marci@168
   439
	ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>, 
marci@168
   440
	ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt, 
marci@168
   441
	ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<bool> > bfs(res_graph);
marci@168
   442
      
marci@168
   443
      //std::cout << "meg jo2" << std::endl;
marci@168
   444
marci@168
   445
      bfs.pushAndSetReached(s);
marci@168
   446
      //std::cout << "meg jo2.5" << std::endl;
marci@168
   447
marci@168
   448
      //typename EAugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
marci@168
   449
      typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::
marci@168
   450
	NodeMap<int>& dist=res_graph.dist;
marci@168
   451
      //std::cout << "meg jo2.6" << std::endl;
marci@168
   452
marci@168
   453
      while ( !bfs.finished() ) {
marci@168
   454
	ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt e=bfs;
marci@168
   455
//	EAugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
marci@168
   456
 	//if (res_graph.valid(e)) {
marci@168
   457
 	//    std::cout<<"a:"<<res_graph.tail(e)<<"b:"<<res_graph.head(e)<<std::endl;
marci@168
   458
 	//}
marci@168
   459
	if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
marci@168
   460
	  dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
marci@168
   461
	}
marci@168
   462
	
marci@168
   463
	++bfs;	
marci@168
   464
      } //computing distances from s in the residual graph
marci@168
   465
marci@168
   466
marci@168
   467
      //std::cout << "meg jo3" << std::endl;
marci@168
   468
marci@168
   469
//       typedef typename EAugGraph::EachNodeIt EAugEachNodeIt;
marci@168
   470
//       for(EAugEachNodeIt n=res_graph.template first<EAugEachNodeIt>(); res_graph.valid(n); res_graph.next(n)) {
marci@168
   471
// 	std::cout << "dist: " << dist.get(n) << std::endl;
marci@168
   472
//       }
marci@168
   473
marci@168
   474
      bool __augment=true;
marci@168
   475
marci@168
   476
      while (__augment) {
marci@168
   477
//	std::cout << "new iteration"<< std::endl;
marci@168
   478
marci@168
   479
	__augment=false;
marci@168
   480
	//computing blocking flow with dfs
marci@168
   481
	typedef typename EAugGraph::NodeMap<bool> BlockingReachedMap;
marci@168
   482
	DfsIterator4< EAugGraph, EAugOutEdgeIt, BlockingReachedMap > 
marci@168
   483
	  dfs(res_graph);
marci@168
   484
	typename EAugGraph::NodeMap<EAugEdgeIt> pred(res_graph); //invalid iterators
marci@168
   485
	typename EAugGraph::NodeMap<Number> free(res_graph);
marci@168
   486
marci@168
   487
	dfs.pushAndSetReached(s);
marci@168
   488
	while (!dfs.finished()) {
marci@168
   489
	  ++dfs;
marci@168
   490
	  if (res_graph.valid(EAugOutEdgeIt(dfs))) { 
marci@168
   491
	    if (dfs.isBNodeNewlyReached()) {
marci@168
   492
// 	      std::cout << "OutEdgeIt: " << dfs; 
marci@168
   493
// 	      std::cout << " aNode: " << res_graph.aNode(dfs); 
marci@168
   494
// 	      std::cout << " res cap: " << EAugOutEdgeIt(dfs).free(); 
marci@168
   495
// 	      std::cout << " bNode: " << res_graph.bNode(dfs) << " ";
marci@168
   496
	  
marci@168
   497
	      typename EAugGraph::NodeIt v=res_graph.aNode(dfs);
marci@168
   498
	      typename EAugGraph::NodeIt w=res_graph.bNode(dfs);
marci@168
   499
marci@168
   500
	      pred.set(w, EAugOutEdgeIt(dfs));
marci@168
   501
marci@168
   502
	      //std::cout << EAugOutEdgeIt(dfs).free() << std::endl;
marci@168
   503
	      if (res_graph.valid(pred.get(v))) {
marci@168
   504
		free.set(w, std::min(free.get(v), res_graph.free(/*EAugOutEdgeIt*/(dfs))));
marci@168
   505
	      } else {
marci@168
   506
		free.set(w, res_graph.free(/*EAugOutEdgeIt*/(dfs))); 
marci@168
   507
	      }
marci@168
   508
	      
marci@168
   509
	      if (w==t) { 
marci@168
   510
//		std::cout << "t is reached, AUGMENTATION"<<std::endl;
marci@168
   511
		__augment=true; 
marci@168
   512
		_augment=true;
marci@168
   513
		break; 
marci@168
   514
	      }
marci@168
   515
	    } else {
marci@168
   516
//	      std::cout << "<<DELETE ";
marci@168
   517
//	      std::cout << " aNode: " << res_graph.aNode(dfs); 
marci@168
   518
//	      std::cout << " res cap: " << EAugOutEdgeIt(dfs).free(); 
marci@168
   519
//	      std::cout << " bNode: " << res_graph.bNode(dfs) << " ";
marci@168
   520
//	      std::cout << "DELETE>> ";
marci@168
   521
marci@168
   522
	      res_graph.erase(dfs);
marci@168
   523
	    }
marci@168
   524
	  } 
marci@168
   525
marci@168
   526
	}
marci@168
   527
marci@168
   528
	if (__augment) {
marci@168
   529
	  typename EAugGraph::NodeIt n=t;
marci@168
   530
	  Number augment_value=free.get(t);
marci@168
   531
//	  std::cout << "av:" << augment_value << std::endl;
marci@168
   532
	  while (res_graph.valid(pred.get(n))) { 
marci@168
   533
	    EAugEdgeIt e=pred.get(n);
marci@168
   534
	    res_graph.augment(e, augment_value);
marci@168
   535
	    //e.augment(augment_value); 
marci@168
   536
	    n=res_graph.tail(e);
marci@168
   537
	    if (res_graph.free(e)==0)
marci@168
   538
	      res_graph.erase(e);
marci@168
   539
	  }
marci@168
   540
	}
marci@135
   541
      
marci@100
   542
      }
marci@133
   543
            
marci@133
   544
      return _augment;
marci@100
   545
    }
marci@43
   546
    void run() {
marci@100
   547
      //int num_of_augmentations=0;
marci@133
   548
      while (augmentOnShortestPath()) { 
marci@133
   549
	//while (augmentOnBlockingFlow<MutableGraph>()) { 
marci@133
   550
	//std::cout << ++num_of_augmentations << " ";
marci@133
   551
	//std::cout<<std::endl;
marci@133
   552
      } 
marci@133
   553
    }
marci@133
   554
    template<typename MutableGraph> void run() {
marci@133
   555
      //int num_of_augmentations=0;
marci@133
   556
      //while (augmentOnShortestPath()) { 
marci@133
   557
	while (augmentOnBlockingFlow<MutableGraph>()) { 
marci@100
   558
	//std::cout << ++num_of_augmentations << " ";
marci@100
   559
	//std::cout<<std::endl;
marci@100
   560
      } 
marci@43
   561
    }
marci@75
   562
    Number flowValue() { 
marci@75
   563
      Number a=0;
marci@148
   564
      OutEdgeIt e;
marci@148
   565
      for(G->getFirst(e, s); G->valid(e); G->next(e)) {
marci@148
   566
	a+=flow->get(e);
marci@69
   567
      }
marci@69
   568
      return a;
marci@69
   569
    }
marci@43
   570
  };
marci@43
   571
marci@75
   572
  
marci@155
   573
//   template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@155
   574
//   class MaxFlow2 {
marci@155
   575
//   public:
marci@155
   576
//     typedef typename Graph::NodeIt NodeIt;
marci@155
   577
//     typedef typename Graph::EdgeIt EdgeIt;
marci@155
   578
//     typedef typename Graph::EachEdgeIt EachEdgeIt;
marci@155
   579
//     typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@155
   580
//     typedef typename Graph::InEdgeIt InEdgeIt;
marci@155
   581
//   private:
marci@155
   582
//     const Graph& G;
marci@155
   583
//     std::list<NodeIt>& S;
marci@155
   584
//     std::list<NodeIt>& T;
marci@155
   585
//     FlowMap& flow;
marci@155
   586
//     const CapacityMap& capacity;
marci@155
   587
//     typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
marci@155
   588
//     typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
marci@155
   589
//     typedef typename AugGraph::EdgeIt AugEdgeIt;
marci@155
   590
//     typename Graph::NodeMap<bool> SMap;
marci@155
   591
//     typename Graph::NodeMap<bool> TMap;
marci@155
   592
//   public:
marci@155
   593
//     MaxFlow2(const Graph& _G, std::list<NodeIt>& _S, std::list<NodeIt>& _T, FlowMap& _flow, const CapacityMap& _capacity) : G(_G), S(_S), T(_T), flow(_flow), capacity(_capacity), SMap(_G), TMap(_G) { 
marci@155
   594
//       for(typename std::list<NodeIt>::const_iterator i=S.begin(); 
marci@155
   595
// 	  i!=S.end(); ++i) { 
marci@155
   596
// 	SMap.set(*i, true); 
marci@155
   597
//       }
marci@155
   598
//       for (typename std::list<NodeIt>::const_iterator i=T.begin(); 
marci@155
   599
// 	   i!=T.end(); ++i) { 
marci@155
   600
// 	TMap.set(*i, true); 
marci@155
   601
//       }
marci@155
   602
//     }
marci@155
   603
//     bool augment() {
marci@155
   604
//       AugGraph res_graph(G, flow, capacity);
marci@155
   605
//       bool _augment=false;
marci@155
   606
//       NodeIt reached_t_node;
marci@75
   607
      
marci@155
   608
//       typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@155
   609
//       BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
marci@155
   610
//       for(typename std::list<NodeIt>::const_iterator i=S.begin(); 
marci@155
   611
// 	  i!=S.end(); ++i) {
marci@155
   612
// 	res_bfs.pushAndSetReached(*i);
marci@155
   613
//       }
marci@155
   614
//       //res_bfs.pushAndSetReached(s);
marci@75
   615
	
marci@155
   616
//       typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph); 
marci@155
   617
//       //filled up with invalid iterators
marci@75
   618
      
marci@155
   619
//       typename AugGraph::NodeMap<Number> free(res_graph);
marci@75
   620
	
marci@155
   621
//       //searching for augmenting path
marci@155
   622
//       while ( !res_bfs.finished() ) { 
marci@155
   623
// 	AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
marci@155
   624
// 	if (e.valid() && res_bfs.isBNodeNewlyReached()) {
marci@155
   625
// 	  NodeIt v=res_graph.tail(e);
marci@155
   626
// 	  NodeIt w=res_graph.head(e);
marci@155
   627
// 	  pred.set(w, e);
marci@155
   628
// 	  if (pred.get(v).valid()) {
marci@155
   629
// 	    free.set(w, std::min(free.get(v), e.free()));
marci@155
   630
// 	  } else {
marci@155
   631
// 	    free.set(w, e.free()); 
marci@155
   632
// 	  }
marci@155
   633
// 	  if (TMap.get(res_graph.head(e))) { 
marci@155
   634
// 	    _augment=true; 
marci@155
   635
// 	    reached_t_node=res_graph.head(e);
marci@155
   636
// 	    break; 
marci@155
   637
// 	  }
marci@155
   638
// 	}
marci@75
   639
	
marci@155
   640
// 	++res_bfs;
marci@155
   641
//       } //end of searching augmenting path
marci@75
   642
marci@155
   643
//       if (_augment) {
marci@155
   644
// 	NodeIt n=reached_t_node;
marci@155
   645
// 	Number augment_value=free.get(reached_t_node);
marci@155
   646
// 	while (pred.get(n).valid()) { 
marci@155
   647
// 	  AugEdgeIt e=pred.get(n);
marci@155
   648
// 	  e.augment(augment_value); 
marci@155
   649
// 	  n=res_graph.tail(e);
marci@155
   650
// 	}
marci@155
   651
//       }
marci@75
   652
marci@155
   653
//       return _augment;
marci@155
   654
//     }
marci@155
   655
//     void run() {
marci@155
   656
//       while (augment()) { } 
marci@155
   657
//     }
marci@155
   658
//     Number flowValue() { 
marci@155
   659
//       Number a=0;
marci@155
   660
//       for(typename std::list<NodeIt>::const_iterator i=S.begin(); 
marci@155
   661
// 	  i!=S.end(); ++i) { 
marci@155
   662
// 	for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
marci@155
   663
// 	  a+=flow.get(e);
marci@155
   664
// 	}
marci@155
   665
// 	for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
marci@155
   666
// 	  a-=flow.get(e);
marci@155
   667
// 	}
marci@155
   668
//       }
marci@155
   669
//       return a;
marci@155
   670
//     }
marci@155
   671
//   };
marci@75
   672
marci@75
   673
marci@75
   674
alpar@107
   675
} // namespace hugo
marci@43
   676
marci@59
   677
#endif //EDMONDS_KARP_HH