Disjoint Sets using union by rank and path compression Graph Algorithm

  Рет қаралды 311,613

Tushar Roy - Coding Made Simple

Tushar Roy - Coding Made Simple

9 жыл бұрын

Design disjoint sets which supports makeSet, union and findSet operations. Uses union by rank and path compression for optimization.
github.com/mission-peace/inte...
github.com/mission-peace/inte...
/ tusharroy25

Пікірлер: 314
@gregross5651
@gregross5651 5 жыл бұрын
All students are saved by Indian Tutors on KZfaq. Thanks India !!
@adhishmalviya2408
@adhishmalviya2408 3 жыл бұрын
🇮🇳 I love my India!
@043_fazlerabbi5
@043_fazlerabbi5 2 жыл бұрын
Allah saves us all.....
@tryler449
@tryler449 4 жыл бұрын
I swear, every time I see Tushar walk in front of the camera I can't help but smile. Thanks for being such a friendly and righteous dude.
@prashishh
@prashishh 9 жыл бұрын
Absolutely love your videos. I'm currently preparing for my interviews and your videos have been the go-to for me so far! Keep up the great work and thanks a million.
@Nihilish
@Nihilish 6 жыл бұрын
This video is amazing. It's explained so clearly, and it was a great idea to showcase the theory first, and then demonstrate the implementation. Great work man!
@akankshasharma7498
@akankshasharma7498 Жыл бұрын
0:05 What is Disjoint Set? Disjoint Set is data structure that supports the following operations: a. make set: create a set with only 1 element in it b. union: take 2 different sets and merge them in one c. find set: to return the identity of a set Identity of a set is usually an element of the set that acts as a representative for it Use Cases: 1. Kruskal's Algorithm 2. finding cycle in an undirected graph 0:38 Implementations: 1. using rank and path - uses a tree - node: int rank int data node parent a. make set: 1. initialize n nodes with data and rank=0 2. make parent of all nodes point to the node itself b. find set(node): 1. dfs to a node that points to itself (root) 2. path compression: node.parent=root 3. return root c. union(a, b): 1. A=find set(a), B=find set(b) 2. if A==B => already in the same set 3. if(A.rank>B.rank) make A te parent of B and increments its rank by 1 vice-verse 11:07 Time and Space Complexity for m operations and n elements Space: O(n) Time: O(m.α(n)) ≈ O(4m) ≈ O(m) 11:57 Code
@reshmaarul8637
@reshmaarul8637 Жыл бұрын
if A.rank > B.rank you shouldn't increment the rank. when both the sets are of same depth/rank only you have to increment the rank. if (A.rank > B.rank) B.parent = A; else if (B.rank > A.rank) A.parent = B; else { // both are equal so pick any B.parent = A; // A is the parent now A.rank++; }
@akankshasharma7498
@akankshasharma7498 Жыл бұрын
@@reshmaarul8637 thanks man, lemme edit that
@Jonatube2
@Jonatube2 8 жыл бұрын
Brilliant, really helps to watch your videos before reading about the topic at hand! Ty very much, keep up the good work :)
@saravanaprabhukarunakaran3667
@saravanaprabhukarunakaran3667 8 жыл бұрын
You can have a special talent of making anything complex look very simple. Thanks Tushar
@minrengwu2134
@minrengwu2134 4 жыл бұрын
Wonderful!! The simplest explanation I have ever seen on disjoint set and union-find!!
@conintava514
@conintava514 7 ай бұрын
Perhaps the most helpful disjointed sets video I've come across so far, thank you!
@davidvader7210
@davidvader7210 8 жыл бұрын
Wonderful video, very clear and very concise. My class didnt bring up "rank" but it really helped me understand. The use of code at the end was AWESOME
@shubhampathak1080
@shubhampathak1080 6 жыл бұрын
Great video ! This video was handy while brushing up the basics of algos. Keep up the good work, really appreciate it.
@wenyuewang5842
@wenyuewang5842 6 жыл бұрын
people like you make this world a better place!
@muskanroxx22
@muskanroxx22 3 жыл бұрын
only if you walked through the code of every problem like you did in this one!! Great work Tushar! You're still helping people after 6 years!!
@brianlau2757
@brianlau2757 8 жыл бұрын
Thanks! This is helping me get through my cs class in college
@sapotico
@sapotico 8 жыл бұрын
Just a small little note: when he's saying 2 goes to zero (10:20), it should have been 1. He was reading the new rank of one which was zero (I just don't want anyone to be confused). Excellent video and explanation! Thanks a lot!
@RAJAT281193
@RAJAT281193 6 жыл бұрын
2 goes to real parent only when will perform find operation ,then all the member in the path will goes to main parent directly . this is will not the case for union. check code once
@ahmedghazy5796
@ahmedghazy5796 4 жыл бұрын
Awesome!! nice one covering all I need to know about disjoint sets to be able to apply it into problem solving
@Joyddep
@Joyddep 4 жыл бұрын
One of the best executed implementation with superb explanation. Thanks a lot.
@anurag9850
@anurag9850 8 жыл бұрын
The video made my day... Was stuck in disjoint sets for 1 day. Keep it up :-)
@amitgp2007
@amitgp2007 8 жыл бұрын
you are THE BEST Sir.. These are best videos on algorithms and data structures.
@KaylaRen
@KaylaRen 6 жыл бұрын
Your video is always the best, helped me a lot of algorithms that I tried hard to understand.
@AnkitVerma-yn3ku
@AnkitVerma-yn3ku 5 жыл бұрын
This is the finest videos on disjoint set available on KZfaq Thanks Tushar for great explanation just a minor error 9 instead of 1 rank will be 2
@sakshamsharma5797
@sakshamsharma5797 6 жыл бұрын
Impressed by your teaching skills :) Thank you for the awesome videos.
@bioinfo9386
@bioinfo9386 8 жыл бұрын
.. great help for me as well!! Even more descriptive than the chapter in Cormen´s Algorithms..thanks a lot for your support!!
@ankitsingh-rb8pc
@ankitsingh-rb8pc 4 жыл бұрын
This is the best explanation available on internet
@atuljain2845
@atuljain2845 5 жыл бұрын
You really made this one simple to get. Thank you.
@debverine
@debverine 8 жыл бұрын
Excellent Tushar, this solved my doubt :) . Was stuck for last 2 days !!
@sumankashyap1
@sumankashyap1 8 жыл бұрын
Great work !! Very nice and simple to understand explanations !
@anirbanmaity93
@anirbanmaity93 6 жыл бұрын
Just awesome. I did not get this much of understanding from ny book. thanks a lot.
@VojtechMach
@VojtechMach 6 жыл бұрын
helped me out big time once again. thank you!
@987654jef
@987654jef 9 жыл бұрын
Ótima explicação! Great work again.
@priyanshagarwal2095
@priyanshagarwal2095 2 жыл бұрын
after 5 year also your videos are great,,, i was not able to understand clearly in any video then i came here, , FINALYY DONE
@mgorasia
@mgorasia 8 жыл бұрын
Dude you are awesome. Continue the good work!
@pisithyim9198
@pisithyim9198 7 жыл бұрын
Best disjoint sets video, thank you for sharing.
@cavalcanteluiz413
@cavalcanteluiz413 4 жыл бұрын
Thank you for the explanation, it helped me a lot!
@azzaea
@azzaea 8 жыл бұрын
Very elaborate explanation! Thank you!
@glaucoa.9214
@glaucoa.9214 3 жыл бұрын
Tushar thanks, I finally learned all about ranking union. Very happy for your video congratulations, I will follow your channel now.
@nalnalnalnal48
@nalnalnalnal48 8 жыл бұрын
Thank you, now that i can understand even kruskal's algorithm in a flash. Great video ;d
@lavanyam3224
@lavanyam3224 3 ай бұрын
I already watched videos on union by rank and didn't quite understand why we're incrementing rank for a few cases and why not for other cases. Your explanation made it so clear! We'll increment rank only if both the sets are of the same rank, otherwise, we just make a union 🙂
@bitlegend00
@bitlegend00 7 жыл бұрын
I know you already receive a lot of praise, but god damn you are good at this. Thank you.
@dustin_echoes
@dustin_echoes 8 жыл бұрын
My professor wouldn't explain shit in the lectures. Great video! It really helped!
@kiranbandagar2572
@kiranbandagar2572 6 жыл бұрын
excellent video..cleared all doubts in very less time...thanks a ton sir...
@jineshdhruv6151
@jineshdhruv6151 6 жыл бұрын
Thank you for the great explanation and clean code!!!!!
@partheshsoni6421
@partheshsoni6421 3 жыл бұрын
Love your videos man!
@vikrammondal5811
@vikrammondal5811 5 жыл бұрын
Thanks a lot for this video. BEST VIDEO FOR THIS TOPIC
@eliyahubasa9401
@eliyahubasa9401 6 ай бұрын
Your videos are the best!
@shanugupta296
@shanugupta296 6 жыл бұрын
precise and good explanation. Keep it up the good work :)
@anujrai605
@anujrai605 7 жыл бұрын
nice video.........has definately helped me in understanding the concept of disjoint set
@pritishthakkar7123
@pritishthakkar7123 8 жыл бұрын
Very nice and well explained video !! gr8 job sir !!!
@priyanka.sarkar
@priyanka.sarkar 5 жыл бұрын
When the path compression happens in findSet, there should be a change in rank in this case as well. Rank of root with data = 1 shouldl be 2. Although there will be no problem in this particular example but if instead of union(7, 13), it was union(7, 14) then due to path compression the rank of both the trees would change(the root with data 1 should be 2 and the root with data 11 should be 1) and after setting the parent of 11 as 1, the final rank of root 1 should be 2. But in this code, the rank is not getting updated which might lead to a problem in further unions which may involve a node in this component.
@juggles5474
@juggles5474 9 ай бұрын
Non-root ranks don't matter which Tushar mentions, since we'll only be merging using the rank of the parents
@saikalyan1916
@saikalyan1916 3 жыл бұрын
Thank you dude what great explanation. Excellent video !!!
@DarkGreiga
@DarkGreiga 8 жыл бұрын
very good, very helpful, and understandable for me even though english isn't my first language. thanks for sharing!
@tanurampal1358
@tanurampal1358 5 жыл бұрын
Amazing video! And the first person Ive heard use whom correctly in one go.. haha
@swetabjahazra8050
@swetabjahazra8050 8 жыл бұрын
Best Data Structures and Algorithms videos on youtube!!!!!!!!!!
@blasttrash
@blasttrash 5 жыл бұрын
Not anymore. Abdul Bari is here to transform everything. Check his channel. He doesnt stammer and talks slowly and his videos are crisp. No offense to Tushar though
@AbhishekKumar-im2xd
@AbhishekKumar-im2xd 4 жыл бұрын
Abdul bari is a beauty... He is a gem💎💎💎😍😍😍
@RakeshSingh-hb7rj
@RakeshSingh-hb7rj 4 жыл бұрын
@@AbhishekKumar-im2xd yes!!
@rohitk472
@rohitk472 5 ай бұрын
@@blasttrash Tushar Roys explanation is top notch...
@blasttrash
@blasttrash 5 ай бұрын
@@rohitk472not really, he jumps directly into say choosing dynamic programming or assuming that its greedy without explaining the reasoning behind it. There are many newer youtubers who explain things better.
@divyanganachaudhary3164
@divyanganachaudhary3164 9 жыл бұрын
Awesome explanation sir ! Gud work !
@anyu8109
@anyu8109 8 жыл бұрын
unbelievable clear and easy to understand.
@sword013
@sword013 3 жыл бұрын
7:42 Rank of 6 will be (1) itself, only 4's rank increased to 2, but then as sir said, it won't matter what is the rank at the non-root node, we're never going to see them, so doesn't matter if its 1 or 0 or any other number. We're concerned only about 4's rank.
@jitender1712
@jitender1712 8 жыл бұрын
nice video to learn how to represent disjoint sets and operations performed on it
@chadmalla
@chadmalla 7 жыл бұрын
Can you please make a video explaining amortized analysis?
@alessiodenny6123
@alessiodenny6123 4 жыл бұрын
excellent ! This video saved me from failing my midterm
@learnhumanity6701
@learnhumanity6701 2 жыл бұрын
And thank you so much for this video it helped me in the middle of the night to understand this concept ❤
@puffvayne5688
@puffvayne5688 2 жыл бұрын
That findSet function with the path compression is so fucking clever, nice dude!
@ewoutmerckx5749
@ewoutmerckx5749 8 жыл бұрын
Great introduction! :D
@chengzhiyang8176
@chengzhiyang8176 5 жыл бұрын
Thank you! you made it simple!
@zjyj520520dd
@zjyj520520dd 8 жыл бұрын
Thank you for your video, now it makes so much more sense to me! I have a question what if the findset number is the root, does the map just not change at all?
@abhinavaman8008
@abhinavaman8008 3 жыл бұрын
yes, cause when we will get the node and the parent and compare them then if the condition will give true cause they are equal and then we will simply return. Though I know you must not need this reply after 5 years but replying just to make you nostalgic
@sotiristotomis332
@sotiristotomis332 7 жыл бұрын
Hello and thank you for this tutorial. But there is something I want to mention. All the parent pointers of the roots must be null, this will help to find the root of each node using a while loop in O(h).
@RalyJoey
@RalyJoey 7 жыл бұрын
I'm confused with the RANK. It seems that rank is determined by the higher ranked set when performing the union rank, i.e., union(set_m rank1, set_n rank0) results to be rank1. Does that indicate rank won't change as we keep attaching rank0 set to another set?
@achuachu6296
@achuachu6296 3 жыл бұрын
me too
@amandixit3555
@amandixit3555 3 жыл бұрын
@@achuachu6296 Rank will change iff we try to union two elements of different sets having same rank . else the higher raked set will always be the parent of lower ranked set.
@user-kg2ql5sh8u
@user-kg2ql5sh8u 2 жыл бұрын
Thank you so much, Ray William Johnson!
@1gouravgg
@1gouravgg 7 жыл бұрын
beautifully written code.. so simple.
@faisalalmamun4312
@faisalalmamun4312 5 жыл бұрын
Thank You for awesome explaination
@rayanajjar6142
@rayanajjar6142 8 жыл бұрын
thank you very much,you are explaining the material very good
@mohammadkaifkaif5441
@mohammadkaifkaif5441 8 жыл бұрын
tushar bro,really great video :)
@waytoanand
@waytoanand 3 жыл бұрын
Tushar videos are fast and great but in this video another thing to notice is : 12.05 Tushar talks in native language and now we know he is desi guy :)
@rutvikrana512
@rutvikrana512 3 жыл бұрын
So much good explanation 🤗
@sujayshanbhag2055
@sujayshanbhag2055 Жыл бұрын
This video is criminally underrated
@heesukson3581
@heesukson3581 3 жыл бұрын
The best explanation!
@yangsong4839
@yangsong4839 2 жыл бұрын
Your code is so clean!
@DeepakSharma-di2fd
@DeepakSharma-di2fd 9 жыл бұрын
simply awesome bro..
@ahmedal-ahmed7606
@ahmedal-ahmed7606 7 жыл бұрын
Great Tutorial !! Thanks
@guvenim
@guvenim 7 жыл бұрын
good tutorial, keep the good work
@rohitbajaj7733
@rohitbajaj7733 2 жыл бұрын
best explanation, made it simple!
@mrm259
@mrm259 6 жыл бұрын
Hi, thanks for your video. I'm wondering rank node 1 at 10:10 second should be 1 why you change it to 0 ?
@abhishekp03
@abhishekp03 3 жыл бұрын
Thank you, Tushar :)
@nikhilgohil8808
@nikhilgohil8808 3 жыл бұрын
Simply amazing!
@oussamamoussaoui7131
@oussamamoussaoui7131 2 жыл бұрын
great explanation, thanks
@overdrivegain
@overdrivegain 4 жыл бұрын
Hello Tushar, my name is Manar and I wanna thank you!
@SantoshKumar-bu2qr
@SantoshKumar-bu2qr 5 жыл бұрын
10:56 isn't the rank of node 4 becomes 1 (depth of the tree after path compression) ?
@pradiptalekar9135
@pradiptalekar9135 8 жыл бұрын
Excellent videos dude
@ilyanaoumov5425
@ilyanaoumov5425 7 жыл бұрын
When you run through the path compression logic, shouldn't the rank decrease for each disjoint set when there is an optimization to be had? For example, when you first merge 2 sets, you might have a rank of 3. After you use findSet(), doesn't the rank fall back down to 1?
@sandepkv83
@sandepkv83 3 жыл бұрын
U save a lot of time... Tqs man.
@ashugupta8609
@ashugupta8609 6 жыл бұрын
thanx it was really helpful, pls can you explain about the other implementation like kruskals and diskestra too.
@SaurabhPatel
@SaurabhPatel 8 жыл бұрын
Such a Great video
@SaurabhPatel
@SaurabhPatel 8 жыл бұрын
+Tushar Roy at 9:51 Why you should increment the rank by 1. Both are not equal, One rank is 2 and Second rank is 1, So thats totally fine as per algorithm. If both rank are same then and then we need to increment any one of them by 1. //increment rank only if both sets have same rank parent1.rank = (parent1.rank == parent2.rank) ? parent1.rank + 1 : parent1.rank; parent2.parent = parent1;
@SaurabhPatel
@SaurabhPatel 8 жыл бұрын
+Tushar Roy thanks.
@RAVIKUMAR-ef4wo
@RAVIKUMAR-ef4wo 5 жыл бұрын
Thanks a lot Tushar
@Manu-wb2uv
@Manu-wb2uv 2 жыл бұрын
Interesting data structure :)
@benjenkincpa
@benjenkincpa 8 жыл бұрын
Excellent video.
@aruldhanasaamprakash
@aruldhanasaamprakash 5 жыл бұрын
Damn good explanation. :-)
@1994ranjeet
@1994ranjeet 7 жыл бұрын
very useful video ..helped a lot
@pradeexsu
@pradeexsu 4 жыл бұрын
Thsnks sir for amazing tutorial
@ramum5424
@ramum5424 7 жыл бұрын
superb explanation
@ronaqbehura8874
@ronaqbehura8874 2 жыл бұрын
When using path compression along with union by rank, the rank might be incorrect on application of the path compression algorithm. I have found union by size and path compression to be logically correct where path compression doesn't affect union by size at all. Any thoughts on this ? Would appreciate if anybody can point out if I'm missing out on anything for Union by Rank + Path compression
@KUSHUTRIVEDI
@KUSHUTRIVEDI 9 жыл бұрын
Great video sir..! :-)
Traveling Salesman Problem Dynamic Programming Held-Karp
20:21
Tushar Roy - Coding Made Simple
Рет қаралды 232 М.
Cycle in Undirected Graph Graph Algorithm
12:23
Tushar Roy - Coding Made Simple
Рет қаралды 144 М.
- А что в креме? - Это кАкАооо! #КондитерДети
00:24
Телеканал ПЯТНИЦА
Рет қаралды 8 МЛН
A little girl was shy at her first ballet lesson #shorts
00:35
Fabiosa Animated
Рет қаралды 11 МЛН
Пранк пошел не по плану…🥲
00:59
Саша Квашеная
Рет қаралды 6 МЛН
Binary Search : Median of two sorted arrays of different sizes.
24:48
Tushar Roy - Coding Made Simple
Рет қаралды 541 М.
Union Find in 5 minutes - Data Structures & Algorithms
5:46
Potato Coders
Рет қаралды 197 М.
Knuth-Morris-Pratt(KMP) Pattern Matching(Substring search)
12:50
Tushar Roy - Coding Made Simple
Рет қаралды 1,1 МЛН
The hidden beauty of the A* algorithm
19:22
polylog
Рет қаралды 848 М.
Disjoint Set | UNION and FIND
26:43
Techdose
Рет қаралды 110 М.
Graph Algorithms for Technical Interviews - Full Course
2:12:19
freeCodeCamp.org
Рет қаралды 1,2 МЛН
Lowest Common Ancestor Binary Tree
11:08
Tushar Roy - Coding Made Simple
Рет қаралды 250 М.
2.6.3 Heap - Heap Sort - Heapify - Priority Queues
51:08
Abdul Bari
Рет қаралды 2 МЛН
How Dijkstra's Algorithm Works
8:31
Spanning Tree
Рет қаралды 1,3 МЛН