gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Improvements related to BFS/DFS/Dijkstra (ticket #96) - Add run(s,t) function to BfsVisit. - Modify run(s,t) functions in the class interfaces to return bool value. - Bug fix in Dijkstra::start(t) function. - Improve Dijkstra::currentDist(). - Extend test files to check named class template parameters. - Doc improvements.
0 6 0
default
6 files changed with 195 insertions and 89 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
... ...
@@ -562,194 +562,194 @@
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 dest.
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 dest)
626
    void start(Node t)
627 627
    {
628 628
      bool reach = false;
629
      while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
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
    int run(Node s,Node t) {
703
    bool run(Node s,Node t) {
704 704
      init();
705 705
      addSource(s);
706 706
      start(t);
707
      return reached(t) ? _curr_dist : 0;
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
... ...
@@ -1576,173 +1576,195 @@
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 dest.
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 dest) {
1640
    void start(Node t) {
1641 1641
      bool reach = false;
1642
      while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
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

	
Ignore white space 6 line context
... ...
@@ -513,177 +513,177 @@
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 dest.
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 dest)
569
    void start(Node t)
570 570
    {
571
      while ( !emptyQueue() && G->target(_stack[_stack_head])!=dest )
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
    int run(Node s,Node t) {
637
    bool run(Node s,Node t) {
638 638
      init();
639 639
      addSource(s);
640 640
      start(t);
641
      return reached(t)?_stack_head+1:0;
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
... ...
@@ -1481,175 +1481,175 @@
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 dest.
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
    int run(Node s,Node t) {
1603
    bool run(Node s,Node t) {
1604 1604
      init();
1605 1605
      addSource(s);
1606 1606
      start(t);
1607
      return reached(t)?_stack_head+1:0;
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

	
Ignore white space 6 line context
... ...
@@ -683,287 +683,294 @@
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 reached.
731
    ///Executes the algorithm until the given target node is processed.
732 732

	
733
    ///Executes the algorithm until the given target node is reached.
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 dest.
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 dest)
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
    Value run(Node s,Node t) {
814
    bool run(Node s,Node t) {
812 815
      init();
813 816
      addSource(s);
814 817
      start(t);
815
      return (*_pred)[t]==INVALID?OperationTraits::zero():(*_dist)[t];
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 start()
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.
Ignore white space 6 line context
... ...
@@ -9,117 +9,142 @@
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
  BType bfs_test(G);
69
  {
70
    BType bfs_test(G);
67 71

	
68
  bfs_test.run(n);
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
  Path<Digraph> pp = bfs_test.path(n);
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();
Ignore white space 96 line context
... ...
@@ -11,117 +11,142 @@
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
  DType dfs_test(G);
71
  {
72
    DType dfs_test(G);
69 73

	
70
  dfs_test.run(n);
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
  Path<Digraph> pp = dfs_test.path(n);
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();
Ignore white space 6 line context
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
  DType dijkstra_test(G,length);
72
  {
73
    DType dijkstra_test(G,length);
69 74

	
70
  dijkstra_test.run(n);
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
  Path<Digraph> pp = dijkstra_test.path(n);
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

	
0 comments (0 inline)