A union/find data structure is a data structure used for maintaining a partition of a set.
Questions tagged [union-find]
184 questions
2
votes
2 answers
What data structure to use for the implementation of disjoint sets(union find)?
Please note that this is not a homework question. I am training on Kattis and I came by a question that requires the use of Union-Find paradigms. Given the nature of the problem, I decided to implement my own UnionFind data structure. I understand…

Ralph
- 2,959
- 9
- 26
- 49
2
votes
0 answers
Union-find data structure - How to use make_sets and find properly
Basically, I am trying to modify Kruskal's algorithm by adding one more condition besides checking whether vertices u and v are not in the same component.
I vaguely understand how union-find data structure works, so I wanted to check if I actually…

Ceria
- 97
- 2
- 9
2
votes
1 answer
Efficient Union Find with Existential Quantifier?
Is there a classical algorithm to solve the following problem.
Assume the union find algorithm without existential quantifiers
has the following input:
x1 = y1 /\ .. /\ xn = yn
It will then build some datastructure u, so that I can…
user502187
2
votes
3 answers
Implementing path compression in a disjoint set data structure?
Here is my Objective-C implementation of a disjoint set.
- Positive number point to parent.
- Negative number indicate root & children count. (So they each start disjointed at -1.)
- The index acts as the data I am grouping.
Seems to work ok... just…

Rakka Rage
- 15,941
- 8
- 33
- 45
2
votes
1 answer
Disjoint-sets for really big data
Are there any enhanced Disjoint-sets algorithm for really big data (such as more than 2^32 elements and more than 2^32 pair to union)?
Obviously the biggest problem is that I cannot make such a large array, so I'm wondering if there is a better…

huangcd
- 2,369
- 3
- 18
- 26
1
vote
2 answers
Union-Find algorithm
I was reading about the famous union-find problem, and the book was saying: "either the find or the union will take O(n) time, and the other one will take O(1)...."
But what about using bit strings to represent the set?
Then both union (using bit…

Betamoo
- 14,964
- 25
- 75
- 109
1
vote
0 answers
In Kruskal's Algorithm, why does the union procedure check whether r_x == r_y since the opposite was required to call union()?
In Algorithms [DPV] Section 5.1.3, the authors outlined Kruskal's Algorithm. In the algorithm itself, while iterating through all edges on the graph, they compare whether find(u) != find(v). If so, they eventually call the union() procedure on u and…

OOProg
- 189
- 1
- 5
- 16
1
vote
0 answers
Union-find: largest component size by common factor algorithm gives different results on every run
I was practicing data structure algorithm and made a Union - Find solution for the question.
The problem is, I think the code seems ok, but when I run it on Xcode playground, it shows different answers for the same input.
For example, I put an array…

Ryan Sfiery
- 35
- 4
1
vote
0 answers
What is the difference between these 2 implementations of quick find?
I was learning about quick find and made a very basic implementation in java but my union function was not working properly when I initially implemented it. I made some changes and it started working but I'm not sure whats causing the problem in the…

Abhi
- 13
- 2
1
vote
1 answer
UnionFind with weighted Quick Union (a.k.a Union by rank)
In the Union Find algorithm provided by LeetCode's tutorial, it seems the weight (rank) of the tree is only incremented when merging two equal weight subsets, however not the case when one is greater or less than the other. Can someone explain…

Tim Zhang
- 13
- 4
1
vote
1 answer
Leetcode 1584. How can I improve my Kruskal&Union Find algorithm to make it faster?
The question was on Leetcode 1584. Min Cost to Connect All Points. My answer to this question is:
class Solution {
public int minCostConnectPoints(int[][] points) {
List list = new ArrayList<>();
for(int i = 0; i <…

Tangcb
- 11
- 2
1
vote
0 answers
Modified Union Find Data Structure
connected: takes two arguments s and t and returns true if they are in the same set and false otherwise.
combine: takes two arguments s and t and creates a union of the two sets that contain s and t such that if s is in S and t is in T it removes S…

Holden21200
- 23
- 3
1
vote
1 answer
Implementation of textbook Union-Find algorithm doesn't work
I have two implementations of union-find: one that I've come up on my own (it works) and another that is based on the textbook explanation (surprisingly, does not work). While I am debugging the faulty implementation as I type this, perhaps someone…

Piotr
- 11
- 2
1
vote
1 answer
Enumerating equivalence classes
What I have: an equivalence relation on the set {1,...,n}, given as the full list of equivalent pairs (a,b) (this is already transitive; no need to compute a transitive closure). n is large (say, a few millions at most); the total number of…

Circonflexe
- 351
- 1
- 9
1
vote
1 answer
How to show top to bottom leaks in my path
I used the percolation class from the algs4.jar library, where you can simulate a whole labyrinth and see where the leaks are. I want to show the leaks that are from top to bottom, but now I get to see all of the leaks.
Does anyone know how I can…

Liza Darwesh
- 401
- 1
- 3
- 20