IntermediateAbout 20 min

    Union-Find (Disjoint Set)

    Read the full article on Substack

    6 min read.

    Master the Union-Find data structure. Essential for Kruskal's Algorithm, connected components, and cycle detection in undirected graphs.

    Think of it this way: 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.

    Aroot r0Broot r0Croot r0Droot r0Eroot r0Froot r0Groot r0Hroot r0

    8 separate sets: every node is its own root.

    1/36

    Check your understanding

    Answer from memory before you scroll back up. Pulling an idea out of your head is what makes it stay there.

    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

    Quiz

    20 questions
    Question 1 of 20
    Score: 0

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

    Done with this lesson?

    Sign in free to keep your progress, streak and quiz scores.

    Keep going