Union Find in 5 minutes — Data Structures & Algorithms

preview_player
Показать описание
This video covers one of the most popular data structures and algorithms topic "Union Find". This is an instruction showing how to run Union-Find on a graph, with examples and code. Union Find and Disjoint Set are not as difficult as we think! 😊

#graph #data_structures #algorithms #faang #Union-find #disjoint set #data-structures #recursion #leetcode #interview #algo #disjoint-sets #disjoint sets

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

Comment down any questions below! Please give it a like and subscribe to support our channel.

Cheers,
Potato Coders 🙂

potatocoders
Автор

You forgot to mention you have to union the shorter tree into the longer tree! Otherwise the trees in a set of n elements can be up to n elements long (just a straight chain, instead of branching) and you don't get log(n) complexity.

robirahman
Автор

This was actually insanely helpful in understanding the concept. Thanks!

richtigmann
Автор

This video has saved me many hours of my life. Thank you for this.

luisgualpa
Автор

To optimize it, the find function should set the parent of it's parent to find(x). (This will probably help a lot)
function find(x):
if Parent[x] != x: parent[x] = find(parent[x])
return parent[x]

If I'm wrong please tell me.

BobTheScience
Автор

There is a little technicality, that when you do find(x), you want to do path compression, by connecting each vertex on the corresponding branch (the branch where x is) to the root. This results in subsequent find operations being cheaper (and you only double your cost one time).

jakubucinski
Автор

Awesome video, thank you! Question though; isn't the find operation O(n) time? I'm reading around that it isn't until you use 'Union By Rank' that it becomes O(log(n)).

Coffeycup
Автор

I was always stuck with the implementation of UF, now I finally got it, thanks!

pieterjanvandecasteele
Автор

Correction, this does not run in O(logn), but in O(n). To get O(logn) optimization, you need to use union by rank or by size.

tenminuteamateurhour
Автор

You explained it so well! Thank you for your video!

Anirozannay
Автор

Solved my hours long quandary in 5 minutes. Thank you!

jashmerchant
Автор

Really clear and concise explanation. This video helped a ton.

danielwang
Автор

Thanks for sharing! It's very clearly and efficiently explained!

Dev_God
Автор

Great video, worth adding path compression as well.

UsamaAziz-lbky
Автор

So practical and helpful video=) Thanks

송예은-hb
Автор

It so simple and straight forward. Thanks you so much

nghiapham
Автор

Bro this topic is confusing AF, but at least this vid makes it somewhat understandable

chrisdahms
Автор

Best illustration... please provide more content

kmishy
Автор

Best video I've found on UF. Thank you!

jeresher
Автор

How do you select the representative? I struggle to understand that part of it.

Shasol