10
Recursion Trees 1 Recursion is a concept of defining a method that makes a call to itself.

Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself

Embed Size (px)

Citation preview

Page 1: Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself

Recursion

Trees 1

Recursion is a concept of defining a method that makes a call

to itself.

Page 2: Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself

Recursion

Trees 2

Recursion is a concept of defining a method that

makes a call to itself.

Page 3: Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself

Factorial

Example: f(n)=n!=n×(n-1)×(n-2)×…×2×1 Initialization: f(0)=1Recursive Call: f(n)=n×f(n-1) and.

Java code:

public static int recursiveFactorial(int n) { if (n==0) return 1; else return n*recursiveFactorial(n-1);}

Trees 3

Page 4: Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself

L16 4

Fibonacci sequenceFibonacci sequence:{fn } = 0,1,1,2,3,5,8,13,21,34,55,…Initialization: f0 = 0, f1 = 1Recursive Call: fn = fn-1+fn-2 for n > 1.

Java code:

public static int recursiveFibonacci(int n) { if (n==0) return 0; if (n==1) return 1; else return recursiveFibonacci(n-1)+recursiveFibonacci (n-2);}

Page 5: Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself

LinearSum

Trees 5

LinearSum(A,5)

LinearSum(A,4)

LinearSum(A,3)

LinearSum(A,2)

LinearSum(A,1)

Algorithm LinearSum(A, n)

Input: an integer array A of n elements

Output: The sum of the n elements

if n=1 then

return A[0]

return LinearSum(A, n-1)+A[n-1]

• The recursive method should always possess—the method terminates.

•We did it by setting :

•” if n=1 then return A[0] ”

return A[0]=4

return 4+A[1]=7

return 7+A[2]=13

return 13+A[3]=15

return 15+A[4]=20

A={4,3,6,2,5}

The compiler of any high level computer language uses a stack to

handle recursive calls.f(n)=A[n-1]+f(n-1) for n>0 and f(1)=A[0]

Page 6: Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself

Factorial

Trees 6

recursiveFactorial(4)

recursiveFactorial(3)

recursiveFactorial(2)

recursiveFactorial(1)

recursiveFactorial (0)

public static int recursiveFactorial(int n) if (n==0) return 1; return n*recursiveFactorial(n-1);}

The recursive method should always possess—the method terminates.

•We did it by setting:

•” if n=0 then return 1 ” return f(0)=1

return f(1)=1*1=1

return f(2)=2*f(1)=2

return f(3)=3*f(2)=6

return f(4)=4*f(3)=24

f(n)=n*f(n-1) for n>0

f(0)=1.

n=4

Page 7: Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself

L16 7

Fibonacci sequencepublic static int recursiveFibonacci(int

n) { if (n==0) return 0; if (n==1) return 1; return recursiveFibonacci(n-1) +recursiveFibonacci (n-2);}

Page 8: Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself

ReverseArrayAlgorithm ReverseArray(A, i, j):input: An array A and nonnegative integer indices i and joutput: The reversal of the elements in A starting at index i

and ending at j if i<j then { swap A[i] and A[j] ReverseArray(A, i+1, j-1)} }

Trees 8

ReverseArray(A, 0, 3)

ReverseArray(A, 1 2)

A=(4,3,2,1}

A={4,2,3,1}

A={1, 2, 3, 4}.

What is the base case?

Page 9: Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself

FindMaxAlgorithm FindMax(A, i, j):input: Array A , indices i and j, i≤j output: The maximum element starting i

and ending at j if i<j then 1 { a←FindMax(A, i, (i+j)/2) T(n/2)+1

b←FindMax(A, (i+j)/2+1, j) T(n/2)+1 return max(a, b) 1 } return A[i] 1

Trees 9

Running time:T(n)=2T(n/2)+c1

T(1)=c2

where c1 and c2 are some constants.

T(n)=2T(n/2)+c1

=2[2T(n/4)+c1]+c1

=4T(n/4)+3c1

=… =2kT(1)+(1+2+4+…2k)c1

=nT(1)+2k+1 c1 =O(n)

Page 10: Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself

Binary SearchAlgorithm BinarySearch(A, i, j, key):input: Sorted Array A , indices i and j, i≤j,

and key output: If key appears between elements

from i to j, inclusively if i≤j mid (i + j) / 2 if A[mid] = key return mid if A[mid] < key return BinarySearch(A, mid+1, j, key) else return BinarySearch(A, i, mid-1, key) return -1

Trees 10

Running time:T(n)=T(n/2)+c1

T(1)=c2

where c1 and c2 are some constants.

T(n)=T(n/2)+c1

=[T(n/4)+c1]+c1

=T(n/4)+2c1

=… =T(1) + kc1 =?