kruskal_minimum_spanning_tree
Find a minimum spanning tree in an undirected graph with weighted edges using Kruskal’s algorithm.
Complexity: O(E log E)
Defined in: <boost/graph/kruskal_min_spanning_tree.hpp>
Example
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/kruskal_min_spanning_tree.hpp>
#include <iostream>
#include <vector>
struct Road { int weight; };
int main() {
using namespace boost;
using Graph = adjacency_list<vecS, vecS, undirectedS, no_property, Road>;
using Edge = graph_traits<Graph>::edge_descriptor;
Graph g(5);
add_edge(0, 1, Road{2}, g);
add_edge(0, 3, Road{1}, g);
add_edge(1, 2, Road{3}, g);
add_edge(1, 3, Road{2}, g);
add_edge(2, 3, Road{5}, g);
add_edge(2, 4, Road{4}, g);
add_edge(3, 4, Road{6}, g);
// Named-parameter overload: needed because the weight lives in a bundled
// property (not the interior `edge_weight_t` tag that the 2-arg positional
// overload would pick up).
std::vector<Edge> mst;
kruskal_minimum_spanning_tree(g, std::back_inserter(mst),
weight_map(get(&Road::weight, g)));
int total = 0;
std::cout << "MST edges:\n";
for (auto& e : mst) {
int w = g[e].weight;
std::cout << " " << source(e, g) << " -- "
<< target(e, g) << " (weight " << w << ")\n";
total += w;
}
std::cout << "Total weight: " << total << "\n";
}
MST edges:
0 -- 3 (weight 1)
0 -- 1 (weight 2)
1 -- 2 (weight 3)
2 -- 4 (weight 4)
Total weight: 10
(1) Named parameter version
template <class Graph, class OutputIterator,
class P, class T, class R>
OutputIterator
kruskal_minimum_spanning_tree(
Graph& g, OutputIterator tree_edges,
const bgl_named_params<P, T, R>& params = all defaults);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The graph type must be a model of Vertex List Graph and Edge List Graph. |
IN |
|
The edges of the minimum spanning tree are output to this Output Iterator. |
IN |
|
The weight or "length" of each edge in the graph. The |
UTIL |
|
This is used by the disjoint sets data structure. The type |
UTIL |
|
This is used by the disjoint sets data structure, and is not used for storing predecessors in the spanning tree. The predecessors of the spanning tree can be obtained from the spanning tree edges output. The type |
IN |
|
This maps each vertex to an integer in the range |
(2) Positional version
template <class Graph, class OutputIterator>
OutputIterator
kruskal_minimum_spanning_tree(
const Graph& g, OutputIterator spanning_tree_edges);
Equivalent to (1) with every named parameter left at its default. Internally calls get(edge_weight, g) and get(vertex_index, g), so the graph must have both interior properties (this is automatic for adjacency_list with vecS vertex storage, but you must declare vertex_index_t when using listS / setS).
If you need to supply a weight map or a custom disjoint-sets backing, use overload (1).
| Direction | Parameter | Description |
|---|---|---|
IN |
|
Same concept requirements as (1). |
IN |
|
Same as (1). |
Description
The kruskal_minimum_spanning_tree() function find a minimum spanning tree (MST) in an undirected graph with weighted edges. A MST is a set of edges that connects all the vertices in the graph where the total weight of the edges in the tree is minimized. For more details, see section Minimum Spanning Tree Problem. The edges in the MST are output to the tree_edges output iterator. This function uses Kruskal’s algorithm to compute the MST [14,5,22,12].
Kruskal’s algorithm starts with each vertex in a tree by itself, and with no edges in the minimum spanning tree T. The algorithm then examines each edge in the graph in order of increasing edge weight. If an edge connects two vertices in different trees the algorithm merges the two trees into a single tree and adds the edge to T. We use the "union by rank" and "path compression" heuristics to provide fast implementations of the disjoint set operations (MAKE-SET, FIND-SET, and UNION-SET). The algorithm is as follows:
KRUSKAL-MST(G, w)
T := O
for each vertex u in V
MAKE-SET(DS, u)
end for
for each edge (u,v) in E in order of nondecreasing weight
if FIND-SET(DS, u) != FIND-SET(DS, v)
UNION-SET(DS, u, v)
T := T U {(u,v)}
end for
return T
Example
The file examples/kruskal-example.cpp contains an example of using Kruskal’s algorithm.