src/hugo/max_flow.h
changeset 747 be163d94c109
parent 735 2859c45c31dd
child 749 8e933219691e
equal deleted inserted replaced
1:5027979724cc 2:b943fe5279af
   138     ///Constructor
   138     ///Constructor
   139 
   139 
   140     ///\todo Document, please.
   140     ///\todo Document, please.
   141     ///
   141     ///
   142     MaxFlow(const Graph& _G, Node _s, Node _t,
   142     MaxFlow(const Graph& _G, Node _s, Node _t,
   143 		   const CapMap& _capacity, FlowMap& _flow) :
   143 	    const CapMap& _capacity, FlowMap& _flow) :
   144       g(&_G), s(_s), t(_t), capacity(&_capacity),
   144       g(&_G), s(_s), t(_t), capacity(&_capacity),
   145       flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0), 
   145       flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0), 
   146       status(AFTER_NOTHING) { }
   146       status(AFTER_NOTHING) { }
   147 
   147 
   148     ///Runs a maximum flow algorithm.
   148     ///Runs a maximum flow algorithm.
   215       int k=n-2;  //bound on the highest level under n containing a node
   215       int k=n-2;  //bound on the highest level under n containing a node
   216       int b=k;    //bound on the highest level under n of an active node
   216       int b=k;    //bound on the highest level under n of an active node
   217 
   217 
   218       VecFirst first(n, INVALID);
   218       VecFirst first(n, INVALID);
   219       NNMap next(*g, INVALID); //maybe INVALID is not needed
   219       NNMap next(*g, INVALID); //maybe INVALID is not needed
   220       //    VecStack active(n);
       
   221 
   220 
   222       NNMap left(*g, INVALID);
   221       NNMap left(*g, INVALID);
   223       NNMap right(*g, INVALID);
   222       NNMap right(*g, INVALID);
   224       VecNode level_list(n,INVALID);
   223       VecNode level_list(n,INVALID);
   225       //List of the nodes in level i<n, set to n.
   224       //List of the nodes in level i<n, set to n.
   252 	    if ( exc > 0 && lev < n && v != t ) 
   251 	    if ( exc > 0 && lev < n && v != t ) 
   253 	      {
   252 	      {
   254 		next.set(v,first[lev]);
   253 		next.set(v,first[lev]);
   255 		first[lev]=v;
   254 		first[lev]=v;
   256 	      }
   255 	      }
   257 	    //	  active[lev].push(v);
       
   258 	  }
   256 	  }
   259 	  break;
   257 	  break;
   260 	}
   258 	}
   261       case GEN_FLOW:
   259       case GEN_FLOW:
   262 	{
   260 	{
   278 	  for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
   276 	  for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
   279 	  break;
   277 	  break;
   280 	}
   278 	}
   281       }
   279       }
   282 
   280 
   283       preflowPreproc(fe, next, first,/*active*/ level_list, left, right);
   281       preflowPreproc(fe, next, first, level_list, left, right);
   284       //End of preprocessing
   282       //End of preprocessing
   285 
   283 
   286 
   284 
   287       //Push/relabel on the highest level active nodes.
   285       //Push/relabel on the highest level active nodes.
   288       while ( true ) {
   286       while ( true ) {
   291 	    b=k;
   289 	    b=k;
   292 	    end=true;
   290 	    end=true;
   293 	  } else break;
   291 	  } else break;
   294 	}
   292 	}
   295 
   293 
   296 	if ( !g->valid(first[b])/*active[b].empty()*/ ) --b;
   294 	if ( !g->valid(first[b]) ) --b;
   297 	else {
   295 	else {
   298 	  end=false;
   296 	  end=false;
   299 	  Node w=first[b];
   297 	  Node w=first[b];
   300 	  first[b]=next[w];
   298 	  first[b]=next[w];
   301 	  /*	Node w=active[b].top();
   299 	  int newlevel=push(w, next, first);
   302 		active[b].pop();*/
   300 	  if ( excess[w] > 0 ) relabel(w, newlevel, next, first, level_list,
   303 	  int newlevel=push(w,/*active*/next, first);
       
   304 	  if ( excess[w] > 0 ) relabel(w, newlevel, /*active*/next, first, level_list,
       
   305 				       left, right, b, k, what_heur);
   301 				       left, right, b, k, what_heur);
   306 
   302 
   307 	  ++numrelabel;
   303 	  ++numrelabel;
   308 	  if ( numrelabel >= heur ) {
   304 	  if ( numrelabel >= heur ) {
   309 	    numrelabel=0;
   305 	    numrelabel=0;
   338       int b=k;    //bound on the highest level under n of an active node
   334       int b=k;    //bound on the highest level under n of an active node
   339 
   335 
   340     
   336     
   341       VecFirst first(n, INVALID);
   337       VecFirst first(n, INVALID);
   342       NNMap next(*g, INVALID); //maybe INVALID is not needed
   338       NNMap next(*g, INVALID); //maybe INVALID is not needed
   343       //    VecStack active(n);
       
   344       level.set(s,0);
   339       level.set(s,0);
   345       std::queue<Node> bfs_queue;
   340       std::queue<Node> bfs_queue;
   346       bfs_queue.push(s);
   341       bfs_queue.push(s);
   347 
   342 
   348       while (!bfs_queue.empty()) {
   343       while (!bfs_queue.empty()) {
   359 	    bfs_queue.push(u);
   354 	    bfs_queue.push(u);
   360 	    level.set(u, l);
   355 	    level.set(u, l);
   361 	    if ( excess[u] > 0 ) {
   356 	    if ( excess[u] > 0 ) {
   362 	      next.set(u,first[l]);
   357 	      next.set(u,first[l]);
   363 	      first[l]=u;
   358 	      first[l]=u;
   364 	      //active[l].push(u);
       
   365 	    }
   359 	    }
   366 	  }
   360 	  }
   367 	}
   361 	}
   368 
   362 
   369 	OutEdgeIt f;
   363 	OutEdgeIt f;
   374 	    bfs_queue.push(u);
   368 	    bfs_queue.push(u);
   375 	    level.set(u, l);
   369 	    level.set(u, l);
   376 	    if ( excess[u] > 0 ) {
   370 	    if ( excess[u] > 0 ) {
   377 	      next.set(u,first[l]);
   371 	      next.set(u,first[l]);
   378 	      first[l]=u;
   372 	      first[l]=u;
   379 	      //active[l].push(u);
       
   380 	    }
   373 	    }
   381 	  }
   374 	  }
   382 	}
   375 	}
   383       }
   376       }
   384       b=n-2;
   377       b=n-2;
   385 
   378 
   386       while ( true ) {
   379       while ( true ) {
   387 
   380 
   388 	if ( b == 0 ) break;
   381 	if ( b == 0 ) break;
   389 
   382 
   390 	if ( !g->valid(first[b])/*active[b].empty()*/ ) --b;
   383 	if ( !g->valid(first[b]) ) --b;
   391 	else {
   384 	else {
   392 
   385 
   393 	  Node w=first[b];
   386 	  Node w=first[b];
   394 	  first[b]=next[w];
   387 	  first[b]=next[w];
   395 	  /*	Node w=active[b].top();
       
   396 		active[b].pop();*/
       
   397 	  int newlevel=push(w,next, first/*active*/);
   388 	  int newlevel=push(w,next, first/*active*/);
   398 
   389 
   399 	  //relabel
   390 	  //relabel
   400 	  if ( excess[w] > 0 ) {
   391 	  if ( excess[w] > 0 ) {
   401 	    level.set(w,++newlevel);
   392 	    level.set(w,++newlevel);
   402 	    next.set(w,first[newlevel]);
   393 	    next.set(w,first[newlevel]);
   403 	    first[newlevel]=w;
   394 	    first[newlevel]=w;
   404 	    //active[newlevel].push(w);
       
   405 	    b=newlevel;
   395 	    b=newlevel;
   406 	  }
   396 	  }
   407 	}  // if stack[b] is nonempty
   397 	}  // if stack[b] is nonempty
   408       } // while(true)
   398       } // while(true)
   409 
   399 
   418     /// It can be called already after running \ref preflowPhase1.
   408     /// It can be called already after running \ref preflowPhase1.
   419     Num flowValue() const {
   409     Num flowValue() const {
   420       Num a=0;
   410       Num a=0;
   421       for(InEdgeIt e(*g,t);g->valid(e);g->next(e)) a+=(*flow)[e];
   411       for(InEdgeIt e(*g,t);g->valid(e);g->next(e)) a+=(*flow)[e];
   422       for(OutEdgeIt e(*g,t);g->valid(e);g->next(e)) a-=(*flow)[e];
   412       for(OutEdgeIt e(*g,t);g->valid(e);g->next(e)) a-=(*flow)[e];
   423 
   413       return a;
   424       //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan   
   414       //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan   
       
   415     }
       
   416     Num flowValue2() const {
       
   417       return excess[t];
       
   418 //       Num a=0;
       
   419 //       for(InEdgeIt e(*g,t);g->valid(e);g->next(e)) a+=(*flow)[e];
       
   420 //       for(OutEdgeIt e(*g,t);g->valid(e);g->next(e)) a-=(*flow)[e];
       
   421 //       return a;
       
   422 //       //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan  
       
   423       
   425     }
   424     }
   426 
   425 
   427     ///Returns a minimum value cut after calling \ref preflowPhase1.
   426     ///Returns a minimum value cut after calling \ref preflowPhase1.
   428 
   427 
   429     ///After the first phase of the preflow algorithm the maximum flow
   428     ///After the first phase of the preflow algorithm the maximum flow
   449 	  }
   448 	  }
   450 	}
   449 	}
   451 	break;
   450 	break;
   452       case AFTER_PRE_FLOW_PHASE_2:
   451       case AFTER_PRE_FLOW_PHASE_2:
   453       case AFTER_NOTHING:
   452       case AFTER_NOTHING:
       
   453       case AFTER_AUGMENTING:
       
   454       case AFTER_FAST_AUGMENTING:
   454 	minMinCut(M);
   455 	minMinCut(M);
   455 	break;
   456 	break;
   456       }
   457       }
   457     }
   458     }
   458 
   459 
   589 	if( lev > level[v] ) { //Push is allowed now
   590 	if( lev > level[v] ) { //Push is allowed now
   590 
   591 
   591 	  if ( excess[v]<=0 && v!=t && v!=s ) {
   592 	  if ( excess[v]<=0 && v!=t && v!=s ) {
   592 	    next.set(v,first[level[v]]);
   593 	    next.set(v,first[level[v]]);
   593 	    first[level[v]]=v;
   594 	    first[level[v]]=v;
   594 	    //	    int lev_v=level[v];
       
   595 	    //active[lev_v].push(v);
       
   596 	  }
   595 	  }
   597 
   596 
   598 	  Num cap=(*capacity)[e];
   597 	  Num cap=(*capacity)[e];
   599 	  Num flo=(*flow)[e];
   598 	  Num flo=(*flow)[e];
   600 	  Num remcap=cap-flo;
   599 	  Num remcap=cap-flo;
   624 	  if( lev > level[v] ) { //Push is allowed now
   623 	  if( lev > level[v] ) { //Push is allowed now
   625 
   624 
   626 	    if ( excess[v]<=0 && v!=t && v!=s ) {
   625 	    if ( excess[v]<=0 && v!=t && v!=s ) {
   627 	      next.set(v,first[level[v]]);
   626 	      next.set(v,first[level[v]]);
   628 	      first[level[v]]=v;
   627 	      first[level[v]]=v;
   629 	      //int lev_v=level[v];
       
   630 	      //active[lev_v].push(v);
       
   631 	    }
   628 	    }
   632 
   629 
   633 	    Num flo=(*flow)[e];
   630 	    Num flo=(*flow)[e];
   634 
   631 
   635 	    if ( flo >= exc ) { //A nonsaturating push.
   632 	    if ( flo >= exc ) { //A nonsaturating push.
   698 	      if ( level[w] < n ) {
   695 	      if ( level[w] < n ) {
   699 		if ( excess[w] <= 0 && w!=t ) 
   696 		if ( excess[w] <= 0 && w!=t ) 
   700 		  {
   697 		  {
   701 		    next.set(w,first[level[w]]);
   698 		    next.set(w,first[level[w]]);
   702 		    first[level[w]]=w;
   699 		    first[level[w]]=w;
   703 		    //active[level[w]].push(w);
       
   704 		  }
   700 		  }
   705 		flow->set(e, c);
   701 		flow->set(e, c);
   706 		excess.set(w, excess[w]+c);
   702 		excess.set(w, excess[w]+c);
   707 	      }
   703 	      }
   708 	    }
   704 	    }
   763 	      if ( level[w] < n ) {
   759 	      if ( level[w] < n ) {
   764 		if ( excess[w] <= 0 && w!=t )
   760 		if ( excess[w] <= 0 && w!=t )
   765 		  {
   761 		  {
   766 		    next.set(w,first[level[w]]);
   762 		    next.set(w,first[level[w]]);
   767 		    first[level[w]]=w;
   763 		    first[level[w]]=w;
   768 		    //active[level[w]].push(w);
       
   769 		  }   
   764 		  }   
   770 		flow->set(e, (*capacity)[e]);
   765 		flow->set(e, (*capacity)[e]);
   771 		excess.set(w, excess[w]+rem);
   766 		excess.set(w, excess[w]+rem);
   772 	      }
   767 	      }
   773 	    }
   768 	    }
   780 	      if ( level[w] < n ) {
   775 	      if ( level[w] < n ) {
   781 		if ( excess[w] <= 0 && w!=t )
   776 		if ( excess[w] <= 0 && w!=t )
   782 		  {
   777 		  {
   783 		    next.set(w,first[level[w]]);
   778 		    next.set(w,first[level[w]]);
   784 		    first[level[w]]=w;
   779 		    first[level[w]]=w;
   785 		    //active[level[w]].push(w);
       
   786 		  }   
   780 		  }   
   787 		excess.set(w, excess[w]+(*flow)[f]);
   781 		excess.set(w, excess[w]+(*flow)[f]);
   788 		flow->set(f, 0);
   782 		flow->set(f, 0);
   789 	      }
   783 	      }
   790 	    }
   784 	    }
   832 	    level.set(v,n);
   826 	    level.set(v,n);
   833 	    v=right[v];
   827 	    v=right[v];
   834 	  }
   828 	  }
   835 	  level_list[i]=INVALID;
   829 	  level_list[i]=INVALID;
   836 	  if ( !what_heur ) first[i]=INVALID;
   830 	  if ( !what_heur ) first[i]=INVALID;
   837 	  /*{
       
   838 	    while ( !active[i].empty() ) {
       
   839 	    active[i].pop();    //FIXME: ezt szebben kene
       
   840 	    }
       
   841 	    }*/
       
   842 	}
   831 	}
   843 
   832 
   844 	level.set(w,n);
   833 	level.set(w,n);
   845 	b=lev-1;
   834 	b=lev-1;
   846 	k=b;
   835 	k=b;
   851 	if ( newlevel == n ) level.set(w,n);
   840 	if ( newlevel == n ) level.set(w,n);
   852 	else {
   841 	else {
   853 	  level.set(w,++newlevel);
   842 	  level.set(w,++newlevel);
   854 	  next.set(w,first[newlevel]);
   843 	  next.set(w,first[newlevel]);
   855 	  first[newlevel]=w;
   844 	  first[newlevel]=w;
   856 	  //	  active[newlevel].push(w);
       
   857 	  if ( what_heur ) b=newlevel;
   845 	  if ( what_heur ) b=newlevel;
   858 	  if ( k < newlevel ) ++k;      //now k=newlevel
   846 	  if ( k < newlevel ) ++k;      //now k=newlevel
   859 	  Node z=level_list[newlevel];
   847 	  Node z=level_list[newlevel];
   860 	  if ( g->valid(z) ) left.set(z,w);
   848 	  if ( g->valid(z) ) left.set(z,w);
   861 	  right.set(w,z);
   849 	  right.set(w,z);