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
0
votes
1 answer

Calculating the height of a Disjoint Set

Consider the program for i from 1 to 60: MakeSet(i) for i from 1 to 30: Union(i, 2*i) for i from 1 to 20: Union(i, 3*i) for i from 1 to 12: Union(i, 5*i) for i from 1 to 60: Find(i) I would like to determine the height of the disjoint…
newUser
  • 3
  • 2
0
votes
2 answers

In Disjoint Set Union (D.S.U.), why do we make the smaller sized subset as the child of the larger sized subset while performing the union operation?

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…
0
votes
1 answer

proof of time complexity of union find with path compression

The Wikipedia page: wikipedia page states that If m operations, either Union or Find, are applied to n elements, the total run time is O(m log*n). The detailed analysis arrived at this result is : My questions are: Shouldn't the sum be…
0
votes
1 answer

Disjoint set union implementation using c++

I am implementing Disjoint-Set Union by Rank and Path Compression in c++ according to the cp algorithm.But here I am getting an error reference to 'rank' is ambiguous . I have read many articles about this error but could not get any satisfactory…
user9857651
0
votes
1 answer

Interesting Python data structure problem involving disjoint sets, hashing, and graphs

Problem: You are planning an around-the-world trip with your two best friends for the summer. There is a total of n cities that the three of you want to visit. As you are traveling around the world, you are worried about time zones and airport…
0
votes
1 answer

How to calculate the maximum height of a tree in the resulting forest?

for i from 1 to 60: MakeSet(i) for i from 1 to 30: Union(i, 2*i) for i from 1 to 20: Union(i, 3*i) for i from 1 to 12: Union(i, 5*i) for i from 1 to 60: Find(i) Assume that the disjoint sets data structure is implemented as disjoint trees…
fds
  • 1
  • 3
0
votes
1 answer

getting error when return value but correct ans when pass by reference

There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the pi-th kid (in case of i=pi the kid will give his book to himself). It is guaranteed that all values of pi are distinct integers…
Aastha
  • 41
  • 8
0
votes
1 answer

Disjoint sets in Mongodb

Hi have a MongoDB collection matchedpairs with a data structure as follows: each document defines a pairwise connection with each other, i.e 1 is in union with 2 and 2 is in union with 10 etc. There are a large number of relationships defined. { …
0
votes
1 answer

How to select elements in a Quick union operation?

There are a set of elements S = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. Here there are 10 elements (N = 10). An array arr to manage the connectivity of the elements. Arr[ ] that is indexed by elements of sets, which are of size N (as N elements in set),…
0
votes
1 answer

Memory efficient version for the construction disjoint random lists

We have to positive integers b,n with b < n/2. We want to generate two random disjoint lists I1, I2 both with b elements from {0,1,...,n}. A simple way to do this is the following. def disjoint_sets(bound,n): import random I1=[];I2=[]; L…
111
  • 133
  • 6
0
votes
0 answers

Why second implementation is faster than the first one?

I recently tried to submit my solution for one of codeforces problem. I successfully solved the question and tried various test cases and the code was working correctly in my local machine. The time constraint was 2 seconds in the problem. I tried…
Roosh
  • 27
  • 1
  • 8
0
votes
1 answer

How to correctly implement wighted union and path compression in a UnionFind data structure

I am trying to implement a Union-Find/Disjoint-Set data structure in C, using weighted Union and path compression in Find. I have some questions as to how the weighted union should be implemented to reduce time complexity when compared to the non…
0
votes
2 answers

Why is my Union Find Disjoint Sets algo for this problem not passing all test cases?

Sample input: 1 3 2 1 2 2 3 First line = # of test cases First number of second line = number of people Second number of second line = number of friendships, F Following F lines = the friendships Output would be the size of the largest friend…
J. Doe
  • 85
  • 1
  • 6
0
votes
1 answer

In Java implementation of Kruskal's algorithm where exactly should we perform Path Compression?

While implementing Kruskal's algo in Java using Disjoint sets, should we call path-compression as separate funtion or it should be integral part of the find() funtion?
0
votes
2 answers

Problem while implementing Find() method for a disjoint set

Been trying to solve this for a while now. I've been given a Node in a Disjoint Set.The Node's implementation is as a class where either it has a different parent node or is it's own class Node { Node parent; int rank; //...constructor…