All lessons in Software Engineering: Programs, Systems, and Maintainable Software

Union-Find and Connectivity

Maintain connected components with weighted union and path compression.

35 minutes · Free with an account · preview

What you will learn

  • Maintain connected components using parent links and component sizes.
  • Trace weighted union and path compression without confusing a representative with a member list.
  • Identify connectivity questions that union-find does not answer.

Several library branches share delivery arrangements. Each new agreement connects two existing groups, and the system repeatedly asks whether two branches are connected through any chain of agreements. Recomputing a full graph traversal after every addition may do unnecessary work when the only required result is connectivity.

Union-find, also called a disjoint-set structure, maintains a partition of elements into nonoverlapping components. It supports finding a representative and merging two components. It does not retain every path that established their connection.

Choose another lesson