Dr. Huma Qayyum Department of Software Engineering Huma - Ayub@uettaxila - Edu.pk
Dr. Huma Qayyum Department of Software Engineering Huma - Ayub@uettaxila - Edu.pk
Dr. Huma Qayyum Department of Software Engineering Huma - Ayub@uettaxila - Edu.pk
Course Code :
Lecture 2
Recurrences, Solution of Recurrences by substitution,
Recursion Tree and Master Method
• 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 : Write the recurrence relation for the following method.
Geometric Series:
Solving Recurrence Relations - Iteration method
Harmonic Series:
Others:
Analysis Of Recursive Binary Search
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);
}
}
• 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
Analysis Of Recursive Binary Search
Expanding:
T(n) = T(n / 2) + b
= [T(n / 4) + b] + b = T (n / 22) + 2b
= [T(n / 8) + b] + 2b = T(n / 23) + 3b
= ……..
= T( n / 2k) + kb
public static void hanoi(int n, char BEG, char AUX, char END){
if (n == 1)
System.out.println(from + " --------> " + to);
else{
hanoi(n - 1, BEG, END, AUX);
System.out.println(from + " --------> " + to);
hanoi(n - 1, END, AUX, BEG);
}
}
• 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
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
= 22 [2T(n – 3) + b] + 2b + b = 23 T(n – 3) + 22b + 2b + b
= 23 [2T(n – 4) + b] + 22b + 2b + b = 24 T(n – 4) + 23 b + 22b + 21b + 20b
= ……
= 2k T(n – k) + b[2k- 1 + 2k– 2 + . . . 21 + 20]
When k = n – 1, we have:
1 if n 1
T ( n)
n
3.T ( ) cn 2 if otherwise
4
Assumption: We assume that n is exact power of 4.
The recurrence tree is given in the next slide
Recursion Tree Method
c.n2
T(n) = 3.T(n/4)+c.n2
c.n2
c.n2 c.n2
2
c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)
(3/16)2.c.n2
2 2 2
c(n/16)2 c(n/16)2 c(n/16) c(n/16)2 c(n/16)2 c(n/16) c(n/16) c(n/16) c(n/16)
2 2
(3/16)2.c.n2
(3/42)k-1 cn2
T(n/4k) T(n/4k) T(n/4k) T(n/4k) T(n/4k) T(n/4k) T(n/4k) T(n/4k)
T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) (nlog43)
Recursion Tree Method
• Now the total computation cost would be
logn4
T ( n) 3 cost at Levels above child level
log3 4 3 0 3 1 3 k 1 2
T ( n) ( n ) ( 2 ) ( 2 ) ( 2 ) cn
4 4 4
Recursion Tree Method
Now total computational cost can be calculated as
log3 4 3 0 3 1 3 k 1 2
T ( n) ( n ) ( 2 ) ( 2 ) ( 2 ) cn
4 4 4
Where 4 h n h log4 n
log3 4 3 0 3 1 2
T ( n ) ( n ) ( 2 ) ( 2 ) cn
4 4
log3 4 1 log3 4 16 2
T ( n ) ( n )( )cn (n
2
) cn
3 13
1 ( )
16
Hence T (n) (n 2 )