Announcements
Upcoming Deadlines:
Lecture today/friday somewhat more slowly paced (given project).
See study guides for each lecture starting Monday:
CS61B
Lecture 18: Asymptotics II: Analysis of Algorithms
Summary of Asymptotic Notations
| Informal meaning: | Family | Family Members |
Big Theta Θ(f(N)) | Order of growth is f(N). | Θ(N2) | N2/2 2N2 N2 + 38N + N |
Big O O(f(N)) | Order of growth is less than or equal to f(N). | O(N2) | N2/2 2N2 lg(N) |
Big Omega Ω(f(N)) | Order of growth is greater than or equal to f(N). | Ω(N2) | N2/2 2N2 eN |
From discussion
Example 1/2:For Loops
Monday’s Lecture
We discussed ways to analyze algorithm performance. To understand how code scales, we symbolically count number of executions of a representative operation as a function of input size N.
int count = 0, N = a.length;
for (int i = 0; i < N; i++) {
if (a[i] == k) {
count += 1;
}
}
a[k] += count;
Big Theta formalizes our intuitive simplifications.
operation | count | simplified count |
increment | N+1 to 2N+1 | Θ(N) |
Example 1: Nested For Loops
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)) in the worst case.
0
1
2
3
4
5
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
i
j
0 1 2 3 4 5
Example 1: Nested For Loops
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)) in the worst case.
Worst case number of j += 1 calls:
1 + 2 + 3 + … + (N - 3) + (N - 2) + (N - 1)
(N-1)/2 of these combinations
Overall worst case runtime: Θ(N2)
0
1
2
3
4
5
0 1 2 3 4 5
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
i
j
1 + (N-1) = N
N
N
= N(N-1)/2
Example 1: Nested For Loops
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)) in the worst case.
Worst case number of j += 1 calls:
1 + 2 + 3 + … + (N - 3) + (N - 2) + (N - 1)
Overall worst case runtime: Θ(N2)
0
1
2
3
4
5
0 1 2 3 4 5
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
i
j
= N(N-1)/2
Overall worst case runtime: Θ(N2)
Nested For Loops II: PollEv.com/jhug or JHUG to 37607
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)). By simple, we mean there should be no unnecessary multiplicative constants or additive terms.
D. N log N
E. N2
F. Something else
Nested For Loops II: PollEv.com/jhug or JHUG to 37607
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
Cost model, println(“hello”) calls:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
0
1
2
3
4
5
0 1 2 3 4 5
j
i
n=1
Nested For Loops II: PollEv.com/jhug or JHUG to 37607
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
Cost model, println(“hello”) calls:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
0
1
2
3
4
5
0 1 2 3 4 5
j
i
n=1
Nested For Loops II: PollEv.com/jhug or JHUG to 37607
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
Cost model, println(“hello”) calls:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
0
1
2
3
4
5
0 1 2 3 4 5
j
i
n=1
n=2
,3
Nested For Loops II: PollEv.com/jhug or JHUG to 37607
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
Cost model, println(“hello”) calls:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
0
1
2
3
4
5
0 1 2 3 4 5
j
i
n=1
n=2
,3
n=4
,5,6,7
Nested For Loops II: PollEv.com/jhug or JHUG to 37607
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
Cost model, println(“hello”) calls:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
0
1
2
3
4
5
0 1 2 3 4 5
j
i
n=1
n=2
,3
n=4
,5,6,7
n=8,9,10, …, 15
Nested For Loops II: PollEv.com/jhug or JHUG to 37607
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
Cost model, println(“hello”) calls:
“Worst case” here is irrelevant, all cases the same.
Cost model, println(“hello”) calls:
D. N log N
E. N2
F. Something else
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
n=1
n=2
,3
n=4
,5,6,7
n=8,9,10, …, 15
Nested For Loops II: PollEv.com/jhug or JHUG to 37607
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
“Worst case” here is irrelevant, all cases the same.
Cost model, println(“hello”) calls:
D. N log N
E. N2
F. Something else
N | R(N) |
1 | 1 |
4 | 1 + 2 + 4 = 7 |
7 | 1 + 2 + 4 = 7 |
8 | 1 + 2 + 4 + 8 = 15 |
27 | 1 + 2 + 4 + 8 + 16 = 31 |
185 | … + 64 + 128 = 255 |
715 | … + 256 + 512 = 1023 |
Nested For Loops II: PollEv.com/jhug or JHUG to 37607
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
N | R(N) | 1 * N < R(N) | 2 * N > R(N) |
1 | 1 | 1 | 2 |
4 | 1 + 2 + 4 = 7 | 4 | 8 |
7 | 1 + 2 + 4 = 7 | 7 | 14 |
8 | 1 + 2 + 4 + 8 = 15 | 8 | 16 |
27 | 1 + 2 + 4 + 8 + 16 = 31 | 27 | 54 |
185 | … + 64 + 128 = 255 | 185 | 370 |
715 | … + 256 + 512 = 1023 | 715 | 1430 |
Nested For Loops II: PollEv.com/jhug or JHUG to 37607
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
R(N) = Θ(1 + 2 + 4 + 8 + … + N)
= Θ(N)
D. N log N
E. N2
F. Something else
Repeat After Me...
There is no magic shortcut for these problems (well… usually)
Repeat After Me...
There is no magic shortcut for these problems (well… usually)
QR decomposition runtime, from a numerical linear algebra textbook
Example 3: Recursion
Recursion (Intuitive): PollEv.com/jhug or JHUG to 37607
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
Using your intuition, give the order of growth of the runtime of this code as a function of N?
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4
Recursion (Intuitive): PollEv.com/jhug or JHUG to 37607
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
Using your intuition, give the order of growth of the runtime of this code as a function of N?
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4
5
Recursion and Recurrence Relations
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
One approach: Count number of calls to f3, given by C(N).
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4
Recursion and Recurrence Relations
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
One approach: Count number of calls to f3, given by C(N).
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4
2C(N-1) + 1
Recursion and Recurrence Relations
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
One approach: Count number of calls to f3, given by C(N).
C(N) = 1 + 2 + 4 + 8 + … + ???
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4
Possible to solve mechanically (with algebra), but we won’t. Instead, we’ll use intuition in 61b.
Recursion and Recurrence Relations: PollEv.com/jhug or JHUG to 37607
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
One approach: Count number of calls to f3, given by C(N).
C(N) = 1 + 2 + 4 + 8 + … + 2 = ???
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4
Possible to solve mechanically (with algebra), but we won’t. Instead, we’ll use intuition in 61b.
N-1
Sums of Powers of 2 (Revisited)
C(N) = 1 + 2 + 4 + 8 + … + 2 = ???
This is just the same sum we saw before, where Q = 2 :
C(N) = 1 + 2 + 4 + 8 + … + 2 = 2(2 ) - 1 = 2 - 1
N-1
N-1
N-1
N
N-1
Recursion and Recurrence Relations
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
One approach: Count number of calls to f3, given by C(N).
C(N) = 1 + 2 + 4 + 8 + … + 2 = 2 -1 = Θ(2N)
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4
Possible to solve mechanically (with algebra), but we won’t. Instead, we’ll use intuition in 61b.
N-1
N
Recursion and Recurrence Relations (Extra)
Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)).
One approach: Count number of calls to f3, given by C(N).
This approach not covered in class. Provided for those of you who really want the algebra.
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4
Example 4: Binary Search
Binary Search (demo: http://goo.gl/iSbyRV)
Trivial to implement?
See Jon Bentley’s book Programming Pearls.
Binary Search (Intuitive): PollEv.com/jhug or JHUG to 37607
Let N = hi - lo + 1.
N=15
N=7
N=3
N=1
for simplicity: assume N=2k-1 for some k
k
Binary Search
Approach: Measure number of string comparisons for N = hi - lo + 1.
N=15
N=7
N=3
N=1
for simplicity: assume N=2k-1 for some k
k
Binary Search
Approach: Measure number of string comparisons for N = hi - lo + 1.
Give C(N) in terms of k:
C(N) = ???
N=15
N=7
N=3
N=1
k
for simplicity: assume N=2k-1 for some k
Binary Search
Approach: Measure number of string comparisons for N = hi - lo + 1.
C(N) = 1 + 1 + … + 1 + 0 = k
N=15
N=7
N=3
N=1
for simplicity: assume N=2k-1 for some k
k
k
Binary Search
Approach: Measure number of string comparisons for N = hi - lo + 1.
C(N) = 1 + 1 + … + 1 + 0 = k
N=15
N=7
N=3
N=1
for simplicity: assume N=2k-1 for some k
k
k
k = ⌈lg N⌉ = Θ(log N)
lg is short for base 2
Unproven BigTheta Properties We’ve Just Used
Some easy-to-prove properties:
Base of logarithm doesn’t matter. We’ll usually omit it completely!
Log Time Is Really Terribly Fast
Throughout this course we will see ways of doings things in constant vs log time. In practice, the difference is minimal.
N | log2 N | Runtime (seconds) |
100 | 6.6 | 1 nanosecond |
100,000 | 16.6 | 2.5 nanoseconds |
100,000,000 | 26.5 | 4 nanoseconds |
100,000,000,000 | 36.5 | 5.5 nanoseconds |
100,000,000,000,000 | 46.5 | 7 nanoseconds |
A Note on Solving Recurrence Relations
The entire goal is to find a pattern that yields a closed form solution for C(N).
Example 5: Merge Sort
Selection Sort: A Prelude to Example 5.
Earlier in class we discussed a sort called selection sort:
This algorithm requires Θ(N2) comparisons.
N=64
~2048
compares
SS
Array Merging of N Total Items (A.length + B.length = N)
What is the runtime for merge? Θ(1), Θ(N), Θ(N2)???
2 | 3 | 6 | 10 | 11 |
4 | 5 | 7 | 8 |
2 | 3 | 4 | 5 | 6 | 7 | 8 | 10 | 11 |
A
B
The Merge Operation
One way to sort N items:
N=64
2048
SS
N=64
N=32
N=32
512
64
512
SS
SS
M
Suppose the other two algs are selection sort.
how do we do better?
Example 5: Merge Sort
One way to sort N items:
Suppose they each use merge sort.
64
32
32
16
16
16
….
8
8
….
64
32
32
16
16
16
8
8
k
Merge Sort: More General
Intuitive explanation:
N
N/2
N/2
N/4
N/4
N/4
….
N/8
N/8
….
N/4
N/8
N/8
k
Merge Sort: Same Idea as Previous Slide, but Using Algebra
C(N): Number of items merged at each stage.
N
N/2
N/2
N/4
N/4
N/4
….
N/8
N/8
….
Only works for N=2k. Can be generalized at the expense of some tedium (e.g. separately prove big O and big Omega)
N/4
N/8
N/8
k
Linear vs. Linearithmic (N log N)
N log N is basically as good as N.
Summary
Theoretical analysis of algorithm performance requires careful thought.
Different solutions to the same problem may have radically different runtimes. N2 vs. N log N kind of a big deal.
Next time: