src/work/jacint/preflow.h
changeset 220 7deda4d6a07a
parent 174 44700ed9ffaa
child 278 c11f84e3da21
equal deleted inserted replaced
2:f580c8af34d8 3:4bb32efc6746
     1 // -*- C++ -*-
     1 // -*- C++ -*-
     2 /*
     2 /*
     3 preflow.h
       
     4 by jacint. 
       
     5 Heuristics: 
     3 Heuristics: 
     6  2 phase
     4  2 phase
     7  gap
     5  gap
     8  list 'level_list' on the nodes on level i implemented by hand
     6  list 'level_list' on the nodes on level i implemented by hand
     9  stack 'active' on the active nodes on level i implemented by hand
     7  stack 'active' on the active nodes on level i implemented by hand
    10  runs heuristic 'highest label' for H1*n relabels
     8  runs heuristic 'highest label' for H1*n relabels
    11  runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
     9  runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
    12  
    10  
    13 Parameters H0 and H1 are initialized to 20 and 10.
    11 Parameters H0 and H1 are initialized to 20 and 10.
    14 
    12 
    15 The best preflow I could ever write.
    13 Constructors:
    16 
    14 
    17 The constructor runs the algorithm.
    15 Preflow(Graph, Node, Node, CapMap, FlowMap)
    18 
    16 
    19 Members:
    17 Members:
    20 
    18 
    21 T maxFlow() : returns the value of a maximum flow
    19 void run()
    22 
    20 
    23 T flowOnEdge(EdgeIt e) : for a fixed maximum flow x it returns x(e) 
    21 T flowValue() : returns the value of a maximum flow
    24 
       
    25 FlowMap Flow() : returns the fixed maximum flow x
       
    26 
    22 
    27 void minMinCut(CutMap& M) : sets M to the characteristic vector of the 
    23 void minMinCut(CutMap& M) : sets M to the characteristic vector of the 
    28      minimum min cut. M should be a map of bools initialized to false.
    24      minimum min cut. M should be a map of bools initialized to false.
    29 
    25 
    30 void maxMinCut(CutMap& M) : sets M to the characteristic vector of the 
    26 void maxMinCut(CutMap& M) : sets M to the characteristic vector of the 
    33 void minCut(CutMap& M) : sets M to the characteristic vector of 
    29 void minCut(CutMap& M) : sets M to the characteristic vector of 
    34      a min cut. M should be a map of bools initialized to false.
    30      a min cut. M should be a map of bools initialized to false.
    35 
    31 
    36 */
    32 */
    37 
    33 
    38 #ifndef PREFLOW_H
    34 #ifndef HUGO_PREFLOW_H
    39 #define PREFLOW_H
    35 #define HUGO_PREFLOW_H
    40 
    36 
    41 #define H0 20
    37 #define H0 20
    42 #define H1 1
    38 #define H1 1
    43 
    39 
    44 #include <vector>
    40 #include <vector>
    45 #include <queue>
    41 #include <queue>
    46 
       
    47 #include <time_measure.h>
       
    48 
    42 
    49 namespace hugo {
    43 namespace hugo {
    50 
    44 
    51   template <typename Graph, typename T, 
    45   template <typename Graph, typename T, 
    52     typename FlowMap=typename Graph::EdgeMap<T>,
    46     typename FlowMap=typename Graph::EdgeMap<T>,
    53     typename CapMap=typename Graph::EdgeMap<T> >
    47     typename CapMap=typename Graph::EdgeMap<T> >
    54   class preflow {
    48   class Preflow {
    55     
    49     
       
    50     typedef typename Graph::Node Node;
       
    51     typedef typename Graph::Edge Edge;
    56     typedef typename Graph::NodeIt NodeIt;
    52     typedef typename Graph::NodeIt NodeIt;
    57     typedef typename Graph::EdgeIt EdgeIt;
       
    58     typedef typename Graph::EachNodeIt EachNodeIt;
       
    59     typedef typename Graph::OutEdgeIt OutEdgeIt;
    53     typedef typename Graph::OutEdgeIt OutEdgeIt;
    60     typedef typename Graph::InEdgeIt InEdgeIt;
    54     typedef typename Graph::InEdgeIt InEdgeIt;
    61     
    55     
    62     Graph& G;
    56     const Graph& G;
    63     NodeIt s;
    57     Node s;
    64     NodeIt t;
    58     Node t;
    65     FlowMap flow;
    59     FlowMap& flow;
    66     CapMap& capacity;  
    60     const CapMap& capacity;  
    67     T value;
    61     T value;
    68 
    62 
    69   public:
    63   public:
    70     double time;
    64     Preflow(Graph& _G, Node _s, Node _t, CapMap& _capacity, FlowMap& _flow ) :
    71     preflow(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity ) :
    65       G(_G), s(_s), t(_t), flow(_flow), capacity(_capacity) {}
    72       G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity)
    66 
    73     {
    67 
       
    68     void run() {
    74 
    69 
    75       bool phase=0;        //phase 0 is the 1st phase, phase 1 is the 2nd
    70       bool phase=0;        //phase 0 is the 1st phase, phase 1 is the 2nd
    76       int n=G.nodeNum(); 
    71       int n=G.nodeNum(); 
    77       int heur0=(int)(H0*n);  //time while running 'bound decrease' 
    72       int heur0=(int)(H0*n);  //time while running 'bound decrease' 
    78       int heur1=(int)(H1*n);  //time while running 'highest label'
    73       int heur1=(int)(H1*n);  //time while running 'highest label'
    92       int b=k;    //bound on the highest level under n of an active node
    87       int b=k;    //bound on the highest level under n of an active node
    93       
    88       
    94       typename Graph::NodeMap<int> level(G,n);      
    89       typename Graph::NodeMap<int> level(G,n);      
    95       typename Graph::NodeMap<T> excess(G); 
    90       typename Graph::NodeMap<T> excess(G); 
    96 
    91 
    97       std::vector<NodeIt> active(n);
    92       std::vector<Node> active(n,INVALID);
    98       typename Graph::NodeMap<NodeIt> next(G);
    93       typename Graph::NodeMap<Node> next(G,INVALID);
    99       //Stack of the active nodes in level i < n.
    94       //Stack of the active nodes in level i < n.
   100       //We use it in both phases.
    95       //We use it in both phases.
   101 
    96 
   102       typename Graph::NodeMap<NodeIt> left(G);
    97       typename Graph::NodeMap<Node> left(G,INVALID);
   103       typename Graph::NodeMap<NodeIt> right(G);
    98       typename Graph::NodeMap<Node> right(G,INVALID);
   104       std::vector<NodeIt> level_list(n);
    99       std::vector<Node> level_list(n,INVALID);
   105       /*
   100       /*
   106 	List of the nodes in level i<n.
   101 	List of the nodes in level i<n.
   107       */
   102       */
   108 
   103 
   109       /*Reverse_bfs from t, to find the starting level.*/
   104       /*Reverse_bfs from t, to find the starting level.*/
   110       level.set(t,0);
   105       level.set(t,0);
   111       std::queue<NodeIt> bfs_queue;
   106       std::queue<Node> bfs_queue;
   112       bfs_queue.push(t);
   107       bfs_queue.push(t);
   113 
   108 
   114       while (!bfs_queue.empty()) {
   109       while (!bfs_queue.empty()) {
   115 
   110 
   116 	NodeIt v=bfs_queue.front();	
   111 	Node v=bfs_queue.front();	
   117 	bfs_queue.pop();
   112 	bfs_queue.pop();
   118 	int l=level.get(v)+1;
   113 	int l=level[v]+1;
   119 
   114 
   120 	for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
   115 	InEdgeIt e;
   121 	  NodeIt w=G.tail(e);
   116 	for(G.first(e,v); G.valid(e); G.next(e)) {
   122 	  if ( level.get(w) == n && w != s ) {
   117 	  Node w=G.tail(e);
       
   118 	  if ( level[w] == n && w != s ) {
   123 	    bfs_queue.push(w);
   119 	    bfs_queue.push(w);
   124 	    NodeIt first=level_list[l];
   120 	    Node first=level_list[l];
   125 	    if ( first != 0 ) left.set(first,w);
   121 	    if ( G.valid(first) ) left.set(first,w);
   126 	    right.set(w,first);
   122 	    right.set(w,first);
   127 	    level_list[l]=w;
   123 	    level_list[l]=w;
   128 	    level.set(w, l);
   124 	    level.set(w, l);
   129 	  }
   125 	  }
   130 	}
   126 	}
   132       
   128       
   133       level.set(s,n);
   129       level.set(s,n);
   134       
   130       
   135 
   131 
   136       /* Starting flow. It is everywhere 0 at the moment. */     
   132       /* Starting flow. It is everywhere 0 at the moment. */     
   137       for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e) 
   133       OutEdgeIt e;
       
   134       for(G.first(e,s); G.valid(e); G.next(e)) 
   138 	{
   135 	{
   139 	  T c=capacity.get(e);
   136 	  T c=capacity[e];
   140 	  if ( c == 0 ) continue;
   137 	  if ( c == 0 ) continue;
   141 	  NodeIt w=G.head(e);
   138 	  Node w=G.head(e);
   142 	  if ( level.get(w) < n ) {	  
   139 	  if ( level[w] < n ) {	  
   143 	    if ( excess.get(w) == 0 && w!=t ) {
   140 	    if ( excess[w] == 0 && w!=t ) {
   144 	      next.set(w,active[level.get(w)]);
   141 	      next.set(w,active[level[w]]);
   145 	      active[level.get(w)]=w;
   142 	      active[level[w]]=w;
   146 	    }
   143 	    }
   147 	    flow.set(e, c); 
   144 	    flow.set(e, c); 
   148 	    excess.set(w, excess.get(w)+c);
   145 	    excess.set(w, excess[w]+c);
   149 	  }
   146 	  }
   150 	}
   147 	}
   151 
   148 
   152       /* 
   149       /* 
   153 	 End of preprocessing 
   150 	 End of preprocessing 
   166 	  if ( !what_heur && !end && k > 0 ) {
   163 	  if ( !what_heur && !end && k > 0 ) {
   167 	    b=k;
   164 	    b=k;
   168 	    end=true;
   165 	    end=true;
   169 	  } else {
   166 	  } else {
   170 	    phase=1;
   167 	    phase=1;
   171 	    time=currTime();
       
   172 	    level.set(s,0);
   168 	    level.set(s,0);
   173 	    std::queue<NodeIt> bfs_queue;
   169 	    std::queue<Node> bfs_queue;
   174 	    bfs_queue.push(s);
   170 	    bfs_queue.push(s);
   175 	    
   171 	    
   176 	    while (!bfs_queue.empty()) {
   172 	    while (!bfs_queue.empty()) {
   177 	      
   173 	      
   178 	      NodeIt v=bfs_queue.front();	
   174 	      Node v=bfs_queue.front();	
   179 	      bfs_queue.pop();
   175 	      bfs_queue.pop();
   180 	      int l=level.get(v)+1;
   176 	      int l=level[v]+1;
   181 	      
   177 	      
   182 	      for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
   178 	      InEdgeIt e;
   183 		if ( capacity.get(e) == flow.get(e) ) continue;
   179 	      for(G.first(e,v); G.valid(e); G.next(e)) {
   184 		NodeIt u=G.tail(e);
   180 		if ( capacity[e] == flow[e] ) continue;
   185 		if ( level.get(u) >= n ) { 
   181 		Node u=G.tail(e);
       
   182 		if ( level[u] >= n ) { 
   186 		  bfs_queue.push(u);
   183 		  bfs_queue.push(u);
   187 		  level.set(u, l);
   184 		  level.set(u, l);
   188 		  if ( excess.get(u) > 0 ) {
   185 		  if ( excess[u] > 0 ) {
   189 		    next.set(u,active[l]);
   186 		    next.set(u,active[l]);
   190 		    active[l]=u;
   187 		    active[l]=u;
   191 		  }
   188 		  }
   192 		}
   189 		}
   193 	      }
   190 	      }
   194 	    
   191 	    
   195 	      for(OutEdgeIt e=G.template first<OutEdgeIt>(v); e.valid(); ++e) {
   192 	      OutEdgeIt f;
   196 		if ( 0 == flow.get(e) ) continue;
   193 	      for(G.first(f,v); G.valid(f); G.next(f)) {
   197 		NodeIt u=G.head(e);
   194 		if ( 0 == flow[f] ) continue;
   198 		if ( level.get(u) >= n ) { 
   195 		Node u=G.head(f);
       
   196 		if ( level[u] >= n ) { 
   199 		  bfs_queue.push(u);
   197 		  bfs_queue.push(u);
   200 		  level.set(u, l);
   198 		  level.set(u, l);
   201 		  if ( excess.get(u) > 0 ) {
   199 		  if ( excess[u] > 0 ) {
   202 		    next.set(u,active[l]);
   200 		    next.set(u,active[l]);
   203 		    active[l]=u;
   201 		    active[l]=u;
   204 		  }
   202 		  }
   205 		}
   203 		}
   206 	      }
   204 	      }
   209 	    }
   207 	    }
   210 	    
   208 	    
   211 	}
   209 	}
   212 	  
   210 	  
   213 	  
   211 	  
   214 	if ( active[b] == 0 ) --b; 
   212 	if ( !G.valid(active[b]) ) --b; 
   215 	else {
   213 	else {
   216 	  end=false;  
   214 	  end=false;  
   217 
   215 
   218 	  NodeIt w=active[b];
   216 	  Node w=active[b];
   219 	  active[b]=next.get(w);
   217 	  active[b]=next[w];
   220 	  int lev=level.get(w);
   218 	  int lev=level[w];
   221 	  T exc=excess.get(w);
   219 	  T exc=excess[w];
   222 	  int newlevel=n;       //bound on the next level of w
   220 	  int newlevel=n;       //bound on the next level of w
   223 	  
   221 	  
   224 	  for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) {
   222 	  OutEdgeIt e;
   225 	    
   223 	  for(G.first(e,w); G.valid(e); G.next(e)) {
   226 	    if ( flow.get(e) == capacity.get(e) ) continue; 
   224 	    
   227 	    NodeIt v=G.head(e);            
   225 	    if ( flow[e] == capacity[e] ) continue; 
       
   226 	    Node v=G.head(e);            
   228 	    //e=wv	    
   227 	    //e=wv	    
   229 	    
   228 	    
   230 	    if( lev > level.get(v) ) {      
   229 	    if( lev > level[v] ) {      
   231 	      /*Push is allowed now*/
   230 	      /*Push is allowed now*/
   232 	      
   231 	      
   233 	      if ( excess.get(v)==0 && v!=t && v!=s ) {
   232 	      if ( excess[v]==0 && v!=t && v!=s ) {
   234 		int lev_v=level.get(v);
   233 		int lev_v=level[v];
   235 		next.set(v,active[lev_v]);
   234 		next.set(v,active[lev_v]);
   236 		active[lev_v]=v;
   235 		active[lev_v]=v;
   237 	      }
   236 	      }
   238 	      
   237 	      
   239 	      T cap=capacity.get(e);
   238 	      T cap=capacity[e];
   240 	      T flo=flow.get(e);
   239 	      T flo=flow[e];
   241 	      T remcap=cap-flo;
   240 	      T remcap=cap-flo;
   242 	      
   241 	      
   243 	      if ( remcap >= exc ) {       
   242 	      if ( remcap >= exc ) {       
   244 		/*A nonsaturating push.*/
   243 		/*A nonsaturating push.*/
   245 		
   244 		
   246 		flow.set(e, flo+exc);
   245 		flow.set(e, flo+exc);
   247 		excess.set(v, excess.get(v)+exc);
   246 		excess.set(v, excess[v]+exc);
   248 		exc=0;
   247 		exc=0;
   249 		break; 
   248 		break; 
   250 		
   249 		
   251 	      } else { 
   250 	      } else { 
   252 		/*A saturating push.*/
   251 		/*A saturating push.*/
   253 		
   252 		
   254 		flow.set(e, cap);
   253 		flow.set(e, cap);
   255 		excess.set(v, excess.get(v)+remcap);
   254 		excess.set(v, excess[v]+remcap);
   256 		exc-=remcap;
   255 		exc-=remcap;
   257 	      }
   256 	      }
   258 	    } else if ( newlevel > level.get(v) ){
   257 	    } else if ( newlevel > level[v] ){
   259 	      newlevel = level.get(v);
   258 	      newlevel = level[v];
   260 	    }	    
   259 	    }	    
   261 	    
   260 	    
   262 	  } //for out edges wv 
   261 	  } //for out edges wv 
   263 	
   262 	
   264 	
   263 	
   265 	if ( exc > 0 ) {	
   264 	if ( exc > 0 ) {	
   266 	  for( InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) {
   265 	  InEdgeIt e;
   267 	    
   266 	  for(G.first(e,w); G.valid(e); G.next(e)) {
   268 	    if( flow.get(e) == 0 ) continue; 
   267 	    
   269 	    NodeIt v=G.tail(e);  
   268 	    if( flow[e] == 0 ) continue; 
       
   269 	    Node v=G.tail(e);  
   270 	    //e=vw
   270 	    //e=vw
   271 	    
   271 	    
   272 	    if( lev > level.get(v) ) {  
   272 	    if( lev > level[v] ) {  
   273 	      /*Push is allowed now*/
   273 	      /*Push is allowed now*/
   274 	      
   274 	      
   275 	      if ( excess.get(v)==0 && v!=t && v!=s ) {
   275 	      if ( excess[v]==0 && v!=t && v!=s ) {
   276 		int lev_v=level.get(v);
   276 		int lev_v=level[v];
   277 		next.set(v,active[lev_v]);
   277 		next.set(v,active[lev_v]);
   278 		active[lev_v]=v;
   278 		active[lev_v]=v;
   279 	      }
   279 	      }
   280 	      
   280 	      
   281 	      T flo=flow.get(e);
   281 	      T flo=flow[e];
   282 	      
   282 	      
   283 	      if ( flo >= exc ) { 
   283 	      if ( flo >= exc ) { 
   284 		/*A nonsaturating push.*/
   284 		/*A nonsaturating push.*/
   285 		
   285 		
   286 		flow.set(e, flo-exc);
   286 		flow.set(e, flo-exc);
   287 		excess.set(v, excess.get(v)+exc);
   287 		excess.set(v, excess[v]+exc);
   288 		exc=0;
   288 		exc=0;
   289 		break; 
   289 		break; 
   290 	      } else {                                               
   290 	      } else {                                               
   291 		/*A saturating push.*/
   291 		/*A saturating push.*/
   292 		
   292 		
   293 		excess.set(v, excess.get(v)+flo);
   293 		excess.set(v, excess[v]+flo);
   294 		exc-=flo;
   294 		exc-=flo;
   295 		flow.set(e,0);
   295 		flow.set(e,0);
   296 	      }  
   296 	      }  
   297 	    } else if ( newlevel > level.get(v) ) {
   297 	    } else if ( newlevel > level[v] ) {
   298 	      newlevel = level.get(v);
   298 	      newlevel = level[v];
   299 	    }	    
   299 	    }	    
   300 	  } //for in edges vw
   300 	  } //for in edges vw
   301 	  
   301 	  
   302 	} // if w still has excess after the out edge for cycle
   302 	} // if w still has excess after the out edge for cycle
   303 	
   303 	
   316 	    next.set(w,active[newlevel]);
   316 	    next.set(w,active[newlevel]);
   317 	    active[newlevel]=w;
   317 	    active[newlevel]=w;
   318 	    b=newlevel;
   318 	    b=newlevel;
   319 	  } else {
   319 	  } else {
   320 	    //unlacing starts
   320 	    //unlacing starts
   321 	    NodeIt right_n=right.get(w);
   321 	    Node right_n=right[w];
   322 	    NodeIt left_n=left.get(w);
   322 	    Node left_n=left[w];
   323 
   323 
   324 	    if ( right_n != 0 ) {
   324 	    if ( G.valid(right_n) ) {
   325 	      if ( left_n != 0 ) {
   325 	      if ( G.valid(left_n) ) {
   326 		right.set(left_n, right_n);
   326 		right.set(left_n, right_n);
   327 		left.set(right_n, left_n);
   327 		left.set(right_n, left_n);
   328 	      } else {
   328 	      } else {
   329 		level_list[lev]=right_n;   
   329 		level_list[lev]=right_n;   
   330 		left.set(right_n, 0);
   330 		left.set(right_n, INVALID);
   331 	      } 
   331 	      } 
   332 	    } else {
   332 	    } else {
   333 	      if ( left_n != 0 ) {
   333 	      if ( G.valid(left_n) ) {
   334 		right.set(left_n, 0);
   334 		right.set(left_n, INVALID);
   335 	      } else { 
   335 	      } else { 
   336 		level_list[lev]=0;   
   336 		level_list[lev]=INVALID;   
   337 
       
   338 	      } 
   337 	      } 
   339 	    } 
   338 	    } 
   340 	    //unlacing ends
   339 	    //unlacing ends
   341 		
   340 		
   342 	    //gapping starts
   341 	    //gapping starts
   343 	    if ( level_list[lev]==0 ) {
   342 	    if ( !G.valid(level_list[lev]) ) {
   344 	      
   343 	      
   345 	      for (int i=lev; i!=k ; ) {
   344 	      for (int i=lev; i!=k ; ) {
   346 		NodeIt v=level_list[++i];
   345 		Node v=level_list[++i];
   347 		while ( v != 0 ) {
   346 		while ( G.valid(v) ) {
   348 		  level.set(v,n);
   347 		  level.set(v,n);
   349 		  v=right.get(v);
   348 		  v=right[v];
   350 		}
   349 		}
   351 		level_list[i]=0;
   350 		level_list[i]=INVALID;
   352 		if ( !what_heur ) active[i]=0;
   351 		if ( !what_heur ) active[i]=INVALID;
   353 	      }	     
   352 	      }	     
   354 
   353 
   355 	      level.set(w,n);
   354 	      level.set(w,n);
   356 	      b=lev-1;
   355 	      b=lev-1;
   357 	      k=b;
   356 	      k=b;
   363 		level.set(w,++newlevel);
   362 		level.set(w,++newlevel);
   364 		next.set(w,active[newlevel]);
   363 		next.set(w,active[newlevel]);
   365 		active[newlevel]=w;
   364 		active[newlevel]=w;
   366 		if ( what_heur ) b=newlevel;
   365 		if ( what_heur ) b=newlevel;
   367 		if ( k < newlevel ) ++k;
   366 		if ( k < newlevel ) ++k;
   368 		NodeIt first=level_list[newlevel];
   367 		Node first=level_list[newlevel];
   369 		if ( first != 0 ) left.set(first,w);
   368 		if ( G.valid(first) ) left.set(first,w);
   370 		right.set(w,first);
   369 		right.set(w,first);
   371 		left.set(w,0);
   370 		left.set(w,INVALID);
   372 		level_list[newlevel]=w;
   371 		level_list[newlevel]=w;
   373 	      }
   372 	      }
   374 	    }
   373 	    }
   375 
   374 
   376 
   375 
   396 	}  // if stack[b] is nonempty
   395 	}  // if stack[b] is nonempty
   397 	
   396 	
   398       } // while(true)
   397       } // while(true)
   399 
   398 
   400 
   399 
   401       value = excess.get(t);
   400       value = excess[t];
   402       /*Max flow value.*/
   401       /*Max flow value.*/
   403      
   402      
   404     } //void run()
   403     } //void run()
   405 
   404 
   406 
   405 
   409 
   408 
   410     /*
   409     /*
   411       Returns the maximum value of a flow.
   410       Returns the maximum value of a flow.
   412      */
   411      */
   413 
   412 
   414     T maxFlow() {
   413     T flowValue() {
   415       return value;
   414       return value;
   416     }
   415     }
   417 
       
   418 
       
   419 
       
   420     /*
       
   421       For the maximum flow x found by the algorithm, 
       
   422       it returns the flow value on edge e, i.e. x(e). 
       
   423     */
       
   424    
       
   425     T flowOnEdge(EdgeIt e) {
       
   426       return flow.get(e);
       
   427     }
       
   428 
       
   429 
   416 
   430 
   417 
   431     FlowMap Flow() {
   418     FlowMap Flow() {
   432       return flow;
   419       return flow;
   433       }
   420       }
   434 
   421 
   435 
   422 
   436     
   423     
   437     void Flow(FlowMap& _flow ) {
   424     void Flow(FlowMap& _flow ) {
   438       for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v)
   425       NodeIt v;
   439 	_flow.set(v,flow.get(v));
   426       for(G.first(v) ; G.valid(v); G.next(v))
   440 	}
   427 	_flow.set(v,flow[v]);
       
   428     }
   441 
   429 
   442 
   430 
   443 
   431 
   444     /*
   432     /*
   445       Returns the minimum min cut, by a bfs from s in the residual graph.
   433       Returns the minimum min cut, by a bfs from s in the residual graph.
   446     */
   434     */
   447    
   435    
   448     template<typename _CutMap>
   436     template<typename _CutMap>
   449     void minMinCut(_CutMap& M) {
   437     void minMinCut(_CutMap& M) {
   450     
   438     
   451       std::queue<NodeIt> queue;
   439       std::queue<Node> queue;
   452       
   440       
   453       M.set(s,true);      
   441       M.set(s,true);      
   454       queue.push(s);
   442       queue.push(s);
   455 
   443 
   456       while (!queue.empty()) {
   444       while (!queue.empty()) {
   457         NodeIt w=queue.front();
   445         Node w=queue.front();
   458 	queue.pop();
   446 	queue.pop();
   459 
   447 
   460 	for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
   448 	OutEdgeIt e;
   461 	  NodeIt v=G.head(e);
   449 	for(G.first(e,w) ; G.valid(e); G.next(e)) {
   462 	  if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
   450 	  Node v=G.head(e);
       
   451 	  if (!M[v] && flow[e] < capacity[e] ) {
   463 	    queue.push(v);
   452 	    queue.push(v);
   464 	    M.set(v, true);
   453 	    M.set(v, true);
   465 	  }
   454 	  }
   466 	} 
   455 	} 
   467 
   456 
   468 	for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
   457 	InEdgeIt f;
   469 	  NodeIt v=G.tail(e);
   458 	for(G.first(f,w) ; G.valid(f); G.next(f)) {
   470 	  if (!M.get(v) && flow.get(e) > 0 ) {
   459 	  Node v=G.tail(f);
       
   460 	  if (!M[v] && flow[f] > 0 ) {
   471 	    queue.push(v);
   461 	    queue.push(v);
   472 	    M.set(v, true);
   462 	    M.set(v, true);
   473 	  }
   463 	  }
   474 	} 
   464 	} 
   475       }
   465       }
   483     */
   473     */
   484     
   474     
   485     template<typename _CutMap>
   475     template<typename _CutMap>
   486     void maxMinCut(_CutMap& M) {
   476     void maxMinCut(_CutMap& M) {
   487     
   477     
   488       std::queue<NodeIt> queue;
   478       std::queue<Node> queue;
   489       
   479       
   490       M.set(t,true);        
   480       M.set(t,true);        
   491       queue.push(t);
   481       queue.push(t);
   492 
   482 
   493       while (!queue.empty()) {
   483       while (!queue.empty()) {
   494         NodeIt w=queue.front();
   484         Node w=queue.front();
   495 	queue.pop();
   485 	queue.pop();
   496 
   486 
   497 	for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) {
   487 
   498 	  NodeIt v=G.tail(e);
   488 	InEdgeIt e;
   499 	  if (!M.get(v) && flow.get(e) < capacity.get(e) ) {
   489 	for(G.first(e,w) ; G.valid(e); G.next(e)) {
       
   490 	  Node v=G.tail(e);
       
   491 	  if (!M[v] && flow[e] < capacity[e] ) {
   500 	    queue.push(v);
   492 	    queue.push(v);
   501 	    M.set(v, true);
   493 	    M.set(v, true);
   502 	  }
   494 	  }
   503 	}
   495 	}
   504 
   496 	
   505 	for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) {
   497 	OutEdgeIt f;
   506 	  NodeIt v=G.head(e);
   498 	for(G.first(f,w) ; G.valid(f); G.next(f)) {
   507 	  if (!M.get(v) && flow.get(e) > 0 ) {
   499 	  Node v=G.head(f);
       
   500 	  if (!M[v] && flow[f] > 0 ) {
   508 	    queue.push(v);
   501 	    queue.push(v);
   509 	    M.set(v, true);
   502 	    M.set(v, true);
   510 	  }
   503 	  }
   511 	}
   504 	}
   512       }
   505       }
   513 
   506 
   514       for(EachNodeIt v=G.template first<EachNodeIt>() ; v.valid(); ++v) {
   507       NodeIt v;
   515 	M.set(v, !M.get(v));
   508       for(G.first(v) ; G.valid(v); G.next(v)) {
       
   509 	M.set(v, !M[v]);
   516       }
   510       }
   517 
   511 
   518     }
   512     }
   519 
   513 
   520 
   514