Disjoint-Set Data Structure (Union-Find) | Fast Subset Checking

preview_player
Показать описание


Problem Statement:

### Terms/Facts

- The disjoint-set structure is a series of directed trees (with all nodes pointing towards the root).
- Each directed tree represents a subset.
- Each node represents an item in a subset.
- The root node of any given subset tree is called the **representative** node and is symbolized by `repr(x)` (”the representative node of the subset `x` is contained in).
- If `repr(x) == repr(y)` then `x` & `y` are in the same subset (the same tree).
- The root node loops back to point to itself as its parent (forming a closed loop).

The disjoint-set structure primes us for fast computation over 2 key operations: `FindRepr(x)` & `Union(x, y)`. Though the structure has 3 operations as follows:

- `MakeSet(x)`: Create a new subset with a single item with value `x`
- `FindRepr(x)`: Find the representative node for the subset containing `x`
- (this primes us for quickly seeing if `x` & `y` are in the same subset)
- `Union(x, y)`: Merge the subset containing `x` with the subset containing `y`
- (if `x` & `y` are in the same subset then no action is necessary)

**Applications**

- We aim to avoid connecting 2 nodes already in the same connected component.
- We only want to expand the Minimum Spanning Tree by connecting 2 disjoint components with the next cheapest edge (the algorithm’s goal).

**References**

Table of Contents:
0:00 - Introduction
1:18 - Trees Review
2:47 - Forests
4:08 - The Core Problem
06:18 - Kruskal’s Algorithm Review
08:01 - Disjoint-Set Operations
08:53 - Subset Representation
10:47 - Operations: MakeSet
11:56 - The Representative Node
12:59 - What Are Subsets in Trees?
13:30 - Operations: FindRepr (unoptimized)
14:55 - FindRepr Worst Case
15:24 - Operations: Union (unoptimized)
17:32 - Optimizing Operations
19:22 - Path Compression
21:49 - Weighted Union
25:01 - Complexities
28:00 - Closing

Socials:
Рекомендации по теме
Комментарии
Автор

Excellent explanation and clear language! I'm starting out in Python and wanted to challenge myself. Thank you for the lesson!

bernardoquaresma
Автор

Nice explanation. If it was a little bit slowly, it would be better :)

cleverabbit
Автор

What is the board you use to draw stuff ?

barakap
Автор

Hey Ben! I can't figure out how interview pen differs from backtobackswe. Which product should I get? Can you please guide me?

programming_and_snacks