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