资源描述:
《算法导论第三版答案Ch5.pdf》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Chapter5MichelleBodnar,AndrewLohrFebruary16,2018Exercise5.1-1Wemayhavebeenpresentedthecandidatesinincreasingorderofgoodness.ThiswouldmeanthatwecanapplytransitivitytodetermineourpreferencebetweenanytwocandidatesExercise5.1-2Algorithm1RANDOM(a,b)1:n=dlg(b a+1)e2:Initializeanarr
2、ayAoflengthn3:whiletruedo4:fori=1tondo5:A[i]=RANDOM(0;1)6:endfor7:ifAholdsthebinaryrepresentationofoneofthenumbersinathroughbthen8:returnnumberrepresentedbyA9:endif10:endwhileEachiterationofthewhilelooptakesntimetorun.Theprobabilitythatthewhileloopstopsonagiveniterationis(b a
3、+1)=2n.Thustheexpectedrunningtimeistheexpectednumberoftimesruntimesn.Thisisgivenby:Xi 1n2nb a+1b a+1b a+122ni1 =n=n=O(lg(b a)):2n2n2nb a+1b a+1i1Exercise5.1-3ClearlysinceaandbareIID,theprobabilitythisalgorithmreturnsoneisequaltotheprobabilityitreturns0.Also,sincether
4、eisaconstantpositiveprobability(2p(p 1))thatthealgorithmreturnsoneachiterationofthefor11:foralleternitydo2:a=BiasedRandom3:b=BiasedRandom4:ifa>bthen5:return16:endif7:ifa