๐ŸŒ‰ Kruskalโ€™s Algorithm โ€“ Complete In-Depth Explanation

**Kruskalโ€™s Algorithm** is a **greedy algorithm** used to find the **Minimum Spanning Tree (MST)** of a weighted, **connected, and undirected graph**. The **MST** is a subset of the graphโ€™s edges that connects all vertices with the **minimum total edge weight** and contains **no cycles**.

๐Ÿ“Œ Why Use Kruskalโ€™s Algorithm?

  • โœ… **Greedy approach ensures global optimization.**
  • ๐Ÿ“ก **Works well with sparse graphs (O(E log E) time complexity).**
  • ๐Ÿ” **Finds the Minimum Spanning Tree efficiently.**
  • ๐ŸŒ **Used in network design, clustering, and image segmentation.**

๐Ÿ“Œ Understanding the Minimum Spanning Tree (MST)

A **Minimum Spanning Tree (MST)** is a **subset of a graphโ€™s edges** that connects all vertices together with the **minimum total edge weight** and **without forming cycles**.

๐Ÿ”น **Properties of an MST**

  • โœ… The MST has **V - 1 edges** (where V is the number of vertices).
  • ๐Ÿ“‰ The MST has the **minimum total edge weight**.
  • ๐Ÿ”„ The MST contains **no cycles**.
  • ๐ŸŒ The MST **may not be unique** (multiple MSTs may exist).

๐Ÿ›  Steps in Kruskalโ€™s Algorithm

๐Ÿ“Œ **Step 1: Sort All Edges by Weight**

Sort all edges in **ascending order** based on their weight.

๐Ÿ“Œ **Step 2: Initialize the MST**

Start with an empty set of edges in the MST.

๐Ÿ“Œ **Step 3: Add the Minimum Edge Without Forming a Cycle**

Use the **Disjoint Set Union (DSU)** (Union-Find) data structure to check for cycles.

๐Ÿ“Œ **Step 4: Stop When MST Has (V - 1) Edges**

Once the MST has **V - 1 edges**, the algorithm stops.

๐Ÿ“œ Example Graph


                    Graph:
                       2
                    A-----B
                    |     |
                  1 |     | 3
                    |     |
                    C-----D
                       4
            
                    Sorted Edges by Weight: 
                    (C-A, 1), (A-B, 2), (B-D, 3), (C-D, 4)
            
                    MST Output:
                       2
                    A-----B
                    |      
                  1 |      
                    |      
                    C      
                

๐Ÿ“Œ Implementation of Kruskalโ€™s Algorithm (O(E log E))


                struct Edge {
                    int src, dest, weight;
                };
            
                struct Graph {
                    int V, E;
                    vector edges;
                };
            
                struct DisjointSet {
                    vector parent, rank;
            
                    DisjointSet(int n) {
                        parent.resize(n);
                        rank.resize(n, 0);
                        for (int i = 0; i < n; i++) parent[i] = i;
                    }
            
                    int find(int x) {
                        if (parent[x] != x) parent[x] = find(parent[x]);
                        return parent[x];
                    }
            
                    void unite(int x, int y) {
                        int rootX = find(x), rootY = find(y);
                        if (rootX != rootY) {
                            if (rank[rootX] > rank[rootY]) parent[rootY] = rootX;
                            else if (rank[rootX] < rank[rootY]) parent[rootX] = rootY;
                            else {
                                parent[rootY] = rootX;
                                rank[rootX]++;
                            }
                        }
                    }
                };
            
                void kruskal(Graph &graph) {
                    sort(graph.edges.begin(), graph.edges.end(), [](Edge a, Edge b) {
                        return a.weight < b.weight;
                    });
            
                    DisjointSet ds(graph.V);
                    vector mst;
                    int mstCost = 0;
            
                    for (Edge e : graph.edges) {
                        if (ds.find(e.src) != ds.find(e.dest)) {
                            ds.unite(e.src, e.dest);
                            mst.push_back(e);
                            mstCost += e.weight;
                        }
                    }
            
                    cout << "Minimum Spanning Tree Cost: " << mstCost << endl;
                    for (Edge e : mst) {
                        cout << e.src << " -- " << e.dest << " == " << e.weight << endl;
                    }
                }
                

โณ Time Complexity Analysis

Step Time Complexity
Sorting Edges O(E log E)
Union-Find (DSU) Operations O(E log V)
Total Complexity O(E log E)

๐ŸŒ Real-World Applications of Kruskalโ€™s Algorithm

  • ๐Ÿ“ก **Network Design** โ€“ Laying down **minimum-cost communication cables.**
  • ๐Ÿš— **Road Systems** โ€“ Designing **minimum-cost road networks.**
  • ๐Ÿ”— **Computer Clustering** โ€“ Connecting **servers with minimal latency.**
  • ๐Ÿ›ฐ **Electric Grid Design** โ€“ Optimizing **power transmission networks.**

๐Ÿ”— Next Topics