13-Find and Union

# 13-Find and Union - Find and Union Connectivity problems...

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

Find and Union Connectivity problems, equivalences Described by distinct pairs of integers between 1 and n, each pair i j specifies that i and j are connected (or equivalent) and should be in the same set. Given: Disjoint sets containing the integers 1 to n. Find - returns the set containing a given integer Union - replaces two disjoint sets by their union Represent sets in an array: S = {3, 7, 18, 5, 6, 10, 9} T = {2, 4, 20.19} Array 1 -1 2 4 S --> 18 T --> 4 3 18 / | \ | 4 -1 3 7 9 2 5 7 / \ | | 6 9 10 5 6 20 7 18 | 8 -1 19 9 18 10 7 11 -1 12 -1 13 -1 14 -1 15 -1 16 -1 17 -1 18 -1 19 20 20 2 What other trees are represented? Find i: Traverses from node i to the root of i’s tree and returns the root. Union i j: Make the root of j’s tree the parent of the root of i’s tree. Starting with n one node sets, doing m-1 unions creates a tree with at most m nodes for m< n. Proof: True for m=1 and 2. Suppose doing k-1 unions creates a tree with at most k nodes for k from 1 to m-1. Suppose we do an m th union on trees t1 and t2. By the induction

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

View Full Document
hypothesis, if k unions created t1 and m-1-k unions created t2 then they contain a total of at most (k+1) + (m-1-k+1) = m+1 nodes. So true for m unions too. Doing n-1 unions, produces one set with all the integers from 1 to n in it since add at least one new integer with each union. 1. Suppose do - union 1 2, union 2 3, union 3 4, . . . , union n-1 n. This produces the tree: n n-1 . 2 1 Now, doing find 1, find 2, . . . , find n will take total time O(n 2 ). So, a tree with n nodes formed by unions and finds can take O(n) time for the unions and O(n 2 ) time for the finds. w-union : Make the root of the union the tree with more nodes. To implement this we can replace the -1 at the root of a tree in the array with - #nodes in the tree and update it as we do the union. 2. A tree with n nodes created from a forest of one node trees by using w-unions has depth at most floor(lg n)+1. Proof: This is clearly true for n=1. Assume it is true for any such tree with i nodes for all i<n. Consider such a tree with n nodes. Suppose the last of the unions was with r and s. Let Nr= #nodes in r and Ns=#nodes in s. We can assume that Nr< Ns with no loss of generality so s will be the root of the union. Since Nr+Nr < Nr+Ns < n, Nr< n/2.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern