Tải bản đầy đủ - 0 (trang)
5 The Selection Problem: Introduction to Adversary Arguments

5 The Selection Problem: Introduction to Adversary Arguments

Tải bản đầy đủ - 0trang

Clearly,thenumberofcomparisonsofkeysdonebythealgorithmis

givenby



Intuitively,itseemsimpossibletoimproveonthisperformance.Thenext

theoremestablishesthatthisisso.Wecanthinkofanalgorithmthat

determinesthelargestkeyasatournamentamongthekeys.Each

comparisonisamatchinthetournament,thelargerkeyisthewinnerof

thecomparison,andthesmallerkeyistheloser.Thelargestkeyisthe

winnerofthetournament.Weusethisterminologythroughoutthis

section.

Theorem8.7

Anydeterministicalgorithmthatcanfindthelargestofnkeysinevery

possibleinputonlybycomparisonsofkeysmustineverycasedoatleast



Proof:Theproofisbycontradiction.Thatis,weshowthatifthealgorithm

doesfewerthann–1comparisonsforsomeinputofsizen,thealgorithm

mustgivethewronganswerforsomeotherinput.Tothatend,ifthe

algorithmfindsthelargestkeybydoingatmostn–2comparisonsfor

someinput,atleasttwokeysinthatinputneverloseacomparison.At

leastoneofthosetwokeyscannotbereportedasthelargestkey.We

cancreateanewinputbyreplacing(ifnecessary)thatkeybyakeythat

islargerthanallkeysintheoriginalinput.Becausetheresultsofall

comparisonswillbethesameasfortheoriginalinput,thenewkeywill

notbereportedasthelargest,whichmeansthatthealgorithmwillgive

thewronganswerforthenewinput.Thiscontradictionprovesthatthe

algorithmmustdoatleastn–1comparisonsforeveryinputofsizen.



YoumustbecarefultointerpretTheorem8.7correctly.Itdoesnotmean

thateveryalgorithmthatsearchesonlybycomparisonsofkeysmustdo

atleastn–1comparisonstofindthelargestkey.Forexample,ifanarray

issorted,wecanfindthelargestkeywithoutdoinganycomparisons



simplybyreturningthelastiteminthearray.However,analgorithmthat

returnsthelastiteminanarraycanfindthelargestkeyonlywhenthe

largestkeyisthelastitem.Itcannotfindthelargestkeyineverypossible

input.Theorem8.7concernsalgorithmsthatcanfindthelargestkeyin

everypossibleinput.

Ofcourse,wecanuseananalogousversionofAlogrithm8.2tofindthe

smallestkeyinn–1comparisonsandananalogousversionofTheorem

8.7toshowthatn–1isalowerboundforfindingthesmallestkey.



8.5.2FindingBoththeSmallestandLargestKeys

Astraightforwardwaytofindthesmallestandlargestkeys

simultaneouslyistomodifyAlogrithm8.2asfollows.

Alogrithm8.3:FindSmallestandLargestKeys

Problem:FindthesmallestandlargestkeysinanarraySofsizen.

Inputs:positiveintegern,arrayofkeysSindexedfrom1ton.

Outputs:variablessmallandlarge,whosevaluesarethesmallestand

largestkeysinS.

voidfind_both(intn,

constkeytypeS[],

keytype&small,

keytype&large)

{

indexi;

small=S[1];

large=S[1];

for(i=2;i<=n;i++)

if(S[i]
small=S[i];

elseif(S[i]>large)

large=S[i];

}



UsingAlogrithm8.3isbetterthanfindingthesmallestandlargestkeys

independently,becauseforsomeinputsthecomparisonofS[i]withlarge

isnotdoneforeveryi.Therefore,wehaveimprovedontheevery-case

performance.ButwheneverS[1]isthesmallestkey,thatcomparisonis

doneforalli.Therefore,theworst-easenumberofcomparisonsofkeys

isgivenby



whichisexactlythenumberofcomparisonsdoneifthesmallestand

largestkeysarefoundseparately.Itmayseemthatwecannotimprove

onthisperformance,butwecan.Thetrickistopairthekeysandfind

whichkeyineachpairissmaller.Thiscanbedonewithaboutn/2

comparisons.Wecanthenfindthesmallestofallthesmallerkeyswith

aboutn/2comparisonsandthelargestofallthelargerkeyswithabout

n/2comparisons.Inthiswaywefindboththesmallestandlargestkeys

withonlyabout3n/2totalcomparisons.Analgorithmforthismethod

follows.Thealgorithmassumesthatniseven.

Alogrithm8.4:FindSmallestandLargestKeysbyPairingKeys

Problem:FindthesmallestandlargestkeysinanarraySofsizen.

Inputs:positiveevenintegern,arrayofkeysSindexedfrom1ton.



Outputs:variablessmallandlarge,whosevaluesarethesmallestand

largestkeysinS.

voidfind_both2(intn,//nisassumedtobeev

constkeytypeS[],

keytype&small,

keytype&large)

{

indexi;

if(S[1]
small=S[1];

large=S[2];

}



else{

small=S[2];

large=S[1];

}

for(i=3;i<=n−1;i=i+2){//Increament

if(S[i]
if(S[i]
small=S[i];

if(S[i+1]>large)

large=S[i+1];

}

else{

if(S[i+1]
small=S[i+1];

if(S[i]>large)

large=S[i];

}

}

}



Itisleftasanexercisetomodifythealgorithmsothatitworkswhennis

oddandtoshowthatitsnumberofcomparisonofkeysisgivenby



Canweimproveonthisperformance?Weshowthattheansweris"no."

Wedonotuseadecisiontreetoshowthisbecausedecisiontreesdonot

workwellfortheSelectionproblem.Thereasonisasfollows.Weknow

thatadecisiontreefortheSelectionproblemmustcontainatleastn

leavesbecausetherearenpossibleoutcomes.Lemma7.3saysthatifa

binarytreehasnleaves,itsdepthisgreaterthanorequalto[lgn].

Therefore,ourlowerboundonthenumberofleavesgivesusalower

boundof[lgn]comparisonsintheworstcase.Thisisnotaverygood



lowerbound,becausewealreadyknowthatittakesatleastn−1

comparisonsjusttofindthelargestkey(Theorem8.7).Lemma8.1isno

moreusefulbecausewecanonlyreadilyestablishthattheremustben−

1comparisonnodesinthedecisiontree.Decisiontreesdonotworkwell

fortheSelectionproblembecausearesultcanbeinmorethanoneleaf.

Figure8.9showsthedecisiontreeforAlogrithm8.2(FindLargestKey)

whenn=4.Therearefourleavesthatreport4andtwothatreport3.The

numberofcomparisonsdonebythealgorithmis3ratherthanlgn=lg4

=2.Weseethatlgnisaweaklowerbound.





Figure8.9:ThedecisiontreecorrespondingtoAlogrithm8.2whenn=

4.

Weuseanothermethod,calledanadversaryargument,toestablishour

lowerbound.Anadversaryisanopponentorfoe.Supposeyouareinthe

localsinglesbarandaninterestedstrangeraskedyouthetiredquestion,

"What'syoursign?"Thismeansthatthepersonwantstoknowyour

astrologicalsign.Thereare12suchsigns,eachcorrespondingtoa

periodofapproximately30days.If,forexample,youwerebornAugust

25,yoursignwouldbeVirgo.Tomakethisstaleencountermoreexciting,

youdecidetospicethingsupbybeinganadversary.Youtellthestranger

toguessyoursignbyaskingyes/noquestions.Beinganadversary,you

havenointentionofdisclosingyoursign—yousimplywanttoforcethe

strangertoaskasmanyquestionsaspossible.Therefore,youalways

provideanswersthatnarrowdownthesearchaslittleaspossible.For

example,supposethestrangerasks,"Wereyouborninsummer?"

Becausea"no"answerwouldnarrowthesearchtoninemonthsanda



"yes"answerwouldnarrowittothreemonths,youanswer"no."Ifthe

strangerthenasks,"Wereyouborninamonthwith31days?"you

answer"yes,"becausemorethanhalfoftheninepossiblemonths

remaininghave31days.Theonlyrequirementofyouranswersisthat

theybeconsistentwithonesalreadygiven.Forexample,ifthestranger

forgetsthatyousaidyouwerenotborninsummerandlaterasksifyou

wereborninJuly,youcouldnotanswer"yes"becausethiswouldnotbe

consistentwithyourpreviousanswers.Inconsistentanswershaveno

sign(birthday)satisfyingthem.Becauseyouanswereachquestionsoas

toleavethemaximumnumberofremainingpossibilitiesandbecause

youranswersareconsistent,youforcethestrangertoaskasmany

questionsaspossiblebeforereachingalogicalconclusion.

Supposesomeadversary'sgoalistomakeanalgorithmworkashardas

possible(asyoudidwiththestranger).Ateachpointwherethealgorithm

mustmakeadecision(forexample,afteracomparisonofkeys),the

adversarytriestochoosearesultofthedecisionthatwillkeepthe

algorithmgoingaslongaspossible.Theonlyrestrictionisthatthe

adversarymustalwayschoosearesultthatisconsistentwiththose

alreadygiven.Aslongastheresultsareconsistent,theremustbean

inputforwhichthissequenceofresultsoccurs.Iftheadversaryforces

thealgorithmtodothebasicinstructionf(n)times,thenf(n)isalower

boundontheworst-casetimecomplexityofthealgorithm.

Weuseanadversaryargumenttoobtainalowerboundontheworstcasenumberofcomparisonsneededtofindboththelargestandsmallest

keys.Toestablishthatbound,wecanassumethatthekeysaredistinct.

Wecandothisbecausealowerboundontheworst-casetime

complexityforasubsetofinputs(thosewithdistinctkeys)isalower

boundontheworst-casetimecomplexitywhenallinputsareconsidered.

Beforepresentingthetheorem,weshowouradversary'sstrategy.

Supposewehavesomealgorithmthatsolvestheproblemoffindingthe

smallestandlargestkeysonlybycomparisonsofkeys.Ifallthekeysare

distinct,atanygiventimeduringtheexecutionofthealgorithmagiven

keyhasoneofthefollowingstates:

State DescriptionofState



X



Thekeyhasnotbeeninvolvedinacomparison.



L



Thekeyhaslostatleastonecomparisonandhasneverwon.



W



Thekeyhaswonatleastonecomparisonandhasneverlost.



WL



Thekeyhaswonatleastonecomparisonandhaslostatleast

one.



Wecanthinkofthesestatesascontainingunitsofinformation.Ifakey

hasstateX,therearezerounitsofinformation.IfakeyhasstateLorW,

thereisoneunitofinformationbecauseweknowthatthekeyhaseither

lostorwonacomparison.IfakeyhasstateWL,therearetwounitsof

informationbecauseweknowthatthekeyhasbothwonandlost

comparisons.Forthealgorithmtoestablishthatonekeysmallissmallest

andanotherkeylargeislargest,thealgorithmmustknowthateverykey

otherthansmallhaswonacomparisonandeverykeyotherthanlarge

haslostacomparison.Thismeansthatthealgorithmmustlearn



unitsofinformation.

Becausethegoalistomakethealgorithmworkashardaspossible,an

adversarywantstoprovideaslittleinformationaspossiblewitheach

comparison.Forexample,ifthealgorithmfirstcomparess2ands1,it

doesn'tmatterwhatanadversaryanswers,becausetwounitsof

informationaresuppliedeitherway.Let'ssaytheadversaryanswersthat

s2islarger.Thestateofs2thenchangesfromXtoW,andthestateof

s1changesfromXtoL.Supposethealgorithmnextcomparess3and

s1.Ifanadversaryanswersthats1islarger,thestateofs1changesfrom

LtoWLandthestateofs3changesfromXtoL.Thismeansthattwo

unitsofinformationaredisclosed.Becauseonlyoneunitofinformationis

disclosedbyansweringthats3islarger,wehaveouradversarygivethis

answer.Table8.2showsanadversarystrategythatalwaysdisclosesa

minimalamountofinformation.Whenitdoesn'tmatterwhichkeyis

answered,wehavesimplychosenthefirstoneinthecomparison.This

strategyisallweneedtoproveourtheorem.Butfirstlet'sshowan



exampleofhowouradversarywouldactuallyusethestrategy.

Table8.2:OurAdversary'sStrategyforFoilinganAlgorithmThat

FindstheSmallestandLargestKeys[*]

Before

Comparison



KeyDeclared

Largerby

Adversary



After

Comparison



UnitsofInformation

Learnedby

Algorithm



Si



Sj







Si



Sj







X



X



Si



W



L



2



X



L



Si



W



L



1



X



W



Sj



L



W



1



X



WL



Si



W



WL



1



L



L



Si



WL



L



1



L



W



Sj



L



W



0



L



WL



Sj



L



WL



0



W



W



Si



W



WL



1



W



WL



Si



W



WL



0



WL



WL



Consistentwith

previousanswers



WL



WL



0



[*]ThekeysS andS arebeingcompared.

i

j



Example8.2



Table8.3showsouradversary'sstrategyforfoilingAlogrithm8.3foran

inputsizeof5.Wehaveassignedvaluestothekeysthatareconsistent

withtheanswers.Assigningsuchvaluesisnotreallynecessary,butthe

adversarymustkeeptrackoftheorderofthekeysimposedbythe

answers,sothataconsistentanswercanbegivenwhenbothkeyshave

stateWL.Aneasywaytodothisistoassignvalues.Furthermore,

assigningvaluesillustratesthataconsistentsetofanswersdoesindeed

haveaninputassociatedwithit.Otherthantheorderdeterminedbythe

adversary'sstrategy,theanswersarearbitrary.Forexample,whens3is

declaredlargerthans1,wegives3thevalue15.Wecouldhavegivenit

anyvaluegreaterthan10.

Noticethatafters3iscomparedwiths2,wechangethevalueofs3from

15to30.Rememberthattheadversaryhasnoactualinputwhen

presentedwiththealgorithm.Ananswer(andthereforeaninputvalue)is

constructeddynamicallywheneverouradversaryisgiventhedecision

thealgorithmiscurrentlymaking.Itisnecessarytochangethevalueof

s3from15becauses2islargerthan15andouradversary'sansweris

thats3islargerthans2.

Afterthealgorithmisdone,s1haslosttoallotherkeysands5haswon

overallotherkeys.Therefore,s1issmallestands5islargest.Theinput

constructedbyouradversaryhass1asthesmallestkeybecausethisis

aninputthatmakesAlogrithm8.3workhardest.Noticethateight

comparisonsaredoneinTable8.3,andtheworst-casenumberof

comparisonsdonebyAlogrithm8.3whentheinputsizeis5isgivenby



ThismeansthatouradversarysucceedsinmakingAlogrithm8.3workas

hardaspossiblewhentheinputsizeis5.



Table8.3:OurAdversary'sAnswersinAttemptingtoFoil

Alogrithm8.3foranInputSizeof5



Key

Camparison Declared

Largerby

Adversary









S2


S2



S2>S1



States/AssignedValues



Unitsof

Information

Learnedby

Algorithm



S4



S5







L/10 W/20 X



X



X



2



S2



L/10 W/20 X



X



X



0



S3


S3



L/10 W/20 W/15 X



X



1



S3>S2



S3



L/10 WL/20 W/30 X



X



1



S4


S4



L/10 WL/20 W/30 W/15 X



1



S4>S3



S4



L/10 WL/20 WL/30 W/40 X



1



S5


S5



L/10 WL/20 WL/30 W/40 W/15



1



S5>S4



S5



L/10 WL/20 WL/30 WL/40 W/50



1



S1 S2



S3



Whenpresentedwithadifferentalgorithmforfindingthelargestand

smallestkeys,ouradversarywillprovideanswersthattrytomakethat

algorithmworkashardaspossible.Youareencouragedtodeterminethe

adversary'sanswerswhenpresentedwithAlogrithm8.4(FindSmallest

andLargestKeysbyPairingKeys)andsomeinputsize.

Whendevelopinganadversarystrategy,ourgoalistomakealgorithms

forsolvingsomeproblemworkashardaspossible.Apooradversary

maynotactuallyachievethisgoal.However,regardlessofwhetherornot

thegoalisreached,thestrategycanbeusedtoobtainalowerboundon

howhardthealgorithmsmustwork.Nextwedothisfortheadversary

strategyjustdescribed.

Theorem8.8



Anydeterministicalgorithmthatcanfindboththesmallestandthelargest

ofnkeysineverypossibleinputonlybycomparisonsofkeysmustinthe

worstcasedoatleastthefollowingnumbersofcomparisonsofkeys:



Proof:Weshowthatthisisalowerboundontheworstcasebyshowing

thatthealgorithmmustdoatleastthismanycomparisonswhenthekeys

aredistinct.Asnotedpreviously,thealgorithmmustlearn2n−2unitsof

informationtofindboththesmallestandlargestkeys.Supposewe

presentouradversarywiththealgorithm.Table8.2showsthatour

adversaryprovidestwounitsofinformationinasinglecomparisononly

whenbothkeyshavenotbeeninvolvedinpreviouscomparisons.Ifnis

even,thiscanbethecaseforatmost



whichmeansthatthealgorithmcangetatmost2(n/2)=nunitsof

informationinthisway.Becauseouradversaryprovidesatmostoneunit

ofinformationintheothercomparisons,thealgorithmmustdoatleast



additionalcomparisonstogetalltheinformationitneeds.Ouradversary

thereforeforcesthealgorithmtodoatleast



comparisonsofkeys.Itisleftasanexercisetoanalyzethecaseinwhich

nisodd.



BecauseAlogrithm8.4doesthenumberofcomparisonsintheboundin

Theorem8.8,thatalgorithmisoptimalinitsworst-caseperformance.We

havechosenaworthyadversarybecausewehavefoundanalgorithm



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

5 The Selection Problem: Introduction to Adversary Arguments

Tải bản đầy đủ ngay(0 tr)

×