src/work/edmonds_karp.hh
author marci
Wed, 03 Mar 2004 14:30:38 +0000
changeset 148 004fdf703abb
parent 141 a17d2a6462ee
child 155 8c6292ec54c6
permissions -rw-r--r--
G.next(...), G.valid(...), ...
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@75
   248
  template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@148
   249
  class ResGraphWrapper {
klao@127
   250
  public:
marci@75
   251
    typedef typename Graph::NodeIt NodeIt;
marci@75
   252
    typedef typename Graph::EachNodeIt EachNodeIt;
klao@127
   253
  private:
marci@75
   254
    typedef typename Graph::OutEdgeIt OldOutEdgeIt;
marci@75
   255
    typedef typename Graph::InEdgeIt OldInEdgeIt;
marci@148
   256
    const Graph* G;
marci@148
   257
    FlowMap* flow;
marci@148
   258
    const CapacityMap* capacity;
marci@75
   259
  public:
marci@148
   260
    ResGraphWrapper(const Graph& _G, FlowMap& _flow, 
marci@75
   261
	     const CapacityMap& _capacity) : 
marci@148
   262
      G(&_G), flow(&_flow), capacity(&_capacity) { }
marci@148
   263
//     ResGraphWrapper(const ResGraphWrapper& res_graph_wrapper) : 
marci@148
   264
//       G(res_graph_wrapper.G), flow(res_graph_wrapper.flow), capacity(res_graph_wrapper.capacity) { }
marci@75
   265
    class EdgeIt; 
marci@75
   266
    class OutEdgeIt; 
marci@75
   267
    friend class EdgeIt; 
marci@75
   268
    friend class OutEdgeIt; 
marci@75
   269
marci@75
   270
    class EdgeIt {
marci@148
   271
      friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
marci@75
   272
    protected:
marci@75
   273
      //const ResGraph3<Graph, Number, FlowMap, CapacityMap>* resG;
marci@75
   274
      const Graph* G;
marci@75
   275
      FlowMap* flow;
marci@75
   276
      const CapacityMap* capacity;
marci@75
   277
      //OldSymEdgeIt sym;
marci@75
   278
      OldOutEdgeIt out;
marci@75
   279
      OldInEdgeIt in;
marci@148
   280
      bool out_or_in; //true, iff out
marci@75
   281
    public:
marci@148
   282
      EdgeIt() : out_or_in(true) { } 
marci@148
   283
      EdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) : 
marci@148
   284
	G(&_G), flow(&_flow), capacity(&_capacity), out_or_in(true) { }
marci@133
   285
      //EdgeIt(const EdgeIt& e) : G(e.G), flow(e.flow), capacity(e.capacity), out(e.out), in(e.in), out_or_in(e.out_or_in) { }
marci@75
   286
      Number free() const { 
marci@75
   287
	if (out_or_in) { 
marci@75
   288
	  return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out)); 
marci@75
   289
	} else { 
marci@75
   290
	  return (/*resG->*/flow->get(in)); 
marci@75
   291
	}
marci@75
   292
      }
marci@75
   293
      bool valid() const { 
marci@75
   294
	return out_or_in && out.valid() || in.valid(); }
marci@75
   295
      void augment(Number a) const {
marci@75
   296
	if (out_or_in) { 
marci@75
   297
	  /*resG->*/flow->set(out, /*resG->*/flow->get(out)+a);
marci@75
   298
	} else { 
marci@75
   299
	  /*resG->*/flow->set(in, /*resG->*/flow->get(in)-a);
marci@75
   300
	}
marci@75
   301
      }
marci@133
   302
      void print() { 
marci@133
   303
	if (out_or_in) {
marci@133
   304
	  std::cout << "out "; 
marci@133
   305
	  if (out.valid()) 
marci@133
   306
	    std::cout << G->id(G->tail(out)) << "--"<< G->id(out) <<"->"<< G->id(G->head(out)); 
marci@133
   307
	  else 
marci@133
   308
	    std::cout << "invalid"; 
marci@133
   309
	}
marci@133
   310
	else {
marci@133
   311
	  std::cout << "in "; 
marci@133
   312
	  if (in.valid()) 
marci@133
   313
	    std::cout << G->id(G->head(in)) << "<-"<< G->id(in) <<"--"<< G->id(G->tail(in)); 
marci@133
   314
	  else 
marci@133
   315
	    std::cout << "invalid"; 
marci@133
   316
	}
marci@133
   317
	std::cout << std::endl;
marci@133
   318
      }
marci@75
   319
    };
marci@75
   320
marci@148
   321
    Number free(OldOutEdgeIt out) const { 
marci@148
   322
      return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out)); 
marci@148
   323
    }
marci@148
   324
    Number free(OldInEdgeIt in) const { 
marci@148
   325
      return (/*resG->*/flow->get(in)); 
marci@148
   326
    }
marci@148
   327
marci@75
   328
    class OutEdgeIt : public EdgeIt {
marci@148
   329
      friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
marci@75
   330
    public:
marci@75
   331
      OutEdgeIt() { }
marci@75
   332
    private:
marci@148
   333
      OutEdgeIt(const Graph& _G, NodeIt v, FlowMap& _flow, const CapacityMap& _capacity) : EdgeIt(_G, _flow, _capacity) { 
marci@133
   334
	//out=/*resG->*/G->template first<OldOutEdgeIt>(v);
marci@133
   335
	G->getFirst(out, v);
marci@148
   336
	while( out.valid() && !(EdgeIt::free()>0) ) { ++out; }
marci@75
   337
	if (!out.valid()) {
marci@75
   338
	  out_or_in=0;
marci@133
   339
	  //in=/*resG->*/G->template first<OldInEdgeIt>(v);
marci@133
   340
	  G->getFirst(in, v);
marci@148
   341
	  while( in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@75
   342
	}
marci@75
   343
      }
marci@75
   344
    public:
marci@75
   345
      OutEdgeIt& operator++() { 
marci@75
   346
	if (out_or_in) {
marci@75
   347
	  NodeIt v=/*resG->*/G->aNode(out);
marci@75
   348
	  ++out;
marci@148
   349
	  while( out.valid() && !(EdgeIt::free()>0) ) { ++out; }
marci@75
   350
	  if (!out.valid()) {
marci@75
   351
	    out_or_in=0;
marci@133
   352
	    G->getFirst(in, v); //=/*resG->*/G->template first<OldInEdgeIt>(v);
marci@148
   353
	    while( in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@75
   354
	  }
marci@75
   355
	} else {
marci@75
   356
	  ++in;
marci@148
   357
	  while( in.valid() && !(EdgeIt::free()>0) ) { ++in; } 
marci@75
   358
	}
marci@75
   359
	return *this; 
marci@75
   360
      }
marci@75
   361
    };
marci@75
   362
marci@133
   363
    class EachEdgeIt : public EdgeIt {
marci@148
   364
      friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>;
marci@133
   365
      typename Graph::EachNodeIt v;
marci@133
   366
    public:
marci@133
   367
      EachEdgeIt() { }
marci@133
   368
      //EachEdgeIt(const EachEdgeIt& e) : EdgeIt(e), v(e.v) { }
marci@148
   369
      EachEdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) : EdgeIt(_G, _flow, _capacity) { 
marci@148
   370
	out_or_in=true;
marci@133
   371
	G->getFirst(v);
marci@133
   372
	if (v.valid()) G->getFirst(out, v); else out=OldOutEdgeIt();
marci@148
   373
	while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
marci@133
   374
	while (v.valid() && !out.valid()) { 
marci@133
   375
	  ++v; 
marci@133
   376
	  if (v.valid()) G->getFirst(out, v); 
marci@148
   377
	  while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
marci@133
   378
	}
marci@133
   379
	if (!out.valid()) {
marci@133
   380
	  out_or_in=0;
marci@133
   381
	  G->getFirst(v);
marci@133
   382
	  if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt();
marci@148
   383
	  while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@133
   384
	  while (v.valid() && !in.valid()) { 
marci@133
   385
	    ++v; 
marci@133
   386
	    if (v.valid()) G->getFirst(in, v); 
marci@148
   387
	    while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@133
   388
	  }
marci@133
   389
	}
marci@133
   390
      }
marci@133
   391
      EachEdgeIt& operator++() { 
marci@133
   392
	if (out_or_in) {
marci@133
   393
	  ++out;
marci@148
   394
	  while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
marci@133
   395
	  while (v.valid() && !out.valid()) { 
marci@133
   396
	    ++v; 
marci@133
   397
	    if (v.valid()) G->getFirst(out, v); 
marci@148
   398
	    while (out.valid() && !(EdgeIt::free()>0) ) { ++out; }
marci@133
   399
	  }
marci@133
   400
	  if (!out.valid()) {
marci@133
   401
	    out_or_in=0;
marci@133
   402
	    G->getFirst(v);
marci@133
   403
	    if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt();
marci@148
   404
	    while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@133
   405
	    while (v.valid() && !in.valid()) { 
marci@133
   406
	      ++v; 
marci@133
   407
	      if (v.valid()) G->getFirst(in, v); 
marci@148
   408
	      while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@133
   409
	    }  
marci@133
   410
	  }
marci@133
   411
	} else {
marci@133
   412
	  ++in;
marci@148
   413
	  while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@133
   414
	  while (v.valid() && !in.valid()) { 
marci@133
   415
	    ++v; 
marci@133
   416
	    if (v.valid()) G->getFirst(in, v); 
marci@148
   417
	    while (in.valid() && !(EdgeIt::free()>0) ) { ++in; }
marci@133
   418
	  }
marci@133
   419
	}
marci@133
   420
	return *this;
marci@133
   421
      }
marci@133
   422
    };
marci@133
   423
marci@148
   424
    void getFirst(EachNodeIt& v) const { G->getFirst(v); }
marci@75
   425
    void getFirst(OutEdgeIt& e, NodeIt v) const { 
marci@148
   426
      e=OutEdgeIt(*G, v, *flow, *capacity); 
marci@75
   427
    }
marci@133
   428
    void getFirst(EachEdgeIt& e) const { 
marci@148
   429
      e=EachEdgeIt(*G, *flow, *capacity); 
marci@133
   430
    }
marci@148
   431
   
marci@148
   432
    EachNodeIt& next(EachNodeIt& n) const { return G->next(n); }
marci@148
   433
marci@148
   434
    OutEdgeIt& next(OutEdgeIt& e) const { 
marci@148
   435
      if (e.out_or_in) {
marci@148
   436
	NodeIt v=G->aNode(e.out);
marci@148
   437
	++(e.out);
marci@148
   438
	while( G->valid(e.out) && !(e.free()>0) ) { ++(e.out); }
marci@148
   439
	if (!G->valid(e.out)) {
marci@148
   440
	  e.out_or_in=0;
marci@148
   441
	  G->getFirst(e.in, v); //=/*resG->*/G->template first<OldInEdgeIt>(v);
marci@148
   442
	  while( G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
marci@148
   443
	}
marci@148
   444
      } else {
marci@148
   445
	++(e.in);
marci@148
   446
	while( G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } 
marci@148
   447
      }
marci@148
   448
      return e;
marci@148
   449
    }
marci@148
   450
marci@148
   451
    EachEdgeIt& next(EachEdgeIt& e) const { 
marci@148
   452
      if (e.out_or_in) {
marci@148
   453
	++(e.out);
marci@148
   454
	while (G->valid(e.out) && !(e.free()>0) ) { ++(e.out); }
marci@148
   455
	  while (G->valid(e.v) && !G->valid(e.out)) { 
marci@148
   456
	    ++(e.v); 
marci@148
   457
	    if (G->valid(e.v)) G->getFirst(e.out, e.v); 
marci@148
   458
	    while (G->valid(e.out) && !(e.free()>0) ) { ++(e.out); }
marci@148
   459
	  }
marci@148
   460
	  if (!G->valid(e.out)) {
marci@148
   461
	    e.out_or_in=0;
marci@148
   462
	    G->getFirst(e.v);
marci@148
   463
	    if (G->valid(e.v)) G->getFirst(e.in, e.v); else e.in=OldInEdgeIt();
marci@148
   464
	    while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
marci@148
   465
	    while (G->valid(e.v) && !G->valid(e.in)) { 
marci@148
   466
	      ++(e.v); 
marci@148
   467
	      if (G->valid(e.v)) G->getFirst(e.in, e.v); 
marci@148
   468
	      while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
marci@148
   469
	    }  
marci@148
   470
	  }
marci@148
   471
	} else {
marci@148
   472
	  ++(e.in);
marci@148
   473
	  while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
marci@148
   474
	  while (G->valid(e.v) && !G->valid(e.in)) { 
marci@148
   475
	    ++(e.v); 
marci@148
   476
	    if (G->valid(e.v)) G->getFirst(e.in, e.v); 
marci@148
   477
	    while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); }
marci@148
   478
	  }
marci@148
   479
	}
marci@148
   480
	return e;
marci@148
   481
      }
marci@75
   482
    
marci@148
   483
marci@75
   484
    template< typename It >
marci@75
   485
    It first() const { 
marci@75
   486
      It e;
marci@75
   487
      getFirst(e);
marci@75
   488
      return e; 
marci@75
   489
    }
marci@75
   490
marci@75
   491
    template< typename It >
marci@75
   492
    It first(NodeIt v) const { 
marci@75
   493
      It e;
marci@75
   494
      getFirst(e, v);
marci@75
   495
      return e; 
marci@75
   496
    }
marci@75
   497
marci@75
   498
    NodeIt tail(EdgeIt e) const { 
marci@148
   499
      return ((e.out_or_in) ? G->aNode(e.out) : G->aNode(e.in)); }
marci@75
   500
    NodeIt head(EdgeIt e) const { 
marci@148
   501
      return ((e.out_or_in) ? G->bNode(e.out) : G->bNode(e.in)); }
marci@75
   502
marci@75
   503
    NodeIt aNode(OutEdgeIt e) const { 
marci@148
   504
      return ((e.out_or_in) ? G->aNode(e.out) : G->aNode(e.in)); }
marci@75
   505
    NodeIt bNode(OutEdgeIt e) const { 
marci@148
   506
      return ((e.out_or_in) ? G->bNode(e.out) : G->bNode(e.in)); }
marci@75
   507
marci@148
   508
    int id(NodeIt v) const { return G->id(v); }
marci@148
   509
marci@148
   510
    bool valid(NodeIt n) const { return G->valid(n); }
marci@148
   511
    bool valid(EdgeIt e) const { 
marci@148
   512
      return e.out_or_in ? G->valid(e.out) : G->valid(e.in); }
marci@75
   513
marci@137
   514
    template <typename T>
marci@75
   515
    class NodeMap {
marci@137
   516
      typename Graph::NodeMap<T> node_map; 
marci@75
   517
    public:
marci@148
   518
      NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(*(_G.G)) { }
marci@148
   519
      NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : node_map(*(_G.G), a) { }
marci@137
   520
      void set(NodeIt nit, T a) { node_map.set(nit, a); }
marci@137
   521
      T get(NodeIt nit) const { return node_map.get(nit); }
marci@137
   522
    };
marci@137
   523
marci@137
   524
    template <typename T>
marci@137
   525
    class EdgeMap {
marci@137
   526
      typename Graph::EdgeMap<T> forward_map, backward_map; 
marci@137
   527
    public:
marci@148
   528
      EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : forward_map(*(_G.G)), backward_map(*(_G.G)) { }
marci@148
   529
      EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : forward_map(*(_G.G), a), backward_map(*(_G.G), a) { }
marci@137
   530
      void set(EdgeIt e, T a) { 
marci@137
   531
	if (e.out_or_in) 
marci@137
   532
	  forward_map.set(e.out, a); 
marci@137
   533
	else 
marci@137
   534
	  backward_map.set(e.in, a); 
marci@137
   535
      }
marci@137
   536
      T get(EdgeIt e) { 
marci@137
   537
	if (e.out_or_in) 
marci@137
   538
	  return forward_map.get(e.out); 
marci@137
   539
	else 
marci@137
   540
	  return backward_map.get(e.in); 
marci@137
   541
      }
marci@43
   542
    };
marci@43
   543
marci@43
   544
  };
marci@43
   545
marci@75
   546
  template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@59
   547
  class MaxFlow {
klao@127
   548
  public:
marci@43
   549
    typedef typename Graph::NodeIt NodeIt;
marci@43
   550
    typedef typename Graph::EdgeIt EdgeIt;
marci@43
   551
    typedef typename Graph::EachEdgeIt EachEdgeIt;
marci@43
   552
    typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@43
   553
    typedef typename Graph::InEdgeIt InEdgeIt;
klao@127
   554
klao@127
   555
  private:
marci@148
   556
    const Graph* G;
marci@64
   557
    NodeIt s;
marci@64
   558
    NodeIt t;
marci@148
   559
    FlowMap* flow;
marci@148
   560
    const CapacityMap* capacity;
marci@148
   561
    typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
marci@59
   562
    typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
marci@59
   563
    typedef typename AugGraph::EdgeIt AugEdgeIt;
marci@75
   564
marci@75
   565
    //AugGraph res_graph;    
marci@75
   566
    //typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@75
   567
    //typename AugGraph::NodeMap<AugEdgeIt> pred; 
marci@133
   568
    //typename AugGraph::NodeMap<Number> free;
marci@59
   569
  public:
marci@75
   570
    MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) : 
marci@148
   571
      G(&_G), s(_s), t(_t), flow(&_flow), capacity(&_capacity) //,  
marci@75
   572
      //res_graph(G, flow, capacity), pred(res_graph), free(res_graph) 
marci@75
   573
      { }
marci@133
   574
    bool augmentOnShortestPath() {
marci@148
   575
      AugGraph res_graph(*G, *flow, *capacity);
marci@59
   576
      bool _augment=false;
marci@59
   577
      
marci@59
   578
      typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@148
   579
      BfsIterator5< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
marci@59
   580
      res_bfs.pushAndSetReached(s);
marci@59
   581
	
marci@59
   582
      typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph); 
marci@75
   583
      //filled up with invalid iterators
marci@75
   584
      //pred.set(s, AugEdgeIt());
marci@59
   585
      
marci@133
   586
      typename AugGraph::NodeMap<Number> free(res_graph);
marci@59
   587
	
marci@59
   588
      //searching for augmenting path
marci@59
   589
      while ( !res_bfs.finished() ) { 
marci@141
   590
	AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
marci@148
   591
	if (res_graph.valid(e) && res_bfs.isBNodeNewlyReached()) {
marci@59
   592
	  NodeIt v=res_graph.tail(e);
marci@59
   593
	  NodeIt w=res_graph.head(e);
marci@59
   594
	  pred.set(w, e);
marci@148
   595
	  if (res_graph.valid(pred.get(v))) {
marci@59
   596
	    free.set(w, std::min(free.get(v), e.free()));
marci@59
   597
	  } else {
marci@59
   598
	    free.set(w, e.free()); 
marci@59
   599
	  }
marci@59
   600
	  if (res_graph.head(e)==t) { _augment=true; break; }
marci@59
   601
	}
marci@59
   602
	
marci@59
   603
	++res_bfs;
marci@59
   604
      } //end of searching augmenting path
marci@43
   605
marci@59
   606
      if (_augment) {
marci@59
   607
	NodeIt n=t;
marci@75
   608
	Number augment_value=free.get(t);
marci@148
   609
	while (res_graph.valid(pred.get(n))) { 
marci@59
   610
	  AugEdgeIt e=pred.get(n);
marci@59
   611
	  e.augment(augment_value); 
marci@59
   612
	  n=res_graph.tail(e);
marci@59
   613
	}
marci@59
   614
      }
marci@59
   615
marci@59
   616
      return _augment;
marci@59
   617
    }
marci@141
   618
marci@133
   619
    template<typename MutableGraph> bool augmentOnBlockingFlow() {
marci@133
   620
      bool _augment=false;
marci@133
   621
marci@148
   622
      AugGraph res_graph(*G, *flow, *capacity);
marci@133
   623
marci@133
   624
      typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@133
   625
      BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
marci@133
   626
marci@100
   627
      bfs.pushAndSetReached(s);
marci@133
   628
      typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
marci@100
   629
      while ( !bfs.finished() ) { 
marci@141
   630
	AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
marci@148
   631
	if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
marci@133
   632
	  dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
marci@100
   633
	}
marci@100
   634
	
marci@100
   635
	++bfs;
marci@133
   636
      } //computing distances from s in the residual graph
marci@100
   637
marci@100
   638
      MutableGraph F;
marci@133
   639
      typename AugGraph::NodeMap<NodeIt> res_graph_to_F(res_graph);
marci@148
   640
      for(typename AugGraph::EachNodeIt n=res_graph.template first<typename AugGraph::EachNodeIt>(); res_graph.valid(n); res_graph.next(n)) {
marci@133
   641
	res_graph_to_F.set(n, F.addNode());
marci@100
   642
      }
marci@133
   643
      
marci@133
   644
      typename MutableGraph::NodeIt sF=res_graph_to_F.get(s);
marci@133
   645
      typename MutableGraph::NodeIt tF=res_graph_to_F.get(t);
marci@133
   646
marci@133
   647
      typename MutableGraph::EdgeMap<AugEdgeIt> original_edge(F);
marci@148
   648
      typename MutableGraph::EdgeMap<Number> residual_capacity(F);
marci@133
   649
marci@133
   650
      //Making F to the graph containing the edges of the residual graph 
marci@133
   651
      //which are in some shortest paths
marci@148
   652
      for(typename AugGraph::EachEdgeIt e=res_graph.template first<typename AugGraph::EachEdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
marci@133
   653
	if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
marci@133
   654
	  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
   655
	  original_edge.update();
marci@133
   656
	  original_edge.set(f, e);
marci@148
   657
	  residual_capacity.update();
marci@148
   658
	  residual_capacity.set(f, e.free());
marci@133
   659
	} 
marci@133
   660
      }
marci@133
   661
marci@133
   662
      bool __augment=true;
marci@133
   663
marci@133
   664
      while (__augment) {
marci@133
   665
	__augment=false;
marci@133
   666
	//computing blocking flow with dfs
marci@133
   667
	typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
marci@133
   668
	DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
marci@133
   669
	typename MutableGraph::NodeMap<EdgeIt> pred(F); //invalid iterators
marci@133
   670
	typename MutableGraph::NodeMap<Number> free(F);
marci@133
   671
marci@133
   672
	dfs.pushAndSetReached(sF);      
marci@133
   673
	while (!dfs.finished()) {
marci@133
   674
	  ++dfs;
marci@148
   675
	  if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
marci@133
   676
	    //std::cout << "OutEdgeIt: " << dfs; 
marci@133
   677
	    //std::cout << " aNode: " << F.aNode(dfs); 
marci@133
   678
	    //std::cout << " bNode: " << F.bNode(dfs) << " ";
marci@133
   679
	  
marci@133
   680
	    typename MutableGraph::NodeIt v=F.aNode(dfs);
marci@133
   681
	    typename MutableGraph::NodeIt w=F.bNode(dfs);
marci@133
   682
	    pred.set(w, dfs);
marci@148
   683
	    if (F.valid(pred.get(v))) {
marci@148
   684
	      free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
marci@133
   685
	    } else {
marci@148
   686
	      free.set(w, residual_capacity.get(dfs)/*original_edge.get(dfs).free()*/); 
marci@133
   687
	    }
marci@133
   688
	    if (w==tF) { 
marci@133
   689
	      //std::cout << "AUGMENTATION"<<std::endl;
marci@133
   690
	      __augment=true; 
marci@133
   691
	      _augment=true;
marci@133
   692
	      break; 
marci@133
   693
	    }
marci@133
   694
	  } else { 
marci@133
   695
	    //std::cout << "OutEdgeIt: " << "invalid"; 
marci@133
   696
	    //std::cout << " aNode: " << dfs.aNode(); 
marci@133
   697
	    //std::cout << " bNode: " << "invalid" << " ";
marci@133
   698
	  }
marci@133
   699
	  if (dfs.isBNodeNewlyReached()) { 
marci@133
   700
	    //std::cout << "bNodeIsNewlyReached ";
marci@133
   701
	  } else { 
marci@133
   702
	    //std::cout << "bNodeIsNotNewlyReached ";
marci@133
   703
	    if (typename MutableGraph::OutEdgeIt(dfs).valid()) {
marci@133
   704
	      //std::cout << "DELETE ";
marci@133
   705
	      F.erase(typename MutableGraph::OutEdgeIt(dfs));
marci@133
   706
	    }
marci@133
   707
	  } 
marci@133
   708
	  //if (dfs.isANodeExamined()) { 
marci@133
   709
	    //std::cout << "aNodeIsExamined ";
marci@133
   710
	    //} else { 
marci@133
   711
	    //std::cout << "aNodeIsNotExamined ";
marci@133
   712
	    //} 
marci@133
   713
	  //std::cout<<std::endl;
marci@100
   714
	}
marci@133
   715
marci@133
   716
	if (__augment) {
marci@133
   717
	  typename MutableGraph::NodeIt n=tF;
marci@133
   718
	  Number augment_value=free.get(tF);
marci@148
   719
	  while (F.valid(pred.get(n))) { 
marci@133
   720
	    typename MutableGraph::EdgeIt e=pred.get(n);
marci@133
   721
	    original_edge.get(e).augment(augment_value); 
marci@133
   722
	    n=F.tail(e);
marci@148
   723
	    if (residual_capacity.get(e)==augment_value) 
marci@135
   724
	      F.erase(e); 
marci@135
   725
	    else 
marci@148
   726
	      residual_capacity.set(e, residual_capacity.get(e)-augment_value);
marci@133
   727
	  }
marci@133
   728
	}
marci@135
   729
      
marci@100
   730
      }
marci@133
   731
            
marci@133
   732
      return _augment;
marci@100
   733
    }
marci@43
   734
    void run() {
marci@100
   735
      //int num_of_augmentations=0;
marci@133
   736
      while (augmentOnShortestPath()) { 
marci@133
   737
	//while (augmentOnBlockingFlow<MutableGraph>()) { 
marci@133
   738
	//std::cout << ++num_of_augmentations << " ";
marci@133
   739
	//std::cout<<std::endl;
marci@133
   740
      } 
marci@133
   741
    }
marci@133
   742
    template<typename MutableGraph> void run() {
marci@133
   743
      //int num_of_augmentations=0;
marci@133
   744
      //while (augmentOnShortestPath()) { 
marci@133
   745
	while (augmentOnBlockingFlow<MutableGraph>()) { 
marci@100
   746
	//std::cout << ++num_of_augmentations << " ";
marci@100
   747
	//std::cout<<std::endl;
marci@100
   748
      } 
marci@43
   749
    }
marci@75
   750
    Number flowValue() { 
marci@75
   751
      Number a=0;
marci@148
   752
      OutEdgeIt e;
marci@148
   753
      for(G->getFirst(e, s); G->valid(e); G->next(e)) {
marci@148
   754
	a+=flow->get(e);
marci@69
   755
      }
marci@69
   756
      return a;
marci@69
   757
    }
marci@43
   758
  };
marci@43
   759
marci@75
   760
  
marci@75
   761
  template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@75
   762
  class MaxFlow2 {
klao@127
   763
  public:
marci@75
   764
    typedef typename Graph::NodeIt NodeIt;
marci@75
   765
    typedef typename Graph::EdgeIt EdgeIt;
marci@75
   766
    typedef typename Graph::EachEdgeIt EachEdgeIt;
marci@75
   767
    typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@75
   768
    typedef typename Graph::InEdgeIt InEdgeIt;
klao@127
   769
  private:
marci@75
   770
    const Graph& G;
marci@75
   771
    std::list<NodeIt>& S;
marci@75
   772
    std::list<NodeIt>& T;
marci@75
   773
    FlowMap& flow;
marci@75
   774
    const CapacityMap& capacity;
marci@75
   775
    typedef ResGraph<Graph, Number, FlowMap, CapacityMap > AugGraph;
marci@75
   776
    typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
marci@75
   777
    typedef typename AugGraph::EdgeIt AugEdgeIt;
marci@75
   778
    typename Graph::NodeMap<bool> SMap;
marci@75
   779
    typename Graph::NodeMap<bool> TMap;
marci@75
   780
  public:
marci@75
   781
    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@75
   782
      for(typename std::list<NodeIt>::const_iterator i=S.begin(); 
marci@75
   783
	  i!=S.end(); ++i) { 
marci@75
   784
	SMap.set(*i, true); 
marci@75
   785
      }
marci@75
   786
      for (typename std::list<NodeIt>::const_iterator i=T.begin(); 
marci@75
   787
	   i!=T.end(); ++i) { 
marci@75
   788
	TMap.set(*i, true); 
marci@75
   789
      }
marci@75
   790
    }
marci@75
   791
    bool augment() {
marci@75
   792
      AugGraph res_graph(G, flow, capacity);
marci@75
   793
      bool _augment=false;
marci@75
   794
      NodeIt reached_t_node;
marci@75
   795
      
marci@75
   796
      typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@75
   797
      BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
marci@75
   798
      for(typename std::list<NodeIt>::const_iterator i=S.begin(); 
marci@75
   799
	  i!=S.end(); ++i) {
marci@75
   800
	res_bfs.pushAndSetReached(*i);
marci@75
   801
      }
marci@75
   802
      //res_bfs.pushAndSetReached(s);
marci@75
   803
	
marci@75
   804
      typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph); 
marci@75
   805
      //filled up with invalid iterators
marci@75
   806
      
marci@133
   807
      typename AugGraph::NodeMap<Number> free(res_graph);
marci@75
   808
	
marci@75
   809
      //searching for augmenting path
marci@75
   810
      while ( !res_bfs.finished() ) { 
marci@141
   811
	AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
marci@75
   812
	if (e.valid() && res_bfs.isBNodeNewlyReached()) {
marci@75
   813
	  NodeIt v=res_graph.tail(e);
marci@75
   814
	  NodeIt w=res_graph.head(e);
marci@75
   815
	  pred.set(w, e);
marci@75
   816
	  if (pred.get(v).valid()) {
marci@75
   817
	    free.set(w, std::min(free.get(v), e.free()));
marci@75
   818
	  } else {
marci@75
   819
	    free.set(w, e.free()); 
marci@75
   820
	  }
marci@75
   821
	  if (TMap.get(res_graph.head(e))) { 
marci@75
   822
	    _augment=true; 
marci@75
   823
	    reached_t_node=res_graph.head(e);
marci@75
   824
	    break; 
marci@75
   825
	  }
marci@75
   826
	}
marci@75
   827
	
marci@75
   828
	++res_bfs;
marci@75
   829
      } //end of searching augmenting path
marci@75
   830
marci@75
   831
      if (_augment) {
marci@75
   832
	NodeIt n=reached_t_node;
marci@75
   833
	Number augment_value=free.get(reached_t_node);
marci@75
   834
	while (pred.get(n).valid()) { 
marci@75
   835
	  AugEdgeIt e=pred.get(n);
marci@75
   836
	  e.augment(augment_value); 
marci@75
   837
	  n=res_graph.tail(e);
marci@75
   838
	}
marci@75
   839
      }
marci@75
   840
marci@75
   841
      return _augment;
marci@75
   842
    }
marci@75
   843
    void run() {
marci@75
   844
      while (augment()) { } 
marci@75
   845
    }
marci@75
   846
    Number flowValue() { 
marci@75
   847
      Number a=0;
marci@75
   848
      for(typename std::list<NodeIt>::const_iterator i=S.begin(); 
marci@75
   849
	  i!=S.end(); ++i) { 
marci@75
   850
	for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
marci@75
   851
	  a+=flow.get(e);
marci@75
   852
	}
marci@75
   853
	for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
marci@75
   854
	  a-=flow.get(e);
marci@75
   855
	}
marci@75
   856
      }
marci@75
   857
      return a;
marci@75
   858
    }
marci@75
   859
  };
marci@75
   860
marci@75
   861
marci@75
   862
alpar@107
   863
} // namespace hugo
marci@43
   864
marci@59
   865
#endif //EDMONDS_KARP_HH