CSE 373: Data Structures and Algorithms...Instructor: Lilian de Greef Quarter: Summer 2017 CSE 373:...

28
Instructor: Lilian de Greef Quarter: Summer 2017 CSE 373: Data Structures and Algorithms Lecture 5: Finishing up Asymptotic Analysis Big-O, Big-Ω, Big-θ, little-o, little-ω & Amortized Analysis

Transcript of CSE 373: Data Structures and Algorithms...Instructor: Lilian de Greef Quarter: Summer 2017 CSE 373:...

Instructor:LiliandeGreefQuarter:Summer2017

CSE373:DataStructuresandAlgorithmsLecture5:FinishingupAsymptoticAnalysis

Big-O,Big-Ω,Big-θ,little-o,little-ω &AmortizedAnalysis

Today:

•Announcements•Big-OandCousins• Big-Omega• Big-Theta• little-o• little-omega

•Averagerunningtime:AmortizedAnalysis

NewsaboutSections

Updatedtimes:• 10:50 – 11:50am• 12:00– 1:00pm

Biggerroom!• 10:50amsectionnowinTHO101

Whichsectiontoattend:• Lastweek,sectionsizeswereunbalanced(~40vs~10people)• Ifyoucan,Iencourageyoutochoosethe12:00sectiontorebalancesizes• Helpsthe12:00TA’sfeellesslonely• Moreimportantly:improvesTA:student ratioinsections(betterfortailoringsectiontoyourneeds)

Homework1

• Duetodayat5:00pm!

• Anoteaboutgradingmethods:• Beforewegrade,we’llrunascriptonyourcodetoreplaceyournamewith### anonymized ### sowewon’tknowwhoyouareaswegradeit(toaddressunconsciousbias).

• It’sstillgoodpracticetohaveyournameandcontactinfointhecomments!

Homework2

• Writtenhomeworkaboutasymptoticanalysis(noJavathistime)• Willbeoutthisevening• DueThursday,July6th at5:00pm• BecauseJuly4th isaholiday

• Anoteforhelponhomework:• Notethatholidaysmeansfewerofficehours• Remember:althoughyoucannotsharesolutions,youcantalktoclassmatesaboutconceptsorworkthroughnon-homeworkexamples(e.g.fromsection)together.• Givetheseclassmatescredit,writetheirnamesatthetopofyourhomework.

FormalDefinitionofBig-ODefinition:f(n)isinO(g(n)) ifthereexistconstants

c andn0 suchthatf(n)£ c g(n)foralln ³ n0

MorePracticewiththeDefinitionofBig-O

Definition:f(n)isinO(g(n)) ifthereexistconstantsc andn0 suchthatf(n)£ c g(n)foralln ³ n0

Leta(n)=10n+3n2 andb(n)=n2

Whataresomevaluesofcandn0wecanusetoshowa(n)∈O(b(n))?

ConstantsandLowerOrderTerms

• Theconstantmultiplierc iswhatallowsfunctionsthatdifferonlyintheirlargestcoefficienttohavethesameasymptoticcomplexityExample:

• Eliminatelower-ordertermsbecause

• Eliminatecoefficientsbecause• 3n2vs5n2ismeaninglesswithoutthecostofconstant-timeoperations• Canalwaysre-scaleanyways• Donotignoreconstantsthatarenotmultipliers!n3isnotO(n2),3n isnotO(2n)

Analyzing“Worst-Case”CheatSheetBasicoperationstake“someamountof”constanttime

• Arithmetic(fixed-width)• Assignment• AccessoneJavafieldorarrayindex• etc.

(Thisisanapproximation ofreality:averyuseful“lie”)

Control Flow TimeRequiredConsecutivestatements SumoftimeofstatementConditionals Time oftestplusslowerbranchLoops Sum ofiterations*timeofbodyMethodcalls Timeofcall’sbodyRecursion Solverecurrencerelation

Big-O & Big-OmegaBig-O:f(n)isinO(g(n)) ifthereexistconstantsc andn0 suchthatf(n) c g(n)foralln ³ n0

Big-Ω:f(n)isinΩ(g(n)) ifthereexistconstantsc andn0 suchthatf(n) c g(n)foralln ³ n0

Big-Theta

Big-θ:f(n)isinθ(g(n)) iff(n)isinbothO(g(n))and Ω(g(n))

little-o& little-omegalittle-o:f(n)isino(g(n)) ifconstantsc >0thereexistsann0s.t. f(n) c g(n)foralln ³ n0

little-ω:f(n)isinω(g(n)) ifconstantsc >0thereexistsann0s.t. f(n) c g(n)foralln ³ n0

PracticeTime!

Letf(n)=75n3 +2andg(n)=n3 +6n +2n2Thenf(n)isin… (chooseallthatapply)

A. Big-O(g)B. Big-Ω(g)C. θ(g)D. little-o(g)E. little-ω(g)

SecondPracticeTime!

Letf(n)=3n andg(n)=n3Thenf(n)isin… (chooseallthatapply)

A. Big-O(g)B. Big-Ω(g)C. θ(g)D. little-o(g)E. little-ω(g)

Big-O,Big-Omega,Big-Theta

• Whichoneismoreusefultodescribeasymptoticbehavior?

• AcommonerroristosayO(f(n))whenyoumeanθ(f(n))• AlinearalgorithmisinbothO(n)andO(n5)• Bettertosayitisθ(n)• Thatmeansthatitisnot,forexampleO(logn)

CommentsonAsymptoticAnalysis

• IschoosingthelowestBig-OorBig-Thetathebestwaytochoosethefastestalgorithm?

• Big-Ocanuseothervariables(e.g.cansumalloftheelementsofann-by-mmatrixinO(nm))

AmortizedAnalysisHowwecalculatetheaveragetime!

CaseStudy:theArrayStack

What’stheworst-case runningtimeofpush()?

What’stheaverage runningtimeofpush()?

Calculatingtheaverage:notbasedoffofrunningasingleoperation,butrunningmanyoperationsinsequence.

Technique:AmortizedAnalysis

AmortizedCost

Theamortizedcostofn operationsistheworst-casetotalcostoftheoperationsdividedbyn.

AmortizedCost

Theamortizedcostofn operationsistheworst-casetotalcostoftheoperationsdividedbyn.

Practice:• n operationstakingO(n)→ amortizedcost=

• n operationstakingO(n3)→ amortizedcost=

• n operationstakingO(nf(n))→ amortizedcost=

Example:ArrayStack

What’stheamortizedcostofcallingpush() n timesifwedoublethearraysizewhenit’sfull?

Theamortizedcostofn operationsistheworst-casetotalcostoftheoperationsdividedbyn.

Use$2foreachpush:$1tocomputer,$1tobank

AnotherPerspective:PayingandSaving“Currency”

PotentialFunction

Spendoursavingsinthebanktoresize.Thatwayitonlycosts$1extratopush(E)!

1operationcosts1$tothecomputer

Example#2:QueuemadeofStacks

in out

Examplewalk-through:• enqueue A• enqueue B• enqueue C• dequeue• enqueue D• enqueue E• dequeue• dequeue• dequeue

AsneakywaytoimplementQueueusingtwoStacks

Example#2:QueuemadeofStacksAsneakywaytoimplementQueueusingtwoStacks

in out

class Queue<E> Stack<E> in = new Stack<E>();Stack<E> out = new Stack<E>();void enqueue(E x) in.push(x); E dequeue()

if(out.isEmpty()) while(!in.isEmpty())

out.push(in.pop());

return out.pop();

Wouldn’titbenicetohaveaqueueoft-shirtstowearinsteadofastack(likeinyourdresser)?Sohavetwostacks• in:stackoft-shirtsgoafteryouwashthem• out:stackoft-shirtstowear• ifout isempty,reversein intoout

Example#2:QueuemadeofStacks(Analysis)class Queue<E> Stack<E> in = new Stack<E>();

Stack<E> out = new Stack<E>();void enqueue(E x) in.push(x);

E dequeue()

if(out.isEmpty())

while(!in.isEmpty())

out.push(in.pop());

return out.pop();

in out

Assumestackoperationsare(amortized)O(1).What’stheworst-casefordequeue()?

Whatoperationsdidweneedtodotoreachthatcondition(startingwithanemptyQueue)?

Hence,whatistheamortizedcost?

Sotheaveragetimefordequeue() is:

Example#2:Using“Currency”Analogy

in out

Examplewalk-through:• enqueue A• enqueue B• enqueue C• enqueue D• enqueue E• dequeue

“Spend”2coinsforeveryenqueue – 1tothe“computer”,1tothe“bank”.

PotentialFunction

Example#3:(Parody/JokeExample)

Lecturesare1hourlong,3timesperweek,soI’msupposedtolecturefor27hoursthisquarter.IfIendthefirst26lectures5minutesearly,thenI’dhave“savedup”130minutesworthofextralecturetime.ThenIcouldspenditallonthelastlectureandcankeepyouherefor3hours(bwahahahaha)!(Afterall,eachlecturewouldstillbe1houramortizedtime)

WrappingupAmortizedAnalysis

• Inwhatcasesdowecaremoreabouttheaverage/amortizedruntime?

• Inwhatcasesdowecaremoreabouttheworst-caseruntime?