Docsity

Data Structures for Disjoint Sets in EE 360C, UT Austin, Study notes of Health sciences

Toggle sidebar
bg1
EE 360C — Algorithms Spring 2007
Data Structures for Disjoint Sets
Adnan Aziz UT Austin
Based on CLRS, Ch 21.
In several applications, need to group ndistinct elements into a collection of disjoint sets.
•Example — electrically equivalent nets on a PCB
Operations:
•return which set an element belongs to
•form the union of sets corresponding to two elements
Formally: disjoint set data structure maintains a collection S={S1, S2, . . . , Sk}of disjoint dy-
namic sets
•identify each set by a “representative” member
–usually don’t care which one is representative; just want it to remain the same if no
operations are performed on the set
Let x, y be elements; want to support the following operations:
1. MAKE-SET(x)— return a new set whose only member is x
•very important: xbetter not be present in any other current set — our assumption is
that we are working with disjoint sets
2. FIND-SET(x)— returns a pointer to representative of set containing x
3. UNION(x, y)— unites sets containing xand y
Want efficient algorithms
•will measure runtimes of operations in terms of
–n— the number of make-set operations, and
1
pf3

Partial preview of the text

Download Data Structures for Disjoint Sets in EE 360C, UT Austin and more Study notes Health sciences in PDF only on Docsity!

EE 360C — Algorithms Spring 2007

Data Structures for Disjoint Sets

Adnan Aziz UT Austin

Based on CLRS, Ch 21. In several applications, need to group n distinct elements into a collection of disjoint sets.

  • Example — electrically equivalent nets on a PCB

Operations:

  • return which set an element belongs to
  • form the union of sets corresponding to two elements Formally: disjoint set data structure maintains a collection S = {S 1 , S 2 ,... , Sk} of disjoint dy- namic sets
  • identify each set by a “representative” member - usually don’t care which one is representative; just want it to remain the same if no operations are performed on the set Let x, y be elements; want to support the following operations:
  1. MAKE-SET(x) — return a new set whose only member is x
  • very important: x better not be present in any other current set — our assumption is that we are working with disjoint sets
  1. FIND-SET(x) — returns a pointer to representative of set containing x
  2. UNION(x, y) — unites sets containing x and y Want efficient algorithms
  • will measure runtimes of operations in terms of - n — the number of make-set operations, and 1

Lecture Data Structures for Disjoint Sets: 2

- m — the total number of make-set, union, and find-set operations Trivial implementation — linked lists

  • each set corresponds to a linked list - each node in list has element, next pointer, and pointer to head of list - first object in each list is the set’s representative
  • perform UNION(x, y) by appending x’s list onto the end of y’s list - need to update the representative field for each node in x’s list Example — Figure 21. Easy to see make-set and find-set take O(1) time. Fact — can have a situation where m operations take Θ(m^2 ) time
  • MAKE-SET(x 1 ), MAKE-SET(x 2 ),.. ., MAKE-SET(xn), UNION(x 1 , x 2 ), UNION(x 2 , x 3 ),.. ., UNION(xn− 1 , xn) - keep having to update lots of representatives! So the average cost of an operation is Θ(m). The problem above is that we are appending a long list onto a short list
  • idea — keep track of length of list (easy, fast) - always append smaller list onto longer list (break ties arbitrarily) ∗ called the “weighted-union” heuristic At first glance, this isn’t very good — a single union can take Ω(m) time
  • when both sets have m members However if you start from scratch (i.e., no sets), and perform m make-set, union, and find opera- tions, can prove that runtime is O(m + n · log n). An even better approach: represent sets using rooted trees.
  • each node contains one element, each tree represents one set as in Figure 21.4(a)
  • root of tree contains representative, and is own parent