Union Find Path Compression

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ต.ค. 2024
  • Related Videos:
    Union find intro: • Union Find Introduction
    Union find kruskal's algorithm: • Union Find Kruskal's A...
    Union find union and find: • Union Find - Union and...
    Union find path compression: • Union Find Path Compre...
    Union find code: • Union Find Code
    Data Structures Source Code:
    github.com/wil...
    My website: www.williamfise... ===================================
    Practicing for interviews? I have used, and recommend `Cracking the Coding Interview` which got me a job at Google. Link on Amazon: amzn.to/3cvMof5
    A lot of the content on this channel is inspired by the book `Competitive Programming` by Steven Halim which I frequently use as a resource and reference. Link on Amazon: amzn.to/3wC2nix

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

  • @vishalmishra7018
    @vishalmishra7018 5 ปีที่แล้ว +18

    Thank you for explaining these concepts so beautifully. Please keep posting new videos. Thank you so much.

  • @ErnestoEnriquez-du4jj
    @ErnestoEnriquez-du4jj ปีที่แล้ว +6

    Holy mother of clarity. Great explanation, bro.

  • @khadijahflowers5566
    @khadijahflowers5566 6 ปีที่แล้ว +37

    Just to be clear, path compression happens during a find operation AND union operations?
    Also, why didn't you also compress A and C to point to E?

    • @WilliamFiset-videos
      @WilliamFiset-videos  6 ปีที่แล้ว +50

      Q: "Just to be clear, path compression happens during a find operation AND union operations?"
      A: Yes! In fact, in the code implementation the union operation calls the find method which is where the path compression is done.
      Q: "why didn't you also compress A and C to point to E?"
      A: We lazily do path compression, so It only applies to the nodes which we unify.

    • @lamihh
      @lamihh 4 ปีที่แล้ว +16

      this comment should be pinned

  • @kutilkol
    @kutilkol 5 ปีที่แล้ว +9

    4:51 I think "compressing" J to H is not justified in vid. you are not doing Union(J,H) nor Find(J) :(

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

      J and G have the same root, which is H. The union function calls find(j) and find(g)

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

    Great video!
    Just one doubt. I saw that people already asked why C and A are not connected to E, after compression, and you already answered, but still cant understand :(

  • @shijieru
    @shijieru 6 ปีที่แล้ว +9

    In around 4:11, for operation Union(A,C), I don't understand why the result is B points to C instead of B points to D?

    • @WilliamFiset-videos
      @WilliamFiset-videos  6 ปีที่แล้ว +6

      It could go eitherway, it does not matter as long as the components are unified.

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

    At 4:09, after Union(H, F), why H points to E? H's group is larger.

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

      it doesn't make a difference
      you can do it both ways

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

      only condition is they should belong to the same group

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

    Hi, William! Do you link some nodes like (C, D) and (E, F) (using different ways. It's just for this example or have some reason?
    Hi William! You link some nodes as (C, D) and (E, F) using different ways (left - > right and left < - right). A little different from the previous video. It's just for this example, or is there some special reason? Thanks, in advance!

    • @WilliamFiset-videos
      @WilliamFiset-videos  7 ปีที่แล้ว +3

      Hi Thiago, if you're referring to the first example that's just a hypothetical setup to illustrate path compression. The actual direction in which the nodes are linked together does not matter, your union find will still work correctly. However, from experience, if you merge the smaller group into the larger group you can get a mild performance boost.

  • @loloioi
    @loloioi 5 ปีที่แล้ว +13

    Hey William, I'm kind of confused approx @5:00 when you do the union(J,G). Why are you path compressing based on the new graph? The graph has yet to merge together. I think it would make sense if the graph you are merging into can be path compressed.
    For example, lets say you have H as root node w/ node I pointing to H, and j pointing to I. There is another directed graph where F is pointing to E.
    Example 1) Suppose you call union(F,H). Suppose E points to H. Then F should still point to E but E now points to H and H having it's original children nodes. F is not apart of H and you are calling find(F) which has E as its root.
    Example 2) Suppose you call union(F,I). Then, path compression should happen and I, J and E is pointing to H. Also, F is pointing to E still.

  • @therealbnut
    @therealbnut 2 หลายเดือนก่อน

    Hey, just wondering why at 3:22 you wouldn’t compress all the traversed nodes when finding F to point to G instead, creating a star topology. It seems like this wouldn’t require any more work and would be much more optimal. Sorry to bring this up after so long, haha.

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

    Thank you! Great explanation.

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

    at 3:27, wouldn't it have been better if everything pointed to G?

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

    It seems it would be better to set the new root of a union as the root of the tree with the larger height so that the resulting tree has the smallest height possible. See union(H,F) @ 4:07.

    • @WilliamFiset-videos
      @WilliamFiset-videos  6 ปีที่แล้ว +2

      I did that setup at 4:07 intentionally to compare how beneficial path compression can be. You're correct in saying that as a heuristic you want to merge the smaller tree into the larger one. I've done imprical tests in the past and this has given me a performance boost.

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

    My version of union-find doesn't use this path compression method. My version makes the representative of each node union together and all nodes from the smaller group enter the larger group, setting it's representative to the representative of the larger group.

  • @t_min_o
    @t_min_o 2 หลายเดือนก่อน

    COuld have explained more clearly as to how it is compressed in each step!

  • @hamza-chaudhry
    @hamza-chaudhry 5 หลายเดือนก่อน

    3:22 Would you not make E, D, C, B, A parents all G?

  • @Yan-rv8mi
    @Yan-rv8mi 4 ปีที่แล้ว +3

    5:25 finding A or C at this point, still take more than one hop (in this case, 2 hops) since they're not directing pointing at E. Could you prove that such 2 hops in worst case? is it still constant time, or become linear time?

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

      He had not enough space to represent all the nodes pointing to E around it that's why he didn't pointed A and C to it.

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

    Thank you!

  • @Sulerhy
    @Sulerhy 3 หลายเดือนก่อน

    Thank you so much it is extremely helpful. One image's worth 1000 of words

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

    So you use Union Rank (aka Weighted Quick Union) together with Path compression. Which ends up with T O(1) find and union?

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

    Great GREAT video!!!!!!! GREAT!!

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

    At 3:24, should we also point A B C D E to G instead of F?

  • @sudonick-kn5zn
    @sudonick-kn5zn 10 หลายเดือนก่อน

    hello prime's chat!

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

    Savior. Thanks for this amazing content ! :)

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

    What if I want to increase the size of my array? Initially the array is initialized as 100 say, and I now want to add new element, how does this algorithm tackle that?

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

    Why don't we compress the whole tree after union rather than just the nodes we would like to merge while searching for their roots ?

    • @WilliamFiset-videos
      @WilliamFiset-videos  7 ปีที่แล้ว +2

      The answer is we're compressing the whole tree is a lazy fashion. Why bother compressing nodes we're not going to use?

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

      @@WilliamFiset-videos lazy compress :)

  • @subee128
    @subee128 5 หลายเดือนก่อน

    Thanks

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

    brilliant and easily explained thhanks

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

    I have one doubt, if we do path compression, we would lose our previous root nodes, so we cannot revert them back :(

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

      We don't need to revert back, since we never un-union

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

    Does path compression implies using trees as a data structure for the union find, and without compression means relying on linked lists?

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

      A bit late, but no. Path compression does not impose an underlying implementation data structure. You can achieve path compression using an array by modifying the stored values

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

    Love the way it's explained.

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

    Great video series

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

    All 4 videos on this topic are a gem!

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

    Good

  • @柯科科-d7p
    @柯科科-d7p 3 ปีที่แล้ว

    thank you!!

  • @z08840
    @z08840 3 ปีที่แล้ว +2

    it's not clear at this stage what's the point of all of this - since all nodes are connected you can simply point all nodes to one - no need for "instructions". If the point was to show disjoint components then this example is awful - there is no disjoint components.

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

    5:10 Something that confuses me is that we are compressing the path during union command, but in your source code we compress during find() method

    • @domicasantoso96
      @domicasantoso96 3 ปีที่แล้ว +2

      www.geeksforgeeks.org/union-find-algorithm-set-2-union-by-rank/
      I think the general case is that whenever we execute a union call, we run a find on the two arguments as well.

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

    you are genius, one of the best explanation!!!

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

    it's a kind of bad animation. If the animation made more clear, then it is easier for beginners to understand more.

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

    omg stop using purple, im color blind, cant distinguish subtle difference between blue and purple. Green would be good!!