3sum and Improvement

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Cast of characters

Programmer needs to develop


a working solution.

Student might play


any or all of these
Client wants to solve roles someday.
problem efficiently.

Theoretician wants
to understand.

Basic blocking and tackling


is sometimes necessary.
[this lecture]

2
Some algorithmic successes

Discrete Fourier transform.


・Break down waveform of N samples into periodic components.
・Applications: DVD, JPEG, MRI, astrophysics, ….
・Brute force: N steps.
2
Friedrich Gauss

・FFT algorithm: N log N steps, enables new technology.


1805

time
quadratic
64T

32T

16T
linearithmic
8T
linear

size 1K 2K 4K 8K

5
Some algorithmic successes

N-body simulation.
・Simulate gravitational interactions among N bodies.
・Brute force: N steps.
2

・Barnes-Hut algorithm: N log N steps, enables new research. Andrew Appel


PU '81

time
quadratic
64T

32T

16T
linearithmic
8T
linear

size 1K 2K 4K 8K

6
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
Example: 3-SUM

3-SUM. Given N distinct integers, how many triples sum to exactly zero?

% more 8ints.txt a[i] a[j] a[k] sum


8
1 30 -40 10 0
30 -40 -20 -10 40 0 10 5
2 30 -20 -10 0
% java ThreeSum 8ints.txt
4 3 -40 40 0 0

4 -10 0 10 0

Context. Deeply related to problems in computational geometry.


10
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++) check each triple

if (a[i] + a[j] + a[k] == 0) for simplicity, ignore


count++; integer overflow

return count;
}

public static void main(String[] args)


{
int[] a = In.readInts(args[0]);
StdOut.println(count(a));
}
}

11
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
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
4039

Observing the running time of a program 12


Measuring the running time

Q. How to time a program?


A. Automatic.

public class Stopwatch (part of stdlib.jar )

Stopwatch() create a new stopwatch

double elapsedTime() time since creation (in seconds)

public static void main(String[] args)


{
int[] a = In.readInts(args[0]);
Stopwatch stopwatch = new Stopwatch();
StdOut.println(ThreeSum.count(a));
double time = stopwatch.elapsedTime();
} client code

13
Empirical analysis

Run the program for various input sizes and measure running time.

14
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

16,000 ?

15
Data analysis

Standard plot. Plot running time T (N) vs. input size N.

standard plot 50 log-log plot 51.2 s


25.6

running time T(N) 40 12.8

6.4

lg (T(N ))
30 3.2

1.6

20 .8

.4

10 .2

.1

1K 2K 4K 8K 1
problem size N
Analysis of experimental data (the running time of ThreeSum)

16
Data analysis

Log-log plot. Plot running time T (N) vs. input size N using log-log scale.

log-log plot 51.2 straight line


of slope 3
25.6

12.8
lg(T (N)) = b lg N + c
6.4 b = 2.999
lg (T(N ))

3.2 c = -33.2103
1.6

.8 T (N) = a N b, where a = 2 c
.4

.2

.1

8K 1K 2K 4K 8K
lgN
ental data (the running time of ThreeSum) power law

Regression. Fit straight line through data points: a N b. slope

Hypothesis. The running time is about 1.006 × 10 –10 × N 2.999 seconds.


17
Prediction and validation

Hypothesis. The running time is about 1.006 × 10 –10 × N 2.999 seconds.

"order of growth" of running


time is about N3 [stay tuned]
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!

18
Doubling hypothesis

Doubling hypothesis. Quick way to estimate b in a power-law relationship.

Run program, doubling the size of the input.

N time (seconds) † ratio lg ratio

250 0.0 –

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

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.
19
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 time (seconds) †

8,000 51.1
51.1 = a × 80003
8,000 51.0
⇒ a = 0.998 × 10 –10
8,000 51.1

Hypothesis. Running time is about 0.998 × 10 –10 × N 3 seconds.

almost identical hypothesis


to one obtained via linear regression
20
Experimental algorithmics

System independent effects.


・Algorithm. determines exponent b

・Input data.
in power law

determines constant a
System dependent effects. in power law

・Hardware: CPU, memory, cache, …


・Software: compiler, interpreter, garbage collector, …
・System: operating system, network, other apps, …

Bad news. Difficult to get precise measurements.


Good news. Much easier and cheaper than other sciences.

e.g., can run huge number of experiments

21
Cost of basic operations

operation example nanoseconds †

integer add a + b 2.1

integer multiply a * b 2.4

integer divide a / b 5.4

floating-point add a + b 4.6

floating-point multiply a * b 4.2

floating-point divide a / b 13.5

sine Math.sin(theta) 91.3

arctangent Math.atan2(y, x) 129.0

... ... ...

† Running OS X on Macbook Pro 2.2GHz with 2GB RAM

25
Cost of basic operations

operation example nanoseconds †

variable declaration int a c1

assignment statement a = b c2

integer compare a < b c3

array element access a[i] c4

array length a.length c5

1D array allocation new int[N] c6 N

2D array allocation new int[N][N] c7 N 2

string length s.length() c8

substring extraction s.substring(N/2, N) c9

string concatenation s + t c10 N

Novice mistake. Abusive string concatenation.


26
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++;

operation frequency

variable declaration 2

assignment statement 2

less than compare N+1

equal to compare N

array access N

increment N to 2 N

27
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++;

1
0 + 1 + 2 + . . . + (N 1) = N (N 1)
2 ⇥
N
=
operation frequency 2

variable declaration N+2

assignment statement N+2

less than compare ⇤ (N + 1) (N + 2)

equal to compare ⇤ N (N 1)
tedious to count exactly
array access N (N 1)

increment ⇤ N (N 1) to N (N 1)

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++;

1
0 + 1 + 2 + . . . + (N 1) = N (N 1)
2 ⇥
N
=
operation frequency 2

variable declaration N+2

assignment statement N+2

less than compare ⇤ (N + 1) (N + 2)

equal to compare ⇤ N (N 1)

array access N (N 1) cost model = array accesses

(we assume compiler/JVM do not


increment ⇤ N (N 1) to N (N 1)
optimize array accesses away!)

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
N 3/6

Ex 1. ⅙ N 3 + 20 N + 16!! ~ ⅙N3
166,666,667 N 3/6 ! N 2/2 + N /3
Ex 2. ⅙N 3 + 100 N 4/3 + 56! ~ ⅙N 3

Ex 3. ⅙N3 - ½N 2 + ⅓ N! ~ ⅙N3 166,167,000

N 1,000
discard lower-order terms Leading-term approximation
(e.g., N = 1000: 500 thousand vs. 166 million)

f (N)
Technical definition. f(N) ~ g(N) means lim = 1
N→ ∞ g(N)

31
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 (N 1) ~ ⇤ N2 to ~ N2

32
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) "inner loop"

count++;

1
0 + 1 + 2 + . . . + (N 1) = N (N 1)
2 ⇥
A. ~ N 2 array accesses. =
N
2

Bottom line. Use cost model and tilde notation to simplify counts.
33
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) "inner loop"

count++;


N N (N 1)(N 2)
=
3 3!
A. ~ ½ N 3 array accesses. ⇥
1 3
N
6

Bottom line. Use cost model and tilde notation to simplify counts.
34
Estimating a discrete sum

Q. How to estimate a discrete sum?


A1. Take discrete mathematics course.
A2. Replace the sum with an integral, and use calculus!

N ⇥ N
1 2
Ex 1. 1 + 2 + … + N. i x dx N
i=1 x=1 2

N N
k 1
Ex 2. 1k + 2k + … + N k. i xk dx N k+1
i=1 x=1 k+1

N ⇥
1 N
1
Ex 3. 1 + 1/2 + 1/3 + … + 1/N. dx = ln N
i=1
i x=1 x

N N N ⇥ ⇥ ⇥
N N N
1 3
Ex 4. 3-sum triple loop. 1 dz dy dx N
i=1 j=i k=j x=1 y=x z=y 6

35
time
Common order-of-growth
200T
classifications
order of growth discards
Good news. the small
100T set of functions leading coefficient
logarithmic
1, log N, N, N log N, N 2, N 3, and 2N constant

suffices to describe order-of-growth of typical algorithms.


100K 200K 500K
size

log-log plot
512T

exponential

tic

ic
cubi

hm
dra

r
r it

ea
qua

ea

lin
lin
64T
time

8T

4T

2T
logarithmic
T
constant

1K 2K 4K 8K size 512K

Typical orders of growth

39
Common order-of-growth classifications

order of
name typical code framework description example T(2N) / T(N)
growth

add two
1 constant a = b + c; statement 1
numbers

while (N > 1)
log N logarithmic divide in half binary search ~1
{ N = N / 2; ... }

for (int i = 0; i < N; i++) find the


N linear loop 2
{ ... } maximum

divide
N log N linearithmic [see mergesort lecture] mergesort ~2
and conquer

for (int i = 0; i < N; i++)


check all
N2 quadratic for (int j = 0; j < N; j++) double loop 4
pairs
{ ... }

for (int i = 0; i < N; i++)


for (int j = 0; j < N; j++) check all
N3 cubic triple loop 8
for (int k = 0; k < N; k++) triples
{ ... }

exhaustive check all


2N exponential [see combinatorial search lecture] T(N)
search subsets

40
Practical implications of order-of-growth

problem size solvable in minutes


growth
rate
1970s 1980s 1990s 2000s

1 any any any any

log N any any any any

tens of hundreds of
N millions billions
millions millions

hundreds of hundreds of
N log N millions millions
thousands millions

tens of
N2 hundreds thousand thousands
thousands

N3 hundred hundreds thousand thousands

2N 20 20s 20s 30

Bottom line. Need linear or linearithmic alg to keep pace with Moore's law.
41
An N2 log N algorithm for 3-SUM

input
Sorting-based algorithm.
・Step 1:
30 -40 -20 -10 40 0 10 5
Sort the N (distinct) numbers.
・Step 2: For each pair of numbers a[i] sort
-40 -20 -10 0 5 10 30 40
and a[j], binary search for -(a[i] + a[j]).
binary search
(-40, -20) 60
(-40, -10) 50
(-40, 0) 40
(-40, 5) 35
(-40, 10) 30
⋮ ⋮
(-40, 40) 0
⋮ ⋮
(-20, -10) 30
only count if
⋮ ⋮
a[i] < a[j] < a[k]
(-10, 0) 10 to avoid
Analysis. Order of growth is N2 log N. ⋮ ⋮ double counting

・Step 1: N 2 with insertion sort. ( 10, 30) -40

・Step 2:
( 10, 40) -50
N 2 log N with binary search. ( 30, 40) -70

45
Comparing programs

Hypothesis. The sorting-based N 2 log N algorithm for 3-SUM is significantly


faster in practice than the brute-force N 3 algorithm.

N time (seconds) N time (seconds)

1,000 0.1 1,000 0.14

2,000 0.8 2,000 0.18

4,000 6.4 4,000 0.34

8,000 51.1 8,000 0.96

ThreeSum.java 16,000 3.67

32,000 14.88

64,000 59.16

ThreeSumDeluxe.java

Guiding principle. Typically, better order of growth ⇒ faster in practice.


46
Types of analyses

Best case. Lower bound on cost.


・Determined by “easiest” input.
・Provides a goal for all inputs.
Worst case. Upper bound on cost.
・Determined by “most difficult” input.
・Provides a guarantee for all inputs.
Average case. Expected cost for random input.
・Need a model for “random” input.
・Provides a way to predict performance.
Ex 1. Array accesses for brute-force 3-SUM. Ex 2. Compares for binary search.
Best: ~ ½ N3 Best: ~ 1
Average: ~ ½ N3 Average: ~ lg N
Worst: ~ ½ N3 Worst: ~ lg N
49
Types of analyses

Best case. Lower bound on cost.


Worst case. Upper bound on cost.
Average case. “Expected” cost.

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.

50
Theory of algorithms

Goals.
・Establish “difficulty” of a problem.
・Develop “optimal” algorithms.
Approach.
・Suppress details in analysis: analyze “to within a constant factor”.
・Eliminate variability in input model by focusing on the worst case.
Optimal algorithm.
・Performance guarantee (to within a constant factor) for any input.
・No algorithm can provide a better performance guarantee.

51
Commonly-used notations in the theory of algorithms

notation provides example shorthand for used to

⇤ N2
asymptotic 10 N2 classify
Big Theta ⇥(N2)
order of growth 5 N2 + 22 N log N + 3N algorithms

10 N2
100 N develop
Big Oh ⇥(N2) and smaller O(N2)
22 N log N + 3 N upper bounds

⇤ N2
N5 develop
Big Omega ⇥(N2) and larger Ω(N2)
N3 + 22 N log N + 3 N lower bounds

52
Theory of algorithms: example 1

Goals.
・Establish “difficulty” of a problem and develop “optimal” algorithms.
・Ex. 1-SUM = “Is there a 0 in the array? ”
Upper bound. A specific algorithm.
・Ex. Brute-force algorithm for 1-SUM: Look at every array entry.
・Running time of the optimal algorithm for 1-SUM is O(N ).
Lower bound. Proof that no algorithm can do better.
・Ex. Have to examine all N entries (any unexamined one might be 0).
・Running time of the optimal algorithm for 1-SUM is Ω(N ).
Optimal algorithm.
・Lower bound equals upper bound (to within a constant factor).
・Ex. Brute-force algorithm for 1-SUM is optimal: its running time is ⇥(N ).
53
Theory of algorithms: example 2

Goals.
・Establish “difficulty” of a problem and develop “optimal” algorithms.
・Ex. 3-SUM.
Upper bound. A specific algorithm.
・Ex. Brute-force algorithm for 3-SUM.
・Running time of the optimal algorithm for 3-SUM is O(N 3 ).

54
Theory of algorithms: example 2

Goals.
・Establish “difficulty” of a problem and develop “optimal” algorithms.
・Ex. 3-SUM.
Upper bound. A specific algorithm.
・Ex. Improved algorithm for 3-SUM.
・Running time of the optimal algorithm for 3-SUM is O(N 2 logN ).

Lower bound. Proof that no algorithm can do better.


・Ex. Have to examine all N entries to solve 3-SUM.
・Running time of the optimal algorithm for solving 3-SUM is Ω(N ).
Open problems.
・Optimal algorithm for 3-SUM?
・Subquadratic algorithm for 3-SUM?
・Quadratic lower bound for 3-SUM?
55

You might also like