CS 5 Algortms and Programs Exam Solutons Jonatan Turner 9/5/0 Be neat and concse, ut complete.. (5 ponts) An ncomplete nstance of te wgrap data structure s sown elow. Fll n te mssng felds for te adjacency lsts. Insert te edges nto te adjacency lsts so tat te edge numers appear n ncreasng order n eac lst. frstedge a c d e f 5 6 8 9 0 edges l lnext wt rnext r a 6 e d 8 6 5 a c 5 a 8 6 c c 0 9 e 9 8 0 f d 0 0 a e 0 6 0 f 0 0 c - -
. (5 ponts) Let A e a lst n numers. Descre an algortm tat creates a lst of te k smallest values n A and wc runs n O(n) tme so long as k < n/lg n. Explan wy your algortm runs n O(n) tme. Use makeeap to uld a -eap on te set of tems. Ts takes O(n) tme. Ten perform k deletemn operatons. Snce te tme per deletemn s O(log n), te runnng tme for k<n/ lg n deletemn operatons s O(n). - -
. (0 ponts) Te fgure elow sows an ncomplete representaton of an ntermedate state n te executon of Prm s algortm. In partcular, te values of te ceap mappng are not sown. Fll n te correct values for te ceap mappng (you may omt entres for vertces tat are n te tree). Ten sow ow te next step n te executon of te algortm modfes all te data structures. a 5 g 9 c e 8 f d 6 f d a c d e f g ceap a c de ef c 9 a 5 a 5 g 9 c e 8 f d 6 c d ceap a c d e a cf de f g a 5 - -
. (0 ponts) In te worst-case analyss of Prm s algortm, we saw tat te numer of calls to te cangekey procedure was O(m). In ts prolem, you are to sow tat t s also Ω(n ). Frst, sow tat you can assgn edge wegts to te complete grap on 5 vertces so tat Prm s algortm executes te cangekey operaton for every edge n te grap. Generalze ts example to sow tat for eac value of n, tere s a wegted grap wt Ω(n ) edges and tat cangekey s nvoked wen all of tese edges are examned. Te fgure elow sows an assgnment of edge wegts to a complete grap on 5 vertces tat causes Prm s algortm to execute cangekey once for every edge, assumng tat t starts from vertex u. Te generalzaton of ts grap as n vertces u,...,u n and te wegt of te edge from u to u j s equal to j for all and j. If Prm s algortm starts wt vertex u, ten for all, vertex u wll e removed from te eap n step and cangekey wll e nvoked for all edges (u,u j ) wt j>. Tat s, t wll e nvoked for every edge. Snce tere are Ω(n ) edges, cangekey s nvoked Ω(n ) tmes. u u u u u 5 - -
5. (0 ponts) A mn-max eap s an extenson of te eap data structure tat effcently supports access to an tem of maxmum key, as well as access to an tem of mnmum key. One way to mplement a mn-max eap s y extendng te data structure for d-eaps. One dvdes te eap nto levels, wt te root eng n level 0, ts cldren n level and so fort. For every node x on an even-numered level, key(x) key(y) for all descendants y of x. For every node x on an odd-numered level, key(x) key(y) for all descendants y of x. Te correctness of any data structure operaton depends on ts mantanng certan essental propertes of te data structure. Te data porton of te class declaraton for a C++ mplementaton of a mn-max eap data structure s sown elow. To make tngs smpler, you may assume a nary eap (eac node as at most two cldren). Wat propertes of te data must e mantaned y programs tat operate on t? Lst as many as you can tnk of. Hnt: a poston s on an even level f lg s even. Oterwse, s on an odd level. class mnmaxheap { nt N; // max numer of tems n eap nt n; // numer of tems n eap tem *; // {[],...,[n]} s set of tems nt *pos; // pos[] gves poston of n keytyp *kvec; // kvec[] s key of tem... }; n N for n, wt lg odd, kvec[[ / ]] kvec[[]] for n, wt lg even, kvec[[ / ]] kvec[[]] for n, wt lg even, kvec[[ / ]] kvec[[]] for n, wt lg odd, kvec[[ / ]] kvec[[]] for n, [] n, pos[] n and pos[[]] = for <j n, [] [j] and pos[] pos[j] - 5 -
6. (0 ponts) Te fgure elow sows a lazy leftst eap n wc some nodes (te saded ones) ave een marked deleted. Sow te eap after te executon of te operaton, purge(a). a 0, e, g,, f 9,, c 6, 0, j 6, d 5, After te purge we ave. e,,, j 6, d 5, 0, c 6, f 9, - 6 -
. (5 ponts) In te analyss of te round-ron algortm, te executon s dvded nto a seres of passes. Te frst pass ends wen all trees on te queue ntally ave een selected and comned wt oter trees. For a grap wt n vertces and m edges, wat s te largest possle numer of nodes n any of te leftst eaps at te concluson of te frst pass? Explan ow for any n and any m n, one can construct a grap on n vertces and m edges for wc te round-ron algortm as a eap of ts sze at te end of te frst pass. Gve a tgt ound on te tme requred y te round-ron algortm on ts grap, excludng te tme requred for ntalzaton. It s possle to end te frst pase wt a sngle eap tat as m+n nodes. Construct a star on n vertces wt edges of wegt. A star s a tree n wc tere s one central vertex wt an edge to every oter vertex. Now, add m (n ) edges of wegt larger tan etween any artrary pars of vertces. Note tat te orgnal star s a unque mnmum spannng tree for ts grap. If we execute te round ron algortm on ts grap, t wll comne all vertces nto a sngle component n te frst pass, resultng n a sngle eap tat contans all te edges, plus n dummy nodes from lazy melds. Te runnng tme for te round ron algortm on ts grap (excludng te ntalzaton) s O(nα(n,n)), snce none of te fndmns removes any nodes. Consequently, eac teraton takes constant tme, except for te fnd operatons and tese take at most α(n,n) tme eac. - -
8. (0 ponts) Consder an alternatve analyss of te partton data structure n wc te accountng varale det(u) s ncremented f te pat etween u and te root as lengt at least and g ( u) = g( p( u)), were te functon g(x) s defned y g ( x) = lg lg( + rank( x)). In all oter cases, fcredt s ncremented. Sow tat fcredt s ncremented O(log log log n) tmes durng eac top level fnd. Te values of g(x) cannot decrease as you go up te tree, so for every value of, tere s at most one node were g(x)= and g(p(x))>. So te numer of nodes were fcredt gets ncremented s at most plus te numer of dstnct values of g(x). Snce g(x) 0 and rank(x) s at most lg(n), ts s O(log log log n). Sow tat f g(x)= at te end of a sequence of operatons, ten + det ( x). Every tme det(x) s ncremented, te rank(p(x)) ncreases y at least. We only ncrement det(x) wle g(p(x))= and g(p(x))= only so long as rank ( p( x)) <. So, once det(x) s ncremented + tmes, g(p(x))> and det(x) s never ncremented agan. + Sow tat te numer of nodes for wc te fnal value of g(x)= s at most n. If g(x)=, ten rank ( x). Snce te numer of nodes wt rank equal k s at most numer of nodes wt g(x)= s at most n n = + + L = k k n k n / te Sow tat for eac value of, te sum of te det varales for all nodes wose fnal values of g(x)= s O(n). n Te sum s at most <n, so n all cases, t s O(n). + + + = n. For =0 and =, ts s n. For all larger values of t s - 8 -