Mathematics Masters Examination OPTION 4 March 30, 2004 COMPUTER SCIENCE 2 5 PM NOTE: Any student whose answers require clarification may be required to submit to an oral examination. Each of the fourteen 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 04 CS 1/4
1. Computer Algorithms Given an array A of n elements, we wish to find the k largest in sorted order using the following 3 algorithms. Find their (worst-case) running-times in terms of n and k. (a) MergeSort A and list k largest. (b) Build a max-heap and call EXTRACT-MAX k times. 2. 3. 4. 5. 6. (c) SELECT the k largest number, partition around that number and sort the k largest numbers. Let G =(V,E) be a directed graph with V = n and E = m. Suppose that each vertex u V is labelled with a unique integer L(u) from the set {1, 2,...,n}. For each u V, let R(u) ={v V : u v} be the set of the vertices that can be reached by a directed path starting from u. Define min(u) to be the vertex in R(u) whole label is minimum. Give an O(n + m)-time algorithm that computes min(u) for all vertices u V. Programming Language Design C++ uses pointers to elements of type (say) E as a common alternative to indexes when accessing elements of arrays. Give at least one reason in favor of this scheme and one reason against it (observe that the scheme has been abandoned in Java). C++ (at least most versions) does not guard against a pointer going out of bounds of an array it is indexing. Why not? Could such a check be put into the language as a modification? What would be the cost of doing so? Combinatorics Consider a rectangular box with distinct dimensions. Color the 12 edges B, W, or R. Find the number of inequivalent colorings with 4B, 4W, and 4R edges. Colorings are equivalent with respect to rotations of the box. (Note that since the dimensions are distinct, the rotation must use an angle of 180 degrees and the axis must join the middle of opposite faces.) Find the number of permutations of 1, 2,...,6 in which 1 and 2 are in positions 3,4,5,6; 3 and 4 are in positions 1,2,5,6; and 5 and 6 are in positions 1,2,3,4. Graph Theory All graphs are simple and undirected. Let H be a subgraph of K n,n with more than (k 1)n edges. Prove that H has a matching of size k. Spring 04 CS 2/4
7. Theory of Computation Let Σ = {0, 1}. Construct a DFA for the following language: L = {w w does not contain the substring 00, and w is even}. 8. Explain and justify your construction. Notes: w is the length of w, and substring means a sequence of consecutive symbols. Let Σ = {0, 1, #}. Construct a CF grammar generating the following language: L = {w # x#w R w, x {0, 1} }. 9. 10. Explain and justify your construction. Symbolic Computation Explain the difference between Int and int in Maple. Give an example to illustrate why we need a command like Int. Numerical Analysis Consider the matrix A = with inverse A 1 = 1.5906E +00 1.1367E 01 8.8133E 01 3.8693E 01 2.7644E 02 2.1440E 01 1.0159E +00 7.2596E 02 5.6290E 01 3.8816E +09 4.5674E +09 7.8171E +09 1.5738E +08 1.8505E +08 3.1690E +08 6.9849E +09 8.2190E +09 1.4067E +10. 1. Estimate the condition number of A using. 1 (the maximum column sum) as norm. 2. For this given matrix A, we now consider the linear system Ax = b, for any general right-hand side vector b, and we forget about the inverse A 1. What is the relation between the relative error on the solution x to Ax = b and the relative error on A? Use cond(a) in your answer. If we want to know the first eight decimal places of x, how accurately must we know A? Justify your answer. Spring 04 CS 3/4
11. Suppose we want to approximate the first derivative f (x 0 ) of some function f(x) at the point x 0. 1. Show that the forward-difference approximation using step h>0 is only a first-order approximation in h for f (x 0 ). 2. Develop extrapolation formulas to compute a third-order approximation in h for f (x 0 ), cutting the step h in half each time. 12. 13. Computational Geometry A Star-shaped polygon (SSP) is a simple polygon all of whose interior is visible from at least one point in the polygon s interior. We shall call such a point a point of total visibility (PTV). It is clear that a star-shaped polygon need not be convex. The Kernel of a SSP is a region of its interior containing all of its PTV s. Observe that if the SSP happens to be convex, its kernel will span the entire interior of the SSP. Outline a proof sketch that the kernel of a SSP is a convex polygon inside the SSP. (Hint: Start with a 4-sided polygon with vertex angles 3 of which are convex, turning to the right in a clockwise traversal of the polygon (just like a convex polygon) and 1 of which is reflex (turning to the left). Draw the kernel of this polygon. Now consider how the kernel is constrained when there are several reflex angles. For instance, draw the kernel when there are two reflex angles.) Error-Correcting Codes and Cryptography A binary code C is described by its parity-check matrix H = 1 1 1 0 1 0 0 1 1 0 1 0 1 0 1 0 1 1 0 0 1. (a) How would you encode the messages m 1 = 0000 and m 2 = 1101? (b) How would you decode the received vectors v 1 = 0000111 and v 2 = 0001110? (c) Find the parameters n, k, and d for C. 14. In a public-key cryptosystem using RSA, you intercept the cryptogram C = 10 sent to a receiver whose public key is e = 5 and n = 35. Find the integer M that was sent. Spring 04 CS 4/4
Solutions to the Mathematics Masters Examination OPTION 4 March 30, 2004 COMPUTER SCIENCE 2 5 PM NOTE: Any student whose answers require clarification may be required to submit to an oral examination. Each of the fourteen 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 04 CS 1/9
1. Computer Algorithms Given an array A of n elements, we wish to find the k largest in sorted order using the following 3 algorithms. Find their (worst-case) running-times in terms of n and k. (a) MergeSort A and list k largest. (b) Build a max-heap and call EXTRACT-MAX k times. (c) SELECT the k largest number, partition around that number and sort the k largest numbers. Solution: (a) Sorting costs n log n and listing costs k. Total O(n log n). (b) Building a max-heap cost n and extracting k times costs k log n. Total O(n + k log n). (c) Selecting and partitioning costs n and sorting costs k log k. Total is O(n + k log k). 2. Let G =(V,E) be a directed graph with V = n and E = m. Suppose that each vertex u V is labelled with a unique integer L(u) from the set {1, 2,...,n}. For each u V, let R(u) ={v V : u v} be the set of the vertices that can be reached by a directed path starting from u. Define min(u) to be the vertex in R(u) whole label is minimum. Give an O(n + m)-time algorithm that computes min(u) for all vertices u V. Solution: Compute G T in the usual way, so that G T is G with its edges reversed. Then do a depth-first search on G T, but in the main loop of DFS, consider the vertices in order of increasing values of L(v). If vertex u is in the depth-first tree with root v, then min(u) =v. Clearly, this algorithm takes O(V + E) time. Spring 04 CS 2/9
3. Programming Language Design C++ uses pointers to elements of type (say) E as a common alternative to indexes when accessing elements of arrays. Give at least one reason in favor of this scheme and one reason against it (observe that the scheme has been abandoned in Java). C++ (at least most versions) does not guard against a pointer going out of bounds of an array it is indexing. Why not? Could such a check be put into the language as a modification? What would be the cost of doing so? Solution: The indexing can be done, at the target level, with no checking and no conversion of an integer index when the elements of the array occupy other than 1 word. It is thus extremely efficient. There is no conversion from an integer to an index, as would be the case for an integer used as an index on an array whose elements are types occupying more than one word. It is easy to implement in the compiler the generation of target code that will check each pointer applied to an array as an index, in order to ensure that the pointer is in bounds. This requires a run-time check each time the pointer is used as an index on an array, and obliges the use of a separate pointer for each array accessed, which is annoying when (say) two arrays of identical size are being accessed by the same index value. The result is a classic trade-off between efficiency and security. With today s fast processors, the right way is almost always to opt for security, unless every extra cycle needs to be squeezed from the computer. Spring 04 CS 3/9
4. 5. 6. Combinatorics Consider a rectangular box with distinct dimensions. Color the 12 edges B, W, or R. Find the number of inequivalent colorings with 4B, 4W, and 4R edges. Colorings are equivalent with respect to rotations of the box. (Note that since the dimensions are distinct, the rotation must use an angle of 180 degrees and the axis must join the middle of opposite faces.) Solution: P 6 = 1 4 (z12 1 +3z6 2 ), with z 1 = B + W + R and z 2 = B 2 + W 2 + R 2. The coefficient of B 4 W 4 R 4 in 1 4 ((B + W + R)12 +3(B 2 + W 2 + R 2 ) 6 ) is 1 4 (( 12 4, 4, 4 ) ( 6 +3 2, 2, 2 )) = 8730. Find the number of permutations of 1, 2,...,6 in which 1 and 2 are in positions 3,4,5,6; 3 and 4 are in positions 1,2,5,6; and 5 and 6 are in positions 1,2,3,4. Solution: (1+4z +2z 2 ) 3 =1+12z +54z 2 + 112z 3 + 108z 4 +48z 5 +8z 6. Then the answer is 6! 12 5! + 54 4! 112 3! + 108 2! 48 1! + 8 0! = 80. Graph Theory All graphs are simple and undirected. Let H be a subgraph of K n,n with more than (k 1)n edges. Prove that H has a matching of size k. Solution: Each vertex covers at most n edges, hence the minimum number of vertices needed to cover all the edges of H is at least k. By the König-Egerváry theorem, the maximum size of a matching in H is equal to the minimum size of a vertex cover. From this the result follows. Spring 04 CS 4/9
7. Theory of Computation Let Σ = {0, 1}. Construct a DFA for the following language: L = {w w does not contain the substring 00, and w is even}. Explain and justify your construction. Notes: w is the length of w, and substring means a sequence of consecutive symbols. Solution: 0 0 1 1 1 0,1 0,1 0 1 0 8. Let Σ = {0, 1, #}. Construct a CF grammar generating the following language: L = {w # x#w R w, x {0, 1} }. 9. Explain and justify your construction. Solution: S 0 S 0 1 S 1 S S # A # A A 0 A 1 ɛ Symbolic Computation Explain the difference between Int and int in Maple. Give an example to illustrate why we need a command like Int. Solution: 1. Int is the inert Maple command: it just displays the integral, but does not attempt to evaluate the integral, this is done by int. 2. Quite often, a symbolic antiderivative does not exist, and then we must resort to numerical approximation. The command evalf(int(f(x),x=a..b)) immediately computes such a numerical approximation, without first trying to compute a symbolic antiderivative. Spring 04 CS 5/9
10. Numerical Analysis Consider the matrix A = with inverse A 1 = 1.5906E +00 1.1367E 01 8.8133E 01 3.8693E 01 2.7644E 02 2.1440E 01 1.0159E +00 7.2596E 02 5.6290E 01 3.8816E +09 4.5674E +09 7.8171E +09 1.5738E +08 1.8505E +08 3.1690E +08 6.9849E +09 8.2190E +09 1.4067E +10. 1. Estimate the condition number of A using. 1 (the maximum column sum) as norm. 2. For this given matrix A, we now consider the linear system Ax = b, for any general right-hand side vector b, and we forget about the inverse A 1. What is the relation between the relative error on the solution x to Ax = b and the relative error on A? Use cond(a) in your answer. If we want to know the first eight decimal places of x, how accurately must we know A? Justify your answer. Solution: Condition number of A: 1. cond(a) = A 1 A 1 1. Since A 1 1 and A 1 1 10 10, cond(a) 10 10. 2. Let x be the system Āx = b, where Ā is the given A, known with limited precision. Then the relative error on x is estimated by x x x cond(a) A Ā. A For x x to be less than 10 8, A Ā must be less than 10 18, because the condition x A number amplifies errors on A with a factor of 10 10. Thus we must know A with at least 18 decimal places accurately to be sure of the first eight decimal places in x. Spring 04 CS 6/9
11. Suppose we want to approximate the first derivative f (x 0 ) of some function f(x) at the point x 0. 1. Show that the forward-difference approximation using step h>0 is only a first-order approximation in h for f (x 0 ). 2. Develop extrapolation formulas to compute a third-order approximation in h for f (x 0 ), cutting the step h in half each time. Solution: Approximation of f (x 0 ) with forward differences: 1. Using f(x 0 + h) =f(x 0 )+f (x 0 )h + f (x 0 ) 2! h 2 + O(h 3 ), we find the error for the forward difference difference approximation f(x 0,h)as f(x 0,h)= f(x 0 + h) f(x 0 ) h = f (x 0 )+ f (x 0 ) h + O(h 2 ), 2! which shows f(x 0,h) is a first-order approximation in h for f (x 0 ). 2. Cutting h in half each time, we derive f(x 0,h) = f (x 0 )+C 1 h + C 2 h 2 + O(h 3 ) f(x 0,h/2) = f (x 0 )+C 1 h/2+c 2 h 2 /4+O(h 3 ) f(x 0,h/4) = f (x 0 )+C 1 h/4+c 2 h 2 /16 + O(h 3 ) for some constants C 1 and C 2. Elimination of C 1 (or equivalently, the term in h) leads to a second-order approximation: f(x 0, h, h/2) = 2 f(x 0,h/2) f(x 0,h) 2 1 = f (x 0 ) C 2 h 2 /2+O(h 3 ) f(x 0,h/2,h/4) = 2 f(x 0,h/4) f(x 0,h/2) 2 1 = f (x 0 ) C 2 h 2 /8+O(h 3 ) We now see that 4 f(x 0,h/2,h/4) f(x 0,h,h/2) eliminates the term in h 2 and gives us the 4 1 desired third-order approximation for f (x 0 ). Spring 04 CS 7/9
12. Computational Geometry A Star-shaped polygon (SSP) is a simple polygon all of whose interior is visible from at least one point in the polygon s interior. We shall call such a point a point of total visibility (PTV). It is clear that a star-shaped polygon need not be convex. The Kernel of a SSP is a region of its interior containing all of its PTV s. Observe that if the SSP happens to be convex, its kernel will span the entire interior of the SSP. Outline a proof sketch that the kernel of a SSP is a convex polygon inside the SSP. (Hint: Start with a 4-sided polygon with vertex angles 3 of which are convex, turning to the right in a clockwise traversal of the polygon (just like a convex polygon) and 1 of which is reflex (turning to the left). Draw the kernel of this polygon. Now consider how the kernel is constrained when there are several reflex angles. For instance, draw the kernel when there are two reflex angles.) Solution: Suppose it is already known that the polygon P is star-shaped. Consider every reflex vertex v and the two vertices u and w on P. From v extend two half-lines continuing uv and vw. These form a wedge. From any point in the wedge, the inside of both uv and vw are visible; from outside the wedge, they are not both visible. Now reshape the polygon by connectiing w and u, forgetting v. It can be shown easily that P is still star-shaped, and that uw will still not pass through any edge of P. Continue this process until there are no more reflex vertices. The remaining polygon Q is convex, and forms the convex hull of P. The kernel must be the intersection of all the half-planes on either side of the wedges (two per wedge), which is convex, with Q, which is convex, and so finally the kernel is convex. Spring 04 CS 8/9
13. Error-Correcting Codes and Cryptography A binary code C is described by its parity-check matrix H = 1 1 1 0 1 0 0 1 1 0 1 0 1 0 1 0 1 1 0 0 1. (a) How would you encode the messages m 1 = 0000 and m 2 = 1101? (b) How would you decode the received vectors v 1 = 0000111 and v 2 = 0001110? (c) Find the parameters n, k, and d for C. Solution: (a) 00000000 and 1101010 (b) 1000111 and 0011110 (c) [7, 4, 3] 14. In a public-key cryptosystem using RSA, you intercept the cryptogram C = 10 sent to a receiver whose public key is e = 5 and n = 35. Find the integer M that was sent. Solution: p =5,q =7 Hence d is the inverse of 5 mod 24. This is 5 again. So M =10 5 (mod 35) = 5. Spring 04 CS 9/9