# Blog Archives

## Minimum Spanning Tree Algorithms

Given an undirected, connected graph G=(V,E), one might be concerned with finding a subset ST of edges from E that “span” the graph by ensuring that the graph remains connected. If we further require that the total weights of the edges in ST are minimized, then we are interested in finding a minimum spanning tree (MST). PRIM’S ALGORITHM, shows how to construct an MST from such a graph by using a greedy approach in which each step of the algorithm makes forward progress toward a solution without reversing earlier decisions. PRIM’S ALGORITHM grows a spanning tree T one edge at a time until an MST results (and the resulting spanning tree is provably minimum).

It randomly selects a start vertex s∈V to belong to a growing set S, and it ensures that T forms a tree of edges rooted at s. PRIM’S ALGORITHM is greedy in that it incrementally adds edges to T until an MST is computed. The intuition behind the algorithm is that the edge (u,v) with lowest weight between u∈S and v∈V–S must belong to the MST. When such an edge (u,v) with lowest weight is found, it is added to T and the vertex v is added to S.

The algorithm uses a priority queue to store the vertices v∈V–S with an associated priority equal to the lowest weight of some edge (u,v) where u∈S. This carefully designed approach ensures the efficiency of the resulting implementation.

**Solution**

The C++ solution below relies on a binary heap to provide the implementation of the priority queue that is central to PRIM’S ALGORITHM. Ordinarily, using a binary heap would be inefficient because of the check in the main loop for whether a particular vertex is a member of the priority queue (an operation not supported by binary heaps). However, the algorithm ensures that vertices

are only removed from the priority queue as it processes, so we need only maintain a status array inQueue[] that is updated whenever a vertex is extracted from the priority queue.

In another implementation optimization, we maintain an external array key[] that records the current priority key for each vertex in the queue, which again eliminates the need to search the priority queue for a given vertex identifier.

/** * Prim’s Algorithm implementation with binary heap * * Given undirected graph, compute MST starting from a randomly * selected vertex. Encoding of MST is done using 'pred' entries. */ void mst_prim (Graph const &graph, vector &pred) { // initialize pred[] and key[] arrays. Start with arbitrary // vertex s=0. Priority Queue PQ contains all v in G. const int n = graph.numVertices( ); pred.assign(n, -1); vector<int> key(n, numeric_limits<int>::max( )); key[0] = 0; BinaryHeap pq(n); vector inQueue(n, true); for (int v = 0; v < n; v++) { pq.insert(v, key[v]); } while (!pq.isEmpty( )) { int u = pq.smallest( ); inQueue[u] = false; // Process all neighbors of u to find if any edge beats best distance for (VertexList::const_iterator ci = graph.begin(u); ci != graph.end(u); ++ci) { int v = ci->first; if (inQueue[v]) { int w = ci->second; if (w < key[v]) { pred[v] = u; key[v] = w; pq.decreaseKey(v, w); } } } } }

**Consequences**

For dense graphs, the priority queue can be implemented instead with a Fibonacci heap. This improves the performance to O(E+V*log V), a significant speedup over the binary heap implementation.

**Analysis**

The initialization phase of PRIM’S ALGORITHM inserts each vertex into the priority queue (implemented by a binary heap) for a total cost of O(V log V). The decreaseKey operation in PRIM’S ALGORITHM requires O(log q) performance, where q is the number of elements in the queue, which will always be less than |V|. It can be called at most 2*|E| times since each vertex is removed once from

the priority queue and each undirected edge in the graph is visited exactly twice. Thus the total performance is O((V+2*E)*log n) or O((V+E)*log V).

###### Related articles

- Single-Source Shortest Path (Dijkstra’s Algorithm Implementation in C++) (alikhuram.wordpress.com)
- Prim’s algorithm for Minimum Spanning Tree in C (jlmedina123.wordpress.com)
- Minimum Spanning Trees (eventuallyalmosteverywhere.wordpress.com)
- [ACM-ICPC] Minimal Spanning Tree (slideshare.net)
- Graph algorithm variants (aandrew99.wordpress.com)
- Dijkstra’s Algorithm (woodscompscitutorials.wordpress.com)
- How many minimal spanning trees are there when all edge costs are distinct? (cs.stackexchange.com)