Lecture 02
Lecture 02
PART O N E
1. Analysis
of
Algorithms
http:// aofa.cs.princeton.edu
Why Analyze an Algorithm?
2
Analysis of Algorithms (Babbage, 1860s)
“As soon as an Analytic Engine exists, it will necessarily guide the future course of the science.
Whenever any result is sought by its aid, the question will arise—By what course of
calculation can these results be arrived at by the machine in the shortest time?”
Analytic Engine
3
Analysis of Algorithms (Turing (!), 1940s)
4
Analysis of Algorithms (Knuth, 1960s)
To analyze an algorithm:
•Develop a good implementation. D. E. Knuth
BENEFITS:
Scientific foundation for AofA.
Can predict performance and compare algorithms.
DRAWBACKS:
Model may be unrealistic.
Too much detail in analysis.
5
Theory of Algorithms (AHU, 1970s; CLR, present day)
Cormen, Leiserson,
Rivest, and Stein
•Classify algorithms by these costs.
6
Example: Two sorting algorithms
Quicksort Mergesort
Worst-case number of compares: O(N 2) Worst-case number of compares: N log N
Classification O(N 2) Classification O(N log N)
BUT
Quicksort is twice as fast as Mergesort in practice and uses half the space
How do we know?
By analyzing both algorithms! (stay tuned)
1. Analysis of Algorithms
• History and motivation
• A scientific approach
• Example: Quicksort
OF
• Resources
http:// aofa.cs.princeton.edu
1a.AofA.History
A N A LY T I C C O M B I N AT O R I C S
PART O N E
1. Analysis of Algorithms
• History and motivation
• A scientific approach
• Example: Quicksort
OF
• Resources
http:// aofa.cs.princeton.edu
1b.AofA.Scientific
Notation for theory of algorithms
11
O-notation considered dangerous
12
Surely, we can do better
13
Galactic algorithms
• theoretical tour-de-force
• too complicated to implement
• cost of implementing would exceed savings in this galaxy, anyway
One blogger’s conservative estimate:
75% SODA, 95% STOC/FOCS are galactic
14
Surely, we can do better
15
Analysis of Algorithms (scientific approach)
Empirical
% java SortTest 1000000
• Run algorithm to solve real problem. 10 44.44
100 847.85
• Measure running time and/or 1000 12985.91
count operations. 10000 175771.70
100000 2218053.41
Challenge: need good implementation
Mathematical
Σ 1
• Develop mathematical model.
CN = N + 1 + (C + C N —k—1)
N k
• Analyze algorithm within model. 1≤k≤N
Challenge: need good model, need to do the math
➛
2. Math may be too difficult.
• A challenge in any scientific discipline (cf. statistical physics).
• A “calculus” for AofA is the motivation for this course!
19
A N A LY T I C C O M B I N AT O R I C S
PART O N E
1. Analysis of Algorithms
• History and motivation
• A scientific approach
• Example: Quicksort
OF
• Resources
http:// aofa.cs.princeton.edu
1b.AofA.Scientific
A N A LY T I C C O M B I N AT O R I C S
PART O N E
1. Analysis of Algorithms
• History and motivation
• A scientific approach
• Example: Quicksort
OF
• Resources
http:// aofa.cs.princeton.edu
1c.AofA.Quicksort
Example: Quicksort
public class Quick
{
private static int partition(Comparable[] a, int lo, int hi)
{
int i = lo, j = hi+1;
while (true)
{
while (less(a[++i], a[lo])) if (i == hi) break;
while (less(a[lo], a[--j])) if (j == lo) break;
if (i >= j) break;
exch(a, i, j);
}
exch(a, lo, j);
return j;
}
Cost model
• running time? timing
Stay tuned.
23
Setup: Relevant questions about quicksort
24
Main step: Formulate a mathematical problem
Σ 1
CN = N + 1 + (Ck —1 + C N —k)
N
1≤k≤N
compares for
for partitioning subarrays
when k is the
partitioning
probability element
k is the
partitioning
element
25
Simplifying the recurrence
Σ 1
CN = N + 1 + (Ck —1 + C N —k) C0 = 0
N
1≤k≤N
Σ
2
Apply symmetry. CN = N + 1 + Ck —1
N
1≤k≤N
Σ
Multiply both sides by N. NCN = N(N + 1)+ 2 Ck—1
1≤k≤N
Collect terms.
NC N = (N + 1)CN—1 + 2N
26
Aside
Σ 1
CN = N + 1 + (Ck + C N —k—1) QUADRATIC time
N
O≤k≤N—1
⤷ c[0] = 0;
for (int N = 1; N <= maxN; N++)
{
c[N] = N+1;
for (int k = 0; k < N; k++)
c[N] += (c[k] + c[N-1-k])/N;
}
⤷ c[0] = 0;
for (int N = 1; N <= maxN; N++)
c[N] = (N+1)*c[N-1]/N + 2;
NC N = (N + 1)CN—1 + 2N
CN CN—1 2
Tricky (but key) step:
= +
divide by N(N+1) N +1 N N +1
CN CN—1 2 CN—2 2 2
= + = + +
N+1 N N+1 N —1 N N+1
Telescope.
C1 2 2 2
= + + .. . + +
2 3 N N +1
Σ 1
Simplify (ignore small terms). C N ~ 2N —2N
k
1≥k≥N
∫ ∞
1
Approximate with an C N ~ 2N ( dx + ц) —2N
integral (stay tuned) 1 x
= 2N ln N —2(1 —ц)N Euler’s constant ≐.57721
28
Finish: Validation (mathematical)
It is always worthwhile to check your math with your computer.
Experiment: Run code for randomly ordered distinct keys, count compares
2N ln N —2(1 —ц)N
1000 trials for each N
one grey dot for each trial
red dot: average for each N
✓
Observation: May be interested in distribution of costs
30
Quicksort compares: limiting distribution is not “normal”
see “Approximating the Limiting Quicksort Distribution.” by Fill and Janson (RSA 2001).
exact distribution
centered
(from recurrence)
on mean
for small N
empirical
validation
N = 1000
Bottom line:
• A great deal is known about the performance of Quicksort.
• AofA leads to intriguing new research problems.
31
Easy method to predict (approximate) performance
Experiment.
• Run for input size N. Observe running time.
• [Could solve for a.]
a(10N ) ln(10N ) = 10 + ln 10 = 10 + 1
• Predict time for 10N to increase by a factor of aN ln N ln N log 10 N
Example:
•Run quicksort 100 times for N = 100,000: Elapsed time: 4 seconds.
•Predict running time of 4 x 10.2 = 40.8 seconds for N = 1M.
•Observe running time of 41 seconds for N = 1M
•Confidently predict running time of 41 x 1000.5 = 11.4 hours for N = 1B.
32
Validate-refine-analyze cycle
— D. E.Knuth (1995)
34
A N A LY T I C C O M B I N AT O R I C S
PART O N E
1. Analysis of Algorithms
• History and motivation
• A scientific approach
• Example: Quicksort
OF
• Resources
http:// aofa.cs.princeton.edu
1c.AofA.Quicksort
A N A LY T I C C O M B I N AT O R I C S
PART O N E
1. Analysis of Algorithms
• History and motivation
• A scientific approach
• Example: Quicksort
OF
• Resources
http:// aofa.cs.princeton.edu
1d.AofA.Resources
Books
are the prime resources associated with this course.
First edition
Main (1995) Reference
text for
(2013) Algorithms
Text
for Reference
Part II for
Java
37
Booksites
are web resources associated with the books.
http:// aofa.cs.princeton.edu
Knuth's
collected
works
research papers
and books
by hundreds
of others
Math typesetting
Symbolic math
Web references
40
Introduce, read, discuss
2. You read the book and do assignments before the next lecture.
Goal: For you to learn quite a few things that you do not now know.
41
Exercises 1.14 and 1.15
42
Exercises 1.17 and 1.18
43
Assignments for next lecture
1. Surf booksites
• http://aofa.cs.princeton.edu
• http://algs4.cs.princeton.edu
44
A N A LY T I C C O M B I N AT O R I C S
PART O N E
1. Analysis of Algorithms
• History and motivation
• A scientific approach
• Example: Quicksort
OF
• Resources
http:// aofa.cs.princeton.edu
1d.AofA.Resources
A N A LY T I C C O M B I N AT O R I C S
PART O N E
1. Analysis
of
Algorithms
http:// aofa.cs.princeton.edu