244 S get(NodeIt nit) const { return node_map.get(nit); } |
244 S get(NodeIt nit) const { return node_map.get(nit); } |
245 }; |
245 }; |
246 }; |
246 }; |
247 |
247 |
248 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap> |
248 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap> |
249 class ResGraph3 { |
249 class ResGraphWrapper { |
250 public: |
250 public: |
251 typedef typename Graph::NodeIt NodeIt; |
251 typedef typename Graph::NodeIt NodeIt; |
252 typedef typename Graph::EachNodeIt EachNodeIt; |
252 typedef typename Graph::EachNodeIt EachNodeIt; |
253 |
|
254 private: |
253 private: |
255 //typedef typename Graph::SymEdgeIt OldSymEdgeIt; |
|
256 typedef typename Graph::OutEdgeIt OldOutEdgeIt; |
254 typedef typename Graph::OutEdgeIt OldOutEdgeIt; |
257 typedef typename Graph::InEdgeIt OldInEdgeIt; |
255 typedef typename Graph::InEdgeIt OldInEdgeIt; |
258 const Graph& G; |
256 const Graph* G; |
259 FlowMap& flow; |
257 FlowMap* flow; |
260 const CapacityMap& capacity; |
258 const CapacityMap* capacity; |
261 public: |
259 public: |
262 ResGraph3(const Graph& _G, FlowMap& _flow, |
260 ResGraphWrapper(const Graph& _G, FlowMap& _flow, |
263 const CapacityMap& _capacity) : |
261 const CapacityMap& _capacity) : |
264 G(_G), flow(_flow), capacity(_capacity) { } |
262 G(&_G), flow(&_flow), capacity(&_capacity) { } |
265 |
263 // ResGraphWrapper(const ResGraphWrapper& res_graph_wrapper) : |
|
264 // G(res_graph_wrapper.G), flow(res_graph_wrapper.flow), capacity(res_graph_wrapper.capacity) { } |
266 class EdgeIt; |
265 class EdgeIt; |
267 class OutEdgeIt; |
266 class OutEdgeIt; |
268 friend class EdgeIt; |
267 friend class EdgeIt; |
269 friend class OutEdgeIt; |
268 friend class OutEdgeIt; |
270 |
269 |
271 class EdgeIt { |
270 class EdgeIt { |
272 friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>; |
271 friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>; |
273 protected: |
272 protected: |
274 //const ResGraph3<Graph, Number, FlowMap, CapacityMap>* resG; |
273 //const ResGraph3<Graph, Number, FlowMap, CapacityMap>* resG; |
275 const Graph* G; |
274 const Graph* G; |
276 FlowMap* flow; |
275 FlowMap* flow; |
277 const CapacityMap* capacity; |
276 const CapacityMap* capacity; |
278 //OldSymEdgeIt sym; |
277 //OldSymEdgeIt sym; |
279 OldOutEdgeIt out; |
278 OldOutEdgeIt out; |
280 OldInEdgeIt in; |
279 OldInEdgeIt in; |
281 bool out_or_in; //1, iff out |
280 bool out_or_in; //true, iff out |
282 public: |
281 public: |
283 EdgeIt() : out_or_in(1) { } |
282 EdgeIt() : out_or_in(true) { } |
|
283 EdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) : |
|
284 G(&_G), flow(&_flow), capacity(&_capacity), out_or_in(true) { } |
284 //EdgeIt(const EdgeIt& e) : G(e.G), flow(e.flow), capacity(e.capacity), out(e.out), in(e.in), out_or_in(e.out_or_in) { } |
285 //EdgeIt(const EdgeIt& e) : G(e.G), flow(e.flow), capacity(e.capacity), out(e.out), in(e.in), out_or_in(e.out_or_in) { } |
285 Number free() const { |
286 Number free() const { |
286 if (out_or_in) { |
287 if (out_or_in) { |
287 return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out)); |
288 return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out)); |
288 } else { |
289 } else { |
315 } |
316 } |
316 std::cout << std::endl; |
317 std::cout << std::endl; |
317 } |
318 } |
318 }; |
319 }; |
319 |
320 |
|
321 Number free(OldOutEdgeIt out) const { |
|
322 return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out)); |
|
323 } |
|
324 Number free(OldInEdgeIt in) const { |
|
325 return (/*resG->*/flow->get(in)); |
|
326 } |
|
327 |
320 class OutEdgeIt : public EdgeIt { |
328 class OutEdgeIt : public EdgeIt { |
321 friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>; |
329 friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>; |
322 public: |
330 public: |
323 OutEdgeIt() { } |
331 OutEdgeIt() { } |
324 private: |
332 private: |
325 OutEdgeIt(const Graph& _G, NodeIt v, FlowMap& _flow, const CapacityMap& _capacity) { |
333 OutEdgeIt(const Graph& _G, NodeIt v, FlowMap& _flow, const CapacityMap& _capacity) : EdgeIt(_G, _flow, _capacity) { |
326 G=&_G; |
|
327 flow=&_flow; |
|
328 capacity=&_capacity; |
|
329 //out=/*resG->*/G->template first<OldOutEdgeIt>(v); |
334 //out=/*resG->*/G->template first<OldOutEdgeIt>(v); |
330 G->getFirst(out, v); |
335 G->getFirst(out, v); |
331 while( out.valid() && !(free()>0) ) { ++out; } |
336 while( out.valid() && !(EdgeIt::free()>0) ) { ++out; } |
332 if (!out.valid()) { |
337 if (!out.valid()) { |
333 out_or_in=0; |
338 out_or_in=0; |
334 //in=/*resG->*/G->template first<OldInEdgeIt>(v); |
339 //in=/*resG->*/G->template first<OldInEdgeIt>(v); |
335 G->getFirst(in, v); |
340 G->getFirst(in, v); |
336 while( in.valid() && !(free()>0) ) { ++in; } |
341 while( in.valid() && !(EdgeIt::free()>0) ) { ++in; } |
337 } |
342 } |
338 } |
343 } |
339 public: |
344 public: |
340 OutEdgeIt& operator++() { |
345 OutEdgeIt& operator++() { |
341 if (out_or_in) { |
346 if (out_or_in) { |
342 NodeIt v=/*resG->*/G->aNode(out); |
347 NodeIt v=/*resG->*/G->aNode(out); |
343 ++out; |
348 ++out; |
344 while( out.valid() && !(free()>0) ) { ++out; } |
349 while( out.valid() && !(EdgeIt::free()>0) ) { ++out; } |
345 if (!out.valid()) { |
350 if (!out.valid()) { |
346 out_or_in=0; |
351 out_or_in=0; |
347 G->getFirst(in, v); //=/*resG->*/G->template first<OldInEdgeIt>(v); |
352 G->getFirst(in, v); //=/*resG->*/G->template first<OldInEdgeIt>(v); |
348 while( in.valid() && !(free()>0) ) { ++in; } |
353 while( in.valid() && !(EdgeIt::free()>0) ) { ++in; } |
349 } |
354 } |
350 } else { |
355 } else { |
351 ++in; |
356 ++in; |
352 while( in.valid() && !(free()>0) ) { ++in; } |
357 while( in.valid() && !(EdgeIt::free()>0) ) { ++in; } |
353 } |
358 } |
354 return *this; |
359 return *this; |
355 } |
360 } |
356 }; |
361 }; |
357 |
362 |
358 class EachEdgeIt : public EdgeIt { |
363 class EachEdgeIt : public EdgeIt { |
|
364 friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>; |
359 typename Graph::EachNodeIt v; |
365 typename Graph::EachNodeIt v; |
360 public: |
366 public: |
361 EachEdgeIt() { } |
367 EachEdgeIt() { } |
362 //EachEdgeIt(const EachEdgeIt& e) : EdgeIt(e), v(e.v) { } |
368 //EachEdgeIt(const EachEdgeIt& e) : EdgeIt(e), v(e.v) { } |
363 EachEdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) { |
369 EachEdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) : EdgeIt(_G, _flow, _capacity) { |
364 G=&_G; |
370 out_or_in=true; |
365 flow=&_flow; |
|
366 capacity=&_capacity; |
|
367 out_or_in=1; |
|
368 G->getFirst(v); |
371 G->getFirst(v); |
369 if (v.valid()) G->getFirst(out, v); else out=OldOutEdgeIt(); |
372 if (v.valid()) G->getFirst(out, v); else out=OldOutEdgeIt(); |
370 while (out.valid() && !(free()>0) ) { ++out; } |
373 while (out.valid() && !(EdgeIt::free()>0) ) { ++out; } |
371 while (v.valid() && !out.valid()) { |
374 while (v.valid() && !out.valid()) { |
372 ++v; |
375 ++v; |
373 if (v.valid()) G->getFirst(out, v); |
376 if (v.valid()) G->getFirst(out, v); |
374 while (out.valid() && !(free()>0) ) { ++out; } |
377 while (out.valid() && !(EdgeIt::free()>0) ) { ++out; } |
375 } |
378 } |
376 if (!out.valid()) { |
379 if (!out.valid()) { |
377 out_or_in=0; |
380 out_or_in=0; |
378 G->getFirst(v); |
381 G->getFirst(v); |
379 if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt(); |
382 if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt(); |
380 while (in.valid() && !(free()>0) ) { ++in; } |
383 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } |
381 while (v.valid() && !in.valid()) { |
384 while (v.valid() && !in.valid()) { |
382 ++v; |
385 ++v; |
383 if (v.valid()) G->getFirst(in, v); |
386 if (v.valid()) G->getFirst(in, v); |
384 while (in.valid() && !(free()>0) ) { ++in; } |
387 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } |
385 } |
388 } |
386 } |
389 } |
387 } |
390 } |
388 EachEdgeIt& operator++() { |
391 EachEdgeIt& operator++() { |
389 if (out_or_in) { |
392 if (out_or_in) { |
390 ++out; |
393 ++out; |
391 while (out.valid() && !(free()>0) ) { ++out; } |
394 while (out.valid() && !(EdgeIt::free()>0) ) { ++out; } |
392 while (v.valid() && !out.valid()) { |
395 while (v.valid() && !out.valid()) { |
393 ++v; |
396 ++v; |
394 if (v.valid()) G->getFirst(out, v); |
397 if (v.valid()) G->getFirst(out, v); |
395 while (out.valid() && !(free()>0) ) { ++out; } |
398 while (out.valid() && !(EdgeIt::free()>0) ) { ++out; } |
396 } |
399 } |
397 if (!out.valid()) { |
400 if (!out.valid()) { |
398 out_or_in=0; |
401 out_or_in=0; |
399 G->getFirst(v); |
402 G->getFirst(v); |
400 if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt(); |
403 if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt(); |
401 while (in.valid() && !(free()>0) ) { ++in; } |
404 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } |
402 while (v.valid() && !in.valid()) { |
405 while (v.valid() && !in.valid()) { |
403 ++v; |
406 ++v; |
404 if (v.valid()) G->getFirst(in, v); |
407 if (v.valid()) G->getFirst(in, v); |
405 while (in.valid() && !(free()>0) ) { ++in; } |
408 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } |
406 } |
409 } |
407 } |
410 } |
408 } else { |
411 } else { |
409 ++in; |
412 ++in; |
410 while (in.valid() && !(free()>0) ) { ++in; } |
413 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } |
411 while (v.valid() && !in.valid()) { |
414 while (v.valid() && !in.valid()) { |
412 ++v; |
415 ++v; |
413 if (v.valid()) G->getFirst(in, v); |
416 if (v.valid()) G->getFirst(in, v); |
414 while (in.valid() && !(free()>0) ) { ++in; } |
417 while (in.valid() && !(EdgeIt::free()>0) ) { ++in; } |
415 } |
418 } |
416 } |
419 } |
417 return *this; |
420 return *this; |
418 } |
421 } |
419 }; |
422 }; |
420 |
423 |
|
424 void getFirst(EachNodeIt& v) const { G->getFirst(v); } |
421 void getFirst(OutEdgeIt& e, NodeIt v) const { |
425 void getFirst(OutEdgeIt& e, NodeIt v) const { |
422 e=OutEdgeIt(G, v, flow, capacity); |
426 e=OutEdgeIt(*G, v, *flow, *capacity); |
423 } |
427 } |
424 void getFirst(EachEdgeIt& e) const { |
428 void getFirst(EachEdgeIt& e) const { |
425 e=EachEdgeIt(G, flow, capacity); |
429 e=EachEdgeIt(*G, *flow, *capacity); |
426 } |
430 } |
427 void getFirst(EachNodeIt& v) const { G.getFirst(v); } |
431 |
|
432 EachNodeIt& next(EachNodeIt& n) const { return G->next(n); } |
|
433 |
|
434 OutEdgeIt& next(OutEdgeIt& e) const { |
|
435 if (e.out_or_in) { |
|
436 NodeIt v=G->aNode(e.out); |
|
437 ++(e.out); |
|
438 while( G->valid(e.out) && !(e.free()>0) ) { ++(e.out); } |
|
439 if (!G->valid(e.out)) { |
|
440 e.out_or_in=0; |
|
441 G->getFirst(e.in, v); //=/*resG->*/G->template first<OldInEdgeIt>(v); |
|
442 while( G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } |
|
443 } |
|
444 } else { |
|
445 ++(e.in); |
|
446 while( G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } |
|
447 } |
|
448 return e; |
|
449 } |
|
450 |
|
451 EachEdgeIt& next(EachEdgeIt& e) const { |
|
452 if (e.out_or_in) { |
|
453 ++(e.out); |
|
454 while (G->valid(e.out) && !(e.free()>0) ) { ++(e.out); } |
|
455 while (G->valid(e.v) && !G->valid(e.out)) { |
|
456 ++(e.v); |
|
457 if (G->valid(e.v)) G->getFirst(e.out, e.v); |
|
458 while (G->valid(e.out) && !(e.free()>0) ) { ++(e.out); } |
|
459 } |
|
460 if (!G->valid(e.out)) { |
|
461 e.out_or_in=0; |
|
462 G->getFirst(e.v); |
|
463 if (G->valid(e.v)) G->getFirst(e.in, e.v); else e.in=OldInEdgeIt(); |
|
464 while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } |
|
465 while (G->valid(e.v) && !G->valid(e.in)) { |
|
466 ++(e.v); |
|
467 if (G->valid(e.v)) G->getFirst(e.in, e.v); |
|
468 while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } |
|
469 } |
|
470 } |
|
471 } else { |
|
472 ++(e.in); |
|
473 while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } |
|
474 while (G->valid(e.v) && !G->valid(e.in)) { |
|
475 ++(e.v); |
|
476 if (G->valid(e.v)) G->getFirst(e.in, e.v); |
|
477 while (G->valid(e.in) && !(e.free()>0) ) { ++(e.in); } |
|
478 } |
|
479 } |
|
480 return e; |
|
481 } |
428 |
482 |
|
483 |
429 template< typename It > |
484 template< typename It > |
430 It first() const { |
485 It first() const { |
431 It e; |
486 It e; |
432 getFirst(e); |
487 getFirst(e); |
433 return e; |
488 return e; |
439 getFirst(e, v); |
494 getFirst(e, v); |
440 return e; |
495 return e; |
441 } |
496 } |
442 |
497 |
443 NodeIt tail(EdgeIt e) const { |
498 NodeIt tail(EdgeIt e) const { |
444 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); } |
499 return ((e.out_or_in) ? G->aNode(e.out) : G->aNode(e.in)); } |
445 NodeIt head(EdgeIt e) const { |
500 NodeIt head(EdgeIt e) const { |
446 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); } |
501 return ((e.out_or_in) ? G->bNode(e.out) : G->bNode(e.in)); } |
447 |
502 |
448 NodeIt aNode(OutEdgeIt e) const { |
503 NodeIt aNode(OutEdgeIt e) const { |
449 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); } |
504 return ((e.out_or_in) ? G->aNode(e.out) : G->aNode(e.in)); } |
450 NodeIt bNode(OutEdgeIt e) const { |
505 NodeIt bNode(OutEdgeIt e) const { |
451 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); } |
506 return ((e.out_or_in) ? G->bNode(e.out) : G->bNode(e.in)); } |
452 |
507 |
453 int id(NodeIt v) const { return G.id(v); } |
508 int id(NodeIt v) const { return G->id(v); } |
|
509 |
|
510 bool valid(NodeIt n) const { return G->valid(n); } |
|
511 bool valid(EdgeIt e) const { |
|
512 return e.out_or_in ? G->valid(e.out) : G->valid(e.in); } |
454 |
513 |
455 template <typename T> |
514 template <typename T> |
456 class NodeMap { |
515 class NodeMap { |
457 typename Graph::NodeMap<T> node_map; |
516 typename Graph::NodeMap<T> node_map; |
458 public: |
517 public: |
459 NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { } |
518 NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(*(_G.G)) { } |
460 NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G, T a) : node_map(_G.G, a) { } |
519 NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : node_map(*(_G.G), a) { } |
461 void set(NodeIt nit, T a) { node_map.set(nit, a); } |
520 void set(NodeIt nit, T a) { node_map.set(nit, a); } |
462 T get(NodeIt nit) const { return node_map.get(nit); } |
521 T get(NodeIt nit) const { return node_map.get(nit); } |
463 }; |
522 }; |
464 |
523 |
465 template <typename T> |
524 template <typename T> |
466 class EdgeMap { |
525 class EdgeMap { |
467 typename Graph::EdgeMap<T> forward_map, backward_map; |
526 typename Graph::EdgeMap<T> forward_map, backward_map; |
468 public: |
527 public: |
469 EdgeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G) : forward_map(_G.G), backward_map(_G.G) { } |
528 EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : forward_map(*(_G.G)), backward_map(*(_G.G)) { } |
470 EdgeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G, T a) : forward_map(_G.G, a), backward_map(_G.G, a) { } |
529 EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : forward_map(*(_G.G), a), backward_map(*(_G.G), a) { } |
471 void set(EdgeIt e, T a) { |
530 void set(EdgeIt e, T a) { |
472 if (e.out_or_in) |
531 if (e.out_or_in) |
473 forward_map.set(e.out, a); |
532 forward_map.set(e.out, a); |
474 else |
533 else |
475 backward_map.set(e.in, a); |
534 backward_map.set(e.in, a); |
492 typedef typename Graph::EachEdgeIt EachEdgeIt; |
551 typedef typename Graph::EachEdgeIt EachEdgeIt; |
493 typedef typename Graph::OutEdgeIt OutEdgeIt; |
552 typedef typename Graph::OutEdgeIt OutEdgeIt; |
494 typedef typename Graph::InEdgeIt InEdgeIt; |
553 typedef typename Graph::InEdgeIt InEdgeIt; |
495 |
554 |
496 private: |
555 private: |
497 const Graph& G; |
556 const Graph* G; |
498 NodeIt s; |
557 NodeIt s; |
499 NodeIt t; |
558 NodeIt t; |
500 FlowMap& flow; |
559 FlowMap* flow; |
501 const CapacityMap& capacity; |
560 const CapacityMap* capacity; |
502 typedef ResGraph3<Graph, Number, FlowMap, CapacityMap > AugGraph; |
561 typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph; |
503 typedef typename AugGraph::OutEdgeIt AugOutEdgeIt; |
562 typedef typename AugGraph::OutEdgeIt AugOutEdgeIt; |
504 typedef typename AugGraph::EdgeIt AugEdgeIt; |
563 typedef typename AugGraph::EdgeIt AugEdgeIt; |
505 |
564 |
506 //AugGraph res_graph; |
565 //AugGraph res_graph; |
507 //typedef typename AugGraph::NodeMap<bool> ReachedMap; |
566 //typedef typename AugGraph::NodeMap<bool> ReachedMap; |
508 //typename AugGraph::NodeMap<AugEdgeIt> pred; |
567 //typename AugGraph::NodeMap<AugEdgeIt> pred; |
509 //typename AugGraph::NodeMap<Number> free; |
568 //typename AugGraph::NodeMap<Number> free; |
510 public: |
569 public: |
511 MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) : |
570 MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) : |
512 G(_G), s(_s), t(_t), flow(_flow), capacity(_capacity) //, |
571 G(&_G), s(_s), t(_t), flow(&_flow), capacity(&_capacity) //, |
513 //res_graph(G, flow, capacity), pred(res_graph), free(res_graph) |
572 //res_graph(G, flow, capacity), pred(res_graph), free(res_graph) |
514 { } |
573 { } |
515 bool augmentOnShortestPath() { |
574 bool augmentOnShortestPath() { |
516 AugGraph res_graph(G, flow, capacity); |
575 AugGraph res_graph(*G, *flow, *capacity); |
517 bool _augment=false; |
576 bool _augment=false; |
518 |
577 |
519 typedef typename AugGraph::NodeMap<bool> ReachedMap; |
578 typedef typename AugGraph::NodeMap<bool> ReachedMap; |
520 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph); |
579 BfsIterator5< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph); |
521 res_bfs.pushAndSetReached(s); |
580 res_bfs.pushAndSetReached(s); |
522 |
581 |
523 typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph); |
582 typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph); |
524 //filled up with invalid iterators |
583 //filled up with invalid iterators |
525 //pred.set(s, AugEdgeIt()); |
584 //pred.set(s, AugEdgeIt()); |
558 } |
617 } |
559 |
618 |
560 template<typename MutableGraph> bool augmentOnBlockingFlow() { |
619 template<typename MutableGraph> bool augmentOnBlockingFlow() { |
561 bool _augment=false; |
620 bool _augment=false; |
562 |
621 |
563 AugGraph res_graph(G, flow, capacity); |
622 AugGraph res_graph(*G, *flow, *capacity); |
564 |
623 |
565 typedef typename AugGraph::NodeMap<bool> ReachedMap; |
624 typedef typename AugGraph::NodeMap<bool> ReachedMap; |
566 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph); |
625 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph); |
567 |
626 |
568 bfs.pushAndSetReached(s); |
627 bfs.pushAndSetReached(s); |
569 typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's |
628 typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's |
570 while ( !bfs.finished() ) { |
629 while ( !bfs.finished() ) { |
571 AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs); |
630 AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs); |
572 if (e.valid() && bfs.isBNodeNewlyReached()) { |
631 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) { |
573 dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1); |
632 dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1); |
574 } |
633 } |
575 |
634 |
576 ++bfs; |
635 ++bfs; |
577 } //computing distances from s in the residual graph |
636 } //computing distances from s in the residual graph |
578 |
637 |
579 MutableGraph F; |
638 MutableGraph F; |
580 typename AugGraph::NodeMap<NodeIt> res_graph_to_F(res_graph); |
639 typename AugGraph::NodeMap<NodeIt> res_graph_to_F(res_graph); |
581 for(typename AugGraph::EachNodeIt n=res_graph.template first<typename AugGraph::EachNodeIt>(); n.valid(); ++n) { |
640 for(typename AugGraph::EachNodeIt n=res_graph.template first<typename AugGraph::EachNodeIt>(); res_graph.valid(n); res_graph.next(n)) { |
582 res_graph_to_F.set(n, F.addNode()); |
641 res_graph_to_F.set(n, F.addNode()); |
583 } |
642 } |
584 |
643 |
585 typename MutableGraph::NodeIt sF=res_graph_to_F.get(s); |
644 typename MutableGraph::NodeIt sF=res_graph_to_F.get(s); |
586 typename MutableGraph::NodeIt tF=res_graph_to_F.get(t); |
645 typename MutableGraph::NodeIt tF=res_graph_to_F.get(t); |
587 |
646 |
588 typename MutableGraph::EdgeMap<AugEdgeIt> original_edge(F); |
647 typename MutableGraph::EdgeMap<AugEdgeIt> original_edge(F); |
589 typename MutableGraph::EdgeMap<Number> free_on_edge(F); |
648 typename MutableGraph::EdgeMap<Number> residual_capacity(F); |
590 |
649 |
591 //Making F to the graph containing the edges of the residual graph |
650 //Making F to the graph containing the edges of the residual graph |
592 //which are in some shortest paths |
651 //which are in some shortest paths |
593 for(typename AugGraph::EachEdgeIt e=res_graph.template first<typename AugGraph::EachEdgeIt>(); e.valid(); ++e) { |
652 for(typename AugGraph::EachEdgeIt e=res_graph.template first<typename AugGraph::EachEdgeIt>(); res_graph.valid(e); res_graph.next(e)) { |
594 if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) { |
653 if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) { |
595 typename MutableGraph::EdgeIt f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e))); |
654 typename MutableGraph::EdgeIt f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e))); |
596 original_edge.update(); |
655 original_edge.update(); |
597 original_edge.set(f, e); |
656 original_edge.set(f, e); |
598 free_on_edge.update(); |
657 residual_capacity.update(); |
599 free_on_edge.set(f, e.free()); |
658 residual_capacity.set(f, e.free()); |
600 } |
659 } |
601 } |
660 } |
602 |
661 |
603 bool __augment=true; |
662 bool __augment=true; |
604 |
663 |
688 //std::cout<<std::endl; |
747 //std::cout<<std::endl; |
689 } |
748 } |
690 } |
749 } |
691 Number flowValue() { |
750 Number flowValue() { |
692 Number a=0; |
751 Number a=0; |
693 for(OutEdgeIt i=G.template first<OutEdgeIt>(s); i.valid(); ++i) { |
752 OutEdgeIt e; |
694 a+=flow.get(i); |
753 for(G->getFirst(e, s); G->valid(e); G->next(e)) { |
|
754 a+=flow->get(e); |
695 } |
755 } |
696 return a; |
756 return a; |
697 } |
757 } |
698 }; |
758 }; |
699 |
|
700 |
|
701 /* |
|
702 template <typename Graph> |
|
703 class IteratorBoolNodeMap { |
|
704 typedef typename Graph::NodeIt NodeIt; |
|
705 typedef typename Graph::EachNodeIt EachNodeIt; |
|
706 class BoolItem { |
|
707 public: |
|
708 bool value; |
|
709 NodeIt prev; |
|
710 NodeIt next; |
|
711 BoolItem() : value(false), prev(), next() {} |
|
712 }; |
|
713 NodeIt first_true; |
|
714 //NodeIt last_true; |
|
715 NodeIt first_false; |
|
716 //NodeIt last_false; |
|
717 const Graph& G; |
|
718 typename Graph::NodeMap<BoolItem> container; |
|
719 public: |
|
720 typedef bool ValueType; |
|
721 typedef NodeIt KeyType; |
|
722 IteratorBoolNodeMap(const Graph& _G) : G(_G), container(G), first_true(NodeIt()) { |
|
723 //for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) { |
|
724 //BoolItem b=container.get(e); |
|
725 //b.me=e; |
|
726 //container.set(b); |
|
727 //} |
|
728 G.getFirst(first_false); |
|
729 NodeIt e_prev; |
|
730 for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) { |
|
731 container[e].prev=e_prev; |
|
732 if (e_prev.valid()) container[e_prev].next=e; |
|
733 e_prev=e; |
|
734 } |
|
735 } |
|
736 //NodeMap(const Graph& _G, T a) : |
|
737 // G(_G), container(G.node_id, a) { } |
|
738 //FIXME |
|
739 void set(NodeIt nit, T a) { container[G.id(nit)]=a; } |
|
740 T get(NodeIt nit) const { return container[G.id(nit)]; } |
|
741 //void update() { container.resize(G.node_id); } |
|
742 //void update(T a) { container.resize(G.node_id, a); } |
|
743 }; |
|
744 */ |
|
745 |
759 |
746 |
760 |
747 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap> |
761 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap> |
748 class MaxFlow2 { |
762 class MaxFlow2 { |
749 public: |
763 public: |