...one of the most highly
regarded and expertly designed C++ library projects in the
world.

— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards

// named parameter versiontemplate <class Graph, class PredMap, class P, class T, class R> void prim_minimum_spanning_tree(const Graph& g, PredMap p_map, const bgl_named_params<P, T, R>& params)// non-named parameter versiontemplate <class Graph, class DijkstraVisitor, class PredecessorMap, class DistanceMap, class WeightMap, class 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)

This is Prim's algorithm [25,8,27,15] 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.

PRIM-MST( |
initialize vertex |

`boost/graph/prim_minimum_spanning_tree.hpp`

An undirected graph. The typeOUT:Graphmust be a model of Vertex List Graph and Incidence Graph.

Python: The parameter is namedgraph.

The predecessor map records the edges in the minimum spanning tree. Upon completion of the algorithm, the edges(p[u],u)for allu in Vare in the minimum spanning tree. Ifp[u] = uthenuis either the root of the tree or is a vertex that is not reachable from the root. ThePredecessorMaptype must be a Read/Write Property Map with key and vertex types the same as the vertex descriptor type of the graph.

Python: Must be avertex_vertex_mapfor the graph.

The vertex that will be the root of the minimum spanning tree. The choice of the root vertex is arbitrary.IN:

Default:*vertices(g).first

The weight or ``length'' of each edge in the graph. The typeIN:WeightMapmust be a model of Readable Property Map. The edge descriptor type of the graph needs to be usable as the key type for the weight map. The value type for the map must beAddablewith the value type of the distance map.

Default:get(edge_weight, g)

Python: Must be anedge_double_mapfor the graph.

Python default:graph.get_edge_double_map("weight")

This maps each vertex to an integer in the rangeUTIL/OUT:[0, num_vertices(g)). This is necessary for efficient updates of the heap data structure when an edge is relaxed. The typeVertexIndexMapmust be a model of Readable Property Map. The value type of the map must be an integer type. The vertex descriptor type of the graph needs to be usable as the key type of the map.

Default:get(vertex_index, g)Note: if you use this default, make sure your graph has an internalvertex_indexproperty. For example,adjacenty_listwithVertexList=listSdoes not have an internalvertex_indexproperty.

Python: Unsupported parameter.

The shortest path weight from the source vertexUTIL/OUT:sto each vertex in the graphgis recorded in this property map. The shortest path weight is the sum of the edge weights along the shortest path. The typeDistanceMapmust be a model of Read/Write Property Map. The vertex descriptor type of the graph needs to be usable as the key type of the distance map. The value type of the distance map must be Less Than Comparable.

Default:iterator_property_mapcreated from astd::vectorof theWeightMap's value type of sizenum_vertices(g)and using thei_mapfor the index map.

Python: Must be avertex_double_mapfor the graph.

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 typeOUT:ColorMapmust be a model of Read/Write Property Map. A vertex descriptor must be usable as the key type of the map, and the value type of the map must be a model of Color Value.

Default:aniterator_property_mapcreated from astd::vectorofdefault_color_typeof sizenum_vertices(g)and using thei_mapfor the index map.

Python: The color map must be avertex_color_mapfor the graph.

Use this to specify actions that you would like to happen during certain event points within the algorithm. The typeDijkstraVisitormust be a model of the Dijkstra Visitor concept. The visitor object is passed by value [1].

Default:dijkstra_visitor<null_visitor>

Python: The parameter should be an object that derives from theDijkstraVisitortype of the graph.

The time complexity is *O(E log V)*.

The file `examples/prim-example.cpp`
contains an example of using Prim's algorithm.

[1]
Since the visitor parameter is passed by value, if your visitor
contains state then any changes to the state during the algorithm
will be made to a copy of the visitor object, not the visitor object
passed in. Therefore you may want the visitor to hold this state by
pointer or reference.

Copyright © 2000-2001 | Jeremy Siek, Indiana University (jsiek@osl.iu.edu) |