Changeset 2397:a501140ce878 in lemon0.x for lemon
 Timestamp:
 03/07/07 12:56:53 (13 years ago)
 Branch:
 default
 Phase:
 public
 Convert:
 svn:c9d7d8f590d60310b91f818b3a526b0e/lemon/trunk@3228
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

lemon/prim.h
r2391 r2397 402 402 403 403 ///Constructor. 404 404 /// 405 405 ///\param _graph the graph the algorithm will run on. 406 406 ///\param _cost the cost map used by the algorithm. 407 407 Prim(const UGraph& _graph, const CostMap& _cost) : 408 408 graph(&_graph), cost(&_cost), 409 _pred( NULL), local_pred(false),410 _tree( NULL), local_tree(false),411 _processed( NULL), local_processed(false),412 _heap_cross_ref( NULL), local_heap_cross_ref(false),413 _heap( NULL), local_heap(false)409 _pred(0), local_pred(false), 410 _tree(0), local_tree(false), 411 _processed(0), local_processed(false), 412 _heap_cross_ref(0), local_heap_cross_ref(false), 413 _heap(0), local_heap(false) 414 414 { 415 415 checkConcept<concepts::UGraph, UGraph>(); … … 426 426 427 427 ///\brief Sets the cost map. 428 428 /// 429 429 ///Sets the cost map. 430 430 ///\return <tt> (*this) </tt> … … 435 435 436 436 ///\brief Sets the map storing the predecessor edges. 437 437 /// 438 438 ///Sets the map storing the predecessor edges. 439 439 ///If you don't use this function before calling \ref run(), … … 451 451 452 452 ///\brief Sets the map storing the tree edges. 453 453 /// 454 454 ///Sets the map storing the tree edges. 455 455 ///If you don't use this function before calling \ref run(), … … 468 468 469 469 ///\brief Sets the heap and the cross reference used by algorithm. 470 470 /// 471 471 ///Sets the heap and the cross reference used by algorithm. 472 472 ///If you don't use this function before calling \ref run(), … … 502 502 503 503 ///\brief Initializes the internal data structures. 504 504 /// 505 505 ///Initializes the internal data structures. 506 506 /// … … 516 516 517 517 ///\brief Adds a new source node. 518 518 /// 519 519 ///Adds a new source node to the priority heap. 520 520 /// … … 527 527 } 528 528 ///\brief Processes the next node in the priority heap 529 529 /// 530 530 ///Processes the next node in the priority heap. 531 531 /// … … 560 560 561 561 ///\brief Next node to be processed. 562 562 /// 563 563 ///Next node to be processed. 564 564 /// … … 569 569 } 570 570 571 ///\brief Returns \c false if there are nodes to be processed in the priority heap 571 ///\brief Returns \c false if there are nodes to be processed in 572 ///the priority heap 572 573 /// 573 574 ///Returns \c false if there are nodes 574 575 ///to be processed in the priority heap 575 576 bool emptyQueue() { return _heap>empty(); } 576 ///\brief Returns the number of the nodes to be processed in the priority heap 577 577 578 ///\brief Returns the number of the nodes to be processed in the 579 ///priority heap 580 /// 578 581 ///Returns the number of the nodes to be processed in the priority heap 579 582 /// … … 581 584 582 585 ///\brief Executes the algorithm. 583 586 /// 584 587 ///Executes the algorithm. 585 588 /// … … 596 599 597 600 ///\brief Executes the algorithm until a condition is met. 598 601 /// 599 602 ///Executes the algorithm until a condition is met. 600 603 /// … … 611 614 612 615 ///\brief Runs %Prim algorithm. 613 616 /// 614 617 ///This method runs the %Prim algorithm 615 618 ///in order to compute the … … 628 631 629 632 ///\brief Runs %Prim algorithm from node \c s. 630 633 /// 631 634 ///This method runs the %Prim algorithm from node \c s 632 635 ///in order to … … 634 637 ///minimun spanning tree 635 638 /// 636 ///\note d.run(s) is just a shortcut of the following code.639 ///\note p.run(s) is just a shortcut of the following code. 637 640 ///\code 638 /// d.init();639 /// d.addSource(s);640 /// d.start();641 /// p.init(); 642 /// p.addSource(s); 643 /// p.start(); 641 644 ///\endcode 642 645 ///\note If the graph has more than one components, the method … … 662 665 ///\brief Returns the 'previous edge' of the minimum spanning tree. 663 666 664 ///For a node \c v it returns the 'previous edge' of the minimum spanning tree, 665 ///i.e. it returns the edge from where \c v was reached. For a source node 666 ///or an unreachable node it is \ref INVALID. 667 ///The minimum spanning tree used here is equal to the minimum spanning tree used 668 ///in \ref predNode(). \pre \ref run() or \ref start() must be called before 669 ///using this function. 667 ///For a node \c v it returns the 'previous edge' of the minimum 668 ///spanning tree, i.e. it returns the edge from where \c v was 669 ///reached. For a source node or an unreachable node it is \ref 670 ///INVALID. The minimum spanning tree used here is equal to the 671 ///minimum spanning tree used in \ref predNode(). 672 ///\pre \ref run() or \ref start() must be called before using 673 ///this function. 670 674 UEdge predEdge(Node v) const { return (*_pred)[v]; } 671 675 672 ///\brief Returns the 'previous node' of the minimum spanning tree. 673 674 ///For a node \c v it returns the 'previous node' of the minimum spanning tree, 675 ///i.e. it returns the node from where \c v was reached. For a source node 676 ///or an unreachable node it is \ref INVALID. 677 //The minimum spanning tree used here is equal to the minimum spanning 678 ///tree used in \ref predEdge(). \pre \ref run() or \ref start() must be called 679 ///before using this function. 676 ///\brief Returns the 'previous node' of the minimum spanning 677 ///tree. 678 /// 679 ///For a node \c v it returns the 'previous node' of the minimum 680 ///spanning tree, i.e. it returns the node from where \c v was 681 ///reached. For a source node or an unreachable node it is \ref 682 ///INVALID. //The minimum spanning tree used here is equal to the 683 ///minimum spanning tree used in \ref predEdge(). 684 ///\pre \ref run() or \ref start() must be called before using 685 ///this function. 680 686 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: 681 687 graph>source((*_pred)[v]); } 682 688 683 ///\brief Returns a reference to the NodeMap of the edges of the minimum spanning tree. 684 685 ///Returns a reference to the NodeMap of the edges of the 689 ///\brief Returns a reference to the NodeMap of the edges of the 686 690 ///minimum spanning tree. 687 ///\pre \ref run() or \ref start() must be called before using this function. 691 /// 692 ///Returns a reference to the NodeMap of the edges of the minimum 693 ///spanning tree. 694 ///\pre \ref run() or \ref start() must be called before using 695 ///this function. 688 696 const PredMap &predMap() const { return *_pred;} 689 697 … … 691 699 692 700 ///Returns a reference to the TreeEdgeMap of the edges of the 693 ///minimum spanning tree. The value of the map is \c true only if the edge is in 694 ///the minimum spanning tree. 701 ///minimum spanning tree. The value of the map is \c true only if 702 ///the edge is in the minimum spanning tree. 703 /// 695 704 ///\warning By default, the TreeEdgeMap is a NullMap. 696 705 /// 697 ///If it is not set before the execution of the algorithm, use the \ref 698 ///treeMap(TreeMap&) function (after the execution) to set an UEdgeMap with the 699 ///edges of the minimum spanning tree in O(n) time where n is the number of 700 ///nodes in the graph. 701 ///\pre \ref run() or \ref start() must be called before using this function. 706 ///If it is not set before the execution of the algorithm, use the 707 ///\ref treeMap(TreeMap&) function (after the execution) to set an 708 ///UEdgeMap with the edges of the minimum spanning tree in O(n) 709 ///time where n is the number of nodes in the graph. 710 ///\pre \ref run() or \ref start() must be called before using 711 ///this function. 702 712 const TreeMap &treeMap() const { return *_tree;} 703 713 704 714 ///\brief Sets the tree edges map. 705 715 /// 706 716 ///Sets the TreeMap of the edges of the minimum spanning tree. 707 717 ///The map values belonging to the edges of the minimum … … 709 719 ///the other map values remain untouched. 710 720 /// 711 ///\pre \ref run() or \ref start() must be called before using this function. 721 ///\pre \ref run() or \ref start() must be called before using 722 ///this function. 712 723 713 724 template<class TreeMap> 714 void quickTreeEdges( 715 TreeMap& tree, 716 const typename TreeMap::Value& tree_edge_value=true) const { 725 void quickTreeEdges(TreeMap& tree) const { 717 726 for(NodeIt i(*graph);i!=INVALID;++i){ 718 if((*_pred)[i]!=INVALID) tree.set((*_pred)[i],tr ee_edge_value);727 if((*_pred)[i]!=INVALID) tree.set((*_pred)[i],true); 719 728 } 720 729 } 721 730 722 731 ///\brief Sets the tree edges map. 723 732 /// 724 733 ///Sets the TreeMap of the edges of the minimum spanning tree. 725 734 ///The map values belonging to the edges of the minimum … … 728 737 ///\c tree_default_value or \c false by default. 729 738 /// 730 ///\pre \ref run() or \ref start() must be called before using this function. 731 732 template<class TreeMap> 733 void treeEdges( 734 TreeMap& tree, 735 const typename TreeMap::Value& tree_edge_value=true, 736 const typename TreeMap::Value& tree_default_value=false) const { 737 for(typename ItemSetTraits<UGraph,UEdge>::ItemIt i(*graph);i!=INVALID;++i) 738 tree.set(i,tree_default_value); 739 for(NodeIt i(*graph);i!=INVALID;++i){ 740 if((*_pred)[i]!=INVALID) tree.set((*_pred)[i],tree_edge_value); 739 ///\pre \ref run() or \ref start() must be called before using 740 ///this function. 741 template <class TreeMap> 742 void treeEdges(TreeMap& tree) const { 743 typedef typename ItemSetTraits<UGraph,UEdge>::ItemIt TreeMapIt; 744 for(TreeMapIt i(*graph); i != INVALID; ++i) { 745 tree.set(i,false); 746 } 747 for(NodeIt i(*graph); i != INVALID; ++i){ 748 if((*_pred)[i] != INVALID) tree.set((*_pred)[i],true); 741 749 } 742 750 } 743 751 744 752 ///\brief Checks if a node is reachable from the starting node. 745 753 /// 746 754 ///Returns \c true if \c v is reachable from the starting node. 747 755 ///\warning The source nodes are inditated as unreached. 748 ///\pre \ref run() or \ref start() must be called before using this function. 756 ///\pre \ref run() or \ref start() must be called before using 757 ///this function. 749 758 /// 750 759 bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; } 751 760 752 761 ///\brief Checks if a node is processed. 753 754 ///Returns \c true if \c v is processed, i.e. \c v is already connencted to the 755 ///minimum spanning tree. 756 ///\pre \ref run() or \ref start() must be called before using this function. 757 /// 762 /// 763 ///Returns \c true if \c v is processed, i.e. \c v is already 764 ///connencted to the minimum spanning tree. \pre \ref run() or 765 ///\ref start() must be called before using this function. 758 766 bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; } 759 767 760 768 761 769 ///\brief Checks if an edge is in the spanning tree or not. 762 770 /// 763 771 ///Checks if an edge is in the spanning tree or not. 764 772 ///\param e is the edge that will be checked … … 767 775 return (*_pred)[*graph.source(e)]==e  (*_pred)[*graph.target(e)]==e; 768 776 } 777 778 ///\brief Returns the value of the total cost of the spanning tree. 779 /// 780 /// Returns the value of the total cost of the spanning tree. 781 Value treeValue() const { 782 Value value = 0; 783 for(NodeIt i(*graph); i!= INVALID; ++i){ 784 if ((*_pred)[i] != INVALID) value += (*cost)[(*_pred)[i]]; 785 } 786 return value; 787 } 769 788 ///@} 770 789 }; … … 780 799 /// \retval tree the EdgeMap that contains whether an edge is in 781 800 /// the spanning tree or not 801 /// \return The total cost of the spanning tree 782 802 /// 783 803 ///\sa Prim 784 804 template<class Graph,class CostMap,class TreeMap> 785 void prim(const Graph& graph, const CostMap& cost,TreeMap& tree){ 786 typename Prim<Graph,CostMap>::template DefTreeMap<TreeMap>:: 805 typename CostMap::Value prim(const Graph& graph, 806 const CostMap& cost, 807 TreeMap& tree){ 808 typename Prim<Graph,CostMap>:: 809 template DefTreeMap<TreeMap>:: 787 810 Create prm(graph,cost); 788 811 prm.treeMap(tree); 789 812 prm.run(); 813 return prm.treeValue(); 790 814 } 791 815 816 /// \ingroup spantree 817 /// 818 /// \brief Function type interface for Prim algorithm. 819 /// 820 /// Function type interface for Prim algorithm. 821 /// \param graph the UGraph that the algorithm runs on 822 /// \param cost the CostMap of the edges 823 /// the spanning tree or not 824 /// \return The total cost of the spanning tree 825 /// 826 ///\sa Prim 827 template<class Graph,class CostMap,class TreeMap> 828 typename CostMap::Value prim(const Graph& graph, 829 const CostMap& cost){ 830 typename Prim<Graph,CostMap>:: 831 Create prm(graph,cost); 832 prm.run(); 833 return prm.treeValue(); 834 } 835 792 836 } //END OF NAMESPACE LEMON 793 837
Note: See TracChangeset
for help on using the changeset viewer.