Algorithms · Visualized

Kruskal's MST

Graph Algorithms

Traverse, order, and find shortest paths and spanning trees on graphs — the machinery behind maps, networks, schedulers and dependency resolvers.

Kruskal builds the MST by scanning all edges from lightest to heaviest and accepting any edge whose endpoints are still in different components — an edge inside the same component would close a cycle, so it is rejected. Time: O(E log E) average, O(E log E) worst; space: O(V) for the union-find parent array.

Big-O: Traversals O(V + E); Dijkstra O(E log V) with a heap; Bellman–Ford O(V·E); Floyd–Warshall O(V³); MST O(E log E).

Kruskal's Minimum Spanning Tree

Graph Algorithms

Kruskal builds the MST by scanning all edges from lightest to heaviest and accepting any edge whose endpoints are still in different components — an edge inside the same component would close a cycle, so it is rejected. A union-find (disjoint set) answers 'same component?' in near-constant time, making the whole algorithm O(E log E).

How Kruskal's MST Works

  1. Sort every edge by weight, ascending.
  2. Put each node in its own component (union-find with itself as parent).
  3. Walk the sorted edges: find the roots of both endpoints.
  4. If the roots differ, accept the edge, add its weight, and union the components.
  5. If the roots match, reject — the edge would create a cycle. Stop after V−1 acceptances.

Kruskal's MST Worked Example

Input: {graph: A—B(4), A—C(2), B—C(1), B—D(5), C—D(8), C—E(10), D—E(2), D—F(6), E—F(3), start: —}

Sorted: B—C(1), A—C(2), D—E(2), E—F(3), A—B(4), B—D(5), D—F(6), C—D(8), C—E(10). Accept B—C, A—C, D—E, E—F; reject A—B (cycle A-C-B); accept B—D → 5 edges. Total 13, same tree as Prim found.

Kruskal's MSTO(E log E)
Sort edges and accept any edge that joins two different components (union-find).
0
0
1
1
2
2
3
3
4
4
5
5
4215810263ABCDEF
sorted edges9
mst weight0
Step 1 / 8
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Sort all edges by weight: B-C(1), A-C(2), D-E(2), E-F(3), A-B(4), B-D(5), D-F(6), C-D(8), C-E(10).
Python · Graph Algorithms in Code
def kruskal_mst(n, edges):
    parent = list(range(n))

    def find(x):
        while parent[x] != x:
            parent[x] = parent[parent[x]]   # path compression
            x = parent[x]
        return x

    total = 0
    for u, v, w in sorted(edges, key=lambda e: e[2]):
        ru, rv = find(u), find(v)
        if ru != rv:                        # different components: accept
            parent[ru] = rv
            total += w
    return total

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Kruskal's MST Time & Space Complexity

BestO(E log E)
AverageO(E log E)
WorstO(E log E)
SpaceO(V) for the union-find parent array

When to Use Kruskal's MST

  • Sparse or edge-list graphs — Kruskal needs no adjacency structure, just a sorted edge list.
  • Clustering: stopping Kruskal early (single-link clustering) groups data instead of connecting it.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.