Download CS61B Lecture #16: Cost Measures and Asymptotic Analysis and more Slides Data Structures and Algorithms in PDF only on Docsity!
CS61B Lecture
Announcements:
- Homework #4 was extended to tonight.
- Auto-grader for project will run early Tuesday morning, and then not until after deadline.
- Please use bug-submit for code problems (now works!).
Readings for Today: Data Structures (Into Java), Chapter 1;
Readings for next Topics: Data Structures, Chapter 2–4, Head First Java, Chapter 16.
What Are the Questions?
- Cost is a principal concern throughout engineering:
“An engineer is someone who can do for a dime what any fool can do for a dollar.”
- Cost can mean
- Operational cost (for programs, time to run, space requirements).
- Development costs: How much engineering time? When deliv- ered?
- Costs of failure: How robust? How safe?
- Is this program fast enough? Depends on:
- For what purpose;
- What input data.
- How much space (memory, disk space)?
- Again depends on what input data.
- How will it scale, as input gets big?
Cost Measures (Time)
- Wall-clock or execution time
- You can do this at home: time java FindPrimes 1000
- Advantages: easy to measure, meaning is obvious.
- Appropriate where time is critical (real-time systems, e.g.).
- Disadvantages: applies only to specific data set, compiler, ma- chine, etc.
- Number of times certain statements are executed:
- Advantages: more general (not sensitive to speed of machine).
- Disadvantages: doesn’t tell you actual time, still applies only to specific data sets.
- Symbolic execution times:
- That is, formulas for execution times or statement counts in terms of input size.
- Advantages: applies to all inputs, makes scaling clear.
- Disadvantage: practical formula must be approximate, may tell very little about actual time.
Asymptotic Cost
- Symbolic execution time lets us see shape of the cost function.
- Since we are approximating anyway, pointless to be precise about certain things: - Behavior on small inputs: ∗ Can always pre-calculate results some results. ∗ Times for small inputs not usually important. - Constant factors (as in “off by factor of 2”): ∗ Just changing machines causes constant-factor change.
- How to abstract away from (i.e., ignore) these things?
Big Oh
- Goal: Specify bounding from above.
2 g(x)
g(x)
f (x)
M = 1
- Here, f (x) ≤ 2 g(x) as long as x > 1 ,
- So f (x) is in g’s upper-bound family, written
f (x) ∈ O(g(x)),
-... even though f (x) > g(x) everywhere.
Big Omega
- Goal: Specify bounding from below:
g(x)
- 5 g(x)
f ′(x)
M = 1
- Here, f ′(x) ≥ 12 g(x) as long as x > 1 ,
- So f ′(x) is in g’s lower-bound family, written
f ′(x) ∈ Ω(g(x)),
-... even though f (x) < g(x) everywhere.
- In fact, we also have f ′(x) ∈ O(g(x)) and f (x) ∈ Ω(g(x)) and so we can also write f (x), f ′(x) ∈ Θ(g(x)).
Why It Matters
- Computer scientists often talk as if constant factors didn’t matter at all, only the difference of Θ(N ) vs. Θ(N 2 ).
- In reality they do, but we still have a point: at some point, constants get swamped. n 16 lg n
n n n lg n n^2 n^3 2 n 2 16 1. 4 2 2 4 8 4 4 32 2 4 8 16 64 16 8 48 2. 8 8 24 64 512 256 16 64 4 16 64 256 4 , 096 65 , 636 32 80 5. 7 32 160 1024 32 , 768 4. 2 × 109 64 96 8 64 384 4 , 096 262 , 144 1. 8 × 1019 128 112 11 128 896 16 , 384 2. 1 × 109 3. 4 × 1038 ... ... ... ... ... ... ... ... 1 , 024 160 32 1 , 024 10 , 240 1. 0 × 106 1. 1 × 109 1. 8 × 10308 ... ... ... ... ... ... ... ... 220 320 1024 1. 0 × 106 2. 1 × 107 1. 1 × 1012 1. 2 × 1018 6. 7 × 10315 ,^652
Careful!
- It’s also true that the worst-case time is O(N 2 ), since N ∈ O(N 2 ) also: Big-Oh bounds are loose.
- The worst-case time is Ω(N ), since N ∈ Ω(N ), but that does not mean that the loop always takes time N , or even K · N for some K.
- Instead, we are just saying something about the function that maps N into the largest possible time required to process an array of length N.
- To say as much as possible about our worst-case time, we should try to give a Θ bound: in this case, we can: Θ(N ).
- But again, that still tells us nothing about best-case time, which happens when we find X at the beginning of the loop. Best-case time is Θ(1).
Recursion and Recurrences: Fast Growth
- Silly example of recursion:
/** True iff X is a substring of S */ boolean occurs (String S, String X) { if (S.equals (X)) return true; if (S.length () <= X.length () return false; return occurs (S.substring (1), X) || occurs (S.substring (0, S.length ()-1), X); }
- In the worst case, both recursive calls happen.
- Consider a fixed size for X, say N 0.
- Define C(N ) to be the worst-case cost of occurs(S,X) for S of length N , measured in # of calls to occurs. Then
C(N ) =
1 , if N ≤ N 0 , 2 C(N − 1) if N > N 0
- So C(N ) grows exponentially:
C(N ) = 2C(N −1) = 2· 2 C(N −2) =... = 2︸ · (^2) ︷︷ · · · 2 ︸ N −N 0
·1 = 2N^ −N^0 ∈ Θ(2N^ )
Binary Search: Slow Growth
/** True X iff is an element of S[L .. U]. Assumes
- S in ascending order, 0 <= L <= U-1 < S.length. */ boolean isIn (String X, String[] S, int L, int U) { if (L > U) return false; int M = (L+U)/2; int direct = X.compareTo (S[M]); if (direct < 0) return isIn (X, S, L, M-1); else if (direct > 0) return isIn (X, S, M+1, U); else return true; }
- Here, worst-case time, C(D), (as measured by # of string compar- isons), depends on size D = U − L + 1.
- We eliminate S[M] from consideration each time and look at half the rest. Assume D = 2k^ − 1 for simplicity, so:
C(D) =
0 , if D ≤ 0 , 1 + C((D − 1)/2), if D > 0. = 1 + 1 +︸ ︷︷... + 1︸ k
= k = dlg De ∈ Θ(lg D)
Amortization: Expanding Vectors
- When using array for expanding sequence, best to double size of array to grow it. Here’s why.
- If array is size s, doubling its size and moving s elements to the new array takes time ∝ 2 s.
- Cost of inserting N items into array, doubling size as needed, start- ing with array size 1: To Insert Resizing Cumulative Resizing Cost Array Size Item # Cost Cost per Item After Insertions 1 0 0 0 1 2 2 2 1 2 3 to 4 4 6 1.5 4 5 to 8 8 14 1.75 8 ... ... ... ... ...
2 m^ + 1 to 2 m+1^2 m+1^2 m+2^ − 2 ≈ 2 2 m+
- If we spread out ( amortize) the cost of resizing, we average about 2 time units on each item: “amortized insertion time is 2 units.”
- So even though worst-case time for adding one element to array of N elements is 2 N , time to add N elements is Θ(N ), not Θ(N 2 ).