Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COPM2123

Tutorial 11: Divide and Conquer II

s1 2023

Warm-up

Problem 1.  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  = k(n)=1 XikYkj . Suppose that X and Y are divided into four n/2 × n/2 blocks each:

X = [C(A)   D(B)] and Y = [G(E)   H(F)] .

Using this block notation we can express the product of X and Y as follows

XY = ] .

In this way, one multiplication of n × n matrices can be expressed in terms of 8 multiplications and 4 additions that involve n/2 × n/2 matrices.  Let T(n) be the time complexity of multiplying two n × n matrices using this recursive algorithm.

a) Derive the recurrence for T(n).   (Assume adding two k × k matrices takes O(k2 ) time.)

b) Solve the recurrence by unrolling it.

Problem 2. Similar to the integer multiplication algorithm, there are algebraic iden- tities that allow us to express the product of two n × n matrices in terms of 7 mul- tiplications and O(1) additions involving n/2 × n/2 matrices. Let T(n) be the time complexity of multiplying two n × n matrices using this recursive algorithm.

a) Derive the recurrence for T(n).   (Assume adding two k × k matrices takes O(k2 ) time.)

b) Solve the recurrence by unrolling it.

Problem solving

Problem 3. Your friend Alex is very excited because they have discovered a novel algorithm for sorting an array of n numbers. The algorithm makes three recursive calls on arrays of size  and spends only O(1) time per call.

1:  function new-sort(A)

2:         if |A| < 3 then

3:               Sort A directly

4:         else

5:               new-sort(A[0 : 2n/3])

6:               new-sort(A[n/3 : n])

7:               new-sort(A[0 : 2n/3])

 

Alex thinks their breakthrough sorting algorithm is very fast but has no idea how to analyze its complexity or prove its correctness. Your task is to help Alex:

a) Find the time complexity of new-sort.

b) Prove that the algorithm actually sorts the input array.

Problem 4. 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 finding a locally optimal index using divide and con- quer. Your algorithm should run in O(log n) time.

Problem 5.  Given two sorted lists of size m and n.  Give an O(log(m + n)) time algorithm for finding the kth smallest element in the union of the two lists.

Problem 6. Solve the following recurrences using the Master Theorem or unrolling (the solutions will use the Master Theorem to allow you to practice that).  All are O(1) for n = 1.

a) T(n) = 4T(n/2) + O(n2 )

b) T(n) = T(n/2) + O(2n )

c)  T(n) = 16T(n/4) + O(n)

d) T(n) = 2T(n/2) + O(n log n)

e) T(n) =^2T(n/2) + O(log n)

f)  T(n) = 3T(n/2) + O(n)

g) T(n) = 3T(n/3) + O(^n)