recursion and recurrence equations

25
Recursion and Recurrence Equations The execution of a recursive program Deriving recurrence equations

Upload: uttara

Post on 25-Feb-2016

45 views

Category:

Documents


2 download

DESCRIPTION

Recursion and Recurrence Equations. The execution of a recursive program Deriving recurrence equations. Recursion. What is the recursive definition of n ! ? Translating to Java/c int fact(int n) { if (n

TRANSCRIPT

Page 1: Recursion and Recurrence Equations

Recursion and Recurrence Equations

• The execution of a recursive program• Deriving recurrence equations

Page 2: Recursion and Recurrence Equations

04/22/23 Cutler/Head 2

Recursion

• What is the recursive definition of n! ?

• Translating to Java/cint fact(int n) {

if (n<=1) return 1; // Note '*' is done after returning from fact(n-1)

else return n*fact(n-1);}

otherwise )1)!-((1or 0 is if 1

!nn

nn

Page 3: Recursion and Recurrence Equations

04/22/23 Cutler/Head 3

Recursive algorithms

• A recursive algorithm typically contains recursive calls to the same algorithm

• In order for the recursive algorithm to terminate, it must contain code for solving directly some “base case(s)”

• A direct solution does not include recursive calls. • We use the following notation:• DirectSolutionSize is the “size” of the base case• DirectSolutionCount is the count of the number of

operations done by the “direct solution”

Page 4: Recursion and Recurrence Equations

04/22/23 Cutler/Head 4

fact(3)

3

2

1

fact(2)

fact(1)

A Call Tree for fact(3) The Run Time Environment

• The following 2 slides show the program stack after the third call from fact(3)

• When a function is called an activation records('ar') is created and pushed on the program stack.

• The activation record stores copies of local variables, pointers to other ‘ar’ in the stack and the return address.

• When a function returns the stack is popped.int fact(int n) {

if (n<=1) return 1;else return n*fact(n-1);}

returns 6

1

*

*2

6

Page 5: Recursion and Recurrence Equations

04/22/23 Cutler/Head 5

ep2

Program stack

Valfact: <ipF,ep0>

N =3function value [=?]

return addressstatic link = ep0

dynamic link =ep1

free memory

AR(Driver)

AR(fact 3)

Snapshot of the environmentat end of third call to fact

ep0

N =2function value [=?]

return addressstatic link = ep0

dynamic link =ep2

AR(fact 2)

ep1

EP

N =1function value [=1]

return addressstatic link = ep0

dynamic link = EP

AR(fact 1)

AR=Activation Recordep= environmental pointer

EP

Program stack

Valfact: <ipF,ep0>

N =3function value [=?]

return addressstatic link = ep0

dynamic link = ep1

free memory

AR(Driver)

AR(fact 3)

Snapshot of the environmentat end of second call to fact

ep0

N =2function value [=2]

return addressstatic link = ep0

dynamic link = EP

AR(fact 2)

ep1

Page 6: Recursion and Recurrence Equations

04/22/23 Cutler/Head 6

EP

Program stack

Val

fact: <ipF,ep0>

N =3

function value [=6]

return address

static link = ep0

dynamic link = EP

free memory

AR(Driver)

AR(fact 3)

Snapshot of the environment

at end of first call to fact

ep0EP

Program stack

Val

fact: <ipF,ep0>

free memory

AR(Driver)

Snapshot of the environment

after fact l returns

Page 7: Recursion and Recurrence Equations

04/22/23 Cutler/Head 7

Goal: Analyzing recursive algorithms

• Until now we have only analyzed (derived a count) for non-recursive algorithms.

• In order to analyze recursive algorithms we must learn to:

• Derive the recurrence equation from the code• Solve recurrence equations.

Page 8: Recursion and Recurrence Equations

04/22/23 Cutler/Head 8

Deriving a Recurrence Equation fora Recursive Algorithm

• Our goal is to compute the count (Time) T(n) as a function of n, where n is the size of the problem

• We will first write a recurrence equation for T(n)For example T(n)=T(n-1)+1 and T(1)=0

• Then we will solve the recurrence equationWhen we solve the recurrence equation above we will

find that T(n)=n, and any such recursive algorithm is linear in n.

Solving: Next lecture

Page 9: Recursion and Recurrence Equations

04/22/23 Cutler/Head 9

Deriving a Recurrence Equation for a Recursive Algorithm

1. Determine the “size of the problem”. The count T is a function of this size

2. Determine DirectSolSize, such that for size DirectSolSize the algorithm computes a direct solution, and the DirectSolCount(s).

otherwise

)(

ntGeneralCouizeDirectSolSsizeountDirectSolC

sizeT

Page 10: Recursion and Recurrence Equations

04/22/23 Cutler/Head 10

Deriving a Recurrence Equation for a Recursive Algorithm

To determine GeneralCount:

3. Identify all the recursive calls done by a single call of the algorithm and their counts, T(n1),…,T(nk) and

compute RecursiveCallSum =

4. Determine the “non recursive count” t(size) done by a single call of the algorithm, i.e., the amount of work, excluding the recursive calls done by the algorithm

otherwise )(Re

)(

sizetlSumcursiveCalizeDirectSolSsizeountDirectSolC

sizeT

)(1

k

iinT

Page 11: Recursion and Recurrence Equations

04/22/23 Cutler/Head 11

Deriving DirectSolutionCount for Factorial

int fact(int n) {if (n<=1) return 1;

// Note '*' is done after returning from fact(n-1)else

return n*fact(n-1);1. Size = n 2.DirectSolSize is (n<=1)3. DirectSolCount is (1)

The algorithm does a small constant number of operations (comparing n to 1, and returning)

Page 12: Recursion and Recurrence Equations

04/22/23 Cutler/Head 12

Deriving a GeneralCount for Factorial

int fact(int n) {if (n<=1) return 1;

// Note '*' is done after returning from fact(n-1)else

return n * fact(n-1);

3. RecursiveCallSum = T( n - 1)

4. t(n) = (1) (for if, *, -, return)

The only recursive call, requiring T(n - 1) operations

1for )1()1(

1for )1()(

nnTn

nT

Operationscounted in t(n)

Page 13: Recursion and Recurrence Equations

04/22/23 Cutler/Head 13

Deriving a Recurrence Equation for Mystery

In this example we are not concerned with what mystery computes. Our goal is to simply write the recurrence equation

mystery (n)1. if n 12. then return n3. else return (5 * mystery(n -1) -

6* mystery(n -2) )

Page 14: Recursion and Recurrence Equations

04/22/23 Cutler/Head 14

Deriving a DirectSolutionCount for Mystery1. Size = n

2. DirectSolSize is (n<=1)3. DirectSolCount is (1)

The algorithm does a small constant number of operations (comparing n to 1, and returning)

1for

1for )1()(

nntGeneralCoun

nT

Page 15: Recursion and Recurrence Equations

04/22/23 Cutler/Head 15

Deriving a GeneralCount for Mystery

mystery (n)1. if n 12. then return n3. else

return (5 * mystery(n -1) -

6 * mystery(n - 2) )

A recursive call toMystery requiring T(n - 1)

A recursive call toMystery requiring T(n - 2)

Operations counted in t(n)

Page 16: Recursion and Recurrence Equations

04/22/23 Cutler/Head 16

Deriving a Recurrence Equation for Mystery

3. RecursiveCallSum = T(n-1) + T(n-2)4. t (n)= (1)

The non recursive count includes the comparison n<=1, the 2 multiplications, the subtraction, and the return. So (1).

1for )1()2()1(

1for )1()(

nnTnTn

nT

Page 17: Recursion and Recurrence Equations

04/22/23 Cutler/Head 17

Deriving a Recurrence Equation for MultiplyMultiply(y, z)//binary multiplication1. if (z ==0) return 02.else if z is odd3.then return (multiply(2y,z/2) + y)4.else return (multiply(2y,z/2))

To derive the recurrence equation we need to determine the size of the problem. The variable which is decreased and provides the direct solution is z. So we can use z as the size. Another possibility is to use n which is the number of bits in z (n=lg z+1).

Let addition be our barometer operation. We do 0 additions for the direct solution. So DirectSolutionSize =0, and DirectSolutionCount=0

Barometeroperation

Page 18: Recursion and Recurrence Equations

04/22/23 Cutler/Head 18

Deriving a Recurrence Equation for Multiply

1. RecursiveCallSum = T(z/2)t(z) = 1 addition in the worst case. So

Solving this equation we get T(z)= (lg z)= (n).

2. RecursiveCallSum = T(n-1) since z/2 has n-1 bits

Solving this equation we get T(n)= (n)

0for 1)1(

0for 0)(

nnTn

nT

0for 1)2/(

0for 0)(

zzTz

zT

Page 19: Recursion and Recurrence Equations

04/22/23 Cutler/Head 19

Deriving a recurrence equation for minimumMinimum(A,lt,rt) // minimum(A,1,length(A))1. if rt - lt 12.then return (min(A[lt], A[rt]))3.else m1 = minimum(A,lt, (lt+rt)/2)4. m2 = minimum(A,(lt+rt)/2+1,rt)5. return (min (m1, m2))• This procedure computes the minimum of the left

half of the array, the right half of the array, and the minimum of the two.

• Size = rt-lt+1= n • DirectSolutionSize is n = (rt - lt)+1 1+1=2.• DirectSolutionCount = (1)

Page 20: Recursion and Recurrence Equations

04/22/23 Cutler/Head 20

Deriving a recurrence equation for minimumMinimum(A,lt,rt) // minimum(A,1,length(A))1. if rt - lt 12.then return (min(A[lt], A[rt]))

3.else m1 = minimum(A,lt, (lt+rt)/2)

4. m2 = minimum(A,(lt+rt)/2+1,rt)5. return (min (m1, m2))

Two recursive calls with n/2 elements.So T(n/2) + T(n/2)

2for )1()2/(2

2for )1()(

nnTn

nT

Page 21: Recursion and Recurrence Equations

04/22/23 Cutler/Head 21

Deriving a recurrence equation for Merge Sort:

1 if (n 2) 2 moveFirst( S, S1, (n+1)/2)) // divide3 moveSecond( S, S2, n/2) // divide4 Sort(S1) // recurs5 Sort(S2) // recurs6 Merge( S1, S2, S ) // conquers

DirectSolutionSize is n<2 DirectSolutionCount is (1) RecursiveCallSum is T( n/2) + T( n/2) The non recursive count t(n) = (n)

Page 22: Recursion and Recurrence Equations

04/22/23 Cutler/Head 22

Recurrence Equation for cost.

• The implementation of the moves costs (n) and the merge costs (n). So the total cost for dividing and merging is (n).

• The recurrence relation for the run time of Sort is T(n) = T( n/2) + T( n/2) + (n) . T(0) =T(1) = (1)

• The solution is T(n)= (nlg n)

Page 23: Recursion and Recurrence Equations

04/22/23 Cutler/Head 23

Computing xn

• We can decrease the number of times that x is multiplied by itself by observing that to compute x8 we need only 3 multiplications

(x2=x*x, x4=x2*x2, and x8=x4*x4) and when we want of compute x9, we need 4 multiplications

(x2=x*x, x4=x2*x2, x8=x4*x4 and x9= x8*x)• A recursive procedure for computing xn should be (lg

n)• For the following algorithm we will derive the worst

case recurrence equation. • Note: we are ignoring the fact that the numbers will

quickly become too large to store in a long integer

Page 24: Recursion and Recurrence Equations

04/22/23 Cutler/Head 24

Recursion - Power

long power (long x, long n) { if (n == 0) {

return 1; } else { long p = power(x, n/2); if ((n % 2) == 0) {

return p*p; } else {

return x * p*p;} } }We use the * as our barometer operation.

Page 25: Recursion and Recurrence Equations

04/22/23 Cutler/Head 25

Deriving a Recurrence Equation for PowerSize = nDirectSolutionSize is n=0; DirectSolutionCount is 0; RecursiveCallSum is T(n/2)t(n) = 2 multiplications in the worst case

T(n)=0 for n=0T(n)=T(n/2)+2 for n>0

//