0
6
0
39
17
22
22
24
17
36
11
36
11
... | ... |
@@ -354,610 +354,610 @@ |
354 | 354 |
///\return <tt> (*this) </tt> |
355 | 355 |
Bfs &predMap(PredMap &m) |
356 | 356 |
{ |
357 | 357 |
if(local_pred) { |
358 | 358 |
delete _pred; |
359 | 359 |
local_pred=false; |
360 | 360 |
} |
361 | 361 |
_pred = &m; |
362 | 362 |
return *this; |
363 | 363 |
} |
364 | 364 |
|
365 | 365 |
///Sets the map that indicates which nodes are reached. |
366 | 366 |
|
367 | 367 |
///Sets the map that indicates which nodes are reached. |
368 | 368 |
///If you don't use this function before calling \ref run(), |
369 | 369 |
///it will allocate one. The destructor deallocates this |
370 | 370 |
///automatically allocated map, of course. |
371 | 371 |
///\return <tt> (*this) </tt> |
372 | 372 |
Bfs &reachedMap(ReachedMap &m) |
373 | 373 |
{ |
374 | 374 |
if(local_reached) { |
375 | 375 |
delete _reached; |
376 | 376 |
local_reached=false; |
377 | 377 |
} |
378 | 378 |
_reached = &m; |
379 | 379 |
return *this; |
380 | 380 |
} |
381 | 381 |
|
382 | 382 |
///Sets the map that indicates which nodes are processed. |
383 | 383 |
|
384 | 384 |
///Sets the map that indicates which nodes are processed. |
385 | 385 |
///If you don't use this function before calling \ref run(), |
386 | 386 |
///it will allocate one. The destructor deallocates this |
387 | 387 |
///automatically allocated map, of course. |
388 | 388 |
///\return <tt> (*this) </tt> |
389 | 389 |
Bfs &processedMap(ProcessedMap &m) |
390 | 390 |
{ |
391 | 391 |
if(local_processed) { |
392 | 392 |
delete _processed; |
393 | 393 |
local_processed=false; |
394 | 394 |
} |
395 | 395 |
_processed = &m; |
396 | 396 |
return *this; |
397 | 397 |
} |
398 | 398 |
|
399 | 399 |
///Sets the map that stores the distances of the nodes. |
400 | 400 |
|
401 | 401 |
///Sets the map that stores the distances of the nodes calculated by |
402 | 402 |
///the algorithm. |
403 | 403 |
///If you don't use this function before calling \ref run(), |
404 | 404 |
///it will allocate one. The destructor deallocates this |
405 | 405 |
///automatically allocated map, of course. |
406 | 406 |
///\return <tt> (*this) </tt> |
407 | 407 |
Bfs &distMap(DistMap &m) |
408 | 408 |
{ |
409 | 409 |
if(local_dist) { |
410 | 410 |
delete _dist; |
411 | 411 |
local_dist=false; |
412 | 412 |
} |
413 | 413 |
_dist = &m; |
414 | 414 |
return *this; |
415 | 415 |
} |
416 | 416 |
|
417 | 417 |
public: |
418 | 418 |
|
419 | 419 |
///\name Execution control |
420 | 420 |
///The simplest way to execute the algorithm is to use |
421 | 421 |
///one of the member functions called \ref lemon::Bfs::run() "run()". |
422 | 422 |
///\n |
423 | 423 |
///If you need more control on the execution, first you must call |
424 | 424 |
///\ref lemon::Bfs::init() "init()", then you can add several source |
425 | 425 |
///nodes with \ref lemon::Bfs::addSource() "addSource()". |
426 | 426 |
///Finally \ref lemon::Bfs::start() "start()" will perform the |
427 | 427 |
///actual path computation. |
428 | 428 |
|
429 | 429 |
///@{ |
430 | 430 |
|
431 | 431 |
///Initializes the internal data structures. |
432 | 432 |
|
433 | 433 |
///Initializes the internal data structures. |
434 | 434 |
/// |
435 | 435 |
void init() |
436 | 436 |
{ |
437 | 437 |
create_maps(); |
438 | 438 |
_queue.resize(countNodes(*G)); |
439 | 439 |
_queue_head=_queue_tail=0; |
440 | 440 |
_curr_dist=1; |
441 | 441 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
442 | 442 |
_pred->set(u,INVALID); |
443 | 443 |
_reached->set(u,false); |
444 | 444 |
_processed->set(u,false); |
445 | 445 |
} |
446 | 446 |
} |
447 | 447 |
|
448 | 448 |
///Adds a new source node. |
449 | 449 |
|
450 | 450 |
///Adds a new source node to the set of nodes to be processed. |
451 | 451 |
/// |
452 | 452 |
void addSource(Node s) |
453 | 453 |
{ |
454 | 454 |
if(!(*_reached)[s]) |
455 | 455 |
{ |
456 | 456 |
_reached->set(s,true); |
457 | 457 |
_pred->set(s,INVALID); |
458 | 458 |
_dist->set(s,0); |
459 | 459 |
_queue[_queue_head++]=s; |
460 | 460 |
_queue_next_dist=_queue_head; |
461 | 461 |
} |
462 | 462 |
} |
463 | 463 |
|
464 | 464 |
///Processes the next node. |
465 | 465 |
|
466 | 466 |
///Processes the next node. |
467 | 467 |
/// |
468 | 468 |
///\return The processed node. |
469 | 469 |
/// |
470 | 470 |
///\pre The queue must not be empty. |
471 | 471 |
Node processNextNode() |
472 | 472 |
{ |
473 | 473 |
if(_queue_tail==_queue_next_dist) { |
474 | 474 |
_curr_dist++; |
475 | 475 |
_queue_next_dist=_queue_head; |
476 | 476 |
} |
477 | 477 |
Node n=_queue[_queue_tail++]; |
478 | 478 |
_processed->set(n,true); |
479 | 479 |
Node m; |
480 | 480 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
481 | 481 |
if(!(*_reached)[m=G->target(e)]) { |
482 | 482 |
_queue[_queue_head++]=m; |
483 | 483 |
_reached->set(m,true); |
484 | 484 |
_pred->set(m,e); |
485 | 485 |
_dist->set(m,_curr_dist); |
486 | 486 |
} |
487 | 487 |
return n; |
488 | 488 |
} |
489 | 489 |
|
490 | 490 |
///Processes the next node. |
491 | 491 |
|
492 | 492 |
///Processes the next node and checks if the given target node |
493 | 493 |
///is reached. If the target node is reachable from the processed |
494 | 494 |
///node, then the \c reach parameter will be set to \c true. |
495 | 495 |
/// |
496 | 496 |
///\param target The target node. |
497 | 497 |
///\retval reach Indicates if the target node is reached. |
498 | 498 |
///It should be initially \c false. |
499 | 499 |
/// |
500 | 500 |
///\return The processed node. |
501 | 501 |
/// |
502 | 502 |
///\pre The queue must not be empty. |
503 | 503 |
Node processNextNode(Node target, bool& reach) |
504 | 504 |
{ |
505 | 505 |
if(_queue_tail==_queue_next_dist) { |
506 | 506 |
_curr_dist++; |
507 | 507 |
_queue_next_dist=_queue_head; |
508 | 508 |
} |
509 | 509 |
Node n=_queue[_queue_tail++]; |
510 | 510 |
_processed->set(n,true); |
511 | 511 |
Node m; |
512 | 512 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
513 | 513 |
if(!(*_reached)[m=G->target(e)]) { |
514 | 514 |
_queue[_queue_head++]=m; |
515 | 515 |
_reached->set(m,true); |
516 | 516 |
_pred->set(m,e); |
517 | 517 |
_dist->set(m,_curr_dist); |
518 | 518 |
reach = reach || (target == m); |
519 | 519 |
} |
520 | 520 |
return n; |
521 | 521 |
} |
522 | 522 |
|
523 | 523 |
///Processes the next node. |
524 | 524 |
|
525 | 525 |
///Processes the next node and checks if at least one of reached |
526 | 526 |
///nodes has \c true value in the \c nm node map. If one node |
527 | 527 |
///with \c true value is reachable from the processed node, then the |
528 | 528 |
///\c rnode parameter will be set to the first of such nodes. |
529 | 529 |
/// |
530 | 530 |
///\param nm A \c bool (or convertible) node map that indicates the |
531 | 531 |
///possible targets. |
532 | 532 |
///\retval rnode The reached target node. |
533 | 533 |
///It should be initially \c INVALID. |
534 | 534 |
/// |
535 | 535 |
///\return The processed node. |
536 | 536 |
/// |
537 | 537 |
///\pre The queue must not be empty. |
538 | 538 |
template<class NM> |
539 | 539 |
Node processNextNode(const NM& nm, Node& rnode) |
540 | 540 |
{ |
541 | 541 |
if(_queue_tail==_queue_next_dist) { |
542 | 542 |
_curr_dist++; |
543 | 543 |
_queue_next_dist=_queue_head; |
544 | 544 |
} |
545 | 545 |
Node n=_queue[_queue_tail++]; |
546 | 546 |
_processed->set(n,true); |
547 | 547 |
Node m; |
548 | 548 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
549 | 549 |
if(!(*_reached)[m=G->target(e)]) { |
550 | 550 |
_queue[_queue_head++]=m; |
551 | 551 |
_reached->set(m,true); |
552 | 552 |
_pred->set(m,e); |
553 | 553 |
_dist->set(m,_curr_dist); |
554 | 554 |
if (nm[m] && rnode == INVALID) rnode = m; |
555 | 555 |
} |
556 | 556 |
return n; |
557 | 557 |
} |
558 | 558 |
|
559 | 559 |
///The next node to be processed. |
560 | 560 |
|
561 | 561 |
///Returns the next node to be processed or \c INVALID if the queue |
562 | 562 |
///is empty. |
563 | 563 |
Node nextNode() const |
564 | 564 |
{ |
565 | 565 |
return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; |
566 | 566 |
} |
567 | 567 |
|
568 | 568 |
///\brief Returns \c false if there are nodes |
569 | 569 |
///to be processed. |
570 | 570 |
/// |
571 | 571 |
///Returns \c false if there are nodes |
572 | 572 |
///to be processed in the queue. |
573 | 573 |
bool emptyQueue() const { return _queue_tail==_queue_head; } |
574 | 574 |
|
575 | 575 |
///Returns the number of the nodes to be processed. |
576 | 576 |
|
577 | 577 |
///Returns the number of the nodes to be processed in the queue. |
578 | 578 |
int queueSize() const { return _queue_head-_queue_tail; } |
579 | 579 |
|
580 | 580 |
///Executes the algorithm. |
581 | 581 |
|
582 | 582 |
///Executes the algorithm. |
583 | 583 |
/// |
584 | 584 |
///This method runs the %BFS algorithm from the root node(s) |
585 | 585 |
///in order to compute the shortest path to each node. |
586 | 586 |
/// |
587 | 587 |
///The algorithm computes |
588 | 588 |
///- the shortest path tree (forest), |
589 | 589 |
///- the distance of each node from the root(s). |
590 | 590 |
/// |
591 | 591 |
///\pre init() must be called and at least one root node should be |
592 | 592 |
///added with addSource() before using this function. |
593 | 593 |
/// |
594 | 594 |
///\note <tt>b.start()</tt> is just a shortcut of the following code. |
595 | 595 |
///\code |
596 | 596 |
/// while ( !b.emptyQueue() ) { |
597 | 597 |
/// b.processNextNode(); |
598 | 598 |
/// } |
599 | 599 |
///\endcode |
600 | 600 |
void start() |
601 | 601 |
{ |
602 | 602 |
while ( !emptyQueue() ) processNextNode(); |
603 | 603 |
} |
604 | 604 |
|
605 | 605 |
///Executes the algorithm until the given target node is reached. |
606 | 606 |
|
607 | 607 |
///Executes the algorithm until the given target node is reached. |
608 | 608 |
/// |
609 | 609 |
///This method runs the %BFS algorithm from the root node(s) |
610 |
///in order to compute the shortest path to \c |
|
610 |
///in order to compute the shortest path to \c t. |
|
611 | 611 |
/// |
612 | 612 |
///The algorithm computes |
613 |
///- the shortest path to \c dest, |
|
614 |
///- the distance of \c dest from the root(s). |
|
613 |
///- the shortest path to \c t, |
|
614 |
///- the distance of \c t from the root(s). |
|
615 | 615 |
/// |
616 | 616 |
///\pre init() must be called and at least one root node should be |
617 | 617 |
///added with addSource() before using this function. |
618 | 618 |
/// |
619 | 619 |
///\note <tt>b.start(t)</tt> is just a shortcut of the following code. |
620 | 620 |
///\code |
621 | 621 |
/// bool reach = false; |
622 | 622 |
/// while ( !b.emptyQueue() && !reach ) { |
623 | 623 |
/// b.processNextNode(t, reach); |
624 | 624 |
/// } |
625 | 625 |
///\endcode |
626 |
void start(Node |
|
626 |
void start(Node t) |
|
627 | 627 |
{ |
628 | 628 |
bool reach = false; |
629 |
while ( !emptyQueue() && !reach ) processNextNode( |
|
629 |
while ( !emptyQueue() && !reach ) processNextNode(t, reach); |
|
630 | 630 |
} |
631 | 631 |
|
632 | 632 |
///Executes the algorithm until a condition is met. |
633 | 633 |
|
634 | 634 |
///Executes the algorithm until a condition is met. |
635 | 635 |
/// |
636 | 636 |
///This method runs the %BFS algorithm from the root node(s) in |
637 | 637 |
///order to compute the shortest path to a node \c v with |
638 | 638 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
639 | 639 |
/// |
640 | 640 |
///\param nm A \c bool (or convertible) node map. The algorithm |
641 | 641 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
642 | 642 |
/// |
643 | 643 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
644 | 644 |
///\c INVALID if no such node was found. |
645 | 645 |
/// |
646 | 646 |
///\pre init() must be called and at least one root node should be |
647 | 647 |
///added with addSource() before using this function. |
648 | 648 |
/// |
649 | 649 |
///\note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
650 | 650 |
///\code |
651 | 651 |
/// Node rnode = INVALID; |
652 | 652 |
/// while ( !b.emptyQueue() && rnode == INVALID ) { |
653 | 653 |
/// b.processNextNode(nm, rnode); |
654 | 654 |
/// } |
655 | 655 |
/// return rnode; |
656 | 656 |
///\endcode |
657 | 657 |
template<class NodeBoolMap> |
658 | 658 |
Node start(const NodeBoolMap &nm) |
659 | 659 |
{ |
660 | 660 |
Node rnode = INVALID; |
661 | 661 |
while ( !emptyQueue() && rnode == INVALID ) { |
662 | 662 |
processNextNode(nm, rnode); |
663 | 663 |
} |
664 | 664 |
return rnode; |
665 | 665 |
} |
666 | 666 |
|
667 |
///Runs the algorithm from the given node. |
|
667 |
///Runs the algorithm from the given source node. |
|
668 | 668 |
|
669 | 669 |
///This method runs the %BFS algorithm from node \c s |
670 | 670 |
///in order to compute the shortest path to each node. |
671 | 671 |
/// |
672 | 672 |
///The algorithm computes |
673 | 673 |
///- the shortest path tree, |
674 | 674 |
///- the distance of each node from the root. |
675 | 675 |
/// |
676 | 676 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
677 | 677 |
///\code |
678 | 678 |
/// b.init(); |
679 | 679 |
/// b.addSource(s); |
680 | 680 |
/// b.start(); |
681 | 681 |
///\endcode |
682 | 682 |
void run(Node s) { |
683 | 683 |
init(); |
684 | 684 |
addSource(s); |
685 | 685 |
start(); |
686 | 686 |
} |
687 | 687 |
|
688 | 688 |
///Finds the shortest path between \c s and \c t. |
689 | 689 |
|
690 | 690 |
///This method runs the %BFS algorithm from node \c s |
691 |
///in order to compute the shortest path to \c t |
|
691 |
///in order to compute the shortest path to node \c t |
|
692 |
///(it stops searching when \c t is processed). |
|
692 | 693 |
/// |
693 |
///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path, |
|
694 |
///if \c t is reachable form \c s, \c 0 otherwise. |
|
694 |
///\return \c true if \c t is reachable form \c s. |
|
695 | 695 |
/// |
696 | 696 |
///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
697 | 697 |
///shortcut of the following code. |
698 | 698 |
///\code |
699 | 699 |
/// b.init(); |
700 | 700 |
/// b.addSource(s); |
701 | 701 |
/// b.start(t); |
702 | 702 |
///\endcode |
703 |
|
|
703 |
bool run(Node s,Node t) { |
|
704 | 704 |
init(); |
705 | 705 |
addSource(s); |
706 | 706 |
start(t); |
707 |
return reached(t) |
|
707 |
return reached(t); |
|
708 | 708 |
} |
709 | 709 |
|
710 | 710 |
///Runs the algorithm to visit all nodes in the digraph. |
711 | 711 |
|
712 | 712 |
///This method runs the %BFS algorithm in order to |
713 | 713 |
///compute the shortest path to each node. |
714 | 714 |
/// |
715 | 715 |
///The algorithm computes |
716 | 716 |
///- the shortest path tree (forest), |
717 | 717 |
///- the distance of each node from the root(s). |
718 | 718 |
/// |
719 | 719 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
720 | 720 |
///\code |
721 | 721 |
/// b.init(); |
722 | 722 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
723 | 723 |
/// if (!b.reached(n)) { |
724 | 724 |
/// b.addSource(n); |
725 | 725 |
/// b.start(); |
726 | 726 |
/// } |
727 | 727 |
/// } |
728 | 728 |
///\endcode |
729 | 729 |
void run() { |
730 | 730 |
init(); |
731 | 731 |
for (NodeIt n(*G); n != INVALID; ++n) { |
732 | 732 |
if (!reached(n)) { |
733 | 733 |
addSource(n); |
734 | 734 |
start(); |
735 | 735 |
} |
736 | 736 |
} |
737 | 737 |
} |
738 | 738 |
|
739 | 739 |
///@} |
740 | 740 |
|
741 | 741 |
///\name Query Functions |
742 | 742 |
///The result of the %BFS algorithm can be obtained using these |
743 | 743 |
///functions.\n |
744 | 744 |
///Either \ref lemon::Bfs::run() "run()" or \ref lemon::Bfs::start() |
745 | 745 |
///"start()" must be called before using them. |
746 | 746 |
|
747 | 747 |
///@{ |
748 | 748 |
|
749 | 749 |
///The shortest path to a node. |
750 | 750 |
|
751 | 751 |
///Returns the shortest path to a node. |
752 | 752 |
/// |
753 | 753 |
///\warning \c t should be reachable from the root(s). |
754 | 754 |
/// |
755 | 755 |
///\pre Either \ref run() or \ref start() must be called before |
756 | 756 |
///using this function. |
757 | 757 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
758 | 758 |
|
759 | 759 |
///The distance of a node from the root(s). |
760 | 760 |
|
761 | 761 |
///Returns the distance of a node from the root(s). |
762 | 762 |
/// |
763 | 763 |
///\warning If node \c v is not reachable from the root(s), then |
764 | 764 |
///the return value of this function is undefined. |
765 | 765 |
/// |
766 | 766 |
///\pre Either \ref run() or \ref start() must be called before |
767 | 767 |
///using this function. |
768 | 768 |
int dist(Node v) const { return (*_dist)[v]; } |
769 | 769 |
|
770 | 770 |
///Returns the 'previous arc' of the shortest path tree for a node. |
771 | 771 |
|
772 | 772 |
///This function returns the 'previous arc' of the shortest path |
773 | 773 |
///tree for the node \c v, i.e. it returns the last arc of a |
774 | 774 |
///shortest path from the root(s) to \c v. It is \c INVALID if \c v |
775 | 775 |
///is not reachable from the root(s) or if \c v is a root. |
776 | 776 |
/// |
777 | 777 |
///The shortest path tree used here is equal to the shortest path |
778 | 778 |
///tree used in \ref predNode(). |
779 | 779 |
/// |
780 | 780 |
///\pre Either \ref run() or \ref start() must be called before |
781 | 781 |
///using this function. |
782 | 782 |
Arc predArc(Node v) const { return (*_pred)[v];} |
783 | 783 |
|
784 | 784 |
///Returns the 'previous node' of the shortest path tree for a node. |
785 | 785 |
|
786 | 786 |
///This function returns the 'previous node' of the shortest path |
787 | 787 |
///tree for the node \c v, i.e. it returns the last but one node |
788 | 788 |
///from a shortest path from the root(s) to \c v. It is \c INVALID |
789 | 789 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
790 | 790 |
/// |
791 | 791 |
///The shortest path tree used here is equal to the shortest path |
792 | 792 |
///tree used in \ref predArc(). |
793 | 793 |
/// |
794 | 794 |
///\pre Either \ref run() or \ref start() must be called before |
795 | 795 |
///using this function. |
796 | 796 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
797 | 797 |
G->source((*_pred)[v]); } |
798 | 798 |
|
799 | 799 |
///\brief Returns a const reference to the node map that stores the |
800 | 800 |
/// distances of the nodes. |
801 | 801 |
/// |
802 | 802 |
///Returns a const reference to the node map that stores the distances |
803 | 803 |
///of the nodes calculated by the algorithm. |
804 | 804 |
/// |
805 | 805 |
///\pre Either \ref run() or \ref init() |
806 | 806 |
///must be called before using this function. |
807 | 807 |
const DistMap &distMap() const { return *_dist;} |
808 | 808 |
|
809 | 809 |
///\brief Returns a const reference to the node map that stores the |
810 | 810 |
///predecessor arcs. |
811 | 811 |
/// |
812 | 812 |
///Returns a const reference to the node map that stores the predecessor |
813 | 813 |
///arcs, which form the shortest path tree. |
814 | 814 |
/// |
815 | 815 |
///\pre Either \ref run() or \ref init() |
816 | 816 |
///must be called before using this function. |
817 | 817 |
const PredMap &predMap() const { return *_pred;} |
818 | 818 |
|
819 | 819 |
///Checks if a node is reachable from the root(s). |
820 | 820 |
|
821 | 821 |
///Returns \c true if \c v is reachable from the root(s). |
822 | 822 |
///\pre Either \ref run() or \ref start() |
823 | 823 |
///must be called before using this function. |
824 | 824 |
bool reached(Node v) const { return (*_reached)[v]; } |
825 | 825 |
|
826 | 826 |
///@} |
827 | 827 |
}; |
828 | 828 |
|
829 | 829 |
///Default traits class of bfs() function. |
830 | 830 |
|
831 | 831 |
///Default traits class of bfs() function. |
832 | 832 |
///\tparam GR Digraph type. |
833 | 833 |
template<class GR> |
834 | 834 |
struct BfsWizardDefaultTraits |
835 | 835 |
{ |
836 | 836 |
///The type of the digraph the algorithm runs on. |
837 | 837 |
typedef GR Digraph; |
838 | 838 |
|
839 | 839 |
///\brief The type of the map that stores the predecessor |
840 | 840 |
///arcs of the shortest paths. |
841 | 841 |
/// |
842 | 842 |
///The type of the map that stores the predecessor |
843 | 843 |
///arcs of the shortest paths. |
844 | 844 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
845 | 845 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
846 | 846 |
///Instantiates a \ref PredMap. |
847 | 847 |
|
848 | 848 |
///This function instantiates a \ref PredMap. |
849 | 849 |
///\param g is the digraph, to which we would like to define the |
850 | 850 |
///\ref PredMap. |
851 | 851 |
///\todo The digraph alone may be insufficient to initialize |
852 | 852 |
static PredMap *createPredMap(const Digraph &g) |
853 | 853 |
{ |
854 | 854 |
return new PredMap(g); |
855 | 855 |
} |
856 | 856 |
|
857 | 857 |
///The type of the map that indicates which nodes are processed. |
858 | 858 |
|
859 | 859 |
///The type of the map that indicates which nodes are processed. |
860 | 860 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
861 | 861 |
///By default it is a NullMap. |
862 | 862 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
863 | 863 |
///Instantiates a \ref ProcessedMap. |
864 | 864 |
|
865 | 865 |
///This function instantiates a \ref ProcessedMap. |
866 | 866 |
///\param g is the digraph, to which |
867 | 867 |
///we would like to define the \ref ProcessedMap. |
868 | 868 |
#ifdef DOXYGEN |
869 | 869 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
870 | 870 |
#else |
871 | 871 |
static ProcessedMap *createProcessedMap(const Digraph &) |
872 | 872 |
#endif |
873 | 873 |
{ |
874 | 874 |
return new ProcessedMap(); |
875 | 875 |
} |
876 | 876 |
|
877 | 877 |
///The type of the map that indicates which nodes are reached. |
878 | 878 |
|
879 | 879 |
///The type of the map that indicates which nodes are reached. |
880 | 880 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
881 | 881 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
882 | 882 |
///Instantiates a \ref ReachedMap. |
883 | 883 |
|
884 | 884 |
///This function instantiates a \ref ReachedMap. |
885 | 885 |
///\param g is the digraph, to which |
886 | 886 |
///we would like to define the \ref ReachedMap. |
887 | 887 |
static ReachedMap *createReachedMap(const Digraph &g) |
888 | 888 |
{ |
889 | 889 |
return new ReachedMap(g); |
890 | 890 |
} |
891 | 891 |
|
892 | 892 |
///The type of the map that stores the distances of the nodes. |
893 | 893 |
|
894 | 894 |
///The type of the map that stores the distances of the nodes. |
895 | 895 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
896 | 896 |
typedef typename Digraph::template NodeMap<int> DistMap; |
897 | 897 |
///Instantiates a \ref DistMap. |
898 | 898 |
|
899 | 899 |
///This function instantiates a \ref DistMap. |
900 | 900 |
///\param g is the digraph, to which we would like to define |
901 | 901 |
///the \ref DistMap |
902 | 902 |
static DistMap *createDistMap(const Digraph &g) |
903 | 903 |
{ |
904 | 904 |
return new DistMap(g); |
905 | 905 |
} |
906 | 906 |
|
907 | 907 |
///The type of the shortest paths. |
908 | 908 |
|
909 | 909 |
///The type of the shortest paths. |
910 | 910 |
///It must meet the \ref concepts::Path "Path" concept. |
911 | 911 |
typedef lemon::Path<Digraph> Path; |
912 | 912 |
}; |
913 | 913 |
|
914 | 914 |
/// Default traits class used by \ref BfsWizard |
915 | 915 |
|
916 | 916 |
/// To make it easier to use Bfs algorithm |
917 | 917 |
/// we have created a wizard class. |
918 | 918 |
/// This \ref BfsWizard class needs default traits, |
919 | 919 |
/// as well as the \ref Bfs class. |
920 | 920 |
/// The \ref BfsWizardBase is a class to be the default traits of the |
921 | 921 |
/// \ref BfsWizard class. |
922 | 922 |
template<class GR> |
923 | 923 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
924 | 924 |
{ |
925 | 925 |
|
926 | 926 |
typedef BfsWizardDefaultTraits<GR> Base; |
927 | 927 |
protected: |
928 | 928 |
//The type of the nodes in the digraph. |
929 | 929 |
typedef typename Base::Digraph::Node Node; |
930 | 930 |
|
931 | 931 |
//Pointer to the digraph the algorithm runs on. |
932 | 932 |
void *_g; |
933 | 933 |
//Pointer to the map of reached nodes. |
934 | 934 |
void *_reached; |
935 | 935 |
//Pointer to the map of processed nodes. |
936 | 936 |
void *_processed; |
937 | 937 |
//Pointer to the map of predecessors arcs. |
938 | 938 |
void *_pred; |
939 | 939 |
//Pointer to the map of distances. |
940 | 940 |
void *_dist; |
941 | 941 |
//Pointer to the shortest path to the target node. |
942 | 942 |
void *_path; |
943 | 943 |
//Pointer to the distance of the target node. |
944 | 944 |
int *_di; |
945 | 945 |
|
946 | 946 |
public: |
947 | 947 |
/// Constructor. |
948 | 948 |
|
949 | 949 |
/// This constructor does not require parameters, therefore it initiates |
950 | 950 |
/// all of the attributes to \c 0. |
951 | 951 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
952 | 952 |
_dist(0), _path(0), _di(0) {} |
953 | 953 |
|
954 | 954 |
/// Constructor. |
955 | 955 |
|
956 | 956 |
/// This constructor requires one parameter, |
957 | 957 |
/// others are initiated to \c 0. |
958 | 958 |
/// \param g The digraph the algorithm runs on. |
959 | 959 |
BfsWizardBase(const GR &g) : |
960 | 960 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
961 | 961 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
962 | 962 |
|
963 | 963 |
}; |
... | ... |
@@ -1368,386 +1368,408 @@ |
1368 | 1368 |
bool local_reached; |
1369 | 1369 |
|
1370 | 1370 |
std::vector<typename Digraph::Node> _list; |
1371 | 1371 |
int _list_front, _list_back; |
1372 | 1372 |
|
1373 | 1373 |
///Creates the maps if necessary. |
1374 | 1374 |
///\todo Better memory allocation (instead of new). |
1375 | 1375 |
void create_maps() { |
1376 | 1376 |
if(!_reached) { |
1377 | 1377 |
local_reached = true; |
1378 | 1378 |
_reached = Traits::createReachedMap(*_digraph); |
1379 | 1379 |
} |
1380 | 1380 |
} |
1381 | 1381 |
|
1382 | 1382 |
protected: |
1383 | 1383 |
|
1384 | 1384 |
BfsVisit() {} |
1385 | 1385 |
|
1386 | 1386 |
public: |
1387 | 1387 |
|
1388 | 1388 |
typedef BfsVisit Create; |
1389 | 1389 |
|
1390 | 1390 |
/// \name Named template parameters |
1391 | 1391 |
|
1392 | 1392 |
///@{ |
1393 | 1393 |
template <class T> |
1394 | 1394 |
struct SetReachedMapTraits : public Traits { |
1395 | 1395 |
typedef T ReachedMap; |
1396 | 1396 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1397 | 1397 |
throw UninitializedParameter(); |
1398 | 1398 |
} |
1399 | 1399 |
}; |
1400 | 1400 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1401 | 1401 |
/// ReachedMap type. |
1402 | 1402 |
/// |
1403 | 1403 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1404 | 1404 |
template <class T> |
1405 | 1405 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
1406 | 1406 |
SetReachedMapTraits<T> > { |
1407 | 1407 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1408 | 1408 |
}; |
1409 | 1409 |
///@} |
1410 | 1410 |
|
1411 | 1411 |
public: |
1412 | 1412 |
|
1413 | 1413 |
/// \brief Constructor. |
1414 | 1414 |
/// |
1415 | 1415 |
/// Constructor. |
1416 | 1416 |
/// |
1417 | 1417 |
/// \param digraph The digraph the algorithm runs on. |
1418 | 1418 |
/// \param visitor The visitor object of the algorithm. |
1419 | 1419 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
1420 | 1420 |
: _digraph(&digraph), _visitor(&visitor), |
1421 | 1421 |
_reached(0), local_reached(false) {} |
1422 | 1422 |
|
1423 | 1423 |
/// \brief Destructor. |
1424 | 1424 |
~BfsVisit() { |
1425 | 1425 |
if(local_reached) delete _reached; |
1426 | 1426 |
} |
1427 | 1427 |
|
1428 | 1428 |
/// \brief Sets the map that indicates which nodes are reached. |
1429 | 1429 |
/// |
1430 | 1430 |
/// Sets the map that indicates which nodes are reached. |
1431 | 1431 |
/// If you don't use this function before calling \ref run(), |
1432 | 1432 |
/// it will allocate one. The destructor deallocates this |
1433 | 1433 |
/// automatically allocated map, of course. |
1434 | 1434 |
/// \return <tt> (*this) </tt> |
1435 | 1435 |
BfsVisit &reachedMap(ReachedMap &m) { |
1436 | 1436 |
if(local_reached) { |
1437 | 1437 |
delete _reached; |
1438 | 1438 |
local_reached = false; |
1439 | 1439 |
} |
1440 | 1440 |
_reached = &m; |
1441 | 1441 |
return *this; |
1442 | 1442 |
} |
1443 | 1443 |
|
1444 | 1444 |
public: |
1445 | 1445 |
|
1446 | 1446 |
/// \name Execution control |
1447 | 1447 |
/// The simplest way to execute the algorithm is to use |
1448 | 1448 |
/// one of the member functions called \ref lemon::BfsVisit::run() |
1449 | 1449 |
/// "run()". |
1450 | 1450 |
/// \n |
1451 | 1451 |
/// If you need more control on the execution, first you must call |
1452 | 1452 |
/// \ref lemon::BfsVisit::init() "init()", then you can add several |
1453 | 1453 |
/// source nodes with \ref lemon::BfsVisit::addSource() "addSource()". |
1454 | 1454 |
/// Finally \ref lemon::BfsVisit::start() "start()" will perform the |
1455 | 1455 |
/// actual path computation. |
1456 | 1456 |
|
1457 | 1457 |
/// @{ |
1458 | 1458 |
|
1459 | 1459 |
/// \brief Initializes the internal data structures. |
1460 | 1460 |
/// |
1461 | 1461 |
/// Initializes the internal data structures. |
1462 | 1462 |
void init() { |
1463 | 1463 |
create_maps(); |
1464 | 1464 |
_list.resize(countNodes(*_digraph)); |
1465 | 1465 |
_list_front = _list_back = -1; |
1466 | 1466 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1467 | 1467 |
_reached->set(u, false); |
1468 | 1468 |
} |
1469 | 1469 |
} |
1470 | 1470 |
|
1471 | 1471 |
/// \brief Adds a new source node. |
1472 | 1472 |
/// |
1473 | 1473 |
/// Adds a new source node to the set of nodes to be processed. |
1474 | 1474 |
void addSource(Node s) { |
1475 | 1475 |
if(!(*_reached)[s]) { |
1476 | 1476 |
_reached->set(s,true); |
1477 | 1477 |
_visitor->start(s); |
1478 | 1478 |
_visitor->reach(s); |
1479 | 1479 |
_list[++_list_back] = s; |
1480 | 1480 |
} |
1481 | 1481 |
} |
1482 | 1482 |
|
1483 | 1483 |
/// \brief Processes the next node. |
1484 | 1484 |
/// |
1485 | 1485 |
/// Processes the next node. |
1486 | 1486 |
/// |
1487 | 1487 |
/// \return The processed node. |
1488 | 1488 |
/// |
1489 | 1489 |
/// \pre The queue must not be empty. |
1490 | 1490 |
Node processNextNode() { |
1491 | 1491 |
Node n = _list[++_list_front]; |
1492 | 1492 |
_visitor->process(n); |
1493 | 1493 |
Arc e; |
1494 | 1494 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1495 | 1495 |
Node m = _digraph->target(e); |
1496 | 1496 |
if (!(*_reached)[m]) { |
1497 | 1497 |
_visitor->discover(e); |
1498 | 1498 |
_visitor->reach(m); |
1499 | 1499 |
_reached->set(m, true); |
1500 | 1500 |
_list[++_list_back] = m; |
1501 | 1501 |
} else { |
1502 | 1502 |
_visitor->examine(e); |
1503 | 1503 |
} |
1504 | 1504 |
} |
1505 | 1505 |
return n; |
1506 | 1506 |
} |
1507 | 1507 |
|
1508 | 1508 |
/// \brief Processes the next node. |
1509 | 1509 |
/// |
1510 | 1510 |
/// Processes the next node and checks if the given target node |
1511 | 1511 |
/// is reached. If the target node is reachable from the processed |
1512 | 1512 |
/// node, then the \c reach parameter will be set to \c true. |
1513 | 1513 |
/// |
1514 | 1514 |
/// \param target The target node. |
1515 | 1515 |
/// \retval reach Indicates if the target node is reached. |
1516 | 1516 |
/// It should be initially \c false. |
1517 | 1517 |
/// |
1518 | 1518 |
/// \return The processed node. |
1519 | 1519 |
/// |
1520 | 1520 |
/// \pre The queue must not be empty. |
1521 | 1521 |
Node processNextNode(Node target, bool& reach) { |
1522 | 1522 |
Node n = _list[++_list_front]; |
1523 | 1523 |
_visitor->process(n); |
1524 | 1524 |
Arc e; |
1525 | 1525 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1526 | 1526 |
Node m = _digraph->target(e); |
1527 | 1527 |
if (!(*_reached)[m]) { |
1528 | 1528 |
_visitor->discover(e); |
1529 | 1529 |
_visitor->reach(m); |
1530 | 1530 |
_reached->set(m, true); |
1531 | 1531 |
_list[++_list_back] = m; |
1532 | 1532 |
reach = reach || (target == m); |
1533 | 1533 |
} else { |
1534 | 1534 |
_visitor->examine(e); |
1535 | 1535 |
} |
1536 | 1536 |
} |
1537 | 1537 |
return n; |
1538 | 1538 |
} |
1539 | 1539 |
|
1540 | 1540 |
/// \brief Processes the next node. |
1541 | 1541 |
/// |
1542 | 1542 |
/// Processes the next node and checks if at least one of reached |
1543 | 1543 |
/// nodes has \c true value in the \c nm node map. If one node |
1544 | 1544 |
/// with \c true value is reachable from the processed node, then the |
1545 | 1545 |
/// \c rnode parameter will be set to the first of such nodes. |
1546 | 1546 |
/// |
1547 | 1547 |
/// \param nm A \c bool (or convertible) node map that indicates the |
1548 | 1548 |
/// possible targets. |
1549 | 1549 |
/// \retval rnode The reached target node. |
1550 | 1550 |
/// It should be initially \c INVALID. |
1551 | 1551 |
/// |
1552 | 1552 |
/// \return The processed node. |
1553 | 1553 |
/// |
1554 | 1554 |
/// \pre The queue must not be empty. |
1555 | 1555 |
template <typename NM> |
1556 | 1556 |
Node processNextNode(const NM& nm, Node& rnode) { |
1557 | 1557 |
Node n = _list[++_list_front]; |
1558 | 1558 |
_visitor->process(n); |
1559 | 1559 |
Arc e; |
1560 | 1560 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1561 | 1561 |
Node m = _digraph->target(e); |
1562 | 1562 |
if (!(*_reached)[m]) { |
1563 | 1563 |
_visitor->discover(e); |
1564 | 1564 |
_visitor->reach(m); |
1565 | 1565 |
_reached->set(m, true); |
1566 | 1566 |
_list[++_list_back] = m; |
1567 | 1567 |
if (nm[m] && rnode == INVALID) rnode = m; |
1568 | 1568 |
} else { |
1569 | 1569 |
_visitor->examine(e); |
1570 | 1570 |
} |
1571 | 1571 |
} |
1572 | 1572 |
return n; |
1573 | 1573 |
} |
1574 | 1574 |
|
1575 | 1575 |
/// \brief The next node to be processed. |
1576 | 1576 |
/// |
1577 | 1577 |
/// Returns the next node to be processed or \c INVALID if the queue |
1578 | 1578 |
/// is empty. |
1579 | 1579 |
Node nextNode() const { |
1580 | 1580 |
return _list_front != _list_back ? _list[_list_front + 1] : INVALID; |
1581 | 1581 |
} |
1582 | 1582 |
|
1583 | 1583 |
/// \brief Returns \c false if there are nodes |
1584 | 1584 |
/// to be processed. |
1585 | 1585 |
/// |
1586 | 1586 |
/// Returns \c false if there are nodes |
1587 | 1587 |
/// to be processed in the queue. |
1588 | 1588 |
bool emptyQueue() const { return _list_front == _list_back; } |
1589 | 1589 |
|
1590 | 1590 |
/// \brief Returns the number of the nodes to be processed. |
1591 | 1591 |
/// |
1592 | 1592 |
/// Returns the number of the nodes to be processed in the queue. |
1593 | 1593 |
int queueSize() const { return _list_back - _list_front; } |
1594 | 1594 |
|
1595 | 1595 |
/// \brief Executes the algorithm. |
1596 | 1596 |
/// |
1597 | 1597 |
/// Executes the algorithm. |
1598 | 1598 |
/// |
1599 | 1599 |
/// This method runs the %BFS algorithm from the root node(s) |
1600 | 1600 |
/// in order to compute the shortest path to each node. |
1601 | 1601 |
/// |
1602 | 1602 |
/// The algorithm computes |
1603 | 1603 |
/// - the shortest path tree (forest), |
1604 | 1604 |
/// - the distance of each node from the root(s). |
1605 | 1605 |
/// |
1606 | 1606 |
/// \pre init() must be called and at least one root node should be added |
1607 | 1607 |
/// with addSource() before using this function. |
1608 | 1608 |
/// |
1609 | 1609 |
/// \note <tt>b.start()</tt> is just a shortcut of the following code. |
1610 | 1610 |
/// \code |
1611 | 1611 |
/// while ( !b.emptyQueue() ) { |
1612 | 1612 |
/// b.processNextNode(); |
1613 | 1613 |
/// } |
1614 | 1614 |
/// \endcode |
1615 | 1615 |
void start() { |
1616 | 1616 |
while ( !emptyQueue() ) processNextNode(); |
1617 | 1617 |
} |
1618 | 1618 |
|
1619 | 1619 |
/// \brief Executes the algorithm until the given target node is reached. |
1620 | 1620 |
/// |
1621 | 1621 |
/// Executes the algorithm until the given target node is reached. |
1622 | 1622 |
/// |
1623 | 1623 |
/// This method runs the %BFS algorithm from the root node(s) |
1624 |
/// in order to compute the shortest path to \c |
|
1624 |
/// in order to compute the shortest path to \c t. |
|
1625 | 1625 |
/// |
1626 | 1626 |
/// The algorithm computes |
1627 |
/// - the shortest path to \c dest, |
|
1628 |
/// - the distance of \c dest from the root(s). |
|
1627 |
/// - the shortest path to \c t, |
|
1628 |
/// - the distance of \c t from the root(s). |
|
1629 | 1629 |
/// |
1630 | 1630 |
/// \pre init() must be called and at least one root node should be |
1631 | 1631 |
/// added with addSource() before using this function. |
1632 | 1632 |
/// |
1633 | 1633 |
/// \note <tt>b.start(t)</tt> is just a shortcut of the following code. |
1634 | 1634 |
/// \code |
1635 | 1635 |
/// bool reach = false; |
1636 | 1636 |
/// while ( !b.emptyQueue() && !reach ) { |
1637 | 1637 |
/// b.processNextNode(t, reach); |
1638 | 1638 |
/// } |
1639 | 1639 |
/// \endcode |
1640 |
void start(Node |
|
1640 |
void start(Node t) { |
|
1641 | 1641 |
bool reach = false; |
1642 |
while ( !emptyQueue() && !reach ) processNextNode( |
|
1642 |
while ( !emptyQueue() && !reach ) processNextNode(t, reach); |
|
1643 | 1643 |
} |
1644 | 1644 |
|
1645 | 1645 |
/// \brief Executes the algorithm until a condition is met. |
1646 | 1646 |
/// |
1647 | 1647 |
/// Executes the algorithm until a condition is met. |
1648 | 1648 |
/// |
1649 | 1649 |
/// This method runs the %BFS algorithm from the root node(s) in |
1650 | 1650 |
/// order to compute the shortest path to a node \c v with |
1651 | 1651 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
1652 | 1652 |
/// |
1653 | 1653 |
/// \param nm must be a bool (or convertible) node map. The |
1654 | 1654 |
/// algorithm will stop when it reaches a node \c v with |
1655 | 1655 |
/// <tt>nm[v]</tt> true. |
1656 | 1656 |
/// |
1657 | 1657 |
/// \return The reached node \c v with <tt>nm[v]</tt> true or |
1658 | 1658 |
/// \c INVALID if no such node was found. |
1659 | 1659 |
/// |
1660 | 1660 |
/// \pre init() must be called and at least one root node should be |
1661 | 1661 |
/// added with addSource() before using this function. |
1662 | 1662 |
/// |
1663 | 1663 |
/// \note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
1664 | 1664 |
/// \code |
1665 | 1665 |
/// Node rnode = INVALID; |
1666 | 1666 |
/// while ( !b.emptyQueue() && rnode == INVALID ) { |
1667 | 1667 |
/// b.processNextNode(nm, rnode); |
1668 | 1668 |
/// } |
1669 | 1669 |
/// return rnode; |
1670 | 1670 |
/// \endcode |
1671 | 1671 |
template <typename NM> |
1672 | 1672 |
Node start(const NM &nm) { |
1673 | 1673 |
Node rnode = INVALID; |
1674 | 1674 |
while ( !emptyQueue() && rnode == INVALID ) { |
1675 | 1675 |
processNextNode(nm, rnode); |
1676 | 1676 |
} |
1677 | 1677 |
return rnode; |
1678 | 1678 |
} |
1679 | 1679 |
|
1680 |
/// \brief Runs the algorithm from the given node. |
|
1680 |
/// \brief Runs the algorithm from the given source node. |
|
1681 | 1681 |
/// |
1682 | 1682 |
/// This method runs the %BFS algorithm from node \c s |
1683 | 1683 |
/// in order to compute the shortest path to each node. |
1684 | 1684 |
/// |
1685 | 1685 |
/// The algorithm computes |
1686 | 1686 |
/// - the shortest path tree, |
1687 | 1687 |
/// - the distance of each node from the root. |
1688 | 1688 |
/// |
1689 | 1689 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1690 | 1690 |
///\code |
1691 | 1691 |
/// b.init(); |
1692 | 1692 |
/// b.addSource(s); |
1693 | 1693 |
/// b.start(); |
1694 | 1694 |
///\endcode |
1695 | 1695 |
void run(Node s) { |
1696 | 1696 |
init(); |
1697 | 1697 |
addSource(s); |
1698 | 1698 |
start(); |
1699 | 1699 |
} |
1700 | 1700 |
|
1701 |
/// \brief Finds the shortest path between \c s and \c t. |
|
1702 |
/// |
|
1703 |
/// This method runs the %BFS algorithm from node \c s |
|
1704 |
/// in order to compute the shortest path to node \c t |
|
1705 |
/// (it stops searching when \c t is processed). |
|
1706 |
/// |
|
1707 |
/// \return \c true if \c t is reachable form \c s. |
|
1708 |
/// |
|
1709 |
/// \note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
|
1710 |
/// shortcut of the following code. |
|
1711 |
///\code |
|
1712 |
/// b.init(); |
|
1713 |
/// b.addSource(s); |
|
1714 |
/// b.start(t); |
|
1715 |
///\endcode |
|
1716 |
bool run(Node s,Node t) { |
|
1717 |
init(); |
|
1718 |
addSource(s); |
|
1719 |
start(t); |
|
1720 |
return reached(t); |
|
1721 |
} |
|
1722 |
|
|
1701 | 1723 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1702 | 1724 |
/// |
1703 | 1725 |
/// This method runs the %BFS algorithm in order to |
1704 | 1726 |
/// compute the shortest path to each node. |
1705 | 1727 |
/// |
1706 | 1728 |
/// The algorithm computes |
1707 | 1729 |
/// - the shortest path tree (forest), |
1708 | 1730 |
/// - the distance of each node from the root(s). |
1709 | 1731 |
/// |
1710 | 1732 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1711 | 1733 |
///\code |
1712 | 1734 |
/// b.init(); |
1713 | 1735 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
1714 | 1736 |
/// if (!b.reached(n)) { |
1715 | 1737 |
/// b.addSource(n); |
1716 | 1738 |
/// b.start(); |
1717 | 1739 |
/// } |
1718 | 1740 |
/// } |
1719 | 1741 |
///\endcode |
1720 | 1742 |
void run() { |
1721 | 1743 |
init(); |
1722 | 1744 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1723 | 1745 |
if (!reached(it)) { |
1724 | 1746 |
addSource(it); |
1725 | 1747 |
start(); |
1726 | 1748 |
} |
1727 | 1749 |
} |
1728 | 1750 |
} |
1729 | 1751 |
|
1730 | 1752 |
///@} |
1731 | 1753 |
|
1732 | 1754 |
/// \name Query Functions |
1733 | 1755 |
/// The result of the %BFS algorithm can be obtained using these |
1734 | 1756 |
/// functions.\n |
1735 | 1757 |
/// Either \ref lemon::BfsVisit::run() "run()" or |
1736 | 1758 |
/// \ref lemon::BfsVisit::start() "start()" must be called before |
1737 | 1759 |
/// using them. |
1738 | 1760 |
///@{ |
1739 | 1761 |
|
1740 | 1762 |
/// \brief Checks if a node is reachable from the root(s). |
1741 | 1763 |
/// |
1742 | 1764 |
/// Returns \c true if \c v is reachable from the root(s). |
1743 | 1765 |
/// \pre Either \ref run() or \ref start() |
1744 | 1766 |
/// must be called before using this function. |
1745 | 1767 |
bool reached(Node v) { return (*_reached)[v]; } |
1746 | 1768 |
|
1747 | 1769 |
///@} |
1748 | 1770 |
|
1749 | 1771 |
}; |
1750 | 1772 |
|
1751 | 1773 |
} //END OF NAMESPACE LEMON |
1752 | 1774 |
|
1753 | 1775 |
#endif |
... | ... |
@@ -305,593 +305,593 @@ |
305 | 305 |
struct SetStandardProcessedMapTraits : public Traits { |
306 | 306 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
307 | 307 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
308 | 308 |
{ |
309 | 309 |
return new ProcessedMap(g); |
310 | 310 |
} |
311 | 311 |
}; |
312 | 312 |
///\brief \ref named-templ-param "Named parameter" for setting |
313 | 313 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
314 | 314 |
/// |
315 | 315 |
///\ref named-templ-param "Named parameter" for setting |
316 | 316 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
317 | 317 |
///If you don't set it explicitly, it will be automatically allocated. |
318 | 318 |
struct SetStandardProcessedMap : |
319 | 319 |
public Dfs< Digraph, SetStandardProcessedMapTraits > { |
320 | 320 |
typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create; |
321 | 321 |
}; |
322 | 322 |
|
323 | 323 |
///@} |
324 | 324 |
|
325 | 325 |
public: |
326 | 326 |
|
327 | 327 |
///Constructor. |
328 | 328 |
|
329 | 329 |
///Constructor. |
330 | 330 |
///\param g The digraph the algorithm runs on. |
331 | 331 |
Dfs(const Digraph &g) : |
332 | 332 |
G(&g), |
333 | 333 |
_pred(NULL), local_pred(false), |
334 | 334 |
_dist(NULL), local_dist(false), |
335 | 335 |
_reached(NULL), local_reached(false), |
336 | 336 |
_processed(NULL), local_processed(false) |
337 | 337 |
{ } |
338 | 338 |
|
339 | 339 |
///Destructor. |
340 | 340 |
~Dfs() |
341 | 341 |
{ |
342 | 342 |
if(local_pred) delete _pred; |
343 | 343 |
if(local_dist) delete _dist; |
344 | 344 |
if(local_reached) delete _reached; |
345 | 345 |
if(local_processed) delete _processed; |
346 | 346 |
} |
347 | 347 |
|
348 | 348 |
///Sets the map that stores the predecessor arcs. |
349 | 349 |
|
350 | 350 |
///Sets the map that stores the predecessor arcs. |
351 | 351 |
///If you don't use this function before calling \ref run(), |
352 | 352 |
///it will allocate one. The destructor deallocates this |
353 | 353 |
///automatically allocated map, of course. |
354 | 354 |
///\return <tt> (*this) </tt> |
355 | 355 |
Dfs &predMap(PredMap &m) |
356 | 356 |
{ |
357 | 357 |
if(local_pred) { |
358 | 358 |
delete _pred; |
359 | 359 |
local_pred=false; |
360 | 360 |
} |
361 | 361 |
_pred = &m; |
362 | 362 |
return *this; |
363 | 363 |
} |
364 | 364 |
|
365 | 365 |
///Sets the map that indicates which nodes are reached. |
366 | 366 |
|
367 | 367 |
///Sets the map that indicates which nodes are reached. |
368 | 368 |
///If you don't use this function before calling \ref run(), |
369 | 369 |
///it will allocate one. The destructor deallocates this |
370 | 370 |
///automatically allocated map, of course. |
371 | 371 |
///\return <tt> (*this) </tt> |
372 | 372 |
Dfs &reachedMap(ReachedMap &m) |
373 | 373 |
{ |
374 | 374 |
if(local_reached) { |
375 | 375 |
delete _reached; |
376 | 376 |
local_reached=false; |
377 | 377 |
} |
378 | 378 |
_reached = &m; |
379 | 379 |
return *this; |
380 | 380 |
} |
381 | 381 |
|
382 | 382 |
///Sets the map that indicates which nodes are processed. |
383 | 383 |
|
384 | 384 |
///Sets the map that indicates which nodes are processed. |
385 | 385 |
///If you don't use this function before calling \ref run(), |
386 | 386 |
///it will allocate one. The destructor deallocates this |
387 | 387 |
///automatically allocated map, of course. |
388 | 388 |
///\return <tt> (*this) </tt> |
389 | 389 |
Dfs &processedMap(ProcessedMap &m) |
390 | 390 |
{ |
391 | 391 |
if(local_processed) { |
392 | 392 |
delete _processed; |
393 | 393 |
local_processed=false; |
394 | 394 |
} |
395 | 395 |
_processed = &m; |
396 | 396 |
return *this; |
397 | 397 |
} |
398 | 398 |
|
399 | 399 |
///Sets the map that stores the distances of the nodes. |
400 | 400 |
|
401 | 401 |
///Sets the map that stores the distances of the nodes calculated by |
402 | 402 |
///the algorithm. |
403 | 403 |
///If you don't use this function before calling \ref run(), |
404 | 404 |
///it will allocate one. The destructor deallocates this |
405 | 405 |
///automatically allocated map, of course. |
406 | 406 |
///\return <tt> (*this) </tt> |
407 | 407 |
Dfs &distMap(DistMap &m) |
408 | 408 |
{ |
409 | 409 |
if(local_dist) { |
410 | 410 |
delete _dist; |
411 | 411 |
local_dist=false; |
412 | 412 |
} |
413 | 413 |
_dist = &m; |
414 | 414 |
return *this; |
415 | 415 |
} |
416 | 416 |
|
417 | 417 |
public: |
418 | 418 |
|
419 | 419 |
///\name Execution control |
420 | 420 |
///The simplest way to execute the algorithm is to use |
421 | 421 |
///one of the member functions called \ref lemon::Dfs::run() "run()". |
422 | 422 |
///\n |
423 | 423 |
///If you need more control on the execution, first you must call |
424 | 424 |
///\ref lemon::Dfs::init() "init()", then you can add a source node |
425 | 425 |
///with \ref lemon::Dfs::addSource() "addSource()". |
426 | 426 |
///Finally \ref lemon::Dfs::start() "start()" will perform the |
427 | 427 |
///actual path computation. |
428 | 428 |
|
429 | 429 |
///@{ |
430 | 430 |
|
431 | 431 |
///Initializes the internal data structures. |
432 | 432 |
|
433 | 433 |
///Initializes the internal data structures. |
434 | 434 |
/// |
435 | 435 |
void init() |
436 | 436 |
{ |
437 | 437 |
create_maps(); |
438 | 438 |
_stack.resize(countNodes(*G)); |
439 | 439 |
_stack_head=-1; |
440 | 440 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
441 | 441 |
_pred->set(u,INVALID); |
442 | 442 |
_reached->set(u,false); |
443 | 443 |
_processed->set(u,false); |
444 | 444 |
} |
445 | 445 |
} |
446 | 446 |
|
447 | 447 |
///Adds a new source node. |
448 | 448 |
|
449 | 449 |
///Adds a new source node to the set of nodes to be processed. |
450 | 450 |
/// |
451 | 451 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
452 | 452 |
///false results.) |
453 | 453 |
/// |
454 | 454 |
///\warning Distances will be wrong (or at least strange) in case of |
455 | 455 |
///multiple sources. |
456 | 456 |
void addSource(Node s) |
457 | 457 |
{ |
458 | 458 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
459 | 459 |
if(!(*_reached)[s]) |
460 | 460 |
{ |
461 | 461 |
_reached->set(s,true); |
462 | 462 |
_pred->set(s,INVALID); |
463 | 463 |
OutArcIt e(*G,s); |
464 | 464 |
if(e!=INVALID) { |
465 | 465 |
_stack[++_stack_head]=e; |
466 | 466 |
_dist->set(s,_stack_head); |
467 | 467 |
} |
468 | 468 |
else { |
469 | 469 |
_processed->set(s,true); |
470 | 470 |
_dist->set(s,0); |
471 | 471 |
} |
472 | 472 |
} |
473 | 473 |
} |
474 | 474 |
|
475 | 475 |
///Processes the next arc. |
476 | 476 |
|
477 | 477 |
///Processes the next arc. |
478 | 478 |
/// |
479 | 479 |
///\return The processed arc. |
480 | 480 |
/// |
481 | 481 |
///\pre The stack must not be empty. |
482 | 482 |
Arc processNextArc() |
483 | 483 |
{ |
484 | 484 |
Node m; |
485 | 485 |
Arc e=_stack[_stack_head]; |
486 | 486 |
if(!(*_reached)[m=G->target(e)]) { |
487 | 487 |
_pred->set(m,e); |
488 | 488 |
_reached->set(m,true); |
489 | 489 |
++_stack_head; |
490 | 490 |
_stack[_stack_head] = OutArcIt(*G, m); |
491 | 491 |
_dist->set(m,_stack_head); |
492 | 492 |
} |
493 | 493 |
else { |
494 | 494 |
m=G->source(e); |
495 | 495 |
++_stack[_stack_head]; |
496 | 496 |
} |
497 | 497 |
while(_stack_head>=0 && _stack[_stack_head]==INVALID) { |
498 | 498 |
_processed->set(m,true); |
499 | 499 |
--_stack_head; |
500 | 500 |
if(_stack_head>=0) { |
501 | 501 |
m=G->source(_stack[_stack_head]); |
502 | 502 |
++_stack[_stack_head]; |
503 | 503 |
} |
504 | 504 |
} |
505 | 505 |
return e; |
506 | 506 |
} |
507 | 507 |
|
508 | 508 |
///Next arc to be processed. |
509 | 509 |
|
510 | 510 |
///Next arc to be processed. |
511 | 511 |
/// |
512 | 512 |
///\return The next arc to be processed or \c INVALID if the stack |
513 | 513 |
///is empty. |
514 | 514 |
OutArcIt nextArc() const |
515 | 515 |
{ |
516 | 516 |
return _stack_head>=0?_stack[_stack_head]:INVALID; |
517 | 517 |
} |
518 | 518 |
|
519 | 519 |
///\brief Returns \c false if there are nodes |
520 | 520 |
///to be processed. |
521 | 521 |
/// |
522 | 522 |
///Returns \c false if there are nodes |
523 | 523 |
///to be processed in the queue (stack). |
524 | 524 |
bool emptyQueue() const { return _stack_head<0; } |
525 | 525 |
|
526 | 526 |
///Returns the number of the nodes to be processed. |
527 | 527 |
|
528 | 528 |
///Returns the number of the nodes to be processed in the queue (stack). |
529 | 529 |
int queueSize() const { return _stack_head+1; } |
530 | 530 |
|
531 | 531 |
///Executes the algorithm. |
532 | 532 |
|
533 | 533 |
///Executes the algorithm. |
534 | 534 |
/// |
535 | 535 |
///This method runs the %DFS algorithm from the root node |
536 | 536 |
///in order to compute the DFS path to each node. |
537 | 537 |
/// |
538 | 538 |
/// The algorithm computes |
539 | 539 |
///- the %DFS tree, |
540 | 540 |
///- the distance of each node from the root in the %DFS tree. |
541 | 541 |
/// |
542 | 542 |
///\pre init() must be called and a root node should be |
543 | 543 |
///added with addSource() before using this function. |
544 | 544 |
/// |
545 | 545 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
546 | 546 |
///\code |
547 | 547 |
/// while ( !d.emptyQueue() ) { |
548 | 548 |
/// d.processNextArc(); |
549 | 549 |
/// } |
550 | 550 |
///\endcode |
551 | 551 |
void start() |
552 | 552 |
{ |
553 | 553 |
while ( !emptyQueue() ) processNextArc(); |
554 | 554 |
} |
555 | 555 |
|
556 | 556 |
///Executes the algorithm until the given target node is reached. |
557 | 557 |
|
558 | 558 |
///Executes the algorithm until the given target node is reached. |
559 | 559 |
/// |
560 | 560 |
///This method runs the %DFS algorithm from the root node |
561 |
///in order to compute the DFS path to \c |
|
561 |
///in order to compute the DFS path to \c t. |
|
562 | 562 |
/// |
563 | 563 |
///The algorithm computes |
564 |
///- the %DFS path to \c dest, |
|
565 |
///- the distance of \c dest from the root in the %DFS tree. |
|
564 |
///- the %DFS path to \c t, |
|
565 |
///- the distance of \c t from the root in the %DFS tree. |
|
566 | 566 |
/// |
567 | 567 |
///\pre init() must be called and a root node should be |
568 | 568 |
///added with addSource() before using this function. |
569 |
void start(Node |
|
569 |
void start(Node t) |
|
570 | 570 |
{ |
571 |
while ( !emptyQueue() && G->target(_stack[_stack_head])!= |
|
571 |
while ( !emptyQueue() && G->target(_stack[_stack_head])!=t ) |
|
572 | 572 |
processNextArc(); |
573 | 573 |
} |
574 | 574 |
|
575 | 575 |
///Executes the algorithm until a condition is met. |
576 | 576 |
|
577 | 577 |
///Executes the algorithm until a condition is met. |
578 | 578 |
/// |
579 | 579 |
///This method runs the %DFS algorithm from the root node |
580 | 580 |
///until an arc \c a with <tt>am[a]</tt> true is found. |
581 | 581 |
/// |
582 | 582 |
///\param am A \c bool (or convertible) arc map. The algorithm |
583 | 583 |
///will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
584 | 584 |
/// |
585 | 585 |
///\return The reached arc \c a with <tt>am[a]</tt> true or |
586 | 586 |
///\c INVALID if no such arc was found. |
587 | 587 |
/// |
588 | 588 |
///\pre init() must be called and a root node should be |
589 | 589 |
///added with addSource() before using this function. |
590 | 590 |
/// |
591 | 591 |
///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
592 | 592 |
///not a node map. |
593 | 593 |
template<class ArcBoolMap> |
594 | 594 |
Arc start(const ArcBoolMap &am) |
595 | 595 |
{ |
596 | 596 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
597 | 597 |
processNextArc(); |
598 | 598 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
599 | 599 |
} |
600 | 600 |
|
601 |
///Runs the algorithm from the given node. |
|
601 |
///Runs the algorithm from the given source node. |
|
602 | 602 |
|
603 | 603 |
///This method runs the %DFS algorithm from node \c s |
604 | 604 |
///in order to compute the DFS path to each node. |
605 | 605 |
/// |
606 | 606 |
///The algorithm computes |
607 | 607 |
///- the %DFS tree, |
608 | 608 |
///- the distance of each node from the root in the %DFS tree. |
609 | 609 |
/// |
610 | 610 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
611 | 611 |
///\code |
612 | 612 |
/// d.init(); |
613 | 613 |
/// d.addSource(s); |
614 | 614 |
/// d.start(); |
615 | 615 |
///\endcode |
616 | 616 |
void run(Node s) { |
617 | 617 |
init(); |
618 | 618 |
addSource(s); |
619 | 619 |
start(); |
620 | 620 |
} |
621 | 621 |
|
622 | 622 |
///Finds the %DFS path between \c s and \c t. |
623 | 623 |
|
624 | 624 |
///This method runs the %DFS algorithm from node \c s |
625 |
///in order to compute the DFS path to \c t |
|
625 |
///in order to compute the DFS path to node \c t |
|
626 |
///(it stops searching when \c t is processed) |
|
626 | 627 |
/// |
627 |
///\return The length of the <tt>s</tt>--<tt>t</tt> DFS path, |
|
628 |
///if \c t is reachable form \c s, \c 0 otherwise. |
|
628 |
///\return \c true if \c t is reachable form \c s. |
|
629 | 629 |
/// |
630 | 630 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is |
631 | 631 |
///just a shortcut of the following code. |
632 | 632 |
///\code |
633 | 633 |
/// d.init(); |
634 | 634 |
/// d.addSource(s); |
635 | 635 |
/// d.start(t); |
636 | 636 |
///\endcode |
637 |
|
|
637 |
bool run(Node s,Node t) { |
|
638 | 638 |
init(); |
639 | 639 |
addSource(s); |
640 | 640 |
start(t); |
641 |
return reached(t) |
|
641 |
return reached(t); |
|
642 | 642 |
} |
643 | 643 |
|
644 | 644 |
///Runs the algorithm to visit all nodes in the digraph. |
645 | 645 |
|
646 | 646 |
///This method runs the %DFS algorithm in order to compute the |
647 | 647 |
///%DFS path to each node. |
648 | 648 |
/// |
649 | 649 |
///The algorithm computes |
650 | 650 |
///- the %DFS tree, |
651 | 651 |
///- the distance of each node from the root in the %DFS tree. |
652 | 652 |
/// |
653 | 653 |
///\note <tt>d.run()</tt> is just a shortcut of the following code. |
654 | 654 |
///\code |
655 | 655 |
/// d.init(); |
656 | 656 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
657 | 657 |
/// if (!d.reached(n)) { |
658 | 658 |
/// d.addSource(n); |
659 | 659 |
/// d.start(); |
660 | 660 |
/// } |
661 | 661 |
/// } |
662 | 662 |
///\endcode |
663 | 663 |
void run() { |
664 | 664 |
init(); |
665 | 665 |
for (NodeIt it(*G); it != INVALID; ++it) { |
666 | 666 |
if (!reached(it)) { |
667 | 667 |
addSource(it); |
668 | 668 |
start(); |
669 | 669 |
} |
670 | 670 |
} |
671 | 671 |
} |
672 | 672 |
|
673 | 673 |
///@} |
674 | 674 |
|
675 | 675 |
///\name Query Functions |
676 | 676 |
///The result of the %DFS algorithm can be obtained using these |
677 | 677 |
///functions.\n |
678 | 678 |
///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start() |
679 | 679 |
///"start()" must be called before using them. |
680 | 680 |
|
681 | 681 |
///@{ |
682 | 682 |
|
683 | 683 |
///The DFS path to a node. |
684 | 684 |
|
685 | 685 |
///Returns the DFS path to a node. |
686 | 686 |
/// |
687 | 687 |
///\warning \c t should be reachable from the root. |
688 | 688 |
/// |
689 | 689 |
///\pre Either \ref run() or \ref start() must be called before |
690 | 690 |
///using this function. |
691 | 691 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
692 | 692 |
|
693 | 693 |
///The distance of a node from the root. |
694 | 694 |
|
695 | 695 |
///Returns the distance of a node from the root. |
696 | 696 |
/// |
697 | 697 |
///\warning If node \c v is not reachable from the root, then |
698 | 698 |
///the return value of this function is undefined. |
699 | 699 |
/// |
700 | 700 |
///\pre Either \ref run() or \ref start() must be called before |
701 | 701 |
///using this function. |
702 | 702 |
int dist(Node v) const { return (*_dist)[v]; } |
703 | 703 |
|
704 | 704 |
///Returns the 'previous arc' of the %DFS tree for a node. |
705 | 705 |
|
706 | 706 |
///This function returns the 'previous arc' of the %DFS tree for the |
707 | 707 |
///node \c v, i.e. it returns the last arc of a %DFS path from the |
708 | 708 |
///root to \c v. It is \c INVALID |
709 | 709 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
710 | 710 |
/// |
711 | 711 |
///The %DFS tree used here is equal to the %DFS tree used in |
712 | 712 |
///\ref predNode(). |
713 | 713 |
/// |
714 | 714 |
///\pre Either \ref run() or \ref start() must be called before using |
715 | 715 |
///this function. |
716 | 716 |
Arc predArc(Node v) const { return (*_pred)[v];} |
717 | 717 |
|
718 | 718 |
///Returns the 'previous node' of the %DFS tree. |
719 | 719 |
|
720 | 720 |
///This function returns the 'previous node' of the %DFS |
721 | 721 |
///tree for the node \c v, i.e. it returns the last but one node |
722 | 722 |
///from a %DFS path from the root to \c v. It is \c INVALID |
723 | 723 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
724 | 724 |
/// |
725 | 725 |
///The %DFS tree used here is equal to the %DFS tree used in |
726 | 726 |
///\ref predArc(). |
727 | 727 |
/// |
728 | 728 |
///\pre Either \ref run() or \ref start() must be called before |
729 | 729 |
///using this function. |
730 | 730 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
731 | 731 |
G->source((*_pred)[v]); } |
732 | 732 |
|
733 | 733 |
///\brief Returns a const reference to the node map that stores the |
734 | 734 |
///distances of the nodes. |
735 | 735 |
/// |
736 | 736 |
///Returns a const reference to the node map that stores the |
737 | 737 |
///distances of the nodes calculated by the algorithm. |
738 | 738 |
/// |
739 | 739 |
///\pre Either \ref run() or \ref init() |
740 | 740 |
///must be called before using this function. |
741 | 741 |
const DistMap &distMap() const { return *_dist;} |
742 | 742 |
|
743 | 743 |
///\brief Returns a const reference to the node map that stores the |
744 | 744 |
///predecessor arcs. |
745 | 745 |
/// |
746 | 746 |
///Returns a const reference to the node map that stores the predecessor |
747 | 747 |
///arcs, which form the DFS tree. |
748 | 748 |
/// |
749 | 749 |
///\pre Either \ref run() or \ref init() |
750 | 750 |
///must be called before using this function. |
751 | 751 |
const PredMap &predMap() const { return *_pred;} |
752 | 752 |
|
753 | 753 |
///Checks if a node is reachable from the root(s). |
754 | 754 |
|
755 | 755 |
///Returns \c true if \c v is reachable from the root(s). |
756 | 756 |
///\pre Either \ref run() or \ref start() |
757 | 757 |
///must be called before using this function. |
758 | 758 |
bool reached(Node v) const { return (*_reached)[v]; } |
759 | 759 |
|
760 | 760 |
///@} |
761 | 761 |
}; |
762 | 762 |
|
763 | 763 |
///Default traits class of dfs() function. |
764 | 764 |
|
765 | 765 |
///Default traits class of dfs() function. |
766 | 766 |
///\tparam GR Digraph type. |
767 | 767 |
template<class GR> |
768 | 768 |
struct DfsWizardDefaultTraits |
769 | 769 |
{ |
770 | 770 |
///The type of the digraph the algorithm runs on. |
771 | 771 |
typedef GR Digraph; |
772 | 772 |
|
773 | 773 |
///\brief The type of the map that stores the predecessor |
774 | 774 |
///arcs of the %DFS paths. |
775 | 775 |
/// |
776 | 776 |
///The type of the map that stores the predecessor |
777 | 777 |
///arcs of the %DFS paths. |
778 | 778 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
779 | 779 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
780 | 780 |
///Instantiates a \ref PredMap. |
781 | 781 |
|
782 | 782 |
///This function instantiates a \ref PredMap. |
783 | 783 |
///\param g is the digraph, to which we would like to define the |
784 | 784 |
///\ref PredMap. |
785 | 785 |
///\todo The digraph alone may be insufficient to initialize |
786 | 786 |
static PredMap *createPredMap(const Digraph &g) |
787 | 787 |
{ |
788 | 788 |
return new PredMap(g); |
789 | 789 |
} |
790 | 790 |
|
791 | 791 |
///The type of the map that indicates which nodes are processed. |
792 | 792 |
|
793 | 793 |
///The type of the map that indicates which nodes are processed. |
794 | 794 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
795 | 795 |
///By default it is a NullMap. |
796 | 796 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
797 | 797 |
///Instantiates a \ref ProcessedMap. |
798 | 798 |
|
799 | 799 |
///This function instantiates a \ref ProcessedMap. |
800 | 800 |
///\param g is the digraph, to which |
801 | 801 |
///we would like to define the \ref ProcessedMap. |
802 | 802 |
#ifdef DOXYGEN |
803 | 803 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
804 | 804 |
#else |
805 | 805 |
static ProcessedMap *createProcessedMap(const Digraph &) |
806 | 806 |
#endif |
807 | 807 |
{ |
808 | 808 |
return new ProcessedMap(); |
809 | 809 |
} |
810 | 810 |
|
811 | 811 |
///The type of the map that indicates which nodes are reached. |
812 | 812 |
|
813 | 813 |
///The type of the map that indicates which nodes are reached. |
814 | 814 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
815 | 815 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
816 | 816 |
///Instantiates a \ref ReachedMap. |
817 | 817 |
|
818 | 818 |
///This function instantiates a \ref ReachedMap. |
819 | 819 |
///\param g is the digraph, to which |
820 | 820 |
///we would like to define the \ref ReachedMap. |
821 | 821 |
static ReachedMap *createReachedMap(const Digraph &g) |
822 | 822 |
{ |
823 | 823 |
return new ReachedMap(g); |
824 | 824 |
} |
825 | 825 |
|
826 | 826 |
///The type of the map that stores the distances of the nodes. |
827 | 827 |
|
828 | 828 |
///The type of the map that stores the distances of the nodes. |
829 | 829 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
830 | 830 |
typedef typename Digraph::template NodeMap<int> DistMap; |
831 | 831 |
///Instantiates a \ref DistMap. |
832 | 832 |
|
833 | 833 |
///This function instantiates a \ref DistMap. |
834 | 834 |
///\param g is the digraph, to which we would like to define |
835 | 835 |
///the \ref DistMap |
836 | 836 |
static DistMap *createDistMap(const Digraph &g) |
837 | 837 |
{ |
838 | 838 |
return new DistMap(g); |
839 | 839 |
} |
840 | 840 |
|
841 | 841 |
///The type of the DFS paths. |
842 | 842 |
|
843 | 843 |
///The type of the DFS paths. |
844 | 844 |
///It must meet the \ref concepts::Path "Path" concept. |
845 | 845 |
typedef lemon::Path<Digraph> Path; |
846 | 846 |
}; |
847 | 847 |
|
848 | 848 |
/// Default traits class used by \ref DfsWizard |
849 | 849 |
|
850 | 850 |
/// To make it easier to use Dfs algorithm |
851 | 851 |
/// we have created a wizard class. |
852 | 852 |
/// This \ref DfsWizard class needs default traits, |
853 | 853 |
/// as well as the \ref Dfs class. |
854 | 854 |
/// The \ref DfsWizardBase is a class to be the default traits of the |
855 | 855 |
/// \ref DfsWizard class. |
856 | 856 |
template<class GR> |
857 | 857 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
858 | 858 |
{ |
859 | 859 |
|
860 | 860 |
typedef DfsWizardDefaultTraits<GR> Base; |
861 | 861 |
protected: |
862 | 862 |
//The type of the nodes in the digraph. |
863 | 863 |
typedef typename Base::Digraph::Node Node; |
864 | 864 |
|
865 | 865 |
//Pointer to the digraph the algorithm runs on. |
866 | 866 |
void *_g; |
867 | 867 |
//Pointer to the map of reached nodes. |
868 | 868 |
void *_reached; |
869 | 869 |
//Pointer to the map of processed nodes. |
870 | 870 |
void *_processed; |
871 | 871 |
//Pointer to the map of predecessors arcs. |
872 | 872 |
void *_pred; |
873 | 873 |
//Pointer to the map of distances. |
874 | 874 |
void *_dist; |
875 | 875 |
//Pointer to the DFS path to the target node. |
876 | 876 |
void *_path; |
877 | 877 |
//Pointer to the distance of the target node. |
878 | 878 |
int *_di; |
879 | 879 |
|
880 | 880 |
public: |
881 | 881 |
/// Constructor. |
882 | 882 |
|
883 | 883 |
/// This constructor does not require parameters, therefore it initiates |
884 | 884 |
/// all of the attributes to \c 0. |
885 | 885 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
886 | 886 |
_dist(0), _path(0), _di(0) {} |
887 | 887 |
|
888 | 888 |
/// Constructor. |
889 | 889 |
|
890 | 890 |
/// This constructor requires one parameter, |
891 | 891 |
/// others are initiated to \c 0. |
892 | 892 |
/// \param g The digraph the algorithm runs on. |
893 | 893 |
DfsWizardBase(const GR &g) : |
894 | 894 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
895 | 895 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
896 | 896 |
|
897 | 897 |
}; |
... | ... |
@@ -1273,390 +1273,390 @@ |
1273 | 1273 |
#endif |
1274 | 1274 |
class DfsVisit { |
1275 | 1275 |
public: |
1276 | 1276 |
|
1277 | 1277 |
/// \brief \ref Exception for uninitialized parameters. |
1278 | 1278 |
/// |
1279 | 1279 |
/// This error represents problems in the initialization |
1280 | 1280 |
/// of the parameters of the algorithm. |
1281 | 1281 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1282 | 1282 |
public: |
1283 | 1283 |
virtual const char* what() const throw() |
1284 | 1284 |
{ |
1285 | 1285 |
return "lemon::DfsVisit::UninitializedParameter"; |
1286 | 1286 |
} |
1287 | 1287 |
}; |
1288 | 1288 |
|
1289 | 1289 |
///The traits class. |
1290 | 1290 |
typedef _Traits Traits; |
1291 | 1291 |
|
1292 | 1292 |
///The type of the digraph the algorithm runs on. |
1293 | 1293 |
typedef typename Traits::Digraph Digraph; |
1294 | 1294 |
|
1295 | 1295 |
///The visitor type used by the algorithm. |
1296 | 1296 |
typedef _Visitor Visitor; |
1297 | 1297 |
|
1298 | 1298 |
///The type of the map that indicates which nodes are reached. |
1299 | 1299 |
typedef typename Traits::ReachedMap ReachedMap; |
1300 | 1300 |
|
1301 | 1301 |
private: |
1302 | 1302 |
|
1303 | 1303 |
typedef typename Digraph::Node Node; |
1304 | 1304 |
typedef typename Digraph::NodeIt NodeIt; |
1305 | 1305 |
typedef typename Digraph::Arc Arc; |
1306 | 1306 |
typedef typename Digraph::OutArcIt OutArcIt; |
1307 | 1307 |
|
1308 | 1308 |
//Pointer to the underlying digraph. |
1309 | 1309 |
const Digraph *_digraph; |
1310 | 1310 |
//Pointer to the visitor object. |
1311 | 1311 |
Visitor *_visitor; |
1312 | 1312 |
//Pointer to the map of reached status of the nodes. |
1313 | 1313 |
ReachedMap *_reached; |
1314 | 1314 |
//Indicates if _reached is locally allocated (true) or not. |
1315 | 1315 |
bool local_reached; |
1316 | 1316 |
|
1317 | 1317 |
std::vector<typename Digraph::Arc> _stack; |
1318 | 1318 |
int _stack_head; |
1319 | 1319 |
|
1320 | 1320 |
///Creates the maps if necessary. |
1321 | 1321 |
///\todo Better memory allocation (instead of new). |
1322 | 1322 |
void create_maps() { |
1323 | 1323 |
if(!_reached) { |
1324 | 1324 |
local_reached = true; |
1325 | 1325 |
_reached = Traits::createReachedMap(*_digraph); |
1326 | 1326 |
} |
1327 | 1327 |
} |
1328 | 1328 |
|
1329 | 1329 |
protected: |
1330 | 1330 |
|
1331 | 1331 |
DfsVisit() {} |
1332 | 1332 |
|
1333 | 1333 |
public: |
1334 | 1334 |
|
1335 | 1335 |
typedef DfsVisit Create; |
1336 | 1336 |
|
1337 | 1337 |
/// \name Named template parameters |
1338 | 1338 |
|
1339 | 1339 |
///@{ |
1340 | 1340 |
template <class T> |
1341 | 1341 |
struct SetReachedMapTraits : public Traits { |
1342 | 1342 |
typedef T ReachedMap; |
1343 | 1343 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1344 | 1344 |
throw UninitializedParameter(); |
1345 | 1345 |
} |
1346 | 1346 |
}; |
1347 | 1347 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1348 | 1348 |
/// ReachedMap type. |
1349 | 1349 |
/// |
1350 | 1350 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1351 | 1351 |
template <class T> |
1352 | 1352 |
struct SetReachedMap : public DfsVisit< Digraph, Visitor, |
1353 | 1353 |
SetReachedMapTraits<T> > { |
1354 | 1354 |
typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1355 | 1355 |
}; |
1356 | 1356 |
///@} |
1357 | 1357 |
|
1358 | 1358 |
public: |
1359 | 1359 |
|
1360 | 1360 |
/// \brief Constructor. |
1361 | 1361 |
/// |
1362 | 1362 |
/// Constructor. |
1363 | 1363 |
/// |
1364 | 1364 |
/// \param digraph The digraph the algorithm runs on. |
1365 | 1365 |
/// \param visitor The visitor object of the algorithm. |
1366 | 1366 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
1367 | 1367 |
: _digraph(&digraph), _visitor(&visitor), |
1368 | 1368 |
_reached(0), local_reached(false) {} |
1369 | 1369 |
|
1370 | 1370 |
/// \brief Destructor. |
1371 | 1371 |
~DfsVisit() { |
1372 | 1372 |
if(local_reached) delete _reached; |
1373 | 1373 |
} |
1374 | 1374 |
|
1375 | 1375 |
/// \brief Sets the map that indicates which nodes are reached. |
1376 | 1376 |
/// |
1377 | 1377 |
/// Sets the map that indicates which nodes are reached. |
1378 | 1378 |
/// If you don't use this function before calling \ref run(), |
1379 | 1379 |
/// it will allocate one. The destructor deallocates this |
1380 | 1380 |
/// automatically allocated map, of course. |
1381 | 1381 |
/// \return <tt> (*this) </tt> |
1382 | 1382 |
DfsVisit &reachedMap(ReachedMap &m) { |
1383 | 1383 |
if(local_reached) { |
1384 | 1384 |
delete _reached; |
1385 | 1385 |
local_reached=false; |
1386 | 1386 |
} |
1387 | 1387 |
_reached = &m; |
1388 | 1388 |
return *this; |
1389 | 1389 |
} |
1390 | 1390 |
|
1391 | 1391 |
public: |
1392 | 1392 |
|
1393 | 1393 |
/// \name Execution control |
1394 | 1394 |
/// The simplest way to execute the algorithm is to use |
1395 | 1395 |
/// one of the member functions called \ref lemon::DfsVisit::run() |
1396 | 1396 |
/// "run()". |
1397 | 1397 |
/// \n |
1398 | 1398 |
/// If you need more control on the execution, first you must call |
1399 | 1399 |
/// \ref lemon::DfsVisit::init() "init()", then you can add several |
1400 | 1400 |
/// source nodes with \ref lemon::DfsVisit::addSource() "addSource()". |
1401 | 1401 |
/// Finally \ref lemon::DfsVisit::start() "start()" will perform the |
1402 | 1402 |
/// actual path computation. |
1403 | 1403 |
|
1404 | 1404 |
/// @{ |
1405 | 1405 |
|
1406 | 1406 |
/// \brief Initializes the internal data structures. |
1407 | 1407 |
/// |
1408 | 1408 |
/// Initializes the internal data structures. |
1409 | 1409 |
void init() { |
1410 | 1410 |
create_maps(); |
1411 | 1411 |
_stack.resize(countNodes(*_digraph)); |
1412 | 1412 |
_stack_head = -1; |
1413 | 1413 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1414 | 1414 |
_reached->set(u, false); |
1415 | 1415 |
} |
1416 | 1416 |
} |
1417 | 1417 |
|
1418 | 1418 |
///Adds a new source node. |
1419 | 1419 |
|
1420 | 1420 |
///Adds a new source node to the set of nodes to be processed. |
1421 | 1421 |
/// |
1422 | 1422 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
1423 | 1423 |
///false results.) |
1424 | 1424 |
/// |
1425 | 1425 |
///\warning Distances will be wrong (or at least strange) in case of |
1426 | 1426 |
///multiple sources. |
1427 | 1427 |
void addSource(Node s) |
1428 | 1428 |
{ |
1429 | 1429 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
1430 | 1430 |
if(!(*_reached)[s]) { |
1431 | 1431 |
_reached->set(s,true); |
1432 | 1432 |
_visitor->start(s); |
1433 | 1433 |
_visitor->reach(s); |
1434 | 1434 |
Arc e; |
1435 | 1435 |
_digraph->firstOut(e, s); |
1436 | 1436 |
if (e != INVALID) { |
1437 | 1437 |
_stack[++_stack_head] = e; |
1438 | 1438 |
} else { |
1439 | 1439 |
_visitor->leave(s); |
1440 | 1440 |
} |
1441 | 1441 |
} |
1442 | 1442 |
} |
1443 | 1443 |
|
1444 | 1444 |
/// \brief Processes the next arc. |
1445 | 1445 |
/// |
1446 | 1446 |
/// Processes the next arc. |
1447 | 1447 |
/// |
1448 | 1448 |
/// \return The processed arc. |
1449 | 1449 |
/// |
1450 | 1450 |
/// \pre The stack must not be empty. |
1451 | 1451 |
Arc processNextArc() { |
1452 | 1452 |
Arc e = _stack[_stack_head]; |
1453 | 1453 |
Node m = _digraph->target(e); |
1454 | 1454 |
if(!(*_reached)[m]) { |
1455 | 1455 |
_visitor->discover(e); |
1456 | 1456 |
_visitor->reach(m); |
1457 | 1457 |
_reached->set(m, true); |
1458 | 1458 |
_digraph->firstOut(_stack[++_stack_head], m); |
1459 | 1459 |
} else { |
1460 | 1460 |
_visitor->examine(e); |
1461 | 1461 |
m = _digraph->source(e); |
1462 | 1462 |
_digraph->nextOut(_stack[_stack_head]); |
1463 | 1463 |
} |
1464 | 1464 |
while (_stack_head>=0 && _stack[_stack_head] == INVALID) { |
1465 | 1465 |
_visitor->leave(m); |
1466 | 1466 |
--_stack_head; |
1467 | 1467 |
if (_stack_head >= 0) { |
1468 | 1468 |
_visitor->backtrack(_stack[_stack_head]); |
1469 | 1469 |
m = _digraph->source(_stack[_stack_head]); |
1470 | 1470 |
_digraph->nextOut(_stack[_stack_head]); |
1471 | 1471 |
} else { |
1472 | 1472 |
_visitor->stop(m); |
1473 | 1473 |
} |
1474 | 1474 |
} |
1475 | 1475 |
return e; |
1476 | 1476 |
} |
1477 | 1477 |
|
1478 | 1478 |
/// \brief Next arc to be processed. |
1479 | 1479 |
/// |
1480 | 1480 |
/// Next arc to be processed. |
1481 | 1481 |
/// |
1482 | 1482 |
/// \return The next arc to be processed or INVALID if the stack is |
1483 | 1483 |
/// empty. |
1484 | 1484 |
Arc nextArc() const { |
1485 | 1485 |
return _stack_head >= 0 ? _stack[_stack_head] : INVALID; |
1486 | 1486 |
} |
1487 | 1487 |
|
1488 | 1488 |
/// \brief Returns \c false if there are nodes |
1489 | 1489 |
/// to be processed. |
1490 | 1490 |
/// |
1491 | 1491 |
/// Returns \c false if there are nodes |
1492 | 1492 |
/// to be processed in the queue (stack). |
1493 | 1493 |
bool emptyQueue() const { return _stack_head < 0; } |
1494 | 1494 |
|
1495 | 1495 |
/// \brief Returns the number of the nodes to be processed. |
1496 | 1496 |
/// |
1497 | 1497 |
/// Returns the number of the nodes to be processed in the queue (stack). |
1498 | 1498 |
int queueSize() const { return _stack_head + 1; } |
1499 | 1499 |
|
1500 | 1500 |
/// \brief Executes the algorithm. |
1501 | 1501 |
/// |
1502 | 1502 |
/// Executes the algorithm. |
1503 | 1503 |
/// |
1504 | 1504 |
/// This method runs the %DFS algorithm from the root node |
1505 | 1505 |
/// in order to compute the %DFS path to each node. |
1506 | 1506 |
/// |
1507 | 1507 |
/// The algorithm computes |
1508 | 1508 |
/// - the %DFS tree, |
1509 | 1509 |
/// - the distance of each node from the root in the %DFS tree. |
1510 | 1510 |
/// |
1511 | 1511 |
/// \pre init() must be called and a root node should be |
1512 | 1512 |
/// added with addSource() before using this function. |
1513 | 1513 |
/// |
1514 | 1514 |
/// \note <tt>d.start()</tt> is just a shortcut of the following code. |
1515 | 1515 |
/// \code |
1516 | 1516 |
/// while ( !d.emptyQueue() ) { |
1517 | 1517 |
/// d.processNextArc(); |
1518 | 1518 |
/// } |
1519 | 1519 |
/// \endcode |
1520 | 1520 |
void start() { |
1521 | 1521 |
while ( !emptyQueue() ) processNextArc(); |
1522 | 1522 |
} |
1523 | 1523 |
|
1524 | 1524 |
/// \brief Executes the algorithm until the given target node is reached. |
1525 | 1525 |
/// |
1526 | 1526 |
/// Executes the algorithm until the given target node is reached. |
1527 | 1527 |
/// |
1528 | 1528 |
/// This method runs the %DFS algorithm from the root node |
1529 |
/// in order to compute the DFS path to \c |
|
1529 |
/// in order to compute the DFS path to \c t. |
|
1530 | 1530 |
/// |
1531 | 1531 |
/// The algorithm computes |
1532 |
/// - the %DFS path to \c dest, |
|
1533 |
/// - the distance of \c dest from the root in the %DFS tree. |
|
1532 |
/// - the %DFS path to \c t, |
|
1533 |
/// - the distance of \c t from the root in the %DFS tree. |
|
1534 | 1534 |
/// |
1535 | 1535 |
/// \pre init() must be called and a root node should be added |
1536 | 1536 |
/// with addSource() before using this function. |
1537 |
void start(Node dest) { |
|
1538 |
while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != dest ) |
|
1537 |
void start(Node t) { |
|
1538 |
while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != t ) |
|
1539 | 1539 |
processNextArc(); |
1540 | 1540 |
} |
1541 | 1541 |
|
1542 | 1542 |
/// \brief Executes the algorithm until a condition is met. |
1543 | 1543 |
/// |
1544 | 1544 |
/// Executes the algorithm until a condition is met. |
1545 | 1545 |
/// |
1546 | 1546 |
/// This method runs the %DFS algorithm from the root node |
1547 | 1547 |
/// until an arc \c a with <tt>am[a]</tt> true is found. |
1548 | 1548 |
/// |
1549 | 1549 |
/// \param am A \c bool (or convertible) arc map. The algorithm |
1550 | 1550 |
/// will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
1551 | 1551 |
/// |
1552 | 1552 |
/// \return The reached arc \c a with <tt>am[a]</tt> true or |
1553 | 1553 |
/// \c INVALID if no such arc was found. |
1554 | 1554 |
/// |
1555 | 1555 |
/// \pre init() must be called and a root node should be added |
1556 | 1556 |
/// with addSource() before using this function. |
1557 | 1557 |
/// |
1558 | 1558 |
/// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
1559 | 1559 |
/// not a node map. |
1560 | 1560 |
template <typename AM> |
1561 | 1561 |
Arc start(const AM &am) { |
1562 | 1562 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
1563 | 1563 |
processNextArc(); |
1564 | 1564 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
1565 | 1565 |
} |
1566 | 1566 |
|
1567 |
/// \brief Runs the algorithm from the given node. |
|
1567 |
/// \brief Runs the algorithm from the given source node. |
|
1568 | 1568 |
/// |
1569 | 1569 |
/// This method runs the %DFS algorithm from node \c s. |
1570 | 1570 |
/// in order to compute the DFS path to each node. |
1571 | 1571 |
/// |
1572 | 1572 |
/// The algorithm computes |
1573 | 1573 |
/// - the %DFS tree, |
1574 | 1574 |
/// - the distance of each node from the root in the %DFS tree. |
1575 | 1575 |
/// |
1576 | 1576 |
/// \note <tt>d.run(s)</tt> is just a shortcut of the following code. |
1577 | 1577 |
///\code |
1578 | 1578 |
/// d.init(); |
1579 | 1579 |
/// d.addSource(s); |
1580 | 1580 |
/// d.start(); |
1581 | 1581 |
///\endcode |
1582 | 1582 |
void run(Node s) { |
1583 | 1583 |
init(); |
1584 | 1584 |
addSource(s); |
1585 | 1585 |
start(); |
1586 | 1586 |
} |
1587 | 1587 |
|
1588 | 1588 |
/// \brief Finds the %DFS path between \c s and \c t. |
1589 | 1589 |
|
1590 | 1590 |
/// This method runs the %DFS algorithm from node \c s |
1591 |
/// in order to compute the DFS path to \c t |
|
1591 |
/// in order to compute the DFS path to node \c t |
|
1592 |
/// (it stops searching when \c t is processed). |
|
1592 | 1593 |
/// |
1593 |
/// \return The length of the <tt>s</tt>--<tt>t</tt> DFS path, |
|
1594 |
/// if \c t is reachable form \c s, \c 0 otherwise. |
|
1594 |
/// \return \c true if \c t is reachable form \c s. |
|
1595 | 1595 |
/// |
1596 | 1596 |
/// \note Apart from the return value, <tt>d.run(s,t)</tt> is |
1597 | 1597 |
/// just a shortcut of the following code. |
1598 | 1598 |
///\code |
1599 | 1599 |
/// d.init(); |
1600 | 1600 |
/// d.addSource(s); |
1601 | 1601 |
/// d.start(t); |
1602 | 1602 |
///\endcode |
1603 |
|
|
1603 |
bool run(Node s,Node t) { |
|
1604 | 1604 |
init(); |
1605 | 1605 |
addSource(s); |
1606 | 1606 |
start(t); |
1607 |
return reached(t) |
|
1607 |
return reached(t); |
|
1608 | 1608 |
} |
1609 | 1609 |
|
1610 | 1610 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1611 | 1611 |
|
1612 | 1612 |
/// This method runs the %DFS algorithm in order to |
1613 | 1613 |
/// compute the %DFS path to each node. |
1614 | 1614 |
/// |
1615 | 1615 |
/// The algorithm computes |
1616 | 1616 |
/// - the %DFS tree, |
1617 | 1617 |
/// - the distance of each node from the root in the %DFS tree. |
1618 | 1618 |
/// |
1619 | 1619 |
/// \note <tt>d.run()</tt> is just a shortcut of the following code. |
1620 | 1620 |
///\code |
1621 | 1621 |
/// d.init(); |
1622 | 1622 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
1623 | 1623 |
/// if (!d.reached(n)) { |
1624 | 1624 |
/// d.addSource(n); |
1625 | 1625 |
/// d.start(); |
1626 | 1626 |
/// } |
1627 | 1627 |
/// } |
1628 | 1628 |
///\endcode |
1629 | 1629 |
void run() { |
1630 | 1630 |
init(); |
1631 | 1631 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1632 | 1632 |
if (!reached(it)) { |
1633 | 1633 |
addSource(it); |
1634 | 1634 |
start(); |
1635 | 1635 |
} |
1636 | 1636 |
} |
1637 | 1637 |
} |
1638 | 1638 |
|
1639 | 1639 |
///@} |
1640 | 1640 |
|
1641 | 1641 |
/// \name Query Functions |
1642 | 1642 |
/// The result of the %DFS algorithm can be obtained using these |
1643 | 1643 |
/// functions.\n |
1644 | 1644 |
/// Either \ref lemon::DfsVisit::run() "run()" or |
1645 | 1645 |
/// \ref lemon::DfsVisit::start() "start()" must be called before |
1646 | 1646 |
/// using them. |
1647 | 1647 |
///@{ |
1648 | 1648 |
|
1649 | 1649 |
/// \brief Checks if a node is reachable from the root(s). |
1650 | 1650 |
/// |
1651 | 1651 |
/// Returns \c true if \c v is reachable from the root(s). |
1652 | 1652 |
/// \pre Either \ref run() or \ref start() |
1653 | 1653 |
/// must be called before using this function. |
1654 | 1654 |
bool reached(Node v) { return (*_reached)[v]; } |
1655 | 1655 |
|
1656 | 1656 |
///@} |
1657 | 1657 |
|
1658 | 1658 |
}; |
1659 | 1659 |
|
1660 | 1660 |
} //END OF NAMESPACE LEMON |
1661 | 1661 |
|
1662 | 1662 |
#endif |
... | ... |
@@ -475,703 +475,710 @@ |
475 | 475 |
|
476 | 476 |
///@} |
477 | 477 |
|
478 | 478 |
protected: |
479 | 479 |
|
480 | 480 |
Dijkstra() {} |
481 | 481 |
|
482 | 482 |
public: |
483 | 483 |
|
484 | 484 |
///Constructor. |
485 | 485 |
|
486 | 486 |
///Constructor. |
487 | 487 |
///\param _g The digraph the algorithm runs on. |
488 | 488 |
///\param _length The length map used by the algorithm. |
489 | 489 |
Dijkstra(const Digraph& _g, const LengthMap& _length) : |
490 | 490 |
G(&_g), length(&_length), |
491 | 491 |
_pred(NULL), local_pred(false), |
492 | 492 |
_dist(NULL), local_dist(false), |
493 | 493 |
_processed(NULL), local_processed(false), |
494 | 494 |
_heap_cross_ref(NULL), local_heap_cross_ref(false), |
495 | 495 |
_heap(NULL), local_heap(false) |
496 | 496 |
{ } |
497 | 497 |
|
498 | 498 |
///Destructor. |
499 | 499 |
~Dijkstra() |
500 | 500 |
{ |
501 | 501 |
if(local_pred) delete _pred; |
502 | 502 |
if(local_dist) delete _dist; |
503 | 503 |
if(local_processed) delete _processed; |
504 | 504 |
if(local_heap_cross_ref) delete _heap_cross_ref; |
505 | 505 |
if(local_heap) delete _heap; |
506 | 506 |
} |
507 | 507 |
|
508 | 508 |
///Sets the length map. |
509 | 509 |
|
510 | 510 |
///Sets the length map. |
511 | 511 |
///\return <tt> (*this) </tt> |
512 | 512 |
Dijkstra &lengthMap(const LengthMap &m) |
513 | 513 |
{ |
514 | 514 |
length = &m; |
515 | 515 |
return *this; |
516 | 516 |
} |
517 | 517 |
|
518 | 518 |
///Sets the map that stores the predecessor arcs. |
519 | 519 |
|
520 | 520 |
///Sets the map that stores the predecessor arcs. |
521 | 521 |
///If you don't use this function before calling \ref run(), |
522 | 522 |
///it will allocate one. The destructor deallocates this |
523 | 523 |
///automatically allocated map, of course. |
524 | 524 |
///\return <tt> (*this) </tt> |
525 | 525 |
Dijkstra &predMap(PredMap &m) |
526 | 526 |
{ |
527 | 527 |
if(local_pred) { |
528 | 528 |
delete _pred; |
529 | 529 |
local_pred=false; |
530 | 530 |
} |
531 | 531 |
_pred = &m; |
532 | 532 |
return *this; |
533 | 533 |
} |
534 | 534 |
|
535 | 535 |
///Sets the map that indicates which nodes are processed. |
536 | 536 |
|
537 | 537 |
///Sets the map that indicates which nodes are processed. |
538 | 538 |
///If you don't use this function before calling \ref run(), |
539 | 539 |
///it will allocate one. The destructor deallocates this |
540 | 540 |
///automatically allocated map, of course. |
541 | 541 |
///\return <tt> (*this) </tt> |
542 | 542 |
Dijkstra &processedMap(ProcessedMap &m) |
543 | 543 |
{ |
544 | 544 |
if(local_processed) { |
545 | 545 |
delete _processed; |
546 | 546 |
local_processed=false; |
547 | 547 |
} |
548 | 548 |
_processed = &m; |
549 | 549 |
return *this; |
550 | 550 |
} |
551 | 551 |
|
552 | 552 |
///Sets the map that stores the distances of the nodes. |
553 | 553 |
|
554 | 554 |
///Sets the map that stores the distances of the nodes calculated by the |
555 | 555 |
///algorithm. |
556 | 556 |
///If you don't use this function before calling \ref run(), |
557 | 557 |
///it will allocate one. The destructor deallocates this |
558 | 558 |
///automatically allocated map, of course. |
559 | 559 |
///\return <tt> (*this) </tt> |
560 | 560 |
Dijkstra &distMap(DistMap &m) |
561 | 561 |
{ |
562 | 562 |
if(local_dist) { |
563 | 563 |
delete _dist; |
564 | 564 |
local_dist=false; |
565 | 565 |
} |
566 | 566 |
_dist = &m; |
567 | 567 |
return *this; |
568 | 568 |
} |
569 | 569 |
|
570 | 570 |
///Sets the heap and the cross reference used by algorithm. |
571 | 571 |
|
572 | 572 |
///Sets the heap and the cross reference used by algorithm. |
573 | 573 |
///If you don't use this function before calling \ref run(), |
574 | 574 |
///it will allocate one. The destructor deallocates this |
575 | 575 |
///automatically allocated heap and cross reference, of course. |
576 | 576 |
///\return <tt> (*this) </tt> |
577 | 577 |
Dijkstra &heap(Heap& hp, HeapCrossRef &cr) |
578 | 578 |
{ |
579 | 579 |
if(local_heap_cross_ref) { |
580 | 580 |
delete _heap_cross_ref; |
581 | 581 |
local_heap_cross_ref=false; |
582 | 582 |
} |
583 | 583 |
_heap_cross_ref = &cr; |
584 | 584 |
if(local_heap) { |
585 | 585 |
delete _heap; |
586 | 586 |
local_heap=false; |
587 | 587 |
} |
588 | 588 |
_heap = &hp; |
589 | 589 |
return *this; |
590 | 590 |
} |
591 | 591 |
|
592 | 592 |
private: |
593 | 593 |
|
594 | 594 |
void finalizeNodeData(Node v,Value dst) |
595 | 595 |
{ |
596 | 596 |
_processed->set(v,true); |
597 | 597 |
_dist->set(v, dst); |
598 | 598 |
} |
599 | 599 |
|
600 | 600 |
public: |
601 | 601 |
|
602 | 602 |
///\name Execution control |
603 | 603 |
///The simplest way to execute the algorithm is to use one of the |
604 | 604 |
///member functions called \ref lemon::Dijkstra::run() "run()". |
605 | 605 |
///\n |
606 | 606 |
///If you need more control on the execution, first you must call |
607 | 607 |
///\ref lemon::Dijkstra::init() "init()", then you can add several |
608 | 608 |
///source nodes with \ref lemon::Dijkstra::addSource() "addSource()". |
609 | 609 |
///Finally \ref lemon::Dijkstra::start() "start()" will perform the |
610 | 610 |
///actual path computation. |
611 | 611 |
|
612 | 612 |
///@{ |
613 | 613 |
|
614 | 614 |
///Initializes the internal data structures. |
615 | 615 |
|
616 | 616 |
///Initializes the internal data structures. |
617 | 617 |
/// |
618 | 618 |
void init() |
619 | 619 |
{ |
620 | 620 |
create_maps(); |
621 | 621 |
_heap->clear(); |
622 | 622 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
623 | 623 |
_pred->set(u,INVALID); |
624 | 624 |
_processed->set(u,false); |
625 | 625 |
_heap_cross_ref->set(u,Heap::PRE_HEAP); |
626 | 626 |
} |
627 | 627 |
} |
628 | 628 |
|
629 | 629 |
///Adds a new source node. |
630 | 630 |
|
631 | 631 |
///Adds a new source node to the priority heap. |
632 | 632 |
///The optional second parameter is the initial distance of the node. |
633 | 633 |
/// |
634 | 634 |
///The function checks if the node has already been added to the heap and |
635 | 635 |
///it is pushed to the heap only if either it was not in the heap |
636 | 636 |
///or the shortest path found till then is shorter than \c dst. |
637 | 637 |
void addSource(Node s,Value dst=OperationTraits::zero()) |
638 | 638 |
{ |
639 | 639 |
if(_heap->state(s) != Heap::IN_HEAP) { |
640 | 640 |
_heap->push(s,dst); |
641 | 641 |
} else if(OperationTraits::less((*_heap)[s], dst)) { |
642 | 642 |
_heap->set(s,dst); |
643 | 643 |
_pred->set(s,INVALID); |
644 | 644 |
} |
645 | 645 |
} |
646 | 646 |
|
647 | 647 |
///Processes the next node in the priority heap |
648 | 648 |
|
649 | 649 |
///Processes the next node in the priority heap. |
650 | 650 |
/// |
651 | 651 |
///\return The processed node. |
652 | 652 |
/// |
653 | 653 |
///\warning The priority heap must not be empty. |
654 | 654 |
Node processNextNode() |
655 | 655 |
{ |
656 | 656 |
Node v=_heap->top(); |
657 | 657 |
Value oldvalue=_heap->prio(); |
658 | 658 |
_heap->pop(); |
659 | 659 |
finalizeNodeData(v,oldvalue); |
660 | 660 |
|
661 | 661 |
for(OutArcIt e(*G,v); e!=INVALID; ++e) { |
662 | 662 |
Node w=G->target(e); |
663 | 663 |
switch(_heap->state(w)) { |
664 | 664 |
case Heap::PRE_HEAP: |
665 | 665 |
_heap->push(w,OperationTraits::plus(oldvalue, (*length)[e])); |
666 | 666 |
_pred->set(w,e); |
667 | 667 |
break; |
668 | 668 |
case Heap::IN_HEAP: |
669 | 669 |
{ |
670 | 670 |
Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]); |
671 | 671 |
if ( OperationTraits::less(newvalue, (*_heap)[w]) ) { |
672 | 672 |
_heap->decrease(w, newvalue); |
673 | 673 |
_pred->set(w,e); |
674 | 674 |
} |
675 | 675 |
} |
676 | 676 |
break; |
677 | 677 |
case Heap::POST_HEAP: |
678 | 678 |
break; |
679 | 679 |
} |
680 | 680 |
} |
681 | 681 |
return v; |
682 | 682 |
} |
683 | 683 |
|
684 | 684 |
///The next node to be processed. |
685 | 685 |
|
686 | 686 |
///Returns the next node to be processed or \c INVALID if the |
687 | 687 |
///priority heap is empty. |
688 | 688 |
Node nextNode() const |
689 | 689 |
{ |
690 | 690 |
return !_heap->empty()?_heap->top():INVALID; |
691 | 691 |
} |
692 | 692 |
|
693 | 693 |
///\brief Returns \c false if there are nodes |
694 | 694 |
///to be processed. |
695 | 695 |
/// |
696 | 696 |
///Returns \c false if there are nodes |
697 | 697 |
///to be processed in the priority heap. |
698 | 698 |
bool emptyQueue() const { return _heap->empty(); } |
699 | 699 |
|
700 | 700 |
///Returns the number of the nodes to be processed in the priority heap |
701 | 701 |
|
702 | 702 |
///Returns the number of the nodes to be processed in the priority heap. |
703 | 703 |
/// |
704 | 704 |
int queueSize() const { return _heap->size(); } |
705 | 705 |
|
706 | 706 |
///Executes the algorithm. |
707 | 707 |
|
708 | 708 |
///Executes the algorithm. |
709 | 709 |
/// |
710 | 710 |
///This method runs the %Dijkstra algorithm from the root node(s) |
711 | 711 |
///in order to compute the shortest path to each node. |
712 | 712 |
/// |
713 | 713 |
///The algorithm computes |
714 | 714 |
///- the shortest path tree (forest), |
715 | 715 |
///- the distance of each node from the root(s). |
716 | 716 |
/// |
717 | 717 |
///\pre init() must be called and at least one root node should be |
718 | 718 |
///added with addSource() before using this function. |
719 | 719 |
/// |
720 | 720 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
721 | 721 |
///\code |
722 | 722 |
/// while ( !d.emptyQueue() ) { |
723 | 723 |
/// d.processNextNode(); |
724 | 724 |
/// } |
725 | 725 |
///\endcode |
726 | 726 |
void start() |
727 | 727 |
{ |
728 | 728 |
while ( !emptyQueue() ) processNextNode(); |
729 | 729 |
} |
730 | 730 |
|
731 |
///Executes the algorithm until the given target node is |
|
731 |
///Executes the algorithm until the given target node is processed. |
|
732 | 732 |
|
733 |
///Executes the algorithm until the given target node is |
|
733 |
///Executes the algorithm until the given target node is processed. |
|
734 | 734 |
/// |
735 | 735 |
///This method runs the %Dijkstra algorithm from the root node(s) |
736 |
///in order to compute the shortest path to \c |
|
736 |
///in order to compute the shortest path to \c t. |
|
737 | 737 |
/// |
738 | 738 |
///The algorithm computes |
739 |
///- the shortest path to \c dest, |
|
740 |
///- the distance of \c dest from the root(s). |
|
739 |
///- the shortest path to \c t, |
|
740 |
///- the distance of \c t from the root(s). |
|
741 | 741 |
/// |
742 | 742 |
///\pre init() must be called and at least one root node should be |
743 | 743 |
///added with addSource() before using this function. |
744 |
void start(Node |
|
744 |
void start(Node t) |
|
745 | 745 |
{ |
746 |
while ( !_heap->empty() && _heap->top()!=dest ) processNextNode(); |
|
747 |
if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio()); |
|
746 |
while ( !_heap->empty() && _heap->top()!=t ) processNextNode(); |
|
747 |
if ( !_heap->empty() ) { |
|
748 |
finalizeNodeData(_heap->top(),_heap->prio()); |
|
749 |
_heap->pop(); |
|
750 |
} |
|
748 | 751 |
} |
749 | 752 |
|
750 | 753 |
///Executes the algorithm until a condition is met. |
751 | 754 |
|
752 | 755 |
///Executes the algorithm until a condition is met. |
753 | 756 |
/// |
754 | 757 |
///This method runs the %Dijkstra algorithm from the root node(s) in |
755 | 758 |
///order to compute the shortest path to a node \c v with |
756 | 759 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
757 | 760 |
/// |
758 | 761 |
///\param nm A \c bool (or convertible) node map. The algorithm |
759 | 762 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
760 | 763 |
/// |
761 | 764 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
762 | 765 |
///\c INVALID if no such node was found. |
763 | 766 |
/// |
764 | 767 |
///\pre init() must be called and at least one root node should be |
765 | 768 |
///added with addSource() before using this function. |
766 | 769 |
template<class NodeBoolMap> |
767 | 770 |
Node start(const NodeBoolMap &nm) |
768 | 771 |
{ |
769 | 772 |
while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode(); |
770 | 773 |
if ( _heap->empty() ) return INVALID; |
771 | 774 |
finalizeNodeData(_heap->top(),_heap->prio()); |
772 | 775 |
return _heap->top(); |
773 | 776 |
} |
774 | 777 |
|
775 |
///Runs the algorithm from the given node. |
|
778 |
///Runs the algorithm from the given source node. |
|
776 | 779 |
|
777 | 780 |
///This method runs the %Dijkstra algorithm from node \c s |
778 | 781 |
///in order to compute the shortest path to each node. |
779 | 782 |
/// |
780 | 783 |
///The algorithm computes |
781 | 784 |
///- the shortest path tree, |
782 | 785 |
///- the distance of each node from the root. |
783 | 786 |
/// |
784 | 787 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
785 | 788 |
///\code |
786 | 789 |
/// d.init(); |
787 | 790 |
/// d.addSource(s); |
788 | 791 |
/// d.start(); |
789 | 792 |
///\endcode |
790 | 793 |
void run(Node s) { |
791 | 794 |
init(); |
792 | 795 |
addSource(s); |
793 | 796 |
start(); |
794 | 797 |
} |
795 | 798 |
|
796 | 799 |
///Finds the shortest path between \c s and \c t. |
797 | 800 |
|
798 | 801 |
///This method runs the %Dijkstra algorithm from node \c s |
799 |
///in order to compute the shortest path to \c t |
|
802 |
///in order to compute the shortest path to node \c t |
|
803 |
///(it stops searching when \c t is processed). |
|
800 | 804 |
/// |
801 |
///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path, |
|
802 |
///if \c t is reachable form \c s, \c 0 otherwise. |
|
805 |
///\return \c true if \c t is reachable form \c s. |
|
803 | 806 |
/// |
804 | 807 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is just a |
805 | 808 |
///shortcut of the following code. |
806 | 809 |
///\code |
807 | 810 |
/// d.init(); |
808 | 811 |
/// d.addSource(s); |
809 | 812 |
/// d.start(t); |
810 | 813 |
///\endcode |
811 |
|
|
814 |
bool run(Node s,Node t) { |
|
812 | 815 |
init(); |
813 | 816 |
addSource(s); |
814 | 817 |
start(t); |
815 |
return (* |
|
818 |
return (*_heap_cross_ref)[t] == Heap::POST_HEAP; |
|
816 | 819 |
} |
817 | 820 |
|
818 | 821 |
///@} |
819 | 822 |
|
820 | 823 |
///\name Query Functions |
821 | 824 |
///The result of the %Dijkstra algorithm can be obtained using these |
822 | 825 |
///functions.\n |
823 | 826 |
///Either \ref lemon::Dijkstra::run() "run()" or |
824 | 827 |
///\ref lemon::Dijkstra::start() "start()" must be called before |
825 | 828 |
///using them. |
826 | 829 |
|
827 | 830 |
///@{ |
828 | 831 |
|
829 | 832 |
///The shortest path to a node. |
830 | 833 |
|
831 | 834 |
///Returns the shortest path to a node. |
832 | 835 |
/// |
833 | 836 |
///\warning \c t should be reachable from the root(s). |
834 | 837 |
/// |
835 | 838 |
///\pre Either \ref run() or \ref start() must be called before |
836 | 839 |
///using this function. |
837 | 840 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
838 | 841 |
|
839 | 842 |
///The distance of a node from the root(s). |
840 | 843 |
|
841 | 844 |
///Returns the distance of a node from the root(s). |
842 | 845 |
/// |
843 | 846 |
///\warning If node \c v is not reachable from the root(s), then |
844 | 847 |
///the return value of this function is undefined. |
845 | 848 |
/// |
846 | 849 |
///\pre Either \ref run() or \ref start() must be called before |
847 | 850 |
///using this function. |
848 | 851 |
Value dist(Node v) const { return (*_dist)[v]; } |
849 | 852 |
|
850 | 853 |
///Returns the 'previous arc' of the shortest path tree for a node. |
851 | 854 |
|
852 | 855 |
///This function returns the 'previous arc' of the shortest path |
853 | 856 |
///tree for the node \c v, i.e. it returns the last arc of a |
854 | 857 |
///shortest path from the root(s) to \c v. It is \c INVALID if \c v |
855 | 858 |
///is not reachable from the root(s) or if \c v is a root. |
856 | 859 |
/// |
857 | 860 |
///The shortest path tree used here is equal to the shortest path |
858 | 861 |
///tree used in \ref predNode(). |
859 | 862 |
/// |
860 | 863 |
///\pre Either \ref run() or \ref start() must be called before |
861 | 864 |
///using this function. |
862 | 865 |
Arc predArc(Node v) const { return (*_pred)[v]; } |
863 | 866 |
|
864 | 867 |
///Returns the 'previous node' of the shortest path tree for a node. |
865 | 868 |
|
866 | 869 |
///This function returns the 'previous node' of the shortest path |
867 | 870 |
///tree for the node \c v, i.e. it returns the last but one node |
868 | 871 |
///from a shortest path from the root(s) to \c v. It is \c INVALID |
869 | 872 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
870 | 873 |
/// |
871 | 874 |
///The shortest path tree used here is equal to the shortest path |
872 | 875 |
///tree used in \ref predArc(). |
873 | 876 |
/// |
874 | 877 |
///\pre Either \ref run() or \ref start() must be called before |
875 | 878 |
///using this function. |
876 | 879 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
877 | 880 |
G->source((*_pred)[v]); } |
878 | 881 |
|
879 | 882 |
///\brief Returns a const reference to the node map that stores the |
880 | 883 |
///distances of the nodes. |
881 | 884 |
/// |
882 | 885 |
///Returns a const reference to the node map that stores the distances |
883 | 886 |
///of the nodes calculated by the algorithm. |
884 | 887 |
/// |
885 | 888 |
///\pre Either \ref run() or \ref init() |
886 | 889 |
///must be called before using this function. |
887 | 890 |
const DistMap &distMap() const { return *_dist;} |
888 | 891 |
|
889 | 892 |
///\brief Returns a const reference to the node map that stores the |
890 | 893 |
///predecessor arcs. |
891 | 894 |
/// |
892 | 895 |
///Returns a const reference to the node map that stores the predecessor |
893 | 896 |
///arcs, which form the shortest path tree. |
894 | 897 |
/// |
895 | 898 |
///\pre Either \ref run() or \ref init() |
896 | 899 |
///must be called before using this function. |
897 | 900 |
const PredMap &predMap() const { return *_pred;} |
898 | 901 |
|
899 | 902 |
///Checks if a node is reachable from the root(s). |
900 | 903 |
|
901 | 904 |
///Returns \c true if \c v is reachable from the root(s). |
902 | 905 |
///\pre Either \ref run() or \ref start() |
903 | 906 |
///must be called before using this function. |
904 | 907 |
bool reached(Node v) const { return (*_heap_cross_ref)[v] != |
905 | 908 |
Heap::PRE_HEAP; } |
906 | 909 |
|
907 | 910 |
///Checks if a node is processed. |
908 | 911 |
|
909 | 912 |
///Returns \c true if \c v is processed, i.e. the shortest |
910 | 913 |
///path to \c v has already found. |
911 |
///\pre Either \ref run() or \ref |
|
914 |
///\pre Either \ref run() or \ref init() |
|
912 | 915 |
///must be called before using this function. |
913 | 916 |
bool processed(Node v) const { return (*_heap_cross_ref)[v] == |
914 | 917 |
Heap::POST_HEAP; } |
915 | 918 |
|
916 | 919 |
///The current distance of a node from the root(s). |
917 | 920 |
|
918 | 921 |
///Returns the current distance of a node from the root(s). |
919 | 922 |
///It may be decreased in the following processes. |
920 |
///\pre \c v should be reached but not processed. |
|
921 |
Value currentDist(Node v) const { return (*_heap)[v]; } |
|
923 |
///\pre Either \ref run() or \ref init() |
|
924 |
///must be called before using this function and |
|
925 |
///node \c v must be reached but not necessarily processed. |
|
926 |
Value currentDist(Node v) const { |
|
927 |
return processed(v) ? (*_dist)[v] : (*_heap)[v]; |
|
928 |
} |
|
922 | 929 |
|
923 | 930 |
///@} |
924 | 931 |
}; |
925 | 932 |
|
926 | 933 |
|
927 | 934 |
///Default traits class of dijkstra() function. |
928 | 935 |
|
929 | 936 |
///Default traits class of dijkstra() function. |
930 | 937 |
///\tparam GR The type of the digraph. |
931 | 938 |
///\tparam LM The type of the length map. |
932 | 939 |
template<class GR, class LM> |
933 | 940 |
struct DijkstraWizardDefaultTraits |
934 | 941 |
{ |
935 | 942 |
///The type of the digraph the algorithm runs on. |
936 | 943 |
typedef GR Digraph; |
937 | 944 |
///The type of the map that stores the arc lengths. |
938 | 945 |
|
939 | 946 |
///The type of the map that stores the arc lengths. |
940 | 947 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
941 | 948 |
typedef LM LengthMap; |
942 | 949 |
///The type of the length of the arcs. |
943 | 950 |
typedef typename LM::Value Value; |
944 | 951 |
|
945 | 952 |
/// Operation traits for Dijkstra algorithm. |
946 | 953 |
|
947 | 954 |
/// This class defines the operations that are used in the algorithm. |
948 | 955 |
/// \see DijkstraDefaultOperationTraits |
949 | 956 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
950 | 957 |
|
951 | 958 |
/// The cross reference type used by the heap. |
952 | 959 |
|
953 | 960 |
/// The cross reference type used by the heap. |
954 | 961 |
/// Usually it is \c Digraph::NodeMap<int>. |
955 | 962 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
956 | 963 |
///Instantiates a \ref HeapCrossRef. |
957 | 964 |
|
958 | 965 |
///This function instantiates a \ref HeapCrossRef. |
959 | 966 |
/// \param g is the digraph, to which we would like to define the |
960 | 967 |
/// HeapCrossRef. |
961 | 968 |
/// \todo The digraph alone may be insufficient for the initialization |
962 | 969 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
963 | 970 |
{ |
964 | 971 |
return new HeapCrossRef(g); |
965 | 972 |
} |
966 | 973 |
|
967 | 974 |
///The heap type used by the Dijkstra algorithm. |
968 | 975 |
|
969 | 976 |
///The heap type used by the Dijkstra algorithm. |
970 | 977 |
/// |
971 | 978 |
///\sa BinHeap |
972 | 979 |
///\sa Dijkstra |
973 | 980 |
typedef BinHeap<Value, typename Digraph::template NodeMap<int>, |
974 | 981 |
std::less<Value> > Heap; |
975 | 982 |
|
976 | 983 |
///Instantiates a \ref Heap. |
977 | 984 |
|
978 | 985 |
///This function instantiates a \ref Heap. |
979 | 986 |
/// \param r is the HeapCrossRef which is used. |
980 | 987 |
static Heap *createHeap(HeapCrossRef& r) |
981 | 988 |
{ |
982 | 989 |
return new Heap(r); |
983 | 990 |
} |
984 | 991 |
|
985 | 992 |
///\brief The type of the map that stores the predecessor |
986 | 993 |
///arcs of the shortest paths. |
987 | 994 |
/// |
988 | 995 |
///The type of the map that stores the predecessor |
989 | 996 |
///arcs of the shortest paths. |
990 | 997 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
991 | 998 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
992 | 999 |
///Instantiates a \ref PredMap. |
993 | 1000 |
|
994 | 1001 |
///This function instantiates a \ref PredMap. |
995 | 1002 |
///\param g is the digraph, to which we would like to define the |
996 | 1003 |
///\ref PredMap. |
997 | 1004 |
///\todo The digraph alone may be insufficient to initialize |
998 | 1005 |
static PredMap *createPredMap(const Digraph &g) |
999 | 1006 |
{ |
1000 | 1007 |
return new PredMap(g); |
1001 | 1008 |
} |
1002 | 1009 |
|
1003 | 1010 |
///The type of the map that indicates which nodes are processed. |
1004 | 1011 |
|
1005 | 1012 |
///The type of the map that indicates which nodes are processed. |
1006 | 1013 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1007 | 1014 |
///By default it is a NullMap. |
1008 | 1015 |
///\todo If it is set to a real map, |
1009 | 1016 |
///Dijkstra::processed() should read this. |
1010 | 1017 |
///\todo named parameter to set this type, function to read and write. |
1011 | 1018 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
1012 | 1019 |
///Instantiates a \ref ProcessedMap. |
1013 | 1020 |
|
1014 | 1021 |
///This function instantiates a \ref ProcessedMap. |
1015 | 1022 |
///\param g is the digraph, to which |
1016 | 1023 |
///we would like to define the \ref ProcessedMap. |
1017 | 1024 |
#ifdef DOXYGEN |
1018 | 1025 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
1019 | 1026 |
#else |
1020 | 1027 |
static ProcessedMap *createProcessedMap(const Digraph &) |
1021 | 1028 |
#endif |
1022 | 1029 |
{ |
1023 | 1030 |
return new ProcessedMap(); |
1024 | 1031 |
} |
1025 | 1032 |
|
1026 | 1033 |
///The type of the map that stores the distances of the nodes. |
1027 | 1034 |
|
1028 | 1035 |
///The type of the map that stores the distances of the nodes. |
1029 | 1036 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1030 | 1037 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
1031 | 1038 |
///Instantiates a \ref DistMap. |
1032 | 1039 |
|
1033 | 1040 |
///This function instantiates a \ref DistMap. |
1034 | 1041 |
///\param g is the digraph, to which we would like to define |
1035 | 1042 |
///the \ref DistMap |
1036 | 1043 |
static DistMap *createDistMap(const Digraph &g) |
1037 | 1044 |
{ |
1038 | 1045 |
return new DistMap(g); |
1039 | 1046 |
} |
1040 | 1047 |
|
1041 | 1048 |
///The type of the shortest paths. |
1042 | 1049 |
|
1043 | 1050 |
///The type of the shortest paths. |
1044 | 1051 |
///It must meet the \ref concepts::Path "Path" concept. |
1045 | 1052 |
typedef lemon::Path<Digraph> Path; |
1046 | 1053 |
}; |
1047 | 1054 |
|
1048 | 1055 |
/// Default traits class used by \ref DijkstraWizard |
1049 | 1056 |
|
1050 | 1057 |
/// To make it easier to use Dijkstra algorithm |
1051 | 1058 |
/// we have created a wizard class. |
1052 | 1059 |
/// This \ref DijkstraWizard class needs default traits, |
1053 | 1060 |
/// as well as the \ref Dijkstra class. |
1054 | 1061 |
/// The \ref DijkstraWizardBase is a class to be the default traits of the |
1055 | 1062 |
/// \ref DijkstraWizard class. |
1056 | 1063 |
/// \todo More named parameters are required... |
1057 | 1064 |
template<class GR,class LM> |
1058 | 1065 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
1059 | 1066 |
{ |
1060 | 1067 |
typedef DijkstraWizardDefaultTraits<GR,LM> Base; |
1061 | 1068 |
protected: |
1062 | 1069 |
//The type of the nodes in the digraph. |
1063 | 1070 |
typedef typename Base::Digraph::Node Node; |
1064 | 1071 |
|
1065 | 1072 |
//Pointer to the digraph the algorithm runs on. |
1066 | 1073 |
void *_g; |
1067 | 1074 |
//Pointer to the length map. |
1068 | 1075 |
void *_length; |
1069 | 1076 |
//Pointer to the map of processed nodes. |
1070 | 1077 |
void *_processed; |
1071 | 1078 |
//Pointer to the map of predecessors arcs. |
1072 | 1079 |
void *_pred; |
1073 | 1080 |
//Pointer to the map of distances. |
1074 | 1081 |
void *_dist; |
1075 | 1082 |
//Pointer to the shortest path to the target node. |
1076 | 1083 |
void *_path; |
1077 | 1084 |
//Pointer to the distance of the target node. |
1078 | 1085 |
void *_di; |
1079 | 1086 |
|
1080 | 1087 |
public: |
1081 | 1088 |
/// Constructor. |
1082 | 1089 |
|
1083 | 1090 |
/// This constructor does not require parameters, therefore it initiates |
1084 | 1091 |
/// all of the attributes to \c 0. |
1085 | 1092 |
DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
1086 | 1093 |
_dist(0), _path(0), _di(0) {} |
1087 | 1094 |
|
1088 | 1095 |
/// Constructor. |
1089 | 1096 |
|
1090 | 1097 |
/// This constructor requires two parameters, |
1091 | 1098 |
/// others are initiated to \c 0. |
1092 | 1099 |
/// \param g The digraph the algorithm runs on. |
1093 | 1100 |
/// \param l The length map. |
1094 | 1101 |
DijkstraWizardBase(const GR &g,const LM &l) : |
1095 | 1102 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1096 | 1103 |
_length(reinterpret_cast<void*>(const_cast<LM*>(&l))), |
1097 | 1104 |
_processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
1098 | 1105 |
|
1099 | 1106 |
}; |
1100 | 1107 |
|
1101 | 1108 |
/// Auxiliary class for the function-type interface of Dijkstra algorithm. |
1102 | 1109 |
|
1103 | 1110 |
/// This auxiliary class is created to implement the |
1104 | 1111 |
/// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm. |
1105 | 1112 |
/// It does not have own \ref run() method, it uses the functions |
1106 | 1113 |
/// and features of the plain \ref Dijkstra. |
1107 | 1114 |
/// |
1108 | 1115 |
/// This class should only be used through the \ref dijkstra() function, |
1109 | 1116 |
/// which makes it easier to use the algorithm. |
1110 | 1117 |
template<class TR> |
1111 | 1118 |
class DijkstraWizard : public TR |
1112 | 1119 |
{ |
1113 | 1120 |
typedef TR Base; |
1114 | 1121 |
|
1115 | 1122 |
///The type of the digraph the algorithm runs on. |
1116 | 1123 |
typedef typename TR::Digraph Digraph; |
1117 | 1124 |
|
1118 | 1125 |
typedef typename Digraph::Node Node; |
1119 | 1126 |
typedef typename Digraph::NodeIt NodeIt; |
1120 | 1127 |
typedef typename Digraph::Arc Arc; |
1121 | 1128 |
typedef typename Digraph::OutArcIt OutArcIt; |
1122 | 1129 |
|
1123 | 1130 |
///The type of the map that stores the arc lengths. |
1124 | 1131 |
typedef typename TR::LengthMap LengthMap; |
1125 | 1132 |
///The type of the length of the arcs. |
1126 | 1133 |
typedef typename LengthMap::Value Value; |
1127 | 1134 |
///\brief The type of the map that stores the predecessor |
1128 | 1135 |
///arcs of the shortest paths. |
1129 | 1136 |
typedef typename TR::PredMap PredMap; |
1130 | 1137 |
///The type of the map that stores the distances of the nodes. |
1131 | 1138 |
typedef typename TR::DistMap DistMap; |
1132 | 1139 |
///The type of the map that indicates which nodes are processed. |
1133 | 1140 |
typedef typename TR::ProcessedMap ProcessedMap; |
1134 | 1141 |
///The type of the shortest paths |
1135 | 1142 |
typedef typename TR::Path Path; |
1136 | 1143 |
///The heap type used by the dijkstra algorithm. |
1137 | 1144 |
typedef typename TR::Heap Heap; |
1138 | 1145 |
|
1139 | 1146 |
public: |
1140 | 1147 |
|
1141 | 1148 |
/// Constructor. |
1142 | 1149 |
DijkstraWizard() : TR() {} |
1143 | 1150 |
|
1144 | 1151 |
/// Constructor that requires parameters. |
1145 | 1152 |
|
1146 | 1153 |
/// Constructor that requires parameters. |
1147 | 1154 |
/// These parameters will be the default values for the traits class. |
1148 | 1155 |
/// \param g The digraph the algorithm runs on. |
1149 | 1156 |
/// \param l The length map. |
1150 | 1157 |
DijkstraWizard(const Digraph &g, const LengthMap &l) : |
1151 | 1158 |
TR(g,l) {} |
1152 | 1159 |
|
1153 | 1160 |
///Copy constructor |
1154 | 1161 |
DijkstraWizard(const TR &b) : TR(b) {} |
1155 | 1162 |
|
1156 | 1163 |
~DijkstraWizard() {} |
1157 | 1164 |
|
1158 | 1165 |
///Runs Dijkstra algorithm from the given source node. |
1159 | 1166 |
|
1160 | 1167 |
///This method runs %Dijkstra algorithm from the given source node |
1161 | 1168 |
///in order to compute the shortest path to each node. |
1162 | 1169 |
void run(Node s) |
1163 | 1170 |
{ |
1164 | 1171 |
if (s==INVALID) throw UninitializedParameter(); |
1165 | 1172 |
Dijkstra<Digraph,LengthMap,TR> |
1166 | 1173 |
dijk(*reinterpret_cast<const Digraph*>(Base::_g), |
1167 | 1174 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
1168 | 1175 |
if (Base::_pred) |
1169 | 1176 |
dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1170 | 1177 |
if (Base::_dist) |
1171 | 1178 |
dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1172 | 1179 |
if (Base::_processed) |
1173 | 1180 |
dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1174 | 1181 |
dijk.run(s); |
1175 | 1182 |
} |
1176 | 1183 |
|
1177 | 1184 |
///Finds the shortest path between \c s and \c t. |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/concepts/digraph.h> |
20 | 20 |
#include <lemon/smart_graph.h> |
21 | 21 |
#include <lemon/list_graph.h> |
22 | 22 |
#include <lemon/lgf_reader.h> |
23 | 23 |
#include <lemon/bfs.h> |
24 | 24 |
#include <lemon/path.h> |
25 | 25 |
|
26 | 26 |
#include "graph_test.h" |
27 | 27 |
#include "test_tools.h" |
28 | 28 |
|
29 | 29 |
using namespace lemon; |
30 | 30 |
|
31 | 31 |
char test_lgf[] = |
32 | 32 |
"@nodes\n" |
33 | 33 |
"label\n" |
34 | 34 |
"0\n" |
35 | 35 |
"1\n" |
36 | 36 |
"2\n" |
37 | 37 |
"3\n" |
38 | 38 |
"4\n" |
39 | 39 |
"5\n" |
40 | 40 |
"@arcs\n" |
41 | 41 |
" label\n" |
42 | 42 |
"0 1 0\n" |
43 | 43 |
"1 2 1\n" |
44 | 44 |
"2 3 2\n" |
45 | 45 |
"3 4 3\n" |
46 | 46 |
"0 3 4\n" |
47 | 47 |
"0 3 5\n" |
48 | 48 |
"5 2 6\n" |
49 | 49 |
"@attributes\n" |
50 | 50 |
"source 0\n" |
51 | 51 |
"target 4\n"; |
52 | 52 |
|
53 | 53 |
void checkBfsCompile() |
54 | 54 |
{ |
55 | 55 |
typedef concepts::Digraph Digraph; |
56 | 56 |
typedef Bfs<Digraph> BType; |
57 |
typedef Digraph::Node Node; |
|
58 |
typedef Digraph::Arc Arc; |
|
57 | 59 |
|
58 | 60 |
Digraph G; |
59 |
Digraph::Node n; |
|
60 |
Digraph::Arc e; |
|
61 |
Node s, t; |
|
62 |
Arc e; |
|
61 | 63 |
int l; |
62 | 64 |
bool b; |
63 | 65 |
BType::DistMap d(G); |
64 | 66 |
BType::PredMap p(G); |
67 |
Path<Digraph> pp; |
|
65 | 68 |
|
66 |
|
|
69 |
{ |
|
70 |
BType bfs_test(G); |
|
67 | 71 |
|
68 |
bfs_test.run( |
|
72 |
bfs_test.run(s); |
|
73 |
bfs_test.run(s,t); |
|
74 |
bfs_test.run(); |
|
69 | 75 |
|
70 |
l = bfs_test.dist(n); |
|
71 |
e = bfs_test.predArc(n); |
|
72 |
n = bfs_test.predNode(n); |
|
73 |
d = bfs_test.distMap(); |
|
74 |
p = bfs_test.predMap(); |
|
75 |
b = bfs_test.reached(n); |
|
76 |
l = bfs_test.dist(t); |
|
77 |
e = bfs_test.predArc(t); |
|
78 |
s = bfs_test.predNode(t); |
|
79 |
b = bfs_test.reached(t); |
|
80 |
d = bfs_test.distMap(); |
|
81 |
p = bfs_test.predMap(); |
|
82 |
pp = bfs_test.path(t); |
|
83 |
} |
|
84 |
{ |
|
85 |
BType |
|
86 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
|
87 |
::SetDistMap<concepts::ReadWriteMap<Node,int> > |
|
88 |
::SetReachedMap<concepts::ReadWriteMap<Node,bool> > |
|
89 |
::SetProcessedMap<concepts::WriteMap<Node,bool> > |
|
90 |
::SetStandardProcessedMap |
|
91 |
::Create bfs_test(G); |
|
76 | 92 |
|
77 |
|
|
93 |
bfs_test.run(s); |
|
94 |
bfs_test.run(s,t); |
|
95 |
bfs_test.run(); |
|
96 |
|
|
97 |
l = bfs_test.dist(t); |
|
98 |
e = bfs_test.predArc(t); |
|
99 |
s = bfs_test.predNode(t); |
|
100 |
b = bfs_test.reached(t); |
|
101 |
pp = bfs_test.path(t); |
|
102 |
} |
|
78 | 103 |
} |
79 | 104 |
|
80 | 105 |
void checkBfsFunctionCompile() |
81 | 106 |
{ |
82 | 107 |
typedef int VType; |
83 | 108 |
typedef concepts::Digraph Digraph; |
84 | 109 |
typedef Digraph::Arc Arc; |
85 | 110 |
typedef Digraph::Node Node; |
86 | 111 |
|
87 | 112 |
Digraph g; |
88 | 113 |
bool b; |
89 | 114 |
bfs(g).run(Node()); |
90 | 115 |
b=bfs(g).run(Node(),Node()); |
91 | 116 |
bfs(g).run(); |
92 | 117 |
bfs(g) |
93 | 118 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
94 | 119 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
95 | 120 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
96 | 121 |
.processedMap(concepts::WriteMap<Node,bool>()) |
97 | 122 |
.run(Node()); |
98 | 123 |
b=bfs(g) |
99 | 124 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
100 | 125 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
101 | 126 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
102 | 127 |
.processedMap(concepts::WriteMap<Node,bool>()) |
103 | 128 |
.path(concepts::Path<Digraph>()) |
104 | 129 |
.dist(VType()) |
105 | 130 |
.run(Node(),Node()); |
106 | 131 |
bfs(g) |
107 | 132 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
108 | 133 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
109 | 134 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
110 | 135 |
.processedMap(concepts::WriteMap<Node,bool>()) |
111 | 136 |
.run(); |
112 | 137 |
} |
113 | 138 |
|
114 | 139 |
template <class Digraph> |
115 | 140 |
void checkBfs() { |
116 | 141 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
117 | 142 |
|
118 | 143 |
Digraph G; |
119 | 144 |
Node s, t; |
120 | 145 |
|
121 | 146 |
std::istringstream input(test_lgf); |
122 | 147 |
digraphReader(input, G). |
123 | 148 |
node("source", s). |
124 | 149 |
node("target", t). |
125 | 150 |
run(); |
126 | 151 |
|
127 | 152 |
Bfs<Digraph> bfs_test(G); |
128 | 153 |
bfs_test.run(s); |
129 | 154 |
|
130 | 155 |
check(bfs_test.dist(t)==2,"Bfs found a wrong path."); |
131 | 156 |
|
132 | 157 |
Path<Digraph> p = bfs_test.path(t); |
133 | 158 |
check(p.length()==2,"path() found a wrong path."); |
134 | 159 |
check(checkPath(G, p),"path() found a wrong path."); |
135 | 160 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
136 | 161 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
137 | 162 |
|
138 | 163 |
|
139 | 164 |
for(ArcIt a(G); a!=INVALID; ++a) { |
140 | 165 |
Node u=G.source(a); |
141 | 166 |
Node v=G.target(a); |
142 | 167 |
check( !bfs_test.reached(u) || |
143 | 168 |
(bfs_test.dist(v) <= bfs_test.dist(u)+1), |
144 | 169 |
"Wrong output. " << G.id(u) << "->" << G.id(v)); |
145 | 170 |
} |
146 | 171 |
|
147 | 172 |
for(NodeIt v(G); v!=INVALID; ++v) { |
148 | 173 |
if (bfs_test.reached(v)) { |
149 | 174 |
check(v==s || bfs_test.predArc(v)!=INVALID, "Wrong tree."); |
150 | 175 |
if (bfs_test.predArc(v)!=INVALID ) { |
151 | 176 |
Arc a=bfs_test.predArc(v); |
152 | 177 |
Node u=G.source(a); |
153 | 178 |
check(u==bfs_test.predNode(v),"Wrong tree."); |
154 | 179 |
check(bfs_test.dist(v) - bfs_test.dist(u) == 1, |
155 | 180 |
"Wrong distance. Difference: " |
156 | 181 |
<< std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1)); |
157 | 182 |
} |
158 | 183 |
} |
159 | 184 |
} |
160 | 185 |
|
161 | 186 |
{ |
162 | 187 |
NullMap<Node,Arc> myPredMap; |
163 | 188 |
bfs(G).predMap(myPredMap).run(s); |
164 | 189 |
} |
165 | 190 |
} |
166 | 191 |
|
167 | 192 |
int main() |
168 | 193 |
{ |
169 | 194 |
checkBfs<ListDigraph>(); |
170 | 195 |
checkBfs<SmartDigraph>(); |
171 | 196 |
return 0; |
172 | 197 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/concepts/digraph.h> |
20 | 20 |
#include <lemon/smart_graph.h> |
21 | 21 |
#include <lemon/list_graph.h> |
22 | 22 |
#include <lemon/lgf_reader.h> |
23 | 23 |
#include <lemon/dfs.h> |
24 | 24 |
#include <lemon/path.h> |
25 | 25 |
|
26 | 26 |
#include "graph_test.h" |
27 | 27 |
#include "test_tools.h" |
28 | 28 |
|
29 | 29 |
using namespace lemon; |
30 | 30 |
|
31 | 31 |
char test_lgf[] = |
32 | 32 |
"@nodes\n" |
33 | 33 |
"label\n" |
34 | 34 |
"0\n" |
35 | 35 |
"1\n" |
36 | 36 |
"2\n" |
37 | 37 |
"3\n" |
38 | 38 |
"4\n" |
39 | 39 |
"5\n" |
40 | 40 |
"6\n" |
41 | 41 |
"@arcs\n" |
42 | 42 |
" label\n" |
43 | 43 |
"0 1 0\n" |
44 | 44 |
"1 2 1\n" |
45 | 45 |
"2 3 2\n" |
46 | 46 |
"1 4 3\n" |
47 | 47 |
"4 2 4\n" |
48 | 48 |
"4 5 5\n" |
49 | 49 |
"5 0 6\n" |
50 | 50 |
"6 3 7\n" |
51 | 51 |
"@attributes\n" |
52 | 52 |
"source 0\n" |
53 | 53 |
"target 5\n"; |
54 | 54 |
|
55 | 55 |
void checkDfsCompile() |
56 | 56 |
{ |
57 | 57 |
typedef concepts::Digraph Digraph; |
58 | 58 |
typedef Dfs<Digraph> DType; |
59 |
typedef Digraph::Node Node; |
|
60 |
typedef Digraph::Arc Arc; |
|
59 | 61 |
|
60 | 62 |
Digraph G; |
61 |
Digraph::Node n; |
|
62 |
Digraph::Arc e; |
|
63 |
Node s, t; |
|
64 |
Arc e; |
|
63 | 65 |
int l; |
64 | 66 |
bool b; |
65 | 67 |
DType::DistMap d(G); |
66 | 68 |
DType::PredMap p(G); |
69 |
Path<Digraph> pp; |
|
67 | 70 |
|
68 |
|
|
71 |
{ |
|
72 |
DType dfs_test(G); |
|
69 | 73 |
|
70 |
dfs_test.run( |
|
74 |
dfs_test.run(s); |
|
75 |
dfs_test.run(s,t); |
|
76 |
dfs_test.run(); |
|
71 | 77 |
|
72 |
l = dfs_test.dist(n); |
|
73 |
e = dfs_test.predArc(n); |
|
74 |
n = dfs_test.predNode(n); |
|
75 |
d = dfs_test.distMap(); |
|
76 |
p = dfs_test.predMap(); |
|
77 |
b = dfs_test.reached(n); |
|
78 |
l = dfs_test.dist(t); |
|
79 |
e = dfs_test.predArc(t); |
|
80 |
s = dfs_test.predNode(t); |
|
81 |
b = dfs_test.reached(t); |
|
82 |
d = dfs_test.distMap(); |
|
83 |
p = dfs_test.predMap(); |
|
84 |
pp = dfs_test.path(t); |
|
85 |
} |
|
86 |
{ |
|
87 |
DType |
|
88 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
|
89 |
::SetDistMap<concepts::ReadWriteMap<Node,int> > |
|
90 |
::SetReachedMap<concepts::ReadWriteMap<Node,bool> > |
|
91 |
::SetProcessedMap<concepts::WriteMap<Node,bool> > |
|
92 |
::SetStandardProcessedMap |
|
93 |
::Create dfs_test(G); |
|
78 | 94 |
|
79 |
|
|
95 |
dfs_test.run(s); |
|
96 |
dfs_test.run(s,t); |
|
97 |
dfs_test.run(); |
|
98 |
|
|
99 |
l = dfs_test.dist(t); |
|
100 |
e = dfs_test.predArc(t); |
|
101 |
s = dfs_test.predNode(t); |
|
102 |
b = dfs_test.reached(t); |
|
103 |
pp = dfs_test.path(t); |
|
104 |
} |
|
80 | 105 |
} |
81 | 106 |
|
82 | 107 |
void checkDfsFunctionCompile() |
83 | 108 |
{ |
84 | 109 |
typedef int VType; |
85 | 110 |
typedef concepts::Digraph Digraph; |
86 | 111 |
typedef Digraph::Arc Arc; |
87 | 112 |
typedef Digraph::Node Node; |
88 | 113 |
|
89 | 114 |
Digraph g; |
90 | 115 |
bool b; |
91 | 116 |
dfs(g).run(Node()); |
92 | 117 |
b=dfs(g).run(Node(),Node()); |
93 | 118 |
dfs(g).run(); |
94 | 119 |
dfs(g) |
95 | 120 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
96 | 121 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
97 | 122 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
98 | 123 |
.processedMap(concepts::WriteMap<Node,bool>()) |
99 | 124 |
.run(Node()); |
100 | 125 |
b=dfs(g) |
101 | 126 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
102 | 127 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
103 | 128 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
104 | 129 |
.processedMap(concepts::WriteMap<Node,bool>()) |
105 | 130 |
.path(concepts::Path<Digraph>()) |
106 | 131 |
.dist(VType()) |
107 | 132 |
.run(Node(),Node()); |
108 | 133 |
dfs(g) |
109 | 134 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
110 | 135 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
111 | 136 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
112 | 137 |
.processedMap(concepts::WriteMap<Node,bool>()) |
113 | 138 |
.run(); |
114 | 139 |
} |
115 | 140 |
|
116 | 141 |
template <class Digraph> |
117 | 142 |
void checkDfs() { |
118 | 143 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
119 | 144 |
|
120 | 145 |
Digraph G; |
121 | 146 |
Node s, t; |
122 | 147 |
|
123 | 148 |
std::istringstream input(test_lgf); |
124 | 149 |
digraphReader(input, G). |
125 | 150 |
node("source", s). |
126 | 151 |
node("target", t). |
127 | 152 |
run(); |
128 | 153 |
|
129 | 154 |
Dfs<Digraph> dfs_test(G); |
130 | 155 |
dfs_test.run(s); |
131 | 156 |
|
132 | 157 |
Path<Digraph> p = dfs_test.path(t); |
133 | 158 |
check(p.length() == dfs_test.dist(t),"path() found a wrong path."); |
134 | 159 |
check(checkPath(G, p),"path() found a wrong path."); |
135 | 160 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
136 | 161 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
137 | 162 |
|
138 | 163 |
for(NodeIt v(G); v!=INVALID; ++v) { |
139 | 164 |
if (dfs_test.reached(v)) { |
140 | 165 |
check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree."); |
141 | 166 |
if (dfs_test.predArc(v)!=INVALID ) { |
142 | 167 |
Arc e=dfs_test.predArc(v); |
143 | 168 |
Node u=G.source(e); |
144 | 169 |
check(u==dfs_test.predNode(v),"Wrong tree."); |
145 | 170 |
check(dfs_test.dist(v) - dfs_test.dist(u) == 1, |
146 | 171 |
"Wrong distance. (" << dfs_test.dist(u) << "->" |
147 | 172 |
<< dfs_test.dist(v) << ")"); |
148 | 173 |
} |
149 | 174 |
} |
150 | 175 |
} |
151 | 176 |
|
152 | 177 |
{ |
153 | 178 |
NullMap<Node,Arc> myPredMap; |
154 | 179 |
dfs(G).predMap(myPredMap).run(s); |
155 | 180 |
} |
156 | 181 |
} |
157 | 182 |
|
158 | 183 |
int main() |
159 | 184 |
{ |
160 | 185 |
checkDfs<ListDigraph>(); |
161 | 186 |
checkDfs<SmartDigraph>(); |
162 | 187 |
return 0; |
163 | 188 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/concepts/digraph.h> |
20 | 20 |
#include <lemon/smart_graph.h> |
21 | 21 |
#include <lemon/list_graph.h> |
22 | 22 |
#include <lemon/lgf_reader.h> |
23 | 23 |
#include <lemon/dijkstra.h> |
24 | 24 |
#include <lemon/path.h> |
25 |
#include <lemon/bin_heap.h> |
|
25 | 26 |
|
26 | 27 |
#include "graph_test.h" |
27 | 28 |
#include "test_tools.h" |
28 | 29 |
|
29 | 30 |
using namespace lemon; |
30 | 31 |
|
31 | 32 |
char test_lgf[] = |
32 | 33 |
"@nodes\n" |
33 | 34 |
"label\n" |
34 | 35 |
"0\n" |
35 | 36 |
"1\n" |
36 | 37 |
"2\n" |
37 | 38 |
"3\n" |
38 | 39 |
"4\n" |
39 | 40 |
"@arcs\n" |
40 | 41 |
" label length\n" |
41 | 42 |
"0 1 0 1\n" |
42 | 43 |
"1 2 1 1\n" |
43 | 44 |
"2 3 2 1\n" |
44 | 45 |
"0 3 4 5\n" |
45 | 46 |
"0 3 5 10\n" |
46 | 47 |
"0 3 6 7\n" |
47 | 48 |
"4 2 7 1\n" |
48 | 49 |
"@attributes\n" |
49 | 50 |
"source 0\n" |
50 | 51 |
"target 3\n"; |
51 | 52 |
|
52 | 53 |
void checkDijkstraCompile() |
53 | 54 |
{ |
54 | 55 |
typedef int VType; |
55 | 56 |
typedef concepts::Digraph Digraph; |
56 | 57 |
typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap; |
57 | 58 |
typedef Dijkstra<Digraph, LengthMap> DType; |
59 |
typedef Digraph::Node Node; |
|
60 |
typedef Digraph::Arc Arc; |
|
58 | 61 |
|
59 | 62 |
Digraph G; |
60 |
Digraph::Node n; |
|
61 |
Digraph::Arc e; |
|
63 |
Node s, t; |
|
64 |
Arc e; |
|
62 | 65 |
VType l; |
63 | 66 |
bool b; |
64 | 67 |
DType::DistMap d(G); |
65 | 68 |
DType::PredMap p(G); |
66 | 69 |
LengthMap length; |
70 |
Path<Digraph> pp; |
|
67 | 71 |
|
68 |
|
|
72 |
{ |
|
73 |
DType dijkstra_test(G,length); |
|
69 | 74 |
|
70 |
dijkstra_test.run( |
|
75 |
dijkstra_test.run(s); |
|
76 |
dijkstra_test.run(s,t); |
|
71 | 77 |
|
72 |
l = dijkstra_test.dist(n); |
|
73 |
e = dijkstra_test.predArc(n); |
|
74 |
n = dijkstra_test.predNode(n); |
|
75 |
d = dijkstra_test.distMap(); |
|
76 |
p = dijkstra_test.predMap(); |
|
77 |
b = dijkstra_test.reached(n); |
|
78 |
l = dijkstra_test.dist(t); |
|
79 |
e = dijkstra_test.predArc(t); |
|
80 |
s = dijkstra_test.predNode(t); |
|
81 |
b = dijkstra_test.reached(t); |
|
82 |
d = dijkstra_test.distMap(); |
|
83 |
p = dijkstra_test.predMap(); |
|
84 |
pp = dijkstra_test.path(t); |
|
85 |
} |
|
86 |
{ |
|
87 |
DType |
|
88 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
|
89 |
::SetDistMap<concepts::ReadWriteMap<Node,VType> > |
|
90 |
::SetProcessedMap<concepts::WriteMap<Node,bool> > |
|
91 |
::SetStandardProcessedMap |
|
92 |
::SetOperationTraits<DijkstraWidestPathOperationTraits<VType> > |
|
93 |
::SetHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > > |
|
94 |
::SetStandardHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > > |
|
95 |
::Create dijkstra_test(G,length); |
|
78 | 96 |
|
79 |
|
|
97 |
dijkstra_test.run(s); |
|
98 |
dijkstra_test.run(s,t); |
|
99 |
|
|
100 |
l = dijkstra_test.dist(t); |
|
101 |
e = dijkstra_test.predArc(t); |
|
102 |
s = dijkstra_test.predNode(t); |
|
103 |
b = dijkstra_test.reached(t); |
|
104 |
pp = dijkstra_test.path(t); |
|
105 |
} |
|
106 |
|
|
80 | 107 |
} |
81 | 108 |
|
82 | 109 |
void checkDijkstraFunctionCompile() |
83 | 110 |
{ |
84 | 111 |
typedef int VType; |
85 | 112 |
typedef concepts::Digraph Digraph; |
86 | 113 |
typedef Digraph::Arc Arc; |
87 | 114 |
typedef Digraph::Node Node; |
88 | 115 |
typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap; |
89 | 116 |
|
90 | 117 |
Digraph g; |
91 | 118 |
bool b; |
92 | 119 |
dijkstra(g,LengthMap()).run(Node()); |
93 | 120 |
b=dijkstra(g,LengthMap()).run(Node(),Node()); |
94 | 121 |
dijkstra(g,LengthMap()) |
95 | 122 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
96 | 123 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
97 | 124 |
.processedMap(concepts::WriteMap<Node,bool>()) |
98 | 125 |
.run(Node()); |
99 | 126 |
b=dijkstra(g,LengthMap()) |
100 | 127 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
101 | 128 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
102 | 129 |
.processedMap(concepts::WriteMap<Node,bool>()) |
103 | 130 |
.path(concepts::Path<Digraph>()) |
104 | 131 |
.dist(VType()) |
105 | 132 |
.run(Node(),Node()); |
106 | 133 |
} |
107 | 134 |
|
108 | 135 |
template <class Digraph> |
109 | 136 |
void checkDijkstra() { |
110 | 137 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
111 | 138 |
typedef typename Digraph::template ArcMap<int> LengthMap; |
112 | 139 |
|
113 | 140 |
Digraph G; |
114 | 141 |
Node s, t; |
115 | 142 |
LengthMap length(G); |
116 | 143 |
|
117 | 144 |
std::istringstream input(test_lgf); |
118 | 145 |
digraphReader(input, G). |
119 | 146 |
arcMap("length", length). |
120 | 147 |
node("source", s). |
121 | 148 |
node("target", t). |
122 | 149 |
run(); |
123 | 150 |
|
124 | 151 |
Dijkstra<Digraph, LengthMap> |
125 | 152 |
dijkstra_test(G, length); |
126 | 153 |
dijkstra_test.run(s); |
127 | 154 |
|
128 | 155 |
check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path."); |
129 | 156 |
|
130 | 157 |
Path<Digraph> p = dijkstra_test.path(t); |
131 | 158 |
check(p.length()==3,"path() found a wrong path."); |
132 | 159 |
check(checkPath(G, p),"path() found a wrong path."); |
133 | 160 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
134 | 161 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
135 | 162 |
|
136 | 163 |
for(ArcIt e(G); e!=INVALID; ++e) { |
137 | 164 |
Node u=G.source(e); |
138 | 165 |
Node v=G.target(e); |
139 | 166 |
check( !dijkstra_test.reached(u) || |
140 | 167 |
(dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]), |
141 | 168 |
"Wrong output. dist(target)-dist(source)-arc_length=" << |
142 | 169 |
dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]); |
143 | 170 |
} |
144 | 171 |
|
145 | 172 |
for(NodeIt v(G); v!=INVALID; ++v) { |
146 | 173 |
if (dijkstra_test.reached(v)) { |
147 | 174 |
check(v==s || dijkstra_test.predArc(v)!=INVALID, "Wrong tree."); |
148 | 175 |
if (dijkstra_test.predArc(v)!=INVALID ) { |
149 | 176 |
Arc e=dijkstra_test.predArc(v); |
150 | 177 |
Node u=G.source(e); |
151 | 178 |
check(u==dijkstra_test.predNode(v),"Wrong tree."); |
152 | 179 |
check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e], |
153 | 180 |
"Wrong distance! Difference: " << |
154 | 181 |
std::abs(dijkstra_test.dist(v)-dijkstra_test.dist(u)-length[e])); |
155 | 182 |
} |
156 | 183 |
} |
157 | 184 |
} |
158 | 185 |
|
159 | 186 |
{ |
160 | 187 |
NullMap<Node,Arc> myPredMap; |
161 | 188 |
dijkstra(G,length).predMap(myPredMap).run(s); |
162 | 189 |
} |
163 | 190 |
} |
164 | 191 |
|
165 | 192 |
int main() { |
166 | 193 |
checkDijkstra<ListDigraph>(); |
167 | 194 |
checkDijkstra<SmartDigraph>(); |
168 | 195 |
return 0; |
169 | 196 |
} |
0 comments (0 inline)