Changeset 109:fc5982b39e10 in lemon0.x for src/work/jacint/preflow_hl2.h
 Timestamp:
 02/21/04 22:01:22 (16 years ago)
 Branch:
 default
 Phase:
 public
 Convert:
 svn:c9d7d8f590d60310b91f818b3a526b0e/lemon/trunk@144
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

src/work/jacint/preflow_hl2.h
r105 r109 4 4 by jacint. 5 5 Runs the highest label variant of the preflow push algorithm with 6 running time O(n^2\sqrt(m)), with the 'empty level' and with the 7 heuristic that the bound b on the active nodes is not increased 8 only when b=0, when we put b=2*n2. 9 10 'A' is a parameter for the empty_level heuristic 11 12 Member functions: 13 14 void run() : runs the algorithm 15 16 The following functions should be used after run() was already run. 17 18 T maxflow() : returns the value of a maximum flow 19 20 T flowonedge(EdgeIt e) : for a fixed maximum flow x it returns x(e) 21 22 FlowMap allflow() : returns the fixed maximum flow x 23 24 void mincut(CutMap& M) : sets M to the characteristic vector of a 6 running time O(n^2\sqrt(m)). 7 8 Heuristics: 9 10 gap: we iterate through the nodes for finding the nodes above 11 the gap and under level n. So it is quite slow. 12 numb: we maintain the number of nodes in level i. 13 highest label 14 15 'A' is a parameter for the gap, we only upgrade the nodes to level n, 16 if the gap is under A*n. 17 18 The constructor runs the algorithm. 19 20 Members: 21 22 T maxFlow() : returns the value of a maximum flow 23 24 T flowOnEdge(EdgeIt e) : for a fixed maximum flow x it returns x(e) 25 26 FlowMap Flow() : returns the fixed maximum flow x 27 28 void minCut(CutMap& M) : sets M to the characteristic vector of a 25 29 minimum cut. M should be a map of bools initialized to false. 26 30 27 void min _mincut(CutMap& M) : sets M to the characteristic vector of the31 void minMinCut(CutMap& M) : sets M to the characteristic vector of the 28 32 minimum min cut. M should be a map of bools initialized to false. 29 33 30 void max _mincut(CutMap& M) : sets M to the characteristic vector of the34 void maxMinCut(CutMap& M) : sets M to the characteristic vector of the 31 35 maximum min cut. M should be a map of bools initialized to false. 32 36 … … 36 40 #define PREFLOW_HL2_H 37 41 38 #define A 142 #define A .9 39 43 40 44 #include <vector> … … 45 49 46 50 template <typename Graph, typename T, 47 typename FlowMap=typename Graph::EdgeMap<T>, typename CapMap=typename Graph::EdgeMap<T>,48 typename IntMap=typename Graph::NodeMap<int>, typename TMap=typename Graph::NodeMap<T> >51 typename FlowMap=typename Graph::EdgeMap<T>, 52 typename CapMap=typename Graph::EdgeMap<T> > 49 53 class preflow_hl2 { 50 54 … … 65 69 66 70 preflow_hl2(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity) : 67 G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity) { } 68 69 70 void run() { 71 72 bool no_end=true; 71 G(_G), s(_s), t(_t), flow(_G), capacity(_capacity) { 72 73 73 int n=G.nodeNum(); 74 74 int b=n2; 75 75 /* 76 76 b is a bound on the highest level of an active node. 77 In the beginning it is at most n2.78 77 */ 79 78 80 IntMaplevel(G,n);81 TMapexcess(G);82 79 typename Graph::NodeMap<int> level(G,n); 80 typename Graph::NodeMap<T> excess(G); 81 83 82 std::vector<int> numb(n); 84 83 /* … … 111 110 } 112 111 } 113 112 114 113 level.set(s,n); 115 114 … … 128 127 } 129 128 } 130 129 131 130 /* 132 131 End of preprocessing … … 134 133 135 134 136 137 135 /* 138 136 Push/relabel on the highest level active nodes. 139 */ 137 */ 140 138 /*While there exists an active node.*/ 141 139 while (b) { 142 if ( stack[b].empty() ) { 143 if ( b==1 ) { 144 if ( !no_end ) break; 145 else { 146 b=2*n2; 147 no_end=false; 148 } 149 } 140 if ( stack[b].empty() ) { 150 141 b; 151 } else { 152 153 no_end=true; 154 155 NodeIt w=stack[b].top(); //w is a highest label active node. 156 stack[b].pop(); 157 int lev=level.get(w); 158 int exc=excess.get(w); 159 int newlevel=2*n; //In newlevel we bound the next level of w. 160 161 // if ( level.get(w) < n ) { //Nem tudom ez mukodike 142 continue; 143 } 144 145 NodeIt w=stack[b].top(); 146 stack[b].pop(); 147 int lev=level.get(w); 148 T exc=excess.get(w); 149 int newlevel=2*n; //In newlevel we bound the next level of w. 150 162 151 for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) { 163 152 … … 173 162 /*v becomes active.*/ 174 163 175 intcap=capacity.get(e);176 intflo=flow.get(e);177 intremcap=capflo;164 T cap=capacity.get(e); 165 T flo=flow.get(e); 166 T remcap=capflo; 178 167 179 168 if ( remcap >= exc ) { … … 211 200 /*v becomes active.*/ 212 201 213 intflo=flow.get(e);202 T flo=flow.get(e); 214 203 215 204 if ( flo >= exc ) { … … 232 221 233 222 } // if w still has excess after the out edge for cycle 234 235 excess.set(w, exc); 223 224 excess.set(w, exc); 225 226 227 228 229 /* 230 Relabel 231 */ 232 233 if ( exc > 0 ) { 234 //now 'lev' is the old level of w 235 level.set(w,++newlevel); 236 236 237 238 /* 239 Relabel 240 */ 237 if ( lev < n ) { 238 numb[lev]; 239 240 if ( !numb[lev] && lev < A*n ) { //If the level of w gets empty. 241 242 for (EachNodeIt v=G.template first<EachNodeIt>(); v.valid() ; ++v) { 243 if (level.get(v) > lev && level.get(v) < n ) level.set(v,n); 244 } 245 for (int i=lev+1 ; i!=n ; ++i) numb[i]=0; 246 if ( newlevel < n ) newlevel=n; 247 } else { 248 if ( newlevel < n ) ++numb[newlevel]; 249 } 250 } 241 251 242 if ( exc > 0 ) { 243 //now 'lev' is the old level of w 244 level.set(w,++newlevel); 245 246 if ( lev < n ) { 247 numb[lev]; 248 249 if ( !numb[lev] && lev < A*n ) { //If the level of w gets empty. 250 251 for (EachNodeIt v=G.template first<EachNodeIt>(); v.valid() ; ++v) { 252 if (level.get(v) > lev && level.get(v) < n ) level.set(v,n); 253 } 254 for (int i=lev+1 ; i!=n ; ++i) numb[i]=0; 255 if ( newlevel < n ) newlevel=n; 256 } else { 257 if ( newlevel < n ) ++numb[newlevel]; 258 } 259 } 260 261 stack[newlevel].push(w); 262 263 } 264 265 } // if stack[b] is nonempty 266 252 stack[newlevel].push(w); 253 b=newlevel; 254 255 } 256 267 257 } // while(b) 268 269 258 259 270 260 value = excess.get(t); 271 261 /*Max flow value.*/ … … 282 272 */ 283 273 284 T max flow() {274 T maxFlow() { 285 275 return value; 286 276 } … … 289 279 290 280 /* 291 For the maximum flow x found by the algorithm, it returns the flow value on Edge e, i.e. x(e). 281 For the maximum flow x found by the algorithm, 282 it returns the flow value on edge e, i.e. x(e). 292 283 */ 293 284 294 T flow onedge(EdgeIt e) {285 T flowOnEdge(const EdgeIt e) { 295 286 return flow.get(e); 296 287 } … … 302 293 */ 303 294 304 FlowMap allflow() {295 FlowMap Flow() { 305 296 return flow; 306 297 } … … 314 305 315 306 template<typename CutMap> 316 void min cut(CutMap& M) {307 void minCut(CutMap& M) { 317 308 318 309 std::queue<NodeIt> queue; … … 324 315 NodeIt w=queue.front(); 325 316 queue.pop(); 326 317 327 318 for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) { 328 319 NodeIt v=G.head(e); … … 339 330 M.set(v, true); 340 331 } 341 } 332 } 342 333 343 334 } 344 345 335 } 346 336 … … 353 343 354 344 template<typename CutMap> 355 void max _mincut(CutMap& M) {345 void maxMinCut(CutMap& M) { 356 346 357 347 std::queue<NodeIt> queue; … … 390 380 391 381 template<typename CutMap> 392 void min _mincut(CutMap& M) {393 min cut(M);382 void minMinCut(CutMap& M) { 383 minCut(M); 394 384 } 395 385 … … 397 387 398 388 }; 399 }//namespace hugo389 }//namespace marci 400 390 #endif 401 391
Note: See TracChangeset
for help on using the changeset viewer.