You are on page 1of 10
S-16 Answers to Ode Numbered Eseries ‘we cant its elements na sequence a i, 9... THE cements ofthe set LJ A canbe ised by ist al terms 1 withi-+ j= 2, dhenall terms. withi + j= 3 thenall terms a) with +) =4,andso.on. 43. There aca inte ‘umber of bit sng of length m, namely, 2". The st ofall bit string isthe union ofthe es of bt sigs of ength m for 1.2,.... Because the union ofa countable number of countable scsi eoumabl (see Exercise l), hee are a sountable numberof it strings. 48. For any init alphabet ‘here area finite number of strings of lenath whenever ns a postive integer It follows bythe result of Exercise 41 that there are ony a countable numberof sings fom any given finite alphabet, Because the st of al computer programs in particular language isa subset ofthe et of ll strings of 2 inte alphabet, which sa countable set by the result fom veri 36 iti itself a countable set. Exercise 4S shows thi there are only a countable numberof computer ‘programs. Consequently there arora countable number ‘computable fuetons. Beans, as Fxerise 46 shows, there fare an uncountable number of funtion, not ll fanetons te ‘compatable, ‘Supplementary Exer ak wane 94-8 OA OAoe BYes_§.A-(A~B) = A~(ANB) = AANA) A @UB)= (An AULANB)= BULAN) = ANB TLet A = (1,8 =0,C =I). Then(A ~ B)—C A-(B—C)={I}. 9.No. For example, let and D = fa). Then (A B)—(C ~D) but (AC) ~(B —D) = fab} ~ (0) =. 1-8) 0} = 1A B| = [Al <|AUB| =U) [AB] Os dae = yn oe all 1050. The next four frms are $346, $353, 37471, and S409. CHAPTER3 Section 3.1 mac 1, f=2, max i= 8, Ha, 606, = % mae itt 3. procedure sum fori == 200% ‘um = sum +4, [sum has desired value) procedure duplicates (2.03. nondecreasing ore) ‘k= [this coum the duplicates) a iter) integers in fsjtt end (6. the desired list] “procedure last even locaton, m0 fori valor ita; seven then k end (isthe desired leation or if there reno evens) a integer) 9, procedure palindrome checHaa...0: tng) fort == 10 [9/21 hen answer = false ue sting i palindeome} 1 procedure inerchangetx, real numbers) y ‘The minimum numberof assignments needed i thre. 1B.Linearsearchs 43 7, location 1S, procedure nsr(, 0.4 ---+0y:itegers) tei isin order: a) $"< ay) tye HT sur while >a, #1 {has bem inserted into coret position) 17. procedure fist largest. integers) Tocation = 1 fori begin imax < g then begin location = end end 19, procedure meun-medion-maxmin(a, b,c: integers) mean = (a+ B+0)/3 (the i diferent orderings ofa bc wih respect to > willbe handed separately ita > bthen begin h> ethen mein = Bima = a:min = ¢ end (Theres ofthe algorithm is simiar) 21, procedure frst (a0... inte) Ha > a then interchange a and o> ia; > a then interchange. and as ia; > a then interchange a anda. 23 procedure ont futon fom Avo B where A [ase n tl, B= (Bors Byla avons 4 ith f(a) =O then begin hig fay) == t count = count +1 end count = m then om = eve ‘ese ont = fale 25, procedure ones: bt sting, ‘oner= 0 fori begin ay = 1 then ‘ones = ones +1 end (ones the numb fone in he bit stings) 27, procedure erary sare: nee, a. increasing integer) 2 a. x Answers to Ost Numbered Exercises S17 ima ‘whites < j—1 beg w+ nAy = DENA) ite> oy then =u +1 che ifs > a then Deg i end che 251 end ix =o, then location = shke ifs =a then location = j tle location =O Heaton isthe subscript of he erm equal 0 (Oifaot found) procedure find a modelo, integer) smodecount ay: nondecreasing while = wanda, = value begin end ‘foun > modecoun then begin ‘modecount end ‘mode is the fst value occuring most often) procedure find duplicaeay. as... ieee) location Z ‘while J <1 and location = 0 ta, = 4; then location che jim 41 iar end cation is the subscript ofthe ist ale that repeats a previous value nthe sequence) procedure find decreases a3... positive integers) location ile <1 and location = 0 S-IBAnenersto Odd Numbered Exercises Ita, < a then location = heii +1 cation isthe subscript ofthe fist vale ess han the immediatly preceding oe] 38. Atte end ofthe fis pas, 3, 5,4, 7a the end of the second pss 3, 8, 7a he endothe third pass 1, 3,4, 5, 7s atthe end of the fourth pas: 1,3, 4 §,7 37 procedure beter bubblesot (a... iter) sdone = false while (= and done = fase) for} ia, > ayy then begin interchange ay and ay. done: = fave end esi end (a). isin increasing order} 29. Atthoend oft ist second andthid passes: 1 3,5, 7,4 atthe end of the fourth pas: 1,3,4,5,7 La), 5, ASL AAR NTA SAAT AS DLSZZSLAVESLARASLAAGS OLAKASLAZASLARASNIRAS 41. We carry out the inca search algorithm given a Algo- ‘ith? inthis secon exceptthat wereplacex # a b9x < a and we replace theese clause with ese cation = n+ 1 AS24344 eon m (wen 2/2 7. Findtbelo- ‘ation forthe 2 in the list 3 (one comparison), and insert it in frat ofthe 3, so thelist now reads 2.3,45,1,6. Find the location forthe 4 (compare i to the 2 and ten the 3), and insert it leaving 2,34 5,16. Find the location fo the 5 (compare i tothe 3 and then the 4), and insert it, leaving 2,3,4,5, 1 6 Find the location fr the | (compare itt the 3nd then the 2 and hen the 2 agin, and inser it leaving 2.3.4, 5,6 Find th loaton forthe 6 (compare ito the 3 and then the 4 and then the 5), and inset i, giving the al answer 1,2.3,4,5,6 4, procedure binary insertion sory +. real numbers wih x 2 2) for j:=2t0” begin binary search for insertion oeation ef right = j=1 wile ft < right begin middle = (left + righty) ay > dat then lef else igh middle end ia, < aug then ile +1 lee = ht +1 (insert nocation by moving trough a. toward back of ist) sus end (a).2, 2, ar sorted} 51. The vation ffom Exercise $0 $3.4) Two quate, fone penny) Two quarters, ne dime, one nike, our pen nies 6) Athree quarts one penny) Two quarters, one dime "$8. Greedy algorithm uss fewest coin in pars (@), (and (a) To quar, one penny) Two guar. ters one dime, nine pennies _e) Three quarters, ne penny 4) Two quarters, one dime 87-4) The variable f wil give the ishing ime ofthe alk ast selected starting ou with cl tothe time the all becomes valbl, Onder the talk in increasing order ofthe ending times, and star atthe tp ofthe list At cach stage ofthe algorithm, go dwn the ist of aks from whereit fof nd find the first one whose tating time isnotlessthan f, Schedule that tlk and upd f tocar ts fishing time. 'b) The 9:00-9:48 alk the 90-10-15 alk, the 10:15-10:5 tlk, the 1:00-11:15 tak $9.) Here we assume tat the menare the suitors andthe women the sites rocedre sabe My Mayo, May Wy Way. We eference ists) fori = Is ark man 8 ejected foris= 10s seta ection listo be empty for j= 1105) ‘et woman / propos ist to be empty whi rejected men remain begin fori s= tos ‘fm is marked rejected then ad to he proposal ist forthe woman j who ranks highest fom is preference st but doesnot pear on hie rejection ist and mark as nt ejected forj:=1t0s ‘if woman js propos iss nonempty then remove from proposal ist all men except the man who ranks highest on her preference list and fr each such man mark him as rejected and add jw his rejection list end for j= 110s "ch / with he one man on / proposal ist (This matching i stable) >) There ae at mos iterations of the while lop, so the algorithm must terminate. Indeed if tthe conclusion of the ‘whe loop rejected men remain, then some man ust hive been rejected and so his rejection ist grew. Thus, each past ‘hrough the while loop, at Teast one more of the +* possible rejections wll have been recorded, unles the lop is about ‘o terminate, Furhermore, when the while lop terminates, each man wll have oe pending proposal and each woman wl have at most one pending propos, so the assignment must be one-to-one. ef the asignmest is not table then there is aman anda woman w such that m prefers wt the ‘woman w wih whom he matched, and prefers tthe ‘man with whom she is matched, But m must have proposed

You might also like