Docsity

CMPS 214 Problem Set 2: Recurrence Relations and Algorithm Analysis, Exercises of Algorithms and Programming

Toggle sidebar
bg1
CMPS 214 Problem Set 2
Style guide and expectations: Please see the “Homework” part of the syllabus for guidance
on what we are looking for in homework solutions. We will grade according to these standards.
You should cite all sources you used outside of the course material.
What we expect: Make sure to look at the “We are expecting” blocks below each problem
to see what we will be grading for in each problem!
Exercises
We suggest you do these on your own. As with any homework problem, though, you may
ask the course staff for help.
1. There exist different ways to solve the recurrence relation T(n)=2·T(n/2) + nwith
T(1) = 1. From lectures, we have seen that T(n)is exactly n(1 + log(n)) when nis a
power of 2. In this exercise, you’ll analyze a few more recurrences for a few variants.
(a) What is the exact solution to T(n)=3·Tn
3+nwith T(1) = 3, when nis a
power of 3?
(b) What is the exact solution to T(n) = 3 ·Tn
3+ 3nwith T(1) = 1, when nis a
power of 3?
[We are expecting: Your answer — no justification required. Notice that we want
the exact answer, so don’t give an O() statement.]
Solution:
a)
T(n)=3·T(n
3) + n
=nlog3n+n·T(1)
So,
T(3m)=3mlog33m+ 3m·T(1)
= 3m(mlog33 + T(1))
= 3m(m+ 3)
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CMPS 214 Problem Set 2: Recurrence Relations and Algorithm Analysis and more Exercises Algorithms and Programming in PDF only on Docsity!

CMPS 214 Problem Set 2

Style guide and expectations: Please see the “Homework” part of the syllabus for guidance on what we are looking for in homework solutions. We will grade according to these standards. You should cite all sources you used outside of the course material.

What we expect: Make sure to look at the “We are expecting” blocks below each problem to see what we will be grading for in each problem!

Exercises

We suggest you do these on your own. As with any homework problem, though, you may ask the course staff for help.

  1. There exist different ways to solve the recurrence relation T (n) = 2 · T (n/2) + n with T (1) = 1. From lectures, we have seen that T (n) is exactly n(1 + log(n)) when n is a power of 2. In this exercise, you’ll analyze a few more recurrences for a few variants.

(a) What is the exact solution to T (n) = 3 · T

(n 3

  • n with T (1) = 3, when n is a power of 3? (b) What is the exact solution to T (n) = 3 · T

(n 3

  • 3n with T (1) = 1, when n is a power of 3? [We are expecting: Your answer — no justification required. Notice that we want the exact answer, so don’t give an O() statement.] Solution: a)

T (n) = 3 · T (

n 3

) + n = n log 3 n + n · T (1)

So,

T (3m) = 3m^ log 3 3 m^ + 3m^ · T (1) = 3m(m log 3 3 + T (1)) = 3m(m + 3)

b)

T (n) = 3 · T (

n 3

) + 3n

= 3n log 3 n + n · T (1)

So,

T (n) = T (3m) = 3m+1^ log 3 3 m^ + 3m^ · T (1) = 3m(3m log 3 3 + T (1)) = 3m(3m + 1)

  1. Use any of the methods we’ve seen in class so far to give big-Oh solutions to the following recurrence relations. You may treat fractions like n/ 2 as either bn/ 2 c or dn/ 2 e, whichever you prefer.

(a) T (n) = 3T

(n 9

n for n ≥ 9 , and T (n) = 1 for n < 9. (b) T (n) = T (n − 4) + n for n ≥ 4 , and T (n) = 1 for n < 4. (You may assume n mod 4 = 0.) (c) T (n) = 6T

(n 4

  • n^2 for n ≥ 4 , and T (n) = 1 for n < 4. (d) T (n) = 5T

(n 2

  • n^2 for n ≥ 2 , and T (n) = 1 for n < 2. [We are expecting: For each item, the best answer you can give of the form T (n) = O( ) and a justification. (That is, all of these satisfy T (n) = O(2n), but you can do better). You do not need to give a formal proof, but your justification should be convincing to the grader. You may use the Master Theorem if it applies.] Solution: a) T (n) = 3T (n 9 ) +

n

T (n) = O(

n log n) via Master Theorem’s case a = bd^ since 3 = 9^12 b) T (n) = T (n − 4) + n

T (n) = O(n^2 ) via any method except Master Theorem. Find an example solution below:

Problems

  • Try the problems on your own before asking for help.
  • Write up your answers yourself, in your own words. You should never share your typed- up solutions with anyone.
  1. Matrix multiplication. Suppose that we have n × n matrices X and Y and we’d like to multiply them. (a) What is the running time of the standard algorithm (that computes the inner product of rows of X and columns of Y )? You can assume that simple arithmetic operations, like multiplication, take constant time (O(1)). [We are expecting: A detailed analysis of the runtime of your algorithm including the Big-O time in terms of n.] Solution:

Algorithm 1 NaiveMatrixMultiplication(A[][], B[][]) 1: n = A.row 2: for i = 0 to n do 3: for j = 0 to n do 4: C[i][j] = 0 5: for k = 0 to n do 6: C[i][j] += A[i][k] * B[k][j] 7: end for 8: end for 9: end for 10: return C

The naive Matrix Multiplication algorithm clearly runs in O(n^3 ) because of the three nested for loops each looping up to n.

(b) Now let’s divide up the problem into smaller chunks like this, where the eight n 2 × n 2 sub-matrices (A, B, C, D, E, F , G, H) are each quarters of the original matrices, X and Y : XY =

[

A B

C D

] [

E F

G H

]

[

AE + BG AF + BH

CE + DG CF + DH

]

We now have a divide and conquer strategy! Find the recurrence relation of this strategy and the runtime of this algorithm.

[We are expecting: A detailed analysis of the runtime of your algorithm including the Big-O time in terms of n.] Solution: T (n) = 8T (n 2 ) + O(n^2 ) The above recurrence is achieved since the described algorithm delegates 8 sub- problems that each multiply two n 2 with each other and does O(n^2 ) work via adding the computed 8 n 2 × n 2 matrices together. T (n) = O(nlog^28 ) = O(n^3 ) via Master Theorem’s case a > bd^ since 8 > 22 (c) Can we do better? It turns out we can by calculating only 7 of the subproblems:

P 1 = A(F − H) P 5 = (A + D)(E + H) P 2 = (A + B)H P 6 = (B − D)(G + H) P 3 = (C + D)E P 7 = (A − C)(E + F ) P 4 = D(G − E)

And we can solve XY by

XY =

[

P 5 + P 4 − P 2 + P 6 P 1 + P 2

P 3 + P 4 P 1 + P 5 − P 3 − P 7

]

We now have a more efficient divide and conquer strategy! What is the recurrence relation of this strategy and what is the runtime of this algorithm? [We are expecting: A detailed analysis of the runtime of your algorithm including the Big-O time in terms of n.] Solution: T (n) = 7T (n 2 ) + O(n^2 ) The above recurrence is achieved since the described algorithm delegates 7 sub- problems that each multiply two n 2 with each other and does O(n^2 ) work via adding/subtracting the computed 7 n 2 × n 2 matrices together. T (n) = O(nlog^27 ) = O(n^2.^8074 ) via Master Theorem’s case a > bd^ since 7 > 22.

  1. On an island, there are trustworthy tarsiers and tricky tarsiers. (Tarsier, by the way, is an insectivorous primate of the family Tarsiidae, having very large eyes and long feet, native mainly to several islands of Southeast Asia.) The trustworthy tarsiers always tell the truth; the tricky tarsiers may lie or may tell the truth. The tarsiers themselves can tell who is tricky and who is trustworthy, but an outsider can’t tell the difference: they all just look like tarsiers. You arrive on this island, and are tasked with finding the trustworthy tarsiers. You are allowed to pair up the tarsiers and have them evaluate each other. For example, if Tiffany the tarsier and Tomás the tarsier are both Trustworthy tarsiers, then they will both say that the other is trustworthy. But if Tiffany the tarsier is a Trustworthy tarsier and Tyrannus the tarsier is a Tricky tarsier, then Tiffany will call Tyrannus out as tricky, but Tyrannus may say either that Tiffany is tricky or that she is trustworthy. We will refer to one of these interactions as a “tarsier-to-tarsier comparison.” The outcomes of comparing tarsiers A and B are as follows: tarsier A tarsier B A says (about B) B says (about A) Trustworthy Trustworthy Trustworthy Trustworthy Trustworthy Tricky Tricky Either Tricky Trustworthy Either Tricky Tricky Tricky Either Either Suppose that there are n tarsiers on the island, and that there are strictly more than n/ 2 trustworthy tarsiers. In this problem, you will develop an algorithm to find all of the trustworthy tarsiers, that only uses O(n) tarsier-to-tarsier comparisons. Before you start this problem, think about how you might do this—hopefully it’s not at all obvious! Along the way, you will also practice some of the skills that we’ve seen in Week 1. You will design two algorithms, formally prove that one is correct using a proof by induction, and formally analyze the running time of a recursive algorithm.

(a) Give a straightforward algorithm that uses O(n^2 ) tarsier-to-tarsier comparisons and identifies all of the trustworthy tarsiers. [We are expecting: A description of the procedure (either in pseudocode or very clear English), with a brief explanation of what it is doing and why it works.] Solution:

Algorithm 2 NaiveTarsier(T[]) 1: n = T.size 2: TrustworthySet = {} 3: for i = 0 to n do 4: TrustworthyCount = 0 5: for j = 0 to n do 6: if (TarsierComparison(T[i], T[j]).BtoA == "Trustworthy") and (i != j) then 7: TrustworthyCount++ 8: end if 9: end for 10: if TrustworthyCount > n / 2 then 11: TrustworthySet.Add(T[i]) 12: end if 13: end for return TrustworthySet

To understand the above algorithm a crucial observation must be made which is the fact that a tarsier is trustworthy if and only if more than n 2 tarsiers claim it is trustworthy. With this fact in mind we can identify all the trustworthy tarsiers via comparing each tarsier against every other and keeping track of how many times it was claimed to be trustworthy. If more than half of the tarsiers claim it to be trustworthy then it is indeed trustworthy.

iv. A and B say tricky: In this case A and B are either trustworthy or tricky but can not be both trustworthy at the same time. Here don’t select anything. Notice that in the first case we either select one trustworthy and discard one trustworthy OR select one tricky and discard one tricky. In cases 2 and 3 we always discard one tricky and select either one tricky or one trustworthy. Finally, in the 4th case we either discard two trickies or one tricky and one trustworthy. If the 4th case is applied notice that we end up in a reduced instance that is of size n − 2 with > n− 2 2 trustworthy tarsiers, so it’s clear applying case 4 is always safe. To see the safeness of cases 1,2 and 3, notice that for every selection of a tricky, if we do end up selecting it, we discard another tricky. This means that we can never select more than half the tricky tarsiers in the list, which implies that the remaining picks will all be trustworthy tarsiers, but half of the tricky tarsiers is less than half of the trustworthy tarsiers so the condition of the output is satisfied. Note: As seen in your solutions, it also okay discard a pair that follows case 2, 3 or 4, and only make a selection of one of the elements in the pairs that follow case 1 (A and B say trustworthy). The proof is fairly similar to the one above since discarding cases 2, 3, 4 will maintain that the remaining tarsiers still have more than half of them as trustworthy, and making a selections via case 1 will not allow you to ever select more than half of the tricky tarsiers.

(c) [This part is NOT REQUIRED, but you may assume it for future parts.] Extend your argument for odd n. That is, given a procedure that does the following:

  • Input: A population of n tarsiers, where n is odd, so that there are strictly more than n/ 2 trustworthy tarsiers in the population.
  • Output: A population of m tarsiers, for 0 < m ≤ dn/ 2 e, so that there are strictly more than m/ 2 trustworthy tarsiers in the population.
  • Constraint: The number of tarsier-to-tarsier comparisons is no more than bn/ 2 c. Solution: This should be easy, extend the algorithm and proof given in the solution of (b) to solve this as an exercise if you weren’t able to solve it already.

(?) For all of the following parts, you may assume that the procedures in parts (b) and (c) exist even if you have not done those parts.

(d) Using the procedures from parts (b) and (c), design a recursive algorithm that uses O(n) tarsier-to-tarsier comparisons and finds a single trustworthy tarsier. [We are expecting: A description of the procedure (either in pseudocode or very clear English).] Solution: The algorithm is fairly simple the idea is to call the ReduceTarsier algorithm from parts (b) and (c) recursively, based on the list size being odd or even, until you’re left with 1 or 2 elements in the list which are guaranteed to be a trustworthy tarsiers. Notice that a list of size 1 or 2 will have to have 1 or 2 trustworthy tarsiers respectively. (e) Prove formally, using induction, that your answer to part (d) is correct.

[We are expecting: A formal argument by induction. Make sure you explicitly state the inductive hypothesis, base case, inductive step, and conclusion.] Solution:

  • Inductive Hypothesis: Given a list of n tarsiers such that > n 2 of them are trustworthy tarsiers, applying the algorithm in part (d), FindSingleTrustwor- thyTarsier(t[]), will return a single trustworthy tarsier. Induce on the length of the list.
  • Base Case: If n = 1 or n = 2 then by definition of the input having > n 2 trustworthy tarsiers, then the input contains 1 or 2 trustworthy tarsiers respectively.
  • Inductive Step: (Strong induction) Assume that FindSingleTrustworthyTar- sier(T[]) outputs a single trustworthy tarsier for input sizes of k such that 0 < k < n such that the input has > n 2 trustworthy tarsiers. Given an input size of size n with > n 2 trustworthy tarsiers, applying FindSingleTrustworthy- Tarsier(T[]) would call the ReduceTarsier(T[]) function, which would return an input of size n/ 2 with > n 4 trustworthy tarsiers and pass that result to FindSingleTrustworthyTarsier(T[]) which we know can solve for sizes k such that 0 < k < n via the inductive hypothesis. Note, the correctness of Reduc- eTarsier(T[]) can be seen in the previous exercises.
  • Conclusion: Via proof by induction on the size of the input, the correctness of the algorithm in part (d) is complete.