Lecture 2
Lecture 2
Algorithmic, Asymptotic,
and Amortized Analysis
Kate Deibel
Summer 2012
MATH REVIEW
Inductive case:
Assume: sum of the first k powers of 2 is 2k-1
Show: sum of the first (k+1) powers is 2k+1-1
P(k+1) = 20+21+…+2k+1-2+2k+1-1
= (20+21+…+2k-1)+2k
= (2k-1)+2k since P(k)=20+21+…+2k-1= 2k-1
= 2∙2k-1
= 2k+1-1
Rules of Thumb:
210 is 1024 / “about a thousand”, kilo in CSE speak
220 is “about a million”, mega in CSE speak
230 is “about a billion”, giga in CSE speak
In Java:
int is 32 bits and signed, so “max int” is 231 - 1
which is about 2 billion
long is 64 bits and signed, so “max long” is 263 - 1
log(Nk)= k log N
In particular,
log2 x = 3.22 log10 x
In general,
logB x = (logA x) / (logA B)
ALGORITHM ANALYSIS
Evaluating an algorithm?
Use asymptotic analysis
Evaluating an implementation?
Use timing
True or false?
1. 4+3n is O(n) True
2. n+2 logn is O(log n) False
3. logn+2 is O(1) False
4. n50 is O(1.1n) True
Example:
My array is too small. Let's enlarge it.
𝑛
cost of calling put for the ith time
𝑖=1
= O(?)
𝑛