DSA
    intermediate

    Union-Find (Disjoint Set)

    Software Engineer path · lesson 29 of 99
    time O(α(n))
    space O(n)

    Each A-B means union(A, B), applied in order. Repeats are allowed.

    Try:
    Path compression
    Union by rank

    Turn both off on the Chain preset and watch pointer hops and tree height grow.

    Aroot r0Broot r0Croot r0Droot r0Eroot r0Froot r0Groot r0Hroot r0
    RootOn a find pathRoot foundRe-pointed

    8 separate sets: every node is its own root.

    Code (running line highlighted)
    1. 1find(x):
    2. 2 while parent[x] != x: x = parent[x] # walk to the root
    3. 3 (compression: point every node on the walk at the root)
    4. 4union(a, b):
    5. 5 ra, rb = find(a), find(b)
    6. 6 if ra == rb: return # already connected
    7. 7 attach the lower-rank root under the other (or rb under ra)
    Pointer hops
    0 / 9
    Unions
    0 / 7
    Invariant: Each set is a tree; two nodes are connected exactly when they share a root.

    Sets: 8

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

    1 / 36

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

    union(x, y)

    Sequence

    Order the steps of union by rank with path compression.

    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
    4 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    What is the primary optimization that makes Union-Find nearly constant time?

    Write the code

    Runs your solution

    Number of Provinces

    Given an `isConnected` matrix, return the total number of provinces (groups of connected cities).

    Example Cases

    Input:[[[1,1,0],[1,1,0],[0,0,1]]]
    Output:2
    Input:[[[1,0,0],[0,1,0],[0,0,1]]]
    Output:3
    javascript
    function findCircleNum(isConnected) {
      // Your code here
    }

    The short version

    Think of social networks. When person A befriends person B, their entire groups of friends merge into one giant community. You can quickly check if two random people are in the same extended network.

    In more depth

    Disjoint Sets

    Union-Find is a data structure that tracks a set of elements partitioned into a number of disjoint (non-overlapping) subsets.

    Operations:

    1.Find: Which group does element X belong to?
    2.Union: Merge the groups containing elements X and Y.

    Optimizations:

    Path Compression: Evaluating 'find' flattens the tree structure.
    Union by Rank/Size: Always attach the smaller tree to the larger one.

    When to reach for this

    • Find connected components in a graph
    • Check if two nodes are connected (fast)
    • Cycle detection in undirected graphs
    • Kruskal's Algorithm for MST

    Interview Questions

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

    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