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

Visit Our Website: https://interviewpen.com/?utm_campaig... Join Our Discord:   / discord   Join Our Newsletter - The Blueprint: https://theblueprint.dev/subscribe Like & Subscribe:    / @interviewpen   This is an example of a full video available on interviewpen.com. Check out our website to find more premium content like this! Problem Statement: A [disjoint-set data structure](https://en.wikipedia.org/wiki/Disjoin...) (also called a “union-find” data structure), stores elements of a [set](https://en.wikipedia.org/wiki/Set_(ab...) as a series of trees (a “disjoint set [forest](https://en.wikipedia.org/wiki/Tree_(g...) representing disjoint subsets. 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* *[Kruskal’s Algorithm](https://en.wikipedia.org/wiki/Kruskal... A disjoint-set data structure allows us to quickly check if adding a candidate edge will create a cycle. 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* [Notes](https://www.math.umd.edu/~immortal/CM...) (by Justin Wyss-Gallifent) Table of Contents: 0:00 - Introduction 1:18 - Trees Review 2:47 - Forests 3:48 - Visit interviewpen.com 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: Twitter:   / interviewpen   Twitter (The Blueprint):   / theblueprintdev   LinkedIn:   / interviewpen   Website: https://interviewpen.com/?utm_campaig...