chapter 2 fundamentals of the analysis of algorithm efficiency copyright © 2007 pearson...

46
Chapter 2 Chapter 2 Fundamentals of the Fundamentals of the Analysis of Algorithm Analysis of Algorithm Efficiency Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.

Upload: mariah-wen

Post on 14-Dec-2015

230 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Chapter 2 Chapter 2

Fundamentals of the Analysis Fundamentals of the Analysis of Algorithm Efficiencyof Algorithm Efficiency

Copyright © 2007 Pearson Addison-Wesley. All rights reserved.

Page 2: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-2Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Analysis of algorithmsAnalysis of algorithms

Issues:Issues:• correctnesscorrectness

• time efficiencytime efficiency

• space efficiencyspace efficiency

• optimalityoptimality

Approaches:Approaches:

• theoretical analysistheoretical analysis

• empirical analysisempirical analysis

Page 3: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-3Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Theoretical analysis of time efficiencyTheoretical analysis of time efficiency

Time efficiency is analyzed by determining the number of Time efficiency is analyzed by determining the number of repetitions of the repetitions of the basic operationbasic operation as a function of as a function of input sizeinput size

Basic operationBasic operation: the operation that contributes the most : the operation that contributes the most towards the running time of the algorithmtowards the running time of the algorithm

TT((nn) ) ≈≈ ccopopCC((nn))running time execution time

for basic operationor cost

Number of times basic operation is

executed

input size

Note: Different basic operations may cost differently!

Page 4: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-4Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Input size and basic operation examplesInput size and basic operation examples

ProblemProblem Input size measureInput size measure Basic operationBasic operation

Searching for key in a Searching for key in a list of list of nn items items

Number of list’s items, Number of list’s items, i.e. i.e. nn

Key comparisonKey comparison

Multiplication of two Multiplication of two matricesmatrices

Matrix dimensions or Matrix dimensions or total number of elementstotal number of elements

Multiplication of two Multiplication of two numbersnumbers

Checking primality of Checking primality of a given integer a given integer nn

n’n’size = number of digits size = number of digits (in binary representation)(in binary representation)

DivisionDivision

Typical graph problemTypical graph problem #vertices and/or edges#vertices and/or edgesVisiting a vertex or Visiting a vertex or traversing an edgetraversing an edge

Page 5: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-5Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Empirical analysis of time efficiencyEmpirical analysis of time efficiency

Select a specific (typical) sample of inputsSelect a specific (typical) sample of inputs

Use physical unit of time (e.g., milliseconds)Use physical unit of time (e.g., milliseconds)

oror

Count actual number of basic operation’s executionsCount actual number of basic operation’s executions

Analyze the empirical dataAnalyze the empirical data

Page 6: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-6Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Best-case, average-case, worst-caseBest-case, average-case, worst-case

For some algorithms, efficiency depends on form of input:For some algorithms, efficiency depends on form of input:

Worst case: CWorst case: Cworstworst((nn) – maximum over inputs of size ) – maximum over inputs of size nn

Best case: CBest case: Cbestbest((nn) – minimum over inputs of size ) – minimum over inputs of size nn

Average case: CAverage case: Cavgavg((nn) – “average” over inputs of size ) – “average” over inputs of size nn• Number of times the basic operation will be executed on typical inputNumber of times the basic operation will be executed on typical input

• NOT the average of worst and best caseNOT the average of worst and best case

• Expected number of basic operations considered as a random variable Expected number of basic operations considered as a random variable under some assumption about the probability distribution of all under some assumption about the probability distribution of all possible inputs. So, avg = expected under uniform distribution.possible inputs. So, avg = expected under uniform distribution.

Page 7: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-7Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Example: Sequential searchExample: Sequential search

Worst caseWorst case

Best caseBest case

Average caseAverage case

n key comparisons

1 comparisons

(n+1)/2, assuming K is in A

Page 8: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-8Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Types of formulas for basic operation’s countTypes of formulas for basic operation’s count

Exact formulaExact formula

e.g., C(e.g., C(nn) = ) = nn((nn-1)/2-1)/2

Formula indicating order of growth with specific Formula indicating order of growth with specific multiplicative constantmultiplicative constant

e.g., C(e.g., C(nn) ) ≈≈ 0.5 0.5 nn22

Formula indicating order of growth with unknown Formula indicating order of growth with unknown multiplicative constantmultiplicative constant

e.g., C(e.g., C(nn) ) ≈≈ cncn22

Page 9: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-9Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Order of growth Order of growth

Most important: Order of growth within a constant multiple Most important: Order of growth within a constant multiple as as nn→∞→∞

Example:Example:

• How much faster will algorithm run on computer that is How much faster will algorithm run on computer that is twice as fast?twice as fast?

• How much longer does it take to solve problem of double How much longer does it take to solve problem of double input size?input size?

Page 10: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-10Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Values of some important functions as Values of some important functions as n n

Page 11: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-11Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Asymptotic order of growthAsymptotic order of growth

A way of comparing functions that ignores constant factors and A way of comparing functions that ignores constant factors and small input sizes small input sizes (because?)(because?)

O(O(gg((nn)): class of functions )): class of functions ff((nn) that grow ) that grow no fasterno faster than than gg((nn))

ΘΘ((gg((nn)): class of functions )): class of functions ff((nn) that grow ) that grow at same rateat same rate as as gg((nn))

ΩΩ((gg((nn)): class of functions )): class of functions ff((nn) that grow ) that grow at least as fastat least as fast as as gg((nn))

Page 12: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-12Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Big-ohBig-oh

Page 13: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-13Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Big-omegaBig-omega

Page 14: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-14Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Big-thetaBig-theta

Page 15: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-15Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Establishing order of growth using the definitionEstablishing order of growth using the definition

Definition:Definition: f f((nn) is in O() is in O(gg((nn)), denoted f)), denoted f(n) (n) O(g(n)), O(g(n)), if order if order of growth of of growth of ff((nn) ) ≤≤ order of growth of order of growth of gg((nn) (within ) (within constant multiple), i.e., there exist positive constant constant multiple), i.e., there exist positive constant cc and and non-negative integer non-negative integer nn00 such that such that

ff((nn) ) ≤≤ c gc g((nn) for every ) for every nn ≥≥ nn0 0

Examples:Examples: 1010nn is in O( is in O(nn22))

55nn+20 is in O(+20 is in O(nn))

Page 16: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-16Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

-notation-notation

Formal definitionFormal definition• A function A function t(n)t(n) is said to be in is said to be in (g(n)),(g(n)), denoted denoted t(n) t(n)

(g(n)),(g(n)), if if t(n)t(n) is bounded below by some constant multiple is bounded below by some constant multiple of of g(n)g(n) for all large for all large nn, i.e., , i.e., if there exist some positive if there exist some positive constant c and some nonnegative integer constant c and some nonnegative integer nn00 such that such that

t(n) t(n) cg(n) for all n cg(n) for all n n n00

Exercises: Exercises: prove the following using the above definitionprove the following using the above definition• 1010nn22 ((nn22))• 0.30.3nn22 - 2n - 2n ((nn22))• 0.10.1nn33 ((nn22))

Page 17: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-17Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

-notation-notation

Formal definitionFormal definition• A function A function t(n)t(n) is said to be in is said to be in (g(n)),(g(n)), denoted denoted t(n) t(n)

(g(n)),(g(n)), if if t(n)t(n) is bounded both above and below by some is bounded both above and below by some positive constant multiples of positive constant multiples of g(n)g(n) for all large for all large nn, i.e., , i.e., if if there exist some positive constant cthere exist some positive constant c11 and c and c22 and some and some nonnegative integer nonnegative integer nn00 such that such that

cc22 g(n) g(n) t(n) t(n) c c11 g(n) for all n g(n) for all n n n00

Exercises: Exercises: prove the following using the above definitionprove the following using the above definition• 1010nn22 ((nn22))• 0.30.3nn22 - 2n - 2n ((nn22))• (1/2)n(n+1)(1/2)n(n+1) ((nn22))

Page 18: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-18Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

(g(n)), functions that grow at least as fast as g(n)

(g(n)), functions that grow at the same rate as g(n)

O(g(n)), functions that grow no faster than g(n)

g(n)

>=

<=

=

Page 19: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-19Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

TheoremTheorem

If If tt11(n) (n) O(g O(g11(n))(n)) and and tt22(n) (n) O(g O(g22(n)),(n)), then then

tt11(n) + t(n) + t22(n) (n) O(max{g O(max{g11(n), g(n), g22(n)}).(n)}).• The analogous assertions are true for the The analogous assertions are true for the -notation -notation

and and -notation.-notation.

Implication: The algorithm’s overall efficiency will be determined by Implication: The algorithm’s overall efficiency will be determined by the part with a larger order of growth, i.e., its least efficient part.the part with a larger order of growth, i.e., its least efficient part.

• For example, For example, 5n5n22 + 3nlogn + 3nlogn O(n O(n22))Proof. There exist constants c1, c2, n1, n2 such that tt11(n) (n) cc11**gg11(n), (n), for allfor all n n nn11

tt22(n) (n) cc22**gg22(n), (n), for allfor all n n nn22

Define Define cc33 = c = c11 + c + c22 and and nn3 3 = max{n= max{n11,n,n22}. Then}. Then

tt11(n) + t(n) + t22(n) (n) c c33**max{gmax{g11(n), g(n), g22(n)}, (n)}, for allfor all n n nn33

Page 20: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-20Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Some properties of asymptotic order of growthSome properties of asymptotic order of growth

ff((nn) ) O( O(ff((nn))))

ff((nn) ) O( O(gg((nn)) iff )) iff gg((nn) ) ((ff(n)) (n))

If If ff ((nn) ) O( O(gg ((nn)) and )) and gg((nn) ) O( O(hh((nn)) , then)) , then f f((nn) ) O( O(hh((nn)) ))

Note similarity with Note similarity with a a ≤≤ bb

If If ff11((nn) ) O( O(gg11((nn)) and )) and ff22((nn) ) O( O(gg22((nn)) , then)) , then

ff11((nn) + ) + ff22((nn) ) O(max{ O(max{gg11((nn), ), gg22((nn)}) )})

Also, Also, 11iinn ((ff((ii)) = )) = ( (11iin n ff((ii)) ))

Exercise: Can you prove these properties?Exercise: Can you prove these properties?

Page 21: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-21Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Establishing order of growth using limitsEstablishing order of growth using limits

limlim TT((nn)/)/gg((nn) = ) =

00 order of growth of TT((n)n) < order of growth of gg((nn))

c c > 0> 0 order of growth of TT((n)n) = order of growth of gg((nn))

∞ ∞ order of growth of TT((n)n) > order of growth of gg((nn))

Examples:Examples:• 1010nn vs. vs. nn22

• nn((nn+1)/2 vs. +1)/2 vs. nn22

nn→∞→∞

Page 22: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-22Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

L’Hôpital’s rule and Stirling’s formulaL’Hôpital’s rule and Stirling’s formula

L’Hôpital’s rule: If L’Hôpital’s rule: If limlimnn ff((nn) = ) = limlimnn g(ng(n) = ) = and and

the derivatives the derivatives ff´, ´, gg´ exist, then´ exist, then

Stirling’s formula: Stirling’s formula: nn! ! (2 (2nn))1/2 1/2 ((nn/e)/e)nn

ff((nn))gg((nn))

limlimnn

= f f ´(´(nn))g g ´(´(nn))

limlimnn

Example: log Example: log nn vs. vs. nn

Example: 2Example: 2nn vs. vs. nn!!

Page 23: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-23Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Orders of growth of some important functionsOrders of growth of some important functions

All logarithmic functions logAll logarithmic functions loga a nn belong to the same classbelong to the same class (log (log nn)) no matter what the logarithm’s base no matter what the logarithm’s base a a > 1 is> 1 is

because because

All polynomials of the same degree All polynomials of the same degree k k belong to the same class: belong to the same class: aakknnkk + + aakk-1-1nnkk-1-1 + … + + … + aa0 0 ((nnkk) )

Exponential functions Exponential functions aan n have different orders of growth for different have different orders of growth for different aa’s’s

order order log log n < n < order order nn ((>0) < order >0) < order aann < order < order nn! < order ! < order nnnn

ann bba log/loglog

Page 24: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-24Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Basic asymptotic efficiency classesBasic asymptotic efficiency classes

11 constantconstant

log log nn logarithmiclogarithmic

nn linearlinear

n n log log nn n-n-loglog-n-n

nn22 quadraticquadratic

nn33 cubiccubic

22nn exponentialexponential

nn!! factorialfactorial

Page 25: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-25Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Time efficiency of nonrecursive algorithmsTime efficiency of nonrecursive algorithms

General Plan for AnalysisGeneral Plan for Analysis Decide on parameter Decide on parameter nn indicating indicating input sizeinput size

Identify algorithm’s Identify algorithm’s basic operationbasic operation

Determine Determine worstworst, , averageaverage, and , and bestbest cases for input of size cases for input of size nn

Set up a sum for the number of times the basic operation is Set up a sum for the number of times the basic operation is executedexecuted

Simplify the sum using standard formulas and rules (see Simplify the sum using standard formulas and rules (see Appendix A)Appendix A)

Page 26: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-26Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Useful summation formulas and rulesUseful summation formulas and rules

lliinn1 = 1+1+…+1 = 1 = 1+1+…+1 = n n - - l l + 1+ 1

In particular, In particular, lliinn1 = 1 = n n - 1 + 1 = - 1 + 1 = n n ((nn) )

11iinn ii = 1+2+…+ = 1+2+…+nn = = nn((nn+1)/2 +1)/2 nn22/2 /2 ((nn22) )

11iinn ii22 = 1 = 122+2+222+…++…+nn22 = = nn((nn+1)(2+1)(2nn+1)/6 +1)/6 nn33/3 /3 ((nn33))

00iinn aaii = 1 = 1 + + a a +…+ +…+ aann = ( = (aann+1 +1 - 1)/(- 1)/(a a - 1) for any - 1) for any a a 1 1

In particular, In particular, 00iinn 22ii = 2 = 20 0 + 2+ 21 1 +…+ 2+…+ 2nn = 2 = 2nn+1+1 - 1 - 1 (2(2nn ))

((aaii ±± bbi i ) = ) = aaii ±± bbi i cacaii = = ccaaii lliiuuaaii = = lliimmaaii + + mm+1+1iiuuaaii

Page 27: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-27Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Example 1: Maximum elementExample 1: Maximum element

T(n) = 11iin-1 n-1 1 = n-1 = 1 = n-1 = ((nn)) comparisons

Page 28: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-28Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Example 2: Element uniqueness problemExample 2: Element uniqueness problem

T(n) = 00iin-2n-2 ( (i+1i+1jjn-1 n-1 1)

= = 00iin-2n-2 n-i-1 = (n-1+1)(n-1)/2

= ( )( ) comparisons2n

Page 29: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-29Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Example 3: Matrix multiplicationExample 3: Matrix multiplication

T(n) = 00iin-1n-1 00iin-1n-1 n

= 00iin-1n-1 (( )

= (( ) multiplications

2n3n

Page 30: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-30Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Example 4: Gaussian eliminationExample 4: Gaussian elimination

AlgorithmAlgorithm GaussianEliminationGaussianElimination((AA[0..[0..nn--1,0..1,0..nn])])

//Implements Gaussian elimination on an //Implements Gaussian elimination on an n-n-byby--((nn+1) matrix+1) matrix AA

forfor ii 00 to to n n -- 22 do do for for jj i i + 1+ 1 to to n n -- 1 1 do do forfor kk i i to to n n dodo

AA[[jj,,kk] ] A A[[jj,,kk] ] -- AA[[ii,,kk] ] AA[[jj,,ii] / ] / AA[[ii,,ii]]

Find the efficiency class and a constant factor improvement.Find the efficiency class and a constant factor improvement.

forfor ii 00 to to n n -- 22 do do for for jj i i + 1+ 1 to to n n -- 1 1 do do

BB AA[[j,ij,i] / ] / AA[[ii,,ii]] forfor kk i i to to n n dodo AA[[jj,,kk] ] A A[[jj,,kk]] – A – A[[i,ki,k] ] * B * B

Page 31: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-31Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Example 5: Counting binary digits Example 5: Counting binary digits

It cannot be investigated the way the previous examples are.It cannot be investigated the way the previous examples are. The halving game: Find integer i such that n/ ≤ ≤ 1.1.

Answer: i ≤ log n. So, T(n) = Answer: i ≤ log n. So, T(n) = ((log n) divisions.

Another solution: Using recurrence relations.

i2

Page 32: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-32Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Plan for Analysis of Recursive AlgorithmsPlan for Analysis of Recursive Algorithms

Decide on a parameter indicating an input’s size.Decide on a parameter indicating an input’s size.

Identify the algorithm’s basic operation. Identify the algorithm’s basic operation.

Check whether the number of times the basic op. is executed Check whether the number of times the basic op. is executed may vary on different inputs of the same size. (If it may, the may vary on different inputs of the same size. (If it may, the worst, average, and best cases must be investigated worst, average, and best cases must be investigated separately.)separately.)

Set up a recurrence relation with an appropriate initial Set up a recurrence relation with an appropriate initial condition expressing the number of times the basic op. is condition expressing the number of times the basic op. is executed.executed.

Solve the recurrence (or, at the very least, establish its Solve the recurrence (or, at the very least, establish its solution’s order of growth) by backward substitutions or solution’s order of growth) by backward substitutions or another method.another method.

Page 33: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-33Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Example 1: Recursive evaluation of Example 1: Recursive evaluation of nn!!

Definition:Definition: n n ! = 1 ! = 1 2 2 … … ((n-n-1) 1) nn for for n n ≥≥ 1 and 0! = 11 and 0! = 1

Recursive definition of Recursive definition of nn!: !: FF((nn) = ) = FF((n-n-1) 1) nn for for n n ≥≥ 1 and 1 and FF((0) = 10) = 1

Size:Size:Basic operation:Basic operation:Recurrence relation:Recurrence relation:

nmultiplicationM(n) = M(n-1) + 1

M(0) = 0

Page 34: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-34Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Solving the recurrence for M(Solving the recurrence for M(nn))

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

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

= (M(n-2) + 1) + 1 = M(n-2) + 2

= (M(n-3) + 1) + 2 = M(n-3) + 3

= M(n-i) + i

= M(0) + n

= n

The method is called backward substitution.

Page 35: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-35Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Example 2: The Tower of Hanoi PuzzleExample 2: The Tower of Hanoi Puzzle

1

2

3

Recurrence for number of moves:Recurrence for number of moves: M(n) = 2M(n-1) + 1

Page 36: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-36Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Solving recurrence for number of movesSolving recurrence for number of moves

M(M(nn) = 2M() = 2M(nn-1) + 1, M(1) = 1-1) + 1, M(1) = 1

M(n) = 2M(n-1) + 1

= 2(2M(n-2) + 1) + 1 = 2^2*M(n-2) + 2^1 + 2^0

= 2^2*(2M(n-3) + 1) + 2^1 + 2^0

= 2^3*M(n-3) + 2^2 + 2^1 + 2^0

= …

= 2^(n-1)*M(1) + 2^(n-2) + … + 2^1 + 2^0

= 2^(n-1) + 2^(n-2) + … + 2^1 + 2^0

= 2^n - 1

Page 37: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-37Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Tree of calls for the Tower of Hanoi PuzzleTree of calls for the Tower of Hanoi Puzzle

n

n-1 n-1

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

1 1

... ... ...2

1 1

2

1 1

2

1 1

2

Page 38: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-38Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Example 3: Counting #bitsExample 3: Counting #bits

A( ) = A( ) + 1, A( ) = 1 (using the Smoothness Rule)

= (A( ) + 1) + 1 = A( ) + 2

= A( ) + i

= A( ) + k = k + 0

=

k2 12 k 02

22 k

n2log

22 k

ik 2

kk 2

A(n) = A( ) + 1, A(1) = 0 2/n

Page 39: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-39Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Smoothness RuleSmoothness Rule

Let f(n) be a Let f(n) be a nonnegativenonnegative function defined on the set of natural function defined on the set of natural numbers. f(n) is call numbers. f(n) is call smoothsmooth if it is if it is eventuallyeventually nondecreasingnondecreasing and andf(2n)f(2n) ∈∈ ΘΘ (f(n)) (f(n))• Functions that do not grow too fast, including Functions that do not grow too fast, including lognlogn, , nn, , nlognnlogn, and , and

nn where where >=0 are smooth.>=0 are smooth. Smoothness ruleSmoothness rule

Let T(n) be an Let T(n) be an eventually nondecreasingeventually nondecreasing function and function and f(n) be a f(n) be a smooth functionsmooth function. If. IfT(n)T(n) ∈∈ ΘΘ (f(n)) (f(n)) for values of n that are powers of b, for values of n that are powers of b, where b>=2, thenwhere b>=2, then

T(n)T(n) ∈∈ ΘΘ (f(n)) for any n. (f(n)) for any n.

Page 40: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-40Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Fibonacci numbersFibonacci numbers

The Fibonacci numbers:The Fibonacci numbers:

0, 1, 1, 2, 3, 5, 8, 13, 21, … 0, 1, 1, 2, 3, 5, 8, 13, 21, …

The Fibonacci recurrence:The Fibonacci recurrence:

F(F(nn) = F() = F(nn-1) + F(-1) + F(nn-2) -2)

F(0) = 0 F(0) = 0

F(1) = 1F(1) = 1

General 2General 2ndnd order order linear homogeneous recurrencelinear homogeneous recurrence with with

constant coefficients:constant coefficients:

aaX(X(nn) + ) + bbX(X(nn-1) + -1) + ccXX(n(n-2) -2) == 0 0

Page 41: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-41Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Solving Solving aaX(X(nn) + ) + bbX(X(nn-1) + -1) + ccX(X(nn-2) -2) == 0 0

Set up the characteristic equation (quadratic)Set up the characteristic equation (quadratic)

arar22 + + brbr + + cc == 0 0

Solve to obtain roots Solve to obtain roots rr11 and and rr22

General solution to the recurrenceGeneral solution to the recurrence

if if rr1 1 and and rr2 2 are two distinct real roots: X( are two distinct real roots: X(nn) = ) = ααrr11n n + + ββrr22

nn

if if rr1 1 == rr2 2 = = rr are two equal real roots: X( are two equal real roots: X(nn) = ) = ααrrn n + + ββnrnr nn

Particular solution can be found by using initial conditionsParticular solution can be found by using initial conditions

Page 42: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-42Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Application to the Fibonacci numbersApplication to the Fibonacci numbers

F(F(nn) = F() = F(nn-1) + F(-1) + F(nn-2) or F(-2) or F(nn) - F() - F(nn-1) - F(-1) - F(nn-2) = 0-2) = 0

Characteristic equation:Characteristic equation:

Roots of the characteristic equation:Roots of the characteristic equation:

General solution to the recurrence:General solution to the recurrence:

Particular solution for F(0) =0, F(1)=1:Particular solution for F(0) =0, F(1)=1:

2r - r -1 = 0

2/)51(2,1 r

nn rr 21

121 rr 0

Page 43: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-43Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Computing Fibonacci numbersComputing Fibonacci numbers

1.1. Definition-based recursive algorithmDefinition-based recursive algorithm

2.2. Nonrecursive definition-based algorithmNonrecursive definition-based algorithm

3.3. Explicit formula algorithmExplicit formula algorithm

4.4. Logarithmic algorithm based on formula:Logarithmic algorithm based on formula:

FF((nn-1)-1) F F((nn))

FF((nn)) F F((nn+1)+1)

0 10 1

1 11 1==

nn

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

Page 44: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-44Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Important Recurrence TypesImportant Recurrence Types

Decrease-by-one recurrencesDecrease-by-one recurrences• A decrease-by-one algorithm solves a problem by exploiting a relationship A decrease-by-one algorithm solves a problem by exploiting a relationship

between a given instance of size between a given instance of size nn and a smaller size and a smaller size n – 1n – 1..• Example: Example: n!n!• The recurrence equation for investigating the time efficiency of such The recurrence equation for investigating the time efficiency of such

algorithms typically has the formalgorithms typically has the formT(n) = T(n-1) + f(n)T(n) = T(n-1) + f(n)

Decrease-by-a-constant-factor recurrencesDecrease-by-a-constant-factor recurrences• A decrease-by-a-constant-factor algorithm solves a problem by dividing its A decrease-by-a-constant-factor algorithm solves a problem by dividing its

given instance of size given instance of size nn into several smaller instances of size into several smaller instances of size n/bn/b, solving , solving each of them recursively, and then, if necessary, combining the solutions to each of them recursively, and then, if necessary, combining the solutions to the smaller instances into a solution to the given instance.the smaller instances into a solution to the given instance.

• Example:Example: binary search. binary search. • The recurrence equation for investigating the time efficiency of such The recurrence equation for investigating the time efficiency of such

algorithms typically has the formalgorithms typically has the form T(n) = aT(n/b) + f (n)T(n) = aT(n/b) + f (n)

Page 45: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-45Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Decrease-by-one RecurrencesDecrease-by-one Recurrences

One (constant) operation reduces problem size by one.One (constant) operation reduces problem size by one.

T(T(nn) = T() = T(nn-1) + -1) + cc T(1) = T(1) = dd

Solution:Solution:

A pass through input reduces problem size by one.A pass through input reduces problem size by one.

T(T(nn) = T() = T(nn-1) + -1) + c nc n T(1) = T(1) = dd

Solution:Solution:

T(n) = (n-1)c + d linear

T(n) = [n(n+1)/2 – 1] c + d quadratic

Page 46: Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2-46Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2

Decrease-by-a-constant-factor recurrences – Decrease-by-a-constant-factor recurrences – The Master Theorem The Master Theorem

TT((nn) = ) = aTaT((n/bn/b) + ) + f f ((nn),), where where f f ((nn)) ∈∈ ΘΘ((nnkk)) , k>=0 , k>=0

1.1. a < ba < bkk T T((nn) ) ∈∈ ΘΘ((nnkk))

2.2. a = ba = bkk T T((nn) ) ∈∈ ΘΘ((nnk k log log n n ))

3.3. a > ba > bkk T T((nn) ) ∈∈ ΘΘ((nnlog log a a))

4.4. Examples:Examples:

1.1. T(n) = T(n/2) + 1T(n) = T(n/2) + 1

2.2. T(n) = 2T(n/2) + nT(n) = 2T(n/2) + n

3.3. T(n) = 3T(n/2) + nT(n) = 3T(n/2) + n

4.4. T(n) = T(n/2) + nT(n) = T(n/2) + n Θ(nlog2

3)

Θ(log n)

Θ(nlog n)

b

Θ(n)