# Analysis of Algorithms ```Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
1.4 A NALYSIS
A LGORITHMS
OF
‣ introduction
‣ observations
Algorithms
F O U R T H
E D I T I O N
R OBERT S EDGEWICK | K EVIN W AYNE
‣ mathematical models
‣ order-of-growth classifications
‣ memory
http://algs4.cs.princeton.edu
Last updated on Sep 21, 2015, 5:44 PM
Running time
“ As soon as an Analytical Engine exists, it will necessarily guide the future
course of the science. Whenever any result is sought by its aid, the question
will then arise—By what course of calculation can these results be arrived
at by the machine in the shortest time? ” — Charles Babbage (1864)
how many times
do you have to turn
the crank?
2
Reasons to analyze algorithms
Predict performance.
Compare algorithms.
this course
(COS 226)
Provide guarantees.
theory of algorithms
(COS 423)
Understand theoretical basis.
Primary practical reason: avoid performance bugs.
3
An algorithmic success story
N-body simulation.
・Simulate gravitational interactions among N bodies.
・Applications: cosmology, fluid dynamics, semiconductors, ...
・Brute force: N steps.
・Barnes-Hut algorithm: N log N steps, enables new research.
2
Andrew Appel
PU '81
time
64T
32T
limit on
available time
16T
linearithmic
8T
size
linear
1K 2K
4K
8K
4
Another algorithmic success story
Discrete Fourier transform.
・Express signal as weighted sum of sines and cosines.
・Applications: DVD, JPEG, MRI, astrophysics, ….
・Brute force: N steps.
・FFT algorithm: N log N steps, enables new technology.
2
James
Cooley
John
Tukey
time
64T
32T
limit on
available time
16T
linearithmic
8T
size
linear
1K 2K
4K
8K
5
1.4 A NALYSIS
OF
A LGORITHMS
‣ introduction
‣ observations
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
‣ mathematical models
‣ order-of-growth classifications
‣ memory
The challenge
Q. Will my program be able to solve a large practical input?
Why is my program so slow ?
Why does it run out of memory ?
Insight. [Knuth 1970s] Use scientific method to understand performance.
7
Scientific method applied to the analysis of algorithms
A framework for predicting performance and comparing algorithms.
Scientific method.
・Observe some feature of the natural world.
・Hypothesize a model that is consistent with the observations.
・Predict events using the hypothesis.
・Verify the predictions by making further observations.
・Validate by repeating until the hypothesis and observations agree.
Principles.
・Experiments must be reproducible.
・Hypotheses must be falsifiable.
Feature of the natural world. Computer itself.
Francis
Bacon
René
Descartes
John Stuart
Mills
8
Example: 3-SUM
3-SUM. Given N distinct integers, how many triples sum to exactly zero?
Context. Deeply related to problems in computational geometry.
% more 8ints.txt
8
30 -40 -20 -10 40 0 10 5
% java ThreeSum 8ints.txt
4
a[i]
a[j]
a[k]
sum
1
30
-40
10
0
2
30
-20
-10
0
3
-40
40
0
0
4
-10
0
10
0
9
3-SUM: brute-force algorithm
public class ThreeSum
{
public static int count(int[] a)
{
int N = a.length;
int count = 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)
count++;
return count;
}
check each triple
for simplicity, ignore
integer overflow
public static void main(String[] args)
{
In in = new In(args);
StdOut.println(count(a));
}
}
10
Measuring the running time
Q. How to time a program?
% java ThreeSum 1Kints.txt
A. Manual.
tick tick tick
70
% java ThreeSum 2Kints.txt
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
528
% java ThreeSum 4Kints.txt
4039
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick
Observing the running time of a program
11
Measuring the running time
Q. How to time a program?
A. Automatic.
public class Stopwatch
(part of stdlib.jar )
Stopwatch()
double elapsedTime()
create a new stopwatch
time since creation (in seconds)
public static void main(String[] args)
{
In in = new In(args);
Stopwatch stopwatch = new Stopwatch();
StdOut.println(ThreeSum.count(a));
client code
double time = stopwatch.elapsedTime();
StdOut.println("elapsed time = " + time);
}
12
Empirical analysis
Run the program for various input sizes and measure running time.
13
Empirical analysis
Run the program for various input sizes and measure running time.
N
time (seconds)
250
0.0
500
0.0
1,000
0.1
2,000
0.8
4,000
6.4
8,000
51.1
†
† on a 2.8GHz Intel PU-226 with 64GB
DDR E3 memory and 32MB L3 cache;
running Oracle Java 1.7.0_45-b18 on
Springdale Linux v. 6.5
14
Data analysis
Standard plot. Plot running time T (N) vs. input size N.
standard plot
log-log plot
50
51.2
s
25.6
12.8
lg(T(N))
running time T(N)
40
30
20
6.4
3.2
1.6
.8
.4
10
.2
.1
1K
2K
4K
8K
1
problem size N
Analysis of experimental data (the running time of ThreeSum)
15
Data analysis
Log-log plot. Plot running time T (N) vs. input size N using log-log scale.
log-log plot
51.2
25.6
straight line
of slope 3
lg (T(N ))
12.8
lg(T (N)) = b lg N + c
b = 2.999
c = -33.2103
6.4
3.2
1.6
T (N) = a N b, where a = 2 c
.8
3 orders
of magnitude
.4
.2
.1
8K
1K
2K
4K
8K
lgN
ental data (the running time of ThreeSum)
Regression. Fit straight line through data points: a N b.
power law
slope
Hypothesis. The running time is about 1.006 × 10 –10 × N 2.999 seconds.
16
Prediction and validation
Hypothesis. The running time is about 1.006 × 10 –10 × N 2.999 seconds.
Predictions.
・51.0 seconds for N = 8,000.
・408.1 seconds for N = 16,000.
Observations.
N
time (seconds)
8,000
51.1
8,000
51.0
8,000
51.1
16,000
410.8
†
validates hypothesis!
17
Doubling hypothesis
Doubling hypothesis. Quick way to estimate b in a power-law relationship.
Run program, doubling the size of the input.
N
250
time (seconds)
†
ratio
0.0
lg ratio
–
500
0.0
4.8
2.3
1,000
0.1
6.9
2.8
2,000
0.8
7.7
2.9
4,000
6.4
8.0
3.0
8,000
51.1
8.0
3.0
T (N )
aN b
=
T (N/2)
a(N/2)b
= 2b
lg (6.4 / 0.8) = 3.0
seems to converge to a constant b ≈ 3
Hypothesis. Running time is about a N b with b = lg ratio.
Caveat. Cannot identify logarithmic factors with doubling hypothesis.
18
Doubling hypothesis
Doubling hypothesis. Quick way to estimate b in a power-law relationship.
Q. How to estimate a (assuming we know b) ?
A. Run the program (for a sufficient large value of N) and solve for a.
N
8,000
time (seconds)
†
51.1
51.1 = a × 80003
8,000
51.0
8,000
51.1
⇒ a = 0.998 × 10 –10
Hypothesis. Running time is about 0.998 × 10 –10 × N 3 seconds.
almost identical hypothesis
to one obtained via regression
19
Analysis of algorithms quiz 1
Estimate the running time to solve a problem of size N = 96,000.
N
A.
39 seconds.
B.
52 seconds.
C.
time (seconds)
1000
0.02
2000
0.05
117 seconds.
4,000
0.20
D.
350 seconds.
8,000
0.81
E.
I don't know.
16,000
3.25
32,000
13.00
†
20
Experimental algorithmics
System independent effects.
・Algorithm.
・Input data.
determines exponent b
in power law a N b
System dependent effects.
・Hardware: CPU, memory, cache, …
・Software: compiler, interpreter, garbage collector, …
・System: operating system, network, other apps, …
determines constant a
in power law a N b
Bad news. Sometimes difficult to get precise measurements.
Good news. Much easier and cheaper than other sciences.
21
An aside
Algorithmic experiments are virtually free by comparison with other sciences.
Chemistry
(1 experiment)
Biology
(1 experiment)
Physics
(1 experiment)
Computer Science
(1 million experiments)
Bottom line. No excuse for not running experiments to understand costs.
22
1.4 A NALYSIS
OF
A LGORITHMS
‣ introduction
‣ observations
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
‣ mathematical models
‣ order-of-growth classifications
‣ memory
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.
Donald Knuth
1974 Turing Award
In principle, accurate mathematical models are available.
24
Example: 1-SUM
Q. How many instructions as a function of input size N ?
int count = 0;
for (int i = 0; i < N; i++)
if (a[i] == 0)
count++;
N array accesses
operation
cost (ns) †
frequency
variable declaration
2/5
2
assignment statement
1/5
2
less than compare
1/5
N+1
equal to compare
1/10
N
array access
1/10
N
increment
1/10
N to 2 N
† representative estimates (with some poetic license)
25
Example: 2-SUM
Q. How many instructions as a function of input size N ?
int count = 0;
for (int i = 0; i < N; i++)
for (int j = i+1; j < N; j++)
if (a[i] + a[j] == 0)
count++;
0 + 1 + 2 + . . . + (N
1)
=
=
Pf. [ Gauss ]
+
T(N)
=
0
+
1
+
…
+
(N – 2)
+
(N – 1)
T(N)
=
(N – 1)
+
(N – 2)
+
…
+
1
+
0
2 T(N)
=
(N – 1)
+
(N – 1)
+
…
+
(N – 1)
+
(N – 1)
⇒
T(N)
=
N (N – 1) / 2
1
N (N
2 ⇥
N
2
1)
26
Example: 2-SUM
Q. How many instructions as a function of input size N ?
int count = 0;
for (int i = 0; i < N; i++)
for (int j = i+1; j < N; j++)
if (a[i] + a[j] == 0)
count++;
0 + 1 + 2 + . . . + (N
operation
cost (ns)
frequency
variable declaration
2/5
N+2
assignment statement
1/5
N+2
less than compare
1/5
½ (N + 1) (N + 2)
equal to compare
1/10
½ N (N − 1)
array access
1/10
N (N − 1)
increment
1/10
½ N (N + 1) to N 2
1)
=
=
1
N (N
2 ⇥
N
2
1)
1/4 N2 + 13/20 N + 13/10 ns
to
3/10 N2 + 3/5 N + 13/10 ns
(tedious to count exactly)
27
Simplifying the calculations
“ It is convenient to have a measure of the amount of work involved
in a computing process, even though it be a very crude one. We may
count up the number of times that various elementary operations are
applied in the whole process and then given them various weights.
We might, for instance, count the number of additions, subtractions,
multiplications, divisions, recording of numbers, and extractions
of figures from tables. In the case of computing with matrices most
of the work consists of multiplications and writing down numbers,
and we shall therefore only attempt to count the number of
multiplications and recordings. ” — Alan Turing
ROUNDING-OFF ERRORS IN MATRIX PROCESSES
By A. M. TURING
{National Physical Laboratory, Teddington, Middlesex)
THIS
paper contains descriptions of a number of methods for solving sets
Downlo
SUMMARY
A number of methods of solving sets of linear equations and inverting matrices
are discussed. The theory of the rounding-off errors involved is investigated for
some of the methods. In all cases examined, including the well-known 'Gauss
elimination process', it is found that the errors are normally quite moderate: no
exponential build-up need occur.
Included amongst the methods considered is a generalization of Choleski's method
which appears to have advantages over other known methods both as regards
accuracy and convenience. This method may also be regarded as a rearrangement
of the elimination process.
28
Simplification 1: cost model
Cost model. Use some basic operation as a proxy for running time.
int count = 0;
for (int i = 0; i < N; i++)
for (int j = i+1; j < N; j++)
if (a[i] + a[j] == 0)
count++;
0 + 1 + 2 + . . . + (N
operation
cost (ns)
frequency
variable declaration
2/5
N+2
assignment statement
1/5
N+2
less than compare
1/5
½ (N + 1) (N + 2)
equal to compare
1/10
½ N (N − 1)
array access
1/10
N (N − 1)
increment
1/10
½ N (N + 1) to N 2
1)
=
=
1
N (N
2 ⇥
N
2
1)
cost model = array accesses
(we assume compiler/JVM do not
optimize any array accesses away!)
29
Simplification 2: tilde notation
・Estimate running time (or memory) as a function of input size N.
・Ignore lower order terms.
– when N is large, terms are negligible
– when N is small, we don't care
N 3/6
Ex 1.
⅙ N 3 + 20 N + 16
~ ⅙N3
Ex 2.
⅙N
~ ⅙N
Ex 3.
⅙N3 - ½N
3
+ 100 N
2
4/3
+ 56
+ ⅓ N
N 3/6 ! N 2/2 + N /3
166,666,667
3
~ ⅙N3
166,167,000
N
(e.g., N = 1000: 166.67 million vs. 166.17 million)
Technical definition. f(N) ~ g(N) means
1,000
f (N)
= 1
∞ g(N)
lim
N→
30
Simplification 2: tilde notation
・Estimate running time (or memory) as a function of input size N.
・Ignore lower order terms.
– when N is large, terms are negligible
– when N is small, we don't care
operation
frequency
tilde notation
variable declaration
N+2
~N
assignment statement
N+2
~N
less than compare
½ (N + 1) (N + 2)
~½N2
equal to compare
½ N (N − 1)
~½N2
array access
N (N − 1)
~N2
increment
½ N (N + 1) to N 2
~ ½ N 2 to ~ N 2
31
Example: 2-SUM
Q. Approximately how many array accesses as a function of input size N ?
int count = 0;
for (int i = 0; i < N; i++)
for (int j = i+1; j < N; j++)
if (a[i] + a[j] == 0)
count++;
"inner loop"
0 + 1 + 2 + . . . + (N
1)
=
=
1
N (N
2 ⇥
N
2
1)
A. ~ N 2 array accesses.
Bottom line. Use cost model and tilde notation to simplify counts.
32
Example: 3-SUM
Q. Approximately how many array accesses as a function of input size N ?
int count = 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)
count++;
"inner loop"
N
3
A. ~ ½ N 3 array accesses.
⇥
=
⇥
N (N
1)(N
3!
2)
1 3
N
6
Bottom line. Use cost model and tilde notation to simplify counts.
33
Estimating a discrete sum
Q. How to estimate a discrete sum?
A1. Take a discrete mathematics course (COS 340).
34
Estimating a discrete sum
Q. How to estimate a discrete sum?
A2. Replace the sum with an integral, and use calculus!
⇥
N
Ex 1. 1 + 2 + … + N.
i
Ex 2. 1 + 1/2 + 1/3 + … + 1/N.
i=1
N
N
⇥
1
i
⇥
1
Ex 4. 1 + ½ + ¼ + ⅛ + …
x=0
x
N
x=1
i=1 j=i k=j
1
2
1
dx =
ln 2
i=0
N
x=1
N
Ex 3. 3-sum triple loop.
x dx
x=1
i=1
N
1 2
N
2
N
1
2
⇥
1
dx = ln N
x
N
y=x
⇥
N
z=y
dz dy dx
1 3
N
6
1.4427
i
= 2
integral trick
doesn't always work!
35
Mathematical models for running time
In principle, accurate mathematical models are available.
In practice, formulas can be complicated.
costs (depend on machine, compiler)
TN = c1 A + c2 B + c3 C + c4 D + c5 E
A=
B=
C=
D=
E=
array access
integer compare
increment
variable assignment
frequencies
(depend on algorithm, input)
Bottom line. We use approximate models in this course: T(N) ~ c N 3.
36
Analysis of algorithms quiz 2
How many array accesses does the following code fragment make as a
function of N ?
int count = 0;
for (int i = 0; i < N; i++)
for (int j = i+1; j < N; j++)
for (int k = 1; k < N; k = k*2)
if (a[i] + a[j] >= a[k])
count++;
A.
~ N 2 lg N
B.
~ 3/2 N 2 lg N
C.
~ 1/2 N 3
D.
~ 3/2 N 3
E.
I don't know.
37
1.4 A NALYSIS
OF
A LGORITHMS
‣ introduction
‣ observations
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
‣ mathematical models
‣ order-of-growth classifications
‣ memory
Common order-of-growth classifications
Definition. If f (N) ~ c g(N) for some constant c > 0, then the order of growth
of f (N) is g(N).
・Ignores lower-order terms.
Ex. The order of growth of the running time of this code is N 3.
int count = 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)
count++;
Typical usage. Mathematical analysis of running times.
depends on machine, compiler, JVM, ...
39
Commonly-used notations in the theory of algorithms
notation
provides
example
shorthand for
used to
½ N2
Big Theta
asymptotic
order of growth
Θ(N2)
10 N 2
5 N 2 + 22 N log N + 3N
classify
algorithms
⋮
10 N 2
Big O
Θ(N2)
and smaller
O(N2)
100 N
22 N log N + 3 N
develop
upper bounds
⋮
½N2
Big Omega
Θ(N2)
and larger
Ω(N2)
N5
N 3 + 22 N log N + 3 N
⋮
develop
lower bounds
time
Common order-of-growth classifications
200T
Good news. The set of functions
100T
1, log N, N, N log N, N 2, N 3, and 2N
logarithmic
constant
suffices to describe the order of growth of most common algorithms.
100K
200K
500K
size
r
ic
ea
hm
lin
lin
ea
qua
r it
dra
tic
cubi
c
512T
exponential
log-log plot
time
64T
8T
4T
2T
logarithmic
T
constant
1K
2K
4K
8K
size
512K
Typical orders of growth
41
Common order-of-growth classifications
order of
growth
name
typical code framework
description
example
T(2N) / T(N)
1
constant
a = b + c;
statement
numbers
1
log N
logarithmic
while (N > 1)
N = N/2; ...
divide
in half
binary search
N
linear
for (int i = 0; i < N; i++)
{ ... }
single
loop
find the
maximum
2
N log N
linearithmic
see mergesort lecture
divide and
conquer
mergesort
~2
double
loop
check all
pairs
4
triple
loop
check all
triples
8
exhaustive
search
check all
subsets
2N
N2
N
3
2N
cubic
exponential
{
}
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 combinatorial search lecture
~1
42
Binary search
Goal. Given a sorted array and a key, find index of the key in the array?
Binary search. Compare key against middle entry.
・Too small, go left.
・Too big, go right.
・Equal, found.
6
13
14
25
33
43
51
53
64
72
84
93
95
96
97
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
43
Binary search: implementation
Trivial to implement?
・First binary search published in 1946.
・First bug-free one in 1962.
・Bug in Java's Arrays.binarySearch() discovered in 2006.
44
Binary search: Java implementation
Invariant. If key appears in array a[], then a[lo] ≤ key ≤ a[hi].
public static int binarySearch(int[] a, int key)
{
int lo = 0, hi = a.length - 1;
while (lo <= hi)
why not mid = (lo + hi) / 2 ?
{
int mid = lo + (hi - lo) / 2;
if
(key < a[mid]) hi = mid - 1;
else if (key > a[mid]) lo = mid + 1;
else return mid;
}
return -1;
one "3-way compare"
}
45
Binary search: mathematical analysis
Proposition. Binary search uses at most 1 + lg N key compares to search in
a sorted array of size N.
Def. T (N) = # key compares to binary search a sorted subarray of size ≤ N.
Binary search recurrence. T (N) ≤ T (N / 2) + 1 for N > 1, with T (1) = 1.
left or right half
(floored division)
possible to implement with one
Pf sketch. [assume N is a power of 2]
T (N)
≤
T (N / 2) + 1
[ given ]
≤
T (N / 4) + 1 + 1
[ apply recurrence to first term ]
≤
T (N / 8) + 1 + 1 + 1
[ apply recurrence to first term ]
≤
T (N / N) + 1 + 1 + … + 1
[ stop applying, T(1) = 1 ]
=
1 + lg N
⋮
lg N
46
1.4 A NALYSIS
OF
A LGORITHMS
‣ introduction
‣ observations
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
‣ mathematical models
‣ order-of-growth classifications
‣ memory
Basics
Bit. 0 or 1.
NIST
most computer scientists
Byte. 8 bits.
Megabyte (MB). 1 million or 220 bytes.
Gigabyte (GB).
1 billion or 230 bytes.
64-bit machine. We assume a 64-bit machine with 8-byte pointers.
some JVMs "compress" ordinary object
pointers to 4 bytes to avoid this cost
48
Typical memory usage for primitive types and arrays
type
bytes
type
bytes
boolean
1
char[]
2 N + 24
byte
1
int[]
4 N + 24
char
2
double[]
8 N + 24
int
4
float
4
long
8
double
one-dimensional arrays
type
bytes
char[][]
~2MN
int[][]
~4MN
double[][]
~8MN
8
primitive types
two-dimensional arrays
49
Typical memory usage for objects in Java
integer wrapper object
Reference. 8 bytes.
public class Integer
{ Each object uses a
private int x;
...
}
Ex 1. A Date object uses 32
date object
public class Date
{
private int day;
private int month;
private int year;
...
}
24 bytes
multiple of 8 bytes.
object
int
x
value
of memory.
32 bytes
object
day
month
year
4 bytes (int)
int
values
4 bytes (int)
4 bytes (int)
32 bytes
counter object
public class Counter
32 bytes
50
Typical memory usage summary
Total memory usage for a data type value:
・Primitive type: 4 bytes for int, 8 bytes for double, …
・Object reference: 8 bytes.
・Array: 24 bytes + memory for each array entry.
・Object: 16 bytes + memory for each instance variable.
・Padding: round up to multiple of 8 bytes.
+ 8 extra bytes per inner class object
(for reference to enclosing class)
Note. Depending on application, we may want to count memory for any
referenced objects (recursively).
51
Analysis of algorithms quiz 3
How much memory does a WeightedQuickUnionUF use as a function of N ?
A.
~ 4 N bytes
B.
~ 8 N bytes
C.
~ 4 N 2 bytes
D.
~ 8 N 2 bytes
E.
I don't know.
public class WeightedQuickUnionUF
{
private int[] parent;
private int[] size;
private int count;
public WeightedQuickUnionUF(int N)
{
parent = new int[N];
size = new int[N];
count = 0;
for (int i = 0; i < N; i++)
parent[i] = i;
for (int i = 0; i < N; i++)
size[i] = 1;
}
...
}
52
TECHNICAL INTERVIEW QUESTIONS
53
THE 3-SUM PROBLEM
3-SUM. Given N distinct integers, find three such that a + b + c = 0.
Version 0. N 3 time, N space.
Version 1. N 2 log N time, N space.
Version 2. N 2 time, N space.
Note. For full credit, running time should be worst case.
*Fastest known algorithm (published in 2014): N 2 / (log N / log log N) 2/3 time
54
THE 3-SUM PROBLEM:
AN N2 LOG N ALGORITHM
input
30 -40 -20 -10 40
Algorithm.
・Step 1:
・Step 2:
Sort the N (distinct) numbers.
For each pair of numbers a[i]
and a[j], binary search for -(a[i] + a[j]).
-40 -20 -10
0
(-40, -10)
50
Analysis. Order of growth is N 2 log N.
(-40,
0)
40
・Step 1:
(-40,
5)
35
(-40,
10)
30
(or N log N with mergesort).
・
Step 2: N 2 log N with binary search.
5 10 30 40
binary search
60
with insertion sort
5
sort
(-40, -20)
N2
0 10
⋮
⋮
(-20, -10)
⋮
(-10,
30
⋮
0)
⋮
10
⋮
( 10,
30)
-40
( 10,
40)
-50
( 30,
40)
-70
only count if
a[i] < a[j] < a[k]
to avoid
double counting
55
Turning the crank: summary
Empirical analysis.
・Execute program to perform experiments.
・Assume power law.
・Formulate a hypothesis for running time.
・Model enables us to make predictions.
Mathematical analysis.
・Analyze algorithm to count frequency of operations.
・Use tilde notation to simplify analysis.
・Model enables us to explain behavior.
⌊lg N ⌋
!
h=0
⌈N/2h+1 ⌉ h ∼ N
Scientific method.
・Mathematical model is independent of a particular
system; applies to machines not yet built.
・Empirical analysis is necessary to validate
mathematical models and to make predictions.
56
```