"Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a connected weighted undirected graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. The algorithm was developed in 1930 by Czech mathematician Vojtěch Jarník and later independently by computer scientist Robert C. Prim in 1957 and rediscovered by Edsger Dijkstra in 1959. Therefore it is also sometimes called the DJP algorithm, the Jarník algorithm, or the Prim–Jarník algorithm." - Wikipedia
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
References :
- Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein (Available @ Flipkart)
Written by Munia Balayil
No comments:
Post a Comment