Disjoint Sets using union by rank and path compression Graph Algorithm

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ก.ย. 2024
  • Design disjoint sets which supports makeSet, union and findSet operations. Uses union by rank and path compression for optimization.
    github.com/mis...
    github.com/mis...
    / tusharroy25

ความคิดเห็น • 312

  • @gregross5651
    @gregross5651 5 ปีที่แล้ว +339

    All
    students are saved by Indian Tutors on TH-cam. Thanks India !!

  • @tryler449
    @tryler449 4 ปีที่แล้ว +41

    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.

  • @akankshasharma7498
    @akankshasharma7498 ปีที่แล้ว +23

    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 ปีที่แล้ว +3

      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

  • @sapotico
    @sapotico 8 ปีที่แล้ว +56

    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

  • @lavanyam3224
    @lavanyam3224 4 หลายเดือนก่อน

    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 🙂

  • @Nihilish
    @Nihilish 6 ปีที่แล้ว +5

    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!

  • @swetabjahazra8050
    @swetabjahazra8050 8 ปีที่แล้ว +73

    Best Data Structures and Algorithms videos on youtube!!!!!!!!!!

    • @blasttrash
      @blasttrash 5 ปีที่แล้ว +7

      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 5 ปีที่แล้ว +5

      Abdul bari is a beauty... He is a gem💎💎💎😍😍😍

    • @RakeshSingh-hb7rj
      @RakeshSingh-hb7rj 5 ปีที่แล้ว

      @@AbhishekKumar-im2xd yes!!

    • @rohitk472
      @rohitk472 6 หลายเดือนก่อน

      @@blasttrash Tushar Roys explanation is top notch...

    • @blasttrash
      @blasttrash 6 หลายเดือนก่อน

      @@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.

  • @priyanka.sarkar
    @priyanka.sarkar 5 ปีที่แล้ว +7

    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 11 หลายเดือนก่อน

      Non-root ranks don't matter which Tushar mentions, since we'll only be merging using the rank of the parents

  • @wenyuewang5842
    @wenyuewang5842 6 ปีที่แล้ว +4

    people like you make this world a better place!

  • @sword013
    @sword013 3 ปีที่แล้ว +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.

  • @saravanaprabhukarunakaran3667
    @saravanaprabhukarunakaran3667 8 ปีที่แล้ว

    You can have a special talent of making anything complex look very simple. Thanks Tushar

  • @priyanshagarwal2095
    @priyanshagarwal2095 3 ปีที่แล้ว

    after 5 year also your videos are great,,,
    i was not able to understand clearly in any video then i came here, , FINALYY DONE

  • @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!!

  • @conintava514
    @conintava514 9 หลายเดือนก่อน

    Perhaps the most helpful disjointed sets video I've come across so far, thank you!

  • @ankitsingh-rb8pc
    @ankitsingh-rb8pc 4 ปีที่แล้ว +1

    This is the best explanation available on internet

  • @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

  • @prashishh
    @prashishh 9 ปีที่แล้ว +1

    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.

  • @brianlau2757
    @brianlau2757 8 ปีที่แล้ว +4

    Thanks! This is helping me get through my cs class in college

  • @minrengwu2134
    @minrengwu2134 5 ปีที่แล้ว

    Wonderful!! The simplest explanation I have ever seen on disjoint set and union-find!!

  • @rutvikrana512
    @rutvikrana512 3 ปีที่แล้ว +1

    So much good explanation 🤗

  • @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 :)

  • @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.

  • @jitender1712
    @jitender1712 8 ปีที่แล้ว

    nice video to learn how to represent disjoint sets and operations performed on it

  • @amitgp2007
    @amitgp2007 8 ปีที่แล้ว

    you are THE BEST Sir.. These are best videos on algorithms and data structures.

  • @Jonatube2
    @Jonatube2 8 ปีที่แล้ว +1

    Brilliant, really helps to watch your videos before reading about the topic at hand! Ty very much, keep up the good work :)

  • @Joyddep
    @Joyddep 5 ปีที่แล้ว

    One of the best executed implementation with superb explanation. Thanks a lot.

  • @anirbanmaity93
    @anirbanmaity93 6 ปีที่แล้ว

    Just awesome. I did not get this much of understanding from ny book. thanks a lot.

  • @puffvayne5688
    @puffvayne5688 2 ปีที่แล้ว

    That findSet function with the path compression is so fucking clever, nice dude!

  • @sujayshanbhag2055
    @sujayshanbhag2055 2 ปีที่แล้ว

    This video is criminally underrated

  • @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!!

  • @learnhumanity6701
    @learnhumanity6701 2 ปีที่แล้ว

    And thank you so much for this video it helped me in the middle of the night to understand this concept ❤

  • @rayanajjar6142
    @rayanajjar6142 8 ปีที่แล้ว

    thank you very much,you are explaining the material very good

  • @AnkitVerma-yn3ku
    @AnkitVerma-yn3ku 5 ปีที่แล้ว

    This is the finest videos on disjoint set available on TH-cam Thanks Tushar for great explanation just a minor error 9 instead of 1 rank will be 2

  • @dustin_echoes
    @dustin_echoes 8 ปีที่แล้ว

    My professor wouldn't explain shit in the lectures. Great video! It really helped!

  • @oussamamoussaoui7131
    @oussamamoussaoui7131 2 ปีที่แล้ว +1

    great explanation, thanks

  • @user-kg2ql5sh8u
    @user-kg2ql5sh8u 2 ปีที่แล้ว

    Thank you so much, Ray William Johnson!

  • @kiranbandagar2572
    @kiranbandagar2572 7 ปีที่แล้ว

    excellent video..cleared all doubts in very less time...thanks a ton sir...

  • @KaylaRen
    @KaylaRen 6 ปีที่แล้ว

    Your video is always the best, helped me a lot of algorithms that I tried hard to understand.

  • @narendrakjha8883
    @narendrakjha8883 4 ปีที่แล้ว +7

    9:03 rank of node 4 in second component should also have been 1 after path compression at 8:17 and therefore either component could be merged to the other while performing union(3, 7) at 9:03

  • @alessiodenny6123
    @alessiodenny6123 4 ปีที่แล้ว

    excellent ! This video saved me from failing my midterm

  • @saikalyan1916
    @saikalyan1916 3 ปีที่แล้ว

    Thank you dude what great explanation. Excellent video !!!

  • @ahmedghazy5796
    @ahmedghazy5796 5 ปีที่แล้ว

    Awesome!! nice one covering all I need to know about disjoint sets to be able to apply it into problem solving

  • @shubhampathak1080
    @shubhampathak1080 6 ปีที่แล้ว

    Great video ! This video was handy while brushing up the basics of algos. Keep up the good work, really appreciate it.

  • @DarkGreiga
    @DarkGreiga 8 ปีที่แล้ว

    very good, very helpful, and understandable for me even though english isn't my first language. thanks for sharing!

  • @VersatileAnthem
    @VersatileAnthem 5 ปีที่แล้ว

    this is the best Disjoint Set Union video in the whole youtube !!! at 7:52 rank of the ( 6 ) should be 1

    • @growingwithtech
      @growingwithtech 5 ปีที่แล้ว

      Doesn't matter, we aren't really concerned about ranks of nodes other than parent's

  • @anyu8109
    @anyu8109 8 ปีที่แล้ว

    unbelievable clear and easy to understand.

  • @sumankashyap1
    @sumankashyap1 9 ปีที่แล้ว

    Great work !! Very nice and simple to understand explanations !

  • @sakshamsharma5797
    @sakshamsharma5797 6 ปีที่แล้ว

    Impressed by your teaching skills :) Thank you for the awesome videos.

  • @anujrai605
    @anujrai605 7 ปีที่แล้ว

    nice video.........has definately helped me in understanding the concept of disjoint set

  • @jagdishwarbiradar1763
    @jagdishwarbiradar1763 4 ปีที่แล้ว +1

    this video was uploaded 5 years ago ,but still best 👍🏻 as compares with others

  • @pritishthakkar7123
    @pritishthakkar7123 8 ปีที่แล้ว

    Very nice and well explained video !! gr8 job sir !!!

  • @debverine
    @debverine 8 ปีที่แล้ว

    Excellent Tushar, this solved my doubt :) . Was stuck for last 2 days !!

  • @anurag9850
    @anurag9850 8 ปีที่แล้ว

    The video made my day...
    Was stuck in disjoint sets for 1 day.
    Keep it up :-)

  • @pisithyim9198
    @pisithyim9198 7 ปีที่แล้ว

    Best disjoint sets video, thank you for sharing.

  • @rohitbajaj7733
    @rohitbajaj7733 2 ปีที่แล้ว

    best explanation, made it simple!

  • @987654jef
    @987654jef 9 ปีที่แล้ว +1

    Ótima explicação! Great work again.

  • @pradeexsu
    @pradeexsu 4 ปีที่แล้ว

    Thsnks sir for amazing tutorial

  • @rummanchowdhury3807
    @rummanchowdhury3807 2 ปีที่แล้ว

    Good explanation

  • @vikrammondal5811
    @vikrammondal5811 5 ปีที่แล้ว

    Thanks a lot for this video. BEST VIDEO FOR THIS TOPIC

  • @bitlegend00
    @bitlegend00 7 ปีที่แล้ว

    I know you already receive a lot of praise, but god damn you are good at this. Thank you.

  • @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).

  • @divyanganachaudhary3164
    @divyanganachaudhary3164 9 ปีที่แล้ว

    Awesome explanation sir !
    Gud work !

  • @Manu-wb2uv
    @Manu-wb2uv 2 ปีที่แล้ว

    Interesting data structure :)

  • @atuljain2845
    @atuljain2845 5 ปีที่แล้ว

    You really made this one simple to get. Thank you.

  • @RalyJoey
    @RalyJoey 8 ปีที่แล้ว +10

    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.

  • @azzaea
    @azzaea 8 ปีที่แล้ว

    Very elaborate explanation! Thank you!

  • @1gouravgg
    @1gouravgg 7 ปีที่แล้ว

    beautifully written code.. so simple.

  • @chadmalla
    @chadmalla 7 ปีที่แล้ว +11

    Can you please make a video explaining amortized analysis?

  • @heesukson3581
    @heesukson3581 3 ปีที่แล้ว

    The best explanation!

  • @overdrivegain
    @overdrivegain 4 ปีที่แล้ว

    Hello Tushar, my name is Manar and I wanna thank you!

  • @zjyj520520dd
    @zjyj520520dd 8 ปีที่แล้ว +6

    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 ปีที่แล้ว +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

  • @eliyahubasa9401
    @eliyahubasa9401 7 หลายเดือนก่อน

    Your videos are the best!

  • @nalnalnalnal48
    @nalnalnalnal48 8 ปีที่แล้ว

    Thank you, now that i can understand even kruskal's algorithm in a flash. Great video ;d

  • @javakishore
    @javakishore 8 ปีที่แล้ว

    Very nice explanation

  • @VojtechMach
    @VojtechMach 6 ปีที่แล้ว +1

    helped me out big time once again. thank you!

  • @ramum5424
    @ramum5424 7 ปีที่แล้ว

    superb explanation

  • @unkonw9621
    @unkonw9621 5 ปีที่แล้ว

    Nicely explain sir.

  • @faisalalmamun4312
    @faisalalmamun4312 5 ปีที่แล้ว

    Thank You for awesome explaination

  • @sandepkv83
    @sandepkv83 3 ปีที่แล้ว

    U save a lot of time... Tqs man.

  • @mikealTuan
    @mikealTuan ปีที่แล้ว

    Indian so great, they dominates the world of IT

  • @RAVIKUMAR-ef4wo
    @RAVIKUMAR-ef4wo 5 ปีที่แล้ว

    Thanks a lot Tushar

  • @benjenkincpa
    @benjenkincpa 9 ปีที่แล้ว

    Excellent video.

  • @abhishekp03
    @abhishekp03 3 ปีที่แล้ว

    Thank you, Tushar :)

  • @partheshsoni6421
    @partheshsoni6421 4 ปีที่แล้ว +1

    Love your videos man!

  • @jineshdhruv6151
    @jineshdhruv6151 6 ปีที่แล้ว

    Thank you for the great explanation and clean code!!!!!

  • @deekshaagrawal344
    @deekshaagrawal344 4 ปีที่แล้ว

    Well explained.

  • @avinashkumar-hz9cp
    @avinashkumar-hz9cp 4 ปีที่แล้ว

    was really very helpful

  • @SMT0808
    @SMT0808 7 ปีที่แล้ว

    awesome explaination

  • @tanurampal1358
    @tanurampal1358 5 ปีที่แล้ว

    Amazing video! And the first person Ive heard use whom correctly in one go.. haha

  • @NehiroHakodate
    @NehiroHakodate 8 ปีที่แล้ว +20

    at around 9:45, why does the rank of node 1 become 0?

    • @prashantshubham
      @prashantshubham 8 ปีที่แล้ว

      Rank does not need to be accurate.

    • @manoszaranis397
      @manoszaranis397 7 ปีที่แล้ว +6

      because for non-root nodes it doesn't matter what the rank is... The rank of the root node is the one that is important.

    • @amitabhbiswal9644
      @amitabhbiswal9644 7 ปีที่แล้ว +2

      Rank should be 1 as we can see in java code.

    • @jiachuandeng6853
      @jiachuandeng6853 7 ปีที่แล้ว +4

      I think its a mistake

    • @eugenecallahan1698
      @eugenecallahan1698 6 ปีที่แล้ว +3

      Yes, it is a mistake. The rank would not change.

  • @SantoshKumar-bu2qr
    @SantoshKumar-bu2qr 5 ปีที่แล้ว +4

    10:56 isn't the rank of node 4 becomes 1 (depth of the tree after path compression) ?

  • @tech_excellence
    @tech_excellence 3 ปีที่แล้ว

    nice lesson

  • @1994ranjeet
    @1994ranjeet 7 ปีที่แล้ว

    very useful video ..helped a lot

  • @akshatBountyHunter
    @akshatBountyHunter 7 ปีที่แล้ว +86

    i have understand the whole video till 12 minutes then java came with hashmap and killed my C ++.

    • @srinivasataruncool
      @srinivasataruncool 7 ปีที่แล้ว +1

      there is unordered_map in c++. duckduckgo.com/?q=unordered_map+c%2B%2B&t=canonical&atb=v55-1&ia=web

    • @a55tech
      @a55tech 5 ปีที่แล้ว

      Why he set a map to a hashmap, instead of defining the variable as a hashmap to begin with?

    • @kipa_chu
      @kipa_chu 5 ปีที่แล้ว +10

      Then I must say you're not good in C++ also.

    • @dhruvsaraff5236
      @dhruvsaraff5236 5 ปีที่แล้ว +1

      @@a55tech Map is an interface in java.util. You can consider an interface as a blueprint of classes. HashMap is a class that implements the interface.
      Since every HashMap is a Map, his way of writing isn't incorrect. He could have done what you're saying, but his method required him to type 4 less letters :)

    • @overdrivegain
      @overdrivegain 4 ปีที่แล้ว +1

      I once gave a lecture in university and I thanked the Indian students for their help

  • @shanugupta296
    @shanugupta296 6 ปีที่แล้ว

    precise and good explanation. Keep it up the good work :)

  • @pradiptalekar9135
    @pradiptalekar9135 8 ปีที่แล้ว

    Excellent videos dude

  • @DeepakSharma-di2fd
    @DeepakSharma-di2fd 9 ปีที่แล้ว

    simply awesome bro..

  • @yangsong4839
    @yangsong4839 2 ปีที่แล้ว

    Your code is so clean!

  • @mattsmith4487
    @mattsmith4487 7 ปีที่แล้ว

    While tail-end recursive loops and do-while loops are functionally the same in high-level programming languages it is always better to use a while loop from the assembly language point of view. The compiler has a much easier time with unconditional branches and counters instead of the mess of making a recursive loop. Just thought you should know.

  • @guvenim
    @guvenim 7 ปีที่แล้ว

    good tutorial, keep the good work