Recursive Algorithms: Understanding Recurrence Relations and Their Analysis, Study notes of Digital & Analog Electronics

An in-depth analysis of recursive algorithms, focusing on the concept of recurrence relations and their application in determining the time complexity of various recursive methods, including recursive factorial, selection sort, binary search, and towers of hanoi. It covers the process of forming recurrence relations, solving them, and understanding the big-o complexity of each method.

Typology: Study notes

2010/2011

Uploaded on 09/02/2011

hamit1990
hamit1990 🇮🇳

4.3

(76)

95 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Analysis of Recursive Algorithms
What is a recurrence relation?
Forming Recurrence Relations
Analysis Of Recursive Factorial method
Analysis Of Recursive Selection Sort
Analysis Of Recursive Binary Search
Analysis Of Recursive Towers of Hanoi Algorithm
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Recursive Algorithms: Understanding Recurrence Relations and Their Analysis and more Study notes Digital & Analog Electronics in PDF only on Docsity!

Analysis of Recursive Algorithms

  • (^) What is a recurrence relation?
  • (^) Forming Recurrence Relations
  • (^) Analysis Of Recursive Factorial method
  • (^) Analysis Of Recursive Selection Sort
  • (^) Analysis Of Recursive Binary Search
  • (^) Analysis Of Recursive Towers of Hanoi Algorithm

What is a recurrence relation?

  • (^) A recurrence relation, T(n), is a recursive function of integer variable n.
  • (^) Like all recursive functions, it has both recursive case and base case.
  • (^) Example:
  • (^) The portion of the definition that does not contain T is called the base

case of the recurrence relation; the portion that contains T is called the

recurrent or recursive case.

  • (^) Recurrence relations are useful for expressing the running times (i.e.,

the number of basic operations executed) of recursive algorithms

Forming Recurrence Relations

  • (^) Example 2: Write the recurrence relation for the following method.
  • (^) The base case is reached when n == 1. The method performs one

comparison and one return statement. Therefore, T(1), is constant c.

  • (^) When n > 1 , the method performs TWO recursive calls, each with the

parameter n / 2, and some constant # of basic operations.

  • (^) Hence, the recurrence relation is:

public int g(int n) {

if (n == 1)

return 2;

else

return 3 * g(n / 2) + g( n / 2) + 5;

Analysis Of Recursive Factorial method

 Example1: Form and solve the recurrence relation for the running time

of factorial method and hence determine its big-O complexity:

T(0) = c T(n) = b + T(n - 1) = b + b + T(n - 2) = b +b +b + T(n - 3) … = kb + T(n - k) When k = n, we have: T(n) = nb + T(n - n) = bn + T(0) = bn + c. Therefore method factorial is O(n). long factorial (int n) { if (n == 0) return 1; else return n * factorial (n – 1); }

Analysis Of Recursive Selection Sort

  • (^) findMinPos is O(n), and swap is O(1), therefore the recurrence relation for

the running time of the selectionSort method is:

T(0) = a

T(n) = T(n – 1) + n + c n > 0

= [T(n-2) +(n-1) + c] + n + c = T(n-2) + (n-1) + n + 2c

= [T(n-3) + (n-2) + c] +(n-1) + n + 2c= T(n-3) + (n-2) + (n-1) + n + 3c

= T(n-4) + (n-3) + (n-2) + (n-1) + n + 4c

= T(n-k) + (n-k + 1) + (n-k + 2) + …….+ n + kc

When k = n, we have :

Therefore, Recursive Selection Sort is O(n

2

Analysis Of Recursive Binary Search

• The recurrence relation for the running time of the method is:

T(1) = a if n = 1 (one element array)

T(n) = T(n / 2) + b if n > 1

public int binarySearch (int target, int[] array, int low, int high) { if (low > high) return -1; else { int middle = (low + high)/2; if (array[middle] == target) return middle; else if(array[middle] < target) return binarySearch(target, array, middle + 1, high); else return binarySearch(target, array, low, middle - 1); } }

Analysis Of Recursive Towers of Hanoi Algorithm

• The recurrence relation for the running time of the method

hanoi is:

T(n) = a if n = 1

T(n) = 2T(n - 1) + b if n > 1

public static void hanoi(int n, char from, char to, char temp){ if (n == 1) System.out.println(from + " --------> " + to); else{ hanoi(n - 1, from, temp, to); System.out.println(from + " --------> " + to); hanoi(n - 1, temp, to, from); } }

Analysis Of Recursive Towers of Hanoi Algorithm

Expanding: T(n) = 2T(n – 1) + b = 2[2T(n – 2) + b] + b = 22 T(n – 2) + 2b + b = 2^2 [2T(n – 3) + b] + 2b + b = 23 T(n – 3) + 2^2 b + 2b + b = 2^3 [2T(n – 4) + b] + 2^2 b + 2b + b = 24 T(n – 4) + 2^3 b + 2^2 b + 2^1 b + 2^0 b = …… = 2 k^ T(n – k) + b[2k- 1^ + 2k– 2^ +... 2^1 + 2^0 ] When k = n – 1, we have:

Therefore, The method hanoi is O(

n