Kruskal Algorithm

code data_structures algorithms
Practice Quiz →
Figure: Kruskal’s algorithm for finding Minimum Spanning Tree (MST)

Kruskal’s algorithm is a method for finding a Minimum Spanning Tree (MST) in a weighted graph. It works by looking at all edges in order from smallest weight to largest, and adding an edge to the tree only if it does not form a cycle with the edges already chosen. To check this, the algorithm uses a disjoint-set (union–find) structure to keep track of which nodes are already connected. The process continues until the tree has V − 1 edges, which is the minimum needed to connect all vertices. The final result is the set of cheapest connections that link the entire graph.

Figure: Kruskal MST Game: Be Greedy, Avoid Cycles

Build a Minimum Spanning Tree (MST). Each edge has a weight (cost). Drag edges into the MST zone using a greedy strategy: always try to take cheaper edges first, but never create a cycle (a closed loop). If adding an edge would complete a loop, you must skip it—this is exactly how Kruskal’s algorithm works: sort edges by weight, add the lightest edges that don’t form a cycle, and stop once all vertices are connected.

💬 Click on the content to Ask AI
algorithm graph_algorithm greedy_algorithm mst spanning_tree undirected_graph weighted_graph combinatorial_optimization