|
| 1 | +/* |
| 2 | +Steps |
| 3 | +1) Initialize keys of all vertices as infinite and |
| 4 | + parent of every vertex as -1. |
| 5 | +
|
| 6 | +2) Create an empty priority_queue pq. Every item |
| 7 | + of pq is a pair (weight, vertex). Weight (or |
| 8 | + key) is used used as first item of pair |
| 9 | + as first item is by default used to compare |
| 10 | + two pairs. |
| 11 | +
|
| 12 | +3) Initialize all vertices as not part of MST yet. |
| 13 | + We use boolean array inMST[] for this purpose. |
| 14 | + This array is required to make sure that an already |
| 15 | + considered vertex is not included in pq again. This |
| 16 | + is where Ptim's implementation differs from Dijkstra. |
| 17 | + In Dijkstr's algorithm, we didn't need this array as |
| 18 | + distances always increase. We require this array here |
| 19 | + because key value of a processed vertex may decrease |
| 20 | + if not checked. |
| 21 | +
|
| 22 | +4) Insert source vertex into pq and make its key as 0. |
| 23 | +
|
| 24 | +5) While either pq doesn't become empty |
| 25 | + a) Extract minimum key vertex from pq. |
| 26 | + Let the extracted vertex be u. |
| 27 | +
|
| 28 | + b) Include u in MST using inMST[u] = true. |
| 29 | +
|
| 30 | + c) Loop through all adjacent of u and do |
| 31 | + following for every vertex v. |
| 32 | +
|
| 33 | + // If weight of edge (u,v) is smaller than |
| 34 | + // key of v and v is not already in MST |
| 35 | + If inMST[v] = false && key[v] > weight(u, v) |
| 36 | +
|
| 37 | + (i) Update key of v, i.e., do |
| 38 | + key[v] = weight(u, v) |
| 39 | + (ii) Insert v into the pq |
| 40 | + (iv) parent[v] = u |
| 41 | +
|
| 42 | +6) Print MST edges using parent array. |
| 43 | +*/ |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +#include<bits/stdc++.h> |
| 51 | +using namespace std; |
| 52 | + |
| 53 | +#define INF INT_MAX |
| 54 | +typedef pair<int, int> iPair; |
| 55 | + |
| 56 | +void adjListGraph(vector<pair<int, int> > adj[], int s, int d, int w){ |
| 57 | + // undirected graph |
| 58 | + adj[s].push_back({d, w}); |
| 59 | + adj[d].push_back({s, w}); |
| 60 | +} |
| 61 | + |
| 62 | +void prims(vector<pair<int, int> > adj[], int V){ |
| 63 | + priority_queue<iPair, vector<iPair>, greater<iPair> > pq; // min heap |
| 64 | + int src=0; |
| 65 | + vector<int> key(V, INF); // weights |
| 66 | + vector<int> parent(V, -1); // u ----> v edge then u is parent of v |
| 67 | + vector<bool> inMST(V,false); // keep track of vertices included in MST |
| 68 | + |
| 69 | + pq.push({0, src}); // insert source itself in priority queue |
| 70 | + key[src]=0; // and initialize its key as 0 |
| 71 | + |
| 72 | + while(!pq.empty()){ |
| 73 | + int u=pq.top().second; // pait< key, vertex> |
| 74 | + pq.pop(); |
| 75 | + inMST[u]=true; |
| 76 | + for(auto x: adj[u]){ |
| 77 | + int v=x.first; // vector<vertex, weight> |
| 78 | + int w=x.second; |
| 79 | + |
| 80 | + // If v is not in MST and weight of (u,v) is smaller than current key of v |
| 81 | + if(inMST[v]==false && w<key[v]){ |
| 82 | + key[v]=w; |
| 83 | + pq.push({key[v], v}); |
| 84 | + parent[v]=u; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + cout<<"Edge Weight"<<endl; |
| 89 | + for(int i=1;i<V;i++){ |
| 90 | + cout<<parent[i]<<" - "<<i<<" "<<key[i]<<endl; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +int main(){ |
| 95 | + ios_base::sync_with_stdio(false); |
| 96 | + cin.tie(NULL); |
| 97 | + int v,e; |
| 98 | + cin>>v>>e; |
| 99 | + vector<pair<int, int> > adj[v]; |
| 100 | + while(e--){ |
| 101 | + int s,d,w; |
| 102 | + cin>>s>>d>>w; |
| 103 | + adjListGraph(adj,s,d,w); |
| 104 | + } |
| 105 | + prims(adj, v); |
| 106 | +} |
0 commit comments