Union Find Kruskal's Algorithm

WilliamFiset · Beginner ·⚡ Algorithms & Data Structures ·9y ago
Introduction to Kruskal's Algorithm 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 ==================================== 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 =================================== 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 introduces Kruskal's Algorithm, a method for finding the minimum spanning tree of a graph, and explains how it works using the union find data structure.

Full Transcript

Okay, let's talk about a really neat application of the union find which is Kru School's minimum spanning tree algorithm. So you might be asking yourself what is a minimum spanning tree? So if we're given some graph with some vertices and some edges, the minimum spanning tree is a subset of the edges which connects to all the vertices and does so at a minimal cost. So if this is our graph um with some edges and some vertices then a possible minimum spanning tree is the following and has edge weight 14 well total edge weight 14. Note that the minimum spanning tree is not necessarily unique. So if there is another minimum spanning tree it will also have a total weight of 14. So how does it work? So we can break it up into three steps essentially. So the first step is easy. Just take all our edges and sort them by ascending edge edge weight. Next thing we want to do is we want to walk through the sorted edges and compare the two nodes that the edge uh belongs to. And if the nodes already belong to the same group, then we want to ignore it because it'll create a cycle in our minimum spanning tree, which we don't want. Otherwise, we want to unify uh the the two groups those nodes belong to and keep going. And we keep doing this process until either we run out of edges or all the vertices have been unified into one big group. And you'll soon see what I mean by a group which is when our union find data structure is going to come into play. So if this is our graph then to run's algorithm on it first let's scale the edges and sort them. So on the left side you see I have all the edges and their edge weights sorted in ascending order. So next we're going to start processing the edges one at a time. Uh started starting with the top. So I to J. So I've harded the edge I to J in orange. And you can see that it connects nodes uh I and J. I and J currently don't belong to any group. So I'm going to unify them together into group uh orange. Next is edge 8 to E. So A to E don't belong to any group. So I'm going to unify them together into group purple. Next is uh C to I. So I belongs to group uh orange, but C doesn't have a group yet. So C can go into group orange. All right. Uh E to F. F doesn't belong to a group. So F can go to group purple. Next, H and G. Ne neither H nor G belong to a group. So, I'm going to say you guys belong to the red group. Next, we have D2B. Uh they also don't belong to a group. So, give them their own group. Let's say uh group green. All right. And now, I believe this is when things start to get interesting. Now we're trying to connect C to J. But notice that C and J already both belong to group orange. So we don't want to include that edge because it's going to create a cycle. So ignore it. And to check that they belong to the same group, we would use the find operation in our union find to check what group they belong to. So this is when the union find really comes into play. Uh next is edge D to E. So note that E belongs to group purple and D belongs to group green. So now we want to merge them together because they don't belong to the same group. So either the purple group is going to become the green group or the green group's going to become the purple group. It doesn't really matter. So now we would merge them. And this is when the union operation in our union find becomes useful. allows us to merge groups of colors together very efficiently and that's the important note. Uh next edge would be uh D to H. H belongs to group red and um D to purple. So merge the groups together. Let's say they both become group purple. Uh next up we want to add edge A to D. But A to D already belong to the same group. So that would create a cycle. So we don't want to include that edge. So skip it. Uh next we want to include edge B to C. Uh B to C belong to different groups. Uh so merge the two groups into one larger group. So we have found the minimum spanning tree using Cruc's minimum spanning tree algorithm. Pretty neat, right? So the underlying data structure which allows us to do this is the union find. It allows us to merge groups of colors together efficiently but also to find out which groups uh nodes belong to uh so that we don't create a cycle. So so that's Chris's algorithm. It's super simple u given that you know how the union find works. So I'm going to go into some detail in next video explaining how the find and the union operations work internally and how we can actually implement it in some useful way. All right, I will see you guys then but thank you so much for watching and I will catch you next time.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from WilliamFiset · WilliamFiset · 46 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
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
48 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

This video teaches Kruskal's Algorithm for finding the minimum spanning tree of a graph, and explains how it uses the union find data structure to efficiently merge groups of nodes and detect cycles. The algorithm is useful for solving problems in graph theory and network design.

Key Takeaways
  1. Sort all edges of the graph by ascending weight
  2. Initialize each node as its own group
  3. Iterate through the sorted edges and check if the nodes belong to the same group using the find operation
  4. If the nodes do not belong to the same group, merge the groups using the union operation and add the edge to the minimum spanning tree
  5. Repeat the process until all nodes are connected or all edges have been processed
💡 The union find data structure allows for efficient merging of groups and detection of cycles, making it a crucial component of Kruskal's 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 →