Minimum Spanning Tree Algorithms
A Minimum Spanning Tree (MST) of a connected, undirected graph is a spanning tree that has the smallest possible total edge weight among all spanning trees. An MST connects all vertices in the graph without cycles and with the minimum sum of edge weights.
Video Explanation

Characteristics:
-
Optimal Substructure:
MST exhibits optimal substructure, meaning that an MST of a graph contains the MSTs of its subgraphs. -
Greedy Approach:
Both Prim's and Kruskal's algorithms use a greedy strategy to find the MST. They build the MST by adding edges in a way that minimizes the total weight while ensuring no cycles are formed.
Prim's Algorithm:
-
Initialization:
Start with an arbitrary vertex and mark it as part of the MST. Initialize a priority queue (min-heap) to keep track of the edges. -
Edge Selection:
Continuously extract the edge with the smallest weight from the priority queue that connects a vertex in the MST to a vertex outside the MST. Add this edge to the MST and mark the new vertex as part of the MST. -
Repeat:
Repeat the process until all vertices are included in the MST.
Time Complexity of Prim's Algorithm:
- Time Complexity:
The complexity arises from maintaining the priority queue for the edges, where E is the number of edges and V is the number of vertices.
Kruskal's Algorithm:
-
Edge Sorting:
Start by sorting all the edges in the graph in ascending order based on their weights. -
Union-Find Structure:
Use a union-find (disjoint-set) data structure to keep track of connected components. -
Edge Selection:
Iterate through the sorted edge list, adding edges to the MST if they do not form a cycle (i.e., if they connect two different components). -
Repeat:
Continue until the MST contains exactly (V-1) edges, where V is the number of vertices.
Time Complexity of Kruskal's Algorithm:
- Time Complexity:
The complexity comes from sorting the edges and performing union-find operations.
Example:
Consider the following graph with vertices and weighted edges: