src/work/athos/preflow_push.hh
author jacint
Sun, 22 Feb 2004 12:17:16 +0000
changeset 113 cf7b01232d86
parent 77 69b2d279c8f0
child 119 9b3345f9d8ed
permissions -rw-r--r--
*** empty log message ***
athos@36
     1
#ifndef PREFLOW_PUSH_HH
athos@36
     2
#define PREFLOW_PUSH_HH
athos@36
     3
athos@36
     4
#include <algorithm>
athos@36
     5
#include <list>
athos@36
     6
#include <vector>
athos@36
     7
//#include "pf_hiba.hh"
athos@36
     8
//#include <marci_list_graph.hh>
athos@77
     9
//#include <marci_graph_traits.hh>
athos@77
    10
athos@36
    11
#include "reverse_bfs.hh"
athos@36
    12
athos@36
    13
using namespace std;
athos@36
    14
alpar@105
    15
namespace hugo {
athos@36
    16
athos@36
    17
  template <typename graph_type, typename T>
athos@36
    18
  class preflow_push {
athos@36
    19
athos@36
    20
    //Hasznos typedef-ek
athos@77
    21
    typedef typename graph_type::NodeIt NodeIt;
athos@77
    22
    typedef typename graph_type::EdgeIt EdgeIt;
athos@77
    23
    typedef typename graph_type::EachNodeIt EachNodeIt;
athos@77
    24
    typedef typename graph_type::EachEdgeIt EachEdgeIt;
athos@77
    25
    typedef typename graph_type::OutEdgeIt OutEdgeIt;
athos@77
    26
    typedef typename graph_type::InEdgeIt InEdgeIt;
athos@77
    27
    typedef typename graph_type::SymEdgeIt SymEdgeIt;
athos@77
    28
athos@77
    29
athos@77
    30
athos@77
    31
    /*
athos@36
    32
    typedef graph_traits<graph_type>::node_iterator node_iterator;
athos@77
    33
    typedef graph_traits<graph_type>::EdgeIt EdgeIt;
athos@36
    34
    typedef graph_traits<graph_type>::each_node_iterator each_node_iterator;
athos@77
    35
    typedef graph_traits<graph_type>::each_EdgeIt each_EdgeIt;
athos@77
    36
    typedef graph_traits<graph_type>::out_EdgeIt out_EdgeIt;
athos@77
    37
    typedef graph_traits<graph_type>::InEdgeIt InEdgeIt;
athos@77
    38
    typedef graph_traits<graph_type>::sym_EdgeIt sym_EdgeIt;
athos@77
    39
    */
athos@36
    40
athos@36
    41
    //---------------------------------------------
athos@36
    42
    //Parameters of the algorithm
athos@36
    43
    //---------------------------------------------
athos@36
    44
    //Fully examine an active node until excess becomes 0
athos@36
    45
    enum node_examination_t {examine_full, examine_to_relabel};
athos@36
    46
    //No more implemented yet:, examine_only_one_edge};
athos@36
    47
    node_examination_t node_examination;
athos@36
    48
    //Which implementation to be used
athos@36
    49
    enum implementation_t {impl_fifo, impl_highest_label};
athos@36
    50
    //No more implemented yet:};
athos@36
    51
    implementation_t implementation;
athos@36
    52
    //---------------------------------------------
athos@36
    53
    //Parameters of the algorithm
athos@36
    54
    //---------------------------------------------
athos@36
    55
 
athos@36
    56
  private:
athos@36
    57
    //input
athos@36
    58
    graph_type& G;
athos@77
    59
    NodeIt s;
athos@77
    60
    NodeIt t;
athos@77
    61
    typename graph_type::EdgeMap<T> &capacity;
athos@77
    62
    //typename graph_type::EdgeMap<T>  &capacity;
athos@36
    63
    //output
athos@77
    64
    //typename graph_type::EdgeMap<T>  
athos@77
    65
    typename graph_type::EdgeMap<T> preflow;
athos@36
    66
    T maxflow_value;
athos@36
    67
  
athos@36
    68
    //auxiliary variables for computation
athos@36
    69
    int number_of_nodes;
athos@77
    70
    
athos@77
    71
    
athos@77
    72
    typename graph_type::NodeMap<int> level;
athos@77
    73
    typename graph_type::NodeMap<T> excess;
athos@77
    74
    //node_property_vector<graph_type, int> level;
athos@77
    75
    //node_property_vector<graph_type, T> excess;
athos@36
    76
    
athos@36
    77
    //Number of nodes on each level
athos@36
    78
    vector<int> num_of_nodes_on_level;
athos@36
    79
    
athos@36
    80
    //For the FIFO implementation
athos@77
    81
    list<NodeIt> fifo_nodes;
athos@36
    82
    //For 'highest label' implementation
athos@36
    83
    int highest_active;
athos@36
    84
    //int second_highest_active;
athos@77
    85
    vector< list<NodeIt> > active_nodes;
athos@36
    86
athos@36
    87
  public:
athos@36
    88
  
athos@36
    89
    //Constructing the object using the graph, source, sink and capacity vector
athos@36
    90
    preflow_push(
athos@36
    91
		      graph_type& _G, 
athos@77
    92
		      NodeIt _s, 
athos@77
    93
		      NodeIt _t, 
athos@77
    94
		      typename graph_type::EdgeMap<T> & _capacity)
athos@36
    95
      : G(_G), s(_s), t(_t), 
athos@36
    96
	capacity(_capacity), 
athos@36
    97
	preflow(_G),
athos@36
    98
	//Counting the number of nodes
athos@77
    99
	//number_of_nodes(count(G.first<EachNodeIt>())),
athos@77
   100
	number_of_nodes(G.nodeNum()),
athos@77
   101
athos@36
   102
	level(_G),
athos@36
   103
	excess(_G)//,
athos@36
   104
        // Default constructor: active_nodes()
athos@36
   105
    { 
athos@36
   106
      //Simplest parameter settings
athos@36
   107
      node_examination = examine_full;//examine_to_relabel;//
athos@36
   108
      //Which implementation to be usedexamine_full
athos@36
   109
      implementation = impl_highest_label;//impl_fifo;
athos@36
   110
 
athos@36
   111
      //
athos@36
   112
      num_of_nodes_on_level.resize(2*number_of_nodes-1);
athos@36
   113
      num_of_nodes_on_level.clear();
athos@36
   114
athos@36
   115
      switch(implementation){
athos@36
   116
      case impl_highest_label :{
athos@36
   117
	active_nodes.resize(2*number_of_nodes-1);
athos@36
   118
	active_nodes.clear();
athos@36
   119
	break;
athos@36
   120
      }
athos@36
   121
      default:
athos@36
   122
	break;
athos@36
   123
      }
athos@36
   124
athos@36
   125
    }
athos@36
   126
athos@36
   127
    //Returns the value of a maximal flow 
athos@36
   128
    T run();
athos@36
   129
  
athos@77
   130
    typename graph_type::EdgeMap<T>  getmaxflow(){
athos@36
   131
      return preflow;
athos@36
   132
    }
athos@36
   133
athos@36
   134
athos@36
   135
  private:
athos@36
   136
    //For testing purposes only
athos@36
   137
    //Lists the node_properties
athos@77
   138
    void write_property_vector(typename graph_type::NodeMap<T> a,
athos@77
   139
			       //node_property_vector<graph_type, T> a, 
athos@36
   140
			       char* prop_name="property"){
athos@77
   141
      for(EachNodeIt i=G.template first<EachNodeIt>(); i.valid(); ++i) {
athos@36
   142
	cout<<"Node id.: "<<G.id(i)<<", "<<prop_name<<" value: "<<a.get(i)<<endl;
athos@36
   143
      }
athos@36
   144
      cout<<endl;
athos@36
   145
    }
athos@36
   146
athos@36
   147
    //Modifies the excess of the node and makes sufficient changes
athos@77
   148
    void modify_excess(const NodeIt& a ,T v){
athos@36
   149
	T old_value=excess.get(a);
athos@77
   150
	excess.set(a,old_value+v);
athos@36
   151
    }
athos@36
   152
  
athos@36
   153
    //This private procedure is supposed to modify the preflow on edge j
athos@36
   154
    //by value v (which can be positive or negative as well) 
athos@36
   155
    //and maintain the excess on the head and tail
athos@36
   156
    //Here we do not check whether this is possible or not
athos@77
   157
    void modify_preflow(EdgeIt j, const T& v){
athos@36
   158
athos@36
   159
      //Auxiliary variable
athos@36
   160
      T old_value;
athos@36
   161
	
athos@36
   162
      //Modifiyng the edge
athos@36
   163
      old_value=preflow.get(j);
athos@77
   164
      preflow.set(j,old_value+v);
athos@36
   165
athos@36
   166
athos@36
   167
      //Modifiyng the head
athos@36
   168
      modify_excess(G.head(j),v);
athos@36
   169
	
athos@36
   170
      //Modifiyng the tail
athos@36
   171
      modify_excess(G.tail(j),-v);
athos@36
   172
athos@36
   173
    }
athos@36
   174
athos@36
   175
    //Gives the active node to work with 
athos@36
   176
    //(depending on the implementation to be used)
athos@77
   177
    NodeIt get_active_node(){
athos@36
   178
      //cout<<highest_active<<endl;
athos@36
   179
athos@36
   180
      switch(implementation) {
athos@36
   181
      case impl_highest_label : {
athos@36
   182
athos@36
   183
	//First need to find the highest label for which there"s an active node
athos@36
   184
	while( highest_active>=0 && active_nodes[highest_active].empty() ){ 
athos@36
   185
	  --highest_active;
athos@36
   186
	}
athos@36
   187
athos@36
   188
	if( highest_active>=0) {
athos@77
   189
	  NodeIt a=active_nodes[highest_active].front();
athos@36
   190
	  active_nodes[highest_active].pop_front();
athos@36
   191
	  return a;
athos@36
   192
	}
athos@36
   193
	else {
athos@77
   194
	  return NodeIt();
athos@36
   195
	}
athos@36
   196
	
athos@36
   197
	break;
athos@36
   198
	
athos@36
   199
      }
athos@36
   200
      case impl_fifo : {
athos@36
   201
athos@36
   202
	if( ! fifo_nodes.empty() ) {
athos@77
   203
	  NodeIt a=fifo_nodes.front();
athos@36
   204
	  fifo_nodes.pop_front();
athos@36
   205
	  return a;
athos@36
   206
	}
athos@36
   207
	else {
athos@77
   208
	  return NodeIt();
athos@36
   209
	}
athos@36
   210
	break;
athos@36
   211
      }
athos@36
   212
      }
athos@36
   213
      //
athos@77
   214
      return NodeIt();
athos@36
   215
    }
athos@36
   216
athos@36
   217
    //Puts node 'a' among the active nodes
athos@77
   218
    void make_active(const NodeIt& a){
athos@36
   219
      //s and t never become active
athos@36
   220
      if (a!=s && a!= t){
athos@36
   221
	switch(implementation){
athos@36
   222
	case impl_highest_label :
athos@36
   223
	  active_nodes[level.get(a)].push_back(a);
athos@36
   224
	  break;
athos@36
   225
	case impl_fifo :
athos@36
   226
	  fifo_nodes.push_back(a);
athos@36
   227
	  break;
athos@36
   228
	}
athos@36
   229
athos@36
   230
      }
athos@36
   231
athos@36
   232
      //Update highest_active label
athos@36
   233
      if (highest_active<level.get(a)){
athos@36
   234
	highest_active=level.get(a);
athos@36
   235
      }
athos@36
   236
athos@36
   237
    }
athos@36
   238
athos@36
   239
    //Changes the level of node a and make sufficent changes
athos@77
   240
    void change_level_to(NodeIt a, int new_value){
athos@36
   241
      int seged = level.get(a);
athos@77
   242
      level.set(a,new_value);
athos@36
   243
      --num_of_nodes_on_level[seged];
athos@36
   244
      ++num_of_nodes_on_level[new_value];
athos@36
   245
    }
athos@36
   246
athos@36
   247
    //Collection of things useful (or necessary) to do before running
athos@77
   248
athos@36
   249
    void preprocess(){
athos@36
   250
athos@36
   251
      //---------------------------------------
athos@36
   252
      //Initialize parameters
athos@36
   253
      //---------------------------------------
athos@36
   254
athos@36
   255
      //Setting starting preflow, level and excess values to zero
athos@36
   256
      //This can be important, if the algorithm is run more then once
athos@77
   257
      for(EachNodeIt i=G.template first<EachNodeIt>(); i.valid(); ++i) {
athos@77
   258
        level.set(i,0);
athos@77
   259
        excess.set(i,0);
athos@77
   260
	for(OutEdgeIt j=G.template first<OutEdgeIt>(i); j.valid(); ++j) 
athos@77
   261
	  preflow.set(j, 0);
athos@36
   262
      }
athos@36
   263
      num_of_nodes_on_level[0]=number_of_nodes;
athos@36
   264
      highest_active=0;
athos@36
   265
      //---------------------------------------
athos@36
   266
      //Initialize parameters
athos@36
   267
      //---------------------------------------
athos@36
   268
athos@36
   269
      
athos@36
   270
      //------------------------------------
athos@36
   271
      //This is the only part that uses BFS
athos@36
   272
      //------------------------------------
athos@36
   273
      //Setting starting level values using reverse bfs
athos@36
   274
      reverse_bfs<graph_type> rev_bfs(G,t);
athos@36
   275
      rev_bfs.run();
athos@36
   276
      //write_property_vector(rev_bfs.dist,"rev_bfs");
athos@77
   277
      for(EachNodeIt i=G.template first<EachNodeIt>(); i.valid(); ++i) {
athos@36
   278
        change_level_to(i,rev_bfs.dist(i));
athos@36
   279
	//level.put(i,rev_bfs.dist.get(i));
athos@36
   280
      }
athos@36
   281
      //------------------------------------
athos@36
   282
      //This is the only part that uses BFS
athos@36
   283
      //------------------------------------
athos@36
   284
      
athos@36
   285
      
athos@36
   286
      //Starting level of s
athos@36
   287
      change_level_to(s,number_of_nodes);
athos@36
   288
      //level.put(s,number_of_nodes);
athos@36
   289
      
athos@36
   290
      
athos@36
   291
      //we push as much preflow from s as possible to start with
athos@77
   292
      for(OutEdgeIt j=G.template first<OutEdgeIt>(s); j.valid(); ++j){ 
athos@36
   293
	modify_preflow(j,capacity.get(j) );
athos@36
   294
	make_active(G.head(j));
athos@36
   295
	int lev=level.get(G.head(j));
athos@36
   296
	if(highest_active<lev){
athos@36
   297
	  highest_active=lev;
athos@36
   298
	}
athos@36
   299
      }
athos@36
   300
      //cout<<highest_active<<endl;
athos@36
   301
    } 
athos@36
   302
athos@36
   303
    
athos@36
   304
    //If the preflow is less than the capacity on the given edge
athos@36
   305
    //then it is an edge in the residual graph
athos@77
   306
    bool is_admissible_forward_edge(OutEdgeIt j, int& new_level){
athos@36
   307
      if (capacity.get(j)>preflow.get(j)){
athos@36
   308
	if(level.get(G.tail(j))==level.get(G.head(j))+1){
athos@36
   309
	  return true;
athos@36
   310
	}
athos@36
   311
	else{
athos@36
   312
	  if (level.get(G.head(j)) < new_level)
athos@36
   313
	    new_level=level.get(G.head(j));
athos@36
   314
	}
athos@36
   315
      }
athos@36
   316
      return false;
athos@36
   317
    }
athos@36
   318
athos@36
   319
    //If the preflow is greater than 0 on the given edge
athos@36
   320
    //then the edge reversd is an edge in the residual graph
athos@77
   321
    bool is_admissible_backward_edge(InEdgeIt j, int& new_level){
athos@36
   322
      if (0<preflow.get(j)){
athos@36
   323
	if(level.get(G.tail(j))==level.get(G.head(j))-1){
athos@36
   324
	  return true;
athos@36
   325
	}
athos@36
   326
	else{
athos@36
   327
	  if (level.get(G.tail(j)) < new_level)
athos@36
   328
	    new_level=level.get(G.tail(j));
athos@36
   329
	}
athos@36
   330
	
athos@36
   331
      }
athos@36
   332
      return false;
athos@36
   333
    }
athos@36
   334
athos@36
   335
 
athos@36
   336
  };  //class preflow_push  
athos@36
   337
athos@36
   338
  template<typename graph_type, typename T>
athos@36
   339
    T preflow_push<graph_type, T>::run() {
athos@36
   340
    
athos@36
   341
    preprocess();
athos@36
   342
    
athos@36
   343
    T e,v;
athos@77
   344
    NodeIt a;
athos@36
   345
    while (a=get_active_node(), a.valid()){
athos@36
   346
      //cout<<G.id(a)<<endl;
athos@36
   347
      //write_property_vector(excess,"excess");
athos@36
   348
      //write_property_vector(level,"level");
athos@36
   349
athos@36
   350
athos@36
   351
      bool go_to_next_node=false;
athos@36
   352
      e = excess.get(a);
athos@36
   353
      while (!go_to_next_node){
athos@77
   354
athos@77
   355
	//Initial value for the new level for the active node we are dealing with
athos@77
   356
	int new_level=2*number_of_nodes;
athos@77
   357
	//write_property_vector(excess,"excess");
athos@77
   358
	//write_property_vector(level,"level");
athos@77
   359
	//cout<<G.id(a)<<endl;
athos@36
   360
	//Out edges from node a
athos@36
   361
	{
athos@77
   362
	  OutEdgeIt j=G.template first<OutEdgeIt>(a);
athos@36
   363
	  while (j.valid() && e){
athos@36
   364
athos@36
   365
	    if (is_admissible_forward_edge(j,new_level)){
athos@36
   366
	      v=min(e,capacity.get(j) - preflow.get(j));
athos@36
   367
	      e -= v;
athos@36
   368
	      //New node might become active
athos@36
   369
	      if (excess.get(G.head(j))==0){
athos@36
   370
		make_active(G.head(j));
athos@36
   371
	      }
athos@36
   372
	      modify_preflow(j,v);
athos@36
   373
	    }
athos@36
   374
	    ++j;
athos@36
   375
	  }
athos@36
   376
	}
athos@36
   377
	//In edges to node a
athos@36
   378
	{
athos@77
   379
	  InEdgeIt j=G.template first<InEdgeIt>(a);
athos@36
   380
	  while (j.valid() && e){
athos@36
   381
	    if (is_admissible_backward_edge(j,new_level)){
athos@36
   382
	      v=min(e,preflow.get(j));
athos@36
   383
	      e -= v;
athos@36
   384
	      //New node might become active
athos@36
   385
	      if (excess.get(G.tail(j))==0){
athos@36
   386
		make_active(G.tail(j));
athos@36
   387
	      }
athos@36
   388
	      modify_preflow(j,-v);
athos@36
   389
	    }
athos@36
   390
	    ++j;
athos@36
   391
	  }
athos@36
   392
	}
athos@36
   393
athos@36
   394
	//cout<<G.id(a)<<" "<<new_level<<endl;
athos@36
   395
athos@36
   396
	if (0==e){
athos@36
   397
	  //Saturating push
athos@36
   398
	  go_to_next_node=true;
athos@36
   399
	}
athos@36
   400
	else{//If there is still excess in node a
athos@77
   401
	  
athos@77
   402
	  //change_level_to(a,new_level+1);
athos@77
   403
	  
athos@36
   404
	  //Level remains empty
athos@36
   405
	  if (num_of_nodes_on_level[level.get(a)]==1){
athos@36
   406
	    change_level_to(a,number_of_nodes);
athos@36
   407
	    //go_to_next_node=True;
athos@36
   408
	  }
athos@36
   409
	  else{
athos@36
   410
	    change_level_to(a,new_level+1);
athos@36
   411
	    //increase_level(a);
athos@36
   412
	  }
athos@77
   413
	  
athos@36
   414
    
athos@36
   415
	  
athos@36
   416
athos@36
   417
	  switch(node_examination){
athos@36
   418
	  case examine_to_relabel:
athos@36
   419
	    make_active(a);
athos@36
   420
athos@36
   421
	    go_to_next_node = true;
athos@36
   422
	    break;
athos@36
   423
	  default:
athos@36
   424
	    break;
athos@36
   425
	  }
athos@36
   426
	  
athos@36
   427
    
athos@36
   428
	
athos@36
   429
	}//if (0==e)
athos@36
   430
      }
athos@36
   431
    }
athos@36
   432
    maxflow_value = excess.get(t);
athos@36
   433
    return maxflow_value;
athos@36
   434
  }//run
athos@36
   435
athos@36
   436
alpar@105
   437
}//namespace hugo
athos@36
   438
athos@36
   439
#endif //PREFLOW_PUSH_HH