src/work/edmonds_karp.hh
author klao
Thu, 26 Feb 2004 16:07:40 +0000
changeset 132 1ac27e476e25
parent 107 8d62f0072ff0
child 133 0631992fe7a1
permissions -rw-r--r--
makefile: tobb include konyvtar, jobb depend keszites
flow_test.cc: torolve, mert a depend-et megakasztja, igy gondolom nem is fordul
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@100
     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@75
   151
      bool out_or_in; //1, iff out
marci@75
   152
    public:
marci@75
   153
      EdgeIt() : out_or_in(1) { } 
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@75
   249
  class ResGraph3 {
klao@127
   250
  public:
marci@75
   251
    typedef typename Graph::NodeIt NodeIt;
marci@75
   252
    typedef typename Graph::EachNodeIt EachNodeIt;
klao@127
   253
klao@127
   254
  private:
marci@75
   255
    //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
marci@75
   256
    typedef typename Graph::OutEdgeIt OldOutEdgeIt;
marci@75
   257
    typedef typename Graph::InEdgeIt OldInEdgeIt;
marci@75
   258
    const Graph& G;
marci@75
   259
    FlowMap& flow;
marci@75
   260
    const CapacityMap& capacity;
marci@75
   261
  public:
marci@75
   262
    ResGraph3(const Graph& _G, FlowMap& _flow, 
marci@75
   263
	     const CapacityMap& _capacity) : 
marci@75
   264
      G(_G), flow(_flow), capacity(_capacity) { }
marci@75
   265
marci@75
   266
    class EdgeIt; 
marci@75
   267
    class OutEdgeIt; 
marci@75
   268
    friend class EdgeIt; 
marci@75
   269
    friend class OutEdgeIt; 
marci@75
   270
marci@75
   271
    class EdgeIt {
marci@75
   272
      friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>;
marci@75
   273
    protected:
marci@75
   274
      //const ResGraph3<Graph, Number, FlowMap, CapacityMap>* resG;
marci@75
   275
      const Graph* G;
marci@75
   276
      FlowMap* flow;
marci@75
   277
      const CapacityMap* capacity;
marci@75
   278
      //OldSymEdgeIt sym;
marci@75
   279
      OldOutEdgeIt out;
marci@75
   280
      OldInEdgeIt in;
marci@75
   281
      bool out_or_in; //1, iff out
marci@75
   282
    public:
marci@75
   283
      EdgeIt() : out_or_in(1) { } 
marci@75
   284
      Number free() const { 
marci@75
   285
	if (out_or_in) { 
marci@75
   286
	  return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out)); 
marci@75
   287
	} else { 
marci@75
   288
	  return (/*resG->*/flow->get(in)); 
marci@75
   289
	}
marci@75
   290
      }
marci@75
   291
      bool valid() const { 
marci@75
   292
	return out_or_in && out.valid() || in.valid(); }
marci@75
   293
      void augment(Number a) const {
marci@75
   294
	if (out_or_in) { 
marci@75
   295
	  /*resG->*/flow->set(out, /*resG->*/flow->get(out)+a);
marci@75
   296
	} else { 
marci@75
   297
	  /*resG->*/flow->set(in, /*resG->*/flow->get(in)-a);
marci@75
   298
	}
marci@75
   299
      }
marci@75
   300
    };
marci@75
   301
marci@75
   302
    class OutEdgeIt : public EdgeIt {
marci@75
   303
      friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>;
marci@75
   304
    public:
marci@75
   305
      OutEdgeIt() { }
marci@75
   306
    private:
marci@75
   307
      OutEdgeIt(const Graph& _G, NodeIt v, FlowMap& _flow, const CapacityMap& _capacity) { 
marci@75
   308
      	G=&_G;
marci@75
   309
	flow=&_flow;
marci@75
   310
	capacity=&_capacity;
marci@75
   311
	out=/*resG->*/G->template first<OldOutEdgeIt>(v);
marci@75
   312
	while( out.valid() && !(free()>0) ) { ++out; }
marci@75
   313
	if (!out.valid()) {
marci@75
   314
	  out_or_in=0;
marci@75
   315
	  in=/*resG->*/G->template first<OldInEdgeIt>(v);
marci@75
   316
	  while( in.valid() && !(free()>0) ) { ++in; }
marci@75
   317
	}
marci@75
   318
      }
marci@75
   319
    public:
marci@75
   320
      OutEdgeIt& operator++() { 
marci@75
   321
	if (out_or_in) {
marci@75
   322
	  NodeIt v=/*resG->*/G->aNode(out);
marci@75
   323
	  ++out;
marci@75
   324
	  while( out.valid() && !(free()>0) ) { ++out; }
marci@75
   325
	  if (!out.valid()) {
marci@75
   326
	    out_or_in=0;
marci@75
   327
	    in=/*resG->*/G->template first<OldInEdgeIt>(v);
marci@75
   328
	    while( in.valid() && !(free()>0) ) { ++in; }
marci@75
   329
	  }
marci@75
   330
	} else {
marci@75
   331
	  ++in;
marci@75
   332
	  while( in.valid() && !(free()>0) ) { ++in; } 
marci@75
   333
	}
marci@75
   334
	return *this; 
marci@75
   335
      }
marci@75
   336
    };
marci@75
   337
marci@75
   338
    void getFirst(OutEdgeIt& e, NodeIt v) const { 
marci@75
   339
      e=OutEdgeIt(G, v, flow, capacity); 
marci@75
   340
    }
marci@75
   341
    void getFirst(EachNodeIt& v) const { G.getFirst(v); }
marci@75
   342
    
marci@75
   343
    template< typename It >
marci@75
   344
    It first() const { 
marci@75
   345
      It e;
marci@75
   346
      getFirst(e);
marci@75
   347
      return e; 
marci@75
   348
    }
marci@75
   349
marci@75
   350
    template< typename It >
marci@75
   351
    It first(NodeIt v) const { 
marci@75
   352
      It e;
marci@75
   353
      getFirst(e, v);
marci@75
   354
      return e; 
marci@75
   355
    }
marci@75
   356
marci@75
   357
    NodeIt tail(EdgeIt e) const { 
marci@75
   358
      return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
marci@75
   359
    NodeIt head(EdgeIt e) const { 
marci@75
   360
      return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
marci@75
   361
marci@75
   362
    NodeIt aNode(OutEdgeIt e) const { 
marci@75
   363
      return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
marci@75
   364
    NodeIt bNode(OutEdgeIt e) const { 
marci@75
   365
      return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
marci@75
   366
marci@75
   367
    int id(NodeIt v) const { return G.id(v); }
marci@75
   368
marci@75
   369
    template <typename S>
marci@75
   370
    class NodeMap {
marci@75
   371
      typename Graph::NodeMap<S> node_map; 
marci@75
   372
    public:
marci@75
   373
      NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
marci@75
   374
      NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
marci@69
   375
      void set(NodeIt nit, S a) { node_map.set(nit, a); }
marci@69
   376
      S get(NodeIt nit) const { return node_map.get(nit); }
marci@43
   377
    };
marci@43
   378
marci@43
   379
  };
marci@43
   380
marci@75
   381
marci@75
   382
  template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@59
   383
  class MaxFlow {
klao@127
   384
  public:
marci@43
   385
    typedef typename Graph::NodeIt NodeIt;
marci@43
   386
    typedef typename Graph::EdgeIt EdgeIt;
marci@43
   387
    typedef typename Graph::EachEdgeIt EachEdgeIt;
marci@43
   388
    typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@43
   389
    typedef typename Graph::InEdgeIt InEdgeIt;
klao@127
   390
klao@127
   391
  private:
marci@43
   392
    const Graph& G;
marci@64
   393
    NodeIt s;
marci@64
   394
    NodeIt t;
marci@59
   395
    FlowMap& flow;
marci@59
   396
    const CapacityMap& capacity;
marci@75
   397
    typedef ResGraph3<Graph, Number, FlowMap, CapacityMap > AugGraph;
marci@59
   398
    typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
marci@59
   399
    typedef typename AugGraph::EdgeIt AugEdgeIt;
marci@75
   400
marci@75
   401
    //AugGraph res_graph;    
marci@75
   402
    //typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@75
   403
    //typename AugGraph::NodeMap<AugEdgeIt> pred; 
marci@75
   404
    //typename AugGraph::NodeMap<int> free;
marci@59
   405
  public:
marci@75
   406
    MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) : 
marci@75
   407
      G(_G), s(_s), t(_t), flow(_flow), capacity(_capacity) //,  
marci@75
   408
      //res_graph(G, flow, capacity), pred(res_graph), free(res_graph) 
marci@75
   409
      { }
marci@59
   410
    bool augment() {
marci@59
   411
      AugGraph res_graph(G, flow, capacity);
marci@59
   412
      bool _augment=false;
marci@59
   413
      
marci@59
   414
      typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@75
   415
      BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
marci@59
   416
      res_bfs.pushAndSetReached(s);
marci@59
   417
	
marci@59
   418
      typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph); 
marci@75
   419
      //filled up with invalid iterators
marci@75
   420
      //pred.set(s, AugEdgeIt());
marci@59
   421
      
marci@59
   422
      typename AugGraph::NodeMap<int> free(res_graph);
marci@59
   423
	
marci@59
   424
      //searching for augmenting path
marci@59
   425
      while ( !res_bfs.finished() ) { 
marci@59
   426
	AugOutEdgeIt e=AugOutEdgeIt(res_bfs);
marci@59
   427
	if (e.valid() && res_bfs.isBNodeNewlyReached()) {
marci@59
   428
	  NodeIt v=res_graph.tail(e);
marci@59
   429
	  NodeIt w=res_graph.head(e);
marci@59
   430
	  pred.set(w, e);
marci@59
   431
	  if (pred.get(v).valid()) {
marci@59
   432
	    free.set(w, std::min(free.get(v), e.free()));
marci@59
   433
	  } else {
marci@59
   434
	    free.set(w, e.free()); 
marci@59
   435
	  }
marci@59
   436
	  if (res_graph.head(e)==t) { _augment=true; break; }
marci@59
   437
	}
marci@59
   438
	
marci@59
   439
	++res_bfs;
marci@59
   440
      } //end of searching augmenting path
marci@43
   441
marci@59
   442
      if (_augment) {
marci@59
   443
	NodeIt n=t;
marci@75
   444
	Number augment_value=free.get(t);
marci@59
   445
	while (pred.get(n).valid()) { 
marci@59
   446
	  AugEdgeIt e=pred.get(n);
marci@59
   447
	  e.augment(augment_value); 
marci@59
   448
	  n=res_graph.tail(e);
marci@59
   449
	}
marci@59
   450
      }
marci@59
   451
marci@59
   452
      return _augment;
marci@59
   453
    }
marci@100
   454
    bool augmentWithBlockingFlow() {
marci@100
   455
      BfsIterator4< Graph, OutEdgeIt, typename Graph::NodeMap<bool> > bfs(G);
marci@100
   456
      bfs.pushAndSetReached(s);
marci@100
   457
      typename Graph::NodeMap<int> dist(G); //filled up with 0's
marci@100
   458
      while ( !bfs.finished() ) { 
marci@100
   459
	OutEdgeIt e=OutEdgeIt(bfs);
marci@100
   460
	if (e.valid() && bfs.isBNodeNewlyReached()) {
marci@100
   461
	  dist.set(G.head(e), dist.get(G.tail(e))+1);
marci@100
   462
	  //NodeIt v=res_graph.tail(e);
marci@100
   463
	  //NodeIt w=res_graph.head(e);
marci@100
   464
	  //pred.set(w, e);
marci@100
   465
	  //if (pred.get(v).valid()) {
marci@100
   466
	  //  free.set(w, std::min(free.get(v), e.free()));
marci@100
   467
	  //} else {
marci@100
   468
	  //  free.set(w, e.free()); 
marci@100
   469
	  //}
marci@100
   470
	  //if (res_graph.head(e)==t) { _augment=true; break; }
marci@100
   471
	}
marci@100
   472
	
marci@100
   473
	++bfs;
marci@100
   474
      } //end of searching augmenting path
marci@100
   475
marci@100
   476
      double pre_time_copy=currTime();
marci@100
   477
      typedef Graph MutableGraph;
marci@100
   478
      MutableGraph F;
marci@100
   479
      typename Graph::NodeMap<NodeIt> G_to_F(G);
marci@100
   480
      for(typename Graph::EachNodeIt n=G.template first<typename Graph::EachNodeIt>(); n.valid(); ++n) {
marci@100
   481
	G_to_F.set(n, F.addNode());
marci@100
   482
      }
marci@100
   483
      for(typename Graph::EachEdgeIt e=G.template first<typename Graph::EachEdgeIt>(); e.valid(); ++e) {
marci@100
   484
	if (dist.get(G.head(e))==dist.get(G.tail(e))+1) {
marci@100
   485
	  F.addEdge(G_to_F.get(G.tail(e)), G_to_F.get(G.head(e)));
marci@100
   486
	}
marci@100
   487
      }
marci@100
   488
      double post_time_copy=currTime();
marci@100
   489
      std::cout << "copy time: " << post_time_copy-pre_time_copy << " sec"<< std::endl; 
marci@100
   490
marci@100
   491
      return 0;
marci@100
   492
    }
marci@43
   493
    void run() {
marci@100
   494
      //int num_of_augmentations=0;
marci@100
   495
      while (augment()) { 
marci@100
   496
	//std::cout << ++num_of_augmentations << " ";
marci@100
   497
	//std::cout<<std::endl;
marci@100
   498
      } 
marci@43
   499
    }
marci@75
   500
    Number flowValue() { 
marci@75
   501
      Number a=0;
marci@69
   502
      for(OutEdgeIt i=G.template first<OutEdgeIt>(s); i.valid(); ++i) {
marci@69
   503
	a+=flow.get(i);
marci@69
   504
      }
marci@69
   505
      return a;
marci@69
   506
    }
marci@43
   507
  };
marci@43
   508
marci@75
   509
marci@75
   510
/*
marci@75
   511
  template <typename Graph>
marci@75
   512
  class IteratorBoolNodeMap {
marci@75
   513
    typedef typename Graph::NodeIt NodeIt;
marci@75
   514
    typedef typename Graph::EachNodeIt EachNodeIt;
marci@75
   515
    class BoolItem {
marci@75
   516
    public:
marci@75
   517
      bool value;
marci@75
   518
      NodeIt prev;
marci@75
   519
      NodeIt next;
marci@75
   520
      BoolItem() : value(false), prev(), next() {}
marci@75
   521
    };
marci@75
   522
    NodeIt first_true;
marci@75
   523
    //NodeIt last_true;
marci@75
   524
    NodeIt first_false;
marci@75
   525
    //NodeIt last_false;
marci@75
   526
    const Graph& G; 
marci@75
   527
    typename Graph::NodeMap<BoolItem> container;
marci@75
   528
  public:
marci@75
   529
    typedef bool ValueType;
marci@75
   530
    typedef NodeIt KeyType;
marci@75
   531
    IteratorBoolNodeMap(const Graph& _G) : G(_G), container(G), first_true(NodeIt()) { 
marci@75
   532
      //for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) {
marci@75
   533
      //BoolItem b=container.get(e);
marci@75
   534
      //b.me=e;
marci@75
   535
      //container.set(b);
marci@75
   536
      //} 
marci@75
   537
      G.getFirst(first_false);
marci@75
   538
      NodeIt e_prev;
marci@75
   539
      for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) {
marci@75
   540
	container[e].prev=e_prev;
marci@75
   541
	if (e_prev.valid()) container[e_prev].next=e;
marci@75
   542
	e_prev=e;
marci@75
   543
      }
marci@75
   544
    }
marci@75
   545
    //NodeMap(const Graph& _G, T a) : 
marci@75
   546
    //  G(_G), container(G.node_id, a) { }
marci@75
   547
    //FIXME
marci@75
   548
    void set(NodeIt nit, T a) { container[G.id(nit)]=a; }
marci@75
   549
    T get(NodeIt nit) const { return container[G.id(nit)]; }
alpar@107
   550
    //void update() { container.resize(G.node_id); }
alpar@107
   551
    //void update(T a) { container.resize(G.node_id, a); }
marci@75
   552
  };
marci@75
   553
*/
marci@75
   554
marci@75
   555
  
marci@75
   556
  template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@75
   557
  class MaxFlow2 {
klao@127
   558
  public:
marci@75
   559
    typedef typename Graph::NodeIt NodeIt;
marci@75
   560
    typedef typename Graph::EdgeIt EdgeIt;
marci@75
   561
    typedef typename Graph::EachEdgeIt EachEdgeIt;
marci@75
   562
    typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@75
   563
    typedef typename Graph::InEdgeIt InEdgeIt;
klao@127
   564
  private:
marci@75
   565
    const Graph& G;
marci@75
   566
    std::list<NodeIt>& S;
marci@75
   567
    std::list<NodeIt>& T;
marci@75
   568
    FlowMap& flow;
marci@75
   569
    const CapacityMap& capacity;
marci@75
   570
    typedef ResGraph<Graph, Number, FlowMap, CapacityMap > AugGraph;
marci@75
   571
    typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
marci@75
   572
    typedef typename AugGraph::EdgeIt AugEdgeIt;
marci@75
   573
    typename Graph::NodeMap<bool> SMap;
marci@75
   574
    typename Graph::NodeMap<bool> TMap;
marci@75
   575
  public:
marci@75
   576
    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
   577
      for(typename std::list<NodeIt>::const_iterator i=S.begin(); 
marci@75
   578
	  i!=S.end(); ++i) { 
marci@75
   579
	SMap.set(*i, true); 
marci@75
   580
      }
marci@75
   581
      for (typename std::list<NodeIt>::const_iterator i=T.begin(); 
marci@75
   582
	   i!=T.end(); ++i) { 
marci@75
   583
	TMap.set(*i, true); 
marci@75
   584
      }
marci@75
   585
    }
marci@75
   586
    bool augment() {
marci@75
   587
      AugGraph res_graph(G, flow, capacity);
marci@75
   588
      bool _augment=false;
marci@75
   589
      NodeIt reached_t_node;
marci@75
   590
      
marci@75
   591
      typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@75
   592
      BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
marci@75
   593
      for(typename std::list<NodeIt>::const_iterator i=S.begin(); 
marci@75
   594
	  i!=S.end(); ++i) {
marci@75
   595
	res_bfs.pushAndSetReached(*i);
marci@75
   596
      }
marci@75
   597
      //res_bfs.pushAndSetReached(s);
marci@75
   598
	
marci@75
   599
      typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph); 
marci@75
   600
      //filled up with invalid iterators
marci@75
   601
      
marci@75
   602
      typename AugGraph::NodeMap<int> free(res_graph);
marci@75
   603
	
marci@75
   604
      //searching for augmenting path
marci@75
   605
      while ( !res_bfs.finished() ) { 
marci@75
   606
	AugOutEdgeIt e=AugOutEdgeIt(res_bfs);
marci@75
   607
	if (e.valid() && res_bfs.isBNodeNewlyReached()) {
marci@75
   608
	  NodeIt v=res_graph.tail(e);
marci@75
   609
	  NodeIt w=res_graph.head(e);
marci@75
   610
	  pred.set(w, e);
marci@75
   611
	  if (pred.get(v).valid()) {
marci@75
   612
	    free.set(w, std::min(free.get(v), e.free()));
marci@75
   613
	  } else {
marci@75
   614
	    free.set(w, e.free()); 
marci@75
   615
	  }
marci@75
   616
	  if (TMap.get(res_graph.head(e))) { 
marci@75
   617
	    _augment=true; 
marci@75
   618
	    reached_t_node=res_graph.head(e);
marci@75
   619
	    break; 
marci@75
   620
	  }
marci@75
   621
	}
marci@75
   622
	
marci@75
   623
	++res_bfs;
marci@75
   624
      } //end of searching augmenting path
marci@75
   625
marci@75
   626
      if (_augment) {
marci@75
   627
	NodeIt n=reached_t_node;
marci@75
   628
	Number augment_value=free.get(reached_t_node);
marci@75
   629
	while (pred.get(n).valid()) { 
marci@75
   630
	  AugEdgeIt e=pred.get(n);
marci@75
   631
	  e.augment(augment_value); 
marci@75
   632
	  n=res_graph.tail(e);
marci@75
   633
	}
marci@75
   634
      }
marci@75
   635
marci@75
   636
      return _augment;
marci@75
   637
    }
marci@75
   638
    void run() {
marci@75
   639
      while (augment()) { } 
marci@75
   640
    }
marci@75
   641
    Number flowValue() { 
marci@75
   642
      Number a=0;
marci@75
   643
      for(typename std::list<NodeIt>::const_iterator i=S.begin(); 
marci@75
   644
	  i!=S.end(); ++i) { 
marci@75
   645
	for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
marci@75
   646
	  a+=flow.get(e);
marci@75
   647
	}
marci@75
   648
	for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
marci@75
   649
	  a-=flow.get(e);
marci@75
   650
	}
marci@75
   651
      }
marci@75
   652
      return a;
marci@75
   653
    }
marci@75
   654
  };
marci@75
   655
marci@75
   656
marci@75
   657
alpar@107
   658
} // namespace hugo
marci@43
   659
marci@59
   660
#endif //EDMONDS_KARP_HH