CSE 373: Data Structures and...
Transcript of CSE 373: Data Structures and...
![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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/1.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/2.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/3.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/4.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/5.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/6.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/7.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/8.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/9.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/10.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/11.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/12.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/13.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/14.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/15.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/16.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/17.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/18.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/19.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/20.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/21.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/22.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/23.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/24.jpg)
(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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/25.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/26.jpg)
(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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/27.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/28.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/29.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/30.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/31.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/32.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/33.jpg)
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”](https://reader035.fdocuments.in/reader035/viewer/2022063009/5fbf991b2ba2d114562bc261/html5/thumbnails/34.jpg)
(continued)