PythiloLesson 20 of 32 · progress kept in this browser

Join sets quickly with union-find

Union-find tracks which items belong together, such as connected computers or merged accounts. Each item points to a parent; following parents reaches a root that names the whole set.

parent = {x: x for x in "abc"}
parent["b"] = "a"      # b joins a's set
Your task

Write a UnionFind class built from an iterable of items, with find(x), union(a, b) and connected(a, b). Use path compression in find.