Disjoint Set / Union-Find Flashcards

(46 cards)

1
Q

What is a disjoint set?

A

A collection of sets where no element appears in more than one set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the union-find data structure?

A

A data structure that tracks a partition of elements into disjoint sets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the main operations of disjoint set?

A

Find and Union.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the find operation do?

A

Determines the representative (or root) of the set containing an element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the union operation do?

A

Merges two disjoint sets into a single set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is path compression?

A

An optimization that flattens the structure of the tree to speed up future find operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is union by rank?

A

An optimization that attaches the shorter tree under the root of the taller tree.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the time complexity of find and union with optimizations?

A

Nearly O(1), specifically O(α(n)), where α is the inverse Ackermann function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the inverse Ackermann function?

A

A very slowly growing function used in the analysis of disjoint set operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a representative element in a disjoint set?

A

The element used to identify the set, usually the root of the tree.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is a disjoint set represented?

A

As a forest of trees using parent pointers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the parent array in union-find?

A

An array where each index points to its parent, used to represent sets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the rank array used for?

A

To keep track of the height or depth of trees in union-find.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the initial value of each element in the parent array?

A

Each element is initially its own parent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the use of union-find in Kruskal’s algorithm?

A

To detect cycles while building a minimum spanning tree.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you check if two elements are in the same set?

A

Compare their roots using the find operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the space complexity of disjoint set with n elements?

A

O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How is path compression implemented?

A

By making each node on the path point directly to the root.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is union by size?

A

An optimization where the smaller set is attached to the larger one.

20
Q

Which is better: union by rank or union by size?

A

Both are efficient; union by rank is more commonly used.

21
Q

Can union-find be used in dynamic connectivity problems?

A

Yes, to maintain and query components efficiently.

22
Q

What is a real-world use case of disjoint sets?

A

Tracking connected components in a network.

23
Q

What is the role of union-find in maze generation?

A

To ensure that adding a passage does not create a cycle.

24
Q

What is the role of disjoint sets in clustering?

A

To group similar data points during algorithms like k-clustering.

25
What happens if you union elements already in the same set?
Nothing changes; they are already connected.
26
What is the amortized time complexity of union-find operations?
O(α(n)) per operation.
27
How do you initialize a disjoint set with n elements?
Create a parent array with each index set to itself.
28
What happens during repeated find calls without path compression?
The depth of the tree can grow, making find slower.
29
How do disjoint sets help in detecting cycles?
By checking if two nodes are already connected before uniting them.
30
What is connected component identification?
Using disjoint sets to find groups of connected elements.
31
What is a flat tree in union-find?
A tree where each node points directly to the root.
32
Can union-find be used in dynamic graph problems?
Yes, especially for offline queries and connectivity.
33
What is a drawback of naive union without rank?
It can lead to deep trees and slow find operations.
34
What is the purpose of the find operation returning the root?
To identify the set to which an element belongs.
35
What is a common mistake in implementing union-find?
Forgetting to apply path compression in find.
36
What are the advantages of union-find over DFS/BFS for connectivity?
Faster for multiple offline queries.
37
Can union-find handle merge and split operations?
It efficiently handles merges but not splits.
38
Is union-find suitable for dynamic addition/removal of elements?
Not for removals; it is static for most use cases.
39
How is union-find used in image processing?
To group pixels into regions or connected components.
40
What is a supernode in disjoint sets?
The root of a set that represents the entire group.
41
What is the total time for m operations on n elements?
O(m * α(n))
42
How do you implement disjoint sets in code?
Use arrays for parent and rank, with recursive or iterative find.
43
Can disjoint sets detect equivalence classes?
Yes, they group items with the same relation.
44
Why is the union-find algorithm efficient?
Because of path compression and union by rank.
45
What is the use of disjoint sets in social networks?
To find friend groups or communities.
46
What is the initialization cost of disjoint sets?
O(n), for setting each element as its own parent.