+ All Categories
Home > Documents > Module 9: Mathematical Inductioncs121/2016W2/slides/gao/9-Induction.pdf · Module 9: Announcements...

Module 9: Mathematical Inductioncs121/2016W2/slides/gao/9-Induction.pdf · Module 9: Announcements...

Date post: 31-Jan-2018
Category:
Upload: buianh
View: 214 times
Download: 0 times
Share this document with a friend
69
Module 9: Mathematical Induction
Transcript

Module9:MathematicalInduction

Module9:Announcements

•ThereisachancethatthisFriday’sclassmaybecancelled.

•Assignment#4isdueThursdayMarch16th at4pm.

•Midterm#2•MondayMarch20th,2017from7pmto9pm•ReadthePiazzapost.• Letusknowifyouhaveaconflict.

Learninggoals:pre-class

•Epp 4th section5.1:•Convertsequencestoandfromexplicitformulasthatdescribethesequence.

•Convertsumstoandfromsummation/Σ notation.•Convertproductstoandfromproduct/πnotation.•Manipulateformulasinsummation/productnotationbyadjustingtheirbounds,mergingorsplittingsummations/products,andfactoringoutvalues.

Learninggoals:pre-class

•Epp 4th sections5.2to5.4:•Givenatheoremtoprovestatedintermsofitsinductionvariable(i.e.,usually,intermsofn),writeouttheskeletonofaninductiveproofincluding:• thebasecase(s)thatneedtobeproven,• theinductionhypothesis,and• theinductivestepthatneedstobeproven.

Learninggoals:in-class

•Bytheendofthismodule,youshouldbeableto:• Establishpropertiesofself-referentialstructuresusinginductiveproofsthatnaturallybuildonthoseself-references.

•Critiqueformalinductiveproofstodeterminewhethertheyarevalidandwheretheerror(s)lieiftheyareinvalid.

•Provepropertiesofthenon-negativeintegers(orasubset)withappropriateself-referentialstructureusingweakorstronginductionasneeded.

TheBIG questions:

•Howcanweconvinceourselvesthatanalgorithmdoeswhatit'ssupposedtodo?

•Howdowedeterminewhetherornotonealgorithmisbetterthananotherone?

•Mathematicalinductionisavery usefultoolwhenprovingthecorrectnessorefficiencyofanalgorithm.

•Wewillseeseveralexamplesofthis.

Module9 outline

•Example:single-eliminationtournaments.•Definingandvalidatingmathematicalinduction.•Moreexampleswherewecanuseinduction.•Aslightlydifferenttypeofinduction.

Single-eliminationtournaments

•Problem:single-eliminationtournament• Teamsplayoneanotherinpairs• Thewinnerofeachpairadvancestothenextround

Montréal

Montréal

Los Angeles

Montréal

Toronto

New York Isl.

Vancouver

St. Louis

Pittsburgh

Buffalo

Los Angeles

Los Angeles

Toronto

New York Isl. Montréal

Round 2 Round 3 Round 4

Single-eliminationtournaments

• Ifwehaven roundsofplayoffs,howmanyteamscanparticipate?a.nb.2nc.n2

d.2ne.Noneoftheabove.

•Let’sprovethisusingmathematicalinduction.

Theinductivestep

If2n teamscanparticipateinaplayoffwithnrounds,howmanyteamscanparticipateinaplayoffwith2n+1 rounds?a. 2n +1b. 2n+1c. (n+1)2d. Noneoftheabove.

Theinductivestep

Theorem:If2n teamscanparticipateinaplayoffofnrounds,then2n+1 teamscanparticipateinaplayoffofn+1rounds.Let𝑃(𝑥, 𝑦) betrueifyteamscanplayinxrounds.Inpredicatelogic:

∀𝑛 ∈ 𝑍, 𝑃(𝑛, 2,) → 𝑃(𝑛 + 1, 2,01)Let’sprovethis.

Theinductivestep

Theorem:If2n teamscanparticipateinaplayoffofnrounds,then2n+1 teamscanparticipateinaplayoffofn+1rounds.

Proof:Consideranunspecifiedplayoffwithnrounds.Assumethat2n teamscanparticipateinaplayoffofnrounds.Wecanthinkofaplayoffwithn+1roundsasfollows:Twoplayoffswithnroundsproceedinparallel.Thetwowinnersthencompleteinonemoreroundtodeterminethewinner.

Sinceeachplayoffwithnroundshas2n teams,aplayoffwithn+1roundshas2n+1 teams.

QED

Thecompleteinductionproof

Theorem:Foranypositiveintegern,atmost2nteamscanparticipateinaplayoffofnrounds.Inpredicatelogic,thetheorembecomes:

∀𝑛 ∈ 𝑍, 𝑃(𝑛, 2,)Proof:Weprovethisbyinduction.Basecase:Inductionstep:

Whatdidweprovewithinduction?

Theorem:∀𝑛 ∈ 𝑍, 𝑃 𝑛, 2,≡ 𝑃 1,2 ∧ 𝑃 2,4 ∧ 𝑃 3,8 ∧ 𝑃 4,16 …

Proof:Weprovethisbyinduction.Basecase:𝑛 = 1,𝑃(1,2) istrue.Inductionstep:Wewillprovethat∀𝑛 ∈ 𝑍, 𝑃 𝑛, 2, → 𝑃 𝑛 + 1, 2,01 ≡ 𝑃(1,2) →𝑃(2,4) ∧ 𝑃 2,4 → 𝑃(3,8) ∧ 𝑃 3,8 → 𝑃(4,16) ∧…

Whatdidweprovewithinduction?

𝑃(1,2)𝑃(1,2) → 𝑃(2,4)𝑃(2,4) bymodusponens

𝑃(2,4)𝑃(2,4) → 𝑃(3,8)𝑃(3,8) bymodusponens

𝑃(3,8)𝑃(3,8) → 𝑃(4,16)𝑃(4,16) bymodusponens

Therefore,wehaveshownthat:𝑃 1,2 ∧ 𝑃 2,4 ∧ 𝑃 3,8 ∧𝑃 4,16 ∧…

≡ ∀𝑛 ∈ 𝑍, 𝑃 𝑛, 2,

Theconnectiontorecursion

•Foranunspecifiedpositiveintegern•Toprove𝑃(𝑛 + 1, 2,01):

•Wecaninsteadprove𝑃(𝑛, 2,).• Thisisarecursivecall.

• Inductionismoreorlessthesameasrecursion!•Afunexampleinvolvinginduction:http://www.youtube.com/watch?v=daRS98L9rHs&feature=related

Module9 outline

•Example:single-eliminationtournaments.•Definingandvalidatingmathematicalinduction.•Moreexampleswherewecanuseinduction.•Aslightlydifferenttypeofinduction.

Orderingstudentsusingadjacentswaps

•Wewanttoorderagroupof5studentsinorderofdayofbirth

•Weareonlyallowedtoswappairsofadjacentstudents.

•Weclaimedthemaximumnumberofswapsfornstudentsisn(n-1)/2.

Howmanyswapsdoweneed?

•Let’splacestudentsfromlefttoright.• Thestudentsalreadyplacedareorderedbydayofbirth.

•Weswapeachnewstudentwithhis/herneighbouruntilhe/sheisattherightplace.

• Theith studentmaybeswappedwithallprevious(i-1)students.

•Sothetotalnumberofswapsis

•Theorem1:

:𝑖,<1

=>?

∀𝑛 ∈ 𝑍0,: 𝑖,<1

=>?

=𝑛 𝑛 − 1

2

Theorem1:

•Let’sparsethetheoremfirst.

Onewaytothinkaboutit:•Theorem1:0 = 0 ∧ 0 + 1 = 1 ∧ 0 + 1 + 2 = 3 ∧0 + 1 + 2 + 3 = 6 ∧ 0 + 1 + 2 + 3 + 4 = 10 ∧…Anotherwaytothinkaboutit:•Let

•Theorem1:𝑃(1) ∧ 𝑃(2) ∧ 𝑃(3) ∧ 𝑃(4) ∧…

∀𝑛 ∈ 𝑍0,: 𝑖,<1

=>?

=𝑛 𝑛 − 1

2

P 𝑛 ≡ : 𝑖,<1

=>?

=𝑛 𝑛 − 1

2

Theorem1:

Whichbasecase(s)doweneedtoprove?a. n=0b. n=1c. n =2d. n=0andn=1e. n=1andn=2

∀𝑛 ∈ 𝑍0,: 𝑖,<1

=>?

=𝑛 𝑛 − 1

2

Theorem1:

Whatdoweneedtoproveintheinductionstep?

a.

b.

c. Bothofaandbarecorrect.

d. Neitherofaandbarecorrect.

∀𝑛 ∈ ℤ0 : 𝑖,<1

=>?

=𝑛 𝑛 − 1

2 → :𝑖,

=>?

=𝑛 + 1 𝑛2

∀𝑛 ∈ ℤ0,: 𝑖,<1

=>?

=𝑛 𝑛 − 1

2 → ∀𝑛 ∈ ℤ0,:𝑖,

=>?

=𝑛 + 1 𝑛2

∀𝑛 ∈ 𝑍0,: 𝑖,<1

=>?

=𝑛 𝑛 − 1

2

Whatisinductionproving?

•Basecase:𝑃 1• Inductionstep:𝑃(1) → 𝑃(2) ∧ 𝑃 2 → 𝑃 3 ∧ 𝑃 3 → 𝑃 4 ∧ ⋯

•Combiningthetwo,wehaveproventhat𝑃(1) ∧ 𝑃 2 ∧ 𝑃 3 ∧ 𝑃 4 ∧ ⋯

P 𝑛 ≡ : 𝑖,<1

=>?

=𝑛 𝑛 − 1

2

Afunvideoaboutinduction…

http://www.youtube.com/watch?v=daRS98L9rHs&feature=related

Theorem1:

•Proof:Weprovethetheorembyinduction.•Basecase:n=1

Clearly,withonly1student,noswapisneeded:whichisequalto1*(1-1)/2=0

• Inductionstep:• Pickanunspecifiedn≥ 1.Assumethatwhenwehavenstudents,weneedatmostn(n-1)/2swaps.Equivalentlyweassumethat

• ThisiscalledtheInductionHypothesis.

:𝑖,<1

=>?

=𝑛 𝑛 − 1

2

∀𝑛 ∈ 𝑍0,: 𝑖,<1

=>?

=𝑛 𝑛 − 1

2

Theorem1:

•Proof(continued)• Inductionstep(continued)

Forn+1students,weneedatmostswaps.

Bytheinductionhypothesis,thisisequalto

swaps,asrequiredfortheinductionstep.•HencebytheprincipleofM.I.,thetheoremholds.QED

:𝑖,

=>?

= : 𝑖,<1

=>?

+ 𝑛

𝑛 𝑛 − 12 + 𝑛 =

𝑛 + 1 𝑛2

∀𝑛 ∈ 𝑍0,: 𝑖,<1

=>?

=𝑛 𝑛 − 1

2

Inductionislikerecursion

Whereisourrecursivecallintheinductionproof?a. Basecase:b. Inductionstep:consideranunspecifiedpositive

integern.c. Inductionstep:Assumethat…(wherewestated

ourinductionhypothesis)d. Inductionstep:Thealgebrawhereweapplied

ourinductionhypothesise. Inductionstep:someotheralgebrasteps

Inductionislikerecursion

• Idon’tknowhowtocalculate0+1+…+(n-2)+(n-1)+n

•However,ifyoutellmehowtocalculate0+1+...+(n-2)+(n-1)

• Icancalculatethefirstsumasfollows0+1+…+(n-2)+(n-1)+n=(0+1+...+(n-2)+(n-1))+n

Reviewquestionsoninduction

•Giveme3criticalcomponentsofaninductionproof.

•Whenwearecompletingtheinductionstep,whatisourmostimportantgoalinlife?

•Writinganinductionproofislikeplayingdominos.•Whatdoesthebasecasecorrespondto?•Whatdoestheinductionstepcorrespondto?

• Inductionissimilartorecursion.•Whichpartofaninductionproofcorrespondstotherecursivecall?

Geometricseries

•Theorem2:

•Thesesummationsoccurfrequentlywhenweneedtodeterminetherunningtimeofdivide-and-conqueralgorithms.

∀𝑡 ∈ 𝑁,:5=H

=>?

=5H01 − 15 − 1

Theorem2:

•Whatshouldweproveinthebasecase?a. t=-1b. t=0c. t=1d. t=2e. t=3

∀𝑡 ∈ 𝑁,:5=H

=>?

=5H01 − 15 − 1

Theorem2:

Whatdoweneedtoproveintheinductionstep?

a.

b.

c. Bothofaandbarecorrect.

d. Neitherofaandbarecorrect.

∀𝑡 ∈ 𝑁,:5=H

=>?

=5H01 − 15 − 1

∀𝑡 ∈ 𝑁, :5=H

=>?

=5H01 − 15 − 1 → :5=

H01

=>?

=5H0I − 15 − 1

∀𝑡 ∈ 𝑁,:5=H

=>?

=5H01 − 15 − 1 → ∀𝑡 ∈ 𝑁,:5=

H01

=>?

=5H0I − 15 − 1

Theorem2:

Proof:•Basecase:t=0Thesummationis50 =1,and

• Inductionstep:Consideranunspecifiednaturalnumbert.Assumethat…Weneedtoprovethat…

HencebytheprincipleofM.I.,thetheoremholds.QED

51 − 15 − 1 = 1

∀𝑡 ∈ 𝑁,:5=H

=>?

=5H01 − 15 − 1

Provinganinequality

•Theorem3:Forallintegers𝑛 ≥ 4, 2𝑛 < 𝑛!•Rulesforinequalities:

• Startfromoneside(saytheleftside)•Workstepbysteptowardstheother.•Whendealingwith<,youareallowedtomaketheexpressionlarger,butneversmaller.• Example:ifIamsmallerthanyou,thenIamstillsmallerthanyouwhenyoustandonabench.

Theorem3:∀𝑛 ≥ 4, 2𝑛 < 𝑛!

• Anotherversionoftheinductionstep:Wewanttoprovethat

∀𝑛 ≥? , 2,<1 < 𝑛 − 1 ! → 2, < 𝑛!.Consideranunspecifiedn≥?.Inductionhypothesis:assumethat2n-1 <(n-1)!

2n =2(2n-1)<2(n-1)!<n(n-1)!=n!

Whatisthesmallestvalueofnthatweshoulduseintheinductionstep?(a) lessthan4 (d)5(b) 3 (e)morethan5(c) 4

Sumofinvertedsquares

•Theorem4:

•Aninterestingmathematicalfact(whichdoesn’thelpwiththeproof):

•Let’sprovethis!

∀𝑛 ≥ 1,:1𝑖I

,

=>1

≤ 2 −1𝑛

:1𝑖I

Q

=>1

=πI

6

Midterm2

•Workthrough thepostedpracticeproblemonconvertingaDFAtoasequentialcircuit!

•Topicscovered:•ConvertingaDFAtoasequentialcircuit•Predicatelogictoproofstructure(assign#4Q1)•Proofstructuretopredicatelogic(assign#4Q2)•Directproof•Proofbycontrapositiveand/orcontradiction• Induction

Theorem4:

•Afterapplyingtheinductionhypothesis,whatinequalitydoweneedtoprove?

a.

b.

c.

d.

2 −1𝑛 +

1(𝑛 + 1)I ≤ 2 −

1𝑛 + 1

2 −1𝑛 +

1(𝑛 + 1)I ≤ 2 −

1𝑛

2 −1

𝑛 + 1 +1

(𝑛 + 1)I ≤ 2 −1𝑛

2 −1

𝑛 + 1 +1

(𝑛 + 1)I ≤ 2 −1

𝑛 + 1

∀𝑛 ≥ 1,:1𝑖I

,

=>1

≤ 2 −1𝑛

Midterm2

•Workthrough thepostedpracticeproblemonconvertingaDFAtoasequentialcircuit!

•Topicscovered:•ConvertingaDFAtoasequentialcircuit•Predicatelogictoproofstructure(assign#4Q1)•Proofstructuretopredicatelogic(assign#4Q2)•Directproof•Proofbycontrapositiveand/orcontradiction• Induction

Sumofinvertedsquares

•Theorem4:

•Aninterestingmathematicalfact(whichdoesn’thelpwiththeproof):

•Let’sprovethis!

∀𝑛 ≥ 1,:1𝑖I

,

=>1

≤ 2 −1𝑛

:1𝑖I

Q

=>1

=πI

6

Theorem4:

•Whatshouldweproveinthebasecase?a. n=-1b. n=0c. n=1d. n =2e. n =3

∀𝑛 ≥ 1,:1𝑖I

,

=>1

≤ 2 −1𝑛

Theorem4:

•Whatdoweneedtoproveintheinductionstep?

a.

b.

c. Bothofaandbarecorrect.

d. Neitherofaandbarecorrect.

∀𝑛 ≥ 1,:1𝑖I

,

=>1

≤ 2 −1𝑛

∀𝑡 ∈ 𝑁, :1𝑖I

,

=>1

≤ 2 −1𝑛 → :

1𝑖I

,01

=>1

≤ 2 −1

𝑛 + 1

∀𝑡 ∈ 𝑁,:1𝑖I

,

=>1

≤ 2 −1𝑛 → ∀𝑡 ∈ 𝑁,:

1𝑖I

,01

=>1

≤ 2 −1

𝑛 + 1

ProvingatheoremaboutaDFA

•ConsiderthefollowingDFA:

320

4

1

WhichstringsdoesthisDFAaccept?

• Inadditiontotheemptystring,whatotherstringsdoesthisDFAaccept?a. Stringswhoselengthisdivisibleby5.b. Unsignedbinaryintegersthatareprime

numbers.c. Stringswithalternating1’sand0’sd. Unsignedbinaryintegersthataredivisibleby5.

WhichstringsdoesthisDFAaccept?

• Inadditiontotheemptystring,whatotherstringsdoesthisDFAaccept?a. Stringswhoselengthisdivisibleby5.b. Unsignedbinaryintegersthatareprime

numbers.c. Stringswithalternating1’sand0’sd. Unsignedbinaryintegersthataredivisibleby5.

ProvingatheoremaboutthisDFA

•SupposethatwewanttoprovethatthisDFAonlyacceptsbinarynumbersthataredivisibleby5.

•Whichstatementshould weprove(ignoringemptystrings)?a.TheDFAisinstate0ifandonlyifsisdivisibleby5.b.TheDFAisinstaterifandonlyifristheremainderwhenwedivides by5.

ProvingatheoremaboutthisDFA

• SupposethatwewanttoprovethatthisDFAonlyacceptsbinarynumbersthataredivisibleby5.

•Whichstatementshould weprove(ignoringemptystrings)?a.TheDFAisinstate0ifandonlyifsisdivisibleby5.b.TheDFAisinstaterifandonlyifristheremainderwhenwedivides by5.

•Wecannotproveafactaboutstate0withoutdiscussingtheotherstates.

Theorem:TheDFAendsupinstaterafterreadingastringsifandonlyif∃𝑞 ∈ 𝑍, 𝑠 = 5𝑞 + 𝑟.

Proof:byinductiononthelengthnofs.•Basecase:n=1

ThiscanbeverifiedeasilybylookingattheDFA.• Inductionstep:•Consideranunspecifiedinteger𝑛 ≥ 2.

Inductionhypothesis:assumethattheDFAbehavescorrectlyforanystringwithnbits.

WeneedtoshowthattheDFAbehavescorrectlyforanystringwithn+1bits.

Theorem:TheDFAendsupinstaterafterreadingastringsifandonlyif∃𝑞 ∈ 𝑍, 𝑠 = 5𝑞 + 𝑟.

Proof(continued):Supposethats=bn-1bn-2...b2b1b0.Afterreadingthebitsbn-1bn-2...b2b1,theDFAisinstater.Bytheinductionhypothesis,thebinaryintegerbn-1bn-2...b2b1 =5q+rforsomeintegerq.Now,s=2(bn-1bn-2...b2b1)+b0 =2(5q+r)+b0 =10q+2r+b0 andsos=5(2q)+(2r+b0).SotheDFAshouldhavethefollowingtransitions:

r b0 2r+b0

0 01 02 03 04 0

r b0 2r+b0

0 11 12 13 14 1

Theorem:TheDFAendsupinstaterafterreadingastringsifandonlyif∃𝑞 ∈ 𝑍, 𝑠 = 5𝑞 + 𝑟.

Proof(continued):Itdoes,andhenceafterreadingalln bitsofs,theDFAendsupinstater ifandonlyif∃𝑞 ∈ 𝑍, 𝑠 =5𝑞 + 𝑟.

•Hencebytheprincipleofmathematicalinduction,theDFAendsupinstater afterreadingastrings ifandonlyif∃𝑞 ∈ 𝑍, 𝑠 = 5𝑞 + 𝑟.QED

•Corollary:theDFAisinstate0ifandonlyifs isdivisibleby5.

Module9 outline

•Example:single-eliminationtournaments.•Definingandvalidatingmathematicalinduction.•Moreexampleswherewecanuseinduction.•Aslightlydifferenttypeofinduction.

Theinductionwehaveseensofar

•Tosolven+1,wemakearecursivecallton(thepreviouscase).

•Examples• (50 +51 +...+5n-1 +5n)+5n+1•2n <n!--->2n+1 <(n+1)!• TheDFAbehavescorrectlyforastringwithnbits,thenitalsobehavescorrectlyforastringwith(n+1)bits.

•Whatifweneedtomakerecursivecalltooneormorepreviouscases?

Carryingwateracrossadesert

Weneedtobuyjugsofwaterinordertocrossadesert.Unfortunately,thewaterjugsonlycomeintwosizes:5litersand7liters.Provethataslongasn>=24,wecanbuynlitersofwaterusing5-literand7-literjugsonly.

Theinductionstep/therecursivecall

•Whichofthefollowingideascanbeusedfortheinductionstep?• IfIcanbuyn-1 litersofwater,thenIcanalsobuynlitersofwater.

• IfIcanbuyn-3 litersofwater,thenIcanalsobuynlitersofwater.

• IfIcanbuyn-5 litersofwater,thenIcanalsobuynlitersofwater.

• IfIcanbuyn-7 litersofwater,thenIcanalsobuynlitersofwater.

Theinductionstep

•Version1:P(n-5)->P(n)• Supposethatwecanbuy(n-5)litersofwaterusingx5-literjugsandy7-literjugs.

• Thenwecanbuynlitersofwaterusing(x+1)5-literjugsandy7-literjugsonly.

•Version2:P(n-7)->P(n)• Supposethatwecanbuy(n-7)litersofwaterusingx5-liter jugsandy7-literjugs.

• Thenwecanbuynlitersofwaterusingx 5-literjugsand(y+1)7-literjugsonly.

Thebasecases

• Inductionstep:•Assumethatwecanbuy(n-5)Lofwater,thenwecanalsobuynL ofwater.

•Whatisthesmallestvalueofnforwhichwecanapplytheinductionstep?a. 24b. 25c. 28d. 29e. 30

Thebasecases

• Inductionstep:•Assumethatwecanbuy(n-5)Lofwater,thenwecanalsobuynL ofwater.

•Howmanybasecasesdoweneedtoprove?a. 1b. 3c. 5d. 7e. 9

AlowerboundforFibonaccinumbers

TheFibonaccinumbersaredefinedasfollows:F(0)=0F(1)=1F(n)=F(n-1)+F(n-2)foranyn>=2.

Theorem:foreveryintegern>=1,F(n)>=1.6n-2.

Theinductionstep/therecursivecall

•Whatshouldweproveintheinductionstep?a. IfF(n-1)satisfiesthetheorem,thenF(n)also

satisfiesthetheorem.b. IfF(n-2)satisfiesthetheorem,thenF(n)also

satisfiesthetheorem.c. IfF(n-2)andF(n-1)satisfythetheorem,thenF(n)

alsosatisfiesthetheorem.d. IfF(n)andF(n+1)satisfythetheorem,then

F(n+2)alsosatisfiesthetheorem.

Let’scompletetheinductionstep.

Thebasecases

•Whatisthesmallestvalueofnforwhichwecanapplytheinductionstep?a. 0b. 1c. 2d. 3e. 4

•Whatarethebasecasesthatweneedtoprove?

Additionalinductionexamples

Abinarytree

•Abinarytreeisadatastructurethatisdefinedrecursively

•Abinarytreeis• Empty,or•Anodewithsomedata,andtwochildrenthatarethemselvestrees.

20

92 15

5

10

177

Calculatingthesizeofabinarytree

•Considerthefollowingproceduretocalculatethesizeofabinarytree.• Ifthetreeisnull,thenthesizeofthetreeis0.•Otherwise,thesizeofthetree=1+thesizeofitsleftchild+thesizeofitsrightchild

•Theorem6:Provethattheaboveprocedurecorrectlycomputesthenumberof(non-null)nodesofthetree.

Calculatingthesizeofabinarytree

•Weprovethisusingmathematicalinductiononthesizeofthetree.•Basecase:t isnull

• Inthiscaset containsexactly0nodes.• Inductionstep:

• Assumethealgorithmworksfortreesthataresmallerthan t.

• Becausetheleftsub-treeoft issmallerthant,the1strecursivecallscorrectlyreturnsthesizeoftheleftsub-treeoft.

Calculatingthesizeofabinarytree

•Proof(continued)• Inductionstep(continued)

• Similarlytherightsub-treeoft issmallerthant,andsothe2ndrecursivecallcorrectlyreturnsthesizeoftherightsub-treeoft.

• Butwereturn1+thesumofthevaluesreturnedbytherecursivecalls.

• Thisisexactlythesizeoft (1fortheroot,andthesumofthesizesofthetwosub-trees).

•HencebytheprincipleofM.I.,ouralgorithmcomputescorrectlythesizeofeverytree.QED

Randomized-quick-select

•Wecanfindtheith smallestelementinanunsortedlistasfollows:•Pickarandomelementx ofthelist.•Dividethelistintothreesublists:

• list-smaller:elementssmallerthanx• list-equal:elementsequaltox• list-larger:elementslargerthanx• Thensearch

• list-smallerifi ≤ lengthoflist-smaller• list-largerifi >lengthoflistsmaller+lengthoflist-equal• otherwisereturnx

Randomized-quick-select

•Thisalgorithmiscalledrandomized-quick-select.•Astudentshowstheexpected numberofstepsS(n) ofthealgorithmonalistwithn elementsis:

S(1)=4cS(2)=12cS(3)=20cS(n)≤ 2cn+S(Floor(3n/4)) when n ≥ 4

Randomized-quick-select

•Prove:foreveryn≥ 1, S(n)≤ 8cn.


Recommended