Lesson 20 — Union-Find
Union-Find: who is in the same group?
Union-Find (also called disjoint-set) tracks groups that merge over time. Every item points to a parent; the item at the top of the chain (whose parent is itself) is the group's root. find(x) climbs to the root. union(a, b) joins two groups by pointing one root at the other. Two items are connected if they have the same root.
parent = [0, 0, 2] # 1 points to 0; 0 and 2 are roots
Why it matters: it answers "are these connected?" almost instantly — used for network connectivity, image regions and Kruskal's minimum spanning tree.
Finish union(a, b): find both roots and, if they differ, point one at the other. Expected: True, False, 3 groups.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based Data Structures courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for Data Structures books — check the publication year so the examples match current versions.
Search Amazon →