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