A union/find data structure is a data structure used for maintaining a partition of a set.
Questions tagged [union-find]
184 questions
3
votes
3 answers
Do I need to take explicit actions to facilitate sharing with persistent data structures?
I come from an imperative background and am trying to implement a simple disjoint sets (“union-find”) data structure to get some practice with creating and modifying (persistent) data structures in Haskell. The goal is to have a simple…

beta
- 2,380
- 21
- 38
3
votes
1 answer
Union-Find algorithm and determining whether an edge belongs to a cycle in a graph
I'm reading a book about algorithms ("Data Structures and Algorithms in C++") and have come across the following exercise:
Ex. 20. Modify cycleDetectionDFS() so that it could determine whether a particular edge is part of a cycle in an undirected…

Quentin
- 1,090
- 13
- 24
3
votes
2 answers
Union-Find (or Disjoint Set) data structure in Scala
I am looking for an existing implementation of a union-find or disjoint set data structure in Scala before I attempt to roll my own as the optimisations look somewhat complicated.
I mean this kind of thing - where the two operations union and find…

selig
- 4,834
- 1
- 20
- 37
3
votes
1 answer
Weighted quick-union with path compression- Implementation
I am implementing the quick union algorithm for a union/find structure. In the implementation given at the "Algorithms in Java" book site, the Princeton implementation fails to maintain the size invariant of tree while implementing path compression…

ksb
- 670
- 8
- 19
2
votes
1 answer
Merging two labels in connect components during the first pass
In connected components labeling, if I see that the pixel to the left and the pixel above the current pixel have the same color but different labels, can't I automatically reassign their labels to be the same (instead of doing with an equivalence…

skytreader
- 11,467
- 7
- 43
- 61
2
votes
1 answer
How to merge sets in better then O(len(set))?
I have a list of sets called groups. groups[i] is a set of labels (integers) that are part of the same group as i. For example,
# idx: 0 1 2 3 4 5 6 7
groups = [{0}, {1}, {2,5,6}, {3}, {4,7}, {2,5,6}, {2,5,6},…

joseville
- 685
- 3
- 16
2
votes
1 answer
Can I detect a cycle in an undirected graph with n edges in O(n log*(n)) ? ( log*(n) is log star function )
I have the following algorithm for detecting a cycle in an undirected graph with a set E of n edges :
for each unvisited edge (u, v) in E:
{
if(Find(u) = Find(v)) // u and v belong to the same set already
{
print "Cycle Detected";
…

MOUSA GANG
- 39
- 2
2
votes
1 answer
columns of numbers and union-find structure
The problem:
Put the numbers from 1 to M into M columns. At the beginning column i contains only number i. Then, perform N queries of two possible types:
A i j: remove all numbers from column i and put them (in the same order) at the end of column…

Benny
- 41
- 4
2
votes
1 answer
Fixing Karger's min cut algorithm with union-find data structure
I was trying to implement Karger's min cut algorithm in the same way it is explained here but I don't like the fact that at each step of the while loop we can pick an edge with it's two endpoints already in a supernode. More specifically, this…

roi_saumon
- 489
- 4
- 13
2
votes
1 answer
Why changing a line in my code will result in a stack overflow?
This is a [union-find problem]: https://leetcode.com/problems/similar-string-groups/
If I change the line parents[find(j)] = i; into parents[find(i)] = j;, the code will result in a stack overflow. Apparently the path is too deep for the recursive…

Wentao Wang
- 89
- 1
- 4
2
votes
1 answer
What is the complexity of path compression technique for disjoint set algorithm?
I was studying the disjoint algorithm with the union by rank and path compression.
It is clear to me if Union by rank is used then the find() operation complexity is O(log(n)).
But I wonder what is the complexity of the path compression technique…

Muidul Alam 2hin
- 141
- 2
- 9
2
votes
1 answer
How to store data elements in groups/clusters by union-find
I have implemented union-find algorithm inspired by this code with this API:
void unify(int p, int q)
int find(int p)
boolean connected(int p, int q)
int clusterSize(int p)
Then I detect clusters with a nested loop like this:
for i = (0) to (data…

Megidd
- 7,089
- 6
- 65
- 142
2
votes
1 answer
Union Find with Path Compression - Python algorithm
Working on the following problem (https://leetcode.com/problems/friend-circles/):
There are N students in a class. Some of them are friends, while some
are not. Their friendship is transitive in nature. For example, if A
is a direct friend of…

segue_segway
- 1,490
- 1
- 22
- 40
2
votes
0 answers
Java program using union-find + treemap too slow
I am solving this challenge: https://open.kattis.com/problems/virtualfriends
My solution seems to be working but kattis's test cases are running too slowly so I was wondering how I can improve code efficiency. I am using a custom made union-find…

mcgillian
- 97
- 7
2
votes
0 answers
Quick-Find implementation in Java stuck after while loop
While trying out quick-find in Java, I am having an issue. In the main function,
the code after while loop is not getting executed even though it doesn't seem like an infinite loop case.
Adding the full code and sample input and output.
Update :-…

Parzival
- 585
- 2
- 12