# Chapter 2: Fundamentals of the Analysis of Algorithm

#### Document technical information

Format ppt
Size 4.6 MB
First found May 22, 2018

#### Document content analysis

Category Also themed
Language
English
Type
not defined
Concepts
no text concepts found

#### Transcript

```Chapter 2
Fundamentals of the Analysis
of Algorithm Efficiency
Analysis of algorithms

Issues:
•
•
•
•

correctness
time efficiency
space efficiency
optimality
Approaches:
• theoretical analysis
• empirical analysis
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-1
Analysis of Algorithms

The term "analysis of algorithms" usually means
an investigation of the efficiency of an algorithm
with respect to two resources, namely execution
time and memory space.
 Why?
 Time efficiency: How fast the algorithm runs?
 Space efficiency: How much memory space the
algorithm requires?
 We will concentrate on analyzing for time
efficiency.
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

Basic operation: the operation that contributes most
towards the running time of the algorithm
input size
T(n) ≈ copC(n)
running time
execution time
for basic operation
Number of times
basic operation is
executed
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-3
Measuring an Input's Size

Observation: Almost all algorithms run longer on
larger inputs
 Example: Sorting
 The time efficiency of an algorithm is usually
calculated as a function of a parameter n
indicating the input size of the algorithm.
 For some algorithms, n is obvious.
 Example: Sorting, Searching
 Sometimes, there is a choice.
 Example: Calculating the product of two n x n
matrices
Input size and basic operation examples
Problem
Input size measure
Basic operation
Searching for key in a
list of n items
Number of list’s items,
i.e. n
Key comparison
Multiplication of two
matrices
Matrix dimensions or
total number of elements
Multiplication of two
numbers
Checking primality of
a given integer n
n’size = number of digits
Division
(in binary representation)
Typical graph problem
#vertices and/or edges
Visiting a vertex or
traversing an edge
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-5
Empirical analysis of time efficiency

Select a specific (typical) sample of inputs

Use physical unit of time (e.g., milliseconds)
or
Count actual number of basic operation’s executions

Analyze the empirical data
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-6
Best-case, average-case, worst-case
For some algorithms efficiency depends on form of input:

Worst case: Cworst(n) – maximum over inputs of size n

Best case:

Average case: Cavg(n) – “average” over inputs of size n
Cbest(n) – minimum over inputs of size n
• Number of times the basic operation will be executed on typical input
• NOT the average of worst and best case
• Expected number of basic operations considered as a random variable
under some assumption about the probability distribution of all
possible inputs
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-7
Best-case, Average-Case, Worst-Case

There are many algorithms the running time of
which can be different for the same list size n,
depending on the condition of the input.
 Example: Sorting
 Example: Sequential Search (see next slide).
 Worst case: no matches, or the first match is the
last element of the list.
Cworst(n) = n
 Best case: the first match is the first element of
the list.
Cbest(n) = 1
Best-case, Average-Case, Worst-Case

Average case
 Calculation assumptions:
 Probability of successful search is equal to p
 Probability of the first match occurring in the
ith position of the list is the same for every i.
Best-case, Average-Case, Worst-Case
1 2  3  ... n
Cavg (n)  p(
)  (1 p)n
n
p n(n  1)
 
 (1 p)n
n
2
p(n  1)

 (1 p)n
2
•
•
p = 0 (search must be unsuccessful)?
p = 1 (search must be successful)?
Example: Sequential search

Worst case

Best case

Average case (p48)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-11
Types of formulas for basic operation’s count

Exact formula
e.g., C(n) = n(n-1)/2

Formula indicating order of growth with specific
multiplicative constant
e.g., C(n) ≈ 0.5 n2

Formula indicating order of growth with unknown
multiplicative constant
e.g., C(n) ≈ cn2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-12
Order of growth

Most important: Order of growth within a constant multiple
as n→∞

Example:
• How much faster will algorithm run on computer that is
twice as fast?
• How much longer does it take to solve problem of double
input size? (p45)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-13
Values of some important functions as n  
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-14
Asymptotic order of growth
A way of comparing functions that ignores constant factors and
small input sizes

O(g(n)): class of functions f(n) that grow no faster than g(n)

Θ(g(n)): class of functions f(n) that grow at same rate as g(n)

Ω(g(n)): class of functions f(n) that grow at least as fast as g(n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-15
Establishing order of growth using the definition
Definition (p53): f(n) is in O(g(n)) if order of growth of f(n) ≤
order of growth of g(n) (within constant multiple),
i.e., there exist positive constant c and non-negative integer
n0 such that
f(n) ≤ c g(n) for every n ≥ n0
Examples:
 10n is O(n2)

5n+20 is O(n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-16
Big-oh
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-17
Establishing order of growth using the definition
Definition : f(n) is in Ω(g(n)) if order of growth of f(n) ≥ order
of growth of g(n) (within constant multiple),
i.e., there exist positive constant c and non-negative integer
n0 such that
f(n) ≥ c g(n) for every n ≥ n0
Examples:
 10n2 is O(n2)

5n2+20 is O(n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-18
Big-omega
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-19
Establishing order of growth using the definition
Definition: f(n) is in Θ(g(n)) if there exist positive constant c1
and c2 and non-negative integer n0 such that
c2 g(n) ≤ f(n) ≤ c1 g(n) for every n ≥ n0
Examples:
 10n is Θ(n)

5n3+20 is Θ(n3)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-20
Big-theta
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-21
Some properties of asymptotic order of growth

f(n)  O(f(n))

f(n)  O(g(n)) iff g(n) (f(n))

If f (n)  O(g (n)) and g(n)  O(h(n)) , then f(n)  O(h(n))
Note similarity with a ≤ b

If f1(n)  O(g1(n)) and f2(n)  O(g2(n)) , then
f1(n) + f2(n)  O(max{g1(n), g2(n)})
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-22
Establishing order of growth using limits
0 order of growth of T(n) < order of growth of g(n)
c > 0 order of growth of T(n) = order of growth of g(n)
lim T(n)/g(n) =
n→∞
∞ order of growth of T(n) > order of growth of g(n)
Examples:
• 10n
vs.
n2
• n(n+1)/2
vs.
n2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-23
L’Hôpital’s rule and Stirling’s formula
L’Hôpital’s rule: If limn f(n) = limn g(n) =  and
the derivatives f´, g´ exist, then
lim
n
f(n)
g(n)
=
lim
n
f ´(n)
g ´(n)
Example: log n vs. n
Example: 2n vs. n!
Stirling’s formula: n!  (2n)1/2 (n/e)n
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-24
Orders of growth of some important functions

All logarithmic functions loga n belong to the same class
(log n) no matter what the logarithm’s base a > 1 is

All polynomials of the same degree k belong to the same class:
aknk + ak-1nk-1 + … + a0  (nk)

Exponential functions an have different orders of growth for
different a’s

order log n < order n (>0) < order an < order n! < order nn
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-25
Basic asymptotic efficiency classes
1
constant
log n
logarithmic
n
linear
n log n
n-log-n
n2
n3
cubic
2n
exponential
n!
factorial
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-26
Time efficiency of nonrecursive algorithms
General Plan for Analysis

Decide on parameter n indicating input size

Identify algorithm’s basic operation

Determine worst, average, and best cases for input of size n

Set up a sum for the number of times the basic operation is
executed

Simplify the sum using standard formulas and rules (see
Appendix A)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-27
Useful summation formulas and rules
liu1 = 1+1+…+1 = u - l + 1
In particular, liu1 = n - 1 + 1 = n  (n)
1in i = 1+2+…+n = n(n+1)/2  n2/2  (n2)
1in i2 = 12+22+…+n2 = n(n+1)(2n+1)/6  n3/3  (n3)
0in ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a  1
In particular, 0in 2i = 20 + 21 +…+ 2n = 2n+1 - 1  (2n )
(ai ± bi ) = ai ± bi
m+1iuai
cai = cai
liuai = limai +
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-28
Example 1: Maximum element
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-29
What indicates the algorithm’s input size?
 What is the algorithm’s basic operation?
 No need to consider worst, average and best
cases for this example. Why?
 Calculate C(n)

n 1
C (n)  1  (n  1)  1  1  n  1
i 1
 C ( n )  ( n )
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-30
Example 2: Element uniqueness problem
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-31
What indicates the algorithm’s input size?
 What is the algorithm’s basic operation?
 The number of times its basic operation is
executed depends not only on its input size.
 Consider Cworst(n)

n  2 n 1
n2
i 0 j i 1
i 0
C (n)   1   (n  1)  (i  1)  1
n2
n2
n2
i 0
i 0
i 0
  (n  1  i )   (n  1)   i
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-32
(n  2)( n  1)
 (n  1)1 
2
i 0
n2
(n  2)( n  1)
 (n  1) 
2
(n  2) 

 (n  1) (n  1) 

2


2
(n  1)n 1 2

 n  ( n 2 )
2
2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-33
Example 3: Matrix multiplication
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-34
What indicates the algorithm’s input size?
 What is the algorithm’s basic operation?
 The number of times its basic operation is
executed depends only on its input size.
 Determine C(n)

n 1 n 1 n 1
n 1 n 1
n 1
i 0 j 0 k 0
i 0 j 0
i 0
C (n)  1   n  n 2  n3
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-35
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]
Find the efficiency class and a constant factor improvement.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-36
Example 5: Counting binary digits
It cannot be investigated the way the previous examples are.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-37
Plan for Analysis of Recursive Algorithms

Decide on a parameter indicating an input’s size.

Identify the algorithm’s basic operation.

Check whether the number of times the basic op. is executed
may vary on different inputs of the same size. (If it may, the
worst, average, and best cases must be investigated
separately.)

Set up a recurrence relation with an appropriate initial
condition expressing the number of times the basic op. is
executed.

Solve the recurrence (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,” 2nd ed., Ch. 2
2-38
Example 1: Recursive evaluation of n!
Definition: n ! = 1  2  … (n-1)  n for n ≥ 1 and 0! = 1
Recursive definition of n!: F(n) = F(n-1)  n for n ≥ 1 and
F(0) = 1
Size:
Basic operation:
Recurrence relation:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-39
Solving the recurrence for M(n)
M(n) = M(n-1) + 1, M(0) = 0
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-40
Example 2: The Tower of Hanoi Puzzle
1
3
2
Recurrence for number of moves:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-41
Solving recurrence for number of moves
M(n) = 2M(n-1) + 1, M(1) = 1
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-42
Tree of calls for the Tower of Hanoi Puzzle
n
n-1
n-1
n-2
2
1
...
1
n-2
n-2
...
...
2
1
n-2
1
2
1
2
1
1
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
1
2-43
Example 3: Counting #bits
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-44
Fibonacci numbers
Textbook p78 – p83
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
General 2nd order linear homogeneous recurrence with
constant coefficients:
aX(n) + bX(n-1) + cX(n-2) = 0
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-45
Solving aX(n) + bX(n-1) + cX(n-2) = 0

Set up the characteristic equation (quadratic)
ar2 + br + c = 0

Solve to obtain roots r1 and r2

General solution to the recurrence
if r1 and r2 are two distinct real roots: X(n) = αr1n + βr2n
if r1 = r2 = r are two equal real roots: X(n) = αrn + βnr n

Particular solution can be found by using initial conditions
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-46
Application to the Fibonacci numbers
F(n) = F(n-1) + F(n-2) or F(n) - F(n-1) - F(n-2) = 0
Characteristic equation:
Roots of the characteristic equation:
General solution to the recurrence:
Particular solution for F(0) =0, F(1)=1:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2
2-47
Computing Fibonacci numbers
1.
Definition-based recursive algorithm
2.
Nonrecursive definition-based algorithm
3.
Explicit formula algorithm
4.
Logarithmic algorithm based on formula:
F(n-1) F(n)
0 1 n
=
1 1
F(n) F(n+1)
for n≥1, assuming an efficient way of computing matrix powers.