04-Analysis Of Algorithms
04-Analysis Of Algorithms
und Algorithmen
Analysis of Algorithms
1.4 A NALYSIS OF A LGORITHMS
h tt p : / / a l g s 4 . c s . p r i n c e t o n . e d u
2
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
will arise—By what course of calculation can these results be arrived at by
the machine in the shortest time? ” — Charles Babbage (1864)
Analytic Engine
3
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
will arise—By what course of calculation can these results be arrived at by
the machine in the shortest time? ” — Charles Babbage (1864)
Analytic Engine
3
Cast of characters
4
Cast of characters
Programmer needs to develop
a working solution.
4
Cast of characters
Programmer needs to develop
a working solution.
4
fi
Cast of characters
Programmer needs to develop
a working solution.
Theoretician wants
to understand.
4
fi
Cast of characters
Programmer needs to develop
a working solution.
Theoretician wants
to understand.
4
fi
Predict performance.
Provide guarantees.
5
Discrete Fourier transform.
• Break down waveform of N samples into periodic components.
32T
16T
linearithmic
8T
linear
size 1K 2K 4K 8K 6
N-body simulation
• Simulate gravitational interactions among N bodies.
7
N-body simulation
• Simulate gravitational interactions among N bodies.
7
The challenge
8
The challenge
Why is my program so slow ?
8
The challenge
Why is my program so slow ? Why does it run out of memory ?
8
The challenge
Why is my program so slow ? Why does it run out of memory ?
9
fi
Scienti c method
9
fi
Scienti c method
9
fi
Scienti c method
9
fi
Scienti c method
9
fi
Scienti c method
9
fi
Principles
10
fi
Example: 3-Sum
Given N distinct integers, how many triples sum to exactly zero?
% cat 8ints.txt
a[i] a[j] a[k] sum
30 -40 -20 -10 40 0 10 5
30 -40 10 0
-10 0 10 0
11
Example: 3-Sum
Given N distinct integers, how many triples sum to exactly zero?
% cat 8ints.txt
a[i] a[j] a[k] sum
30 -40 -20 -10 40 0 10 5
30 -40 10 0
-10 0 10 0
11
3-Sum
brute-force algorithm
import sys
import DSA
def count(a):
N = len(a)
count = 0
for i in range(0,N):
for j in range(i+1,N):
for k in range(j+1,N):
if a[i] + a[j] + a[k] == 0: check each triple
count+=1
return count
f = DSA.In(sys.argv[1])
a = f.readAllInts()
print(count(a))
12
Q: How to time a program? Manual!
% python3 3Sum.py 1Kints.txt
70
% python3 3Sum.py 2Kints.txt
528
% python3 3Sum.py 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
% python3 3Sum.py 1Kints.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
70 tick tick tick tick tick tick tick tick
% python3 3Sum.py 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
tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick
tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick tick
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 tick tick tick tick tick tick tick tick
% python3 3Sum.py 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 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 13
Automatic
class Stopwatch (part of DSA.py )
f = DSA.In(sys.argv[1])
a = f.readAllInts()
s = DSA.Stopwatch()
c = count(a)
time = s.elapsedTime()
print("elapsed time:",time,"seconds")
print(c)
client code
14
Empirical analysis
15
Empirical analysis
15
Empirical analysis
N time (seconds) †
250 0
8.000 51,1
16.000 ?
16
Standard plot. Plot running time T(N) vs. input size N.
40 12.8
lg(T(N))
30 3.2
1.6
20 .8
.4
10 .2
.1
1K 2K 4K 8K 1K 2K 4K 8K
problem size N lg N
Analysis of experimental data (the running time of ThreeSum)
17
Log-log plot: Plot running time T(N) vs. input size N using log-log scale.
lg (T (N)) = b ⋅ lg (N) + c
12.8
6.4
b = 2.999
lg(T(N))
3.2
1.6
c = − 33.2103
.8
b c
.4 T (N) = a ⋅ N , where a = 2
.2
.1
1K 2K 4K 8K 1K 2K 4K 8K
problem size N lg N
Analysis of experimental data (the running time of ThreeSum)
b
Regression: Fit straight line through data points: a ⋅ N .
−10 2.999
Hypothesis: The running time is about 1.006 ⋅ 10 ⋅N seconds.
18
−10 2.999
Hypothesis: The running time is about 1.006 ⋅ 10 ⋅N seconds.
Predictions:
8.000 51,1
8.000 51
8.000 51,1
16.000 410,8
validates hypothesis!
19
Doubling hypothesis
Run program, doubling the size of the input.
b
T (2N) a ⋅ (2N) b
= = 2
T (N) a⋅N b
20
b
Doubling Hypothesis: Running time is about a ⋅ N with b = lg(ratio)
250 0 –
b
T (2N) a ⋅ (2N)
500 0 4,8 2,3
b
1.000 0,1 6,9 2,8 = = 2
T (N) a ⋅ Nb
2.000 0,8 7,7 2,9
8.000 51,1 8 3
21
Experimental algorithmics
• System independent effects:
• Algorithm
determines exponent
determines
• System dependent effects. constant in power
law
• Hardware: CPU, memory, cache, …
23
Donald Knuth’s approach
Total running time:
sum of cost * frequency for operations.
24
Challenge: How to estimate
constants.
operation example nanoseconds †
assignment statement a = b c2
operation frequency
variable declaration 2
count = 0 assignment statement 2
for i in range(0,N):
if a[i] == 0: less than compare N+1
count+=1
equal to compare N
array access N
27
2-Sum
count = 0
for i in range(0,N):
for j in range(i+1,N):
if a[i] + a[j] == 0:
count+=1
28
2-Sum
count = 0
for i in range(0,N):
for j in range(i+1,N):
if a[i] + a[j] == 0:
count+=1
1
0 + 1 + 2 + . . . + (N 1) = N (N 1)
2 ⇥
N
=
2
28
In nite Sum
https://www.youtube.com/watch?v=w-I6XTVZXww
29
fi
2-Sum
operation frequency
array access N (N − 1)
increment ½ N (N − 1) to N (N − 1)
30
2-Sum
operation frequency
30
“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
Downlo
THIS paper contains descriptions of a number of methods for solving sets 31
Simpli cation 1 ”Cost Model“
32
fi
Cost model
count = 0
for i in range(0,N):
for j in range(i+1,N):
if a[i] + a[j] == 0:
count+=1
operation frequency
range accesses ½ (N + 1) (N + 2)
equal to compare ½ N (N − 1)
array access N (N − 1)
cost model = array accesses
increment ½ N (N − 1) to N (N − 1)
(we assume compiler or runtime do not
33
Cost model
count = 0
for i in range(0,N):
for j in range(i+1,N):
if a[i] + a[j] == 0:
count+=1
operation frequency
equal to compare ½ N (N − 1)
array access N (N − 1)
cost model = array accesses
increment ½ N (N − 1) to N (N − 1)
(we assume compiler or runtime do not
33
Simpli cation 2 ”Tilde Notation“
34
fi
Tilde Notation
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 ~ ⅙N3
166,167,000
Ex 3: ⅙N3 - ½N 2 + ⅓ N ~ ⅙N3
N 1,000
Leading-term approximation
35
fi
Tilde Notation
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 ~ ⅙N3
166,167,000
Ex 3: ⅙N3 - ½N 2 + ⅓ N ~ ⅙N3
N 1,000
Leading-term approximation
f (N)
Technical de nition. f(N) ~ g(N) means lim = 1
N → ∞ g(N)
35
fi
Tilde Notation
operation frequency tilde notation
increment ½ N (N − 1) to N (N − 1) ~ ½ N 2 to ~ N 2
36
2-Sum
Q: Approximately how many array
accesses as a function of input size N ?
count = 0
for i in range(0,N):
for j in range(i+1,N):
if a[i] + a[j] == 0: "inner loop"
count+=1
A: ~ N2 array accesses.
37
2-Sum
Q: Approximately how many array
accesses as a function of input size N ?
count = 0
for i in range(0,N):
for j in range(i+1,N):
if a[i] + a[j] == 0: "inner loop"
count+=1
1
0 + 1 + 2 + . . . + (N 1) = N (N 1)
2 ⇥
N
=
2
A: ~ N2 array accesses.
37
Bottom line:
Use cost model and tilde
notation to simplify counts!
38
3-Sum
Q: Approximately how many array accesses
as a function of input size N ?
count = 0
for i in range(0,N):
for j in range(i+1,N):
for k in range(j+1,N):
if a[i] + a[j] + a[k] == 0: "inner loop"
count+=1
(3)
N N (N − 1) (N − 2) 1 3
= ∼ N
3! 6
A: ~ ½N 3 array accesses.
39
Q: How to estimate a discrete sum?
• A1: Take a discrete mathematics course.
• A2: Replace the sum with an integral, and use calculus
(doesn’t always work)!
N N
1 2
∑ ∫x=1
Ex 1. 1 + 2 + … + N. i∼ x dx ∼ N
i=1
2
N N
1
∫x=1
ik ∼ x k dx ∼ N k+1
Ex 2. 1k + 2k + … + N k. ∑ k + 1
i=1
N N
1 1
∑i ∫x=1 x
Ex 3. 1 + 1/2 + 1/3 + … + 1/N. ∼ dx ∼ ln N
i=1
40
A3: Use Maple, Wolfram Alpha etc.
41
A3: Use Maple, Wolfram Alpha etc.
wolframalpha.com
41
A3: Use Maple, Wolfram Alpha etc.
wolframalpha.com
N (N - 1) (N - 2)
-----------------
6
41
• In principle, accurate mathematical models are available.
• In practice,
• Formulas can be complicated.
• Advanced mathematics might be required.
• Exact models best left for experts.
• Bottom line: We use approximate models in this course:
T(N) ~ c N .
3
42
Order-of-growth
Classi cations
43
fi
Common order-of-growth
classi cations
De nition. If f(N) ~ c g(N) for some constant c > 0, then the order
of growth of f(N) is g(N).
• Ignores leading coef cient.
• Ignores lower-order terms.
44
fi
fi
fi
Example
count = 0
for i in range(0,N):
for j in range(i+1,N):
for k in range(j+1,N):
if a[i] + a[j] + a[k] == 0:
count+=1
45
time
Common Classes
200T
100T
logarithmic
constant
Good news: The set of functions: 1, log N, N, N log N, N 2, N 3, and 2N
suf ces to describe the order of growth of most common algorithms.
100K 200K 500K
size
log-log plot
512T
exponential
ic
ic
cubi
hm
rat
r
rit
d
ea
qua
ea
lin
lin
64T
time
8T
4T
2T
logarithmic
T
constant
1K 2K 4K 8K size 512K
while N > 1:
log N logarithmic divide in half binary search ~1
N = N // 2 ...
tens of hundreds of
N millions billions minutes seconds second "instant"
millions millions
tens of
N2 hundreds thousand thousands decades years months weeks
thousands
48
Binary search
• Goal: Given a sorted array and a key, nd index of the key
in the array?
• Idea: Compare key against middle entry.
• Too small, go left.
• Too big, go right.
• Equal, found.
49
fi
Binary search demo
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
lo hi
50
Binary search demo
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
lo mid hi
50
Binary search demo
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
lo hi
51
Binary search demo
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
lo mid hi
51
Binary search demo
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
lo hi
52
Binary search demo
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
lo mid hi
52
Binary search demo
lo = hi
successful search for 33
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
53
Binary search demo
lo = hi
successful search for 33
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
mid
return 4
53
Binary search demo
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
lo hi
54
Binary search demo
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
lo mid hi
54
Binary search demo
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
lo hi
55
Binary search demo
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
lo mid hi
55
Binary search demo
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
lo hi
56
Binary search demo
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
lo mid hi
56
Binary search demo
lo = hi
unsuccessful search for 34
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
57
Binary search demo
lo = hi
unsuccessful search for 34
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
mid
return -1
57
Binary search implementation
Trivial to implement?
58
Binary search implementation
Trivial to implement?
• First binary search published in 1946.
58
Binary search implementation
Trivial to implement?
• First binary search published in 1946.
• First bug-free one in 1962.
58
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.
58
Binary search: Python3
def binSearch(a,e):
lo = 0
hi = len(a)-1
while hi >= lo:
mid = lo+(hi-lo)//2
if e > a[mid]:
lo = mid+1
elif e < a[mid]:
hi = mid-1
else:
return mid
return -1
Invariant: If key appears in the array a[], then a[lo] ≤ key ≤ a[hi].
59
Mathematical analysis
Proposition: Binary search uses at most 1 + lg N key compares to search
in a sorted array of size N.
60
Proof Sketch
T (N) ≤ T (N // 2) + 1 [ given ]
61
Proof Sketch
T (N) ≤ T (N // 2) + 1 [ given ]
≤ T (N // 4) + 1 + 1 [ apply recurrence to rst term ]
61
fi
Proof Sketch
T (N) ≤ T (N // 2) + 1 [ given ]
≤ T (N // 4) + 1 + 1 [ apply recurrence to rst term ]
≤ T (N // 8) + 1 + 1 + 1 [ apply recurrence to rst term ]
61
fi
fi
Proof Sketch
T (N) ≤ T (N // 2) + 1 [ given ]
≤ T (N // 4) + 1 + 1 [ apply recurrence to rst term ]
≤ T (N // 8) + 1 + 1 + 1 [ apply recurrence to rst term ]
⋮
61
fi
fi
Proof Sketch
T (N) ≤ T (N // 2) + 1 [ given ]
≤ T (N // 4) + 1 + 1 [ apply recurrence to rst term ]
≤ T (N // 8) + 1 + 1 + 1 [ apply recurrence to rst term ]
⋮
≤ T (N // N) + 1 + 1 + … + 1 [ stop applying, T(1) = 1 ]
61
fi
fi
Proof Sketch
T (N) ≤ T (N // 2) + 1 [ given ]
≤ T (N // 4) + 1 + 1 [ apply recurrence to rst term ]
≤ T (N // 8) + 1 + 1 + 1 [ apply recurrence to rst term ]
⋮
≤ T (N // N) + 1 + 1 + … + 1 [ stop applying, T(1) = 1 ]
= 1 + lg N
61
fi
fi
N 2 log N algorithm for 3-Sum
input
62
N 2 log N algorithm for 3-Sum
input
62
N 2 log N algorithm for 3-Sum
binary search
sort (-40, -20) 60
(-20, -10) 30
• Step 1: Sort the N (distinct) numbers. only count if
⋮ ⋮
a[i] < a[j] < a[k]
• Step 2: For each pair of numbers a[i] (-10, 0) 10 to avoid
and a[j], binary search for -(a[i] + a[j]).
⋮ ⋮ double counting
62
Analysis
32.000 14,88
64.000 59,16
3Sum-fast.py
64
Theory of algorithms
65
Types of analyses
• Best case
Lower bound on cost
• Worst case
Upper bound on cost
• Average case
Expected cost for random input
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
66
Types of analyses
• Best case
Lower bound on cost
• Worst case
Upper bound on cost
this course
• Average case
Expected cost for random input
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
66
Theory of algorithms
• Goals:
• Approach:
67
fi
Theory of algorithms
• Upper bound:
Performance guarantee of algorithm for any input.
• Lower bound:
Proof that no algorithm can do better.
• Optimal algorithm:
Lower bound = upper bound (to within a constant factor).
68
Commonly-used notations in the theory of
algorithms
notation provides example shorthand for used to
½ N2
asymptotic 10 N 2 classify
Big Theta Θ(N2)
order of growth 5 N 2 + 22 N log N + 3N algorithms
⋮
10 N 2
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)
N 3 + 22 N log N + 3 N lower bounds
⋮
69
Example 1
1-Sum
“Is there a 0 in the array?”
70
• Upper bound: A speci c algorithm.
71
fi
• Upper bound: A speci c algorithm.
• Optimal algorithm.
71
fi
• Upper bound: A speci c algorithm.
• Optimal algorithm.
71
fi
Example 2
3-Sum
72
• Upper bound: A speci c algorithm.
73
fi
• Upper bound: A speci c algorithm.
74
fi
• Upper bound: A speci c algorithm.
75
fi
• Upper bound: A speci c algorithm.
• Open problems:
75
fi
Algorithm Design
76
Algorithm Design
Develop a new Algorithm
76
Algorithm Design
Develop a new Algorithm
76
Algorithm Design
Develop a new Algorithm
Gap?
76
Algorithm Design
Lower the upper bound
Develop a new Algorithm (discover a new algorithm).
Gap?
76
Algorithm Design
Lower the upper bound
Develop a new Algorithm (discover a new algorithm).
Gap?
76
fi
Golden Age of Algorithm Design
• 1970s
• Steadily decreasing upper bounds for many important problems.
• Many known optimal algorithms.
77
‑
Caveats
78
Commonly-used notations in the theory of
algorithms
notation provides example shorthand for used to
10 N 2
provide
Tilde leading term ~ 10 N 2 10 N 2 + 22 N log N
approximate model
10 N 2 + 2 N + 37
½ N2
asymptotic classify
Big Theta Θ(N2) 10 N 2
order of growth algorithms
5 N 2 + 22 N log N + 3N
10 N 2
develop
Big Oh Θ(N2) and smaller O(N2) 100 N
upper bounds
22 N log N + 3 N
½N2
develop
Big Omega Θ(N2) and larger Ω(N2) N5
lower bounds
N 3+ 22 N log N + 3 N
79
Turning the crank: summary
80
Summary
Empirical analysis.
• Execute program to perform
experiments.
• Assume power law and formulate a
hypothesis for running time.
• Model enables us to make predictions.
81
Summary
Mathematical analysis.
• Analyze algorithm to count frequency of
operations.
• Use tilde notation to simplify analysis.
• Model enables us to explain behavior.
82
Summary
Scienti c 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.
83
fi
R EADING L IST :
h tt p : / / a l g s 4 . c s . p r i n c e t o n . e d u
84