Solutions to the Mathematics Masters Examination OPTION 4 Spring 2007 COMPUTER SCIENCE 2 5 PM NOTE: Any student whose answers require clarification may be required to submit to an oral examination. Each of the twelve numbered questions is worth 20 points. All questions will be graded, but your score for the examination will be the sum of your scores on your eight best questions. Please observe the following: DO NOT answer two or more questions on the same sheet (not even on both sides of the same sheet). DO NOT write your name on any of your answer sheets. You will be given separate instructions on the use of these answer sheets. When you have completed a question, place it in the large envelope provided. Spring 07 CS 1/11
1. Computer Algorithms Let A = (81, 82, 63, 24, 65, 86, 17, 8, 99, 60, 91) be an array. Illustrate a linear time algorithm to build a max-heap. Show all your steps. Explain the heap sort algorithm. A = (99, 91, 86, 82, 81, 63, 17, 8, 24, 66, 65). Swap the first and last element of the array and use the build heap procedure with array length one less. Continue till array length is one. The original array now stores the numbers in nondecreasing order. Spring 07 CS 2/11
2. The nuts and bolts problem is defined as follows. You are given a collection of n bolts, A = {a 1, a 2,..., a n }, of different widths, and n corresponding nuts, B = {b 1, b 2,... b n }. You can test a given nut and bolt together, from which you learn whether the nut is too large, too small, or an exact match for the bolt. You are not allowed to compare two nuts or two bolts. You can only compare a nut and a bolt. You are to match each bolt to each nut. 1. Give an O(n 2 ) algorithm to solve the nuts and bolts problem. 2. Suppose that instead of matching all of the nuts and bolts, you wish to find the smallest bolt and its corresponding nut. Show that this can be done in only 2n 2 comparisons. Give the algorithm and prove that it makes at most 2n 2 comparisons. (i) Let the Nuts be denoted by the set A = {a 1, a 2,..., a n } and the bolts be denoted by the set B = {b 1, b 2,... b n }. Algorithm: for i = 1 to n do for j = 1 to n do if a i matches b j then pair (a i, b j ) return all the pairs (ii) Algorithm: i = 1, j = 1 while (i n) AND (j n) AND i + j 2n 1 do if a i < b j then j j + 1 if a i > b j then i i + 1 if a i = b j then if i j then (a,b) (a i, b j ), i i + 1 else (a,b) (a i, b j ), j j + 1 if i > n or j > n return (a, b) else return (a n, b n ) (iii) Let (a, b) denote there are a possible choices from set A and there are b possible choices among B. Initially a = b = n. After each comparison we can reject at most one of the possible candidates. So after a comparison (a, b) can go to (a, b), (a 1, b) or (a, b 1). We stop when we reach (1, 1). Thus, in the worst case we need at least 2n 2 comparisons Spring 07 CS 3/11
3. Combinatorics Find the general solution of the recurrence h n = 4h n 1 3h n 2 + 3 n, n 0. The characteristic equation of the homogeneous recurrence is x 2 4x+3 = 0 with roots 1, 3, so the general solution of the homogeneous recurrence is c 1 +c 2 3 n. For a particular solution of the nonhomogeneous recurrence try h n = p3 n, but this gives 3p = 4p p + 3, which is impossible. Instead, try h n = np 3 n. This satisfies the recurrence when p = 3/2. h n = c 1 + c 2 3 n + 1 2 n 3n+1. 4. The 9 squares of a 3 3 board are colored red, white and blue. Two colorings are equivalent when one can be obtained from the other by rotating the board (no flips). Find the number of non-equivalent colorings with 4 red, 3 white and 2 blue squares. The cycle index is 1 4 (z9 1 + 2z 1z 2 4 + z 1z 4 2 ). The answer is the coefficient of r4 w 3 b 2 in P G (r + b + w, r 2 + b 2 + w 2,...) = 1 ( (r + b + w) 9 + 2(r + b + w)(r 4 + b 4 + w 4 ) + (r + b + w)(r 2 + b 2 + w 2 ) 4). 4 4 times this coefficient equals ( ) 9 + 0 + coefficient of r 4 w 2 b 2 in (r 2 + b 2 + w 2 ) 4. 4, 3, 2 This last coefficient is ( ) ( 4 2,1,1. The answer is 1 ( 9 ) ( 4 4,3,2 + 4 2,1,1) ) = 318. Spring 07 CS 4/11
Graph Theory 5. 1. Construct a simple graph with 2n vertices and n 2 edges which has exponentially many perfect matchings. 2. Construct a simple graph with 2n vertices and n 2 edges which has a unique perfect matching. (1) Consider the bipartite graph G(V, E) with (i)v (G) = A B, where A = {a 1, a 2,..., a n } and B = {b 1, b 2,..., b n }, and (ii)) E(G) = {{a i, b j } : i, j {1, 2,..., n}}. Observe that E(G) = n 2. Let number of perfect matchings be equal to f(n). f(n) satisfies the recurrence relation f(n) = nf(n 1) with f(1) = 1. This implies f(n) = n!. (2) Consider the graph G(V, E) with (i)v (G) = A B, where A = {a 1, a 2,..., a n } and B = {b 1, b 2,..., b n, and (ii) E(G) = E 1 E 2 where E 1 = {{a i, b j } : i, j {1, 2,..., n} and i j} and E 2 = {{b i, b j } : i, j {1, 2,..., n}}. E = E 1 + E 2 = (1 + 2 + + n) + ( n 2) = n 2. There is only one perfect matching M = {{a i, b i } : i {1, 2,..., n}}. 6. Show that if G is simple and connected but not complete, then G has three vertices u, v and w such that uv, vw E(G) and uw / E(G). Since G is not complete it has two vertices x, y such that {x, y} is not an edge. Let P = x = v 1 e 1 v 2 e 2 e 3... e k v k+1 = y be the shortest path from x to y. Then v 1, v 2 and v 3 have the required property. Spring 07 CS 5/11
7. Error-Correcting Codes and Cryptography Consider the following technique for finding a 1000-bit prime integer for use with a cryptographic algorithm. do { With the help of a cryptographically secure pseudo-random number generator, generate a pseudo-random 1000-bit odd integer p; } while ( the Rabin-Miller test finds p to be composite ); At this point, p is a 1000-bit integer that is prime with near certainty; What is the probability that no more than 500 passes through the do-while loop are required? The probability that a large randomly-chosen integer p is prime is approximately 1/ ln(p). If p is known to be odd, the probability rises to 2/ ln(p). For p a 1000-bit integer, 2 999 p < 2 1000, so ln(p) 1000 ln(2) and 2/ ln(p) 2/(1000 ln(2)) 0.002885. So each pseudo-random odd integer has probability about 0.002885 of being prime. The probability of obtaining no prime in 500 trials is about (1 0.002885) 500, or 0.236. The probability of obtaining a prime within 500 trials is about 1 0.236, or about 0.764 (76.4%). Spring 07 CS 6/11
8. Alice wants to receive messages encrypted with the RSA. She chooses two large primes p A and q A, computes n A = p A q A and ϕ(n A ), chooses an encryption exponent e A, and then computes a decryption exponent d A. a) What formula does Alice use in computing ϕ(n A )? What condition must e A satisfy? How is d A related to e A? b) Of the six quantities p A, q A, n A, ϕ(n A ), e A, and d A, which become part of Alice s public key? Which become part of her private key? Which are no longer needed and may she discard (securely)? c) Eve suggested to Alice that she choose p A and q A as follows: Using a cryptographically secure pseudo-random number generator, generate a pseudo-random even integer r of appropriate size. Then choose p A as the largest prime less than r, and q A as the smallest prime greater than r. Would this be a good way for Alice to choose p A and q A? Why or why not? d) What computation does Bob perform to encrypt a short message M that he wishes to send to Alice? e) What computation does Alice perform to decrypt a short encrypted message C that she receives? a) ϕ(n A ) = (p A 1)(q A 1). gcd(ϕ(n A ), e A ) = 1. d A e 1 A (mod ϕ(n A)). b) Alice s public key is (n A, e A ). Her private key is d A. She may discard p A, q A, and ϕ(n A ) once she has computed her public and private keys. c) This would be a terrible way for Alice to choose p A and q A. p A and q A would be very close together, quite likely differing by no more than two thousand. (The average difference between 1000-bit primes is about ln(2 1000 ) 700.) Now n A (p + q)/2 since p and q are close together, so p is quite likely greater than n A 1000. Eve could factor n A by computing the largest odd integer less than n A, call it u, and then checking whether u, u 2, u 4, u 6,... divide n A. Most likely she would find a divisor of n A after checking fewer than 500 possibilities. Once Eve has factored n A, she can compute ϕ(n A ) and d A. Then she can decrypt any encrypted message sent to Alice. d) Bob encrypts M as M e A (mod n A ). e) Alice decrypts C as C d A (mod n A ). Spring 07 CS 7/11
9. Theory of Computation Let Σ = {0, 1}, and L be the language recognized by the NFA 1 1 q1 0 0 q0 q3 0,1 0 1 q2 0 Construct a DFA for the complement of L. 1 0 q 0 0 q 1 q 2 1 0 q 1 q 3 1 0 q 2 q 3 1 q 3 0,1 Spring 07 CS 8/11
10. Let a) Is L regular? b) Is L context-free? L = {a m b n m = n or m = 2n}. a) No. Assume it is regular. For any p, consider the string w = a 2p b p. By the pumping lemma, w = x y z, where x y p, y > 0 and x y i z L for every i 0. Let i = 2. Then x y 2 z has more than 2p a s and p b s, so it is not in L, a contradiction. b) Yes. A CF grammar generating L is S S 1 S 2 S 1 as 1 b ɛ S 2 aas 2 b ɛ Spring 07 CS 9/11
Numerical Analysis 11. 1. Consider the matrix A = 1.225E 1 2.998E 1 6.069E 1 8.066E 1 4.385E 1 7.297E 1 5.426E 1 1.410E 1 2.166E 1 Compute the LU decomposition of A with partial pivoting. Calculate with four decimal places, using rounding: write the answer of every step rounded to four decimal places in scientific format, and use the rounded number in the calculations of the next step. In addition to L and U, also give the permutation matrix P. 2. Explain how you would use the LU decomposition of A to compute A 1. Use pseudo code to describe the algorithm you would use. 1. The LU decomposition consists of L, U, and P : 1.000E+0 0.000E+0 0.000E+0 L = 1.518E 1 1.000E+0 0.000E+0 6.727E 1 6.601E 1 1.000E+0 8.066E 1 4.385E 1 7.297E 1 0 1 0 U = 0.000E+0 2.333E 1 4.961E 1 P = 1 0 0 0.000E+0 0.000E+0 5.313E 2 0 0 1. 2. Let I n be the n-by-n identity matrix. Then A 1 satisfies the matrix equation AX = I n. Denote by x k the kth column of X: X = [x 1 x 2 x n ], and let e k be the kth column of I n : I n = [e 1 e 2 e n ]. Then the columns x k of A 1 are computed in n stages, solving Ax k = e k, k = 1, 2,..., n, using each time the LU decomposition of A to compute x k, as in the pseudo code below: for k from 1 to n do x k := U 1 L 1 e k ; end for; A 1 := [x 1 x 2 x n ]. Spring 07 CS 10/11
12. Discuss the advantages and computational costs of Runge-Kutta methods, compared to using predictor-corrector multistep methods of the same accuracy. A k-stage Runge-Kutta method requires k new function evaluations, whereas a predictorcorrect multistep method takes only one new function evaluation. While a Runga-Kutta method is thus more expensive, the method allows to use variable step sizes which is an advantage over the predictor-corrector multistep methods. Spring 07 CS 11/11