Recurrence Relation and Recursion
Recurrence Relation and Recursion
• Recurrence Relations
• Recursion
2
Recurrence Relations
3
Recurrence Relations
4
Recursion can be solved by Mathematical Induction
◼ In both these concepts, we have general and boundary conditions, with the general
condition breaking the larger problem into smaller problems.
5
Techniques for solving Recurrence Relations
6
Techniques for solving Recurrence Relations
7
Induction Approach
Tn = 2Tn-1 + 1 ; T0 = 0
Prove: Tn = 2n - 1 by induction:
Solution:
8
Example: Merge sort analysis
Mergesort(array)
n = size(array)
if ( n = = 1) return array
array1 = Mergesort(array[1 .. n/2])
array2 = Mergesort(array[n/2 + 1 .. n])
return Merge(array1, array2)
Recurrence relation for Merge Sort will be: T(n) = 2T( n/2 ) + n , T(1) = 1
9
Merge Sort Induction Proof
Base cases:
T(2) = 4 ≤ c 2 lg 2
T(3) = 5 ≤ c 3 lg 3
c ≥ 2 suffices
10
Merge Sort Induction Proof
11
Some complications that might come in the way
◼ We need to reach the conclusion that T (n) <= c*n , and to do this we must
determine a number c such that c*n +1 <=c*n .
◼ But this inequality is obviously false for all positive c. Apparently the induction
step cannot be made to work, which might lead us to believe that our guess is
incorrect.
12
Master Method
◼ It can be tedious and difficult to work out some recurrences. The master method
provides a way to generalize the asymptotic qualities of recurrences of the form:
◼ If you memorize three cases, most recurrences are solved very quickly.
13
Master Method
14
Master Method
15
Examples of the Master Theorem
◼ T(n) = 4 T(n/2) + n
16
Examples of the Master Theorem
◼ T(n) = 4 T(n/2) + n2
Is ?
17
Examples of the Master Theorem
◼ T(n) = 4 T(n/2) + n3
Is ?
Yes, for c>= ½ , so there exists a c<1 to satisfy the regular condition,
so case 3 applies and T(n) = Θ(n3)
18
◼ In this presentation, we learnt
the concept of recursion as a strategy for divide and conquer
Learnt how to develop recurrence relation from a given code, and find their
complexities
recurrence relations and the two approaches to solve them
19
Introduction to Divide and Conquer
◼ Divide and conquer attempts to solve larger problems by dividing then into smaller
problems and apply the same strategy to solve these problems.
One option is to deploy 10000 tele-callers and ask them to call just 10 persons each, but
that would be costly.
Another options is to call 10 of their managers and delegate them this job. They will in
turn delegate this to 10 other employees. This delegation will continue until there are
employees who will end up calling 10 people to spread their msg.
20
Introduction to Divide and Conquer
21
Introduction to Divide and Conquer
◼ You may see that the nature of the problem is same, contacting x employees to
delegate so that each time the number of employees is reduced by a factor of 10.
◼ To implement this concept in programming this would mean you are calling the
function itself, each time reducing the problem by a factor ( in this case, a factor of
10)
22
Some examples
23
Some examples
◼ An iterative solution
int factorial(int n)
{
int x, y;
x = 1;
for ( y=n; y>=1; y--)
x = x*y;
return ( x );
}
24
Some examples
◼ A recursive solution
int factorial(int n)
{
if (n==0) return (1);
else return (n * factorial(n-1));
}
A step by step evaluation:
factorial (5) = 5 * factorial (4)
= 5 * 4 * factorial (3)
= 20 * 3 * factorial (2)
= 60 * 2 * factorial (1)
= 120 * 1 * factorial (0)
= 120
◼ This is called tracing of a recursive function
25
Some examples
◼ Exponentiation
◼ Xn = X * Xn-1
26
Some examples
◼ Exponentiation
◼ Xn = X * Xn-1
27
Some examples
◼ Exponentiation – another version
◼ Xn = (X2)n/2 if n is even
◼ = (X2)n/2 * X if n is odd
Which has better time complexity between the two approaches, and how do we prove it?
28
Time Complexity Analysis of Recursive Functions
◼ Let's take both the versions of Exponentiation:
29
Time Complexity Analysis of Recursive Functions
◼ Xn = X * Xn-1
T(n) = T(n-1) + 2,
T(1) =1
30
Time Complexity Analysis – Recursive Functions
◼ To solve T(n) = T(n-1) + 2, T(1) =1, we can see that
T(n – 1 ) = T(n – 2 ) + 2
Implies T(n ) = T(n – 2 ) + 2 (2)
T(n ) = T(n – 3 ) + 2 (3)
Hence, we can write:
T(n ) = T(n – k ) + 2(k)
We can also see that T(1) =1
Let us set n-k =1 (we want to eliminate the T(n-k) term)
Hence k = n-1
This implies
T(n) = T(1) + 2(n – 1 )
and so, T(n) = 2n– 1
31
Time Complexity Analysis – Recursive Functions
◼ Xn = (X2)n/2 if n is even
◼ = (X2)n/2 * X if n is odd
32
Time Complexity Analysis – Recursive Functions
To solve T(n) = T(n/2) + 3 , T(1) =1, we can see that
T(n/2) = T(n/4) + 3
T(n) = T(n/4) + 3(2)
= T(n/8) + 3(3)
…
implies T(n) = T( n/ 23 ) + 3(3)
or T(n) = T( n/ 2k) + 3(k)
As T(1) =1, lets try some substitution that would make this possible.
Let 2k = n then k = log(n)
Then T(n) = T(1) + 3 log(n)
33
Some examples
if n= 0 then return;
For i := 1 to n do
34
Some examples
Algorithm Method1(A[1..n], B[1..n], C[1..n]);
if n= 0 then return;
For i := 1 to n do
C[1] := A[1] * B[i];
call Method1(A[2..n], B[2..n], C[2..n]);
Find T(n)?
36
Some examples
37
Some examples
if(n == 1) return 0;
Find T(n)
38
Some examples
if(n == 1) return 0;
39