cse 373: data structures and...
TRANSCRIPT
Instructor:LiliandeGreefQuarter:Summer2017
CSE373:DataStructuresandAlgorithmsLecture3:AsymptoticAnalysispart2
MathReview,InductiveProofs,RecursiveFunctions
Today:
•BriefMathReview(reviewmostlyonyourown)
•ContinueasymptoticanalysiswithBig-O•ProofbyInduction•RecursiveFunctions
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)
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)
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)
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)
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
Whichmeans…
Youcouldgiveauniqueidto…• EverypersonintheU.S.with29bits• Everypersonintheworldwith33bits• Everypersontohaveeverlivedwith38bits(estimate)• Everyatomintheuniversewith250-300bits
Soifapasswordis128bitslongandrandomlygenerated,doyouthinkyoucouldguessit?
MathReview:Logs&Exponents(Interlude#2fromBig-O)
Logs&Exponents
Definition: 𝑙𝑜𝑔$𝑥 = 𝑦 if 𝑎) = 𝑥
• 𝑙𝑜𝑔*32 =
• 𝑙𝑜𝑔-.10,000 =
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)
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)
Logs&Exponents
Definition: 𝑙𝑜𝑔$𝑥 = 𝑦 if 𝑎) = 𝑥
• 𝑙𝑜𝑔*32 =
• 𝑙𝑜𝑔-.10,000 =
OutsideofCSE,𝑙𝑜𝑔(𝑥) isoftenshort-handforInCSE,𝑙𝑜𝑔(𝑥) isoftenshort-handfor
…but,doesitmatter?
CanMakealog2 OutofAnylog!
𝑙𝑜𝑔4𝑥 =𝑙𝑜𝑔5(𝑥)𝑙𝑜𝑔5(𝐴)
so
𝑙𝑜𝑔*𝑥 =𝑙𝑜𝑔78$9:;:<(𝑥)𝑙𝑜𝑔78$9:;:<(2)
OtherPropertiesofLogarithms
• 𝑙𝑜𝑔 𝐴 ∗ 𝐵 = 𝑙𝑜𝑔𝐴 + 𝑙𝑜𝑔𝐵• So 𝑙𝑜𝑔 𝑁A = 𝑘 ∗ 𝑙𝑜𝑔𝑁
• 𝑙𝑜𝑔 𝐴/𝐵 = 𝑙𝑜𝑔𝐴 − 𝑙𝑜𝑔𝐵
• 𝑙𝑜𝑔 𝑙𝑜𝑔𝑥 = 𝑙𝑜𝑔 𝑙𝑜𝑔𝑥• Growsasslowlyas22 growsquickly
• 𝑙𝑜𝑔 𝑥 𝑙𝑜𝑔 𝑥 iswritten𝑙𝑜𝑔* 𝑥• Itisgreaterthan𝑙𝑜𝑔 𝑥 forall𝑥 > 2• Itisnotthesameas𝑙𝑜𝑔 𝑙𝑜𝑔𝑥
(toreviewonyourowntime)
FloorandCeiling
ë ûX
é ùX
Floor function: the largest integer < X
Ceiling function: the smallest integer > X
ë û ë û ë û 2232.722.7 =-=-=
é ù é ù é ù 2222.332.3 =-=-=
(toreviewonyourowntime)
FloorandCeilingProperties
ë ûé ù
ë û é ù integer an is n ifnn/2n/23.1XXX2.
XX1X1.
=++<££<-
(toreviewonyourowntime)
BacktoBig-O
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!
InductiveProofs(InterludefromAsymptoticAnalysis)
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
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!
Example#1Provethatthenumberofloopiterationsis
x := 0;for i=1 to N dofor j=1 to i do
x := x + 3;return x;
𝑛 ∗ (𝑛 + 1)2
(Extraroomfornotes)
Example#2:Provethat1+2+4+8+…+2n =2n+1 - 1
(Extraroomfornotes)
UsefulMathematicalProperty!
You’lluseitorseeitagainbeforetheendofCSE373.
H2I =J
IK.
2JL- − 1
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.
Big-O:RecursiveFunctionsHowdoweasymptoticallyanalyzerecursivefunctions?
Example#1:TowersofHanoi
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);
} }
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);
}
Example#1:SolvingtheRecurrenceRelation
RecurrenceRelation:
(continued)