Professional Documents
Culture Documents
Analysis Og Algorithms
Analysis Og Algorithms
estimating running time mathematical analysis order-of-growth hypotheses input models measuring space
Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
Running time
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
Analytic Engine
Reasons to analyze algorithms Predict performance. Compare algorithms. Provide guarantees. Understand theoretical basis.
theory of algorithms (COS 423) this course (COS 226)
client gets poor performance because programmer did not understand performance characteristics
Some algorithmic successes Discrete Fourier transform. Break down waveform of N samples into periodic components. Applications: DVD, JPEG, MRI, astrophysics, . Brute force: N2 steps. FFT algorithm: N log N steps, enables new technology.
Freidrich Gauss 1805
time
64T
quadratic
32T
16T 8T
linearithmic linear
1K 2K 4K 8K
4
size
Some algorithmic successes N-body Simulation. Simulate gravitational interactions among N bodies. Brute force: N2 steps. Barnes-Hut: N log N steps, enables new research.
Andrew Appel PU '81
time
64T
quadratic
32T
16T 8T
linearithmic linear
1K 2K 4K 8K
5
size
estimating running time mathematical analysis order-of-growth hypotheses input models measuring space
Scientific analysis of algorithms A framework for predicting performance and comparing algorithms. Scientific method. Observe some feature of the universe. Hypothesize a model that is consistent with observation. Predict events using the hypothesis. Verify the predictions by making further observations. Validate by repeating until the hypothesis and observations agree.
Experimental algorithmics Every time you run a program you are doing an experiment!
First step. Debug your program! Second step. Choose input model for experiments. Third step. Run and time the program for problems of increasing size.
Example: 3-sum 3-sum. Given N integers, find all triples that sum to exactly zero. Application. Deeply related to problems in computational geometry.
% more 8ints.txt 30 -30 -20 -10 40 0 10 5 % java ThreeSum < 8ints.txt 4 30 -30 0 30 -20 -10 -30 -10 40 -10 0 10
public class ThreeSum { public static int count(long[] a) { int N = a.length; int cnt = 0; for (int i = 0; i < N; i++) for (int j = i+1; j < N; j++) for (int k = j+1; k < N; k++) if (a[i] + a[j] + a[k] == 0) cnt++; return cnt; } public static void main(String[] args) { int[] a = StdArrayIO.readLong1D(); StdOut.println(count(a)); } }
10
11
public class Stopwatch { private final long start = System.currentTimeMillis(); public double elapsedTime() { long now = System.currentTimeMillis(); return (now - start) / 1000.0; } }
implementation
12
3-sum: initial observations Data analysis. Observe and plot running time as a function of input size N.
13
Empirical analysis Log-log plot. Plot running time vs. input size N on log-log scale.
slope = 3
power law
Regression. Fit straight line through data points: c N a. Hypothesis. Running time grows cubically with input size: c N 3.
slope
14
agrees
N 16384
agrees
15
Doubling hypothesis Q. What is effect on the running time of doubling the size of the input?
Experimental algorithmics Many obvious factors affect running time: Machine. Compiler. Algorithm. Input data.
More factors (not so obvious): Caching. Garbage collection. Just-in-time compilation. CPU use by other applications.
Bad news. It is often difficult to get precise measurements. Good news. Easier than other sciences.
e.g., can run huge number of experiments
17
estimating running time mathematical analysis order-of-growth hypotheses input models measuring space
18
Mathematical models for running time Total running time: sum of cost frequency for all operations. Need to analyze program to determine set of operations. Cost depends on machine, compiler. Frequency depends on algorithm, input data.
operation integer add integer multiply integer divide floating point add floating point multiply floating point divide sine arctangent ...
nanoseconds
20
operation variable declaration assignment statement integer compare array element access array length 1D array allocation 2D array allocation string length substring extraction string concatenation
example int a a = b a < b a[i] a.length new int[N] new int[N][N] s.length() s.substring(N/2, N) s + t
nanoseconds c1 c2 c3 c4 c5 c6 N c7 N 2 c8 c9 c10 N
operation variable declaration assignment statement less than comparison equal to comparison array access increment
frequency
2 2 N+1 N N 2N
between N (no zeros) and 2N (all zeros)
22
operation variable declaration assignment statement less than comparison equal to comparison array access increment
frequency
0 + 1 + 2 + . . . + (N 1)
= =
1 N (N 1) 2 N 2
23
Tilde notation
Estimate running time (or memory) as a function of input size N. Ignore lower order terms.
Ex 1. Ex 2. Ex 3.
lg N + 7 N
lim
f (N ) = 1 g( N )
24
"inner loop"
operation variable declaration assignment statement less than comparison equal to comparison array access increment total
frequency
cost
total cost
~ N ~ N ~ 1/2 N 2
c1 c2 c3
~ c1 N ~ c2 N ~ c3 N 2 ~ c4 N 2 c5 N 2 ~ cN2
25
~ 1/2 N 2 ~ N2 N2 c4 c5
Example: 3-sum
478
the leading term of mathemati pressions by using a mathem device known as the tilde no 1 We write f (N) to represe for (int i = 0; i < N; i++) quantity that, when divided b for (int j = i+1; j < N; j++) N approaches 1 as N grows. W inner ~N / 2 for (int k = j+1; k < N; k++) loop (N 1)() N 2) indicat write N g (= N)N f (N to ~N / 6 if (a[i] + a[j] + a[k] == 0) 3 3! cnt++; g (N) f (N )1approaches 1 as N N 6 With this notation, we can return cnt; depends on input data } complicated parts of an exp public static void main(String[] args) that represent small values. F { ample, the if statement in Thr int[] a = StdArrayIO.readInt1D(); int cnt = count(a); is executed N 3/6 times b StdOut.println(cnt); N (N1)(N2)/6 N 3/6 N } } Nelse! /3, which certainly, when div Remark. Focus on instructions in inner loop; ignore everything N 3/6, approaches 1 as N grow Anatomy of a programs statement execution frequencies notation is useful when the ter 26 ter the leading term are relativ signicant (for example, when N = 1000, this assumption amounts to sayi
2 3
Mathematical models for running time In principle, accurate mathematical models are available. In practice, Formulas can be complicated. Advanced mathematics might be required. Exact models best left for experts.
costs (depend on machine, compiler)
estimating running time mathematical analysis order-of-growth hypotheses input models measuring space
28
Common order-of-growth hypotheses To determine order-of-growth: Assume a power law TN ~ c N a. Estimate exponent a with doubling hypothesis. Validate with mathematical analysis.
N 1,000 2,000 4,000 8,000 16,000 32,000 64,000 time (seconds) 0.43 0.53 1.01 2.87 11.00 44.64 177.48 observations
482
Good news. the small set ofFor functions logarithm is not relevant. example, CouponCollector (PROGRAM 1.4.2) is linsuffices
time for a problem of size N has order of growth N log N. Again, the base of the
time
1024T 512T
NROGRAM 4.2.6). Several imearithmic. The prototypical is, mergesort (see 1, log N, N, N logexample N, N2 N3, and 2P portant problems have natural solutions that are quadratic but clever algorithms that are linearithmic. Such algorithms (including mergesort) are critically importo describe order-of-growth of typical algorithms. tant in practice because they enable us to address problem sizes far larger than could be addressed with quadratic solutions. In the next section, we consider a general design technique for developing growth rate name linearithmic algorithms.
cubi c
dra tic
exponential
64T
8T 4T 2T T
running time has order of growth N 2 log N logarithmic has two nested for loops, used for some calculation involving all pairs linear of N eleN ments. The force update double loop in NBody (PROGRAM prototype of N log3.4.2 N ) is a linearithmic the programs in this classication, as is 2 quadratic the elementaryN sorting algorithm Insertion (PROGRAM 4.2.4). 3
N 2 cubic
qua
size
is cubic (its running time has constant order of growth N 3) because it has three nested for loops, to process all triples of 1K 2K 4K 8K 1024K N elements. The running time of matrix factor for Orders of growth (log-log plot) multiplication, as implemented in SEC- doubling hypothesis TION 1.4 has order of growth M 3 to multiply two M-by-M matrices, so the basic matrix multiplication algorithm is often considered to be cubic. However, the size of the input (the number of entries in the matrices) is proportional to N = M 2, so the algorithm is best classied as N 3/2, not cubic.
logarithmic
exponential
30
name constant
description statement
log N
logarithmic
divide in half
binary search
linear
loop
N log N
linearithmic
[see lecture 5]
mergesort
quadratic
for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { ... } for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) for (int k = 0; k < N; k++) { ... } [see lecture 24]
double loop
N3
cubic
triple loop
2N
exponential
exhaustive search
Practical implications of order-of-growth Q. How long to process millions of inputs? Ex. Population of NYC was "millions" in 1970s; still is. Q. How many inputs can be processed in minutes? Ex. Customers lost patience waiting "minutes" in 1970s; they still do.
seconds 1 10 102 103 104 equivalent 1 second 10 seconds 1.7 minutes 17 minutes 2.8 hours 1.1 days 1.6 weeks 3.8 months 3.1 years 3.1 decades 3.1 centuries forever age of universe
32
growth rate
any
any
any
any
instant
instant
instant
instant
log N
any
any
instant
instant
instant
instant
minutes
seconds
instant
N log N
hour
minutes
seconds
N2
thousand
thousands
decades
years
weeks
N3
hundred
hundreds
thousand
never
never
never
millennia
33
growth rate
name
description
effect on a program that runs for a few seconds time for 100x more data a few minutes a few minutes several hours several weeks forever size for 100x faster computer 100x 100x 10x 4-5x 1x
1 log N N N log N N2 N3 2N
independent of input size nearly independent of input size optimal for N inputs nearly optimal for N inputs not practical for large problems not practical for medium problems useful only for tiny problems
34
estimating running time mathematical analysis order-of-growth hypotheses input models measuring space
35
Types of analyses Best case. Running time determined by easiest inputs. Ex. N-1 compares to insertion sort N elements in ascending order. Worst case. Running time guarantee for all inputs. Ex. No more than N2 compares to insertion sort any N elements. Average case. Expected running time for "random" input. Ex. ~ N2 compares on average to insertion sort N random elements.
36
Commonly-used notations
notation
provides
example
shorthand for
used to
Tilde
leading term
~ 10 N 2
Big Theta
(N 2)
classify algorithms
Big Oh
(N 2) and smaller
O(N 2)
Big Omega
(N 2) and larger
(N 2)
37
Commonly-used notations Ex 1. Our brute-force 3-sum algorithm takes (N 3) time. Ex 2. Conjecture: worst-case running time for any 3-sum algorithm is (N 2). Ex 3. Insertion sort uses O(N 2) compares to sort any array of N elements; it uses ~ N compares in best case (already sorted) and ~ N 2 compares in the worst case (reverse sorted). Ex 4. The worst-case height of a tree created with union find with path compression is (N). Ex 5. The height of a tree created with weighted quick union is O(log N).
base of logarithm absorbed by big-Oh
loga N =
1 logb N logb a
38
Predictions and guarantees Theory of algorithms. Worst-case running time of an algorithm is O(f(N)). Advantages describes guaranteed performance. O-notation absorbs input model.
f(N) values represented by O(f(N)) time/memory
input size
39
Predictions and guarantees Experimental algorithmics. Given input model,average-case running time is ~ c f(N). Advantages. Can use to predict performance. Can use to compare algorithms.
values represented by ~ c f(N) c f(N) time/memory
Challenges. Need to develop accurate input model. May not provide guarantees.
input size
40
estimating running time mathematical analysis order-of-growth hypotheses input models measuring space
41
Typical memory requirements for primitive types in Java Bit. 0 or 1. Byte. 8 bits. Megabyte (MB). 210 bytes ~ 1 million bytes. Gigabyte (GB). 220 bytes ~ 1 billion bytes.
type boolean byte char int float long double bytes 1 1 2 4 4 8 8
42
Typical memory requirements for arrays in Java Array overhead. 16 bytes on a typical machine.
bytes 2N + 16 4N + 16 8N + 16
one-dimensional arrays
two-dimensional arrays
Q. What's the biggest double[] array you can store on your computer?
typical computer in 2008 has about 1GB memory
43
3.2.1) object uses 32 ee double instance variTypical memory requirements for objects in Java ny programs create milthe information needed Object overhead. 8 bytes on a typical machine. or transparency). A referReference. 4 bytes on a typical machine. n a data type contains a 4 bytes for the reference Ex 1. Each Complex object consumes 24 bytes of memory. y needed for the objects
public 32class bytes Complex { object private double re; overhead private double im; rx ... double ry values } q
8 bytes overhead for object 8 bytes 8 bytes
(Program 3.2.1)
class Charge
24 bytes
ct (Program 3.2.6)
24 bytes
object overhead re im
class Complex
double
values
Java library)
12 bytes
object overhead
44
class Color
ate byte r;
... }
values
public object { overhead private double re; private double im; double re Object overhead. im 8 bytes on a values ... }
24 bytes
typical machine.
public class Color object { Ex 2. A String of length overhead private byte r; private byte g; r g b a private byte b; private byte a; public class String byte ... values } {
N consumes 2N + 40 bytes.
8 bytes overhead for object 4 bytes 4 bytes 4 bytes 4 bytes for reference (plus 2N + 16 bytes for array) 2N + 40 bytes
private int offset; 16 bytes + string + vector private int count; public class Document object hash; private int { overhead private String id;private char[] value; id private Vector profile; references ...
... }
profile
reference values
45
int
public class RandomWalk { public static void main(String[] args) { int N = Integer.parseInt(args[0]); int[][] count = new int[N][N]; int x = N/2; int y = N/2; for (int i = 0; i < N; i++) { // no new variable declared in loop ... count[x][y]++; } } }
46
Example 2 Q. How much memory does this code fragment use as a function of N ?
... int N = Integer.parseInt(args[0]); for (int i = 0; i < N; i++) { int[] a = new int[N]; ... }
% java RandomWalk 10000 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space % java -Xmx 500m RandomWalk 10000 ... % java RandomWalk 30000 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space % java -Xmx 4500m RandomWalk 30000 Invalid maximum heap size: -Xmx4500m The specified size exceeds the maximum representable size. Could not create the Java virtual machine.
48
Turning the crank: summary In principle, accurate mathematical models are available. In practice, approximate mathematical models are easily achieved. Timing may be flawed? Limits on experiments insignificant compared to other sciences. Mathematics might be difficult? Only a few functions seem to turn up. Doubling hypothesis cancels complicated constants.
Actual data might not match input model? Need to understand input to effectively process it. Approach 1: design for the worst case. Approach 2: randomize, depend on probabilistic guarantee.
49