Union Find Kruskal's Algorithm

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ก.พ. 2025
  • Introduction to Kruskal's Algorithm
    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...
    ====================================
    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 ===================================
    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

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

  • @madanrajvenkatesan518
    @madanrajvenkatesan518 5 ปีที่แล้ว +36

    Such a neat and crisp explanation. People like you are making our lives simpler. Thanks a bunch !!! Keep it up.

  • @andreas9109
    @andreas9109 6 ปีที่แล้ว +136

    4:25.
    It does matter!
    The smaller group becomes part of the greater group, otherwise the worst case runtime would be different.

    • @TheDemeaN
      @TheDemeaN 4 ปีที่แล้ว +13

      yas it does matter but I think he was saying in the context of find the mst in the graph, like imagine an exam exercise where you need to draw the mst

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

      Merging a smaller group into a larger group would require fewer operations, but it does not affect the worst-case runtime. Please let me know if I'm wrong!

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

      @@JanacMeena It actually does! Weighted union keeps tree depth below log(n), even in the worst case (can prove by induction), so find is O(log(n)), while without weighted union, tree depth can be n in the worst case, leading to O(n) find. Applied to Kruskal's algorithm, this is the difference between an overall O(mlog(n)) and O(mn) complexity.

    • @SAJID-zs2gf
      @SAJID-zs2gf ปีที่แล้ว +1

      @@lordquaggan during each find operation you can update the parent of children and attach children directly to it's top-most parent (root node), then in that case tree-depth won't reach O(n), since at every call to find(), the children node will be attached to root node of the whole group.

    • @tanzimchowdhury320
      @tanzimchowdhury320 14 วันที่ผ่านมา

      you must be fun at parties

  • @adityabhashkar3405
    @adityabhashkar3405 7 ปีที่แล้ว +34

    I've been trying to understand it for a long time. finally understood. thanks a lot!

  • @chepaiytrath
    @chepaiytrath 4 ปีที่แล้ว +10

    I saw your Prim's explanation using PriorityQueue first. Kruskal's using PriorityQueue and Union Find was a piece of cake thence. Your explanations are great and so is your code. Thanks a lot

  • @Megan-gl7pi
    @Megan-gl7pi 4 ปีที่แล้ว +5

    The color grouping is really intuitive. Thank you for the helpful video.

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

    This Algorithm is crazy!! I find beautiful how people come with these solutions. This is a beautiful application of Data Structures to simplify hard problems to solve. Just waw! Oh and Thanks William for bringing the quality content as usual!

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

    Great video! Finally I understood this algorithm. I had been trying to understand it for days until I watched your video

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

    Your channel is so underrated. Love it!

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

    i love u
    u explained it better than my textbook and professor
    regarding applications of mfset so I understand disjoint set better!

  • @gyudols
    @gyudols 7 ปีที่แล้ว +17

    You made my life so much easier!

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

    You have really worked hard in designing these colourful ppts/video. Thank you very much.

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

    I swear you are the king of graph theory

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

    Basically, sort the edge then run Union Find

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

    This was absolutely freaking great. I love this stuff, u explained it so well. Thank you for reminding me why I love what I do. Keep up the great work!

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

    Your explanation made it super simple... great work

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

    Just found your channel while studying for my algorithms exam. Cannot thank you enough for making these great videos! You are f@#$!ing awesome!

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

    Fantastic simulation my friend - keep up

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

    Great illustration! This is a great series!

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

    very adaptive to my brain. thanks !

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

    You rock dude, thanks for the video

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

    Thank you!!!!!By far the best explanation!!!

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

    This is amazing! Thank you so much for that, really!

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

    Thank you, this is very helpful!

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

    Sehr gut erklärt. Vielen Dank :)

  • @saidathanikhil.k6415
    @saidathanikhil.k6415 2 ปีที่แล้ว

    Great explanation

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

    Thanks a lot William 😁😁

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

    greatexplanation ,you made it easy

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

    nice explanation, thanks!

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

    This is brilliant, thank you!

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

    nicely explained. thanks.

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

    It's not important, but at 2:55 when you said 'I' belongs to group orange but 'C' doesn't have a group yet...I said "Oh I see". Just a funny moment. Happy learning ya'll lol

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

    Excellent explaination! :)

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

    thank you so much

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

    nice explanation thanks

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

    at 0:36 CJ=8 but at 2:02 CJ=2 ? AD's weight is changed too. Are you using different graphs?

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

    mind blowing

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

    Well presented.

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

    Great job, keep it up!

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

    Great illustration. Just wanted to add that union-find is an algorithm and not a data structure. Graph is a data structure.

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

      No its not. Read the first line ( written in black color ) of this article :
      cp-algorithms.com/data_structures/disjoint_set_union.html

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

    Very interesting, thanks ^^

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

    I am your fan, bro!

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

    Thanks

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

    at 5:20 you say we've found the minimum spanning tree, but how did you know that they were all connected at the point? Does Kruskals algorithm keep track of group size at each root node?

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

      The would all have the same 'root' parent. That's how you know the algorithm is complete. When every vertice has the same parent (belongs to the same 'group')

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

      You can keep track of group count. At the start, each node is a group, so it'll be the node count. Decrease the count on union. Stop immediately when the group size is one instead of going through remaining edges (which won't be added anyway)

  • @YT.Nikolay
    @YT.Nikolay 2 ปีที่แล้ว

    Thanks for the video, love your channel! Why did you stop after the pair "B to C"? how do I know when to stop iterating over the list on left?

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

    thank u

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

    ehrenmann

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

    Can you please help me understand how are giving weight to a junction.

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

    Great vid

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

    i don't understand the logic behind each number being assigned to which path.
    Any kind of help is aprecciated.

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

    Why B to C instead of G to I or H to C is it because of node size?

  • @dimitrijs.869
    @dimitrijs.869 6 ปีที่แล้ว

    very good

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

    how do you assign the edge weights?

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

    are the edge weights arbitrarily assigned?

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

      A weighted graph will come with predetermined edge weights.

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

    Is this algorithm work when graph is directed?

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

      Yes

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

    I'm still not sure where "size" comes into play

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

      Whether you merge the smaller group into the larger group or vice versa doesn't matter. It's just a good heuristic to use for efficiency of the Union find if you merge the smaller group into the larger one.

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

    done

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

    !!!!!!!!!! WAIT ,
    The title of your video is very misleading . It should say something like "Application of Union Find data structure (Kruskal's Algorithm)" . Cause when you say adding "C and J" will create a cycle ,that is where we need to know how union Find Algo works .
    else video was awesome

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

      I know this is obviously very late, but for others reading this, just in case...
      It has nothing to do with the union find algorithm, but with Kruskal's algorithm, which tries to find a minimum spanning tree. A minimum spanning tree is the minimum set of weighted edges needed to connect all nodes. If we select edges that form cycles, then we have by default not found a minimum spanning tree. It doesn't even have anything to do with a cycle itself, it's simply because since that node was already "spanned" before (aka already in the group) we did not have to actually use that edge to visit it, and we needlessly incurred the cost by travelling over that vertex, which violates the invariant of the minimum spanning tree algorithm.
      A scenario that happens to be equivalent to creating cycles. (because we are reaching a node in the group, from a node that is also in that group => cycle)

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

    All these "Algorithms", are recipes that a six year old might use when introduced to such problems with no prior knowledge. Very simple !

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

    Thanks