chapter_02 fundamental analysis

38
Chapter 2 Chapter 2 Fundamentals of the Analysis Fundamentals of the Analysis of Algorithm Efficiency of Algorithm Efficiency Copyr ight ©2007 Pearson Addison-Wesley. All righ ts reserved.

Upload: nipaporn-tanny-thipmanee

Post on 03-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 1/38

Chapter 2Chapter 2

Fundamentals of the AnalysisFundamentals of the Analysis

of Algorithm Efficiencyof Algorithm Efficiency

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

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 2/38

2-1Copyright ©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

II Issues:Issues:

•• correctnesscorrectness

•• time efficiencytime efficiency

•• space efficiencyspace efficiency

•• optimalityoptimality

II Approaches:Approaches:

••

theoretical analysistheoretical analysis

•• empirical analysisempirical analysis

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 3/38

2-2Copyright ©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 therepetitions of the basic operationbasic operation as a function of as a function of input sizeinput size

II Basic operation Basic operation: the operation that contributes most: the operation that contributes most

towards the running time of the algorithmtowards the running time of the algorithm

T T ((nn)) ≈≈ ccopopC C ((nn))

running time execution time

for basic operation

Number of times

basic operation is

executed

input size

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 4/382-3Copyright ©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

Problem Problem Input size measure Input size measure Basic operation Basic operation

Searching for key in aSearching for key in a

list of list of nn itemsitems

Number of listNumber of list’’s items,s items,

i.e.i.e. nnKey comparisonKey comparison

Multiplication of twoMultiplication of twomatricesmatrices

Matrix dimensions orMatrix dimensions ortotal number of elementstotal number of elements

Multiplication of twoMultiplication of twonumbersnumbers

Checking primality of Checking primality of

a given integera given integer nn

nn’ ’ size = number of digitssize = 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 orVisiting a vertex or

traversing an edgetraversing an edge

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 5/38

2-4Copyright ©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

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

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

Count actual number of basic operationCount actual number of basic operation’’s executionss executions

II Analyze the empirical dataAnalyze the empirical data

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 6/38

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

BestBest--case, averagecase, average--case, worstcase, worst--casecase

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

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

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

II Average case: CAverage case: Cavgavg((nn)) – – ““averageaverage”” over inputs of sizeover inputs of size nn

•• Number of times the basic operation will be executed on typicalNumber of times the basic operation will be executed on typical inputinput

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

•• Expected number of basic operations considered as a random variaExpected number of basic operations considered as a random variableble

under some assumption about the probability distribution of allunder some assumption about the probability distribution of all

possible inputspossible inputs

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 7/38

2-6Copyright ©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

IIWorst caseWorst case

II Best caseBest case

II Average caseAverage case

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 8/38

2-7Copyright ©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 operationTypes of formulas for basic operation’’s counts count

II Exact formulaExact formula

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

II Formula indicating order of growth with specificFormula indicating order of growth with specific

multiplicative constantmultiplicative constant

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

II Formula indicating order of growth with unknownFormula indicating order of growth with unknown

multiplicative constantmultiplicative constant

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

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 9/38

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

Order of growthOrder of growth

II Most important: Order of growth within a constant multipleMost important: Order of growth within a constant multiple

asas nn→∞→∞

II Example:Example:

•• How much faster will algorithm run on computer that isHow 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 doubleHow much longer does it take to solve problem of double

input size?input size?

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 10/38

2-9Copyright ©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 asValues of some important functions as nn →∞

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 11/38

2-10Copyright ©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 andA way of comparing functions that ignores constant factors and

small input sizessmall input sizes

II O(O( g g ((nn)): class of functions)): class of functions f f ((nn) that grow) that grow no faster thanthan g g ((nn))

II ΘΘ(( g g ((nn)): class of functions)): class of functions f f ((nn) that grow) that grow at same rate asas g g ((nn))

II ΩΩ(( g g ((nn)): class of functions)): class of functions f f ((nn) that grow) that grow at least as fast asas g g ((nn)

no faster

at same rate

at least as fast )

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 12/38

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

BigBig--ohoh

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 13/38

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

BigBig--omegaomega

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 14/38

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

BigBig--thetatheta

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 15/38

2-14Copyright ©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( g g ((nn)) if order of growth of )) if order of growth of f f ((nn)) ≤≤ orderorder

of growth of of growth of g g ((nn) (within constant multiple),) (within constant multiple),

i.e., there exist positive constanti.e., there exist positive constant cc and nonand non--negative integernegative integer

nn00 such thatsuch that

f f ((nn)) ≤≤ c g c g ((nn) for every) for every nn ≥≥ nn00

Examples:Examples:

II 1010nn is O(is O(nn22))

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

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 16/38

2-15Copyright ©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

II f f ((nn)) ∈ O(O( f f ((nn))))

II f f ((nn))∈

O(O( g g ((nn)) iff )) iff g g ((nn))∈ΩΩ

(( f f (n))(n))

II If If f f ((nn)) ∈ O(O( g g ((nn)) and)) and g g ((nn)) ∈ O(O(hh((nn)) , then)) , then f f ((nn)) ∈ O(O(hh((nn))))

Note similarity withNote similarity with aa ≤ ≤ bb

II If If f f 11((nn)) ∈ O(O( g g

11((nn)) and)) and f f

22((nn)) ∈ O(O( g g

22((nn)) , then)) , then

f f 11((nn)) ++ f f 22((nn)) ∈ O(maxO(max g g 11((nn),), g g 22((nn))))

E t bli hi d f th i li itE t bli hi d f th i li it

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 17/38

2-16Copyright ©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 T T ((nn)/)/ g g ((nn) =) =

00 order of growth of T T ((n)n) < order of growth of g g ((nn))

cc > 0> 0 order of growth of T T ((n)n) = order of growth of g g ((nn))

∞∞ order of growth of T T ((n)n) > order of growth of g g ((nn))

nn→∞→∞

Examples:Examples:

• 1010nn vs.vs. nn22

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

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 18/38

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

LL’’HôpitalHôpital’’s rule and Stirlings rule and Stirling’’s formulas formula

LL’’HôpitalHôpital’’s rule: If s rule: If limlimnn→∞∞ f f ((nn) =) = limlimnn→∞∞ g(n g(n) =) = ∞ andand

the derivativesthe derivatives f f ,, g g exist, thenexist, then

StirlingStirling’’s formula:s formula: nn!! ≈ (2(2πnn))1/21/2 ((nn/e)/e)nn

f f ((nn))

g g ((nn))limlimnn→∞∞

= f f ´((nn))

g g ´((nn))limlimnn→∞∞

Example: logExample: log nn vs.vs. nn

Example:Example: 22nn vs.vs. nn!!

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 19/38

2-18Copyright ©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

II All logarithmic functions logAll logarithmic functions logaa nn belong to the same classbelong to the same classΘ((loglog nn)) no matter what the logarithmno matter what the logarithm’’s bases base aa > 1 is> 1 is

II All polynomials of the same degreeAll polynomials of the same degree k k belong to the same class:belong to the same class:aak k nn

k k ++ aak k --11nnk k --11 ++ …… ++ aa00∈ Θ((nnk k ))

II Exponential functionsExponential functions aann have different orders of growth forhave different orders of growth fordifferentdifferent aa’’ss

II orderorder loglog n <n < orderorder nnα ((α>0) < order>0) < order aann < order< order nn! < order! < order nnnn

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 20/38

2-19Copyright ©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

loglog nn logarithmiclogarithmic

nn linearlinear

nn loglog nn nn--loglog--nn

nn22 quadraticquadratic

nn33 cubiccubic

22nn exponentialexponential

nn!! factorialfactorial

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 21/38

2-20Copyright ©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

II Decide on parameterDecide on parameter nn indicatingindicating input size

II Identify algorithmIdentify algorithm’’ss basic operation

II DetermineDetermine worst ,, average, and, and best cases for input of sizecases for input of size nn

II Set up a sum for the number of times the basic operation isSet up a sum for the number of times the basic operation isexecutedexecuted

II Simplify the sum using standard formulas and rules (seeSimplify the sum using standard formulas and rules (seeAppendix A)

input size

basic operation

worst average best

Appendix A)

U f l ti f l d lU f l ti f l d l

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 22/38

2-21Copyright ©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

Σl l ≤i i ≤uu1 = 1+1+1 = 1+1+……+1 =+1 = uu -- l l + 1+ 1

In particular,In particular, Σll≤i i ≤uu1 =1 = nn -- 1 + 1 =1 + 1 = nn ∈ Θ((nn))

Σ11≤i i ≤nn i i = 1+2+= 1+2+……++nn == nn((nn+1)/2+1)/2 ≈ nn22/2/2 ∈ Θ((nn22))

Σ11≤i i ≤nn i i 22

= 1= 122

+2+222

++……++nn22

== nn((nn+1)(2+1)(2nn+1)/6+1)/6 ≈ nn33

/3/3 ∈ Θ((nn33

))

Σ00≤i i ≤nn aai i = 1= 1 ++ aa ++……++ aann = (= (aann+1+1 -- 1)/(1)/(aa -- 1) for any1) for any aa ≠ 11

In particular,In particular, Σ00≤i i ≤nn 22i i = 2= 200 + 2+ 211 ++……+ 2+ 2nn = 2= 2nn+1+1 -- 11 ∈ Θ(2(2nn ))

Σ((aai i ±± bbi i ) =) = Σaai i ±± Σbbi i Σcacai i == ccΣaai i Σl l ≤i i ≤uuaai i == Σl l ≤i i ≤mmaai i ++ Σmm+1+1≤i i ≤uuaai i

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 23/38

2-22Copyright ©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

E l 2 El i bl

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 24/38

2-23Copyright ©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

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 25/38

2-24Copyright ©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

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 26/38

2-25Copyright ©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(( A A[0..[0..nn--1,0..1,0..nn])])

//Implements Gaussian elimination of an//Implements Gaussian elimination of an nn-- by by--((nn+1) matrix+1) matrix A A

forfor ii←←

00 toto nn -- 22 dodoforfor j j←← ii + 1+ 1 toto nn -- 11 dodo

forfor k k ←← ii toto nn dodo

A A

[[ j j

,,k k ]] ←←

A A

[[ j j

,,k k ]]

-- A A

[[ii,,k k ]] ∗∗

A A

[[ j j

,,ii] /] / A A

[[ii,,ii]]

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

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 27/38

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

Example 5: Counting binary digitsExample 5: Counting binary digits

It cannot be investigated the way the previous examples are.It cannot be investigated the way the previous examples are.

Plan for Analysis of Recursive AlgorithmsPlan for Analysis of Recursive Algorithms

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 28/38

2-27Copyright ©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

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

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

II Check whether the number of times the basic op. is executedCheck whether the number of times the basic op. is executedmay vary on different inputs of the same size. (If it may, themay vary on different inputs of the same size. (If it may, theworst, average, and best cases must be investigatedworst, average, and best cases must be investigated

separately.)separately.)

II Set up a recurrence relation with an appropriate initialSet up a recurrence relation with an appropriate initialcondition expressing the number of times the basic op. iscondition expressing the number of times the basic op. is

executed.executed.

II Solve the recurrence (or, at the very least, establish itsSolve the recurrence (or, at the very least, establish itssolutionsolution’’s order of growth) by backward substitutions ors order of growth) by backward substitutions or

another method.another method.

E l 1 R i l i fE l 1 R i l ti f !!

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 29/38

2-28Copyright ©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: nn ! = 1! = 1 ∗∗ 22 ∗∗ …… ∗∗((nn--1)1) ∗∗ nn forfor nn ≥ ≥ 1 and 0! = 11 and 0! = 1

Recursive definition of Recursive definition of nn!:!: F F ((nn) =) = F F ((nn--1)1) ∗∗ nn forfor nn ≥ ≥ 1 and1 and

F F ((0) = 10) = 1

Size:Size:

Basic operation:Basic operation:

Recurrence relation:Recurrence relation:

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

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 30/38

2-29Copyright ©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) = 01) + 1, M(0) = 0

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

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 31/38

2-30Copyright ©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:

Solving recurrence for number of movesSolving recurrence for number of moves

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 32/38

2-31Copyright ©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) = 11) + 1, M(1) = 1

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

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 33/38

2-32Copyright ©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

E ample 3: Co nting #bitsExample 3: Counting #bits

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 34/38

2-33Copyright ©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

Fibonacci numbersFibonacci numbers

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 35/38

2-34Copyright ©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) = 0F(0) = 0F(1) = 1F(1) = 1

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

constant coefficients:constant coefficients:

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

SolvingSolving aaX(X(nn) +) + bbX(X(nn--1) +1) + ccX(X(nn--2)2) == 00

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 36/38

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

SolvingSolving aaX(X(nn) +) + bbX(X(nn--1) +1) + ccX(X(nn--2)2) = 00

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

ar ar 22 ++ br br ++ cc == 00

II Solve to obtain rootsSolve to obtain roots r r 11 andand r r 22

II General solution to the recurrenceGeneral solution to the recurrenceif if r r 11 andand r r 22 are two distinct real roots: X(are two distinct real roots: X(nn) =) = ααr r 11

nn ++ ββr r 22nn

if if r r 11 == r r 22 == r r are two equal real roots: X(are two equal real roots: X(nn) =) = ααr r nn ++ ββnr nr nn

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

Application to the Fibonacci numbersApplication to the Fibonacci numbers

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 37/38

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

pppp

F(F(nn) = F() = F(nn--1) + F(1) + F(nn--2) or F(2) or F(nn)) -- F(F(nn--1)1) -- F(F(nn--2) = 02) = 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:

Computing Fibonacci numbersComputing Fibonacci numbers

7/28/2019 Chapter_02 Fundamental Analysis

http://slidepdf.com/reader/full/chapter02-fundamental-analysis 38/38

2-37Copyright ©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. DefinitionDefinition--based recursive algorithmbased recursive algorithm

2.2. Nonrecursive definitionNonrecursive definition--based algorithmbased algorithm

3.3. Explicit formula algorithmExplicit formula algorithm

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

F F ((

nn--1)1)

F F ((

nn))

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

0 10 1

1 11 1==

nn

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