Disjoint Sets using union by rank and path compression Graph Algorithm

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ธ.ค. 2024

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

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

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

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

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

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

      @@reshmaarul8637 thanks man, lemme edit that

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

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

      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

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

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

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

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

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

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

    This is the best explanation available on internet

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

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

  • @lavanya_m01
    @lavanya_m01 8 หลายเดือนก่อน

    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 🙂

  • @sanjay472
    @sanjay472 4 วันที่ผ่านมา

    its a great one while all others left this concept you made it exceptional 👌👌👌👌👌👌👌👌

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

    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!

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

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

  • @swetabjahazra8050
    @swetabjahazra8050 9 ปีที่แล้ว +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 10 หลายเดือนก่อน

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

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

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

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

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

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

    This video is criminally underrated

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

  • @mariiooP
    @mariiooP 5 วันที่ผ่านมา

    Amazing explanation, cleared some little doubts I had. 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.

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

    excellent ! This video saved me from failing my midterm

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

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

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

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

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

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

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

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

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

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

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

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

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

  • @priyanka.sarkar
    @priyanka.sarkar 6 ปีที่แล้ว +8

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

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

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

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

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

    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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    So much good explanation 🤗

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

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

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

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

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

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

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

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

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

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

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

    unbelievable clear and easy to understand.

  • @ブタでよろしく
    @ブタでよろしく 3 ปีที่แล้ว

    Thank you so much, Ray William Johnson!

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

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

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

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

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

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

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

    Best disjoint sets video, thank you for sharing.

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

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

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

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

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

    great explanation, thanks

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

    Awesome explanation sir !
    Gud work !

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

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

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

    Dude you are awesome. Continue the good work!

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

    helped me out big time once again. thank you!

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

    Very elaborate explanation! Thank you!

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

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

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

    Ótima explicação! Great work again.

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

    best explanation, made it simple!

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

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

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

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

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

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

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

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

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

    Thank you for the explanation, it helped me a lot!

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

    Your videos are the best!

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

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

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

    Love your videos man!

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

    beautifully written code.. so simple.

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

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

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

    tushar bro,really great video :)

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

    Thsnks sir for amazing tutorial

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

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

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

    Indian so great, they dominates the world of IT

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

    Thank You for awesome explaination

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

    The best explanation!

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

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

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

    Very nice explanation

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

    superb explanation

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

    Nicely explain sir.

  • @ganapatibiswas5858
    @ganapatibiswas5858 4 ปีที่แล้ว +2

    17:30 why the output is 4?

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

      yeah, it should be 1

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

    Can you please make a video explaining amortized analysis?

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

    Interesting data structure :)

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

    At around 8:20, before the path of 7 is compressed, the rank of 6 should be (1) instead of (0).

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

    Your code is so clean!

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

    Thank you, Tushar :)

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

    simply awesome bro..

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

    Good explanation

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

    I had the rank question too. If you have it pay close attention to the explanation at 15:21

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

    at 8:47 , why the rank for node 4 is still 2? I'm confused with the ranks

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

    Hi, thanks for your video. I'm wondering rank node 1 at 10:10 second should be 1 why you change it to 0 ?

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

    very useful video ..helped a lot

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

    good tutorial, keep the good work

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

    Thanks a lot Tushar