322 NodeIt v=/*resG->*/G->aNode(out); |
342 NodeIt v=/*resG->*/G->aNode(out); |
323 ++out; |
343 ++out; |
324 while( out.valid() && !(free()>0) ) { ++out; } |
344 while( out.valid() && !(free()>0) ) { ++out; } |
325 if (!out.valid()) { |
345 if (!out.valid()) { |
326 out_or_in=0; |
346 out_or_in=0; |
327 in=/*resG->*/G->template first<OldInEdgeIt>(v); |
347 G->getFirst(in, v); //=/*resG->*/G->template first<OldInEdgeIt>(v); |
328 while( in.valid() && !(free()>0) ) { ++in; } |
348 while( in.valid() && !(free()>0) ) { ++in; } |
329 } |
349 } |
330 } else { |
350 } else { |
331 ++in; |
351 ++in; |
332 while( in.valid() && !(free()>0) ) { ++in; } |
352 while( in.valid() && !(free()>0) ) { ++in; } |
333 } |
353 } |
334 return *this; |
354 return *this; |
335 } |
355 } |
336 }; |
356 }; |
337 |
357 |
|
358 class EachEdgeIt : public EdgeIt { |
|
359 typename Graph::EachNodeIt v; |
|
360 public: |
|
361 EachEdgeIt() { } |
|
362 //EachEdgeIt(const EachEdgeIt& e) : EdgeIt(e), v(e.v) { } |
|
363 EachEdgeIt(const Graph& _G, FlowMap& _flow, const CapacityMap& _capacity) { |
|
364 G=&_G; |
|
365 flow=&_flow; |
|
366 capacity=&_capacity; |
|
367 out_or_in=1; |
|
368 G->getFirst(v); |
|
369 if (v.valid()) G->getFirst(out, v); else out=OldOutEdgeIt(); |
|
370 while (out.valid() && !(free()>0) ) { ++out; } |
|
371 while (v.valid() && !out.valid()) { |
|
372 ++v; |
|
373 if (v.valid()) G->getFirst(out, v); |
|
374 while (out.valid() && !(free()>0) ) { ++out; } |
|
375 } |
|
376 if (!out.valid()) { |
|
377 out_or_in=0; |
|
378 G->getFirst(v); |
|
379 if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt(); |
|
380 while (in.valid() && !(free()>0) ) { ++in; } |
|
381 while (v.valid() && !in.valid()) { |
|
382 ++v; |
|
383 if (v.valid()) G->getFirst(in, v); |
|
384 while (in.valid() && !(free()>0) ) { ++in; } |
|
385 } |
|
386 } |
|
387 } |
|
388 EachEdgeIt& operator++() { |
|
389 if (out_or_in) { |
|
390 ++out; |
|
391 while (out.valid() && !(free()>0) ) { ++out; } |
|
392 while (v.valid() && !out.valid()) { |
|
393 ++v; |
|
394 if (v.valid()) G->getFirst(out, v); |
|
395 while (out.valid() && !(free()>0) ) { ++out; } |
|
396 } |
|
397 if (!out.valid()) { |
|
398 out_or_in=0; |
|
399 G->getFirst(v); |
|
400 if (v.valid()) G->getFirst(in, v); else in=OldInEdgeIt(); |
|
401 while (in.valid() && !(free()>0) ) { ++in; } |
|
402 while (v.valid() && !in.valid()) { |
|
403 ++v; |
|
404 if (v.valid()) G->getFirst(in, v); |
|
405 while (in.valid() && !(free()>0) ) { ++in; } |
|
406 } |
|
407 } |
|
408 } else { |
|
409 ++in; |
|
410 while (in.valid() && !(free()>0) ) { ++in; } |
|
411 while (v.valid() && !in.valid()) { |
|
412 ++v; |
|
413 if (v.valid()) G->getFirst(in, v); |
|
414 while (in.valid() && !(free()>0) ) { ++in; } |
|
415 } |
|
416 } |
|
417 return *this; |
|
418 } |
|
419 }; |
|
420 |
338 void getFirst(OutEdgeIt& e, NodeIt v) const { |
421 void getFirst(OutEdgeIt& e, NodeIt v) const { |
339 e=OutEdgeIt(G, v, flow, capacity); |
422 e=OutEdgeIt(G, v, flow, capacity); |
|
423 } |
|
424 void getFirst(EachEdgeIt& e) const { |
|
425 e=EachEdgeIt(G, flow, capacity); |
340 } |
426 } |
341 void getFirst(EachNodeIt& v) const { G.getFirst(v); } |
427 void getFirst(EachNodeIt& v) const { G.getFirst(v); } |
342 |
428 |
343 template< typename It > |
429 template< typename It > |
344 It first() const { |
430 It first() const { |
399 typedef typename AugGraph::EdgeIt AugEdgeIt; |
485 typedef typename AugGraph::EdgeIt AugEdgeIt; |
400 |
486 |
401 //AugGraph res_graph; |
487 //AugGraph res_graph; |
402 //typedef typename AugGraph::NodeMap<bool> ReachedMap; |
488 //typedef typename AugGraph::NodeMap<bool> ReachedMap; |
403 //typename AugGraph::NodeMap<AugEdgeIt> pred; |
489 //typename AugGraph::NodeMap<AugEdgeIt> pred; |
404 //typename AugGraph::NodeMap<int> free; |
490 //typename AugGraph::NodeMap<Number> free; |
405 public: |
491 public: |
406 MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) : |
492 MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) : |
407 G(_G), s(_s), t(_t), flow(_flow), capacity(_capacity) //, |
493 G(_G), s(_s), t(_t), flow(_flow), capacity(_capacity) //, |
408 //res_graph(G, flow, capacity), pred(res_graph), free(res_graph) |
494 //res_graph(G, flow, capacity), pred(res_graph), free(res_graph) |
409 { } |
495 { } |
410 bool augment() { |
496 bool augmentOnShortestPath() { |
411 AugGraph res_graph(G, flow, capacity); |
497 AugGraph res_graph(G, flow, capacity); |
412 bool _augment=false; |
498 bool _augment=false; |
413 |
499 |
414 typedef typename AugGraph::NodeMap<bool> ReachedMap; |
500 typedef typename AugGraph::NodeMap<bool> ReachedMap; |
415 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph); |
501 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph); |
449 } |
535 } |
450 } |
536 } |
451 |
537 |
452 return _augment; |
538 return _augment; |
453 } |
539 } |
454 bool augmentWithBlockingFlow() { |
540 template<typename MutableGraph> bool augmentOnBlockingFlow() { |
455 BfsIterator4< Graph, OutEdgeIt, typename Graph::NodeMap<bool> > bfs(G); |
541 bool _augment=false; |
|
542 |
|
543 AugGraph res_graph(G, flow, capacity); |
|
544 |
|
545 typedef typename AugGraph::NodeMap<bool> ReachedMap; |
|
546 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph); |
|
547 |
456 bfs.pushAndSetReached(s); |
548 bfs.pushAndSetReached(s); |
457 typename Graph::NodeMap<int> dist(G); //filled up with 0's |
549 typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's |
458 while ( !bfs.finished() ) { |
550 while ( !bfs.finished() ) { |
459 OutEdgeIt e=OutEdgeIt(bfs); |
551 AugOutEdgeIt e=AugOutEdgeIt(bfs); |
460 if (e.valid() && bfs.isBNodeNewlyReached()) { |
552 if (e.valid() && bfs.isBNodeNewlyReached()) { |
461 dist.set(G.head(e), dist.get(G.tail(e))+1); |
553 dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1); |
462 //NodeIt v=res_graph.tail(e); |
|
463 //NodeIt w=res_graph.head(e); |
|
464 //pred.set(w, e); |
|
465 //if (pred.get(v).valid()) { |
|
466 // free.set(w, std::min(free.get(v), e.free())); |
|
467 //} else { |
|
468 // free.set(w, e.free()); |
|
469 //} |
|
470 //if (res_graph.head(e)==t) { _augment=true; break; } |
|
471 } |
554 } |
472 |
555 |
473 ++bfs; |
556 ++bfs; |
474 } //end of searching augmenting path |
557 } //computing distances from s in the residual graph |
475 |
558 |
476 double pre_time_copy=currTime(); |
|
477 typedef Graph MutableGraph; |
|
478 MutableGraph F; |
559 MutableGraph F; |
479 typename Graph::NodeMap<NodeIt> G_to_F(G); |
560 typename AugGraph::NodeMap<NodeIt> res_graph_to_F(res_graph); |
480 for(typename Graph::EachNodeIt n=G.template first<typename Graph::EachNodeIt>(); n.valid(); ++n) { |
561 for(typename AugGraph::EachNodeIt n=res_graph.template first<typename AugGraph::EachNodeIt>(); n.valid(); ++n) { |
481 G_to_F.set(n, F.addNode()); |
562 res_graph_to_F.set(n, F.addNode()); |
482 } |
563 } |
483 for(typename Graph::EachEdgeIt e=G.template first<typename Graph::EachEdgeIt>(); e.valid(); ++e) { |
564 |
484 if (dist.get(G.head(e))==dist.get(G.tail(e))+1) { |
565 typename MutableGraph::NodeIt sF=res_graph_to_F.get(s); |
485 F.addEdge(G_to_F.get(G.tail(e)), G_to_F.get(G.head(e))); |
566 typename MutableGraph::NodeIt tF=res_graph_to_F.get(t); |
486 } |
567 |
487 } |
568 typename MutableGraph::EdgeMap<AugEdgeIt> original_edge(F); |
488 double post_time_copy=currTime(); |
569 typename MutableGraph::EdgeMap<Number> free_on_edge(F); |
489 std::cout << "copy time: " << post_time_copy-pre_time_copy << " sec"<< std::endl; |
570 |
490 |
571 //Making F to the graph containing the edges of the residual graph |
491 return 0; |
572 //which are in some shortest paths |
|
573 for(typename AugGraph::EachEdgeIt e=res_graph.template first<typename AugGraph::EachEdgeIt>(); e.valid(); ++e) { |
|
574 if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) { |
|
575 typename MutableGraph::EdgeIt f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e))); |
|
576 original_edge.update(); |
|
577 original_edge.set(f, e); |
|
578 free_on_edge.update(); |
|
579 free_on_edge.set(f, e.free()); |
|
580 } |
|
581 } |
|
582 |
|
583 bool __augment=true; |
|
584 |
|
585 while (__augment) { |
|
586 __augment=false; |
|
587 //computing blocking flow with dfs |
|
588 typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap; |
|
589 DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F); |
|
590 typename MutableGraph::NodeMap<EdgeIt> pred(F); //invalid iterators |
|
591 typename MutableGraph::NodeMap<Number> free(F); |
|
592 |
|
593 dfs.pushAndSetReached(sF); |
|
594 while (!dfs.finished()) { |
|
595 ++dfs; |
|
596 if (typename MutableGraph::OutEdgeIt(dfs).valid()) { |
|
597 //std::cout << "OutEdgeIt: " << dfs; |
|
598 //std::cout << " aNode: " << F.aNode(dfs); |
|
599 //std::cout << " bNode: " << F.bNode(dfs) << " "; |
|
600 |
|
601 typename MutableGraph::NodeIt v=F.aNode(dfs); |
|
602 typename MutableGraph::NodeIt w=F.bNode(dfs); |
|
603 pred.set(w, dfs); |
|
604 if (pred.get(v).valid()) { |
|
605 free.set(w, std::min(free.get(v), free_on_edge.get(dfs))); |
|
606 } else { |
|
607 free.set(w, free_on_edge.get(dfs)/*original_edge.get(dfs).free()*/); |
|
608 } |
|
609 if (w==tF) { |
|
610 //std::cout << "AUGMENTATION"<<std::endl; |
|
611 __augment=true; |
|
612 _augment=true; |
|
613 break; |
|
614 } |
|
615 } else { |
|
616 //std::cout << "OutEdgeIt: " << "invalid"; |
|
617 //std::cout << " aNode: " << dfs.aNode(); |
|
618 //std::cout << " bNode: " << "invalid" << " "; |
|
619 } |
|
620 if (dfs.isBNodeNewlyReached()) { |
|
621 //std::cout << "bNodeIsNewlyReached "; |
|
622 } else { |
|
623 //std::cout << "bNodeIsNotNewlyReached "; |
|
624 if (typename MutableGraph::OutEdgeIt(dfs).valid()) { |
|
625 //std::cout << "DELETE "; |
|
626 F.erase(typename MutableGraph::OutEdgeIt(dfs)); |
|
627 } |
|
628 } |
|
629 //if (dfs.isANodeExamined()) { |
|
630 //std::cout << "aNodeIsExamined "; |
|
631 //} else { |
|
632 //std::cout << "aNodeIsNotExamined "; |
|
633 //} |
|
634 //std::cout<<std::endl; |
|
635 } |
|
636 |
|
637 if (__augment) { |
|
638 typename MutableGraph::NodeIt n=tF; |
|
639 Number augment_value=free.get(tF); |
|
640 while (pred.get(n).valid()) { |
|
641 typename MutableGraph::EdgeIt e=pred.get(n); |
|
642 original_edge.get(e).augment(augment_value); |
|
643 n=F.tail(e); |
|
644 F.erase(e); |
|
645 } |
|
646 } |
|
647 |
|
648 } |
|
649 |
|
650 return _augment; |
492 } |
651 } |
493 void run() { |
652 void run() { |
494 //int num_of_augmentations=0; |
653 //int num_of_augmentations=0; |
495 while (augment()) { |
654 while (augmentOnShortestPath()) { |
|
655 //while (augmentOnBlockingFlow<MutableGraph>()) { |
|
656 //std::cout << ++num_of_augmentations << " "; |
|
657 //std::cout<<std::endl; |
|
658 } |
|
659 } |
|
660 template<typename MutableGraph> void run() { |
|
661 //int num_of_augmentations=0; |
|
662 //while (augmentOnShortestPath()) { |
|
663 while (augmentOnBlockingFlow<MutableGraph>()) { |
496 //std::cout << ++num_of_augmentations << " "; |
664 //std::cout << ++num_of_augmentations << " "; |
497 //std::cout<<std::endl; |
665 //std::cout<<std::endl; |
498 } |
666 } |
499 } |
667 } |
500 Number flowValue() { |
668 Number flowValue() { |