Algorithms - : Solutions
Algorithms - : Solutions
Algorithms -- SOLUTIONS
Midterm , June, 2009
(10 points per problem)
1. Definitions. To give the definitions of the italicized terms below, complete the
sentence provided.
there exist positive constants c, n0 such that f(n) cg(n) for all n n0
QS (5, 23, 8) , k = 1
QS (5), k = 1
L = [ ], E = [5], G = [ ]
Given positive constants c, n0, we find n n0 for which n2+n > cn.
Pick n so that n > c, n0, 1. Since c < n, n*c < n2 and so we have
limn (log n)2/n = limn (2c0 log n)/n (by L’Hopital) // 2logn*1/n*loge
=0
4. Use the Master Formula to compute T(n) explicitly, assuming it satisfies the
following recurrence relation:
T(1) = d (d > 0)
T(n) = 10T(n/3) + 2n2
Therefore
P ← new list
S ← new Set //S is the empty set
P.add(S) //P is now the set { S }
if X.isEmpty then
return P
else
while (!X.isEmpty() ) do
f ← X.removeFirst()
for each x in P do
S ← x U {f} // S is the set containing f & all elements of x
P.add(S)
return P
6. The following function computes, for any positive input integer n, the number of
positive integers n that are relatively prime to n. What is the asymptotic running
time of this function, as a function of input values? What is the asymptotic running
time as a function of input size? Hint: If the running time as a function of input
values is O(f(n), then the running time as a function of input size is O(2f(length(n))).
c(AddOne) = 1
c(AddTwo) = 2
c(ClearAll) = k, where k is number of non-null Strings in the
array
ĉ(AddOne) =2
ĉ(AddTwo) =4
ĉ(ClearAll) =0
How do you know that your amortized cost function never results in negative
amortized profit:
The array obtains values via the operations AddOne and AddTwo. Whenever
AddOne is invoked, $1 profit is stored. Likewise, whenever AddTwo is invoked, $2
profit is stored. In each case, $1 profit is stored for each element added. Therefore,
whenever ClearAll is invoked when there are k elements in the table, $k profit is
available to pay for ClearAll, and this is exactly enough to pay for the ClearAll operation.
Provide a bound for the amortized cost of running n of these operations in succession.
ClearAll has amortized running time O(4n)/n since amortized running time of all n
operations executed in sequence is O(4n). Therefore ClearAll has amortized
running time O(1)
8. The following algorithm computes the difference between the minimum and
maximum values of an input array. Compute the total number of primitive operations
used in the worst case.
Algorithm arrayRange(A, n)
Input array A of n integers
Output difference between max and min values of A
currentMax A[0]
+2
currentMin A[0] +2
for i 1 to n - 1 do +n + 1
if A[i] > currentMax then +2(n-1)
currentMax A[i] +2(n-1) (one or other)
if A[i] currentMin then +2(n-1)
currentMin A[i]
{ ii n-1)
return currentMax - currentMin +2
__________
9n – 1
Note: This calc doesn’t count repeated computation of n-1 in for loop.
Doing so would lead to an additional n operations, producing a total of
10n – 1
9. Work through the steps of MergeSort (implementation version) on the input array
[1,4,7,2,3,4,1]. Show all steps.
ms(temp, 0, 6)
mid = 3
ms(temp,0,3)
mid =1
ms(temp, 0, 1)
mid = 0
ms(temp, 0, 0)
return
ms(temp, 1, 1)
return
merge(temp, 0,1,1) //state: [1][4] [1,4]
//array: [1,4,7,2,3,4,1]
ms(temp,2,3)
mid = 2
ms(temp, 2, 2)
return
ms(temp, 3, 3)
return
merge(temp, 0, 3, 3) //state: [7][2] [2,7]
//array: [1,4,2,7,3,4,1]
merge(temp,0,2,3) //state: [1,4],[2,7] [1,2,4,7]
//array: [1,2,4,7,3,4,1]
ms(temp,4,6)
mid=5
ms(temp,4,5)
mid = 4
ms(temp, 4, 4)
return
ms(temp, 5, 5)
return
merge(temp,4,5,5) //state: [3], [4] [3,4]
//array: [1,2,4,7,3,4,1]
ms(temp,6,6)
return
merge(temp,4,6,6) //state: [3,4][1] [1,3,4]
//array: [1,4,7,2,1,3,4]
merge(0,2,6) //state: [1,2,4,7], [1,3,4] [1,1, 2, 3, 4, 4, 7]
10. Give a high-level proof of just one of the following theorems that we
proved in class:
Theorem 1. Every comparison based sorting algorithm has, for each n, running on
input of size n, a worst case in which its running time is Ω(nlog n).
The high-level proof of the theorem you wrote in the previous box:
11. SCI Extra Credit Essay Question (3 points). Elaborate upon a parallel between
points and topics in Algorithms and one or more SCI principles.
The Master Formula