






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







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!
We suggest you do these on your own. As with any homework problem, though, you may ask the course staff for help.
(a) What is the exact solution to T (n) = 3 · T
(n 3
(n 3
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)
(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
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:
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 =
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
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.
(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:
(?) 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: