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