Presentation is loading. Please wait.

Presentation is loading. Please wait.

The UNION-FIND Abstract Data Type

Similar presentations


Presentation on theme: "The UNION-FIND Abstract Data Type"— Presentation transcript:

1 The UNION-FIND Abstract Data Type
Variation on the mathematical set Supports managing a dynamic equivalence relation Can be implemented with lists, trees, etc. CSE 373, Copyright S. Tanimoto, UNION-FIND ADT -

2 CSE 373, Copyright S. Tanimoto, 2002 UNION-FIND ADT -
A Set of Elements Let S be a set of elements. We can assume that there is a way to create the set and insert an element into it. { a, b, c, d, e} For example... s = newSet(); s.insert(a); s.insert(b); ... s.member(a)  true CSE 373, Copyright S. Tanimoto, UNION-FIND ADT -

3 CSE 373, Copyright S. Tanimoto, 2002 UNION-FIND ADT -
Equivalence Relation Let R be a binary relation on S. Then R is an equivalence relation on S iff R is reflexive on S. R is symmetric R is transitive Example. Suppose S is the set of all people living at midnight on New Year’s Eve 2000. (x, y) is in R iff x and y have the same first name. CSE 373, Copyright S. Tanimoto, UNION-FIND ADT -

4 Another Equivalence Relation
Let S = { a, b, c, d, e} Let R = { (a,a), (b,b), (c,c), (d,d), (e,e) (a,b), (b,a), (a,c), (c,a), (b,c), (c,b), (d,e), (e,d) } S can be partitioned into two subsets, such that within each subset, all the elements are related in both directions. P = { {a, b, c}, {d, e}} The two subsets are called equivalence classes. They are "induced" by R. CSE 373, Copyright S. Tanimoto, UNION-FIND ADT -

5 CSE 373, Copyright S. Tanimoto, 2002 UNION-FIND ADT -
The FIND Operation Let’s assume that we have an equivalence relation R, and a set of induced equivalence classes {E1, E2 , E3 , ... , Ek} Each Ei will have a representative element. FIND(x) for x in S, returns the representative element for the equivalence class containing x. CSE 373, Copyright S. Tanimoto, UNION-FIND ADT -

6 CSE 373, Copyright S. Tanimoto, 2002 UNION-FIND ADT -
FIND -- an Example Let S = { a, b, c, d, e} P = { {a, b, c}, {d, e}} E1 = {a, b, c}, E2 = {d, e} Let a and d be the representative elements of E1 and E2, respectively. FIND(c)  a FIND(d)  d FIND(e)  d CSE 373, Copyright S. Tanimoto, UNION-FIND ADT -

7 CSE 373, Copyright S. Tanimoto, 2002 UNION-FIND ADT -
The UNION Operation Let’s assume that we have an equivalence relation R, and a set of induced equivalence classes {E1, E2 , E3 , ... , Ek} UNION( Ei, Ej ) changes the partition so that Ei and Ej have been merged into one subset. For example, after UNION(E2, E3) we have {E1, E2 U E3 , ... , Ek } Note: Each Ei can be represented by its represent. element. So it’s OK to write UNION(a, d) CSE 373, Copyright S. Tanimoto, UNION-FIND ADT -

8 Application of UNION-FIND
In an electric circuit, finding the buses and other common signal points. The set S is given as a list of nodes. The relation R is (partially) given as a wire list. We want to find a minimal partition of S such that within each subset of nodes, they are all electrically connected by wire. CSE 373, Copyright S. Tanimoto, UNION-FIND ADT -

9 Electric Circuit (Cont)
Start with an initial partition into singleton sets. S = { a, b, c, d, e } Wire list: WIRE(a, b) WIRE(a, c) WIRE(d, e). CSE 373, Copyright S. Tanimoto, UNION-FIND ADT -

10 Electric Circuit Procedure
Start with an initial partition into singleton sets. P = { {a}, {b}, {c}, {d}, {e} } Process one wire at a time. WIRE(a, b) Perform FIND operations on each element, then UNION on the results: UNION(FIND(a),FIND(b)) Continue until all wires have been processed. CSE 373, Copyright S. Tanimoto, UNION-FIND ADT -

11 An Array Implementation
1 2 3 4 index a b c d e element 1 2 3 4 representative elt. index CSE 373, Copyright S. Tanimoto, UNION-FIND ADT -

12 CSE 373, Copyright S. Tanimoto, 2002 UNION-FIND ADT -
UNION(a,b) 1 2 3 4 index a b c d e element 2 3 4 representative elt. index CSE 373, Copyright S. Tanimoto, UNION-FIND ADT -

13 CSE 373, Copyright S. Tanimoto, 2002 UNION-FIND ADT -
UNION(a,c) 1 2 3 4 index a b c d e element 3 4 representative elt. index CSE 373, Copyright S. Tanimoto, UNION-FIND ADT -

14 CSE 373, Copyright S. Tanimoto, 2002 UNION-FIND ADT -
UNION(d,e) 1 2 3 4 index a b c d e element 3 3 representative elt. index CSE 373, Copyright S. Tanimoto, UNION-FIND ADT -

15 CSE 373, Copyright S. Tanimoto, 2002 UNION-FIND ADT -
Time Complexity In this method FIND takes O(1) and UNION takes O(n) Later, we’ll see that using Up-trees, we can make the time complexity almost O(n) for a full sequence of n FIND and n UNION operations. CSE 373, Copyright S. Tanimoto, UNION-FIND ADT -


Download ppt "The UNION-FIND Abstract Data Type"

Similar presentations


Ads by Google