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!
@@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.
@@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.
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
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!
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!
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
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?
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')
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)
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.
!!!!!!!!!! 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
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)
4:25.
It does matter!
The smaller group becomes part of the greater group, otherwise the worst case runtime would be different.
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
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!
@@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.
@@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.
Such a neat and crisp explanation. People like you are making our lives simpler. Thanks a bunch !!! Keep it up.
I've been trying to understand it for a long time. finally understood. thanks a lot!
That's really encouraging to hear! :)
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
The color grouping is really intuitive. Thank you for the helpful video.
Basically, sort the edge then run Union Find
Great video! Finally I understood this algorithm. I had been trying to understand it for days until I watched your video
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!
Its just greedy search.
i love u
u explained it better than my textbook and professor
regarding applications of mfset so I understand disjoint set better!
You made my life so much easier!
You have really worked hard in designing these colourful ppts/video. Thank you very much.
I swear you are the king of graph theory
Your channel is so underrated. Love it!
Your explanation made it super simple... great work
very adaptive to my brain. thanks !
Fantastic simulation my friend - keep up
Great illustration! This is a great series!
Great explanation
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?
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!
Can you please help me understand how are giving weight to a junction.
Thank you!!!!!By far the best explanation!!!
greatexplanation ,you made it easy
Thanks a lot William 😁😁
Thank you, this is very helpful!
You rock dude, thanks for the video
Just found your channel while studying for my algorithms exam. Cannot thank you enough for making these great videos! You are f@#$!ing awesome!
GL on your exam!
Why B to C instead of G to I or H to C is it because of node size?
This is amazing! Thank you so much for that, really!
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
i don't understand the logic behind each number being assigned to which path.
Any kind of help is aprecciated.
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?
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')
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)
nice explanation, thanks!
Sehr gut erklärt. Vielen Dank :)
Great illustration. Just wanted to add that union-find is an algorithm and not a data structure. Graph is a data structure.
No its not. Read the first line ( written in black color ) of this article :
cp-algorithms.com/data_structures/disjoint_set_union.html
nicely explained. thanks.
This is brilliant, thank you!
how do you assign the edge weights?
Is this algorithm work when graph is directed?
Excellent explaination! :)
hello,Willliam can you teach me how to do the ppt, I am very interested
Well presented.
nice explanation thanks
thank you so much
are the edge weights arbitrarily assigned?
A weighted graph will come with predetermined edge weights.
I'm still not sure where "size" comes into play
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.
I am your fan, bro!
Thanks
Great job, keep it up!
mind blowing
Very interesting, thanks ^^
Great vid
thank u
very good
done
ehrenmann
!!!!!!!!!! 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
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)
All these "Algorithms", are recipes that a six year old might use when introduced to such problems with no prior knowledge. Very simple !
Thanks