A union/find data structure is a data structure used for maintaining a partition of a set.
Questions tagged [union-find]
184 questions
8
votes
1 answer
Application of Union+Find algorithm(Disjoint Set)
Problem Statement:
Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number).
Given some queries, return the answers. If the answer does not exist, return…

user1008636
- 2,989
- 11
- 31
- 45
7
votes
2 answers
Finding cycles: DFS versus union-find?
DFS with coloring would take O(V+E) vs union find would take O(ElogV)
reference: http://www.geeksforgeeks.org/detect-cycle-undirected-graph/
So union find approach is slower.
If V = 100, E = 100, DFS = 200, Union find is 1,000.
Is there a reason to…

codereviewanskquestions
- 13,460
- 29
- 98
- 167
7
votes
1 answer
Printing out nodes in a disjoint-set data structure in linear time
I'm trying to do this exercise in Introduction to Algorithms by Cormen et al that has to do with the Disjoin Set data structure:
Suppose that we wish to add the operation PRINT-SET(x), which is given
a node x and prints all the members of x's…
user1596241
6
votes
1 answer
Is this Union Find really O(n) as they claim?
I am solving a problem on LeetCode:
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence. You must write an algorithm that runs in O(n) time. So for nums = [100,4,200,1,3,2], the output is…

Someone
- 611
- 4
- 13
6
votes
3 answers
Javascript Union Pairs Union Find
I working on union finding. I want to group pairs of numbers based on whether one of the indices shares a number with an index of another pair. So:
I have an array of pairs such as these:
pairs: [[1,3], [6,8], [3,8], [2,7]]
whats the best way to…

Stephen Agwu
- 1,013
- 2
- 15
- 29
6
votes
0 answers
check if input is valid binary-tree (using union-find)
Given multiple tuples in the form of (A,B) where A is the parent and B is the child in a binary tree, find if the input is valid or not. 4 error conditions were provided:
If a parent has more than 2 children,
If duplicate tuples entered,
If the…

Sushil
- 163
- 1
- 1
- 6
6
votes
2 answers
Why merge by rank, not count?
I was wondering, why in the union-find algorithm - you merge two trees by their height - attaching the smaller one to the taller one (in the simpler variant without path compression).
Would it be a worse approach if you merged them by their count of…

Rafael Korbas
- 2,213
- 3
- 19
- 30
5
votes
3 answers
Why is the time complexity of performing n union find (union by size) operations O(n log n)?
In Tree based Implementation of Union Find operation, each element is stored in a node, which contains a pointer to a set name. A node v whose set pointer points back to v is also a set name. Each set is a tree, rooted at a node with a…

romanian ego
- 85
- 1
- 1
- 7
5
votes
5 answers
Disjoint-set forests - why should the rank be increased by one when the find of two nodes are of same rank?
I am implementing the disjoint-set datastructure to do union find. I came across the following statement in Wikipedia:
... whenever two trees of the same rank r are united, the rank of the result is r+1.
Why should the rank of the joined tree be…

jeffreyveon
- 13,400
- 18
- 79
- 129
5
votes
3 answers
Set union algorithm using vector in C++
I'm only using std::vector in this problem, and I can guarantee no duplicates in each vector (but there isn't any order in each vector). How do I union the vectors I have?
Example:
If I have following vectors...
1
1
3 2
5
5 4
2
4
4 2
After the…

Arch1tect
- 4,128
- 10
- 48
- 69
4
votes
1 answer
Optimize Union Find (Disjoint Set Union) implementation
I have implemented a Disjoint Set Union in C++ for a Kattis problem. However, my solution is inefficient, as I get TLA (Time Limit Exceeded) for the first hidden test case. Can anyone spot an inefficiency with my code?
I have based my implementation…

Bart Louwers
- 873
- 9
- 28
4
votes
1 answer
Is the disjoint-set data structure implemented natively in Java?
I searched for a native implementation of the disjoint-set data structure in Java. But I didn't find one, only in external libraries.
Did I miss it or does it actually not exist?

Moritz Groß
- 1,352
- 12
- 30
4
votes
2 answers
What is meant when it's said that the union-find data structure will be "linear in the real world?"
I am undertaking the algorithms course on Coursera, there is a section where the author mentions the following
the running time of weighted quick union with path compression is
going be linear in the real world and actually could be improved to
…

Sudarshan
- 8,574
- 11
- 52
- 74
4
votes
3 answers
Dealing with Union-Find algorithms with a lot of objects
I have a problem with (not anymore with stackoverflow (hehe)) Find algorithm when trying to implement UnionFind structure algorithm with path-compression.
I have standard array of ints, array can get pretty big -> it works fine until 60.000.000…

SubjectX
- 836
- 2
- 9
- 33
3
votes
3 answers
quick find algorithm - union operation - is it same as union in set theory?
I am unable to correlate union operation in quick find algorithm with general meaning of A U B in set theory.
Book(Algorithms in C++ Robert Sedgewick) tells union operation is "scanning throgh the whole array for each input pair.(line 9 and 10 in…

P K
- 9,972
- 12
- 53
- 99