Introduction
Kruskal's Algorithm: Kruskal's Algorithm is a greedy algorithm used to find the Minimum Spanning Tree (MST) of a connected, weighted graph. It ensures that the total weight of the MST is minimized.
This algorithm is widely used in network design, such as designing least-cost networks.
How does it work?
- Sort all the edges of the graph in non-decreasing order of their weights.
- Initialize an empty set to store the edges of the MST.
- Iterate through the sorted edges and add an edge to the MST if it does not form a cycle with the edges already in the MST.
- Use a union-find data structure to efficiently check for cycles.
- Repeat the process until the MST contains exactly (V - 1) edges, where V is the number of vertices.
Important Observations
- The graph must be connected for the algorithm to work correctly.
- The algorithm guarantees the MST for weighted graphs.
- It uses a greedy approach to select the smallest edge at each step.
Key Characteristics:
- Greedy algorithm for MST calculation.
- Efficient for sparse graphs.
- Works for both directed and undirected graphs.
Advantages:
- Simple and easy to implement.
- Efficient for graphs with fewer edges.
- Uses union-find for cycle detection, which is efficient.
Disadvantages:
- Sorting edges can be computationally expensive for dense graphs.
- Not suitable for dynamic graphs where edges are frequently added or removed.
Time Complexity:
- Using union-find with path compression: O(E log V), where E is the number of edges and V is the number of vertices.