Disjoint Set | Union By Rank and Path Compression

Поділитися
Вставка
  • Опубліковано 30 лис 2024

КОМЕНТАРІ • 178

  • @takeUforward
    @takeUforward  3 роки тому +21

    I will be posting a video every Sunday on the other channel, so bell DABAAAAA dena: ua-cam.com/channels/vEKHATlVq84hm1jduTYm8g.html

    • @praveengautam9851
      @praveengautam9851 3 роки тому +3

      hey!..inside that union function what if the parent of u and v is already same...means they are connected to same parent...then we should be doing anything....right?.....see this (reference: en.wikipedia.org/wiki/Disjoint-set_data_structure)
      function Union(x, y) is

      x := Find(x)
      y := Find(y)
      if x = y then
      return // x and y are already in the same set
      end if
      // If necessary, rename variables to ensure that
      // x has at least as many descendants as y
      if x.size < y.size then
      (x, y) := (y, x)
      end if
      // Make x the new root
      y.parent := x
      // Update the size of x
      x.size := x.size + y.size
      end function

    • @praveengautam9851
      @praveengautam9851 3 роки тому

      and what about the time complexity of makeset.......O(n) is there....?

    • @suryakiran2970
      @suryakiran2970 2 роки тому

      @@praveengautam9851 Same node will connect to his parent

    • @newbienate
      @newbienate 2 роки тому

      why do we need height or rank if we are path compressing ?? Ultimately when we find parent for any node with path compression implemented, it will go at most one level to find absolute parent of the component, right ?
      What I believe is if we are path compressing then we do not need rank to make algo efficient. Please explain if I am wrong.

  • @kumarnishchay7771
    @kumarnishchay7771 2 роки тому +7

    In kruskal algo we check if parents of u and v are different and then only we call the union (u,v) .. since DSU is used at many places if someone tries this code as it is , it will lead to wrong answers. So for anybody reading this comment, always make sure before doing the union that the parent of u and v are different else if they are same no need of union as they already belong to same component.

    • @newbienate
      @newbienate 2 роки тому

      why do we need height or rank if we are path compressing ?? Ultimately when we find parent for any node with path compression implemented, it will go at most one level to find absolute parent of the component, right ?
      What I believe is if we are path compressing then we do not need rank to make algo efficient. Please explain if I am wrong.

  • @stith_pragya
    @stith_pragya 11 місяців тому +1

    Thank You So Much for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @Ravipoddar1999
    @Ravipoddar1999 3 роки тому +33

    Each And Every video till I watched, The explanation is so simple that I understand the concepts from a single play even though I watched videos in 2x 😄.
    Well Explained.
    Thanks for the graph series.
    Please make series on other topics too.

    • @takeUforward
      @takeUforward  3 роки тому +6

      You're very welcome!

    • @samkr200
      @samkr200 3 роки тому +1

      @@takeUforward Bro! Do you post extra videos when we join your channel or is it the same as if we only subscribe?

    • @anmolsharma9539
      @anmolsharma9539 3 роки тому

      @@samkr200 it's same

  • @shubhambhardwaj8894
    @shubhambhardwaj8894 3 роки тому +70

    Please bring tree series too, will remain thankful throughout my life.🙏

  • @lambar0
    @lambar0 2 роки тому

    I believe in the Union function you could keep track of number of connected components…increment it if parent of u not equal to parent of v…if u and v same keep connected components same

  • @deepanshukhanna1780
    @deepanshukhanna1780 3 роки тому +13

    path compression looks so simple in recursion, that's the beauty of recursion.

  • @riddhimanbhattacharya9541
    @riddhimanbhattacharya9541 3 роки тому

    Referred to a many resources on the net but could conveniently understand the concept only from your video. Thank you so much for your efforts

  • @shwetanksingh5208
    @shwetanksingh5208 2 роки тому +2

    You should have put a check in union that if(u != v) then only proceed for union otherwise it will do union for two nodes having same parent and will unnecessarily increase rank of one node

  • @Nitin-wy8wh
    @Nitin-wy8wh 3 роки тому +9

    yrr ye banda kitna awesome hai, awesome explanation bhaiya :)

    • @takeUforward
      @takeUforward  3 роки тому +1

      Keep sharing jahan bhi possible ho paaye. :)

  • @aadeshsharma0001
    @aadeshsharma0001 3 роки тому +5

    the tree series and this graph series have ended my fear of trees and graphs ds.

  • @nitinkumar5974
    @nitinkumar5974 2 роки тому +8

    C++ Code :
    //check if 2 nodes are belong to the same components or not
    #include
    using namespace std;
    void makeSet(vector&parent, vector&rank)
    {
    int n=rank.size();
    for(int i=0;i

  • @nishantbanjade920
    @nishantbanjade920 2 роки тому +4

    I think the time duration of this video has reduced to 14 min and part from 4:50 sec about 5-6 min of explanation is missing

    • @namansharma5128
      @namansharma5128 2 роки тому

      yesss........... rank wala part hi gayab hai

  • @sarthakchaudhary4375
    @sarthakchaudhary4375 3 роки тому +11

    The rank might change during path compression and we have not implemented anything in the findPar function to account for that change. We might be making sub-optimal connections in the union function if the ranks are unreliable

    • @manavarora1130
      @manavarora1130 3 роки тому

      Same doubt

    • @Live-hh6li
      @Live-hh6li 3 роки тому

      I think the same

    • @amanagarwal6897
      @amanagarwal6897 3 роки тому +3

      Refer Tech Dose video for this. His video will exactly clear your doubt. Striver's graph series is great, but in some concepts like this one, it is lacking intuition.

    • @manavarora1130
      @manavarora1130 3 роки тому +1

      @@amanagarwal6897 Thanks

    • @rahulchauhan144
      @rahulchauhan144 2 роки тому

      @@amanagarwal6897 even tech dose video on rank and path compression is not changing the rank during every path compression....you can find any article no one is changing the rank during path compression and the reason i got to know is changing rank would increase the time complexity !

  • @namansharma5128
    @namansharma5128 2 роки тому +3

    16:02 what is the logic of attaching 1 to 4 if we have done the path compression, the height of both the components are same now????

  • @amitmohapatra292
    @amitmohapatra292 2 роки тому

    i think the rank of the 1 will also be 2 coz 0 ---> 1 for the 2 node and 1--->2 for 3 node and another doubt is that the path compression will happens when it will be called like in this case union (*, 7) for the second time then only it will be effective but for the first time it will tranverse for logn times

  • @ankit_irl
    @ankit_irl 3 роки тому +6

    I literally understood this concept in a 24 minute video :0
    Your explanation is neat

  • @kamalarora253
    @kamalarora253 2 роки тому +3

    Please reUpload this video some part is missing around 4:50 @striver

  • @ashishgopalhattimare
    @ashishgopalhattimare 3 роки тому +2

    Very well explained. The Rank and Path Compression is always very confusing, but this made it easy to remember and implement whenever required

  • @manishisaxena5657
    @manishisaxena5657 3 роки тому

    the best series for graph that i have come across till now

  • @-ArbiChaliha
    @-ArbiChaliha 2 роки тому +4

    Sir, have you made videos on heaps and priority queue also?If no,can you please make it sir. It"ll be very helpful.All your contents helps a lot. Thanks!

  • @tejaswarambhe7464
    @tejaswarambhe7464 3 роки тому

    Recursion is beauty, perfect example of recursion importance

  • @newbienate
    @newbienate 2 роки тому

    why do we need height or rank if we are path compressing ?? Ultimately when we find parent for any node with path compression implemented, it will go at most one level to find absolute parent of the component, right ?
    What I believe is if we are path compressing then we do not need rank to make algo efficient. Please explain if I am wrong.

  • @arpanbanejee5143
    @arpanbanejee5143 3 роки тому +1

    Just to reiterate the use of path compression. Is someone is still confused with the optimization-
    at 14:00 secs- using the same example-
    last query union (3,7)
    x= findParent(7); // 7->6->4->4->4 so finally parent is 4 after 4 hops
    y=findParent(3); // 3->1->1 parent 1
    lets say we dn t do path compression we keep the parent array as it is in this step.
    Now the next step is UNION which says -
    lets suppose rank of x > rank of y
    parent[y]=x; rank of 4 is greater than rank of 1
    that means parent[1]=4
    now lets say we have another query (something,7)
    x= findParent(7); // 7->6->4->4->4 again we will have to make 4 hops
    if we would have done path compression then
    x= findParent(7); // 7->4->4 2 hops only.

  • @23butlouder47
    @23butlouder47 2 роки тому +2

    instead of calling the vector rank , u can name it as height , it helps in much better understanding

    • @newbienate
      @newbienate 2 роки тому

      why do we need height or rank if we are path compressing ?? Ultimately when we find parent for any node with path compression implemented, it will go at most one level to find absolute parent of the component, right ?
      What I believe is if we are path compressing then we do not need rank to make algo efficient. Please explain if I am wrong.

  • @shivangsharma4873
    @shivangsharma4873 3 роки тому +1

    When we compress the path,it can also change the rank of any node. Will this impact the logic or time complexity of the algorithm, since we are not handling this change in rank by path compression in our algo??

  • @abhisheksharma1031
    @abhisheksharma1031 3 роки тому

    Simply Awesome !! I dont think I need any other video to understand this now.

  • @NrN_RL_SIDESWIPE
    @NrN_RL_SIDESWIPE 2 роки тому

    Really very helpful ... Crystal Clear!!

  • @dhruvkaran9724
    @dhruvkaran9724 2 роки тому

    for explaining path compression if some array example is used it would have more intuitive. Rest other part of video is super awesome.

  • @StrollerEngineer
    @StrollerEngineer 3 роки тому

    You people are giving back to community. Good job🙌
    Btw any mrwwe fan here 😁

  • @ecea044gauravgogoi2
    @ecea044gauravgogoi2 3 роки тому

    You explained so beautifully by live coding !!!

  • @ayushupadhyaya739
    @ayushupadhyaya739 3 роки тому +6

    why can't we compare parent[2] != parent[3] directly ? since after union operation parent array will contain compressed path only??

    • @takeUforward
      @takeUforward  3 роки тому +10

      No it won’t, may be 3 had a tree. And you connected it to 2, so something below 3 has not been compressed yet.

  • @AnshuKumar-zn1qb
    @AnshuKumar-zn1qb 2 роки тому

    Matlab kyahi padhaye ho striver bhai .... No one can teach better then you...🔥🔥🔥🔥🔥

  • @tarunkumarrajput9154
    @tarunkumarrajput9154 3 роки тому +4

    Best explanation , I really wish this channel grow well, thank u striver .

  • @krishnarajyadav3953
    @krishnarajyadav3953 3 роки тому +2

    Great video mate. I just totally visualised the theory explanation with the code. Fabulous!!!

  • @divyanshmishra8615
    @divyanshmishra8615 3 роки тому

    great explaination brother.......tbh nobody explained it this good along with the code

  • @devanshubilthare5277
    @devanshubilthare5277 3 роки тому

    bhaiya graph series se concept boht acche ho gye, ab ek list of ques aur de do jisse tagdi wali practice ho jaye sare graph algos ki.

  • @ClashWithSwap
    @ClashWithSwap 3 роки тому +4

    In the union operation if the parents are the same then we can just skip it right, rather than entering the else statement and increasing rank[u] by 1 like your code does?

    • @takeUforward
      @takeUforward  3 роки тому +7

      Actually the union is called only if parents are not same, look at the kruskal

    • @ClashWithSwap
      @ClashWithSwap 3 роки тому

      @@takeUforward Alright thanks!

  • @cinime
    @cinime 2 роки тому

    Understood! Super extreme explanation as always, thank you very much!!

  • @setupatel3632
    @setupatel3632 2 роки тому +2

    Dont you need to update rank after path compression ? as the level of the tree will be decreased

    • @kaushikpower
      @kaushikpower 2 роки тому

      I too have this question . Can someone answer?

  • @shanmukhchandrayama8508
    @shanmukhchandrayama8508 3 роки тому +1

    shouldn't the time complexity be o(logn) with path compression and union, why does it become constant time?

  • @ritikkumarjain2094
    @ritikkumarjain2094 3 роки тому

    So simple you made it!

  • @codetochange8
    @codetochange8 2 роки тому

    Beautifully explained...Thanks Striver!!!

  • @ka_ka3141
    @ka_ka3141 2 роки тому

    I almost shouted after I understood how the path compression is done

  • @bhaveshkumar6842
    @bhaveshkumar6842 2 роки тому +1

    You're a blessing

  • @justcode7326
    @justcode7326 2 роки тому

    22:45 If we are using path compression, can we not use parent array directly at line 48???

  • @oqant0424
    @oqant0424 3 роки тому +1

    thanks a lot vaeya ...this is literally very very helpful....

  • @vishaldayalani7748
    @vishaldayalani7748 3 роки тому

    path compression ke time rank bhi update karni padegi na

  • @priyanshubansal6820
    @priyanshubansal6820 2 роки тому

    I think at 5:05 some part of video is missing

  • @SuperWhatusername
    @SuperWhatusername 2 роки тому

    Superb Striver "Understood"

  • @kashishpurswani2284
    @kashishpurswani2284 3 роки тому

    I referred some other sources and TC there was E Log E + E log V. why so and which is correct?

  • @abcefg7045
    @abcefg7045 2 роки тому +1

    sir should we practice questions while watching videos or should we complete the playlist first and then attempt the questions

  • @aasthajha1051
    @aasthajha1051 3 роки тому

    I LOVE THIS !!!!!!!!!!! whataaaaaaaaaaaaaaaa a playlist !!!!!!!!!!!!!😊😊

  • @HarivanshKripaVlogs
    @HarivanshKripaVlogs 3 роки тому +1

    Well explained as expected striver bhaiya, please bring other series too.
    It will be very helpful.
    Thank You.❤️

  • @shreyankshrestha9274
    @shreyankshrestha9274 3 роки тому

    bahut acchay say clear hogaya .

  • @KrishnaSharma-bv5ys
    @KrishnaSharma-bv5ys Рік тому

    why do we not decreasing the rank when we compress the path as height of the tree is reduced.

  • @mayankverma3690
    @mayankverma3690 3 роки тому +1

    Please make a series on dp!!

  • @sanskarkumar2312
    @sanskarkumar2312 2 роки тому +1

    Is this 14 minute video complete?

  • @samkr200
    @samkr200 3 роки тому +2

    Bro! Do you post extra videos when we join your channel or is it the same as if we only subscribe?

    • @takeUforward
      @takeUforward  3 роки тому +8

      Its the same. Its just for appreciation purpose, nothing else :)

  • @adityajoshi3922
    @adityajoshi3922 3 роки тому

    Thank You for Such a Wonderful Explanation

  • @shivamkumar2671
    @shivamkumar2671 2 роки тому

    Something happened with the video, its incomplete. It was fine few days ago.

  • @ritikashishodia675
    @ritikashishodia675 2 роки тому

    Ek level up whaaa bhaiya kya explain kiya 🥳🥳🥳🥳pr bhaiya 4 alpha kya h ye

  • @muhammadlanang644
    @muhammadlanang644 2 роки тому

    Thank you!

  • @siddharthb2226
    @siddharthb2226 2 роки тому

    Why no visualize parent array

  • @ramsai6141
    @ramsai6141 3 роки тому +1

    hey striver...can we do path compression if we implement findparent function with while loop....?

    • @workandearnwithshubhi9050
      @workandearnwithshubhi9050 3 роки тому

      I think not... because inorder to do path compression we need to have some sort of backtracking...which is not available in the iterative version of the function!!!

  • @toshendrabohra_7867
    @toshendrabohra_7867 3 роки тому

    why we are not checking for decreasing the rank in path compression ???

  • @dhivyashri6554
    @dhivyashri6554 3 роки тому

    You rock! I hope god blesses you abundantly!

  • @sherlockholmes1605
    @sherlockholmes1605 2 роки тому

    Exceptional Explanation!

  • @abhishekkumargupta3605
    @abhishekkumargupta3605 2 роки тому

    you have told nothing about the rank array which you have used , please explain it..

  • @pedadagopal3062
    @pedadagopal3062 3 роки тому

    can we use it for directed graph?

  • @kirtanprajapati8464
    @kirtanprajapati8464 3 роки тому

    MindBlowing Explanation bro
    Thank you so much

  • @krishanpratap3286
    @krishanpratap3286 2 роки тому

    in this que can we add a condition in the union func that if(u!=v)? jisse same parent waale pe union find nahi karega yeh please confirm

  • @paragroy5359
    @paragroy5359 3 роки тому +1

    Nice explanation sir.. thanks for the playlist

  • @dreamymoon6135
    @dreamymoon6135 3 роки тому +1

    bhaiya for the challenge we have to send only one screenshot after we do all the challenge videos ?
    and the hashtag is to be used in linkedin while sending u the screenshot? or we can send it personally to your linkedin dm???

  • @sakshamchaturvedi
    @sakshamchaturvedi 2 роки тому +2

    Suppose we have 4 vertices and their values: 55, 65, 22, 32.
    Now, we created a parent array of size 10^5. Cool. Because every vertex gets themselves assigned as their parent initially (i.e. index value will be same as the vertex value.)
    But, inside the makeSet() function, we're running the self-initialization loop for the COUNT of vertices i.e. 4 and not FOR THE vertices' value.
    Eg. In the parent array, we're initializing index 0, 1, 2, and 3 with the same value as the index i.e. 0, 1, 2, 3. BUT, our vertices are actually present at index 55, 65, 22 and 32 respectively which do not get initialized as we only ran the loop for the COUNT of vertices i.e. 4.
    So, shouldn't we run the makeSet() loop for all 10^5 elements. And, doesn't this brings a disadvantage of array space being wasted? :/
    ​ @take U forward

  • @sadashivshinde9150
    @sadashivshinde9150 2 роки тому

    Disjoint set is Taserface of DSA

  • @prazoles4450
    @prazoles4450 Рік тому

    How does the rank work if they are all set to 0? What is updating the rank?

  • @tanayshah275
    @tanayshah275 3 роки тому +1

    what does alpha denotes in Time complexity: 4 x alpha ?

  • @siddharthb2226
    @siddharthb2226 2 роки тому

    What exacty does rank array signify?

  • @huzefataj7694
    @huzefataj7694 2 роки тому

    Python Code:
    n=8
    parent=[[]]*n
    rank=[[]]*n
    def makeSet():
    for i in range(1,n):
    parent[i]=i
    rank[i]=0
    def findpath(node):
    if node == parent[node]:
    return node
    parent[node] = findpath(parent[node])
    return parent[node]
    def union(u,v):
    u=findpath(u)
    v=findpath(v)
    if rank[u]

  • @naro.tam_
    @naro.tam_ 3 роки тому

    WHEN WE ARE DOING COMPRESSION THEN RANK OF PARENT NODE WILL DECREASE?

    • @takeUforward
      @takeUforward  3 роки тому

      Nah let it be, no need to decrease.. as a particular node is getting decreased. There might be other nodes as we

  • @protyaybanerjee5051
    @protyaybanerjee5051 Рік тому

    What is this weird logic where the heights won't increase ? Can anyone explain ? The size of the tree after unite operation would increase, obviously!

  • @ashutoshjha5356
    @ashutoshjha5356 3 роки тому

    very nice explanantion thank you bhaiya

  • @RahulSingh-hq3ny
    @RahulSingh-hq3ny 3 роки тому

    I love the way you explain 🔥❤️

  • @LaughTale1999
    @LaughTale1999 2 роки тому

    if i give u & v with same parent in union operation then the rank of their parent increases according to the union function written in the above code but no component was added, this should be an error.
    please make it clear to me if possible

  • @manishbolbanda9872
    @manishbolbanda9872 2 роки тому

    superb explaination

  • @ShubhamSharma-rv7yg
    @ShubhamSharma-rv7yg 2 роки тому

    Great Explanation.

  • @utkarshmishra6667
    @utkarshmishra6667 3 роки тому

    hey striver, in the makeSet() you wrote i

    • @takeUforward
      @takeUforward  3 роки тому +1

      I just wrote snippet, you can declare globally or pss by parameter

    • @utkarshmishra6667
      @utkarshmishra6667 3 роки тому

      @@takeUforward Ok. Thanks bro.

  • @roushanraj8530
    @roushanraj8530 3 роки тому +1

    Next level explanation 💯💥💥

  • @abhishekkunal8958
    @abhishekkunal8958 3 роки тому

    chaa gye guru ..thoko video pr like

  • @rohitdatta1103
    @rohitdatta1103 3 роки тому

    Well,rank doesn't change in path compression?

    • @takeUforward
      @takeUforward  3 роки тому +2

      Nah, rank does not guarantees to give you the height, but it will assure you to give the heavier tree of both.

  • @workandearnwithshubhi9050
    @workandearnwithshubhi9050 3 роки тому

    Concept katai...killed Dead Slaughtered 😎😎

  • @ashirbad29
    @ashirbad29 3 роки тому

    Got it in a single watch😀

  • @siddharthrawat7063
    @siddharthrawat7063 2 роки тому

    What is n in makeSet() ?

  • @_-6912
    @_-6912 3 роки тому

    Hi Raj, in union operation, instead of parent[u] = v, can we do parent[u] = findPar(v)?

    • @debottammazumder9103
      @debottammazumder9103 3 роки тому +3

      after entering the function, the first 2 lines are used to make u as the parent of u and v as the parent of v,
      see u=findPar(u)
      v=findPar(v)
      so even if you write parent[u] = findPar(v), it won't change anything except calling the findPar() function once more and returning V itself

    • @_-6912
      @_-6912 3 роки тому +1

      @@debottammazumder9103 Thank you Mazumder!!

  • @devanshuagrawal7557
    @devanshuagrawal7557 3 роки тому

    i don't understand why rank is necessary since we are doing path compression at the end all the parents should have only one rank

    • @takeUforward
      @takeUforward  3 роки тому +2

      No.. ranks are needed.. as bada tree chote me add hga, th bade trees k nodes fir path compress krne me time lengy na

    • @devanshuagrawal7557
      @devanshuagrawal7557 3 роки тому

      @@takeUforward yes , I got it thanks

  • @mikelan9854
    @mikelan9854 2 роки тому

    anyone knows which sublime theme he's using?

  • @justinmyth4980
    @justinmyth4980 3 роки тому

    Can someone please clear my dount that in second last main, if statement if(findPar(2)!=findPar(3)) why cant we just check the parent array instead,i mean it do stores the parent right

    • @takeUforward
      @takeUforward  3 роки тому +1

      No.. we always need the root of the tree.

    • @justinmyth4980
      @justinmyth4980 3 роки тому

      Thanks for reply big fan of ur work

  • @CpWithSunil
    @CpWithSunil 2 роки тому

  • @vanshikakapoor85
    @vanshikakapoor85 3 роки тому +1

    You are awesome💯