0% found this document useful (0 votes)
41 views

Fundamentals of Analysis of Algorithms Efficiency

This document discusses analyzing the efficiency of algorithms, including non-recursive and recursive algorithms. It defines key terms like time efficiency, space efficiency, input size, basic operations, asymptotic notations, and efficiency classes. For non-recursive algorithms, it provides a process to determine efficiency: identify the basic operation, analyze worst, average, and best cases, set up a summation of basic operations, and simplify using formulas. Examples analyze the efficiency of finding the maximum element and checking element uniqueness in an array.

Uploaded by

nourah
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Fundamentals of Analysis of Algorithms Efficiency

This document discusses analyzing the efficiency of algorithms, including non-recursive and recursive algorithms. It defines key terms like time efficiency, space efficiency, input size, basic operations, asymptotic notations, and efficiency classes. For non-recursive algorithms, it provides a process to determine efficiency: identify the basic operation, analyze worst, average, and best cases, set up a summation of basic operations, and simplify using formulas. Examples analyze the efficiency of finding the maximum element and checking element uniqueness in an array.

Uploaded by

nourah
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 40

Chapter 2:

Fundamentals of Analysis of
Algorithms Efficiency
Topics
 Analysis Framework
 Definitions
 Asymptotic Notations and Basic Efficiency Classes
 Mathematical Analysis of Nonrecursive Algorithms.
 Examples
 Mathematical Analysis of Recursive Algorithms.
 Examples
 Assignment.
 Empirical Analysis of algorithms
 Algorithm Visualization.
 There are two kinds of Algorithm efficiency:
 Time efficiency: It indicates how fast an algorithm in question runs.
 Space efficiency: It deals with the extra space an algorithm requires.

 Input’s size:
 The algorithm’s efficiency is measured as a function if some
parameter n indicating the algorithm’s input size.
OR
 An algorithm’s time efficiency is principally measured as a function of
its input size by counting the number of time its basic operation is
executed.
 Eg : Some algorithm require more than one parameter to indicate the
size if their inputs, like the number of vertices and the number of
edges for algorithms on graphs represented by the adjacency linked
lists.
Units of measuring Running Time
 Basic Operation:
 It is the operation that contributes most towards running
time, and computer the number of times the basic
operation is executed.

Running Time T(n):
T(n) ≈ copC(n)
Where cop = time of execution of an algorithm’s basic operation.
C(n) = the number of times this operation needs to be
executed for this algorithm
Input size and basic operation
examples
Problem Input size measure Basic operation

Searching for key in Number of list’s items,


Key comparison
a list of n items i.e. n
Matrix dimensions or
Multiplication of two Multiplication of
total number of
matrices two numbers
elements
n’size = number of
Checking primality
digits (in binary Division
of a given integer n
representation)
Visiting a vertex or
Typical graph
#vertices and/or edges traversing an
problem
edge
 Orders of Growth:
The analysis of algorithm’s time efficiency is known through
the order of growth of the algorithm’s running time as its
input size goes to infinity.
 The notations O, Θ and Ω are used to indicate and compare the
orders of growth of functions expressing algorithm efficiencies.
 Example:
 How much faster will algorithm run on computer that is ten times

as fast as we have?

 How much longer does it take to solve problem/algorithm if we


double input size?
Values of some important functions as n

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: Cbest(n) – minimum over inputs of size n

 Average case: Cavg(n) – “average” 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
Example: Sequential search

 Worst case
 Best case
 Average case
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)
Asymptotic Notations
The notations O, Θ and Ω are used to indicate and compare the orders
of growth of functions expressing algorithm efficiencies.
t(n) & g(n) are two non-negative functions defined on the set of natural
numbers.
t(n)= Algorithms running time

C(n)= Basic operation count

g(n)= simple function to compare the count with.

O(g(n)): is the set of all functions with a smaller or same order of


growth as g(n).
Ω(g(n)): is the set of all functions with a larger or same order of
growth as g(n).
Θ(g(n)): is the set of all functions with same order of growth as
g(n).
Big-oh
Big-omega
Big-theta
Establishing order of growth using the
definition
O-notation:
Definition: A function t(n) is said to be in O(g(n)) if order of growth of
t(n) ≤ order of growth of g(n) (within constant multiple), i.e., there
exist positive constant c and non-negative integer n0 such that
t(n) ≤ c g(n) for every n ≥ n0

Examples:
 10n  O(n2),
 n O(n2),
 5n+20  O(n)
 - notation:
Definition: A function t(n) is said to be in (g(n)) if order of growth of
t(n) ≥ order of growth of g(n) (within constant multiple), i.e., there
exist positive constant c and non-negative integer n0 such that
t(n) ≥ c g(n) for every n ≥ n0
Example:
n2  (n2)
 Θ- notation:
Definition: A function t(n) is said to be in Θ(g(n)) if order of growth of
t(n) is in between order of growth of g(n) (within constant multiple),
i.e., there exist positive constant C1 and C2and non-negative
integer n0 such that
C2 g(n) ≤t(n)≤ C1 g(n) for every n ≥ n0
Useful properties involving the Asymptotic
Notations

 If t1(n)  O(g1(n)) and t2(n)  O(g2(n)) , then


t1(n) + t2(n)  O(max{g1(n), g2(n)})

This is true for both , Θ.


Using Limits for comparingOrgers of
growth
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
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
Stirling’s formula: n!  (2n)1/2 (n/e)n
lim f(n) lim f ´(n)
=
n g(n) n g ´(n)
Example: log n vs. n

Example: 2n vs. n!
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-
k-1 + … + a  (nk)
1n 0

 Exponential functions an have different orders of growth for different a’s

 order log n < order n (>0) < order an < order n! < order nn
Basic asymptotic efficiency classes
1 constant
log n logarithmic
n linear
n log n n-log-n
n2 quadratic
n3 cubic
2n exponential
n! factorial
Mathematical Analysis of
Nonrecursive Algorithms
General Plan for Analyzing efficiency of nonrecursive algorithms

 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)
Example 1: Maximum element
 In the above example:
 Input size= n (number of elements in the array)
 Basic Operation = A[i] > maxval;
 In this example there are no cases, as the algorithm has
to continue for all the elements of the array.
 Let C(n) be the number of times the comparison is done.
 The algorithm makes one comparison on each execution
of the loop, which is repeated for each value of i within
the bounds betweenn-1 1 and n-1.
i=1

C(n)=  1= n-1  Θ(n).


Example 2: Element uniqueness
problem
 In the above example:
 Input size= n
 Basic Operation = comparison;
 In this example the comparison will not only depend on
n, but also whether there are qual elements in the array,
and if there are, which array positions they occupy.
 Here we just calculate the worst case.
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 cai = cai liuai = limai + m+1iuai


Mathematical Analysis of
Nonrecursive Algorithms
General Plan for Analyzing efficiency of nonrecursive 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.
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: n
Basic operation: multiplication
Recurrence relation: ???
Solving the recurrence for M(n)

M(n) = M(n-1) + 1, M(0) = 0


Example 2: The Tower of Hanoi
Puzzle

1 3

Recurrence for number of moves:


Solving recurrence for number of
moves
M(n) = 2M(n-1) + 1, M(1) = 1
Tree of calls for the Tower of Hanoi
Puzzle n

n-1 n-1

n-2 n-2 n-2 n-2


... ... ...
2 2 2 2

1 1 1 1 1 1 1 1
Example 3: Counting #bits
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

General 2nd order linear homogeneous recurrence with


constant coefficients:
aX(n) + bX(n-1) + cX(n-2) = 0
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


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:


Computing Fibonacci numbers

1. Definition-based recursive algorithm

2. Nonrecursive definition-based algorithm

3. Explicit formula algorithm

4. Logarithmic algorithm based on formula:


n
F(n-1) F(n) 0 1
=
F(n) F(n+1) 1 1

for n≥1, assuming an efficient way of computing matrix powers.


Empirical Analysis of algorithms

 Empirical analysis of an algorithm is


performed by running a program ,
implementing the algorithm on a sample of
inputs and analyzing the data observed.
Algorithm Visualization.

 Algorithm Visualization is the use of images


to convey useful information about
algorithms.
 The two principal visualization are static
algorithm visualization and dynamic algorithm
visualization.

You might also like