1. Union-Find & Applications

WIP: I am still migrating the notes to this site
Table of Contents

1. Dynamic Connectivity

Simple connected components
Simple connected components

1.1 Problem statement

Given a set of objects and connections, find whether two objects are connected.

A connection between objects can be transitive, i.e object $p$ can be connected $s$ if $p$ is connected to $q$ and $q$ is connected to $s$.


The following are the expected basic operators required for the desired data structure:

  1. connected() -> bool: Check if two objects are in the same component.
  2. union() -> None: adds a connection between two objects.

Therefore the goal can be summarized as following:

Goal: Design efficient data structure for to check the connectivity between two objects (union-find).


  1. Number of objects N can be huge
  2. Number of operations M can be huge
  3. Find queries and union commands may be intermixed

1.2 Modeling the objects

The fundamental aspect of designing the data structure is to suppress details that are not relevant to union-find.

There are several way to achieve this goal, however the simplest method is to model Objects as integers. These integers are used as an array index. Another method is to use a symbol table to translate from objects to integers.

1.3 Modeling the connections

A connection is assumed to be an equivalence relation:

  • Reflexive: $p$ is connected to $p$.
  • Symmetric: if $p$ is connected to $q$, then $q$ is connected to $p$.
  • Transitive: if $p$ is connected to $q$ and $q$ is connected to $r$, then $p$ is connected to $r$.

Connected components: Set of objects that are mutually connected.

2. Quick Find

3. Quick Union

4. Improvements

4.1 Weighted Quick Union

4.2 Weighted Quick Union with path compression

5. Applications