0% found this document useful (0 votes)
55 views

Algorithms - : Solutions

The document contains 11 problems related to algorithms and data structures. Problem 1 defines key terms like inversion-bound sorting, big-O notation, and stable sorting. Problem 2 works through QuickSelect to find the 4th smallest element. Problem 3 proves statements about big-O using limits. Problem 4 uses the Master Formula to solve a recurrence relation. Problem 5 provides pseudocode for generating all subsets of a set. Problem 6 analyzes the running time of a function counting numbers relatively prime to n. Problem 7 analyzes the amortized cost of operations on a flexible table data structure. Problem 8 counts primitive operations in an algorithm to find the range of an array. Problem 9 works through all steps of MergeSort. Problem 10 provides

Uploaded by

Dagnachew
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Algorithms - : Solutions

The document contains 11 problems related to algorithms and data structures. Problem 1 defines key terms like inversion-bound sorting, big-O notation, and stable sorting. Problem 2 works through QuickSelect to find the 4th smallest element. Problem 3 proves statements about big-O using limits. Problem 4 uses the Master Formula to solve a recurrence relation. Problem 5 provides pseudocode for generating all subsets of a set. Problem 6 analyzes the running time of a function counting numbers relatively prime to n. Problem 7 analyzes the amortized cost of operations on a flexible table data structure. Problem 8 counts primitive operations in an algorithm to find the range of an array. Problem 9 works through all steps of MergeSort. Problem 10 provides

Uploaded by

Dagnachew
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

Name ______________________ Student ID ___________________

Algorithms -- SOLUTIONS
Midterm , June, 2009
(10 points per problem)

1. Definitions. To give the definitions of the italicized terms below, complete the
sentence provided.

A. A sorting algorithm is inversion-bound if for any input array arr of size n…

the algorithm performs at least as many comparisons as the number of inversions


in arr

B. A function f(n) is said to belong to O(g(n)) if…

there exist positive constants c, n0 such that f(n)  cg(n) for all n  n0

C. A sorting algorithm is stable if, whenever


[(k0, d0), (k1,d1), …, (ki,di),…,(kj,dj),…,(kn,dn)]
is an input sequence of objects to be sorted by the keys k0, k1,… kn, and ki = kj and
i < j, then …

(ki,di) occurs before (kj,dj) in the final output sequence


2. Use the QuickSelect algorithm to manually compute the 4th smallest
element of the array [1, 5, 23, 0, 8, 4]. Assume that the rightmost element is used as
the pivot in each case. Show what happens in each self-call, indicating the new input
array and the current value of k.

QS (1, 5, 23, 0, 8, 4), k = 4

L = [1,0], E = [4], G = [5, 23, 8]


k' = k - |L| - |E| = 4 – 3 = 1

QS (5, 23, 8) , k = 1

L = [5], E = [8], G = [23]


k' = k = 1

QS (5), k = 1

L = [ ], E = [5], G = [ ]

|L| < k  |L| + |E|, so return 5


3. Do just ONE of the following (if you do both problems, you will get no credit for
either of the problems).

a. Use the definition of big-oh to show that


n2+n is not O(n)

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

cn < n2 < n2+n

b. Use limits to prove the following:


(log n)2 is O(n)

limn (log n)2/n = limn (2c0 log n)/n (by L’Hopital) // 2logn*1/n*loge

= limn 2c0((log n)/n)

= 2c0 limn c1/n

=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

Using the Master Formula, we see that

a = 10, b = 3, c = 2, k = 2, and a > bk

Therefore

T(n) is (nx), where x = log3 10

Since log3 10 > 2, it follows that

T(n) belongs to Ω(n2).

5. Write out the PowerSet Algorithm in pseudo code.


Algorithm: PowerSet(X)

Input: A list X of elements


Output: A list P consisting of all subsets of X – elements of P are Sets

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))).

public static int eulerPhi(int n) {


if(n < 1) return -1;
int numRelPrime = 0;
for(int d = 1; d <= n; ++d) {
if(gcd(d,n) == 1) ++numRelPrime;
}
return numRelPrime;
}

private static int gcd(int m, int n) {


if(n == 0) return m;
return gcd(n, m%n);
}

Relative to input values:


gcd performs in O(log n)
eulerPhi performs in O(nlog n)

Relative to input size:


gcd performs in O(length(n))
eulerPhi performs in O(length(n)*2length(n)))
7. A flexible table is a kind of data structure storing Strings that uses an array in the
background (assume it is already initialized and has as many available slots as
necessary without resizing) and that has three primary operations:
 AddOne(x) – adds String x to the background array in the next available slot
 AddTwo(x,y) – adds Strings x and y to the background array by placing them
in the next two available slots.
 ClearAll() – removes all Strings currently in the array by setting them to null.
Show that the amortized running time of ClearAll is O(1). Do the steps required by
filling in the following table. Hint: For your amortized cost function, try charging 2
cyberdollars for AddOne, 4 cyberdollars for AddTwo and 0 cyberdollars for ClearAll.

Your cost function c:

c(AddOne) = 1
c(AddTwo) = 2
c(ClearAll) = k, where k is number of non-null Strings in the
array

Your amortized cost function ĉ:

ĉ(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.

Since profit is always nonnegative,

1i nc(si)  1i n ĉ(si)  1i n 4 = 4n

Explain why ClearAll has amortized running time O(1):

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]
{ ii 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.

Array length = 7, so length – 1 = 6

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).

Theorem 2. The average case running time of QuickSort is O(nlog n).

Theorem 3. The running time of GCD(m,n) in terms of its input values m, n is


O(log n). In terms of its input sizes, its running time is O(length(n)).

The statement of the Theorem you are going to prove:

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

You might also like