1
1
/*
2
2
Steps
3
- 1) Initialize keys of all vertices as infinite and
4
- parent of every vertex as -1 .
3
+ 1) Initialize keys of all vertices as infinite and parent of every vertex as -1.
4
+ For ie; for u---> v, Parent stores the u and v is the index of Parent array .
5
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.
6
+ 2) Create an empty priority_queue pq.
7
+ Every item of pq is a pair (weight, vertex).
8
+ Weight aka key is used as first item of pair as first item is by default used to compare two pairs.
11
9
12
10
3) Initialize all vertices as not part of MST yet.
13
11
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.
12
+ This array is required to make sure that an already considered vertex is not included in pq again.
13
+ This is where Prim's implementation differs from Dijkstra.
14
+ In Dijkstra's algorithm, we don't need this array as distances always increase.
15
+ We require this array here because key value of a processed vertex may decrease if not checked.
21
16
22
17
4) Insert source vertex into pq and make its key as 0.
23
18
@@ -27,14 +22,13 @@ Steps
27
22
28
23
b) Include u in MST using inMST[u] = true.
29
24
30
- c) Loop through all adjacent of u and do
31
- following for every vertex v.
25
+ c) Loop through all adjacent of u and do following for every vertex v.
32
26
33
27
// If weight of edge (u,v) is smaller than
34
28
// key of v and v is not already in MST
35
- If inMST[v] = false && key[v] > weight(u, v)
29
+ If inMST[v] == false && key[v] > weight(u, v)
36
30
37
- (i) Update key of v, i.e. , do
31
+ (i) Update key of v, ie , do
38
32
key[v] = weight(u, v)
39
33
(ii) Insert v into the pq
40
34
(iv) parent[v] = u
@@ -59,9 +53,8 @@ void adjListGraph(vector<pair<int, int> > adj[], int s, int d, int w){
59
53
adj[d].push_back ({s, w});
60
54
}
61
55
62
- void prims (vector<pair<int , int > > adj[], int V){
56
+ void prims (vector<pair<int , int > > adj[], int V, int src ){
63
57
priority_queue<iPair, vector<iPair>, greater<iPair> > pq; // min heap
64
- int src=0 ;
65
58
vector<int > key (V, INF); // weights
66
59
vector<int > parent (V, -1 ); // u ----> v edge then u is parent of v
67
60
vector<bool > inMST (V,false ); // keep track of vertices included in MST
@@ -70,7 +63,7 @@ void prims(vector<pair<int, int> > adj[], int V){
70
63
key[src]=0 ; // and initialize its key as 0
71
64
72
65
while (!pq.empty ()){
73
- int u=pq.top ().second ; // pait < key, vertex>
66
+ int u=pq.top ().second ; // pair < key, vertex>
74
67
pq.pop ();
75
68
inMST[u]=true ;
76
69
for (auto x: adj[u]){
@@ -102,5 +95,7 @@ int main(){
102
95
cin>>s>>d>>w;
103
96
adjListGraph (adj,s,d,w);
104
97
}
105
- prims (adj, v);
98
+ prims (adj, v, 0 ); // <adj, v, source>
99
+
100
+ return 0 ;
106
101
}
0 commit comments