CS Avn Dt Struturs n Algorithms Exm Solution Jon Turnr //. ( points) Suppos you r givn grph G=(V,E) with g wights w() n minimum spnning tr T o G. Now, suppos nw g {u,v} is to G. Dsri (in wors) mtho or trmining i T is still minimum spnning tr or G. Exmin th pth in T rom u to v. I ny vrtx on this pth hs wight lrgr thn tht o th nw g, thn T is no longr n MST. W n moiy T to otin nw MST y rmoving th mx wight g on this pth n rpling it with th nw g. Explin how your mtho n implmnt to run in O(n) tim i oth G n T r provi s instns o th wgrph t strutur. Using th wgrph or T, w n o rursiv tr trvrsl in T, strting t vrtx u. On th trvrsl rhs v, w unwin th rursion, n s w o so, w look or th mx wight g long th u,v pth. Th runtim or tr trvrsl is O(n) n th rquir hngs to T n on in onstnt tim. Suppos tht inst o singl g, you r givn st o k nw gs to to G. For smll nough k it mks sns to pply your lgorithm rptly in orr to upt th MST, ut i k is too lrg, it s mor iint to r-omput th MST rom srth. How ig os k hv to (s untion o m n n) in orr or this to ttr hoi? Assum tht th MST is omput using Prim s lgorithm with -hp, whr =. Whn =, th running tim or Prim s lgorithm is O(m log n), so i kn grows str thn this, it mks sns to romput rom srth. So, i k>(m/n) log n, it mks sns to romput th MST. - -
. ( points) Suppos w pply Dijkstr s lgorithm to th grph shown low, strting rom vrtx. 7-8 8-6 g h - j i 7 6 List th irst 7 vrtis tht r snn.,,,,g,h,j In th igrm ll h vrtx with its tnttiv istn tr th irst 7 vrtis r snn n init th prnt pointr o h vrtx using n rrow pointing rom h vrtx to its prnt. List th nxt two vrtis to snn. i,j List th nxt six.,g,h,j,i,j Wht is th totl numr o snning stps tht Dijkstr s lgorithm will prorm on this grph? Th numr o stps is +(+())=9. I you xtn th grph y ing ourth imon to th lt, with g lngths o 8, 8, n -, how mny stps woul Dijkstr s lgorithm us or this grph? +(9)=6 - -
. ( points ) Consir Fioni hp ontining n unmrk no x or whih p(x), p (x),..., p 9 (x) r ll mrk, ut p (x) is not (whr p(x) is th prnt o x, p (x) th grnprnt, n so orth). Suppos tht ruky oprtion is prorm t x tht mks th ky o x smllr thn th ky o p(x). Do ny prviously unmrk nos om mrk s rsult o this oprtion? I so, whih ons? Assum tht p (x) is not tr root. p (x) oms mrk. I k is th numr o rits n to mintin th rit invrint, in th mortiz nlysis or th ruky oprtion, how mny rits r n tr th oprtion. Th numr o mrk non-root nos gos own y 8, whil th numr o trs gos up y. So, th numr o rits n to mintin th invrint gos own y 6 to k 6. Rll tht uring ltmin, th si stp involvs insrting tr root into n rry, t position trmin y its rnk. In som stps, th urrnt tr root ollis with tr root tht ws insrt rlir. In othr stps, thr is no ollision. Suppos tht ltmin is on on this hp n tht uring th ltmin, thr r stps uring whih no ollision ours. Giv n xprssion (in trms o φ=(+ / )/) tht rprsnts lowr oun on th numr o nos in th hp. Justiy your nswr. I thr ltmin stps with no ollision, thn t th n o th ltmin, thr must som no with rnk t lst qul to 9. From th nlysis w know tht th numr o nos in hp with rnk k is t lst φ k, so φ 7 in this s. W n tully gt ttr lowr oun y noting tht i thr wr stps with no ollision, thn tr th ltmin, th hp ontins t lst trs with rnks o t lst,,,,,...,9. Consquntly, th numr o nos in th hp is t lst +φ+φ + +φ 7. - -
. ( points) Th rsiul grph shown low is or som low on low grph G. s 7 t Wht is th pity o th g onnting n in G? Justiy your nswr. This g hs pity 7 in th rsiul grph us th sum o th rsiul pitis in opposit irtions is qul to th originl pity. Is th g in G irt rom to or rom to. Justiy your nswr. (Hint: onsir th totl inoming low t.) You my ssum tht thr is no mor thn on g joining ny two vrtis, ut you shoul not ssum nything out th irtion o th gs t s n t (tht is, G my hv gs ntring s or lving t). Consir vrtx. Sin this is not th sour or th sink, it must stisy low onsrvtion. For h or its thr inint gs, thr r two possil low vlus ntring. For th g, th inlow is ithr or. For th t g, th inlow is ithr or -. For th g, th inlow is ithr or -. Sin th sum o th inlows must, th inlow on th g must, th inlow on th t g must - n th inlow on th g must. This implis tht th g is irt rom to. Fin shortst ugmnting pth rltiv to. Drw th rsiul grph tht rsults rom ing s muh low s possil to this pth. Th pth is st n th nw rsiul grph pprs low. s t - -
. ( points) Lt R rsiul grph or min-ost low, lt p sour-sink pth in R with ost n lt q sour-sink pth in R with ost. Lt + th low w gt whn w nough low to p to sturt it n lt R + th rsulting low grph. Dos R ontin ngtiv yl? Explin your nswr. No, us is min-ost low n min-ost lows nnot ontin ngtiv yls. I R + hs no ngtiv yl, wht os tht tll us out p? Explin. It implis tht p is min-ost ugmnting pth, us th sn o ngtiv yl implis tht R + is min-ost low n sin w otin + y ugmnting long p, it ollows tht p is min-ost ugmnting pth. I < os R + ontin ngtiv yl. Explin. Ys. < implis tht p is not min-ost ugmnting pth, so + must not min-ost low, n th rsiul grph or non min-ost low must ontin ngtiv yl. I (u,v) is in R + ut not in R, wht os tht tlls us out (u,v)? Explin. It tlls us tht (v,u) is on p, sin nw g n only pprs in th rsiul grph whn low is in th opposit irtion. I (u,v) is in R ut not in R +, wht os tht tll us out ist (u) n ist (v) (whr ist (x) is th lngth o th shortst pth rom th sour s to x in R )? This mns tht (u,v) is on p, so ist (v)=ist (u)+ost(u,v) - -
6. ( points) Th grph t lt low hs ngtiv lngth gs. In th igrm t right, giv trnsorm g lngths tht prsrv th rltiv lngths o shortst pths whil liminting ngtiv g lngths. -9-9 - 7 6 - - 6 - - Th numrs nxt to th vrtis in th lt-hn grph rprsnt th shortst pth istns rom n sour vrtx with zro ost g to h originl vrtx. Th right hn grph shows th trnsorm g lngths omput using ths istn vlus. Lt G n ritrry grph with ngtiv lngth gs n lt lngth (u,v) th trnsorm g lngth or (u,v). Suppos p is pth rom x to y with lngth(p)= 8 n lngth (p)=. I q is nothr pth rom x to y with lngth(q)=7, wht is lngth (q)? lngth (q) = lngth(q) + (lngth (p) - lngth(p)) = 7 + ( - (-8)) = - - 7 In th min-ost ugmnting pth lgorithm (or th min-ost low prolm) using trnsorm g osts, nw shortst pth tr is omput uring h stp, n th shortst pth istns r us to moiy th g osts. Wht is th totl ost o th gs in th shortst pth tr, using th nwly moii osts? Explin. Th totl ost is. For h g (u,v), th nw ost (u,v) = ost(u,v) + ist(u) ist(v). I (u,v) is n g in th shortst pth tr, thn ist(v) = ist(u) + ost(u,v), n hn ost (u,v)=. Sin this is tru or ll gs in th shortst pth tr, th totl ost is. - 6 -
7. ( points) In th gr-onstrin sugrph prolm, w r givn grph G=(V,E) n gr oun (u) or vry vrtx u. Th ojtiv is to in sugrph o G in whih vry vrtx u hs t most (u) inint gs. In th grph t right, in gr-onstrin sugrph with 6 gs. Init th gs in th sugrph y mking thm hvir wight. In th wight vrsion o th prolm, h g hs wight w() n w r intrst in th gr-onstrin sugrph o mximum wight. Dsri (in wors) n lgorithm to solv this prolm whn th grph is iprtit. Us th grph shown t right to illustrt your solution. (You n not tully prou mx wight sugrph.) Not tht th shps o th vrtis in th ivision o th vrtis into susts. W solv th prolm y onvrting it to min-ost low prolm, s w i or th mx wight mthing prolm. Tht is, w sour vrtx with n g to vry vrtx in th lt sust o vrtis n sink with n g rom vry vrtx in th right sust. Th gs (s,u) hv pity (u) n ost. Th gs (u,t) hv pity (u) n ost. Eh originl g {u,v} is turn into irt g rom th lt vrtx u to v, with ost(u,v)= wt(u,v) n p(u,v)=. W thn low to th grph using th min-ost ugmnting pth mtho. Th lgorithm hlts or ining mximum low i th nxt min-ost ugmnting pth hs non-ngtiv ost. At tht point th gs in th ntrl prt o th grph tht hv positiv low in gr-oun sugrph o mximum wight. g 6 g pity, ost s,,,,,-,-6,-,-,-,-,-,-,- g,,, t - 7 -