Union Find in 5 minutes - Data Structures & Algorithms

Sdílet
Vložit
  • čas přidán 2. 12. 2020
  • This video covers one of the most popular data structures and algorithms topic "Union Find". This is an instruction showing how to run Union-Find on a graph, with examples and code. Union Find and Disjoint Set are not as difficult as we think! 😊
    #graph #data_structures #algorithms #faang #Union-find #disjoint set #data-structures #recursion #leetcode #interview #algo #disjoint-sets #disjoint sets
    Facebook: / potatocoders
    Linkedin: / potato-coders
  • Věda a technologie

Komentáře • 88

  • @potatocoders2028
    @potatocoders2028  Před 3 lety +12

    Comment down any questions below! Please give it a like and subscribe to support our channel.
    Cheers,
    Potato Coders 🙂

  • @robirahman
    @robirahman Před 2 lety +211

    You forgot to mention you have to union the shorter tree into the longer tree! Otherwise the trees in a set of n elements can be up to n elements long (just a straight chain, instead of branching) and you don't get log(n) complexity.

    • @tunguyenxuan8296
      @tunguyenxuan8296 Před rokem +3

      true. if we represent the group as a tree, it will take O(n) for find function in worst case. We better to represent a group as a trie, where all children point to one root.

    • @neerajvshah
      @neerajvshah Před rokem +1

      @@tunguyenxuan8296 Trie's don't change the runtime complexity, a trie is just a type of tree (keep in mind not all trees are binary trees, a trie is just an k-ary tree). As Robi mentioned it's path compression which makes union find efficient. This video is incorrect - O(logn) is never the time complexity for union find. It's either O(n) without path compression or O(a(n)) with path compression.

    • @lightlysal
      @lightlysal Před 11 měsíci

      ​​​@@neerajvshahWhat's a(n) mean in your explanation? Is it the amount of times we "compress" a parent? I thought when people say the time complexity of union find they mean O(log N) on average over all use cases

    • @sirmonke8946
      @sirmonke8946 Před 9 měsíci

      @@lightlysal a(n) is the reverse ackermann function. It grows insanely slow (slower than any log) and can basically be rounded to constant time

    • @MrKlaygomes
      @MrKlaygomes Před 8 měsíci

      @@neerajvshahpartially correct, if it is only weighted without path compression it is O(log n).

  • @Dev_God
    @Dev_God Před 2 lety +2

    Thanks for sharing! It's very clearly and efficiently explained!

  • @richtigmann1
    @richtigmann1 Před 7 měsíci +2

    This was actually insanely helpful in understanding the concept. Thanks!

  • @donaldcodes
    @donaldcodes Před 2 lety +1

    Love it man, short and clear!

  • @adam23sp
    @adam23sp Před 2 lety +1

    Amazing video, very easy to follow, thank you! 🎉

  • @user-dc7oj3el7o
    @user-dc7oj3el7o Před 2 lety +5

    So practical and helpful video=) Thanks

  • @samruddhisaoji7195
    @samruddhisaoji7195 Před 2 lety

    Loved this bite sized explanation!

  • @chrismiaomiao9426
    @chrismiaomiao9426 Před rokem

    Amazing! It's very clear. Thank you

  • @Coffeycup27
    @Coffeycup27 Před 3 lety +23

    Awesome video, thank you! Question though; isn't the find operation O(n) time? I'm reading around that it isn't until you use 'Union By Rank' that it becomes O(log(n)).

  • @arjunv7055
    @arjunv7055 Před rokem

    wonderful explanation! Thanks mate!

  • @manojmpatil1269
    @manojmpatil1269 Před rokem

    Perfectly explained. Thanks

  • @paccini1
    @paccini1 Před 2 lety

    Wow, thanks a lot, great explanation!

  • @jubiliudu
    @jubiliudu Před 2 lety +1

    Amazing! thank you!

  • @nghiapham1632
    @nghiapham1632 Před 8 měsíci

    It so simple and straight forward. Thanks you so much

  • @JamesBrodski
    @JamesBrodski Před rokem +1

    Thank you so much this is great!

  • @danielwang8833
    @danielwang8833 Před 26 dny

    Really clear and concise explanation. This video helped a ton.

  • @shamitfatin3516
    @shamitfatin3516 Před 2 lety

    under-rated video.. thanks for the explanation

  • @luisgualpa7019
    @luisgualpa7019 Před 3 lety +9

    This video has saved me many hours of my life. Thank you for this.

  • @ahmetemin7572
    @ahmetemin7572 Před rokem

    Short and clear, thanks

  • @nguyennguyendinh5215
    @nguyennguyendinh5215 Před 4 měsíci

    very clear! Thanks!

  • @Hdhdushzhz57743
    @Hdhdushzhz57743 Před 3 měsíci

    Thank you! This was very helpful

  • @nightmare_9
    @nightmare_9 Před 3 lety +4

    Crystal clear and precise...Thanks!!

    • @potatocoders2028
      @potatocoders2028  Před 3 lety +1

      Glad it was helpful! Also I like your profile picture :)

  • @jakobmusic9187
    @jakobmusic9187 Před rokem

    fantastic video! thanks!!!

  • @kmishy
    @kmishy Před 2 lety +1

    Best illustration... please provide more content

  • @gdthegreat
    @gdthegreat Před 2 lety

    thanks a lot for explaining in few minutes.

  • @alesblaze4745
    @alesblaze4745 Před 2 lety

    wow , i must say that this was really easy and nice video , really nice explanation

  • @pieterjanvandecasteele135

    I was always stuck with the implementation of UF, now I finally got it, thanks!

  • @Anirozannay
    @Anirozannay Před 3 lety +5

    You explained it so well! Thank you for your video!

  • @bozecki
    @bozecki Před 2 lety +1

    Thanks bro, now I finally understand it for tommorow exam ;p

  • @MinhPham-eh6lr
    @MinhPham-eh6lr Před rokem

    Thanks for the concise and easy-to-understand explanation!

  • @persas1683
    @persas1683 Před rokem

    Thank you very much. Nice video.

  • @samwilson4597
    @samwilson4597 Před rokem

    Thank you so much man

  • @mykz814
    @mykz814 Před měsícem +1

    bro... come back ur vids r so good 😭

  • @Baal3033
    @Baal3033 Před 3 lety +2

    Awesome explanation, thank you!

  • @jashmerchant5121
    @jashmerchant5121 Před 4 měsíci

    Solved my hours long quandary in 5 minutes. Thank you!

  • @Shasol
    @Shasol Před 3 lety +2

    How do you select the representative? I struggle to understand that part of it.

  • @piotr780
    @piotr780 Před rokem

    best explanation every ! :D

  • @tudorradu5848
    @tudorradu5848 Před 2 lety

    THANK YOU!

  • @civilspot5912
    @civilspot5912 Před 4 měsíci

    Great explanation

  • @SterlingVanlaere
    @SterlingVanlaere Před 3 lety +2

    Excellent video, thanks!

  • @DeepakSankar88
    @DeepakSankar88 Před 11 měsíci

    Great explanation. Was able to recall what I had learnt a while back. Thank you!

  • @jakubucinski
    @jakubucinski Před 2 lety +19

    There is a little technicality, that when you do find(x), you want to do path compression, by connecting each vertex on the corresponding branch (the branch where x is) to the root. This results in subsequent find operations being cheaper (and you only double your cost one time).

    • @rm-stl
      @rm-stl Před rokem

      I’ve seen this done but never heard it explained. It looked odd to have a side effect in a find(). Makes perfect sense now. Thanks

    • @stevenhicks3321
      @stevenhicks3321 Před rokem

      I also noticed this and thought it may be better to use sets if you don't care about the three structure of the disjoineted set.

  • @aadityakiran_s
    @aadityakiran_s Před 11 měsíci

    You got a video on path compression?

  • @jeresher6821
    @jeresher6821 Před 3 lety +3

    Best video I've found on UF. Thank you!

  • @WebSurfingIsMyPastime

    This video was great! definitely not something that's very clearly covered in alot of other sources

  • @JackSparrow-vv2uq
    @JackSparrow-vv2uq Před 3 lety

    Thank you

  • @chrisdahms9682
    @chrisdahms9682 Před 11 měsíci +1

    Bro this topic is confusing AF, but at least this vid makes it somewhat understandable

  • @mahlahlana
    @mahlahlana Před 3 lety

    wow!

  • @Rockyzach88
    @Rockyzach88 Před rokem

    Thanks Mr. Tater.

  • @tenminuteamateurhour
    @tenminuteamateurhour Před 6 měsíci +3

    Correction, this does not run in O(logn), but in O(n). To get O(logn) optimization, you need to use union by rank or by size.

  • @waynegreen7970
    @waynegreen7970 Před 3 lety +3

    Good content!

    • @potatocoders2028
      @potatocoders2028  Před 3 lety

      Thank you. Let me know if there is any more content that you would like to see! 😀

  • @chku
    @chku Před 6 měsíci

    This video is so gud but looks like the channel ain't active anymkre 🥺💔 I really loved the explanation n quality so thankuu n hope u r doing well ✨

  • @corgirun7892
    @corgirun7892 Před rokem

    awesome

  • @pamp3657
    @pamp3657 Před 11 měsíci

    good video

  • @KoScosss
    @KoScosss Před rokem

    Thanks

  • @BobTheScience
    @BobTheScience Před 5 měsíci +2

    To optimize it, the find function should set the parent of it's parent to find(x). (This will probably help a lot)
    function find(x):
    if Parent[x] != x: parent[x] = find(parent[x])
    return parent[x]
    If I'm wrong please tell me.

  • @rasheedlewis1
    @rasheedlewis1 Před 9 měsíci

    The Big-Theta runtime for find() would be Θ(n).

  • @algorithmdatastructures9244

    Subbed 💛

  • @samuraipiyush
    @samuraipiyush Před 8 měsíci

    beauty

  • @kmishy
    @kmishy Před 2 lety +15

    algorithm was easy but you should write code and execute

  • @Luca_040
    @Luca_040 Před 2 měsíci +1

    Wer sieht das auch für's HAW Studium?

  • @jashmerchant5121
    @jashmerchant5121 Před 4 měsíci

    Python code:
    # WITH Path Compression
    def find(self, parent, x): # finds root or representative of disjoint-set/union
    if parent[x] != x:
    parent[x] = self.find(parent, parent[x]) # path compression step
    return parent[x]
    def union(self, parent, x, y):
    root_x = self.find(parent, x)
    root_y = self.find(parent, y)
    parent[root_x] = root_y
    # WITHOUT Path Compression
    def find(self, parent, x): # finds root or representative of disjoint-set/union
    if parent[x] != x:
    return self.find(parent, parent[x])
    return parent[x]
    def union(self, parent, x, y):
    root_x = self.find(parent, x)
    root_y = self.find(parent, y)
    parent[root_x] = root_y

  • @moshebixenshpaner5094
    @moshebixenshpaner5094 Před 2 lety +1

    There are two issues here:
    1. When performing union, you set parent of the smaller set to the representative of the larger set.
    2. When finding a path, you compress the path on your way back, improving O(logn) ops to O(log* n) in average.

    • @CSSFACE
      @CSSFACE Před 2 lety +2

      what do you mean by O(log* n) what is the *?

  • @quantum_psi
    @quantum_psi Před 2 lety

    This really didn't make sense