#include #include #include #include using namespace std; typedef pair pii; void primMST(int V, vector>& adj) { vector inMST(V, false); vector key(V, INT_MAX); vector parent(V, -1); priority_queue, greater> pq; int src = 0; // Starting vertex pq.push({0, src}); key[src] = 0; while (!pq.empty()) { int u = pq.top().second; pq.pop(); inMST[u] = true; for (auto& neighbor : adj[u]) { int v = neighbor.first; int weight = neighbor.second; if (!inMST[v] && weight < key[v]) { key[v] = weight; pq.push({key[v], v}); parent[v] = u; } } } // Print the Minimum Spanning Tree cout << "Edges in the Minimum Spanning Tree:" << endl; for (int i = 1; i < V; ++i) { cout << "Edge: " << parent[i] << " - " << i << " Weight: " << key[i] << endl; } } int main() { int V = 5; // Number of vertices vector> adj(V); // Add edges with their weights adj[0].push_back({1, 2}); adj[0].push_back({3, 8}); adj[1].push_back({2, 3}); adj[1].push_back({3, 5}); adj[1].push_back({4, 7}); adj[2].push_back({4, 1}); adj[3].push_back({4, 4}); primMST(V, adj); return 0; }