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