



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The handout for cs231 problem set 5, which includes problem statements, suggested and required problems, and reading materials for topics such as binary search trees, order statistics, and dynamic sets. Students are required to submit solutions to several problems, including evaluating a false claim about an o(n) binary search tree algorithm and implementing dynamic set operations.
Typology: Assignments
1 / 5
This page cannot be seen from the preview
Don't miss anything!
CS231 Algorithms Handout # 19 Prof. Lyn Turbak October 26, 2001 Wellesley College
Important: On Friday, Nov. 2, you will receive a take-home midterm exam that is due at 5pm on Saturday, Nov. 10. In order to give you enough time to work on your exam, I require Problem Set 5 to be turned in no later than 5pm on Saturday, Nov. 3, and recommend that you turn in on Friday, Nov. 2 if at all possible. (The exam is worth far more than the problem set, so it is important to focus on the exam.) Solutions to Problem Set 5 will be made available on Sunday, Nov. 4.
Reading: Handouts 16 (Order Statistics), 18 (Dynamic Sets), 20 (AVL Trees); CLRS Chapter 9, Intro to Part III (pp. 197-199), Appendix B.5, Chapters 10 and 12; CLR Chapter 10, Intro to Part III (pp. 197-199), Section 5.5, Chapters 11 and 13
Sugggested Problems: CLRS 9.3-1, 9.3-3, 9.3-6, 9.3-8, 9.3-9, 9-1, 9-3, 12.1-3, 12.1-5, 12.2-1, 12.2-4, 12.2-7, 12.3-4, 12.4-3, 12-2; CLR 10.3-1, 10.3-3, 10.3-6, 10.3-8, 10.3-9; 10-1, 10-3 13.1-3, 13.1-5, 13.2-1, 13.2-2, 13.2-4, 13-3.5, 13.4-2, 13-2.
Required Problems: You should write up and turn in the solutions to the problems listed below: The points awarded per problem are given in brackets.
Problem 1 [10]: Ima Fleik Dr. Ima Fleik of the Snake Oil Institute of Learning has just submitted a paper to an algorithms conference in which she claims that she has discovered an O(n) worst-case running time comparison- based algorithm for constructing a binary search tree with n elements. The program chair of the conference has asked you to read Dr. Fleik’s paper and evaluate this claim. Unfortunately, her algorithms are inscrutable and her analyses are impenetrable. But then it hits you that you can prove her claim is false from first principles by applying the decision model for comparison-based sorts to the following Haskell “tree sort” algorithm:
treeSort xs = inorderList (buildBST xs)
Here, buildBST encodes Dr. Fleik’s algorithm for building a binary search tree from n the elements of the list xs, and inorderList is a Θ(n) algorithm for listing the elements of a binary tree as they would be visited in an inorder traversal. Based on the above treeSort program, write a brief but convincing note to the program chair explaining why Dr. Fleik’s claim is false.
Problem 2 [15]: Second Smallest Do CLRS Exercise 9.1-1 (CLR Exercise 10.1-1). Hint: Build a “tournament tree” over the elements in which each node has a winner (smaller element) and a loser (larger element). Be careful that your algorithm accounts for all comparisons performed.
Problem 3 [20]: Closest Encounters This problem is based on CLRS Exercise 9.3-7 (CLR Exercise 10.3-7). It involves a function closest(S,k) that satisfies the problem specification. Assume that S is a list of length n; for simplicity, you may assume that n is odd (so that the median is unique). closest(S,k) should return a list of length k containing the k closest elements to the median; the order of the element in the resulting list does not matter. (If you prefer, you may use arrays rather than lists as the inputs/outputs of this algorithm.) Note that:
For example, consider the list S = [20, 5, 16, 1, 3, 23, 19, 4, 17, 29, 57, 2, 21]. The following table shows the set^1 of elements that should appear, in the list that results from calling closest(S,k) for various values of k:
closest(S,1) { 17 } closest(S,2) { 16 , 17 } closest(S,3) { 16 , 17 , 19 } closest(S,4) { 16 , 17 , 19 , 20 } closest(S,5) { 16 , 17 , 19 , 20 , 21 } closest(S,6) { 16 , 17 , 19 , 20 , 21 , 23 } closest(S,7) { 5 , 16 , 17 , 19 , 20 , 21 , 23 } closest(S,8) { 5 , 16 , 17 , 19 , 20 , 21 , 23 , 29 }
Note that closest(S,7) includes 5 rather than 29 even though both have distance 12 from the median 17.
a. [10] Write pseudocode for an implementation of closest(S,k) that runs in time Θ(n), where n is the size of the set S. You may use any of the algorithms that we have studied as ”black boxes” in solving the problem. Important: the argument k in this problem may be any number in the range [0..n]. Since k may be as large as n, it is incorrect to design an algorithm with running time Θ(kn), because in the case where k = n, such an algorithm would have quadratic running time rather than linear running time. In particular, it is incorrect for your algorithm to call a linear selection procedure k times, because such an algorithm would have time Θ(kn).
b. [5] Argue that your algorithm takes time linear in n.
(^1) The order of elements in the resulting list is irrelevant, so the order may be any permutation of the elements in a given set.
c. [9] Draw the sequence of AVL trees B 0 , B 1 ,... , B 17 that results from inserting the following letters one-by-one (from left to right) into an empty tree: T H E Q U I C K B R O W N Y A M S Use the algorithm presented in Handout 20 for rebalancing AVL trees that are unbalanced after insertion. For each node in the AVL tree, indicate (1) its value, (2) its height, and (3) its balance factor (i.e., <, =, or >).
d. [9] Draw the sequence of AVL trees B 17 , B 18 ,... , B 34 that results from deleting the following letters one-by-one (from left to right) from the tree B 17 that results from part c: T H E Q U I C K B R O W N Y A M S Use the algorithm presented in Handout 20 for rebalancing AVL trees that are unbalanced after insertion. For each node in the AVL tree, indicate (1) its value, (2) its height, and (3) its balance factor (i.e., <, =, or >).
Extra Credit 1 [20] Do CLRS Exercise 9.3-4 (CLR Exercise 10.3-4).
Extra Credit 2 [40]: D o CLRS Problem 9-2 (CLR Problem 10-2). In part c, give pseudocode (using Select as a black box) and justify why your algorithm is Θ(n). The notation in this problem is somewhat confusing. Here’s one way to view the problem. You are given an array A with n elements, each of which is a record with two real-numbered fields named x and w. What the problem calls xi is A[i].x and what it calls wi is A[i].w. Thus, each value x has an associated weight w that does not change throughout the problem. All the x values are distinct, but the weights are not guaranteed to be distinct. In the notation , the sigma summation notation
xi<xk wi^ means ”The sum of the weights of all^ xi^ such that^ xi^ < xk”. It does^ not^ mean ”The sum of the weights of all xi such that i < k”. The other sigma sum is symmetric. The weighted median of a collection is not necessarily unique. For example, if the each of the four elements 1, 2, 17, and 72 has weight 0.25, then both 2 and 17 are weighted medians.
Extra Credit 3 [20] Do CLRS Exercise 10.4-5 (CLR 10.4-5). Express your algorithm in pseu- docode. Your algorithm should print out the keys in an inorder traversal; i.e., the key of every node should be printed out after the key of every node in its left subtree but before every node in its right subtree. Your algorithm should run in Θ(1) space (i.e., no non-tail-recursion or explicit stacks), and you should not modify the tree. However, you may assume that you can compare two pointers for equality. Remember that each node holds a pointer to its parent in addition to its left and right subtrees. Hint: if you were exploring a cave system that branched out like a binary tree, what strategy would you use to explore the whole system?
Problem Set Header Page Please make this the first page of your hardcopy submission.
In the Time column, please estimate the time you spend on the parts of this problem set. Please try to be as accurate as possible; this information will help me design future problem sets. I will fill out the Score column when grading your problem set.