src/work/jacint/preflowproba.h
author jacint
Thu, 22 Apr 2004 14:11:28 +0000
changeset 372 e6a156fc186d
parent 370 5eceadf9316c
child 374 0fc9cd9b854a
permissions -rw-r--r--
(none)
jacint@370
     1
// -*- C++ -*-
jacint@370
     2
jacint@370
     3
//run gyorsan tudna adni a minmincutot a 2 fazis elejen , ne vegyuk be konstruktorba egy cutmapet?
jacint@370
     4
//constzero jo igy?
jacint@370
     5
jacint@370
     6
//majd marci megmondja betegyem-e bfs-t meg resgraphot
jacint@370
     7
jacint@370
     8
/*
jacint@370
     9
Heuristics: 
jacint@370
    10
 2 phase
jacint@370
    11
 gap
jacint@370
    12
 list 'level_list' on the nodes on level i implemented by hand
jacint@370
    13
 stack 'active' on the active nodes on level i implemented by hand
jacint@370
    14
 runs heuristic 'highest label' for H1*n relabels
jacint@370
    15
 runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
jacint@370
    16
 
jacint@370
    17
Parameters H0 and H1 are initialized to 20 and 10.
jacint@370
    18
jacint@370
    19
Constructors:
jacint@370
    20
jacint@370
    21
Preflow(Graph, Node, Node, CapMap, FlowMap, bool) : bool must be false if 
jacint@370
    22
     FlowMap is not constant zero, and should be true if it is
jacint@370
    23
jacint@370
    24
Members:
jacint@370
    25
jacint@370
    26
void run()
jacint@370
    27
jacint@370
    28
T flowValue() : returns the value of a maximum flow
jacint@370
    29
jacint@370
    30
void minMinCut(CutMap& M) : sets M to the characteristic vector of the 
jacint@370
    31
     minimum min cut. M should be a map of bools initialized to false.
jacint@370
    32
jacint@370
    33
void maxMinCut(CutMap& M) : sets M to the characteristic vector of the 
jacint@370
    34
     maximum min cut. M should be a map of bools initialized to false.
jacint@370
    35
jacint@370
    36
void minCut(CutMap& M) : sets M to the characteristic vector of 
jacint@370
    37
     a min cut. M should be a map of bools initialized to false.
jacint@370
    38
jacint@370
    39
FIXME reset
jacint@370
    40
jacint@370
    41
*/
jacint@370
    42
jacint@370
    43
#ifndef HUGO_PREFLOW_H
jacint@370
    44
#define HUGO_PREFLOW_H
jacint@370
    45
jacint@370
    46
#define H0 20
jacint@370
    47
#define H1 1
jacint@370
    48
jacint@370
    49
#include <vector>
jacint@370
    50
#include <queue>
jacint@370
    51
#include<graph_wrapper.h>
jacint@370
    52
jacint@370
    53
namespace hugo {
jacint@370
    54
jacint@370
    55
  template <typename Graph, typename T, 
jacint@370
    56
	    typename CapMap=typename Graph::EdgeMap<T>, 
jacint@370
    57
            typename FlowMap=typename Graph::EdgeMap<T> >
jacint@370
    58
  class Preflow {
jacint@370
    59
    
jacint@370
    60
    typedef typename Graph::Node Node;
jacint@370
    61
    typedef typename Graph::Edge Edge;
jacint@370
    62
    typedef typename Graph::NodeIt NodeIt;
jacint@370
    63
    typedef typename Graph::OutEdgeIt OutEdgeIt;
jacint@370
    64
    typedef typename Graph::InEdgeIt InEdgeIt;
jacint@370
    65
    
jacint@370
    66
    const Graph& G;
jacint@370
    67
    Node s;
jacint@370
    68
    Node t;
jacint@370
    69
    const CapMap& capacity;  
jacint@370
    70
    FlowMap& flow;
jacint@370
    71
    T value;
jacint@370
    72
    bool constzero;
jacint@370
    73
jacint@370
    74
    typedef ResGraphWrapper<const Graph, T, CapMap, FlowMap> ResGW;
jacint@370
    75
    typedef typename ResGW::OutEdgeIt ResOutEdgeIt;
jacint@370
    76
    typedef typename ResGW::InEdgeIt ResInEdgeIt;
jacint@370
    77
    typedef typename ResGW::Edge ResEdge;
jacint@370
    78
 
jacint@370
    79
  public:
jacint@370
    80
    Preflow(Graph& _G, Node _s, Node _t, CapMap& _capacity, 
jacint@370
    81
	    FlowMap& _flow, bool _constzero ) :
jacint@370
    82
      G(_G), s(_s), t(_t), capacity(_capacity), flow(_flow), constzero(_constzero) {}
jacint@370
    83
    
jacint@370
    84
    
jacint@370
    85
    void run() {
jacint@370
    86
jacint@370
    87
      ResGW res_graph(G, capacity, flow);
jacint@370
    88
jacint@370
    89
      value=0;                //for the subsequent runs
jacint@370
    90
jacint@370
    91
      bool phase=0;        //phase 0 is the 1st phase, phase 1 is the 2nd
jacint@370
    92
      int n=G.nodeNum(); 
jacint@370
    93
      int heur0=(int)(H0*n);  //time while running 'bound decrease' 
jacint@370
    94
      int heur1=(int)(H1*n);  //time while running 'highest label'
jacint@370
    95
      int heur=heur1;         //starting time interval (#of relabels)
jacint@370
    96
      bool what_heur=1;       
jacint@370
    97
      /*
jacint@370
    98
	what_heur is 0 in case 'bound decrease' 
jacint@370
    99
	and 1 in case 'highest label'
jacint@370
   100
      */
jacint@370
   101
      bool end=false;     
jacint@370
   102
      /*
jacint@370
   103
	Needed for 'bound decrease', 'true'
jacint@370
   104
	means no active nodes are above bound b.
jacint@370
   105
      */
jacint@370
   106
      int relabel=0;
jacint@370
   107
      int k=n-2;  //bound on the highest level under n containing a node
jacint@370
   108
      int b=k;    //bound on the highest level under n of an active node
jacint@370
   109
      
jacint@370
   110
      typename Graph::NodeMap<int> level(G,n);      
jacint@370
   111
      typename Graph::NodeMap<T> excess(G); 
jacint@370
   112
jacint@370
   113
      std::vector<Node> active(n-1,INVALID);
jacint@370
   114
      typename Graph::NodeMap<Node> next(G,INVALID);
jacint@370
   115
      //Stack of the active nodes in level i < n.
jacint@370
   116
      //We use it in both phases.
jacint@370
   117
jacint@370
   118
      typename Graph::NodeMap<Node> left(G,INVALID);
jacint@370
   119
      typename Graph::NodeMap<Node> right(G,INVALID);
jacint@370
   120
      std::vector<Node> level_list(n,INVALID);
jacint@370
   121
      /*
jacint@370
   122
	List of the nodes in level i<n.
jacint@370
   123
      */
jacint@370
   124
jacint@370
   125
jacint@370
   126
      if ( constzero ) {
jacint@370
   127
     
jacint@370
   128
	/*Reverse_bfs from t, to find the starting level.*/
jacint@370
   129
	level.set(t,0);
jacint@370
   130
	std::queue<Node> bfs_queue;
jacint@370
   131
	bfs_queue.push(t);
jacint@370
   132
	
jacint@370
   133
	while (!bfs_queue.empty()) {
jacint@370
   134
	  
jacint@370
   135
	  Node v=bfs_queue.front();	
jacint@370
   136
	  bfs_queue.pop();
jacint@370
   137
	  int l=level[v]+1;
jacint@370
   138
	  
jacint@370
   139
	  InEdgeIt e;
jacint@370
   140
	  for(G.first(e,v); G.valid(e); G.next(e)) {
jacint@370
   141
	    Node w=G.tail(e);
jacint@370
   142
	    if ( level[w] == n && w != s ) {
jacint@370
   143
	      bfs_queue.push(w);
jacint@370
   144
	      Node first=level_list[l];
jacint@370
   145
	      if ( G.valid(first) ) left.set(first,w);
jacint@370
   146
	      right.set(w,first);
jacint@370
   147
	      level_list[l]=w;
jacint@370
   148
	      level.set(w, l);
jacint@370
   149
	    }
jacint@370
   150
	  }
jacint@370
   151
	}
jacint@370
   152
jacint@370
   153
	//the starting flow
jacint@370
   154
	OutEdgeIt e;
jacint@370
   155
	for(G.first(e,s); G.valid(e); G.next(e)) 
jacint@370
   156
	{
jacint@370
   157
	  T c=capacity[e];
jacint@370
   158
	  if ( c == 0 ) continue;
jacint@370
   159
	  Node w=G.head(e);
jacint@370
   160
	  if ( level[w] < n ) {	  
jacint@370
   161
	    if ( excess[w] == 0 && w!=t ) {
jacint@370
   162
	      next.set(w,active[level[w]]);
jacint@370
   163
	      active[level[w]]=w;
jacint@370
   164
	    }
jacint@370
   165
	    flow.set(e, c); 
jacint@370
   166
	    excess.set(w, excess[w]+c);
jacint@370
   167
	  }
jacint@370
   168
	}
jacint@370
   169
      }
jacint@370
   170
      else 
jacint@370
   171
      {
jacint@370
   172
	
jacint@370
   173
	/*
jacint@370
   174
	  Reverse_bfs from t in the residual graph, 
jacint@370
   175
	  to find the starting level.
jacint@370
   176
	*/
jacint@370
   177
	level.set(t,0);
jacint@370
   178
	std::queue<Node> bfs_queue;
jacint@370
   179
	bfs_queue.push(t);
jacint@370
   180
	
jacint@370
   181
	while (!bfs_queue.empty()) {
jacint@370
   182
	  
jacint@370
   183
	  Node v=bfs_queue.front();	
jacint@370
   184
	  bfs_queue.pop();
jacint@370
   185
	  int l=level[v]+1;
jacint@370
   186
	  
jacint@370
   187
	  InEdgeIt e;
jacint@370
   188
	  for(G.first(e,v); G.valid(e); G.next(e)) {
jacint@370
   189
	    if ( capacity[e] == flow[e] ) continue;
jacint@370
   190
	    Node w=G.tail(e);
jacint@370
   191
	    if ( level[w] == n && w != s ) {
jacint@370
   192
	      bfs_queue.push(w);
jacint@370
   193
	      Node first=level_list[l];
jacint@370
   194
	      if ( G.valid(first) ) left.set(first,w);
jacint@370
   195
	      right.set(w,first);
jacint@370
   196
	      level_list[l]=w;
jacint@370
   197
	      level.set(w, l);
jacint@370
   198
	    }
jacint@370
   199
	  }
jacint@370
   200
	    
jacint@370
   201
	  OutEdgeIt f;
jacint@370
   202
	  for(G.first(f,v); G.valid(f); G.next(f)) {
jacint@370
   203
	    if ( 0 == flow[f] ) continue;
jacint@370
   204
	    Node w=G.head(f);
jacint@370
   205
	    if ( level[w] == n && w != s ) {
jacint@370
   206
	      bfs_queue.push(w);
jacint@370
   207
	      Node first=level_list[l];
jacint@370
   208
	      if ( G.valid(first) ) left.set(first,w);
jacint@370
   209
	      right.set(w,first);
jacint@370
   210
	      level_list[l]=w;
jacint@370
   211
	      level.set(w, l);
jacint@370
   212
	    }
jacint@370
   213
	    }
jacint@370
   214
	}
jacint@370
   215
      
jacint@370
   216
	
jacint@370
   217
	/*
jacint@370
   218
	  Counting the excess
jacint@370
   219
	*/
jacint@370
   220
	NodeIt v;
jacint@370
   221
	for(G.first(v); G.valid(v); G.next(v)) {
jacint@370
   222
	  T exc=0;
jacint@370
   223
jacint@370
   224
	  InEdgeIt e;
jacint@370
   225
	  for(G.first(e,v); G.valid(e); G.next(e)) exc+=flow[e];
jacint@370
   226
	  OutEdgeIt f;
jacint@370
   227
	  for(G.first(f,v); G.valid(f); G.next(f)) exc-=flow[e];
jacint@370
   228
jacint@370
   229
	  excess.set(v,exc);	  
jacint@370
   230
jacint@370
   231
	  //putting the active nodes into the stack
jacint@370
   232
	  int lev=level[v];
jacint@370
   233
	  if ( exc > 0 && lev < n ) {
jacint@370
   234
	    next.set(v,active[lev]);
jacint@370
   235
	    active[lev]=v;
jacint@370
   236
	  }
jacint@370
   237
	}
jacint@370
   238
jacint@370
   239
jacint@370
   240
	//the starting flow
jacint@370
   241
	OutEdgeIt e;
jacint@370
   242
	for(G.first(e,s); G.valid(e); G.next(e)) 
jacint@370
   243
	{
jacint@370
   244
	  T rem=capacity[e]-flow[e];
jacint@370
   245
	  if ( rem == 0 ) continue;
jacint@370
   246
	  Node w=G.head(e);
jacint@370
   247
	  if ( level[w] < n ) {	  
jacint@370
   248
	    if ( excess[w] == 0 && w!=t ) {
jacint@370
   249
	      next.set(w,active[level[w]]);
jacint@370
   250
	      active[level[w]]=w;
jacint@370
   251
	    }
jacint@370
   252
	    flow.set(e, capacity[e]); 
jacint@370
   253
	    excess.set(w, excess[w]+rem);
jacint@370
   254
	  }
jacint@370
   255
	}
jacint@370
   256
	
jacint@370
   257
	InEdgeIt f;
jacint@370
   258
	for(G.first(f,s); G.valid(f); G.next(f)) 
jacint@370
   259
	{
jacint@370
   260
	  if ( flow[f] == 0 ) continue;
jacint@370
   261
	  Node w=G.head(f);
jacint@370
   262
	  if ( level[w] < n ) {	  
jacint@370
   263
	    if ( excess[w] == 0 && w!=t ) {
jacint@370
   264
	      next.set(w,active[level[w]]);
jacint@370
   265
	      active[level[w]]=w;
jacint@370
   266
	    }
jacint@370
   267
	    excess.set(w, excess[w]+flow[f]);
jacint@370
   268
	    flow.set(f, 0); 
jacint@370
   269
	  }
jacint@370
   270
	}
jacint@370
   271
      }
jacint@370
   272
jacint@370
   273
jacint@370
   274
jacint@370
   275
jacint@370
   276
      /* 
jacint@370
   277
	 End of preprocessing 
jacint@370
   278
      */
jacint@370
   279
jacint@370
   280
jacint@370
   281
jacint@370
   282
      /*
jacint@370
   283
	Push/relabel on the highest level active nodes.
jacint@370
   284
      */	
jacint@370
   285
      while ( true ) {
jacint@370
   286
	
jacint@370
   287
	if ( b == 0 ) {
jacint@370
   288
	  if ( phase ) break;
jacint@370
   289
	  
jacint@370
   290
	  if ( !what_heur && !end && k > 0 ) {
jacint@370
   291
	    b=k;
jacint@370
   292
	    end=true;
jacint@370
   293
	  } else {
jacint@370
   294
	    phase=1;
jacint@370
   295
	    level.set(s,0);
jacint@370
   296
	    std::queue<Node> bfs_queue;
jacint@370
   297
	    bfs_queue.push(s);
jacint@370
   298
	    
jacint@370
   299
	    while (!bfs_queue.empty()) {
jacint@370
   300
	      
jacint@370
   301
	      Node v=bfs_queue.front();	
jacint@370
   302
	      bfs_queue.pop();
jacint@370
   303
	      int l=level[v]+1;
jacint@370
   304
	      
jacint@372
   305
	      /*	      ResInEdgeIt e;
jacint@370
   306
	      for(res_graph.first(e,s); res_graph.valid(e); 
jacint@370
   307
		  res_graph.next(e)) {
jacint@370
   308
		Node u=res_graph.tail(e);
jacint@370
   309
		if ( level[u] >= n ) { 
jacint@370
   310
		  bfs_queue.push(u);
jacint@370
   311
		  level.set(u, l);
jacint@370
   312
		  if ( excess[u] > 0 ) {
jacint@370
   313
		    next.set(u,active[l]);
jacint@370
   314
		    active[l]=u;
jacint@370
   315
		  }
jacint@370
   316
		}
jacint@372
   317
		}*/
jacint@372
   318
	            InEdgeIt e;
jacint@370
   319
	      for(G.first(e,v); G.valid(e); G.next(e)) {
jacint@370
   320
		if ( capacity[e] == flow[e] ) continue;
jacint@370
   321
		Node u=G.tail(e);
jacint@370
   322
		if ( level[u] >= n ) { 
jacint@370
   323
		  bfs_queue.push(u);
jacint@370
   324
		  level.set(u, l);
jacint@370
   325
		  if ( excess[u] > 0 ) {
jacint@370
   326
		    next.set(u,active[l]);
jacint@370
   327
		    active[l]=u;
jacint@370
   328
		  }
jacint@370
   329
		}
jacint@370
   330
	      }
jacint@370
   331
	    
jacint@370
   332
	      OutEdgeIt f;
jacint@370
   333
	      for(G.first(f,v); G.valid(f); G.next(f)) {
jacint@370
   334
		if ( 0 == flow[f] ) continue;
jacint@370
   335
		Node u=G.head(f);
jacint@370
   336
		if ( level[u] >= n ) { 
jacint@370
   337
		  bfs_queue.push(u);
jacint@370
   338
		  level.set(u, l);
jacint@370
   339
		  if ( excess[u] > 0 ) {
jacint@370
   340
		    next.set(u,active[l]);
jacint@370
   341
		    active[l]=u;
jacint@370
   342
		  }
jacint@370
   343
		}
jacint@372
   344
		}
jacint@370
   345
	    }
jacint@370
   346
	    b=n-2;
jacint@370
   347
	    }
jacint@370
   348
	    
jacint@370
   349
	}
jacint@370
   350
	  
jacint@370
   351
	  
jacint@370
   352
	if ( !G.valid(active[b]) ) --b; 
jacint@370
   353
	else {
jacint@370
   354
	  end=false;  
jacint@370
   355
jacint@370
   356
	  Node w=active[b];
jacint@370
   357
	  active[b]=next[w];
jacint@370
   358
	  int lev=level[w];
jacint@370
   359
	  T exc=excess[w];
jacint@370
   360
	  int newlevel=n;       //bound on the next level of w
jacint@370
   361
	  
jacint@370
   362
	  OutEdgeIt e;
jacint@370
   363
	  for(G.first(e,w); G.valid(e); G.next(e)) {
jacint@370
   364
	    
jacint@370
   365
	    if ( flow[e] == capacity[e] ) continue; 
jacint@370
   366
	    Node v=G.head(e);            
jacint@370
   367
	    //e=wv	    
jacint@370
   368
	    
jacint@370
   369
	    if( lev > level[v] ) {      
jacint@370
   370
	      /*Push is allowed now*/
jacint@370
   371
	      
jacint@370
   372
	      if ( excess[v]==0 && v!=t && v!=s ) {
jacint@370
   373
		int lev_v=level[v];
jacint@370
   374
		next.set(v,active[lev_v]);
jacint@370
   375
		active[lev_v]=v;
jacint@370
   376
	      }
jacint@370
   377
	      
jacint@370
   378
	      T cap=capacity[e];
jacint@370
   379
	      T flo=flow[e];
jacint@370
   380
	      T remcap=cap-flo;
jacint@370
   381
	      
jacint@370
   382
	      if ( remcap >= exc ) {       
jacint@370
   383
		/*A nonsaturating push.*/
jacint@370
   384
		
jacint@370
   385
		flow.set(e, flo+exc);
jacint@370
   386
		excess.set(v, excess[v]+exc);
jacint@370
   387
		exc=0;
jacint@370
   388
		break; 
jacint@370
   389
		
jacint@370
   390
	      } else { 
jacint@370
   391
		/*A saturating push.*/
jacint@370
   392
		
jacint@370
   393
		flow.set(e, cap);
jacint@370
   394
		excess.set(v, excess[v]+remcap);
jacint@370
   395
		exc-=remcap;
jacint@370
   396
	      }
jacint@370
   397
	    } else if ( newlevel > level[v] ){
jacint@370
   398
	      newlevel = level[v];
jacint@370
   399
	    }	    
jacint@370
   400
	    
jacint@370
   401
	  } //for out edges wv 
jacint@370
   402
	
jacint@370
   403
	
jacint@370
   404
	if ( exc > 0 ) {	
jacint@370
   405
	  InEdgeIt e;
jacint@370
   406
	  for(G.first(e,w); G.valid(e); G.next(e)) {
jacint@370
   407
	    
jacint@370
   408
	    if( flow[e] == 0 ) continue; 
jacint@370
   409
	    Node v=G.tail(e);  
jacint@370
   410
	    //e=vw
jacint@370
   411
	    
jacint@370
   412
	    if( lev > level[v] ) {  
jacint@370
   413
	      /*Push is allowed now*/
jacint@370
   414
	      
jacint@370
   415
	      if ( excess[v]==0 && v!=t && v!=s ) {
jacint@370
   416
		int lev_v=level[v];
jacint@370
   417
		next.set(v,active[lev_v]);
jacint@370
   418
		active[lev_v]=v;
jacint@370
   419
	      }
jacint@370
   420
	      
jacint@370
   421
	      T flo=flow[e];
jacint@370
   422
	      
jacint@370
   423
	      if ( flo >= exc ) { 
jacint@370
   424
		/*A nonsaturating push.*/
jacint@370
   425
		
jacint@370
   426
		flow.set(e, flo-exc);
jacint@370
   427
		excess.set(v, excess[v]+exc);
jacint@370
   428
		exc=0;
jacint@370
   429
		break; 
jacint@370
   430
	      } else {                                               
jacint@370
   431
		/*A saturating push.*/
jacint@370
   432
		
jacint@370
   433
		excess.set(v, excess[v]+flo);
jacint@370
   434
		exc-=flo;
jacint@370
   435
		flow.set(e,0);
jacint@370
   436
	      }  
jacint@370
   437
	    } else if ( newlevel > level[v] ) {
jacint@370
   438
	      newlevel = level[v];
jacint@370
   439
	    }	    
jacint@370
   440
	  } //for in edges vw
jacint@370
   441
	  
jacint@370
   442
	} // if w still has excess after the out edge for cycle
jacint@370
   443
	
jacint@370
   444
	excess.set(w, exc);
jacint@370
   445
	 
jacint@370
   446
	/*
jacint@370
   447
	  Relabel
jacint@370
   448
	*/
jacint@370
   449
	
jacint@370
   450
jacint@370
   451
	if ( exc > 0 ) {
jacint@370
   452
	  //now 'lev' is the old level of w
jacint@370
   453
	
jacint@370
   454
	  if ( phase ) {
jacint@370
   455
	    level.set(w,++newlevel);
jacint@370
   456
	    next.set(w,active[newlevel]);
jacint@370
   457
	    active[newlevel]=w;
jacint@370
   458
	    b=newlevel;
jacint@370
   459
	  } else {
jacint@370
   460
	    //unlacing starts
jacint@370
   461
	    Node right_n=right[w];
jacint@370
   462
	    Node left_n=left[w];
jacint@370
   463
jacint@370
   464
	    if ( G.valid(right_n) ) {
jacint@370
   465
	      if ( G.valid(left_n) ) {
jacint@370
   466
		right.set(left_n, right_n);
jacint@370
   467
		left.set(right_n, left_n);
jacint@370
   468
	      } else {
jacint@370
   469
		level_list[lev]=right_n;   
jacint@370
   470
		left.set(right_n, INVALID);
jacint@370
   471
	      } 
jacint@370
   472
	    } else {
jacint@370
   473
	      if ( G.valid(left_n) ) {
jacint@370
   474
		right.set(left_n, INVALID);
jacint@370
   475
	      } else { 
jacint@370
   476
		level_list[lev]=INVALID;   
jacint@370
   477
	      } 
jacint@370
   478
	    } 
jacint@370
   479
	    //unlacing ends
jacint@370
   480
		
jacint@370
   481
	    if ( !G.valid(level_list[lev]) ) {
jacint@370
   482
	      
jacint@370
   483
	       //gapping starts
jacint@370
   484
	      for (int i=lev; i!=k ; ) {
jacint@370
   485
		Node v=level_list[++i];
jacint@370
   486
		while ( G.valid(v) ) {
jacint@370
   487
		  level.set(v,n);
jacint@370
   488
		  v=right[v];
jacint@370
   489
		}
jacint@370
   490
		level_list[i]=INVALID;
jacint@370
   491
		if ( !what_heur ) active[i]=INVALID;
jacint@370
   492
	      }	     
jacint@370
   493
jacint@370
   494
	      level.set(w,n);
jacint@370
   495
	      b=lev-1;
jacint@370
   496
	      k=b;
jacint@370
   497
	      //gapping ends
jacint@370
   498
	    
jacint@370
   499
	    } else {
jacint@370
   500
	      
jacint@370
   501
	      if ( newlevel == n ) level.set(w,n); 
jacint@370
   502
	      else {
jacint@370
   503
		level.set(w,++newlevel);
jacint@370
   504
		next.set(w,active[newlevel]);
jacint@370
   505
		active[newlevel]=w;
jacint@370
   506
		if ( what_heur ) b=newlevel;
jacint@370
   507
		if ( k < newlevel ) ++k;      //now k=newlevel
jacint@370
   508
		Node first=level_list[newlevel];
jacint@370
   509
		if ( G.valid(first) ) left.set(first,w);
jacint@370
   510
		right.set(w,first);
jacint@370
   511
		left.set(w,INVALID);
jacint@370
   512
		level_list[newlevel]=w;
jacint@370
   513
	      }
jacint@370
   514
	    }
jacint@370
   515
jacint@370
   516
jacint@370
   517
	    ++relabel; 
jacint@370
   518
	    if ( relabel >= heur ) {
jacint@370
   519
	      relabel=0;
jacint@370
   520
	      if ( what_heur ) {
jacint@370
   521
		what_heur=0;
jacint@370
   522
		heur=heur0;
jacint@370
   523
		end=false;
jacint@370
   524
	      } else {
jacint@370
   525
		what_heur=1;
jacint@370
   526
		heur=heur1;
jacint@370
   527
		b=k; 
jacint@370
   528
	      }
jacint@370
   529
	    }
jacint@370
   530
	  } //phase 0
jacint@370
   531
	  
jacint@370
   532
	  
jacint@370
   533
	} // if ( exc > 0 )
jacint@370
   534
	  
jacint@370
   535
	
jacint@370
   536
	}  // if stack[b] is nonempty
jacint@370
   537
	
jacint@370
   538
      } // while(true)
jacint@370
   539
jacint@370
   540
jacint@370
   541
      value = excess[t];
jacint@370
   542
      /*Max flow value.*/
jacint@370
   543
     
jacint@370
   544
    } //void run()
jacint@370
   545
jacint@370
   546
jacint@370
   547
jacint@370
   548
jacint@370
   549
jacint@370
   550
    /*
jacint@370
   551
      Returns the maximum value of a flow.
jacint@370
   552
     */
jacint@370
   553
jacint@370
   554
    T flowValue() {
jacint@370
   555
      return value;
jacint@370
   556
    }
jacint@370
   557
jacint@370
   558
jacint@370
   559
    FlowMap Flow() {
jacint@370
   560
      return flow;
jacint@370
   561
      }
jacint@370
   562
jacint@370
   563
jacint@370
   564
    
jacint@370
   565
    void Flow(FlowMap& _flow ) {
jacint@370
   566
      NodeIt v;
jacint@370
   567
      for(G.first(v) ; G.valid(v); G.next(v))
jacint@370
   568
	_flow.set(v,flow[v]);
jacint@370
   569
    }
jacint@370
   570
jacint@370
   571
jacint@370
   572
jacint@370
   573
    /*
jacint@370
   574
      Returns the minimum min cut, by a bfs from s in the residual graph.
jacint@370
   575
    */
jacint@370
   576
   
jacint@370
   577
    template<typename _CutMap>
jacint@370
   578
    void minMinCut(_CutMap& M) {
jacint@370
   579
    
jacint@370
   580
      std::queue<Node> queue;
jacint@370
   581
      
jacint@370
   582
      M.set(s,true);      
jacint@370
   583
      queue.push(s);
jacint@370
   584
jacint@370
   585
      while (!queue.empty()) {
jacint@370
   586
        Node w=queue.front();
jacint@370
   587
	queue.pop();
jacint@370
   588
jacint@370
   589
	OutEdgeIt e;
jacint@370
   590
	for(G.first(e,w) ; G.valid(e); G.next(e)) {
jacint@370
   591
	  Node v=G.head(e);
jacint@370
   592
	  if (!M[v] && flow[e] < capacity[e] ) {
jacint@370
   593
	    queue.push(v);
jacint@370
   594
	    M.set(v, true);
jacint@370
   595
	  }
jacint@370
   596
	} 
jacint@370
   597
jacint@370
   598
	InEdgeIt f;
jacint@370
   599
	for(G.first(f,w) ; G.valid(f); G.next(f)) {
jacint@370
   600
	  Node v=G.tail(f);
jacint@370
   601
	  if (!M[v] && flow[f] > 0 ) {
jacint@370
   602
	    queue.push(v);
jacint@370
   603
	    M.set(v, true);
jacint@370
   604
	  }
jacint@370
   605
	} 
jacint@370
   606
      }
jacint@370
   607
    }
jacint@370
   608
jacint@370
   609
jacint@370
   610
  
jacint@370
   611
    /*
jacint@370
   612
      Returns the maximum min cut, by a reverse bfs 
jacint@370
   613
      from t in the residual graph.
jacint@370
   614
    */
jacint@370
   615
    
jacint@370
   616
    template<typename _CutMap>
jacint@370
   617
    void maxMinCut(_CutMap& M) {
jacint@370
   618
    
jacint@370
   619
      std::queue<Node> queue;
jacint@370
   620
      
jacint@370
   621
      M.set(t,true);        
jacint@370
   622
      queue.push(t);
jacint@370
   623
jacint@370
   624
      while (!queue.empty()) {
jacint@370
   625
        Node w=queue.front();
jacint@370
   626
	queue.pop();
jacint@370
   627
jacint@370
   628
jacint@370
   629
	InEdgeIt e;
jacint@370
   630
	for(G.first(e,w) ; G.valid(e); G.next(e)) {
jacint@370
   631
	  Node v=G.tail(e);
jacint@370
   632
	  if (!M[v] && flow[e] < capacity[e] ) {
jacint@370
   633
	    queue.push(v);
jacint@370
   634
	    M.set(v, true);
jacint@370
   635
	  }
jacint@370
   636
	}
jacint@370
   637
	
jacint@370
   638
	OutEdgeIt f;
jacint@370
   639
	for(G.first(f,w) ; G.valid(f); G.next(f)) {
jacint@370
   640
	  Node v=G.head(f);
jacint@370
   641
	  if (!M[v] && flow[f] > 0 ) {
jacint@370
   642
	    queue.push(v);
jacint@370
   643
	    M.set(v, true);
jacint@370
   644
	  }
jacint@370
   645
	}
jacint@370
   646
      }
jacint@370
   647
jacint@370
   648
      NodeIt v;
jacint@370
   649
      for(G.first(v) ; G.valid(v); G.next(v)) {
jacint@370
   650
	M.set(v, !M[v]);
jacint@370
   651
      }
jacint@370
   652
jacint@370
   653
    }
jacint@370
   654
jacint@370
   655
jacint@370
   656
jacint@370
   657
    template<typename CutMap>
jacint@370
   658
    void minCut(CutMap& M) {
jacint@370
   659
      minMinCut(M);
jacint@370
   660
    }
jacint@370
   661
jacint@370
   662
    
jacint@370
   663
    void reset_target (Node _t) {t=_t;}
jacint@370
   664
    void reset_source (Node _s) {s=_s;}
jacint@370
   665
   
jacint@370
   666
    template<typename _CapMap>   
jacint@370
   667
    void reset_cap (_CapMap _cap) {capacity=_cap;}
jacint@370
   668
jacint@370
   669
    template<typename _FlowMap>   
jacint@370
   670
    void reset_cap (_FlowMap _flow, bool _constzero) {
jacint@370
   671
      flow=_flow;
jacint@370
   672
      constzero=_constzero;
jacint@370
   673
    }
jacint@370
   674
jacint@370
   675
jacint@370
   676
jacint@370
   677
  };
jacint@370
   678
jacint@370
   679
} //namespace hugo
jacint@370
   680
jacint@370
   681
#endif //PREFLOW_H
jacint@370
   682
jacint@370
   683
jacint@370
   684
jacint@370
   685