



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
[Week 6] Master Theorem, Recurrence Relations
Typology: Exercises
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Algorithms 9007 Tutorial questions 2019 Semester 1 Tutorial 5
The University of Sydney School of Computer Science
Do you know the basic concepts of this week's lecture content? These questions are only to test yourself. They will not be explicitly discussed in the tutorial, and no solutions will be given to them.
(a) What is the general idea of a Divide-and-Conquer algorithm? Can you break up the general structure into three steps? (b) Why are recursions usually needed to analyse the running time of a Divide-and-Conquer algo- rithm?
(a) Describe the general idea of merge sort. (b) What are the three main steps in the algorithm for counting inversions?
(a) State the master method. (b) Try to explain the master method using a recursion tree.
Problem 1 Solve the following recurrences:
p 2 T (n/2) + logn
p n
Solution: First we state the Master Theorem. The Master Theorem applies to recurrences of the following form: T (n) = aT (n/b) + f (n) where a 1 and b > 1 are constants and f (n) is an asymptotically positive function. There are three cases:
p 2 T (n/2) + logn! T (n) = (
p n) (Case 1)
p n! T (n) = (n) (Case 1)
Problem 2 Consider the following algorithm.
Algorithm 1 Reverse 1: function reverse(A) 2: if jAj = 1 then 3: return A 4: else 5: Let B and C be the rst and second half of A 6: return concatenate reverse(C) and reverse(B) 7: end if 8: end function
Let T (n) be the running time of the algorithm on a instance of size n. Write down the recurrence relation for T (n) and solve it by unrolling it.
Solution: T (n) = 2 T
( (^) n 2
Problem 3 The product of two n n matrices X and Y is a third n n matrix Z = XY , where the (i, j) entry of Z is Zij =
∑n k=1 XikYkj^. Suppose that^ X^ and^ Y^ are divided into four^ n/^2 ^ n/2 blocks each:
and Y =
Solution:
n log 323 )
. That's roughly O(n^2.^71 ). Much worse that bubble sort! Alex is heartbroken...
1 3 of the elements in sorted order are placed in the middle 13 of the array. Within each 13 of the array the elements are sorted, so the array is indeed sorted at the end of the algorithm.
Problem 5 Given an array A holding n objects, we want to test whether there is a majority element; that is, we want to know whether there is an object that appears in more than n/2 positions of A. Assume we can test equality of two objects in O(1) time, but we cannot use a dictionary indexed by the objects. Your task is to design an O(n log n) time algorithm for solving the majority problem.
Solution:
If x is a majority element in the original array then, by the pigeon-hole principle, x must be a majority element in at least one of the two halves. Suppose that n is even. If x is a majority element at least n/2 + 1 entries equal x. By the pigeon hole principle either the rst half or the second half must contain n/4 + 1 copies of x, which makes x a majority element within that half.
We scan the array counting how many entries equal x. If the count is more than n/ 2 we declare x to be a majority element. The algorithm scans the array and spends O(1) time per element, so O(n) time overall.
We break the input array A into two halves, recursively call the algorithm on each half, and then test in A if either of the elements returned by the recursive calls is indeed a majority element of A. If that is the case we return the majority element, otherwise, we report that there is \no majority element". To argue the correctness, we see if there is no majority element of A then the algorithm must return \no majority element" since no matter what the recursive call return, we always check if an element is indeed a majority element. Otherwise, if there is a majority element x in A we are guaranteed that one of our recursive call will identify x and the subsequent check will lead the algorithm to return x. Regarding time complexity, breaking the main problem into the two subproblems and testing the two candidate majority elements can be done in O(n) time. Thus we get the following recurrence T (n) = 2T (n/2) + O(n).
T (n) = O(n log n).
Problem 6 Suppose we are given an array A with n distinct numbers. We say an index i is locally optimal if A[i] < A[i 1] and A[i] < A[i + 1] for 0 < i < n 1, or A[i] < A[i + 1] for if i = 0, or A[i] < A[i 1] for i = n 1. Design an algorithm for nding a locally optimal index using divide an conquer. Your algorithm should run in O(log n) time.
Solution: First we test whether i = 1 or i = n are locally optimal entries. Otherwise, we know that A[1] > A[2] and A[n 1] < A[n]. If n 4, it is easy to see that either i = 2 or i = 3 is locally optimal and we can check that in O(1) time. Otherwise, pick the middle positions in the array for example i = ⌈n/ 2 ⌉ and test whether i is locally optimal. If it is we are done, other wise A[i 1] < A[i] or A[i] > A[i + 1]; in the former case we recurse on A[1,... , i] in the latter we recurse on A[i,... , n]. Either way, we reduce the size of the array by half. Rather than creating a new array each time we recurse (which would take O(n) time) we can keep the working subarray implicitly by maintaining a pair of indices (b, e) telling us that we are working with the array A[b,... , e]. Thus, each call demands O(1) work plus the work done by recursive calls. This leads to the following recurrence,
T (n) = T (n/2) + O(1),
which solves to T (n) = O(log n).