Interview concept

What is Union-Find (disjoint set union)?

Union-Find, also called disjoint set union, maintains a collection of disjoint sets with two operations: find, which returns a set representative, and union, which merges two sets. Path compression and union by rank make it near-constant amortized time.

Interview concept

How it works

Core operations

Find returns the root of an element, and union merges two trees under a single root.

Optimizations

Path compression flattens trees and union by rank keeps them shallow, giving near-constant amortized time.

When to use it

Use it for connectivity, cycle detection in undirected graphs, and Kruskal minimum spanning trees.

Interview concept

Responsible use

Use any live AI assistant only where interview, workplace, school, and platform rules allow it. Do not use generated answers to misrepresent your skills, experience, or authorship.

FAQ

Common questions.

What problems use Union-Find?

Connected components, cycle detection in undirected graphs, and building minimum spanning trees with Kruskal.

How fast is Union-Find?

With path compression and union by rank, operations run in near-constant amortized time, the inverse Ackermann function.