DSA
    advanced

    Minimum Spanning Tree (Kruskal's Algorithm)

    Software Engineer path · lesson 28 of 99
    time O(E log E)
    space O(V + E)

    Like A-B 4, separated by commas. Up to 9 nodes.

    Try:
    Algorithm

    Both find the same total weight.

    4312576ABCDE
    sortedBC:1BD:2AC:3AB:4CE:5CD:6DE:7
    ConsideringIn the MSTSkipped (cycle)

    Sort the 7 edges by weight: 1, 2, 3, 4, 5, 6, 7.

    Code (running line highlighted)
    1. 1sort edges by weight
    2. 2for (u, v, w) in sorted edges:
    3. 3 if find(u) != find(v): # different components
    4. 4 take the edge; union(u, v)
    5. 5 else: skip — it would close a cycle
    6. 6stop when n - 1 edges are taken
    Edges examined
    0 / 5
    Edges taken
    0 / 4
    Invariant: Taken edges never form a cycle (they are a forest).

    Tap any element in the diagram to inspect its state at this step.

    1 / 12

    Keys: ← → step, Space play/pause. Predict mode pauses before key steps and asks what happens next.

    Kruskal’s algorithm

    Sequence

    Order the steps of building a minimum spanning tree.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    When is Kruskal preferred over Prim?

    The short version

    Imagine connecting all houses in a neighborhood with water pipes. You want to use the minimum total pipe length while ensuring every house gets water. MST finds the cheapest way to connect everyone.

    In more depth

    Two Approaches

    Kruskal's Algorithm:

    1.Sort all edges by weight
    2.For each edge, if it connects two different components (Union-Find), add it
    3.Stop when we have V-1 edges

    Prim's Algorithm:

    1.Start from any node, add to MST
    2.Add all edges from MST nodes to priority queue
    3.Pop minimum edge that leads to a new node, add that node
    4.Repeat until all nodes in MST

    When to Use:

    Kruskal: Sparse graphs, edges already available
    Prim: Dense graphs, when using adjacency matrix

    Key Gotchas:

    MST may not be unique (multiple MSTs possible)
    Graph must be connected (check at end!)
    For disconnected graphs, build MST per component

    When to reach for this

    • Connect all nodes with minimum total edge weight
    • Network/cable/pipe laying problems
    • Keywords: 'minimum cost to connect', 'spanning tree'
    • Any graph edge selection with global minimum goal

    Interview Questions

    Amazon
    Google
    Uber
    Bloomberg
    Google
    Microsoft
    Netflix
    LinkedIn
    Meta
    Palantir
    LinkedIn
    Microsoft

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required