Lecture 3 -...
Transcript of Lecture 3 -...
![Page 1: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/1.jpg)
Lecture3Big-Onotation,morerecurrences!!
![Page 2: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/2.jpg)
Announcements!
• HW1isposted!(DueFriday)
• SeePiazzaforalistofHWclarifications
• Firstrecitationsectionwasthismorning,there’sanothertomorrow(samematerial). (Theseareoptional,it’sachanceforTAstogoovermoreexamplesthanwecangettoinclass).
![Page 3: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/3.jpg)
FAQ• HowrigorousdoIneedtobeonmyhomework?• SeeourexampleHWsolutiononline
• Ingeneral,weareshootingfor:
Youshouldbeabletogiveafriendyoursolution
andtheyshouldbeabletoturnitintoarigorousproofwithoutmuchthought.
• Thisisadelicatelinetowalk,andthere’snoeasyanswer.Thinkofitlikemorelikewritingagoodessaythan“correctly”solvingamathproblem.
• What’swiththearrayboundsinpseudocode?• SORRY!I’mtryingtomatchCLRSandthiscausesmetomakemistakessometimes. Inthisclass,I’mtryingtodo:• Arraysare1-indexed
• A[1..n]isallentriesbetween1andn,inclusive
• IwillalsouseA[1:n](pythonnotation)tomeanthesamething(notpythonnotation).
• PleasecallmeoutwhenImessup.
![Page 4: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/4.jpg)
Lasttime….
• Sorting:InsertionSort andMergeSort
• Analyzingcorrectnessofiterative+recursivealgs
• Via“loopinvariant”andinduction
• Analyzingrunningtimeofrecursivealgorithms
• Bywritingoutatreeandaddingupalltheworkdone.
![Page 5: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/5.jpg)
Today
• Howdowemeasuretheruntimeofanalgorithm?
• Worst-caseanalysis
• AsymptoticAnalysis
• Recurrencerelations:
• IntegerMultiplicationandMergeSort again
• The“MasterMethod”forsolvingrecurrences.
![Page 6: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/6.jpg)
Recallfromlasttime…
• WeanalyzedINSERTIONSORTandMERGESORT.
• Theywerebothcorrect!
• INSERTIONSORTtooktimeaboutn2
• MERGESORT tooktimeaboutnlog(n).
0
200000
400000
600000
800000
1000000
0 200 400 600 800 1000
nlog(n) n^2
nlog(n)iswaybetter!!!
![Page 7: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/7.jpg)
Afewreasonstobegrumpy
• Sorting
shouldtakezerosteps…whynlog(n)??
• What’swiththisT(MERGE)<2+4n<=6n?
1 2 3 4 5 6 7 8
![Page 8: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/8.jpg)
Analysis
T(n)=T(n/2)+T(n/2)+T(MERGE)
Let’ssay
T(MERGEofsizen/2)≤ 6n
operations
Wewillseelaterhowtoanalyzerecurrencerelationslikethese
automagically…buttodaywe’lldoitfromfirstprinciples.
T(n)=timetorun
MERGESORTona
listofsizen
T(MERGEtwolistsofsizen/2)
isthetimetodo:
• 3 variableassignments(counters←1)
• ncomparisons
• nmoreassignments
• 2ncounterincrements
Sothat’s
2T(assign)+nT(compare)+
nT(assign)+2nT(increment)
or4n+2operations
Pluckythe
pedanticpenguin Luckythe
lackadaisicallemur
=2T(n/2)+6n
Thisiscalledarecurrencerelation:it
describestherunningtimeofaproblem
ofsizenintermsoftherunningtimeof
smallerproblems.
Or 4n + 3…
![Page 9: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/9.jpg)
Afewreasonstobegrumpy
• Sorting
shouldtakezerosteps…whynlog(n)??
• What’swiththisT(MERGE)<2+4n<=6n?
• The“2+4n”operationsthingdoesn’tevenmakesense.Differentoperationstakedifferentamountsoftime!
• Webounded2+4n<=6n.Iguessthat’strue,butthatseemsprettydumb.
1 2 3 4 5 6 7 8
![Page 10: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/10.jpg)
Howwewilldealwithgrumpiness
• Takeadeepbreath…
• Worstcaseanalysis
• Asymptoticnotation
![Page 11: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/11.jpg)
Worst-caseanalysis
• Inthisclass,wewillfocusonworst-caseanalysis
• Pros:verystrongguarantee
• Cons:verystrongguarantee
1 2 3 4 5 6 7 8
Sortingasortedlist
shouldbefast!!
Algorithm
designer
Algorithm:
Do the thing
Do the stuff
Return the answer
Here is my algorithm!Here is an
input!
![Page 12: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/12.jpg)
Big-Onotation
• Whatdowemeanwhenwemeasureruntime?
• Weprobablycareaboutwalltime: howlongdoesittaketosolvetheproblem,insecondsorminutesorhours?
• Thisisheavilydependentontheprogramminglanguage,architecture,etc.
• Thesethingsareveryimportant,butarenotthepointofthisclass.
• Wewantawaytotalkabouttherunningtimeofanalgorithm,independentoftheseconsiderations.
Howlongdoesan
operationtake?Whyare
webeingsosloppyabout
that“6”?
![Page 13: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/13.jpg)
Rememberthisslide?
n nlog(n) n^2
8 24 64
16 64 256
32 160 1024
64 384 4096
128 896 16384
256 2048 65536
512 4608 262144
1024 10240 1048576 0
200000
400000
600000
800000
1000000
0 200 400 600 800 1000 1200
nlog(n) n^2
![Page 14: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/14.jpg)
Changenlog(n)to5nlog(n)….
n 5nlog(n) n^2
8 120 64
16 320 256
32 800 1024
64 1920 4096
128 4480 16384
256 10240 65536
512 23040 262144
1024 51200 1048576 0
200000
400000
600000
800000
1000000
0 200 400 600 800 1000 1200
5nlog(n) n^2
![Page 15: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/15.jpg)
AsymptoticAnalysisHowdoestherunningtimescaleasngetslarge?
• Abstractsawayfromhardware- andlanguage-specificissues.
• Makesalgorithmanalysismuchmoretractable.
• Onlymakessenseifnis
large(comparedtothe
constantfactors).
Pros:Cons:
Onealgorithmis“faster”thananotherifits
runtimegrowsmore“slowly”asngetslarge.
2100000000000000 n
is“better”thann2 ?!?!
Thisisespeciallyrelevant
now,asdatagetbiggerand
biggerandbigger…
Thiswillprovideaformalwayofsaying
thatn2 is“worse”than100nlog(n).
![Page 16: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/16.jpg)
• Quickreminders:
• ∃: “Thereexists”• ∀:”Forall”• Example: ∀ studentsinCS161,∃ analgorithmsproblemthatreallyexcitesthestudent.
• Muchstrongerstatement: ∃ analgorithmsproblemsothat,∀ studentsinCS161,thestudentisexcitedbytheproblem.
• We’regoingtoformallydefineanupperbound:
• “T(n)growsnofasterthanf(n)”
Nowforsomedefinitions…
![Page 17: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/17.jpg)
O(…)meansanupperbound
• LetT(n),f(n)befunctionsofpositiveintegers.• ThinkofT(n)asbeingaruntime:positiveandincreasinginn.
• Wesay“T(n)isO(f(n))”iff(n)growsatleastasfastasT(n)asngetslarge.
• Formally,
� � = � � � ⟺
∃�, �. > 0�. �. ∀� ≥ �.,0 ≤ � � ≤ � ⋅ �(�)
pronounced“big-ohof…”orsometimes“ohof…”
![Page 18: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/18.jpg)
Parsingthat…
� � = � � � ⟺
∃�, �. > 0�. �. ∀� ≥ �.,0 ≤ � � ≤ � ⋅ �(�)
T(n)
f(n)
cf(n)
n0n
T(n)=O(f(n))means:
Eventually,(forlargeenoughn)
somethingthatgrowslikef(n)
isalwaysbiggerthanT(n).
![Page 19: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/19.jpg)
Example1
• T(n)=n,f(n)=n2.
• T(n)=O(f(n))f(n)
n
T(n)
c=1
n0 =1
� � = � � � ⟺
∃�, �. > 0�. �. ∀� ≥ �.,0 ≤ � � ≤ � ⋅ �(�)
(formalproof
onboard)
cf(n)=
![Page 20: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/20.jpg)
Examples2and3
• AlldegreekpolynomialswithpositiveleadingcoefficientsareO(nk).
• Foranyk≥ 1,nk isnot O(nk-1).
(Ontheboard)
![Page 21: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/21.jpg)
Take-awayfromexamples
• ToproveT(n)=O(f(n)),youhavetocomeupwithcandn0sothatthedefinitionissatisfied.
• ToproveT(n)isNOT O(f(n)),onewayisbycontradiction:
• Supposethatsomeonegivesyouacandann0 sothatthedefinitionissatisfied.
• Showthatthissomeonemustbylyingtoyoubyderivingacontradiction.
![Page 22: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/22.jpg)
O(…)meansanupperbound,and
Ω(…)meansalowerbound
• Wesay“T(n)isΩ(f(n))”iff(n)growsatmostasfastasT(n)asngetslarge.
• Formally,
� � = Ω � � ⟺
∃�, �. > 0�. �. ∀� ≥ �.,0 ≤ � ⋅ � � ≤ � �
Switchedthese!!
![Page 23: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/23.jpg)
Parsingthat…
� � = Ω � � ⟺
∃�, �. > 0�. �. ∀� ≥ �.,0 ≤ � ⋅ � � ≤ � �
T(n)
cf(n)
f(n)
n0n
![Page 24: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/24.jpg)
Θ(…)meansboth!
•Wesay“T(n)isΘ(f(n))”if:
T(n)=O(f(n))
-AND-
T(n)=Ω(f(n))
![Page 25: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/25.jpg)
Yetmoreexamples
• n3 – n2+3n=O(n3)
• n3 – n2+3n=Ω(n3)
• n3 – n2+3n=Θ(n3)
• 3n isnot O(2n)
• nlog(n)=Ω(n)
• nlog(n)isnot Θ(n). Funexercise:
checkallofthese
carefully!!
![Page 26: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/26.jpg)
We’llbeusinglotsofasymptoticnotationfromhereonout
• ThismakesbothPluckyandLuckyhappy.• PluckythePedanticPenguinishappybecausethere
isaprecisedefinition.
• LuckytheLackadaisicalLemurishappybecausewe
don’thavetopaycloseattentiontoallthosepesky
constantfactorslike“4”or“6”.
• Butweshouldalwaysbecarefulnottoabuseit.
• Inthecourse,(almost)everyalgorithmwesee
willbeactuallypractical,withoutneedingto
take� ≥ �. = 2:........
Thisismy
happyface!
Questionsaboutasymptoticnotation?
![Page 27: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/27.jpg)
Backtorecurrencerelations
We’veseenthreerecursivealgorithmssofar.
• Needlesslyrecursiveintegermultiplication
• T(n)=4T(n/2)+O(n)
• T(n)=O(n2)
• Karatsubaintegermultiplication
• T(n)=3T(n/2)+O(n)
• T(n)=O(nlog_2(3) ≈ n1.6)
• MergeSort
• T(n)=2T(n/2)+O(n)
• T(n)=O(nlog(n))
T(n)=timetosolveaproblemofsizen.
What’sthepattern?!?!?!?!
(Remindersonboard)
![Page 28: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/28.jpg)
Themastertheorem
• Aformula thatsolvesrecurrenceswhenallofthesub-problemsarethesamesize.
• (We’llseeanexampleWednesdaywhennotallproblemsarethesamesize).
JedimasterYoda
A useful
formula it is.
Know why it works
you should.
![Page 29: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/29.jpg)
Themastertheorem
• Suppose� � = � ⋅ � => + � �@ .Then
Many symbols
those are….
Threeparameters:
a:numberofsubproblems
b:factorbywhichinputsizeshrinks
d:needtodond worktocreateallthe
subproblems andcombinetheirsolutions.
![Page 30: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/30.jpg)
Examples(detailsonboard)
• Needlesslyrecursiveintegermult.
• T(n)=4T(n/2)+O(n)
• T(n)=O(n2)
• Karatsubaintegermultiplication
• T(n)=3T(n/2)+O(n)
• T(n)=O(nlog_2(3) ≈ n1.6)
• MergeSort
• T(n)=2T(n/2)+O(n)
• T(n)=O(nlog(n))
� � = � ⋅ � => + � �@ .
a=4
b=2
d=1
a=3
b=2
d=1
a=2
b=2
d=1
a>bd
a>bd
a=bd
✓
✓
✓
![Page 31: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/31.jpg)
Proofofthemastertheorem
• We’lldothesamerecursiontreethingwedidforMergeSort,butbemorecareful.
• Supposethat � � = � ⋅ � => + � ⋅ �@.
Pluckythe
PedanticPenguin
Hangon!ThehypothesisoftheMasterTheoremwas
thetheextraworkateachlevelwasO(nd).That’sNOT
thesameaswork<=cn forsomeconstantc.
Luckythe
lackadaisicallemur
That’strue… we’llactuallyproveaweaker
statementthatusesthishypothesisinsteadof
thehypothesisthat� � = � ⋅ � => + � �@ .
It’sagoodexercisetrytomakethisproofwork
rigorouslywiththeO()notation.
![Page 32: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/32.jpg)
Recursiontree
Sizen
n/bn/b
(Size1)
…
n/b2
n/btn/btn/btn/btn/btn/bt
…
Level
Amountof
workatthis
level
0
#
problems
1
2
t
logb(n)
1
a
a2
at
alog_b(n)
Sizeof
each
problem
n
n/b
n/b2
n/bt
1
…
n/b
n/b2
n/b2n/b2
n/b2
n/b2
n/b2
� � = � ⋅ � �� + � ⋅ �@
� ⋅ ��
�C� ��C
@
�� ��@
�D� ��D
@
�EFG_>(=)�
![Page 33: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/33.jpg)
Recursiontree
Sizen
n/bn/b
(Size1)
…
n/b2
n/btn/btn/btn/btn/btn/bt
…
Level
Amountof
workatthis
level
0
#
problems
1
2
t
logb(n)
1
a
a2
at
alog_b(n)
Sizeof
each
problem
n
n/b
n/b2
n/bt
1
…
n/b
n/b2
n/b2n/b2
n/b2
n/b2
n/b2
� � = � ⋅ � �� + � ⋅ �@
� ⋅ ��
�C� ��C
@
�� ��@
�D� ��D
@
�EFG_>(=)�
Totalwork(derivationonboard)isatmost:
� ⋅ �@ ⋅ I ��@
DJKLM(=)
DN.
![Page 34: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/34.jpg)
Nowlet’scheckallthecases(onboard)
![Page 35: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/35.jpg)
Evenmoregenerally,forT(n)=aT(n/b)+f(n)…
![Page 36: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/36.jpg)
Recap
• O()notationmakesourliveseasier.
• The”MasterMethod”alsomakeourliveseasier.
Nexttime:
• Whatifthesub-
problemsare
differentsizes?
• Andwhenmight
thathappen?
![Page 37: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/37.jpg)
Extraslides…
![Page 38: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/38.jpg)
Somebrainteasers
• Aretherefunctionsf,gsothatNEITHER f=O(g)norf=Ω(g)?
• Aretherenon-decreasing functionsf,gsothattheaboveistrue?
• Definethen’th fibonacci numberbyF(0)=1,F(1)=1,F(n)=F(n-1)+F(n-2)forn>2.
• 1,1,2,3,5,8,13,21,34,55,…
Trueorfalse:
• F(n)=O(2n)
• F(n)=Ω(2n)
![Page 39: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/39.jpg)
AfewmoreO()examples
![Page 40: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/40.jpg)
ExampleA
• g(n)=2,f(n)=1.
• g(n)=O(f(n))(andalsof(n)=O(g(n)))
� � = � � � ⟺
∃�, �. > 0�. �. ∀� ≥ �.,0 ≤ � � ≤ � ⋅ �(�)
f(n)
n0=1n
g(n)
2.1⋅ f(n)
1
2
2.1
![Page 41: Lecture 3 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs161/cs161.1176/Slides/Lecture3... · Jedi master Yoda A useful formula it is. Know why it works you should. The master](https://reader031.fdocuments.in/reader031/viewer/2022022006/5abfb31d7f8b9a7e418e7cd1/html5/thumbnails/41.jpg)
ExampleB
• f(n)=1,g(n)asbelow.
• g(n)=O(f(n))(andalsof(n)=O(g(n)))
� � = � � � ⟺
∃�, �. > 0�. �. ∀� ≥ �.,0 ≤ � � ≤ � ⋅ �(�)
f(n)
n
g(n)
2.1⋅ f(n)
1
2
2.1
n0