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