Chapter 2: Fundamentals of The Analysis of Algorithm Efficiency
Chapter 2: Fundamentals of The Analysis of Algorithm Efficiency
16
Number of Bits in an Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Mathematical Analysis of Recursive Algorithms 18
Chapter 2: Fundamentals of the Analysis of Plan for Analyzing Recursive Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Factorial Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Algorithm Efficiency Recurrence for Factorial Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Solving the Factorial Recurrence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
The first rule of any technology used in a busi- Towers of Hanoi Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Recurrence for Towers of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
ness is that automation applied to an efficient Solving the Towers of Hanoi Recurrence . . . . . . . . . . . . . . . . . . . . . . . . . . 24
operation will magnify the efficiency. The sec- Number of Bits in an Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
ond is that automation applied to an inefficient Recurrence for BitCount Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Solving the BitCount Recurrence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
operation will magnify the inefficiency. (Bill
Fibonacci Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Gates) Recurrence for Fibonacci Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Approximating the Fibonacci Recurrence . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Empirical Analysis of Algorithms 31
Plan for Empirical Analysis of Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . 31
Empirical Analysis of UniqueElements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Analysis Framework 2 Results of Empirical Analysis: Comparisons . . . . . . . . . . . . . . . . . . . . . . . . 33
Analysis of Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Results of Empirical Analysis: Timings. . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Efficiency as a Function of Input Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Algorithm Visualization 35
Measuring Running Time. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Visualization of Sorting Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
worst/best/average. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Asymptotic Notation 6
Order of Growth. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Asymptotic Order of Growth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Illustration of Asymptotic Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Examples of Asymptotic Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Properties of Order of Growth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Basic Asymptotic Efficiency Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Mathematical Analysis of Nonrecursive Algorithms 12
Plan for Analyzing Nonrecursive Algorithms . . . . . . . . . . . . . . . . . . . . . . . . 12
Useful Summation Formulas and Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Finding the Maximum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Element Uniqueness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1 2
Analysis Framework 2
Measuring Running Time
Analyze time efficiency by:
Analysis of Algorithms
identifying the basic operation(s), the operation(s) contributing the most to
Issues: running time,
– Correctness. Is the algorithm correct? characterizing the number of times it is performed as a function of input size.
– Time Efficiency. How much time does the algorithm use? We can crudely estimate running time by T (n) ≈ cop ∗ C(n)
– Space Efficiency. How much extra space (in addition to the space needed
for the input and output) does the algorithm use? T (n): running time as a function of n.
cop : running time of a single basic operation.
Approaches:
C(n): number of basic operations as a function of n.
– Theoretical Analysis. Proof of correctness and big-Oh and related
CS 3343 Analysis of Algorithms Chapter 2: Slide – 4
notations.
– Empirical Analysis. Testing and measurement over a range of instances.
CS 3343 Analysis of Algorithms Chapter 2: Slide – 2
Worst-Case, Best-Case, and Average-Case
algorithm SequentialSearch(A[0..n − 1], K)
Efficiency as a Function of Input Size // Searches for a value in an array
// Input: An array A and a search key K
Typically, more time and space are needed to run an algorithm on bigger inputs // Output: The index where K is found or −1
(e.g., more numbers, longer strings, larger graphs). for i ← 0 to n − 1 do
if A[i] = K then return i
Analyze efficiency as a function of n=size of input.
return −1
Searching/sorting. n=number of items in list.
String processing. n = length of string(s). Basic Operation: The comparison in the loop
Matrix operations, n = dimension of matrix. n × n matrix has n2 elements. Worst-Case: n comparisons
Graph processing. nV = number of vertices and nE = number of edges. Best-Case: 1 comparison
CS 3343 Analysis of Algorithms Chapter 2: Slide – 3 Average-Case: (n+1)/2 comparisons assuming each element equally likely to
be searched.
CS 3343 Analysis of Algorithms Chapter 2: Slide – 5
3 4
Asymptotic Notation 6
Illustration of Asymptotic Order
20
n + 3 sin n
Order of Growth 2n
15 n/2
functions on n
Typically, the basic operation count can be approximated as c g(n), where g(n) is
the order of growth, often some “simple” function such as: 10
0
0 5 10 15 20
n
5 6
Properties of Order of Growth Mathematical Analysis of Nonrecursive Algorithms 12
If f1(n) is order g1(n), and f2(n) is order g2(n), then f1 (n) + f2(n) is order
Plan for Analyzing Nonrecursive Algorithms
max(g1 (n), g2(n)).
If b > 1, then logb n ∈ Θ(log n). Decide on parameter n indicating input size.
Polynomials of degree k ∈ Θ(nk ), that is, ak nk + ak−1nk−1 + · · · + a0 ∈ Θ(nk ). Identify algorithm’s basic operation(s).
Exponential functions an have different orders of growth for different a’s. Determine worst, average, and best cases for input of size n.
order log n < order nk where k > 0 < order an where a > 1 < order n! < Set up a sum for the number of times the basic operation is executed.
order nn Simplify the sum using standard formulas and rules (see Appendix A).
CS 3343 Analysis of Algorithms Chapter 2: Slide – 10 CS 3343 Analysis of Algorithms Chapter 2: Slide – 12
7 8
Finding the Maximum Matrix Multiplication
Element Uniqueness
Number of Bits in an Integer
algorithm UniqueElements(A[0..n − 1])
// Returns true if all elements are different algorithm BitCount(m)
// Input: An array A // Input: A positive integer m
// Output: Returns true if there are no duplicates // Output: The number of bits to encode m
for i ← 0 to n − 2 do count ← 1
for j ← i + 1 to n − 1 do while m > 1 do
if A[i] = A[j] then return false count ← count + 1
return true m ← ⌊m/2⌋
return count
Basic Operation: inner loop comparison n = length of m in bits
Best/Worst-Case: from 1 to n(n − 1)/2 Basic Operation: The division by 2 in loop
X
n−2 X
n−1 Performs n − 1 operations because n = count.
n−(i+1) = (n−1)+(n−2)+· · ·+1 = k = n(n−1)/2 CS 3343 Analysis of Algorithms Chapter 2: Slide – 17
i=0 k=1
CS 3343 Analysis of Algorithms Chapter 2: Slide – 15
9 10
Mathematical Analysis of Recursive Algorithms 18
Solving the Factorial Recurrence
Make a reasonable guess.
Plan for Analyzing Recursive Algorithms
Forward substitution. M(1) = M(0) + 1 = 1
Decide on parameter n indicating input size.
M(2) = M(1) + 1 = 2
Identify algorithm’s basic operation(s). M(3) = M(2) + 1 = 3
Determine worst, average, and best cases for input of size n. Backward substitution. M(n) = M(n − 1) + 1
= [M(n − 2) + 1] + 1 = M(n − 2) + 2
Set up a recurrence relation expressing the basic operation count. = [M(n − 3) + 1] + 2 = M(n − 3) + 3
Solve the recurrence (at least determine it’s order of growth). Prove M(n) = n by mathematical induction.
CS 3343 Analysis of Algorithms Chapter 2: Slide – 18
Basis: if n = 0, then M(n) = M(0) = 0 = n
Induction: if M(n − 1) = n − 1, then
Factorial Function M(n) = M(n − 1) + 1 = (n − 1) + 1 = n
CS 3343 Analysis of Algorithms Chapter 2: Slide – 21
algorithm Factorial(n)
// Computes n! recursively Towers of Hanoi Algorithm
// Input: A nonnegative integer n
// Output: The value of n! algorithm Towers(n, i, j)
if n = 0 then return 1 // Moves n disks from peg i to peg j
else return Factorial(n − 1) ∗ n // Input: Integers n > 0, 1 ≤ i, j ≤ 3, i 6= j
Input Size: Use number n (actually n has about log2 n bits) // Output: Specifies disk moves in correct order
Basic Operation: multiplication if n = 1 then
CS 3343 Analysis of Algorithms Chapter 2: Slide – 19 move disk 1 from peg i to peg j
else
Recurrence for Factorial Function Towers(n−1, i, 6−i−j)
move disk n from peg i to peg j
Let M(n) = multiplication count to compute Factorial(n). Towers(n−1, 6−i−j, j)
M(0) = 0 because no multiplications are performed to compute Factorial(0). Input Size: Use number n
If n > 0, then Factorial(n) performs recursive call plus one multiplication. Basic Operation: moving a disk
CS 3343 Analysis of Algorithms Chapter 2: Slide – 22
M(n) = M(n − 1) + 1
to compute to multiply
Factorial(n − 1) Factorial(n − 1) by n
11 12
Recurrence for Towers of Hanoi Number of Bits in an Integer
Let M(n) = move count to compute Towers(n, ·, ·).
algorithm BitCount(n)
M(1) = 1 because 1 move is needed for Towers(1, ·, ·).
// Input: A positive integer n
If n > 1, then Towers(n, ·, ·) performs 2 recursive calls plus one move. // Output: The number of bits to encode n
if m = 1 then return 1
M(n) = 2M(n − 1) + 1 else return BitCount(⌊n/2⌋) + 1
to compute to move
Towers(n−1, ·, ·) disk n Input Size: Use number n (actually n has about log2 n bits)
twice Basic Operation: division by 2
CS 3343 Analysis of Algorithms Chapter 2: Slide – 25
CS 3343 Analysis of Algorithms Chapter 2: Slide – 23
13 14
Solving the BitCount Recurrence Recurrence for Fibonacci Function
Make a reasonable guess using powers of 2. Let A(n) = addition count to compute F (n).
Forward substitution. D(2) = D(1) + 1 = 1 A(1) = A(0) = 0 because no additions are performed to compute F (0) or
D(4) = D(2) + 1 = 2 F (1).
D(8) = D(4) + 1 = 3 If n > 1, then F (n) performs two recursive calls plus one addition.
Backward substitution. D(n) = D(n/2) + 1
= [D(n/4) + 1] + 1 = D(n/4) + 2 A(n) = A(n − 1) + A(n − 2) + 1
= [D(n/8) + 1] + 2 = D(n/8) + 3 to compute to compute to add F (n − 1)
F (n − 1) F (n − 2) and F (n − 2)
Prove D(2k ) = k by mathematical induction. CS 3343 Analysis of Algorithms Chapter 2: Slide – 29
k
Basis: if k = 0, then D(2 ) = D(1) = 0 = k
Induction: if D(2k−1) = k − 1, then D(2k ) = D(2k−1) + 1 = (k − 1) + 1 = k Approximating the Fibonacci Recurrence
CS 3343 Analysis of Algorithms Chapter 2: Slide – 27
Make a reasonable guess at a lower bound.
Fibonacci Function Forward substitution. A(2) = 1, A(3) = 2, A(4) = 4, A(5) = 7, A(6) = 12.
Backward substitution.
A(n) = A(n − 1) + A(n − 2) + 1
algorithm F (n) = [A(n − 2) + A(n − 3) + 1] + A(n − 2) + 1
// Computes nth Fibonacci number recursively = 2A(n − 2) + A(n − 3) + 2
// Input: A nonnegative integer n
// Output: The nth Fibonacci number Prove A(n) ≥ 2n/2 when n ≥ 4.
if n = 0 or n = 1 then return n Basis: True for A(4) and A(5).
else return F (n − 1) + F (n − 2) Induction: A(n) = 2A(n − 2) + A(n − 3) + 2
≥ 2A(n − 2) ≥ 2 ∗ 2(n−2)/2 = 2n/2
Input Size: Use number n (actually n has about log2 n bits) CS 3343 Analysis of Algorithms Chapter 2: Slide – 30
Basic Operation: addition in else statement
CS 3343 Analysis of Algorithms Chapter 2: Slide – 28
15 16
Empirical Analysis of Algorithms 31
Results of Empirical Analysis: Comparisons
500000
Plan for Empirical Analysis of Algorithms No Duplicates
Number of Comparisons
One Duplicate
400000
Understand the experiment’s purpose. 300000
Decide on the metric M and the measurement unit.
200000
Time in Milliseconds
8
7
For arrays with a duplicate, randomly choose a pair of positions that will have 6
the same value. Also, run 10 times for each input size. 5
4
3
Prepare UniqueElementsExperiment.java. 2
1
0
Run program, record data, and create graph. 0 200 400 600 800 1000
n
Analyze the data. No duplicates and one duplicate lines are:
CS 3343 Analysis of Algorithms Chapter 2: Slide – 32 −0.037 + 0.0016 ∗ n + 8.4 × 10−6 ∗ n2 .
−0.073 + 0.00047 ∗ n + 5.2 × 10−6 ∗ n2.
CS 3343 Analysis of Algorithms Chapter 2: Slide – 34
17 18
Algorithm Visualization 35
http://maven.smith.edu/∼thiebaut/java/sort/demo.html
CS 3343 Analysis of Algorithms Chapter 2: Slide – 35
19