Sunday, 29 March 2015

Master Theorem: Practice Problems and Solutions

Master Theorem: Practice Problems and Solutions


Practice Problems For each of the following recurrences, give an expression for the runtime T (n) if the recurrence can be solved with the Master Theorem. Otherwise, indicate that the Master Theorem does not apply.

 1. T (n) = 3T (n/2) + n^2
2. T (n) = 4T (n/2) + n^2
3. T (n) = T (n/2) + 2n
4. T (n) = 2nT (n/2) + n^n
5. T (n) = 16T (n/4) + n
6. T (n) = 2T (n/2) + n log n
7. T (n) = 2T (n/2) + n/ log n
8. T (n) = 2T (n/4) + n ^0.51
9. T (n) = 0.5T (n/2) + 1/n
10. T (n) = 16T (n/4) + n!
11. T (n) = √ 2T (n/2) + log n
12. T (n) = 3T (n/2) + n
13. T (n) = 3T (n/3) + √ n
14. T (n) = 4T (n/2) + cn
15. T (n) = 3T (n/4) + n log n
16. T (n) = 3T (n/3) + n/2
17. T (n) = 6T (n/3) + n^2 log n
18. T (n) = 4T (n/2) + n/ log n
19. T (n) = 64T (n/8) − n^2 log n
20. T (n) = 7T (n/3) + n^2
21. T (n) = 4T (n/2) + log n
22. T (n) = T (n/2) + n(2 − cos n)


Solutions 

1. T (n) = 3T (n/2) + n 2 =⇒ T (n) = Θ(n^2 ) (Case 3)
2. T (n) = 4T (n/2) + n 2 =⇒ T (n) = Θ(n^2 log n) (Case 2)
3. T (n) = T (n/2) + 2n =⇒ Θ(2n ) (Case 3)
4. T (n) = 2nT (n/2) + n n =⇒ Does not apply (a is not constant)
5. T (n) = 16T (n/4) + n =⇒ T (n) = Θ(n^2 ) (Case 1)
6. T (n) = 2T (n/2) + n log n =⇒ T (n) = n log2 n (Case 2)
7. T (n) = 2T (n/2) + n/ log n =⇒ Does not apply (non-polynomial difference between f(n) and n logb a )
8. T (n) = 2T (n/4) + n 0.51 =⇒ T (n) = Θ(n^0.51) (Case 3)
9. T (n) = 0.5T (n/2) + 1/n =⇒ Does not apply (a < 1)
10. T (n) = 16T (n/4) + n! =⇒ T (n) = Θ(n!) (Case 3)
11. T (n) = √ 2T (n/2) + log n =⇒ T (n) = Θ(√ n) (Case 1)
12. T (n) = 3T (n/2) + n =⇒ T (n) = Θ(n^lg 3 ) (Case 1)
13. T (n) = 3T (n/3) + √ n =⇒ T (n) = Θ(n) (Case 1)
14. T (n) = 4T (n/2) + cn =⇒ T (n) = Θ(n^2 ) (Case 1)
15. T (n) = 3T (n/4) + n log n =⇒ T (n) = Θ(n log n) (Case 3)
16. T (n) = 3T (n/3) + n/2 =⇒ T (n) = Θ(n log n) (Case 2)
17. T (n) = 6T (n/3) + n 2 log n =⇒ T (n) = Θ(n^2 log n) (Case 3)
18. T (n) = 4T (n/2) + n/ log n =⇒ T (n) = Θ(n^2 ) (Case 1)
19. T (n) = 64T (n/8) − n 2 log n =⇒ Does not apply (f(n) is not positive)
20. T (n) = 7T (n/3) + n 2 =⇒ T (n) = Θ(n^2 ) (Case 3)
21. T (n) = 4T (n/2) + log n =⇒ T (n) = Θ(n^2 ) (Case 1)
22. T (n) = T (n/2) + n(2 − cos n) =⇒ Does not apply. We are in Case 3, but the regularity condition is violated. (Consider n = 2πk, where k is odd and arbitrarily large. For any such choice of n, you can show that c ≥ 3/2, thereby violating the regularity condition.) 

Analysis of Algorithms (The Master Theorem)

 Master Method:

Master Method is a direct way to get the solution. The master method works only for following type of recurrences or for recurrences that can be transformed to following type.
T(n) = aT(n/b) + f(n) where a >= 1 and b > 1

There are following three cases:

1. If f(n) = \Theta \left(n^{{c}}\right) where c < \log _{b}a then T(n) = \Theta \left(n^{{\log _{b}a}}\right)
2. If f(n) = \Theta \left(n^{{c}}\right) where c = \log _{b}a then T(n) = \Theta \left(n^{{c}}logn\right)
3.If f(n) = \Theta \left(n^{{c}}\right) where c > \log _{b}a then T(n) = \Theta \left(f(n)\right)

How does this work?

Master method is mainly derived from recurrence tree method. If we draw recurrence tree of T(n) = aT(n/b) + f(n), we can see that the work done at root is f(n) and work done at all leaves is \Theta \left(n^{{c}}\right)where c is \log _{b}a. And the height of recurrence tree is \log _{b}n
Master Theorem
In recurrence tree method, we calculate total work done. If the work done at leaves is polynomially more, then leaves are the dominant part, and our result becomes the work done at leaves (Case 1). If work done at leaves and root is asymptotically same, then our result becomes height multiplied by work done at any level (Case 2). If work done at root is asymptotically more, then our result becomes work done at root (Case 3).

Examples of some standard algorithms whose time complexity can be evaluated using Master Method 

Merge Sort: T(n) = 2T(n/2) + \Theta(n). It falls in case 2 as c is 1 and \log _{b}a is also 1. So the solution is \Theta(nLogn)
Binary Search: T(n) = T(n/2) + \Theta(1). It also falls in case 2 as c is 0 and \log _{b}a is also 0. So the solution is \Theta(Logn)
Notes: 
1) It is not necessary that a recurrence of the form T(n) = aT(n/b) + f(n) can be solved using Master Theorem. The given three cases have some gaps between them. For example, the recurrence T(n) = 2T(n/2) + n/Logn cannot be solved using master method.
2) Case 2 can be extended for f(n) = \Theta \left(n^{{c}}\log ^{{k}}n\right).
If f(n) = \Theta \left(n^{{c}}\log ^{{k}}n\right) for some constant k >= 0 and c = \log _{b}a, then T(n) = \Theta \left(n^{{c}}\log ^{{k+1}}n\right)

Analysis of Algorithms (Worst, Average and Best Cases)

In this post, we will take an example of Linear Search and analyze it using Asymptotic analysis.
We can have three cases to analyze an algorithm:
1) Worst Case
2) Average Case
3) Best Case
Let us consider the following implementation of Linear Search.

#include <stdio.h>
// Linearly search x in arr[].  If x is present then return the index,
// otherwise return -1
int search(int arr[], int n, int x)
{
    int i;
    for (i=0; i<n; i++)
    {
       if (arr[i] == x)
         return i;
    }
    return -1;
}
/* Driver program to test above functions*/
int main()
{
    int arr[] = {1, 10, 30, 15};
    int x = 30;
    int n = sizeof(arr)/sizeof(arr[0]);
    printf("%d is present at index %d", x, search(arr, n, x));
    getchar();
    return 0;
}
Worst Case Analysis (Usually Done)

In the worst case analysis, we calculate upper bound on running time of an algorithm. We must know the case that causes maximum number of operations to be executed. For Linear Search, the worst case happens when the element to be searched (x in the above code) is not present in the array. When x is not present, the search() functions compares it with all the elements of arr[] one by one. Therefore, the worst case time complexity of linear search would be \theta(n).

Average Case Analysis (Sometimes done) 

In average case analysis, we take all possible inputs and calculate computing time for all of the inputs. Sum all the calculated values and divide the sum by total number of inputs. We must know (or predict) distribution of cases. For the linear search problem, let us assume that all cases are uniformly distributed (including the case of x not being present in array). So we sum all the cases and divide the sum by (n+1). Following is the value of average case time complexity.

Average Case Time = {\sum_{i=1}^{n+1} \theta (i)}\over{(n+1)}

                  = { \theta ((n+1)*(n+2)/2)}\over{(n+1)}  

                  = { \theta (n)   

Best Case Analysis (Bogus) 

In the best case analysis, we calculate lower bound on running time of an algorithm. We must know the case that causes minimum number of operations to be executed. In the linear search problem, the best case occurs when x is present at the first location. The number of operations in the best case is constant (not dependent on n). So time complexity in the best case would be \theta(1)


Most of the times, we do worst case analysis to analyze algorithms. In the worst analysis, we guarantee an upper bound on the running time of an algorithm which is good information.
The average case analysis is not easy to do in most of the practical cases and it is rarely done. In the average case analysis, we must know (or predict) the mathematical distribution of all possible inputs.
The Best Case analysis is bogus. Guaranteeing a lower bound on an algorithm doesn’t provide any information as in the worst case, an algorithm may take years to run.
For some algorithms, all the cases are asymptotically same, i.e., there are no worst and best cases. For example, Merge Sort. Merge Sort does \theta(nLogn)operations in all cases. Most of the other sorting algorithms have worst and best cases. For example, in the typical implementation of Quick Sort (where pivot is chosen as a corner element), the worst occurs when the input array is already sorted and the best occur when the pivot elements always divide array in two halves. For insertion sort, the worst case occurs when the array is reverse sorted and the best case occurs when the array is sorted in the same order as output.