# UF - CSE 4101/5101 Prof. Andy Mirzaian Disjoint Set Union...

This preview shows pages 1–10. Sign up to view the full content.

CSE 4101/5101 Disjoint Set Union Prof. Andy Mirzaian

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
References: [CLRS] chapter 21 Lecture Note 6 2
Disjoint Set Union § Items are drawn from the finite universe U = {1, 2, …, n} for some fixed n. Maintain a partition of (a subset of) U, as a collection of disjoint sets. Uniquely name each set by one of its items called its representative item. § These disjoint sets are maintained under the following operations: MakeSet(x): Given item xU currently not belonging to any set in the collection, create a new singleton set {x}. Name this set x. [This is usually done at start, once per item, to create the initial trivial partition.] Union(A,B): Change the current partition by replacing its sets A and B with AB. Name the new set A or B. [The operation may choose either one of the two reps as the new rep.] Find(x): Return the name of the set that currently contains item x. 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Example for x 1. .9 do MakeSet(x) 1 2 3 4 5 6 7 8 9 Union(1,2); Union(3,4); Union(5,8); Union(6,9) 1 2 3 4 5 6 7 8 9 Union(1,5); Union(7,4) 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 Find(9): Find(1): returns 5; Find(9): returns 9; Union(5,9) retruns 5. 4
Union-Find Problem PROBLEM: s = an on-line sequence of m = |s| MakeSet, Union, and Find operations (intermixed in arbitrary order), n of which are MakeSet, at most n–1 are Union, and the rest are Finds. Cost(s) = total computation time to execute sequence s. Goal: find an implementation that, for every m and n, minimizes the amortized cost per operation: max s Cost(s)/|s|. 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Applications 1. Maintaining partitions and equivalence classes. 2. Graph connectivity under edge insertion. 3. Minimum Spanning Trees (e.g., Kruskal’s algorithm). 4. Random maze construction. 5. More applications as exercises at the end of this Slide. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 5 6 3 8 9 12 14 15 1 2 10 11 13 16 4 7 6
Implementation1: Circular Lists Data Structure: 2 arrays Set[1. .n], next[1. .n] (each maps to 1. .n) Set[x] = name of the set that contains item x. A is a set Set[A] = A next[x] = next item on the list of the set that contains item x. n = 16, Partition: { { 1 , 2, 8, 9} , { 4 , 3, 10, 13, 14, 15, 16} , { 7 , 6, 5, 11, 12} } U 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 next 2 8 10 3 12 5 6 9 1 13 7 11 14 15 16 4 Set 1 1 4 4 7 7 7 1 1 4 7 7 4 4 4 4 1 8 9 7 6 2 13 14 15 16 12 5 4 3 10 11 4 7 1 7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Implementation1: MakeSet (x) O(1) time Set[x]  x next[x]  x end Find (x) O(1) time return Set[x] end Union1 (A,B) O(|B|) time (* Set[A]=A  Set[B]=B *) (* move all items from set B into set A *) Set[B]  A x  next[B] while x  B do Set[x]  A (*rename set B to A*) x  next[x] end-while x  next[B] next[B]  next[A] next[A]  x end A B x Sequence s: for x  1 . . n do MakeSet(x) for x  1 . . n-1 do Union1(x+1,x) Aggregate Time = Θ (n2) Amortized Time per operation = Θ (n) 8
Implementation2: Weighted Lists MakeSet (x) O(1) time Set[x]  x next[x]  x size[x]  1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/13/2012 for the course CSE 4101 taught by Professor Mirzaian during the Winter '12 term at York University.

### Page1 / 48

UF - CSE 4101/5101 Prof. Andy Mirzaian Disjoint Set Union...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online