Questions tagged [disjoint-sets]

Anything related to disjoint sets, i.e. mathematical sets that have no element in common.

Anything related to disjoint sets, i.e. mathematical sets that have no element in common.

194 questions
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
1 answer

Disjoint Set implementation in C#

I didn't find any good implementation of Disjoint Set in C# using Union by rank implementation so I implemented my own. It works in O(log n) time complexity. Is there faster (or built-in implementation in C#) or I can work with my own…
gagro
  • 371
  • 7
  • 18
6
votes
2 answers

random sampling with Pandas data frame disjoint groups

I need to randomly separate a data frame into two disjoint sets by the attribute 'ids'. For example, consider the following data frame: df= Out[470]: 0 1 2 3 ids 0 17.0 18.0 16.0 15.0 13.0 1 18.0 16.0 …
Arnold Klein
  • 2,956
  • 10
  • 31
  • 60
6
votes
2 answers

How to (efficiently) generate disjoint sets while usings pairs of elements only once?

What I would like to do is split a group of (n) items into groups of equal size (groups of size m, and for simplicity assume that there are no leftovers, i.e. n is divisible by m). Doing this multiple times, I would like to ensure that no pair of…
6
votes
2 answers

Find the number of disjoint sets

For those not familiar with Disjoint-set data structure. https://en.wikipedia.org/wiki/Disjoint-set_data_structure I'm trying to find the no. of groups of friends from the given sets of friends and their relationships. Of course, there is no doubt…
nitte93
  • 1,820
  • 3
  • 26
  • 42
5
votes
2 answers

Determine whether a grammar is an LL using pairwise disjoint test

I have three grammars: A -> aB | b | CBB B -> aB | ba | aBb C -> aaA | b | caB I need to "determine whether [they] are LL grammars by performing the pairwise disjoint test, showing the first sets of each RHS of each nonterminal. This is what I have…
tommy1370
  • 133
  • 1
  • 2
  • 10
5
votes
2 answers

path compression is enough for disjoint-set forests , why do we need union by rank

MAKE-SET(x) x.p = x x.rank = 0 UNION(x, y) LINK(FIND-SET(x),FIND-SET(y)) LINK(x, y) if x.rank > y.rank y.p = x else x.p = y if x.rand == y.rank y.rank = y.rank +1 The FIND-SET procedure…
5
votes
0 answers

Union-Find/Disjoin-Set data structure for Directed Graph

I'm looking for an efficient Union-Find (aka Disjoint Set) data structure for my directed graph, which has cycles and forests. Given such a graph G , I want to to answer following queries: Can I reach node v from u? From which nodes, u is not…
Shafiul
  • 2,832
  • 9
  • 37
  • 55
5
votes
3 answers

What operations can be performed on disjoint sets?

I just studied the disjoint set data structure and I know that it is also called "union-find data structures", union and find are two main operations of this data structure. We can can perform union on disjoint sets, similarly we can perform find…
Zia ur Rahman
  • 1,411
  • 4
  • 26
  • 43
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
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

Why don't we update rank for disjoint set after path compression?

I have made a template for disjoint set with rank heuristic and path compression. template class disJSet { map parent; map rank; public: //Linear time complexity void makeSet(vector it) { …
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
1 answer

Merge communities and find a size of the largest one

I'm struggling with this problem on HackerRank. https://www.hackerrank.com/challenges/friend-circle-queries/problem I tried solving it using a custom linked list - NodeList. It has three fields - Node first, Node current, int size. 'add' is an…
ghoul932
  • 192
  • 1
  • 8
4
votes
5 answers

Implementing Disjoint Set System In Python

What I have so far is largely based off page 571 of "Introduction To Algorithms" by Cormen et al. I have a Node class in python that represents a set: class Node: def __init__(self, parent, rank = 0): self.parent = parent …
Jordan
  • 4,928
  • 4
  • 26
  • 39
1
2
3
12 13