659 |
662 |
660 ///@{ |
663 ///@{ |
661 |
664 |
662 ///\brief Returns the 'previous edge' of the minimum spanning tree. |
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, |
667 ///For a node \c v it returns the 'previous edge' of the minimum |
665 ///i.e. it returns the edge from where \c v was reached. For a source node |
668 ///spanning tree, i.e. it returns the edge from where \c v was |
666 ///or an unreachable node it is \ref INVALID. |
669 ///reached. For a source node or an unreachable node it is \ref |
667 ///The minimum spanning tree used here is equal to the minimum spanning tree used |
670 ///INVALID. The minimum spanning tree used here is equal to the |
668 ///in \ref predNode(). \pre \ref run() or \ref start() must be called before |
671 ///minimum spanning tree used in \ref predNode(). |
669 ///using this function. |
672 ///\pre \ref run() or \ref start() must be called before using |
|
673 ///this function. |
670 UEdge predEdge(Node v) const { return (*_pred)[v]; } |
674 UEdge predEdge(Node v) const { return (*_pred)[v]; } |
671 |
675 |
672 ///\brief Returns the 'previous node' of the minimum spanning tree. |
676 ///\brief Returns the 'previous node' of the minimum spanning |
673 |
677 ///tree. |
674 ///For a node \c v it returns the 'previous node' of the minimum spanning tree, |
678 /// |
675 ///i.e. it returns the node from where \c v was reached. For a source node |
679 ///For a node \c v it returns the 'previous node' of the minimum |
676 ///or an unreachable node it is \ref INVALID. |
680 ///spanning tree, i.e. it returns the node from where \c v was |
677 //The minimum spanning tree used here is equal to the minimum spanning |
681 ///reached. For a source node or an unreachable node it is \ref |
678 ///tree used in \ref predEdge(). \pre \ref run() or \ref start() must be called |
682 ///INVALID. //The minimum spanning tree used here is equal to the |
679 ///before using this function. |
683 ///minimum spanning tree used in \ref predEdge(). |
|
684 ///\pre \ref run() or \ref start() must be called before using |
|
685 ///this function. |
680 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
686 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
681 graph->source((*_pred)[v]); } |
687 graph->source((*_pred)[v]); } |
682 |
688 |
683 ///\brief Returns a reference to the NodeMap of the edges of the minimum spanning tree. |
689 ///\brief Returns a reference to the NodeMap of the edges of the |
684 |
|
685 ///Returns a reference to the NodeMap of the edges of the |
|
686 ///minimum spanning tree. |
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 const PredMap &predMap() const { return *_pred;} |
696 const PredMap &predMap() const { return *_pred;} |
689 |
697 |
690 ///\brief Returns a reference to the tree edges map. |
698 ///\brief Returns a reference to the tree edges map. |
691 |
699 |
692 ///Returns a reference to the TreeEdgeMap of the edges of the |
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 |
701 ///minimum spanning tree. The value of the map is \c true only if |
694 ///the minimum spanning tree. |
702 ///the edge is in the minimum spanning tree. |
|
703 /// |
695 ///\warning By default, the TreeEdgeMap is a NullMap. |
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 |
706 ///If it is not set before the execution of the algorithm, use the |
698 ///treeMap(TreeMap&) function (after the execution) to set an UEdgeMap with the |
707 ///\ref treeMap(TreeMap&) function (after the execution) to set an |
699 ///edges of the minimum spanning tree in O(n) time where n is the number of |
708 ///UEdgeMap with the edges of the minimum spanning tree in O(n) |
700 ///nodes in the graph. |
709 ///time where n is the number of nodes in the graph. |
701 ///\pre \ref run() or \ref start() must be called before using this function. |
710 ///\pre \ref run() or \ref start() must be called before using |
|
711 ///this function. |
702 const TreeMap &treeMap() const { return *_tree;} |
712 const TreeMap &treeMap() const { return *_tree;} |
703 |
713 |
704 ///\brief Sets the tree edges map. |
714 ///\brief Sets the tree edges map. |
705 |
715 /// |
706 ///Sets the TreeMap of the edges of the minimum spanning tree. |
716 ///Sets the TreeMap of the edges of the minimum spanning tree. |
707 ///The map values belonging to the edges of the minimum |
717 ///The map values belonging to the edges of the minimum |
708 ///spanning tree are set to \c tree_edge_value or \c true by default, |
718 ///spanning tree are set to \c tree_edge_value or \c true by default, |
709 ///the other map values remain untouched. |
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 template<class TreeMap> |
724 template<class TreeMap> |
714 void quickTreeEdges( |
725 void quickTreeEdges(TreeMap& tree) const { |
715 TreeMap& tree, |
|
716 const typename TreeMap::Value& tree_edge_value=true) const { |
|
717 for(NodeIt i(*graph);i!=INVALID;++i){ |
726 for(NodeIt i(*graph);i!=INVALID;++i){ |
718 if((*_pred)[i]!=INVALID) tree.set((*_pred)[i],tree_edge_value); |
727 if((*_pred)[i]!=INVALID) tree.set((*_pred)[i],true); |
719 } |
728 } |
720 } |
729 } |
721 |
730 |
722 ///\brief Sets the tree edges map. |
731 ///\brief Sets the tree edges map. |
723 |
732 /// |
724 ///Sets the TreeMap of the edges of the minimum spanning tree. |
733 ///Sets the TreeMap of the edges of the minimum spanning tree. |
725 ///The map values belonging to the edges of the minimum |
734 ///The map values belonging to the edges of the minimum |
726 ///spanning tree are set to \c tree_edge_value or \c true by default while |
735 ///spanning tree are set to \c tree_edge_value or \c true by default while |
727 ///the edge values not belonging to the minimum spanning tree are set to |
736 ///the edge values not belonging to the minimum spanning tree are set to |
728 ///\c tree_default_value or \c false by default. |
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. |
739 ///\pre \ref run() or \ref start() must be called before using |
731 |
740 ///this function. |
732 template<class TreeMap> |
741 template <class TreeMap> |
733 void treeEdges( |
742 void treeEdges(TreeMap& tree) const { |
734 TreeMap& tree, |
743 typedef typename ItemSetTraits<UGraph,UEdge>::ItemIt TreeMapIt; |
735 const typename TreeMap::Value& tree_edge_value=true, |
744 for(TreeMapIt i(*graph); i != INVALID; ++i) { |
736 const typename TreeMap::Value& tree_default_value=false) const { |
745 tree.set(i,false); |
737 for(typename ItemSetTraits<UGraph,UEdge>::ItemIt i(*graph);i!=INVALID;++i) |
746 } |
738 tree.set(i,tree_default_value); |
747 for(NodeIt i(*graph); i != INVALID; ++i){ |
739 for(NodeIt i(*graph);i!=INVALID;++i){ |
748 if((*_pred)[i] != INVALID) tree.set((*_pred)[i],true); |
740 if((*_pred)[i]!=INVALID) tree.set((*_pred)[i],tree_edge_value); |
|
741 } |
749 } |
742 } |
750 } |
743 |
751 |
744 ///\brief Checks if a node is reachable from the starting node. |
752 ///\brief Checks if a node is reachable from the starting node. |
745 |
753 /// |
746 ///Returns \c true if \c v is reachable from the starting node. |
754 ///Returns \c true if \c v is reachable from the starting node. |
747 ///\warning The source nodes are inditated as unreached. |
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 bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; } |
759 bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; } |
751 |
760 |
752 ///\brief Checks if a node is processed. |
761 ///\brief Checks if a node is processed. |
753 |
762 /// |
754 ///Returns \c true if \c v is processed, i.e. \c v is already connencted to the |
763 ///Returns \c true if \c v is processed, i.e. \c v is already |
755 ///minimum spanning tree. |
764 ///connencted to the minimum spanning tree. \pre \ref run() or |
756 ///\pre \ref run() or \ref start() must be called before using this function. |
765 ///\ref start() must be called before using this function. |
757 /// |
|
758 bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; } |
766 bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; } |
759 |
767 |
760 |
768 |
761 ///\brief Checks if an edge is in the spanning tree or not. |
769 ///\brief Checks if an edge is in the spanning tree or not. |
762 |
770 /// |
763 ///Checks if an edge is in the spanning tree or not. |
771 ///Checks if an edge is in the spanning tree or not. |
764 ///\param e is the edge that will be checked |
772 ///\param e is the edge that will be checked |
765 ///\return \c true if e is in the spanning tree, \c false otherwise |
773 ///\return \c true if e is in the spanning tree, \c false otherwise |
766 bool tree(UEdge e){ |
774 bool tree(UEdge e){ |
767 return (*_pred)[*graph.source(e)]==e || (*_pred)[*graph.target(e)]==e; |
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 }; |
771 |
790 |
772 |
791 |
773 /// \ingroup spantree |
792 /// \ingroup spantree |
777 /// Function type interface for Prim algorithm. |
796 /// Function type interface for Prim algorithm. |
778 /// \param graph the UGraph that the algorithm runs on |
797 /// \param graph the UGraph that the algorithm runs on |
779 /// \param cost the CostMap of the edges |
798 /// \param cost the CostMap of the edges |
780 /// \retval tree the EdgeMap that contains whether an edge is in |
799 /// \retval tree the EdgeMap that contains whether an edge is in |
781 /// the spanning tree or not |
800 /// the spanning tree or not |
|
801 /// \return The total cost of the spanning tree |
782 /// |
802 /// |
783 ///\sa Prim |
803 ///\sa Prim |
784 template<class Graph,class CostMap,class TreeMap> |
804 template<class Graph,class CostMap,class TreeMap> |
785 void prim(const Graph& graph, const CostMap& cost,TreeMap& tree){ |
805 typename CostMap::Value prim(const Graph& graph, |
786 typename Prim<Graph,CostMap>::template DefTreeMap<TreeMap>:: |
806 const CostMap& cost, |
|
807 TreeMap& tree){ |
|
808 typename Prim<Graph,CostMap>:: |
|
809 template DefTreeMap<TreeMap>:: |
787 Create prm(graph,cost); |
810 Create prm(graph,cost); |
788 prm.treeMap(tree); |
811 prm.treeMap(tree); |
789 prm.run(); |
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 } //END OF NAMESPACE LEMON |
836 } //END OF NAMESPACE LEMON |
793 |
837 |
794 #endif |
838 #endif |