Union Find Path Compression

WilliamFiset · Beginner ·⚡ Algorithms & Data Structures ·9y ago
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 JES Image Manipulation - 2 - Installation
JES Image Manipulation - 2 - Installation
WilliamFiset
2 JES Image Manipulation - 3 - User Interface
JES Image Manipulation - 3 - User Interface
WilliamFiset
3 JES Image Manipulation - 5 - Negative
JES Image Manipulation - 5 - Negative
WilliamFiset
4 JES Image Manipulation - 6 - Black & White
JES Image Manipulation - 6 - Black & White
WilliamFiset
5 JES Image Manipulation - 4 - Grayscale
JES Image Manipulation - 4 - Grayscale
WilliamFiset
6 JES Image Manipulation - 8 - Blur
JES Image Manipulation - 8 - Blur
WilliamFiset
7 JES Image Manipulation - 7 - Edge Detection
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
8 JES Image Manipulation - 9 - Blend
JES Image Manipulation - 9 - Blend
WilliamFiset
9 JES Image Manipulation - 10 - Matte
JES Image Manipulation - 10 - Matte
WilliamFiset
10 JES Image Manipulation - 13 - Rotate90
JES Image Manipulation - 13 - Rotate90
WilliamFiset
11 JES Image Manipulation - 12 - Mirroring Picture
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
12 JES Image Manipulation - 11  - Crop Image
JES Image Manipulation - 11 - Crop Image
WilliamFiset
13 JES Image Manipulation - 14 - Stretch picture
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
14 Java Fractal Explorer [6/8]
Java Fractal Explorer [6/8]
WilliamFiset
15 Java Fractal Explorer [4/8]
Java Fractal Explorer [4/8]
WilliamFiset
16 Java Fractal Explorer [8/8]
Java Fractal Explorer [8/8]
WilliamFiset
17 Java Fractal Explorer [5/8]
Java Fractal Explorer [5/8]
WilliamFiset
18 Java Fractal Explorer [2/8]
Java Fractal Explorer [2/8]
WilliamFiset
19 Java Fractal Explorer [7/8]
Java Fractal Explorer [7/8]
WilliamFiset
20 Java Fractal Explorer [1/8]
Java Fractal Explorer [1/8]
WilliamFiset
21 Java Fractal Explorer [3/8]
Java Fractal Explorer [3/8]
WilliamFiset
22 Introduction [Programming Competition Problems]
Introduction [Programming Competition Problems]
WilliamFiset
23 String Manipulation 1 [Programming Competition Problems]
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
24 String Manipulation 2 [Programming Competition Problems]
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
25 Graph Theory 1 [Programming Competition Problems]
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
26 Logic 1 [Programming Competition Problems]
Logic 1 [Programming Competition Problems]
WilliamFiset
27 Grid Problems 1 [Programming Competition Problems]
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
28 Dynamic Programming 1 [Programming Competition Problems]
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
29 Introduction to Big-O
Introduction to Big-O
WilliamFiset
30 Dynamic and Static Arrays
Dynamic and Static Arrays
WilliamFiset
31 Dynamic Array Code
Dynamic Array Code
WilliamFiset
32 Linked Lists Introduction
Linked Lists Introduction
WilliamFiset
33 Doubly Linked List Code
Doubly Linked List Code
WilliamFiset
34 Stack Introduction
Stack Introduction
WilliamFiset
35 Stack Implementation
Stack Implementation
WilliamFiset
36 Stack Code
Stack Code
WilliamFiset
37 Queue Introduction
Queue Introduction
WilliamFiset
38 Queue Implementation
Queue Implementation
WilliamFiset
39 Queue Code
Queue Code
WilliamFiset
40 Priority Queue Introduction
Priority Queue Introduction
WilliamFiset
41 Priority Queue Min Heaps and Max Heaps
Priority Queue Min Heaps and Max Heaps
WilliamFiset
42 Priority Queue Inserting Elements
Priority Queue Inserting Elements
WilliamFiset
43 Priority Queue Removing Elements
Priority Queue Removing Elements
WilliamFiset
44 Priority Queue Code
Priority Queue Code
WilliamFiset
45 Union Find Introduction
Union Find Introduction
WilliamFiset
46 Union Find Kruskal's Algorithm
Union Find Kruskal's Algorithm
WilliamFiset
47 Union Find - Union and Find Operations
Union Find - Union and Find Operations
WilliamFiset
Union Find Path Compression
Union Find Path Compression
WilliamFiset
49 Union Find Code
Union Find Code
WilliamFiset
50 Binary Search Tree Introduction
Binary Search Tree Introduction
WilliamFiset
51 Binary Search Tree Insertion
Binary Search Tree Insertion
WilliamFiset
52 Binary Search Tree Removal
Binary Search Tree Removal
WilliamFiset
53 Binary Search Tree Traversals
Binary Search Tree Traversals
WilliamFiset
54 Binary Search Tree Code
Binary Search Tree Code
WilliamFiset
55 Fenwick Tree range queries
Fenwick Tree range queries
WilliamFiset
56 Fenwick Tree point updates
Fenwick Tree point updates
WilliamFiset
57 Fenwick Tree construction
Fenwick Tree construction
WilliamFiset
58 Fenwick tree source code
Fenwick tree source code
WilliamFiset
59 Hash table hash function
Hash table hash function
WilliamFiset
60 Hash table separate chaining
Hash table separate chaining
WilliamFiset

The Union Find data structure with path compression is an efficient algorithm for solving graph problems. Path compression improves the performance of Union Find operations by reducing the number of nodes to traverse. The speaker provides examples and explanations to help viewers understand the concept.

Key Takeaways
  1. Understand the basics of the Union Find data structure
  2. Learn how path compression works
  3. Apply path compression to improve the efficiency of Union Find operations
  4. Analyze the time complexity of Union Find operations with path compression
  5. Implement Union Find with path compression in a programming language
💡 Path compression is a technique that improves the efficiency of Union Find operations by compressing the paths to the root node, reducing the number of nodes to traverse and improving the overall performance of the algorithm.

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →