I recently came across D.S.U. and its applications on the tree.As i was solving the related problems, I got Time Limit Exceeded error in some so i read the tutorial again and there I found that an improvised version of the normal union is weighted-union. In this weighted union operation, we make the smaller sized subset's root as child of larger sized subset's(among the two) root. How is it benefiting us? Link to Tutorial
2
There are 2 best solutions below
0
Daniel
On
Doesn't make difference in correctness' point of view, but it is usually faster.
Check this example:
In the first case, you put the biggest set as child of the smallest. You can see that in this case, if you try the find method in the deepest node, it will perform 3 steps. This doesn't happen in the second case.
This is not a rule but pratically it's what happens.
Related Questions in ALGORITHM
- MCNP 6 - Doubts about cells
- Given partially sorted array of type x<y => first apperance of x comes before first of y, sort in average O(n)
- What is the algorithm behind math.gcd and why it is faster Euclidean algorithm?
- Purpose of last 2 while loops in the merge algorithm of merge sort sorting technique
- Dots and Boxes with apha-beta pruning
- What is the average and worst-case time complexity of my string searching algorithm?
- Building a School Schedule Generator
- TC problem 5-2:how to calculate the probability of the indicator random variable?
- LCA of a binary tree implemented in Python
- Identify the checksum algorithm
- Algorithm for finding a subset of nodes in a weighted connected graph such that the distance between any pair nodes are under a postive number?
- Creating an efficent and time-saving algorithm to find difference between greater than and lesser than combination
- Algorithm to find neighbours of point by distance with no repeats
- Asking code suggestions about data structure and algorithm
- Heap sort with multithreading
Related Questions in BREADTH-FIRST-SEARCH
- Leetcode BFS Set insertion giving TLE (200. Number of Islands)
- Why DFS and not BFS for finding cycle in graph
- Approach for solving a Breadth first search(BFS) components problem
- Breadth First Search Causing Heap Memory Error With exponentially growing graph
- Undirected Graph, get list of all nodes that can be visited
- Struggling to separate breadth first search section of my main loop into its own function
- Algorithm to connect every node between each level of a graph / tree
- Generate tuples of non-negative integers closed under promotion?
- Finding goal through BFS and DFS
- Number of islands problem Leetcode recursive DFS and non recursive BFS
- Print path to node in a 2D dictionary-structured graph
- BFS Maximize Minimum Distance from a Monster along a path
- Find the path with the minimum number of transfers between stations?
- BFS ever failing for a unweighted and undirected graph?
- How would I show the parenthesis theorem doesn't work for Breadth First Search?
Related Questions in DISJOINT-SETS
- Complexity in Union of disjointed sets with lists
- Disjointed-sets-forest Why is the heigth of a tree with n elements log n?
- How to draw a visualization of disjoined set structure for the following p[] array?
- Can I change the solution to the DSU painting subarrays question?
- Disjoint Sets on finding Path
- Consequences of disjoint set union path compression in union by size
- Union-Find: Do you do find operations in the union when using path compression
- Finding the minimum cost for 'm' compatible elements for group 1 and group 2 (algorithm)
- Find all disjoint subsets of a binray matrix in Pyhton
- minimum collection of vertice disjoint path that covers a given vertice set
- Merging database tables using rank and path compression heuristics
- Disjoint Union of Strings
- How to merge sets in better then O(len(set))?
- Do we need to update the ranks while path compression in Disjoint set data structure?
- The Value of Friendship : Disjoint set union
Related Questions in DISJOINT-UNION
- disjoint union in julia
- Typescript: Checking disjoint union with extra null-checks
- Disjoint Union of Strings
- How to merge sets in better then O(len(set))?
- Insert items into a list based on their occurrence
- Representatives in Disjoint Set Union
- Getting ambiguous error for Vector. How to Fix it?
- Returning the right number of islands using Union Find
- Disjoint Set Union
- Leetcode Number of Province not passing edge case last few edge case
- Distance to representative in Disjoint set union data structure
- Amortized complexity of the union operation
- How to solve this Union-Find disjoint set problem?
- mother vertex using disjoint dataset in directed graph
- union find set algorithm return parent[v] = find_set(parent[v]); means
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?

You should realise the purpose/logic behind weighted union-find.
First, why do we need weighted union-find? That's because a simple inefficient union-find can lead to an unbalanced tree. In the worst cast a linked list. What's the complexity of traversal over a linked list?
O(N). That's the worst complexity when using a simple union-find.Our goal is - balancing the hence-formed tree.
How and why weighted union-find works? It's a simple optimization by just keeping the size of each subset and making the smaller subset child of the larger subset when performing union between the two.
Why this works? Because, as mentioned, our goal is to balance out the tree when doing the union and not unbalance it. If you make the smaller subset a child of the larger subset, the height of the overall tree is not increasing (obv when the sizes are equal we handle it differently :/). On the other hand, if you make the bigger subset a child of the smaller tree, you know what will happen.
Using just this optimization we improve the worst case time complexity from
O(N)toO(log2(N))- because the height of the tree will never go beyondlog2(N)There's another optimization that can be done along with this which will take the complexity down even further. Your link probably has it.