Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology Singapore-MIT Alliance Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Day 20 6.046J/18.410J SMA5503 Handout 22 Problem Set 5
MIT students: This problem set is due in lecture on Day 20. Reading: Chapters 12 and 13 Both exercises and problems should be solved, but only the problems should be turned in. Exercises are intended to help you master the course material. Even though you should not turn in the exercise solutions, you are responsible for material covered by the exercises. Mark the top of each sheet with your name, the course number, the problem number, your recitation instructor and time, the date, and the names of any students with whom you collaborated. MIT students: Each problem should be done on a separate sheet (or sheets) of three-hole punched paper. You will often be called upon to "give an algorithm" to solve a certain problem. Your write-up should take the form of a short essay. A topic paragraph should summarize the problem you are solving and what your results are. The body of your essay should provide the following: 1. A description of the algorithm in English and, if helpful, pseudocode. 2. At least one worked example or diagram to show more precisely how your algorithm works. 3. A proof (or indication) of the correctness of the algorithm. 4. An analysis of the running time of the algorithm. Remember, your goal is to communicate. Graders will be instructed to take off points for convo luted and obtuse descriptions. Exercise 5-1. Do exercise 12.2-9 on page 260 of CLRS. Exercise 5-2. Do exercise 12.4-3 on page 268 of CLRS. 2
G: 4 Handout 22: Problem Set 5 B: 7 H: 5 A: 10 E: 23 K: 65 I: 73 Figure 1: A treap. Each node key and priority 4. is labeled with key priority Exercise 5-3. Do exercise 13.2-4 on page 279 of CLRS. Exercise 5-4. Do exercise 13.4-7 on page 295 of CLRS. Problem 5-1. Treaps If we insert a set of items into a binary search tree, the resulting tree may be horribly unbalanced, leading to long search times. On the other hand, we know that randomly built binary search trees tend to be balanced. Therefore, a strategy that, on average, builds a balanced tree for a fixed set of items is to randomly permute the items and then insert them in that order into the tree. But, what if we do not have all the items at once? If we receive the items one at a time, can we still randomly build a binary search tree out of them? We will examine a data structure that answers this question in the affirmative. A treap is a binary search tree with a modified way of ordering the nodes. Figure 1 shows an example. As usual, each node in the tree has a key value key . In addition, we assign priority , which is a random number chosen independently for each node. We assume that all priorities are distinct and also that all keys are distinct. The nodes of the treap are ordered so that the keys obey the binary-search-tree property and the priorities obey the min-heap order property: (This combination of properties is why the tree is called a "treap;" it has features of both a binary search tree and a heap.) It helps to think of treaps in the following way. Suppose that we insert nodes , with associated keys, into a treap. Then the resulting treap is the tree that would have been formed if the nodes had been inserted into a normal binary search tree in the order given by their (randomly chosen) priorities, i.e., priority priority means that was inserted before . If is a child of , then priority priority If is a right child of , then key If is a left child of , then key key . key . . . For example, the root has Handout 22: Problem Set 5 3 nodes is equivalent to a randomly built binary search tree on Solution: Assigning priorities to nodes as they are inserted into a treap is the same as inserting the nodes into a normal binary search tree in the (increasing) order defined by their priorities. So if we assign the priorities randomly, we will get a random order of priorities, which is the same as a random permutation of the inputs, so we can view this as inserting the items in random order i.e. a randomly built binary search tree. Solution: The time to search for an item that is in the treap is equal to the depth of that item. Now, we know that for an item in a randomly built binary search tree, the expected depth of is (the expectation is taken over permutations of the nodes, not the choice of ). Thus, the expected time to search for a value in the treap is . Let us see how to insert a new node into an existing treap. The first thing we do is assign to the new node a random priority. Then we call the insertion algorithm, which we call T REAP -I NSERT , whose operation is illustrated in Figure 2. (c) Explain how T REAP -I NSERT works. Explain the idea in English and give pseudocode. (Hint: Execute the usual binary-search-tree insertion procedure and then perform ro tations to restore the min-heap order property.) Solution: The hint gives the idea: do the usual binary search tree insert and then perform rota tions to restore the min-heap order property. T REAP -I NSERT inserts into the treap (by modifying ). It requires that has defined and values. We have used the subroutines T REE -I NSERT , R IGHT-ROTATE , and R IGHT-ROTATE as defined in CLRS. T REAP -I NSERT 1 T REE -I NSERT 2 while root and priority 3 do if left 4 then R IGHT-ROTATE 5 else L EFT-ROTATE priority (b) Conclude that the expected time to search for a value in the treap is (a) Explain why a treap on nodes. . 4 Handout 22: Problem Set 5 G: 4 C: 25 G: 4 B: 7 H: 5 B: 7 H: 5 A: 10 E: 23 K: 65 A: 10 E: 23 K: 65 I: 73 C: 25 I: 73 (a)
G: 4 (b)
G: 4 D: 9 B: 7 H: 5 B: 7 H: 5 A: 10 E: 23 K: 65 A: 10 E: 23 K: 65 C: 25 I: 73 D: 9 I: 73 D: 9 C: 25 (c)
F: 2 (d)
F: 2 B: 7 H: 5 ...
A: 10 B: 7 G: 4 A: 10 D: 9 D: 9 H: 5 C: 25 E: 23 I: 73 C: 25 E: 23 K: 65 I: 73 (e) (f) Figure 2: The operation of T REAP -I NSERT . (a) The original treap, prior to insertion. (b) The treap after inserting a node with key and priority 25. (c)(d) Intermediate stages when inserting a node with key and priority 9. (e) The treap after the insertion of parts (c) and (d) is done. (f) The treap after inserting a node with key and priority 2. Handout 22: Problem Set 5
Note that parent pointers simplify the code but are not necessary. Since we only need to know the parent of each node on the path from the root to (after the call to T REE -I NSERT ), we can keep track of these ourselves. 5 Solution: T REAP -I NSERT first inserts an item in the tree using the normal binary search tree insert and then performs a number of rotations to restore the min-heap property. The normal binary search tree insert always places the new item at a new leaf of tree. Therefore the expected time to insert an item into a treap is the expected height of a randomly built binary search tree, which is . Since the height of every binary tree is (a complete tree has the smallest height and its height is ), the expected height is . The maximum number of rotations occurs when the new item receives a priority less than all priorities in the tree. In this case it needs to be rotated from a leaf to the root. An upper bound on the expected number of rotations is therefore the expected height of a randomly built binary search tree, which is . Since each rotation take constant time, the expected time to rotate is . Thus the expected running time of T REAP -I NSERT is . (d) Show that the expected running time of T REAP -I NSERT is . 6 Problem 5-2. Join operation on red-black trees Handout 22: Problem Set 5 The join operation takes two dynamic sets and and an element such that for any and , we have key key key . It returns a set . In this problem, we investigate how to implement the join operation on red-black trees. (a) Given a red-black tree , we store its black-height as the field bh . Argue that this field can be maintained by RB-I NSERT and RB-D ELETE (as given in the textbook, on pages 280 and 288 respectively) without requiring extra storage in the nodes of the tree and without increasing the asymptotic running times. Show that while descending through , we can determine the black-height of each node we visit in time per node visited. Solution: Starting at the root, we can proceed to a leaf, couting the number of black nodes on the path. This does not require any extra storage in the nodes of the tree and will take time. Since RB-I NSERT and RB-D ELETE also run in time, the asymptotic running time is not increased. by 1 everytime we encounter a While descending through , we decrement bh black node. The black-height of a node, , is then bh minus the number of black nodes encountered (excluding node itself). This decrement can be done in time per node visited. We wish to implement the operation RB-J OIN , which may destroy and and returns a red-black tree . Let be the total number of nodes in and . (b) Assume that bh bh . Describe an -time algorithm that finds a black node in with the largest key from among those nodes whose black-height is bh . Solution: Since is a binary search tree, the largest element at any level is on the rightmost path. So, we decend down the rightmost path, calculating bh at each node (as described in the previous part), until we reach the black node whose black-height is bh , which is what we want. Thus the running time is at most the height of the tree, i.e. . (Calculating the black-height takes per node, as shown in the previous part). Solution: (c) Let be the subtree rooted at . Describe how time without destroying the binary-search-tree property. can replace in Handout 22: Problem Set 5
Insert into where was in . Form by letting be the left subtree of , and be the right subtree of . Given that this join operation is such that key key key where and , the binary search tree property is maintained and this operation takes time. 7 Consider the following red-black properties: every node is either red or black every leaf is black for each node, all paths from the node to descendant leaves contain the same number of black nodes (d) What color should we make Solution: so that the above red-black properties are maintained? We should make red. Since already has black-height = bh maintain the same black-height, bh = bh Consider the following red-black properties: the root is black if a node is red, then both its children are black time. Solution: Use RB-I NSERT-F IXUP on the new tree, to perform the recoloring and rotations nec essary to enforce these two properties. We know that RB-I NSERT-F IXUP runs in time, thus we conclude that the enforcement can be done in time. Solution: RB-J OIN is implemented by using all the previous parts: The black-height can be calculated and maintained in time. The required black node, , can be found in time. Then, the join is done in time, and finally, after assigning the right color, the red-black tree properties can be enforced in time. So the total runing time is (f) Argue that the running time of RB-J OIN is . (e) Describe how the above two properties can be enforced in , must be red to ...
View Full Document
- Fall '04
- Algorithms, CLRS, REAP -I NSERT