1.4. Analysis of Algorithms
1.4. Analysis of Algorithms
Theoretician wants
to understand.
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)
y
have to turn the crank?
Analytic Engine
4
Reasons to analyze algorithms
Predict performance.
Provide guarantees.
5
Example: 3-SUM
3-SUM. Given N distinct integers, how many triples sum to exactly zero?
4 -10 0 10 0
Run the program for various input sizes and measure running time.
time (seconds) t
250 0.0
500 0.0
1,000 0.1
2,000 0.8
4,000 6.4
8,000 51.1
16,000 7
16
Doubling hypothesis
20
Experimental algorithmics
determines constant
System dependent effects. in power law
• Hardware: CPU, memory, cache, ...
• Software: compiler, interpreter, garbage collector, ...
• System: operating system, network, other apps, ...
22
Activity 1
• Suppose that n equals 1 million. Approximately how much faster is an
algorithm that performs nlgn operations versus one that
performs n^2 operations? Recall that lg is the base‐2 logarithm
function.
a) 20x
b) 1,000x
c) 50,000x
d) 1,000,000x
Activity 2
• Suppose that you make the following observations of the running
time T(n) (in seconds) of a program as a function of the input size n.
Which of the following functions best models the running time T(n)?
n T(n) a) 3.3 × 10 × n
1,000 0.0
b 𝑛
c) 5* 10 × 𝑛
2,000 0.0
d) 6.25 × 10 × 𝑛
4,000 0.1
8,000 0.3
16,000 1.3
32,000 5.1
64,000 20.5
Mathematical models for running time
24
Cost of basic operations
assignment statement a = b c2
In practice,
• Formulas can be complicated.
• Advanced mathematics might be required.
• Exact models best left for experts.
�/\��
TN = c1 A + c2 B + c3 C + c4 D + cs E
A = array access
B = integer add � frequencies
C = integer compare
7
(depend on algorithm, input)
D = increment
E = variable assignment
40
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: focus on the worst case.
52
53
Theory of algorithms: example 1
Goals.
• Establish "difficulty" of a problem and develop "optimal" algorithms.
• Ex. 1-SUM = "Is there a O in the array?"
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 E>(N).
54
Big-O Notation
Specifically, Big-O is defined as follows:
Given functions f(n) and g(n), we say that f(n) is O(g(n)) if
there are positive constants c and n0 such that
55
Big-O Notation
Example 1:
What is O() if f(n) = 2n + 10?
◦ 2n 2n for n 0
◦ 10 10n for n 1
56
Big-O Notation
Important Note: Big-O only gives an upper bound of the
algorithm.
However, if f(n) is O(n), then it is also
O(n + 10), O(n3), O(n2 + 5n + 7), O(n10), etc.
We, generally, choose the smallest element from this
hierarchy of orders.
58
Big-Omega
big-Omega
Big-Omega is defined as follows: Given functions f(n) and
g(n), we say that f(n) is (g(n)) if there are positive constants
c and n0 such that
f(n) cg(n) for n n0
59
Big-Omega
60
Big-
Example: What are O() and ()
if f(n) = 2n + 10?
As seen before, the method is O(n).
Now,
◦ 2n + 10 2n n for n 0
So, for any n 0
◦ 2n + 10 n consider c = 1, n0 = 0 (n) = n
61
Big-Theta
62
Big-
Example 2 (Revised Again): What is () of the following
function:
f(n) = 3n4 + 6n3 + 10n2 + 5n + 4?
63
Big-O, Big- & Big-
Quick Examples
5n2 is (n2)
f(n) is (g(n)) if there is a constant c > 0 and an integer
constant n0 1 such that f(n) c•g(n) for n n0
let c = 5 and n0 = 1
5n2 is (n)
f(n) is (g(n)) if there is a constant c > 0 and an integer
constant n0 1 such that f(n) c•g(n) for n n0
let c = 1 and n0 = 1
64
Big-O, Big- & Big-
Quick Examples
5n2 is (n2)
f(n) is (g(n)) if it is (n2) and O(n2). We have already seen
the former, for the latter recall that f(n) is O(g(n)) if there is
a constant c > 0 and an integer constant n0 1 such that
f(n) < c•g(n) for n n0
Let c = 5 and n0 = 1
65
Theory of algorithms: example 2
Goals.
• Establish "difficulty" of a problem and develop "optimal" algorithms.
• Ex. 3-SUM.
66
Theory of algorithms: example 2
Goals.
• Establish "difficulty" of a problem and develop "optimal" algorithms.
• Ex. 3-SUM.
Open problems.
• Optimal algorithm for 3-SuM?
• Subquadratic algorithm for 3-SuM?
• Quadratic lower bound for 3-SuM?
67
Algorithm design approach
Start.
• Develop an algorithm.
• Prove a lower bound.
Gap?
• Lower the upper bound (discover a new algorithm).
• Raise the lower bound (more difficult).
Caveats.
• Overly pessimistic to focus on worst case?
• Need better than "to within a constant factor" to predict performance.
68
Commonly-used notations in the theory of algorithms
10N2 provide
Tilde leading term ~ 10N2 10 N 2 + 22Nlog N approximate
10N 2 + 2N + 37 model
½N2
asymptotic classify
Big Theta 0(N2) 10N 2
order of growth algorithms
5 N 2 + 22 Nlog N+ 3N
10N 2
develop
Big Oh 0(N2) and smaller O(N2) l00N
upper bounds
22NlogN+ 3 N
½N 2
develop
Big Omega 0(N2) and larger Q(N2) NS
lower bounds
N 3 + 22 Nlog N+ 3 N
Byte. 8 bits.
Megabyte (MB). 1 million or 2 2 0 bytes.
Gigabyte (GB). 1 billion or 2 30 bytes.
71
Typical memory usage for primitive types and arrays
float 4
long 8
type bytes
double 8
char[][] ~2MN
primitive types
int[][] ~4MN
double[][] ~8MN
two-dimensional arrays
72
Typical memory usage for obiects in Java
32 bytes
73
Typical memory usage summary
74
Example
Empirical analysis.
• Execute program to perform experiments.
• Assume power law and 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.
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.
76