CS 146 Exam 1 Name Grade: The test will be open book, open notes, 75 minute time limit. Please write your answers on the exam sheet. Ten problems, 10 points per problem. 1. Analysis of non-recursive algorithms. Consider the problem of finding the longest palindrome occurring as a substring of a given input string s. For example, if we are given abaracecardog we should find racecar. (There might, of course, be two palindromes of equal longest length. In that case, we want the one starting nearest the beginning of the input string.) You will not be asked to write code to solve this problem, but rather, to analyze two possible approaches to the problem. Let N be the length of the input string. Plan A: First write a method Palindrome(String z) that checks if its input is or is not a palindrome. Then, to solve the main problem, examine, for each i and j with i+j < N, the substring of j characters beginning at index i. Call Palindrome to check if that substring is a palindrome and keep track of the longest one found so far. Plan B: examine, for each i < N, the longest palindrome centered either at the i-th input character or (in the case of even-length palindromes) between the i-th input character and the next input character, by looking for the first mismatch between characters at i-k and i+k (for odd-length palindromes) or between characters at i-k and i+k+1 (for evenlength palindromes). (a) Which plan leads to the fastest code, plan A or plan B? Plan B (b) Give the running time of plan A in terms of N using Θ-notation. Θ(N 3 ) Explanation: it takes three nested loops, one over i, one over j, and one in Palindrome. (c) Give the running time of plan B in terms of N using Θ-notation. Θ(N 2 ) Explanation: it takes two nested loops, one over i and one over k. (d) What is the largest value of N for which you would expect to be able to run your chosen program in one minute on a 1 gigahertz machine? 10 N 2 = 6 times N 10 so N is about 80000. The 10 on the left is for the loop body; if it s 20 instead we get more like 50000, so anyway, somewhere between 10000 and 100000.
2. (a) Indicate, for each pair of expressions (A,B) in the table below, whether A is O of B. Write Yes or No in each blank box of the table. A B is A = O(B)? n 27 2 n yes lg n n yes lg n + n n yes n n + 5 yes n + 5 n yes n lg n n 2 yes lg n n yes 2 (b). Use Θ-notation to express the worst-case running times of the following algorithms that have been discussed in class: (a) Insertion sort, on an array of length n. Θ( n 2 ) (b) A faster sorting algorithm, on an array of length n. Θ( n lg n ) (c) Binary search, on a sorted array of length n. Θ( lg n ) (d) Testing an n-digit integer to see if it is a prime, by dividing it in turn by each number up to its square root. Θ( 2 n/2 ) Note: the number itself is about 2 n, where n is the number of digits; so its square root is about 2 n/2. (e) Multiplying two n by n matrices (whose entries are of fixed size, say doubles). Θ( n 3 ) Explanation: this takes three nested loops, one over rows of the first matrix, one over columns of the second matrix, and one more to compute the entry at a given row and column. 3. Suppose given an initially empty stack, and suppose the following operations are executed. Assume the add method works as in your Calculator program. push(5); push(1); push(3); add(); multiply(); x = peek(); pop(); (a) What is the final value of x? 20 (b) What are the final contents of the stack? empty (no contents) Solution. The stack is successively 5 1,5 3,1,5 4,5 20 20 empty
4. Queues. Suppose a queue is implemented in a fixed array capable of holding 100 entries, using the method discussed in the textbook and in class. Suppose the queue is initially empty, and then objects are put into the queue at the rate of 10 per minute while meantime they are processed and removed from the queue at the rate of 5 per minute. After 120 elements have been added to the queue, which of the following is true? (a) You can t add 120 elements to an array holding 100 entries. (b) There will be 60 elements in the queue, 20 of them at the end of the array where the queue started, and 40 at the other end. (c) There will be 60 elements in the queue, all in one contiguous segment of the array. (d) There will be 60 elements in the queue, 40 of them at the end of the array where the queue started, and 20 at the other end. Answer: (b). The tail of the queue, which starts at 0, has moved 120 units, so it has wrapped around and is at index 20. The head of the queue, which also starts at 0, has moved 60 units, so at that time the queue occupies the last 40 elements of the array and the first 20. 5. Linked lists. The class StringList has these members: { String key; StringList next; } Write a static method boolean find(string needle, StringList haystack) that returns true if needle is in the list haystack, and false if not. The input null for haystack is legal, meaning haystack is an empty list. public static boolean find(string needle, StringList haystack){ for(stringlist marker = haystack; marker!= null; marker = marker.next) if(marker.key.equals(needle)) return true; return false; } 6. The operation Append on linked lists makes one list out of two. For example, Append applied to (cat, dog, giraffe) and (ant, salamander, deer) produces (cat, dog, giraffe, ant, salamander, deer). Write a member function Append(StringList x) that appends x to this. (After it runs, this will be the new, longer, list.) public StringList Append(StringList x){ for(stringlist marker = this; marker.next!= null; marker = marker.next) ; // empty loop body // now marker points to the last element of this.
marker.next = x; } 7. Binary Search Trees. The picture represents a binary search tree. The numbers shown are arbitrary node labels, not numbers representing the contents of the nodes. The contents are not shown. If node 1 is deleted, using binary search tree deletion, what will be the new root node? Answer 6 [It is not required to draw the tree after the deletion.] 8. Red-black trees. For each of the following two trees, either indicate how to color the nodes red and black (use R and B to label the nodes) to make the tree a red-black tree, or explain why that is not possible. In these pictures, as in the applet we used, the NIL leaf nodes are not shown. (a) (b) Answers: (a) The root has to be black, so 3 is black. Then 1 has to be black too, or else the black-height will be only 1, which is too small for the branch of length 4. So the black height has to be 2. Then there must be 2 red nodes on the path of length 3; since they can t be consecutive they must be nodes 7 and 11, and 9 must be black. Then 5
must be black to achieve black height 2 on the path 3-7-5. That coloring (7 and 11 red and everything else black) meets all the necessary conditions for a red-black tree. (b) We proved that in a red-black tree, the shortest path is at least half as long as the longest path; but here the shortest path has length 2 and the longest has length 5, so this can t be made into a red-black tree. Alternately you could argue as follows directly from the definition of red-black tree: The shortest branch has length 2, so the black-height can t be more than 2. On the branch of length 5, you would have to have 3 red nodes, each of which must have a black parent, which is impossible with only two black nodes available. 9. Red-black insertion. Node 7 is to be inserted into the following red-black tree. In these pictures, as in the applet we used, the NIL leaf nodes are not shown. After the red-black insertion algorithm completes, (a) What color will 7 be? red (b) What will be the path from the root of the tree to 7? 8,5,6,7 (c) What will be the black height of the tree after the insertion? 2 (d) What node will be the root of the tree? 8 (e) What will be the color of the root node? black
10. We studied the Google PageRank algorithm. Consider an example web containing three pages A, C, and G, in which pages G and C both link to page A and to each other, and A links only to C. Assume that the pages A, C, and G are indexed as pages 0,1, and 2 respectively. (a) What is the B-matrix for this example? nlinks[0] = 1 nlinks[1] = 2 nlinks[2] = 2 B = 0 0.5 0.5 1 0 0.5 0 0.5 0 (b) What is the rank order of these pages as produced by the Google algorithm (in descending order of rank)? Circle one of the six possible answers: (C,A,G) is correct. Starting with (1,1,1) = R0 for the ranks, we get R1 = BR0 = (1,1.5, 0.5), corresponding to rank order C,A,G, and then R2 = BR1 = (1, 1.25, 0.75), also corresponding to rank order C,A,G. Since the rank order didn t change, the algorithm terminates. Is this intuitively correct? Both A and C have two pages pointing at them, so they should both be highly ranked. Both are pointed at by G and by the other one; but the power of C s links is diluted since there are two links on page C, but only one on page A, so, A s link counts more and makes C higher-ranked than A. At first it seemed to me that A should be higher ranked; but intuition is only an approximation here; the algorithm tells the truth.