kruskal_mst.h File Reference

Go to the source code of this file.

Functions

template<typename OutputIter >
OutputIter kruskal_mst (graph &G, OutputIter res)
 

Function Documentation

template<typename OutputIter >
OutputIter kruskal_mst ( graph &  G,
OutputIter  res 
)
21  {
22 
23  /*
24  The
25  */
26 
27  disjoint_sets<graph::vertex_t> sets;
28 
29  // vertex iterator
30  for(graph::vertex_iter it = G.vbegin(); it != G.vend(); it++) {
31  sets.make_set(*it);
32  }
33 
34  std::list<Graph::edge_type> edges;
35 
36  // edge iterator
37  for(graph::edge_iter it = G.ebegin(); it != G.eend(); it++)
38  edges.push_back(*it);
39 
40  edges.sort();
41 
42  // expand and merge the components iteratively
43  for(graph::edge_type edge : edges) {
44 
45  graph::vertex_type u = edge.either(), v = edge.other();
46 
47  if(sets.find(u) != sets.find(v)) {
48  // Different component
49  sets.unite(u, v);
50  res++ = edge;
51  }
52  }
53 
54  return res;
55 }