cse 373: data structures and...

34
Instructor: Lilian de Greef Quarter: Summer 2017 CSE 373: Data Structures and Algorithms Lecture 3: Asymptotic Analysis part 2 Math Review, Inductive Proofs, Recursive Functions

Upload: others

Post on 17-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

Instructor:LiliandeGreefQuarter:Summer2017

CSE373:DataStructuresandAlgorithmsLecture3:AsymptoticAnalysispart2

MathReview,InductiveProofs,RecursiveFunctions

Page 2: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

Today:

•BriefMathReview(reviewmostlyonyourown)

•ContinueasymptoticanalysiswithBig-O•ProofbyInduction•RecursiveFunctions

Page 3: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

CommonBig-ONames

O(1) constant(sameasO(k)forconstantk)O(log n) logarithmicO(n) linearO(nlog n)“nlog n”O(n2) quadraticO(n3) cubicO(nk) polynomial(whereisk isanyconstant)O(kn) exponential(wherek isanyconstant>1)

Page 4: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

0

5

10

15

1 2 3 4 5

AFewCommonBig-O's

O(1)

O(n) O(n^2) O(logn)

O(2^n) O(nlogn)

Page 5: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

0

100

200

300

400

500

600

1 2 3 4 5 6 7 8 9 10

AFewCommonBig-O's

O(1)

O(n) O(n^2) O(logn)

O(2^n) O(nlogn)

Page 6: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

0

1E+29

2E+29

3E+29

4E+29

5E+29

6E+29

7E+29

1 11 21 31 41 51 61 71 81 91

AFewCommonBig-O's

O(1)

O(n) O(n^2) O(logn)

O(2^n) O(nlogn)

Page 7: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

Powersof2:FunFacts

• Abitis0or1(justtwodifferent“letters”or“symbols”)• Asequenceofn bitscanrepresent2n distinctthings

(Forexample,thenumbers1through2n)• 210 is1024(“aboutathousand”,kiloinCSEspeak)• 220 is“aboutamillion”,megainCSEspeak• 230 is“aboutabillion”,giga inCSEspeak

Java:anint is32bitsandsigned,so“maxint”is“about2billion”along is64bitsandsigned,so“maxlong”is263-1

Page 8: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

Whichmeans…

Youcouldgiveauniqueidto…• EverypersonintheU.S.with29bits• Everypersonintheworldwith33bits• Everypersontohaveeverlivedwith38bits(estimate)• Everyatomintheuniversewith250-300bits

Soifapasswordis128bitslongandrandomlygenerated,doyouthinkyoucouldguessit?

Page 9: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

MathReview:Logs&Exponents(Interlude#2fromBig-O)

Page 10: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

Logs&Exponents

Definition: 𝑙𝑜𝑔$𝑥 = 𝑦 if 𝑎) = 𝑥

• 𝑙𝑜𝑔*32 =

• 𝑙𝑜𝑔-.10,000 =

Page 11: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

0

100

200

300

400

500

600

1 2 3 4 5 6 7 8 9 10

AFewCommonBig-O's

O(1)

O(n) O(n^2) O(logn)

O(2^n) O(nlogn)

Page 12: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

0

0.5

1

1.5

2

2.5

3

3.5

1 101 201 301 401 501 601 701 801 901 1001

O(logn)

Page 13: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

Logs&Exponents

Definition: 𝑙𝑜𝑔$𝑥 = 𝑦 if 𝑎) = 𝑥

• 𝑙𝑜𝑔*32 =

• 𝑙𝑜𝑔-.10,000 =

OutsideofCSE,𝑙𝑜𝑔(𝑥) isoftenshort-handforInCSE,𝑙𝑜𝑔(𝑥) isoftenshort-handfor

…but,doesitmatter?

Page 14: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

CanMakealog2 OutofAnylog!

𝑙𝑜𝑔4𝑥 =𝑙𝑜𝑔5(𝑥)𝑙𝑜𝑔5(𝐴)

so

𝑙𝑜𝑔*𝑥 =𝑙𝑜𝑔78$9:;:<(𝑥)𝑙𝑜𝑔78$9:;:<(2)

Page 15: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

OtherPropertiesofLogarithms

• 𝑙𝑜𝑔 𝐴 ∗ 𝐵 = 𝑙𝑜𝑔𝐴 + 𝑙𝑜𝑔𝐵• So 𝑙𝑜𝑔 𝑁A = 𝑘 ∗ 𝑙𝑜𝑔𝑁

• 𝑙𝑜𝑔 𝐴/𝐵 = 𝑙𝑜𝑔𝐴 − 𝑙𝑜𝑔𝐵

• 𝑙𝑜𝑔 𝑙𝑜𝑔𝑥 = 𝑙𝑜𝑔 𝑙𝑜𝑔𝑥• Growsasslowlyas22 growsquickly

• 𝑙𝑜𝑔 𝑥 𝑙𝑜𝑔 𝑥 iswritten𝑙𝑜𝑔* 𝑥• Itisgreaterthan𝑙𝑜𝑔 𝑥 forall𝑥 > 2• Itisnotthesameas𝑙𝑜𝑔 𝑙𝑜𝑔𝑥

(toreviewonyourowntime)

Page 16: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

FloorandCeiling

ë ûX

é ùX

Floor function: the largest integer < X

Ceiling function: the smallest integer > X

ë û ë û ë û 2232.722.7 =-=-=

é ù é ù é ù 2222.332.3 =-=-=

(toreviewonyourowntime)

Page 17: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

FloorandCeilingProperties

ë ûé ù

ë û é ù integer an is n ifnn/2n/23.1XXX2.

XX1X1.

=++<££<-

(toreviewonyourowntime)

Page 18: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

BacktoBig-O

Page 19: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

What’stheasymptoticruntimeofthis(semi-)pseudocode?x := 0;for i=1 to N dofor j=1 to i do

x := x + 3;return x;

A. O(n)B. O(n2)C. O(n+n/2)D. Noneoftheabove

Howdoweprovetherightanswer?ProofbyInduction!

Page 20: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

InductiveProofs(InterludefromAsymptoticAnalysis)

Page 21: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

StepstoInductiveProof

1. Ifnotgiven,definen (or“x”or“t”orwhateverletteryouuse)

2. BaseCase3. InductiveHypothesis(IHOP):

Assumewhatyouwanttoproveistrueforsomearbitraryvaluek(or“p”or“d”orwhateverletteryouchoose)

4. InductiveStep:UsetheIHOP(andmaybebasecase)toproveit'strueforn=k+1

Page 22: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

Example#0:ProofthatIcanclimbanylengthladder1. Letn=numberofrungsonaladder.2. BaseCase:forn=13. InductiveHypothesis(IHOP):

Assumetrueforsomearbitraryintegern=k.4. InductiveStep:(aimingtoproveit'strueforn=k+1)

• ByIHOP,Icanclimbkstepsoftheladder.• IfI’veclimbedthatfar,Icanalwaysclimbonemore.• SoIcanclimbk+1steps.• Icanclimbforever!

Page 23: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

Example#1Provethatthenumberofloopiterationsis

x := 0;for i=1 to N dofor j=1 to i do

x := x + 3;return x;

𝑛 ∗ (𝑛 + 1)2

Page 24: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

(Extraroomfornotes)

Page 25: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

Example#2:Provethat1+2+4+8+…+2n =2n+1 - 1

Page 26: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

(Extraroomfornotes)

Page 27: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

UsefulMathematicalProperty!

You’lluseitorseeitagainbeforetheendofCSE373.

H2I =J

IK.

2JL- − 1

Page 28: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

Example#3:(Parody)ReverseInduction!ProofbyReverseInductionThatYouCanAlwaysCageaLion:

Letn=numberoflionsBaseCase:Thereexistssomecountable,arbitrarilylargevalueofMsuchthatwhenn=M,thelionsaresopackedtogetherthatit'strivialtocageone.IHOP:Assumethisisalsotrueforn=kforsomearbitraryvaluek.InductiveStep:Thenforn=k-1,releasealiontoreducetheproblemtothecaseofn=k,whichbytheIHOPistrue.QED:)

Funfact:Reverseinductionisathing!Themathpartoftheaboveisactuallycorrect.

Page 29: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

Big-O:RecursiveFunctionsHowdoweasymptoticallyanalyzerecursivefunctions?

Page 30: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

Example#1:TowersofHanoi

Page 31: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

Example#1:TowersofHanoi

// Prints instructions for moving disks from one // pole to another, where the three poles are // labeled with integers "from", "to", and "other". // Code from rosettacode.orgpublic void move(int n, int from, int to, int other) {

if (n == 1) {System.out.println("Move disk from pole " + from +

" to pole " + to);} else {

move(n - 1, from, other, to); move(1, from, to, other); move(n - 1, other, to, from);

} }

Page 32: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

Example#1:TowersofHanoi

if (n == 1) {System.out.println("Move disk from pole " + from +

" to pole " + to);}

else { move(n - 1, from, other, to); move(1, from, to, other); move(n - 1, other, to, from);

}

Page 33: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

Example#1:SolvingtheRecurrenceRelation

RecurrenceRelation:

Page 34: CSE 373: Data Structures and Algorithmscourses.cs.washington.edu/courses/cse373/17su/lectures/Lecture 03... · Powers of 2: Fun Facts •A bit is 0 or 1 (just two different “letters”

(continued)