Union Find Path Compression
Related Videos:
Union find intro: https://www.youtube.com/watch?v=ibjEGG7ylHk
Union find kruskal's algorithm: https://www.youtube.com/watch?v=JZBQLXgSGfs
Union find union and find: https://www.youtube.com/watch?v=0jNmHPfA_yE
Union find path compression: https://www.youtube.com/watch?v=VHRhJWacxis
Union find code: https://www.youtube.com/watch?v=KbFlZYCpONw
Data Structures Source Code:
https://github.com/williamfiset/algorithms
My website: http://www.williamfiset.com ===================================
Practicing for interviews? I have used, and recommend `Cracking the Coding Interview` which got me a job at Google. Link on Amazon: https://amzn.to/3cvMof5
A lot of the content on this channel is inspired by the book `Competitive Programming` by Steven Halim which I frequently use as a resource and reference. Link on Amazon: https://amzn.to/3wC2nix
What You'll Learn
The video discusses the Union Find data structure with path compression, a technique that improves the efficiency of the Union Find operations by compressing the paths to the root node. The speaker explains how path compression works and provides examples to illustrate its benefits.
Full Transcript
let's talk about path compression now this operation is really what makes the UN find one of the most remarkable data structures because it's how the union find gets to boast in its efficiency so let's Dive Right In but before we get started it's critical that you watch the last video so that you understand how the find and the union operation work otherwise you won't understand what's going on and what's up with path compression and how we're going to get that really nice amortized constant time all right suppose we have this hypothetical Union find I say hypothetical because with path compression I'm almost certain it's impossible to achieve a data structure or a structure that looks like this nonetheless it's a good example so now suppose we want to unify nodes uh e and l or just unify groups uh orange and blue but we have access to enl and that's what we're calling the unify operation on well we would have two pointers that start on enl and what we would want to do is find the root node of e and find the root node of L and then uh get one of them to point to the other but with path compression we do that but we're also going to do something else so let's start by finding the parent node of e so E's parent is uh D and then D's is c c to B uh and B to a and then a to F so we found the root node of e but with path compression here's what we're going to do now that we have a reference to the root node we're going to make make e point to the root node and similarly D is going to point to the root node and c and b and a so now everything along the path got compressed and now points to that root node and in doing so every time we do a lookup on either a b c d or e in constant time we will be able to find out what the parent or or the root node is for that component because we immediately point to it we don't have to Traverse a sequence of other nodes and we can do this because in a union find we're always unifying things together and making them more and more compressed we're never un unifying things if you will so if we do the same thing for L we find L's parent so we Traverse up all the parents until we find the root and then we compress the path so uh J points to g i points to g h points to G and and so we compress that path but we also have found the parents now so make one point to the other and we've unified both groups so now the group that was once with E and once with L have now been merged into one single group but the only difference is we've compressed along the way as we've done this and now it's so much more efficient now let's have a look at another example and this one I want to compare and contrast the the regular Union find operations we were doing the last video to the path compression version uh we now know so if I run all those Union instructions uh this is what would happen so I start by getting all these pairs of components and then now I'm executing the instructions on the right and this is the final state of our Union find and note that if I'm trying to determine what groups say A and J are in then I have to Traverse a whole bunch of different nodes so J goes to I I goes to h h goes to e but now if I include path compression let's see what happens so I still get all those components but now as I execute the instruction on the right hand side uh this is what happens so I get the green group but then because of path compression that J merged into the H so already that path is a little shorter and then I keep executing more instructions but as I'm doing it uh the path gets compressed dynamically so so I'm getting more and more compression and even up to the very end so on the last example we haven't even finished all the instructions and we have reached the final state but with path compression as long as there's something to compress along our path we get to compress the path along the way pointing it to the root so right now we only have one root being e and almost everything in constant time points to uh e so we do a look up on any of our nodes and we know that the root is e so we know it belongs to that component and this structure becomes very stable eventually uh because of this uh path compression this is why the unit find with path compression is so efficient so I hope you guys enjoyed this video and I will be going over an implementation of the Union find uh you can find it on my GitHub repo at github.com Willam FUSD structures and I will all go be going over in the next video so don't worry about it and we'll look at path compression and the union and find operations I also mentioned guys thanks for watching and I'll catch you next time
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from WilliamFiset · WilliamFiset · 48 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
▶
49
50
51
52
53
54
55
56
57
58
59
60
JES Image Manipulation - 2 - Installation
WilliamFiset
JES Image Manipulation - 3 - User Interface
WilliamFiset
JES Image Manipulation - 5 - Negative
WilliamFiset
JES Image Manipulation - 6 - Black & White
WilliamFiset
JES Image Manipulation - 4 - Grayscale
WilliamFiset
JES Image Manipulation - 8 - Blur
WilliamFiset
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
JES Image Manipulation - 9 - Blend
WilliamFiset
JES Image Manipulation - 10 - Matte
WilliamFiset
JES Image Manipulation - 13 - Rotate90
WilliamFiset
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
JES Image Manipulation - 11 - Crop Image
WilliamFiset
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
Java Fractal Explorer [6/8]
WilliamFiset
Java Fractal Explorer [4/8]
WilliamFiset
Java Fractal Explorer [8/8]
WilliamFiset
Java Fractal Explorer [5/8]
WilliamFiset
Java Fractal Explorer [2/8]
WilliamFiset
Java Fractal Explorer [7/8]
WilliamFiset
Java Fractal Explorer [1/8]
WilliamFiset
Java Fractal Explorer [3/8]
WilliamFiset
Introduction [Programming Competition Problems]
WilliamFiset
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
Logic 1 [Programming Competition Problems]
WilliamFiset
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
Introduction to Big-O
WilliamFiset
Dynamic and Static Arrays
WilliamFiset
Dynamic Array Code
WilliamFiset
Linked Lists Introduction
WilliamFiset
Doubly Linked List Code
WilliamFiset
Stack Introduction
WilliamFiset
Stack Implementation
WilliamFiset
Stack Code
WilliamFiset
Queue Introduction
WilliamFiset
Queue Implementation
WilliamFiset
Queue Code
WilliamFiset
Priority Queue Introduction
WilliamFiset
Priority Queue Min Heaps and Max Heaps
WilliamFiset
Priority Queue Inserting Elements
WilliamFiset
Priority Queue Removing Elements
WilliamFiset
Priority Queue Code
WilliamFiset
Union Find Introduction
WilliamFiset
Union Find Kruskal's Algorithm
WilliamFiset
Union Find - Union and Find Operations
WilliamFiset
Union Find Path Compression
WilliamFiset
Union Find Code
WilliamFiset
Binary Search Tree Introduction
WilliamFiset
Binary Search Tree Insertion
WilliamFiset
Binary Search Tree Removal
WilliamFiset
Binary Search Tree Traversals
WilliamFiset
Binary Search Tree Code
WilliamFiset
Fenwick Tree range queries
WilliamFiset
Fenwick Tree point updates
WilliamFiset
Fenwick Tree construction
WilliamFiset
Fenwick tree source code
WilliamFiset
Hash table hash function
WilliamFiset
Hash table separate chaining
WilliamFiset
More on: Graph Algorithms
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Bloom Filters, Explained Properly
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Medium · Python
🎓
Tutor Explanation
DeepCamp AI