Snapdeal - Competitive Exam Books: Buy Entrance Exams Preparation Books Online
Written by Code Blocks
1. MST_PRIM(G, cost, root)
2. for each vertex u ∈ V[G]
3. do Key[u] <- ∞
4. Pred[u] <- NIL
5. Key[root] <- 0
6. Q <- V[G] // create a priority queue of the vertices
7. while Q ≠ φ
8. do u <- EXTRACT_MIN(Q)
9. for each v ∈ Adj[u]
10. do if v ∈ Q & (cost(u,v) < Key[v])
11. then Pred[v] <- u
12. Key[v] <- cost(u,v)
Key[v] -> minimum weight of any edge connecting v to a vertex in the tree
Pred[v] -> parent of v in the tree
Q -> min priority queue of all the vertices that are not in the tree
Initially, A = { (v, Pred[v]) : v ∈ V - {root} - Q }
Finally, A = { (v, Pred[v] : v ∈ V - {root} } // Q is empty