Union Find in 5 minutes - Data Structures & Algorithms

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ธ.ค. 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
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @potatocoders2028
    @potatocoders2028  3 ปีที่แล้ว +12

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

  • @robirahman
    @robirahman 2 ปีที่แล้ว +201

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

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

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

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

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

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

  • @richtigmann1
    @richtigmann1 6 หลายเดือนก่อน +1

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

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

    Love it man, short and clear!

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

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

  • @danielwang8833
    @danielwang8833 6 วันที่ผ่านมา

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

  • @user-dc7oj3el7o
    @user-dc7oj3el7o 2 ปีที่แล้ว +5

    So practical and helpful video=) Thanks

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

    Loved this bite sized explanation!

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

    Amazing! It's very clear. Thank you

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

    wonderful explanation! Thanks mate!

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

    It so simple and straight forward. Thanks you so much

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

    under-rated video.. thanks for the explanation

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

    Perfectly explained. Thanks

  • @Coffeycup27
    @Coffeycup27 3 ปีที่แล้ว +22

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

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

    Wow, thanks a lot, great explanation!

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

    Best illustration... please provide more content

  • @luisgualpa7019
    @luisgualpa7019 3 ปีที่แล้ว +8

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

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

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

  • @nightmare_9
    @nightmare_9 3 ปีที่แล้ว +4

    Crystal clear and precise...Thanks!!

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

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

  • @mykz814
    @mykz814 หลายเดือนก่อน +1

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

  • @BobTheScience
    @BobTheScience 5 หลายเดือนก่อน +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.

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

    Amazing! thank you!

  • @pieterjanvandecasteele135
    @pieterjanvandecasteele135 3 ปีที่แล้ว +4

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

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

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

  • @Anirozannay
    @Anirozannay 3 ปีที่แล้ว +5

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

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

      Thank you! We hope it has helped!

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

      @@potatocoders2028 it helped me a lot, thank you man!

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

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

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

    thanks a lot for explaining in few minutes.

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

    fantastic video! thanks!!!

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

    Thank you so much this is great!

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

    Awesome explanation, thank you!

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

    Short and clear, thanks

  • @MinhPham-eh6lr
    @MinhPham-eh6lr ปีที่แล้ว

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

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

    best explanation every ! :D

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

    very clear! Thanks!

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

    Thank you! This was very helpful

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

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

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

      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.

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

    Excellent video, thanks!

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

    Thank you very much. Nice video.

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

    Thank you so much man

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

    Great explanation

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

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

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

      Wow, thank you for the great comment! :)

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

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

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

    THANK YOU!

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

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

  • @chrisdahms9682
    @chrisdahms9682 11 หลายเดือนก่อน +1

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

  • @tenminuteamateurhour
    @tenminuteamateurhour 5 หลายเดือนก่อน +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.

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

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

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

    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 ✨

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

    Good content!

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

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

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

    You got a video on path compression?

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

    good video

  • @JackSparrow-vv2uq
    @JackSparrow-vv2uq 3 ปีที่แล้ว

    Thank you

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

    Thanks Mr. Tater.

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

    wow!

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

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

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

    awesome

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

    Subbed 💛

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

    Thanks

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

    beauty

  • @Luca_040
    @Luca_040 หลายเดือนก่อน +1

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

  • @kmishy
    @kmishy 2 ปีที่แล้ว +15

    algorithm was easy but you should write code and execute

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

      That’s how Algo is studied

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

      Yes important is implementation

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

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

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

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

    This really didn't make sense