Prim Minimum Spanning Tree
Solves the minimum spanning tree problem for an undirected graph with weighted edges using Prim’s algorithm.
Complexity: O(E log V)
Defined in: <boost/graph/prim_minimum_spanning_tree.hpp>
Example
// Prim Minimum Spanning Tree example
#include <iostream>
#include <vector>
#include <limits>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/prim_minimum_spanning_tree.hpp>
struct EdgeWeight { int weight; };
using Graph = boost::adjacency_list<
boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, EdgeWeight>;
using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
int main() {
Graph g{5};
auto add = [&](int u, int v, int w) { boost::add_edge(u, v, EdgeWeight{w}, g); };
add(0, 1, 2); add(0, 3, 1); add(1, 2, 3);
add(1, 3, 2); add(2, 4, 4); add(3, 4, 6);
auto n = boost::num_vertices(g);
std::vector<Vertex> pred(n);
std::vector<int> dist(n);
auto index_map = boost::get(boost::vertex_index, g);
auto pred_map = boost::make_iterator_property_map(pred.begin(), index_map);
auto dist_map = boost::make_iterator_property_map(dist.begin(), index_map);
auto weight_map = boost::get(&EdgeWeight::weight, g);
boost::prim_minimum_spanning_tree(g, *boost::vertices(g).first,
pred_map, dist_map, weight_map, index_map,
boost::default_dijkstra_visitor{});
std::cout << "Predecessor map (vertex : parent):\n";
for (Vertex v = 0; v < boost::num_vertices(g); ++v) {
std::cout << " " << v << " : " << pred[v] << "\n";
}
}
Predecessor map (vertex : parent):
0 : 0
1 : 0
2 : 1
3 : 0
4 : 2
(1) Positional version
template <typename Graph, typename DijkstraVisitor,
typename PredecessorMap, typename DistanceMap,
typename WeightMap, typename IndexMap>
void prim_minimum_spanning_tree(
const Graph& g,
typename graph_traits<Graph>::vertex_descriptor s,
PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
IndexMap index_map, DijkstraVisitor vis);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The type |
IN |
|
The root vertex for the minimum spanning tree. The choice of the root vertex is arbitrary. |
OUT |
|
The predecessor map records the edges in the minimum spanning tree. Upon
completion of the algorithm, the edges (p[u],u) for all u in V are in
the minimum spanning tree. If p[u] = u then u is either the root of
the tree or is a vertex that is not reachable from the root. The
|
UTIL/OUT |
|
The weight of the spanning tree edge into each vertex in the graph |
IN |
|
The weight or "length" of each edge in the graph. The type |
IN |
|
This maps each vertex to an integer in the range |
IN |
|
Use this to specify actions that you would like to happen during certain
event points within the algorithm. The type |
(2) Named parameter version
template <typename Graph, typename PredMap,
typename P, typename T, typename R>
void prim_minimum_spanning_tree(
const Graph& g, PredMap p_map,
const bgl_named_params<P, T, R>& params);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The type |
OUT |
|
The predecessor map records the edges in the minimum spanning tree. Upon
completion of the algorithm, the edges (p[u],u) for all u in V are in
the minimum spanning tree. If p[u] = u then u is either the root of
the tree or is a vertex that is not reachable from the root. The
|
IN |
|
Named parameters passed via |
| Direction | Named Parameter | Description / Default |
|---|---|---|
IN |
|
The vertex that will be the root of the minimum spanning tree. The choice
of the root vertex is arbitrary. |
IN |
|
The weight or "length" of each edge in the graph. The type |
IN |
|
This maps each vertex to an integer in the range |
UTIL/OUT |
|
The weight of the spanning tree edge into each vertex in the graph |
UTIL/OUT |
|
This is used during the execution of the algorithm to mark the vertices.
The vertices start out white and become gray when they are inserted in
the queue. They then turn black when they are removed from the queue. At
the end of the algorithm, vertices reachable from the source vertex will
have been colored black. All other vertices will still be white. The type
|
OUT |
|
Use this to specify actions that you would like to happen during certain
event points within the algorithm. The type |
Description
This is Prim’s algorithm
for solving the minimum spanning tree problem for 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. See Section
Minimum Spanning
Tree Problem for more details. The implementation is simply a call to
dijkstra_shortest_paths()
with the appropriate choice of comparison and combine functors. The
pseudo-code for Prim’s algorithm is listed below. The algorithm as
implemented in Boost.Graph does not produce correct results on graphs
with parallel edges.
Pseudo-Code
|
|
Visitor Event Points
-
vis.initialize_vertex(u, g)is invoked on each vertex in the graph before the start of the algorithm. -
vis.examine_vertex(u, g)is invoked on a vertex as it is removed from the priority queue. -
vis.examine_edge(e, g)is invoked on each out-edge of a vertex immediately after it has been examined. -
vis.edge_relaxed(e, g)is invoked on edge (u,v) if w(u,v) < d[v]. The edge (u,v) that participated in the last relaxation for vertex v is an edge in the minimum spanning tree. -
vis.discover_vertex(v, g)is invoked on vertex v when the edge (u,v) is examined and v is WHITE. Since a vertex is colored GRAY when it is discovered, each reachable vertex is discovered exactly once. This is also when the vertex is inserted into the priority queue. -
vis.edge_not_relaxed(e, g)is invoked if the edge is not relaxed (see above). -
vis.finish_vertex(u, g)is invoked on a vertex after all of its out edges have been examined.
Example
See example/prim-example.cpp
for an example of using Prim’s algorithm.