11 template <typename Graph, typename EdgeCostMap, typename EdgeBoolMap>
12 class MinCostTreeKruskal
15 typedef typename Graph::EdgeIt EdgeIt;
16 typedef typename Graph::Edge Edge;
19 typedef typename EdgeCostMap::ValueType EdgeCost;
20 typedef std::pair<Edge, EdgeCost> EdgeCostPair;
21 typedef std::vector<EdgeCostPair> EdgeCostVector;
25 EdgeCostMap const &costmap;
28 //template <typename EdgeCostPair>
31 bool operator()(EdgeCostPair const& a, EdgeCostPair const& b) {
32 return a.second < b.second;
39 MinCostTreeKruskal(Graph const& _G, EdgeCostMap const& _costmap,
40 EdgeBoolMap& _treemap) : G(_G), costmap(_costmap),
47 for (EdgeIt e=G.template first<EdgeIt>(); G.valid(e); G.next(e)) {
48 rank.push_back(make_pair(e, costmap.get(e)));
51 std::sort(rank.begin(), rank.end(), comparePair());
57 EdgeCost run(EdgeCostVector const &rank)
59 typedef typename Graph::NodeMap<int> NodeIntMap;
60 typedef typename Graph::Node Node;
61 NodeIntMap comp_map(G, -1);
62 UnionFind<Node,NodeIntMap> uf(comp_map);
64 EdgeCost tot_cost = 0;
65 for (typename EdgeCostVector::const_iterator p = rank.begin();
66 p!=rank.end(); ++p ) {
67 if ( uf.joinComponents(G.head(p->first), G.tail(p->first)) ) {
68 treemap.set(p->first, true);
69 tot_cost += p->second;
72 treemap.set(p->first, false);
84 #endif //HUGO_KRUSKAL_H