Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 analysis Applications

2 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}

3 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 iff R is reflexive on S. R is symmetric R 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 = b m and y = b n. So 36 and 216 have the same base, because 36 = 6 2 and 216 = 6 3. The relation of having the same base is an equivalence relation.

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

5 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 {E 1, E 2, E 3,..., E k } Each E i will have a representative element. FIND(x) for x in S, returns the representative element for the equivalence class containing x.

6 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}} E 1 = {a, b, c}, E 2 = {d, e} Let a and d be the representative elements of E 1 and E 2, respectively. FIND(c) -> a FIND(d) -> d FIND(e) -> d

7 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 {E 1, E 2, E 3,..., E k } UNION( E i, E j ) changes the partition so that E i and E j have been merged into one subset. For example, after UNION(E 2, E 3 ) we have {E 1, E 2 U E 3,..., E k } Note: Each E i can be represented by its represent. element. So it’s OK to write UNION(a, d)

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

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

10 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 g ed a { { a, d, e, g}, { c }, { f, g, h, i } }

11 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

12 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 g ed a

13 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)

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

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

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

17 CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 17 Enhanced FIND FIND(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.

18 CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 18 Path Compression FIND(h). ih b f c f c i bh

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

20 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) = 2 65536.

21 CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 21 Inverse Ackermann’s Function  (p,q) = min{ z  A ( z,  p/q  ) > log 2 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

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

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

24 CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 24 Example Graph + Minimum Spanning Tree e a b d c f g 6 8 2 3 27 24 12 8 17 9 35 5

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


Download ppt "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."

Similar presentations


Ads by Google