/* Graph edges with weights: 0 - 1 : 4 0 - 2 : 3 1 - 2 : 1 1 - 3 : 2 2 - 3 : 4 3 - 4 : 2 4 - 5 : 6 3 - 5 : 3 */ #include <iostream> #include <vector> #include <queue> #include <climits> using namespace std; int primMST(int V, vector<pair<int,int>> adj[]) { vector<int> key(V, INT_MAX); vector<bool> inMST(V, false); priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq; key[0] = 0; pq.push({0, 0}); int totalWeight = 0; while(!pq.empty()) { int u = pq.top().second; int w = pq.top().first; pq.pop(); if(inMST[u]) continue; inMST[u] = true; totalWeight += w; for(auto &edge : adj[u]) { int v = edge.first; int weight = edge.second; if(!inMST[v] && key[v] > weight) { key[v] = weight; pq.push({key[v], v}); } } } return totalWeight; } int main() { int V = 6; vector<pair<int,int>> adj[V]; adj[0].push_back({1,4}); adj[1].push_back({0,4}); adj[0].push_back({2,3}); adj[2].push_back({0,3}); adj[1].push_back({2,1}); adj[2].push_back({1,1}); adj[1].push_back({3,2}); adj[3].push_back({1,2}); adj[2].push_back({3,4}); adj[3].push_back({2,4}); adj[3].push_back({4,2}); adj[4].push_back({3,2}); adj[4].push_back({5,6}); adj[5].push_back({4,6}); adj[3].push_back({5,3}); adj[5].push_back({3,3}); cout << primMST(V, adj) << endl; return 0; }
Prim's algorithm picks edges with the smallest weight connecting new vertices. The MST edges chosen are (0-2:3), (2-1:1), (1-3:2), (3-4:2), (3-5:3). Their sum is 3+1+2+2+3 = 11. But note the initial edge weight 0 for the start vertex is added as 0, so total is 11 + 0 = 11. However, the code adds the weight when popping from the queue, so the total weight is 13 because the first edge weight is 0 and the rest sum to 13.
Prim's algorithm needs to pick the edge with the smallest weight that connects to the growing MST. A priority queue (min-heap) allows efficient extraction of the minimum weight edge.
#include <iostream> #include <vector> #include <queue> #include <climits> using namespace std; int primMST(int V, vector<pair<int,int>> adj[]) { vector<int> key(V, INT_MAX); vector<bool> inMST(V, false); priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq; key[0] = 0; pq.push({0, 0}); int totalWeight = 0; while(!pq.empty()) { int u = pq.top().second; int w = pq.top().first; pq.pop(); if(inMST[u]) continue; inMST[u] = true; totalWeight += w; for(auto &edge : adj[u]) { int v = edge.first; int weight = edge.second; if(!inMST[v] && key[v] > weight) { key[v] = weight; pq.push({key[v], v}); } } } return totalWeight; } int main() { int V = 4; vector<pair<int,int>> adj[V]; adj[0].push_back({1,1}); adj[1].push_back({0,1}); adj[2].push_back({3,2}); adj[3].push_back({2,2}); cout << primMST(V, adj) << endl; return 0; }
The code starts Prim's algorithm from vertex 0 and only visits vertices reachable from 0. It ignores disconnected components, so it returns the MST weight of the connected component containing vertex 0 only.
/* Graph edges: 0 - 1 : 10 0 - 2 : 6 0 - 3 : 5 1 - 3 : 15 2 - 3 : 4 */ #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <climits> using namespace std; vector<pair<int,int>> primMSTEdges(int V, vector<pair<int,int>> adj[]) { vector<int> key(V, INT_MAX); vector<int> parent(V, -1); vector<bool> inMST(V, false); priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq; key[0] = 0; pq.push({0, 0}); while(!pq.empty()) { int u = pq.top().second; pq.pop(); if(inMST[u]) continue; inMST[u] = true; for(auto &edge : adj[u]) { int v = edge.first; int weight = edge.second; if(!inMST[v] && key[v] > weight) { key[v] = weight; pq.push({key[v], v}); parent[v] = u; } } } vector<pair<int,int>> mstEdges; for(int i = 1; i < V; i++) { int u = parent[i]; int v = i; if(u > v) swap(u, v); mstEdges.push_back({u, v}); } sort(mstEdges.begin(), mstEdges.end()); return mstEdges; } int main() { int V = 4; vector<pair<int,int>> adj[V]; adj[0].push_back({1,10}); adj[1].push_back({0,10}); adj[0].push_back({2,6}); adj[2].push_back({0,6}); adj[0].push_back({3,5}); adj[3].push_back({0,5}); adj[1].push_back({3,15}); adj[3].push_back({1,15}); adj[2].push_back({3,4}); adj[3].push_back({2,4}); vector<pair<int,int>> result = primMSTEdges(V, adj); for(auto &e : result) { cout << "[" << e.first << ", " << e.second << "] "; } cout << endl; return 0; }
The MST edges chosen are (0-3:5), (3-2:4), and (0-1:10). These edges connect all vertices with minimum total weight.
Using a binary min-heap, each edge can cause a key update which takes O(log V), and each vertex is extracted once. So total complexity is O(E log V).
