50. Minimum Spanning Trees (MST): Prim's Algorithm Concept
A Spanning Tree of a graph is a subgraph that connects all the vertices together, without any cycles. A Minimum Spanning Tree (MST) is a spanning tree where the sum of the weights of the edges is minimized.
Prim's Algorithm (Greedy Approach)
Prim's algorithm builds the MST iteratively by adding edges that connect a new vertex to the growing tree set. It is a local optimization strategy.
- Start with an arbitrary source vertex
Sin the MST. - Repeat V-1 times:
- Select the edge (U, V) that connects a vertex
Ualready in the MST to an unvisited vertexVsuch that the weight of the edge (U, V) is minimal. - Add this minimum-weight edge and vertex
Vto the MST.
- Select the edge (U, V) that connects a vertex
Prim's is implemented efficiently using a Min Heap (Priority Queue), much like Dijkstra's, to quickly select the next smallest edge.