final-f2009-sol

final-f2009-sol - Introduction to Algorithms Massachusetts...

Info iconThis preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 12
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 14
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 16
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Algorithms December 14, 2009 Massachusetts Institute of Technology 6.006 Fall 2009 Professors Srini Devadas and Constantinos (Costis) Daskalakis Final Exam Final Exam 0 Do not open this quiz booklet until directed to do so. Read all the instructions on this page. 0 When the quiz begins, write your name on every page of this quiz booklet. 0 You have 180 minutes to earn 150 points. Do not spend too much time on any one problem. Read them all through first, and attack them in the order that allows you to make the most progress. 0 This quiz booklet contains 13 pages, including this one. Two extra sheets of scratch paper are attached. Please detach them before turning in your quiz at the end of the exam period. 0 This quiz is closed book. You may use three 8%" x 11” or A4 crib sheets (both sides). No calculators or programmable devices are permitted. No cell phones or other communications devices are permitted. 0 Write your solutions in the space provided. If you need more space, write on the back of the sheet containing the problem. Pages may be separated for grading. 0 Do not waste time and paper rederiving facts that we have studied. It is sufficient to cite known results. 0 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. 0 Good luck! I --I-- --In-- --I-- --In-- -----I--- Name: 6.006 Final Exam Name___—_—_—_ 2 Problem 1. True or False [30 points] (10 parts) For each of the following questions, circle either True, False or Unknown. 1.After hashing n keys into a hash table of size m that uses chaining to handle collisions, we hash two new keys k1 and k2. Under the simple uniform hashing assumption, the probability that k1 and k2 are hashed into the same table location is exactly 1 / m with no dependence on the number of kg: S n. Answer = @ False 2.Under the uniform hashing assumption, if we use a hash table of size m with open addressing to hash 3 keys, the probability that the third inserted key needs exactly three probes before being inserted into the table is exactly 2 A m(m—1) ° Answer False 3.We use a hash table of size m with open addressing to hash 72 items. Under the uniform hashing assumption, the expected cost to insert another element into the table is at most 1 + oz, where a = n / m is the average load. Answer = True 4.There is a polynomial-time algorithm for the Knapsack problem if all items have size in {0, 1} regardless of the bits required to describe their values and the size of the knapsack. Answer = @ False Unknown 5.There exists a polynomial-time algorithm for finding longest simple paths in weighted di- rected acyclic raphs. Answer False Unknown 6.006 Final Exam Name 3 6.Every search roblem in NP can be solved in exponential time. Answer =alse Unknown 7.If there are negative edges in a graph but no negative cycles, Dijkstra’s algorithm still runs correctly. Answer = True 8.In a shortest path problem, if each arc length increases by k units, shortest path distances increase by a multiple of k. Answer = True 9.For any two functions f and 9, we always have either f = 0(9) or g = O( f ). Answer = True 5}; 3 a Milka 10.Dijkstra’s shortest path algorithm runs in 0(V3) time. Answer = True False 7N Owl/rho“ \3 [5.547% ml: 9‘, 6.006 Final Exam Name—___— 4 Problem 2. Data Structures [10 points] Design a data structure that keeps a sequence of real numbers S = (x1, ..., xn), and supports the following operations in O(log n) time, where n is the current length of the sequence: olnsert(y,_2'): inserts 3/ between $1- and xi.” .7 oSum(z‘, j): compute the sum 2 mt t=i Assume that the data structure is initially empty. 0 ML an em AVL {rib‘tnSpl'F-(J’ firufi‘z’ur-L . W 5mg Molt m bu MS [:aclL o'f' HM; #rwolu MWH (aura, 0 gram 0va “:1; MMS m Hm fiH Sultrngw {& Wham * anj point I‘a PW, tau 1'5 M l’tVL {new W on. Hm, HULL vac (lull number UL “w CWWmL uqunw S (Imfarl'ml mta- wL 0L: mt MUM W 'le mmlmmt geqmnw #4 W MMLWI (5 around) , ' To perhrm I"§£rl’(lj,i).‘ we compare, 4 ‘50 rod-"W‘le ,4 MINOLWJZMQHW Ma InurrL(q,L/ thfJu-u [Ida IJ’ 1:: mohwmrfltt” “41 04% I'LCU‘LC5/{'r0021_num&“;m+. right @199 M0, Cultlt MM.Zemp’y flu; e (la a. I £5+L~CLUU31VK9UJRWM15 [InuM$L@( Menu”, 1, 1 Hal notszth mm a!” 0&2 mob! what at “M it in H1, FLWVHWVl , Mu! incruue by 3 W Sum—[fail (gmlmg , CLLaQAHCe, Hv, {fa/MM? l€2%-€LV'M,HI9MJL’ZJ, 970 Jo 5m (.4); guy gt I M Eva Mal 'fujrrédr Mm'zw’wm'fifi 3%? - “L 0 Hum l0 évxé him down an i-i I'n MUJMV WIN; fight" “5. 'l‘,‘ Inger-l (IMMIan M“th hall! 54 S‘Z‘OP WM E-L=VHU\,12HJ [map 0:, Sum WWW (Mb/4,0311 lL'I O ouuL W LM,WM..+° :‘fi. :‘Hery M9 Wm H: WARM WW I I - l h...-n 6.006 Final Exam Name 5 Problem 3. Binary Trees [10 points] Consider the family of binary trees of n nodes with the following invariant for every node. If m and m are the number of nodes in the left and the right subtree respectively, then max{n1, n2} 3 (min{n1, 712»2 + 1. Is the height of these trees bounded by 0(log n)? Justify your answer with a rigorous argument or a counter-example. (BMW “M \PJUOMVL a W '_ M i 3*“ Q’jr [5E em?) buds (5 Q {x bimrfi W Oh \m V1046). .‘ TM {myriad holds 409‘ 04614 fl ‘. hock +M'vikuva (lit {0" Vwch/kdok «Memm \“I’v‘zlél. («gag Th. lube/rim} (10% ~F9r ‘CQCJ/l 0% 41*?» We tractqu Wet-41W W is MW om, owl {is me Is (A! 50 Maw awn M um at at watt subW. M Mob? 0i We We Is QM)! so We maid of W W5 (‘9 910+ (mutant ()3 000a”). \ \ 6.006 Final Exam Name _ 6 Problem 4. km minimum in min-heap [20 points] Present an 0(k log k) time algorithm to return the 19‘" minimum element in a min-heap H of size n, where 1 S k g n. Partial credit will be given to less efficient solutions provided your complexity analysis is accurate. CV‘e—a-it‘e (L new W‘N-\Aeq‘; 1 WVCM )5 ‘\\A‘\-t‘.<L\\>I ewe-ky- 1“§€V~( 't\&€ \rao‘r L {L t ——- . c \xd-o L wctk q; Q wKW‘Q :S TKQ \lqiuc OL fie W TOE:ij QGU‘ U9“: 1/0)/ .u 09 o «\U‘fifK wiec the WY 01)» H. glue? 7; kg ck Wu,“ keg“? T *0 fie 41 ¢$+ dew-t \m (kt QQ1W_B ’C k C“ '64: POW _ '\___\_H }@ Ki / :1“ p), i} - I QX‘W‘exc-tM'MQ ‘ t 3: K '1 qua” excwy offha- ekewevd all? [Jr :5 Q‘rt\x€u~ \W 1 ‘k\£_ Um ' OJ‘ EL" I fl ‘ / ejewwr M 'L \5 T\\€ mm gwqdiew zlekuq \ MC 9&1 0Q \«eqe 1 wavew Weecflg WK 6 0 (tag k) t: n.2, w; 0 CK\ Q\€ “sew-rs \QQQQV‘C wC QWOWH ink ‘6 0 CK by K3 00“), Se» OKPQVQT}~M_S “0‘51” @401 GLM°< QM cocwxe/ $6 QR“ 6.006 Final Exam Name—___—__— 7 Problem 5. 2-Satisfiability [20 points] The 2-SAT problem is defined as follows: There are n Boolean variables 1:1, . . . , gun, and a set of m clauses. Each clause has two variables which are either in true (55,) or complemented (ac—i) form. Here are two examples: 1.(x1 + 26—2)(a:_1 + x4)(fi + x4)(x2 + x4) is satisfiable. 2.(:r1 + x2)($_2 + 26—3) (203 + x1)($_1 + x4)($_4 + 13—1) is not satisfiable. For a 2-SAT formula to be satisfiable, every clause should be satisfiable. To satisfy a clause (x1 + 20—2) we can set :51 = TRUE and/or 202 = FALSE. If x1 = FALSE and x2 = TRUE, the clause is not satisfied. Example 1 is a satisfiable set of clauses because we can set x1 = TRUE, 202 = TRUE, 123 = FALSE and x4 = TRUE to satisfy all the clauses. There is no satisfying assignment for Example 2. Variable assignments that are required to satisfy some of the clauses conflict with assignments that are required to satisfy other clauses in this case. The 2-SAT problem is to find a satisfying assignment, if one exists. We note that 2-SAT (unlike 3-SAT) can be solved in polynomial time. Here we are only concerned with a sufi‘iciency check for unsatisfiability. That is, we want to devise a graph-based algorithm that checks if a given set of clauses is unsatisfiable. We want this check to be as efficient and as general as possible. To do this, we will represent the 2-SAT problem as a graph. The two graphs for the examples above are shown below in Figures 1 and 2. Each graph has 2n vertices: there are two vertices for every variable corresponding to the true and complemented forms, namely, x, = TRUE and x,- = FALSE for each x,. The edges of the graph represent the implied assignments for variables. For example, for every clause of the form (331 + 33—2), we have an edge from 1:1 = FALSE to $2 = FALSE and an edge from $2 = TRUE to $1 = TRUE (If we set 201 = FALSE, then we require 122 = FALSE for. this clause to be satisfied, similarly the other case). x, = TRUE 11 = FALSE 223 = TRUE 13 = FALSE (1121 + + + 1134)(1L‘2 + 1134) Figure 1: Example 1 satisfiable 6.006 Final Exam Name—__—___—_ 8 1:1 = TRUE II = FALSE 13 = TRUE 1;, = FALSE (131+ x2)(:v_2 + 13—3)(:v3 + x1)(:v_1 + x4)(:v_4 + :71) Figure 2: Example 2 unsatisfiable Devise an algorithm that operates on the graph derived from the 2-SAT problem. Your algorithm should return FALSE if it discovers that the problem is unsatisfiable and UNKNOWN otherwise. You will be graded on both the efficiency and generality of your algorithm. The generality is defined in the following way. Let A and B be two correct algorithms. We say that A is more general than B if the set of inputs for which A returns FALSE is a strict superset of the set of inputs for which [3 returns FALSE. GEM“); {111+ 10023 1”“) @ @ @ CW \och “with. 6.006 Final Exam Name“— 9 6.006 Final Exam Name 10 Problem 6. Second Shortest Paths [15 points] In an acyclic directed weighted graph G with a specified source vertex 3, let a(z') be the length of the second shortest path from s to the vertex 2'. You can assume that all path lengths between any two vertices are distinct. How can we determine a(z’) for all vertices in G in 0(V + E) time? You will receive partial credit for less efficient algorithms if your complexity analysis is accurate. 503-- Wow 90% mgm MM 9% W} Dem : (rd gawk Pm WWW zero} @HWW \MMSLAM WQWW WW‘ 0% I :Ma‘t gm: Mfléfls7§+ “MW 69/0 “36%;” ._/ / 6.006 Final Exam Name“— 11 Problem 7. Common Vertex [20 points] Given four vertices u, v, s and t in a directed weighted graph G = (V, E) with non-negative edge weights, present an algorithm to find out if there exists a vertex vc E V which is part of some shortest path from u to v and also a part of some shortest path from s to t. The algorithm should run in 0(E + V log V) time. Partial credit will be given to less efficient algorithms provided your complexity analysis is accurate. 6.006 Final Exam Name_——_—_ 12 Problem 8. The Ball Game (3 parts) [25 points] Professors Devadas and Daskalakis play the following game. N balls are inserted into a tube whose diameter matches the diameter of the balls, and therefore the balls cannot change positions inside the tube. Each ball has a distinct value on it. Let A1, A2, . . ., A N be the values of the balls in the order they are inserted. The tube is opaque, but it is open at both ends, so only the first ball at each end is visible (its value is visible as well). In each turn, a player removes one ball from the tube from either end and collects as many points as the value of the ball. Players take alternate turns, and each has a goal to maximize his score. Professor Daskalakis has a reasonable strategy. He always removes the ball with a higher value (out of the two visible balls). Professor Devadas, however, uses his infra-red vision that only people who have been at MIT long enough have been secretly taught. Therefore, he can see all balls and their values through the tube. (Note that this is not regarded cheating at MIT; it is attn'buted to professor skills.) Of course, he wants to use his power to maximize his score. Example: A = (3, 7, 1, 2) If Professor Devadas plays first, he will choose 2, then Professor Daskalakis will choose 3, then Professor Devadas will choose 7 and finally Professor Daskalakis will take 1. So the score would be Devadas: 9, Daskalakis: 4. If Professor Daskalakis plays first, he will choose 3, then professor Devadas will choose 7, then Professor Daskalakis will choose 2 and finally Professor Devadas will take 1. So the score would be Daskalakis: 5, Devadas: 8. Develop an efficient DP algorithm that computes the maximum score Professor Devadas can achieve when he plays first given, the array A1, A2, AN. Less efficient solutions will be given partial credit provided the complexity analysis is accurate. (a) [10 points] State the set of subproblems that you will use to solve this problem and the corresponding recurrence relation to compute the solution. 5%Wg: NEVA “Wk 5053/ ‘9 Pr‘fiflemtr Act/o.an cm Arrtg Afr-‘IAfi (1965K, 4$§$m) 1m: DtctthAt Man‘s». \ Dtitt+fi120~o~fohARq . 4 St AW Wu: A;+D[€*"1l"‘] I; A5>AH1 DEN-*— wm Az+btmm We Ad‘AH" (by «saw-4 Ag-HEHM‘U ‘f “RAW Haggis/x Ad+b[5,£-2] I74— fltéAa-M Soc/x‘bhiki D ink] 6.006 Final Exam Name (b) [8 points] Describe an iterative (non-recursive) algorithm to compute the maximum score. Analyze the running time of your algorithm. 0L4 COMPUPé,3CoR£(A)" ‘Flfr (:3 (ll—4H“ 7 (,3; {04: (mm D [:3 H13 :MX {ANAL-M)" ' d“) “‘7 {iv (:3hwlh: I (mo 0o») 9 {in}. zz1'i-»m— L—M: ' =£+L~4 CO“) 9 cufiovl‘e DUN? 63 M A‘ M 0‘ (WWW) W Mam] 135% hmvvvfi, 35 GOAL) (c) [7 points] Modify your algorithm above to print the set of moves made by both pro- fessors. Write down the modified algorithm below. 06:4: GAneM); ConP0r6~SCofi£00 //ASS(~H.Q_ 6% D “Li-7‘4} a! wwkd G? 6% (Na {:4 .izn we 6% : q. 52:5: fM‘ud: "Devam lbkg’fl A; "of" C i=c+4 dd; Isl-12:5: ” " trawl“ "OWN +0146” W(AHALM} 005 WWXMMH‘) "‘4‘ HDMMW My! “HAHN” "obngmwxmmqwg when, {LS-C: I ii— AP/lm ml DIz‘finAL-wccflwq, frtk’c "kv.&~§ Mn Al "0+" 6 "Ba—3% (mu Au. "wt." ,8 1::C‘f‘4' 036—1 ; . “4 Ai‘iAm WA Ai-‘BEC‘VL'H goon]; "WV "W 61%" A; "at" c I‘m/at ZENI‘fl‘KJP "OHM 64%," Am "at" 5+1 \ x SCRATCH PAPER "beva M" Ad " 0+"Q [303$ "DMlémnyqx +014." A: 1’35" 5 (3+4 ‘ {12‘} '\ . K {ls-e. lg Aim“ cowl Dfiui13.4$:601:éta‘7~1 I’ch "W3 we" A5 we N h- PWVT'WMWAX fiW'Aaé-a WC n" 4 . and—“L I . SCRATCH PAPER ...
View Full Document

This note was uploaded on 01/20/2012 for the course CS 6.006 taught by Professor Erikdemaine during the Fall '08 term at MIT.

Page1 / 16

final-f2009-sol - Introduction to Algorithms Massachusetts...

This preview shows document pages 1 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online