This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson December 20, 2005 6.046J/18.410J Final Exam Final Exam
�2 �2 �2 �2 �2 �2 �2 �2 �2 �2
Do not open this exam booklet until you are directed to do so. Read all the instructions on this page. When the exam begins, write your name on every page of this exam booklet. This exam contains 11 problems, some with multiple parts. You have 180 minutes to earn 180 points. This exam booklet contains 20 pages, including this one. Two extra sheets of scratch paper are attached. Please detach them before turning in your exam at the end of the examination period. This exam is closed book. You may use two handwritten A4 or crib sheets. No calculators or programmable devices are permitted. Write your solutions in the space provided. If you need more space, write on the back of the sheet containing the problem. Do not put part of the answer to one problem on the back of the sheet for another problem, since the pages may be separated for grading. Do not waste time and paper rederiving facts that we have studied. It is sufﬁcient to cite known results. Do not spend too much time on any one problem. Read them all through ﬁrst, and attack them in the order that allows you to make the most progress. Show your work, as partial credit will be given. You will be graded not only on the correct ness of your answer, but also on the clarity with which you express it. Be neat. Good luck! Name: 2 2 6.046J/18.410J Final Exam Name 2 Grade sheet: Do not write in the spaces below. Problem Title 1 2 3 4 5 6 7 8 9 Recurrences Algorithms and running times Substitution method True or False, and Justify Redblack trees Wiggly arrays Difference constraints Amortized increment Minimum spanning trees Parts Points Grade Grader 3 1 1 7 3 1 2 1 1 1 4 15 9 10 35 15 10 12 12 12 10 40 180 10 Multithreaded scheduling 11 Transposing a matrix Total 6.046J/18.410J Final Exam Name 3 Problem 1. Recurrences [15 points] (3 parts) Give a tight asymptotic upper bound ( notation) on the solution to each of the following recur rences. You need not justify your answers. (a) . where % (c) if if is a variable independent from . *+)$ %( &%# '$ (b) � 2 " %2 ! 6.046J/18.410J Final Exam Name 4 Problem 2. Algorithms and running times [9 points] Match each algorithm below with the tightest asymptotic upper bound for its worstcase running time by inserting one of the letters A, B, , I into the corresponding box. For sorting algorithms, is the number of input elements. For matrix algorithms, the input matrix has size . For graph algorithms, the number of vertices is , and the number of edges is . You need not justify your answers. Some running times may be used multiple times or not at all. Because points will be deducted for wrong answers, do not guess unless you are reasonably sure.
�2 � � Insertion sort A: 2 � Prim’s I: 2 � 2 Johnson’s H: � 2 FloydWarshall G: � Depthﬁrst search F: 2 � BellmanFord E: 2 � Strassen’s D: 2 2 � B UILD H EAP C: 2 � Heapsort B: "! 2 � 6.046J/18.410J Final Exam Name 5 Problem 3. Substitution method [10 points] Use the substitution method to prove a tight asymptotic lower bound ( notation) on the solution to the recurrence
�2 �2 2 ) 6.046J/18.410J Final Exam Name 6 Problem 4. True or False, and Justify [35 points] (7 parts) Circle T or F for each of the following statements to indicate whether the statement is true or false, respectively. If the statement is correct, brieﬂy state why. If the statement is wrong, explain why. The more content you provide in your justiﬁcation, the higher your grade, but be brief. Your justiﬁcation is worth more points than your trueorfalse designation. T F Let , , and be three sorted arrays of real numbers (all distinct). In the comparison model, constructing a balanced binary search tree of the set requires time.
T F Let be a complete binary tree with nodes. Finding a path from the root of using breadthﬁrst search takes time. vertex to a given �2 2 �2 2 �2 �2 2 � 2 �2 2 2 �2 2 �2 6.046J/18.410J Final Exam Name 7 T F Given an unsorted array of integers, building a maxheap out of the elements of can be performed asymptotically faster than building a redblack tree out of the elements of . T F Suppose we use a hash function to hash distinct keys into an array of length . Assuming simple uniform hashing, the expected number of colliding pairs of elements is . 2 � 2 � 2 �2 �2 2 ! 2 �2 �2 6.046J/18.410J Final Exam Name
inputs has depth
�2 8 T F If a dynamicprogramming problem satisﬁes the optimalsubstructure property, then a lo cally optimal solution is globally optimal. T F Every sorting network with . 6.046J/18.410J Final Exam Name 9 T F Let be a directed graph with negativeweight edges, but no negativeweight to all faster cycles. Then, one can compute all shortest paths from a source than BellmanFord using the technique of reweighting.
2 2 2 & 2 �2 6.046J/18.410J Final Exam Name 10 Problem 5. Redblack trees [15 points] (3 parts) (a) Assign the keys to the nodes of the binary search tree below so that they satisfy the binarysearchtree property. 2 nil nil nil (b) Explain why this binary search tree cannot be colored to form a legal redblack tree. & �2 & 2 & & & & & � nil nil nil nil nil nil 6.046J/18.410J Final Exam Name 11 (c) The binary search tree can be transformed into a redblack tree by performing a single rotation. Draw the redblack tree that results, labeling each node with “red” or “black.” Include the keys from part (a). 6.046J/18.410J Final Exam
Problem 6. Wiggly arrays [10 points] Name 12 An array is wiggly if . of real numbers, describe an efﬁcient algorithm that outputs Given an unsorted array of such that is a wiggly array. a permutation 2 � �2 �2 � �2 ( �2 � � �2 � � ( 2 � �2 �2 � � �2 2 � ( 2 2 2 2 �2 � 2 � 2 2 � 2 � 2 �2 �2 2 2 � 2 � �2
�2 �2 6.046J/18.410J Final Exam Name 13 Problem 7. Difference constraints [12 points] (2 parts) Consider the following linearprogramming system of difference constraints (note that one con straint is an equality): (a) Draw the constraint graph for these constraints. 2 2 (b) Solve for the unknowns , , , , and , or explain why no solution exists. 2 2 2 ( � 2 2 �2 ( 2 �2 2 2 �2 2 2 ( �2 2 2� ( �2 2 2 � ( 2 �2 �2 2 2 �2 2 �2 2 ( 2 �2 2 2 �2 2 ( �2 �2 2 2 ( � �2 �2 � �2 2 �2 2 �2 6.046J/18.410J Final Exam Name 14 Problem 8. Amortized increment [12 points] To add (modulo I NCREMENT 1 2 while 3 do 4 5 if 6 then ¡ $ ) to , we use the following procedure: Given a number , deﬁne the potential of to be the number of ’s in the binary representation of . For example, , because . Use a potentialfunction argument to prove that the amortized cost of an increment is , where the initial value in the counter is . ¨ © © ¤ © & © ¦ ©¢ ¨ £© ¢ ¦ ¢ % " ¨ ¦ £© ¢ ¢ © ¨ ¢ ¡ and ¢ ¡ ¨ ©§ © An array of bits (each array element is or ) stores a binary number § ¢ ¦ ¤ ¥£ ¨ ¦ ¡ ¢ 8 © £ ¢ © ¡ ¢¡ ¡ ¡ ©
% . "! "! ¢ ¢ ¡ # 6.046J/18.410J Final Exam Name 15 2 2 2 2 2 2 Let be a connected, undirected graph with edgeweight function in , where assume all edge weights are distinct. Consider a cycle and let be the edge in the cycle with the largest edge weight. Prove that not belong to the minimum spanning tree of .
�2 2 2 2 & 2 2 � 2 2
Problem 9. Minimum spanning trees [12 points] 2 2 & 2 & 2 � �2 � 2 & & 2 2 �2 2 2 & , and , does 2 2 2 2 2 & �2 6.046J/18.410J Final Exam Name 16 Problem 10. Multithreaded scheduling [10 points]
2 �2 A greedy scheduler runs a multithreaded computation in seconds on seconds on processors. Is it possible that the computation has work ? Justify your answer. length
� processors and in and criticalpath � �2 � 2 6.046J/18.410J Final Exam Name 17 Problem 11. Transposing a matrix [40 points] (4 parts) Consider the cacheoblivious twolevel memory model with a cache of elements and blocks of elements. Assume that both matrices and are stored in rowmajor order, that is, the linear order of in memory is , and similarly for . (a) Analyze the number MT 2� �2 �2 � 2 2 � �2 �2 � 2 � �2 2 � �2 2 2 � �2 � 2 � �2 2 � � � �2 � & 2 & & & & & & & & 2 & & & 2 & �2 & & 2 & 2 & 2 of memory transfers incurred by T RANS when % 2 % 2 2 � & 2 �2 & 2 � 2 2 2
to
2 �2 2 T RANS 1 for to 2 do for 3 do
2 2 & 2� �2 & � �2 & 2 & 2� �2 �2 �2 2 Let be an T : matrix, where 2 2 2 2 & 2 & �2 2 2 2 �2 2 �2
2 is an exact power of . The following code computes . 6.046J/18.410J Final Exam Name 18 Now, consider the following divideandconquer algorithm for computing the transpose: RT RANS 1 if 2 then 3 else Partition into four 4 Partition into four 5 RT RANS 6 RT RANS 7 RT RANS 8 RT RANS submatrices submatrices , , , , , and . , and . (b) Give and solve a recurrence for the number MT R T RANS when . 2 2 � Assume that the cost of partitioning is . of memory transfers incurred by 2 2 2 �2 2 2 2 �2 2 2 2 �2 2 �2 2 " 2 " & 2 " & 2 " & 2 " " & " 2 " 2 2 � &
% 2 & 2 & 2 & 2 2 & 2 �2 � �2 �2 2� �2 2 2 � 2 & 2 2 & 2 & �2
6.046J/18.410J Final Exam Name 19 The following multithreaded algorithm computes the transpose in parallel: PT RANS 1 if 2 then 3 else Partition into four 4 Partition into four 5 spawn P T RANS 6 spawn P T RANS 7 spawn P T RANS 8 spawn P T RANS 9 sync submatrices submatrices , , , , , and . , and . and criticalpath length (c) Give and solve recurrences describing the work of PT RANS . What is the asymptotic parallelism of the algorithm? " 2 2 2 2 �2 2 2 2 �2 2 2 2 �2 2 �2 2 2 2 " 2 " & 2 & 2 " & 2 & 2 " & 22 & 2 " 2 & "& " 2 " �2 �2 �2 �2 2 2 � & 2� �2 2 2 � 2 & 2 2 & 2 & �2
6.046J/18.410J Final Exam Name 20 Professor Kellogg inadvertantly places two additional sync statements into his code as follows: KT RANS 1 if 2 then 3 else Partition into four 4 Partition into four 5 spawn KT RANS 6 sync 7 spawn KT RANS 8 sync 9 spawn KT RANS 10 spawn KT RANS 11 sync submatrices submatrices , , , , , and . , and . (d) Give and solve recurrences describing the work and criticalpath length of KT RANS . What is the asymptotic parallelism of this algorithm? ¦ ©4£¡ ¡¢ ¢ ¤¤ ¤¤ ¢ ¢¤ ¢ ¢ ¢¢ ¤ ¢ ¤ ¢ ¤ ¢ ¢ ¢ ¢ ¦ ¡¢ ¢ ¡ ¦ 2#4¡ ¤ ¤ ¤ ¤ ¦ 2#4¡ 8 ¤ ¢ ¢¢ 8 ¢ ¤ 88 ¦ 2#4¡ ¢ ¤ ¢ ¤ ¢ 88 ¦ 2#4¡ ¢ ¢ ¢ ¢ ¦ 224¡ ¢ § 8 2248 ©¢ ¦ ¢ ¡ ¦ ¡ ¦ ¡ 224©¢ § #24©¢ ¤¢ ¤¢ ¤¢ ¤ ¢ ¢ © © 8 ¢ ¦ ¢ © © ¢ 8 ©¨¡ ¡ 8 ¢ 8 ¢
¦ SCRATCH PAPER — Please detach this page before handing in your exam. SCRATCH PAPER — Please detach this page before handing in your exam. ...
View Full
Document
 Fall '09
 s
 Algorithms

Click to edit the document details