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

doesfewerthann–1comparisonsforsomeinputofsizen,thealgorithm

algorithmfindsthelargestkeybydoingatmostn–2comparisonsfor

someinput,atleasttwokeysinthatinputneverloseacomparison.At

leastoneofthosetwokeyscannotbereportedasthelargestkey.We

cancreateanewinputbyreplacing(ifnecessary)thatkeybyakeythat

islargerthanallkeysintheoriginalinput.Becausetheresultsofall

comparisonswillbethesameasfortheoriginalinput,thenewkeywill

notbereportedasthelargest,whichmeansthatthealgorithmwillgive

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

comparisons.Wecanthenfindthesmallestofallthesmallerkeyswith

n/2comparisons.Inthiswaywefindboththesmallestandlargestkeys

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

workwellfortheSelectionproblem.Thereasonisasfollows.Weknow

leavesbecausetherearenpossibleoutcomes.Lemma7.3saysthatifa

binarytreehasnleaves,itsdepthisgreaterthanorequalto[lgn].

Therefore,ourlowerboundonthenumberofleavesgivesusalower

boundof[lgn]comparisonsintheworstcase.Thisisnotaverygood

comparisonsjusttofindthelargestkey(Theorem8.7).Lemma8.1isno

1comparisonnodesinthedecisiontree.Decisiontreesdonotworkwell

fortheSelectionproblembecausearesultcanbeinmorethanoneleaf.

Figure8.9showsthedecisiontreeforAlogrithm8.2(FindLargestKey)

whenn=4.Therearefourleavesthatreport4andtwothatreport3.The

numberofcomparisonsdonebythealgorithmis3ratherthanlgn=lg4

=2.Weseethatlgnisaweaklowerbound.

Figure8.9:ThedecisiontreecorrespondingtoAlogrithm8.2whenn=

4.

"What'syoursign?"Thismeansthatthepersonwantstoknowyour

astrologicalsign.Thereare12suchsigns,eachcorrespondingtoa

periodofapproximately30days.If,forexample,youwerebornAugust

25,yoursignwouldbeVirgo.Tomakethisstaleencountermoreexciting,

havenointentionofdisclosingyoursign—yousimplywanttoforcethe

toleavethemaximumnumberofremainingpossibilitiesandbecause

questionsaspossiblebeforereachingalogicalconclusion.

possible(asyoudidwiththestranger).Ateachpointwherethealgorithm

algorithmgoingaslongaspossible.Theonlyrestrictionisthatthe

thealgorithmtodothebasicinstructionf(n)times,thenf(n)isalower

boundontheworst-casetimecomplexityofthealgorithm.

keys.Toestablishthatbound,wecanassumethatthekeysaredistinct.

Wecandothisbecausealowerboundontheworst-casetime

complexityforasubsetofinputs(thosewithdistinctkeys)isalower

boundontheworst-casetimecomplexitywhenallinputsareconsidered.

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

comparison.Forexample,ifthealgorithmfirstcomparess2ands1,it

s2islarger.Thestateofs2thenchangesfromXtoW,andthestateof

s1changesfromXtoL.Supposethealgorithmnextcomparess3and

LtoWLandthestateofs3changesfromXtoL.Thismeansthattwo

unitsofinformationaredisclosed.Becauseonlyoneunitofinformationis

minimalamountofinformation.Whenitdoesn'tmatterwhichkeyis

strategyisallweneedtoproveourtheorem.Butfirstlet'sshowan

FindstheSmallestandLargestKeys[*]

Before

Comparison

KeyDeclared

Largerby

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

WL

WL

0

[*]ThekeysS andS arebeingcompared.

i

j

Example8.2

inputsizeof5.Wehaveassignedvaluestothekeysthatareconsistent

stateWL.Aneasywaytodothisistoassignvalues.Furthermore,

haveaninputassociatedwithit.Otherthantheorderdeterminedbythe

declaredlargerthans1,wegives3thevalue15.Wecouldhavegivenit

anyvaluegreaterthan10.

Noticethatafters3iscomparedwiths2,wechangethevalueofs3from

thealgorithmiscurrentlymaking.Itisnecessarytochangethevalueof

thats3islargerthans2.

Afterthealgorithmisdone,s1haslosttoallotherkeysands5haswon

overallotherkeys.Therefore,s1issmallestands5islargest.Theinput

aninputthatmakesAlogrithm8.3workhardest.Noticethateight

comparisonsaredoneinTable8.3,andtheworst-casenumberof

comparisonsdonebyAlogrithm8.3whentheinputsizeis5isgivenby

hardaspossiblewhentheinputsizeis5.

Alogrithm8.3foranInputSizeof5

Key

Camparison Declared

Largerby

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

algorithmworkashardaspossible.Youareencouragedtodeterminethe

andLargestKeysbyPairingKeys)andsomeinputsize.

maynotactuallyachievethisgoal.However,regardlessofwhetherornot

thegoalisreached,thestrategycanbeusedtoobtainalowerboundon

strategyjustdescribed.

Theorem8.8

Anydeterministicalgorithmthatcanfindboththesmallestandthelargest

ofnkeysineverypossibleinputonlybycomparisonsofkeysmustinthe

worstcasedoatleastthefollowingnumbersofcomparisonsofkeys:

Proof:Weshowthatthisisalowerboundontheworstcasebyshowing

thatthealgorithmmustdoatleastthismanycomparisonswhenthekeys

aredistinct.Asnotedpreviously,thealgorithmmustlearn2n−2unitsof

informationtofindboththesmallestandlargestkeys.Supposewe

whenbothkeyshavenotbeeninvolvedinpreviouscomparisons.Ifnis

even,thiscanbethecaseforatmost

whichmeansthatthealgorithmcangetatmost2(n/2)=nunitsof

ofinformationintheothercomparisons,thealgorithmmustdoatleast

thereforeforcesthealgorithmtodoatleast

comparisonsofkeys.Itisleftasanexercisetoanalyzethecaseinwhich

nisodd.

BecauseAlogrithm8.4doesthenumberofcomparisonsintheboundin

Theorem8.8,thatalgorithmisoptimalinitsworst-caseperformance.We