Algorithm Analysis
Algorithm Analysis
● Issues:
• correctness
• time efficiency
• space efficiency
• optimality
● Approaches:
• empirical analysis – less useful
• theoretical analysis – most important
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 1
Empirical analysis of time efficiency
● Select a specific sample of inputs
● Problems:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2
Empirical analysis of time efficiency
● Select a specific sample of inputs
● Problem - Inefficient:
• Must implement algorithm
• Must run on many data sets to see effects of scaling
• Hard to see patterns in actual data
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3
Theoretical analysis of time efficiency
Time efficiency is analyzed by determining the number of
repetitions of the basic operation as a function of input size
T(n) ≈ copC(n)
running execution time Number of times
time for basic basic operation is
operation executed
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4
Input size and basic operation examples
Visiting a vertex or
Typical graph problem #vertices and/or edges
traversing an edge
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 5
Counting Operations
Consider counting steps in FindMax
● Problems:
• Hard to analyze
• May not need precise information
– Precise details less relevant than order growth
– May not know times (or relative times) of steps
– Only gives results within constants (constants relevant later)
– aC(n) < T(n) < bC(n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 6
Best-case, average-case, worst-case
For a given input size, how does algorithm perform
on different datasets of that size
For datasets of size n identify different datasets that give:
● Worst case: Cworst(n) – maximum over all inputs of size n
● Best case: Cbest(n) – minimum over all inputs of size n
● Some algs are same for all three (eg all case performance)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 7
Example: Sequential search
● Worst case
● Best case
● Average case: depends on assumputions about input: ?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 8
Example: Sequential search
● Worst case
● Best case
● Average case: depends on assumputions about input:
proportion of found vs not-found keys
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 9
Example: Find maximum
● Worst case:
● Best case:
● Average case:
● All case:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10
Critical factor for analysis: Growth rate
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 11
Growth rate: critical for performance performance
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 13
Basic asymptotic efficiency classes
1 constant Best case
log n logarithmic Divide
Ignore part
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 15
Order of growth: Upper, tight, lower bounds
More formally:
- Θ(g(n)): class of functions f(n) that grow at same rate as g(n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 16
t(n) ∈ O(g(n)) iff t(n) <=cg(n) for n > n0
t(n) = 10n3 in O(n3) and in O(n5). What c and n0? More later.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 17
t(n) ∈ Ω(g(n)) iff t(n) >=cg(n) for n > n0
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 18
t(n)∈ Θ(g(n)) iff t(n)∈O(g(n)) and
∈Ω(g(n))
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 19
Terminology
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 20
Big O, Ω, Θ : Informal Definitions
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 21
Big O, Ω, Θ : Formal Definitions
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 22
Big O, Ω, Θ: Why Constant c
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 23
Big O, Ω, Θ: Why Constant c
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 24
Using the Definition: Big O
Examples:
● 10n is O(n2)
• [Can choose c and n0. Solve for 2 different c’s]
Examples:
● 10n2 is Ω(n)
● 5n + 20 is Ω(n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 26
Using the Definition: Theta
Examples:
● 10n2 is Θ(n2)
● 5n + 20 is Θ(n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 27
Some properties of asymptotic order of growth
● f(n) ∈ O(f(n))
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 28
Establishing order of growth using limits
Examples:
• 10n vs. n2
• n(n+1)/2 vs. n2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 29
Big O, Little o. Big and little Omega
Examples:
• 10n vs. n2
• 10n is O(n^2) and o(n^2)
• 10n is O(n) but not o(n)
Big O: upper bound or the same
•Little o: upper bound and not the same
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 30
L’Hôpital’s rule and Stirling’s formula
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 31
Orders of growth of some important functions
● order log n < order nα (α>0) < order an < order n! < order n
• Order n log n ?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 32
Basic asymptotic efficiency classes
1 constant Best case
log n logarithmic Divide
Ignore part
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 34
Time efficiency of nonrecursive algorithms
General Plan for Analysis
● Decide on parameter n indicating input size
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 35
Useful summation formulas and rules
Σl≤i≤u1 = 1+1+ ⋯ +1 = u - l + 1
In particular, Σl≤i≤u1 = n - 1 + 1 = n ∈ Θ(n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 36
Example: Sequential search
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 37
Example 1: Maximum element
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 38
Example 2: Element uniqueness problem
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 39
Example 3: Matrix multiplication
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 40
Example 4: Gaussian elimination
Algorithm GaussianElimination(A[0..n-1,0..n])
//Implements Gaussian elimination of an n-by-(n+1) matrix A
for i ← 0 to n - 2 do
for j ← i + 1 to n - 1 do
for k ← i to n do
A[j,k] ← A[j,k] - A[i,k] * A[j,i] / A[i,i]
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 41
Example 5: Counting binary digits
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 42
Plan for Analysis of Recursive Algorithms
● Decide on a parameter indicating an input’s size.
● Solve the recurrence (ie find a closed form or, at the very
least, establish its solution’s order of growth) by backward
substitutions or another method.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 43
Recurrences
Size:
Basic operation:
Recurrence relation:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 45
Solving the recurrence for M(n)
Solution Methods:
- Guess?
- Forward Substitution?
- Backward Substitution?
- General Methods?
Guess closed form: ?
How to check a possible solution?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 46
Check a possible solution with Substitution
M(n) = ??
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 48
Example 2: The Tower of Hanoi Puzzle
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 49
Solving recurrence for number of moves
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 50
Tree of calls for the Tower of Hanoi Puzzle
M(n) = M(?) ?,
M(?) = ?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 52
General Methods
Forward and Backward Substitution problems:
solution must be proved
solution may be difficult to find
General Methods:
Apply to certain classes of recurrences
Solution always possible within the class
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 53
Fibonacci numbers
The Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, …
The Fibonacci recurrence:
F(n) = F(n-1) + F(n-2)
F(0) = 0
F(1) = 1
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 55
Application to the Fibonacci numbers
Characteristic equation:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 56
Computing Fibonacci numbers
1. Definition-based recursive algorithm
F(n-1) F(n) 0 1 n
=
F(n) F(n+1) 1 1
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 57