25
CSE 373, Copyright S. Ta nimoto, 2002 1 Up-Trees •Review of the UNION-FIND ADT •Straight implementation with Up-Trees •Path compression •Worst-case analysis •Applications

CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

Embed Size (px)

Citation preview

Page 1: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

1

Up-Trees

•Review of the UNION-FIND ADT

•Straight implementation with Up-Trees

•Path compression

•Worst-case analysis

•Applications

Page 2: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

2

(Review) A Set of Elements

Let S be a set of elements.e.g, S = { a, b, c, d, e}

Page 3: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

3

(Review) Equivalence Relation

Let R be a binary relation on S.

Then R is an equivalence relation on S iffR is reflexive on S.R is symmetricR is transitive

Example. Let’s say that two positive integers x and y not equal to 1 have the same base if there exist three integers b, m, and n such that x = bm and y = bn.

So 36 and 216 have the same base, because 36 = 62 and 216 = 63.The relation of having the same base is an equivalence relation.

Page 4: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

4

(Review) 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) }

R 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.

Page 5: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

5

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.

Page 6: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

6

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) -> aFIND(d) -> dFIND(e) -> d

Page 7: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

7

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)

Page 8: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

8

Time Complexity

In the straight array method, FIND takes O(1) and UNION takes O(n)

Now, we’ll see that by using Up-trees, we can make the time complexity practically O(n) for a full sequence of n FIND and n UNION operations.

Page 9: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

9

Up-Trees

An up-tree, is a tree in which each node except the root has a single pointer (or simulated pointer) that points to its parent.

A forest of up-trees is a set of up-trees.

Page 10: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

10

Up-Tree Example

A forest of up-trees can represent the subsets for the UNION-FIND ADT.

ih

b

fc

ged

a

{ { a, d, e, g}, { c }, { f, g, h, i } }

Page 11: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

11

FIND using Up-Trees

To determine the representative element for x, start at the node for x and follow the links to the root of x’s up-tree.

FIND(h) = f

ih

b

f

Page 12: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

12

UNION using Up-Trees

To merge to subsets, make the root of one up-tree point to the root of the other.

UNION(a, f)

ih

b

f

ged

a

Page 13: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

13

Time Complexity using Basic Up-Trees

FIND(x): O(height(representative(x)) = O(n)

UNION(x,y): O(1)

Page 14: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

14

Improving on the Basic Implementation

Typically, UNION and FIND operations are executed in sequences of about n/2 UNION operations and (n) FIND operations.

Therefore: Do a little extra work each time a UNION or FIND is performed, in order to reduce the work done in the future.

Page 15: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

15

Enhanced UNION

UNION(x,y): try to make the smaller tree point to the root of the larger.

Height method: Try to use the higher root as the new root. (Difficult to easily update heights when path compression is done with FIND; instead we can use ranks -- estimated heights.)

Weight method: Try to use the root of the larger up-tree as the new root. Keep a count of nodes at the root of each up-tree, using a 1-bit flag in each node to tell whether it’s the root.

Page 16: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

16

Enhanced UNION (Cont)

Even if no path compression is used with FIND, the enhanced UNION method cuts the time for a FIND operation to O(log n), assuming that a sequence of UNION and FIND operations is performed starting with singleton subsets (each element in its own subset), because the worst case up-trees formed will be “bushy” with branching factor at least 2 almost everywhere.

Page 17: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

17

Enhanced FINDFIND(x): While performing this operation, compress the path(s) of nodes encountered along the way so that they are closer to the root.

a. Full path compression. After finding the root r, make another pass from x to r making each node along the way point to r.

b. Path halving: Make every node along the path from x to r point to its grandparent.

c. Path splitting: Make every other node along the path from x to r point to its grandparent.

Page 18: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

18

Path Compression

FIND(h).

ih

b

f

c

f

c

i

bh

Page 19: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

19

Worst-Case Analysis of Running Time for UNION-FIND using Path-Compressed Up-Trees

Main points:

(a) A simple technique may require a complex analysis method.

(b) Ackermann’s function has an application.

(c) UNION-FIND with path-compressed up-trees is highly efficient.

Note: Amortized analysis is a novel way to account for the running time of an algorithm, but it is beyond the scope of the course.

Page 20: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

20

Ackermann’s Function

A(1, j) = 2 j

A(i, 1) = A(i-1, 2) for i 2

A(i, j) = A(i-1, A(i, j-1)) for i 2, j 2

Example values:

A(1,1) = 2; A(1,2) = 4; A(1, 3) = 8;

A(2,1) = 4; A(2, 2) = 16; A(2, 3) = 65536; A(2, 4) = 265536.

Page 21: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

21

Inverse Ackermann’s Function

(p,q) = min{ z A(z, p/q ) > log2 q },

p q 1, z 1

For all practical cases, (p,q) 4.

The “Single-variable inverse Ackermann’s function”:

(n) = log*n

(n) = min{ k | log (log ( ... log(n) ... ) ) 1}

k

Page 22: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

22

Theorem

Any sequence of m UNION/FIND operations, where m is (n) can be done in O(m log*n) time.

In theory, this is not linear, but in practice it is, since log*n 5 for values of n that come up in practice.

The UNION operations are done by rank.

FIND operations use path compression.

Page 23: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

23

Applying the UNION-FIND ADT to Constructing Minimum Spanning

Trees for Graphs

Given a connected undirected weighted graph G = (V, E), a minimum spanning tree is a subgraph T of G such that:

T is a tree (connected and contains no cycles),

T contains each v in V,

The total weight of all edges in T is as low as possible for all such spanning trees.

Page 24: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

24

Example Graph + Minimum Spanning Tree

e

a

bd

c

f

g

68

2

3

27

24

12

817

9

35

5

Page 25: CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case

CSE 373, Copyright S. Tanimoto, 2002 Up-trees -

25

Kruskal’s Algorithm

Initialize a UNION-FIND structure with each vertex v of V in a separate subset.

Consider each edge e of E in order of increasing weight.

For each edge (u, v), perform FIND(u) and FIND(v), and if they are in the same subset, go on to the next edge. Otherwise, add (u, v) to the spanning tree, and perform a UNION on the two subsets.

Keep a count of the number of edges added. When n-1 edges have been added (where n is the number of vertices), halt.