Тёмный

Union Find in 5 minutes - Data Structures & Algorithms 

Potato Coders
Подписаться 2,9 тыс.
Просмотров 216 тыс.
50% 1

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
Facebook: / potatocoders
Linkedin: / potato-coders

Опубликовано:

 

7 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 93   
@potatocoders2028
@potatocoders2028 3 года назад
Comment down any questions below! Please give it a like and subscribe to support our channel. Cheers, Potato Coders 🙂
@cartooncartel6493
@cartooncartel6493 22 дня назад
Bro you cooked
@robirahman
@robirahman 2 года назад
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.
@tunguyenxuan8296
@tunguyenxuan8296 2 года назад
true. if we represent the group as a tree, it will take O(n) for find function in worst case. We better to represent a group as a trie, where all children point to one root.
@neerajvshah
@neerajvshah Год назад
@@tunguyenxuan8296 Trie's don't change the runtime complexity, a trie is just a type of tree (keep in mind not all trees are binary trees, a trie is just an k-ary tree). As Robi mentioned it's path compression which makes union find efficient. This video is incorrect - O(logn) is never the time complexity for union find. It's either O(n) without path compression or O(a(n)) with path compression.
@lightlysal
@lightlysal Год назад
​​​@@neerajvshahWhat's a(n) mean in your explanation? Is it the amount of times we "compress" a parent? I thought when people say the time complexity of union find they mean O(log N) on average over all use cases
@sirmonke8946
@sirmonke8946 Год назад
@@lightlysal a(n) is the reverse ackermann function. It grows insanely slow (slower than any log) and can basically be rounded to constant time
@MrKlaygomes
@MrKlaygomes 11 месяцев назад
@@neerajvshahpartially correct, if it is only weighted without path compression it is O(log n).
@luisgualpa7019
@luisgualpa7019 3 года назад
This video has saved me many hours of my life. Thank you for this.
@potatocoders2028
@potatocoders2028 3 года назад
I'm so glad!
@richtigmann1
@richtigmann1 9 месяцев назад
This was actually insanely helpful in understanding the concept. Thanks!
@BobTheScience
@BobTheScience 8 месяцев назад
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.
@nomealow
@nomealow Месяц назад
Yes, this is called path compression, by setting the parent of x and all of the ancestors of x to be the root ancestor of x. This makes it so that find(), for that particular tree in the future, will be O(1) instead of being dependent on the tree depth
@pieterjanvandecasteele135
@pieterjanvandecasteele135 3 года назад
I was always stuck with the implementation of UF, now I finally got it, thanks!
@potatocoders2028
@potatocoders2028 3 года назад
You're welcome!
@vishaldhawan9236
@vishaldhawan9236 2 года назад
+1
@Coffeycup27
@Coffeycup27 3 года назад
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)).
@nightmare_9
@nightmare_9 3 года назад
Crystal clear and precise...Thanks!!
@potatocoders2028
@potatocoders2028 3 года назад
Glad it was helpful! Also I like your profile picture :)
@danielwang8833
@danielwang8833 3 месяца назад
Really clear and concise explanation. This video helped a ton.
@Anirozannay
@Anirozannay 3 года назад
You explained it so well! Thank you for your video!
@potatocoders2028
@potatocoders2028 3 года назад
Thank you! We hope it has helped!
@tudorradu5848
@tudorradu5848 2 года назад
@@potatocoders2028 it helped me a lot, thank you man!
@jakubucinski
@jakubucinski 2 года назад
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).
@rm-stl
@rm-stl Год назад
I’ve seen this done but never heard it explained. It looked odd to have a side effect in a find(). Makes perfect sense now. Thanks
@stevenhicks3321
@stevenhicks3321 Год назад
I also noticed this and thought it may be better to use sets if you don't care about the three structure of the disjoineted set.
@nghiapham1632
@nghiapham1632 10 месяцев назад
It so simple and straight forward. Thanks you so much
@jashmerchant5121
@jashmerchant5121 6 месяцев назад
Solved my hours long quandary in 5 minutes. Thank you!
@송예은-h7b
@송예은-h7b 2 года назад
So practical and helpful video=) Thanks
@chrisdahms9682
@chrisdahms9682 Год назад
Bro this topic is confusing AF, but at least this vid makes it somewhat understandable
@Dev_God
@Dev_God 2 года назад
Thanks for sharing! It's very clearly and efficiently explained!
@tenminuteamateurhour
@tenminuteamateurhour 8 месяцев назад
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.
@JamesBrodski
@JamesBrodski Год назад
Thank you so much this is great!
@mykz814
@mykz814 4 месяца назад
bro... come back ur vids r so good 😭
@UsamaAziz-lb7ky
@UsamaAziz-lb7ky Месяц назад
Great video, worth adding path compression as well.
@jeresher6821
@jeresher6821 3 года назад
Best video I've found on UF. Thank you!
@potatocoders2028
@potatocoders2028 3 года назад
Wow, thank you for the great comment! :)
@bozecki
@bozecki 3 года назад
Thanks bro, now I finally understand it for tommorow exam ;p
@jashmerchant5121
@jashmerchant5121 6 месяцев назад
Python code: # WITH Path Compression def find(self, parent, x): # finds root or representative of disjoint-set/union if parent[x] != x: parent[x] = self.find(parent, parent[x]) # path compression step return parent[x] def union(self, parent, x, y): root_x = self.find(parent, x) root_y = self.find(parent, y) parent[root_x] = root_y # WITHOUT Path Compression def find(self, parent, x): # finds root or representative of disjoint-set/union if parent[x] != x: return self.find(parent, parent[x]) return parent[x] def union(self, parent, x, y): root_x = self.find(parent, x) root_y = self.find(parent, y) parent[root_x] = root_y
@austecon6818
@austecon6818 Месяц назад
Thanks and other implementations include rank. Does it matter?
@Baal3033
@Baal3033 3 года назад
Awesome explanation, thank you!
@potatocoders2028
@potatocoders2028 3 года назад
Np :) I am glad it helped
@kmishy
@kmishy 2 года назад
Best illustration... please provide more content
@gdthegreat
@gdthegreat 2 года назад
thanks a lot for explaining in few minutes.
@manojmpatil1269
@manojmpatil1269 Год назад
Perfectly explained. Thanks
@donaldcodes
@donaldcodes 2 года назад
Love it man, short and clear!
@collyyang9664
@collyyang9664 11 дней назад
good, thank you very much
@ahmetemin7572
@ahmetemin7572 Год назад
Short and clear, thanks
@SterlingVanlaere
@SterlingVanlaere 3 года назад
Excellent video, thanks!
@potatocoders2028
@potatocoders2028 3 года назад
Glad it helped!
@chrismiaomiao9426
@chrismiaomiao9426 Год назад
Amazing! It's very clear. Thank you
@jubiliudu
@jubiliudu 3 года назад
Amazing! thank you!
@Hdhdushzhz57743
@Hdhdushzhz57743 6 месяцев назад
Thank you! This was very helpful
@adam23sp
@adam23sp 2 года назад
Amazing video, very easy to follow, thank you! 🎉
@samruddhisaoji7195
@samruddhisaoji7195 2 года назад
Loved this bite sized explanation!
@persas1683
@persas1683 Год назад
Thank you very much. Nice video.
@shamitfatin3516
@shamitfatin3516 2 года назад
under-rated video.. thanks for the explanation
@arjunv7055
@arjunv7055 Год назад
wonderful explanation! Thanks mate!
@alesblaze4745
@alesblaze4745 2 года назад
wow , i must say that this was really easy and nice video , really nice explanation
@civilspot5912
@civilspot5912 6 месяцев назад
Great explanation
@piotr780
@piotr780 2 года назад
best explanation every ! :D
@samwilson4597
@samwilson4597 Год назад
Thank you so much man
@waynegreen7970
@waynegreen7970 3 года назад
Good content!
@potatocoders2028
@potatocoders2028 3 года назад
Thank you. Let me know if there is any more content that you would like to see! 😀
@DeepakSankar88
@DeepakSankar88 Год назад
Great explanation. Was able to recall what I had learnt a while back. Thank you!
@tudorradu5848
@tudorradu5848 2 года назад
THANK YOU!
@chku
@chku 8 месяцев назад
This video is so gud but looks like the channel ain't active anymkre 🥺💔 I really loved the explanation n quality so thankuu n hope u r doing well ✨
@paccini1
@paccini1 2 года назад
Wow, thanks a lot, great explanation!
@WebSurfingIsMyPastime
@WebSurfingIsMyPastime Год назад
This video was great! definitely not something that's very clearly covered in alot of other sources
@MinhPham-eh6lr
@MinhPham-eh6lr Год назад
Thanks for the concise and easy-to-understand explanation!
@Shasol
@Shasol 3 года назад
How do you select the representative? I struggle to understand that part of it.
@kmishy
@kmishy 3 года назад
algorithm was easy but you should write code and execute
@helloyogeshpatel
@helloyogeshpatel Год назад
That’s how Algo is studied
@errorless7921
@errorless7921 Год назад
Yes important is implementation
@Rockyzach88
@Rockyzach88 Год назад
Thanks Mr. Tater.
@JackSparrow-vv2uq
@JackSparrow-vv2uq 3 года назад
Thank you
@aadityakiran_s
@aadityakiran_s Год назад
You got a video on path compression?
@rasheedlewis1
@rasheedlewis1 11 месяцев назад
The Big-Theta runtime for find() would be Θ(n).
@KoScosss
@KoScosss Год назад
Thanks
@corgirun7892
@corgirun7892 Год назад
awesome
@pamp3657
@pamp3657 Год назад
good video
@mahlahlana
@mahlahlana 3 года назад
wow!
@algorithmdatastructures9244
@algorithmdatastructures9244 3 года назад
Subbed 💛
@potatocoders2028
@potatocoders2028 3 года назад
Thank you ☺️
@samuraipiyush
@samuraipiyush 10 месяцев назад
beauty
@Luca_040
@Luca_040 4 месяца назад
Wer sieht das auch für's HAW Studium?
@moshebixenshpaner5094
@moshebixenshpaner5094 2 года назад
There are two issues here: 1. When performing union, you set parent of the smaller set to the representative of the larger set. 2. When finding a path, you compress the path on your way back, improving O(logn) ops to O(log* n) in average.
@CSSFACE
@CSSFACE 2 года назад
what do you mean by O(log* n) what is the *?
@quantum_psi
@quantum_psi 2 года назад
This really didn't make sense
@nguyennguyendinh5215
@nguyennguyendinh5215 7 месяцев назад
very clear! Thanks!
@jakobmusic9187
@jakobmusic9187 Год назад
fantastic video! thanks!!!
Далее
How I would learn Leetcode if I could start over
18:03
Просмотров 574 тыс.
Bro's Using 3 Weapons
00:36
Просмотров 4 млн
Top 7 Data Structures for Interviews Explained SIMPLY
13:02
Union Find Kruskal's Algorithm
6:15
Просмотров 204 тыс.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Bellman-Ford in 5 minutes - Step by step example
5:10
How Dijkstra's Algorithm Works
8:31
Просмотров 1,3 млн