You are on page 1of 5

Page 1 of 5

Name (Last, First): ____________________________________

University of Washington, Tacoma


TCSS 543: Advanced Algorithms
Donald Chinn

om
January 29, 2013
MIDTERM 1

r.c
lur
 This test is CLOSED book and CLOSED notes. This is a 60 minute exam.

nb
The blank space left for your answers is roughly proportional to the length of a
correct answer. If you need more space for your answer, use the back of the previous
page and indicate that you did so.

tu
 The questions’ weights give you an indication of how much time you are expected to

ar
spend on each of them. Sm
 Think carefully before writing down your answers. Use the back of the pages as
scratch paper if you need it. Don’t spend too much time on any one question. Some
questions may be harder for you than other questions.
via

 This exam has 5 pages.


e d
ar
Sh

1 /10
2 /15
is

3 /15
file

4 /10
Total /50
is
Th

We provide unlocked studymaterials from popular websites at affordable price, email enquiries to rishabhk28@live.com
Page 2 of 5

Question 1. (10 points total)


1a. (6 points) Consider the following code fragment.

sum  0
for i  1 to n do
for j  1 to i do

om
if (j < 1000) then
for k  1 to j2 do

r.c
sum  sum + 1
else

lur
sum  sum + j

nb
What is the asymptotic running time of this code fragment, expressed as a function of n?
For the purposes of this problem, you should assume that n is large. Use big-Oh notation

tu
and justify your answer.

ar
Sm
via
e d
ar
Sh
is

1b. (4 points) True or False: n2 / log n  O(n log n). Justify your answer using the limit
test.
file
is
Th

We provide unlocked studymaterials from popular websites at affordable price, email enquiries to rishabhk28@live.com
Page 3 of 5

Question 2. (15 points total) Consider the following problem. Given an array A[0 .. n-1]
of n positive integers, find an index i in A such that A[i] + A[i + 1] is maximized. For
example, if the input array were
5 6 1 9 11 7 12 6
then the output should be 3, because 9 + 11 is greater than the sum of any other adjacent
pair of elements in the array.

om
Input: An array A[0 .. n-1] of n positive integers (where n > 1)
Output: integer i such that A[i] + A[i + 1] is maximized

r.c
a. (9 points) Describe using pseudocode or high-level pseudocode a divide-and-conquer

lur
algorithm to solve this problem.

nb
tu
ar
Sm
via
e d
ar
Sh

b. (6 points) Define and solve a recurrence equation T(n) that describes the running time
of your algorithm, where n is the number of integers in the array. Be sure to define T(1).
is

Do NOT use the Master Theorem.


file
is
Th

We provide unlocked studymaterials from popular websites at affordable price, email enquiries to rishabhk28@live.com
Page 4 of 5

Question 3. (15 points) The coin changing problem is as follows. Given a set D of m
distinct positive integers (representing denominations of coins) and a positive integer n,
determine the minimum number of “coins” that add up to the value n. For example, if the
denominations of the coins are the ones used in the United States (1, 5, 10, 25, and 50),
then if n = 17, the output should be 4 (since {10, 5, 1, 1} is the minimum number of coins
needed to add up to 17). If n = 38, then the output should be 5.

om
One way to solve this problem is to use a dynamic programming approach. The idea is to
have a one-dimensional array (let’s call it M). In general, the value in M[i] contains the

r.c
minimum number of coins from D that add up to i.

lur
a. (9 points) Fill in the pseudocode below so that it solves the coin changing problem.
Comments are included to help you.

nb
Algorithm CoinChanging (D[0 .. m-1], n)
Input: an array D of m distinct positive integers and a positive integer n

tu
Output: the minimum number of instances of integers in D that add up to the value n

ar
M[0]  0 // initialize M[0] to 0 because it takes no coins to add up to 0
for i  1 to n do
Sm
// Calculate M[i] by considering one denomination of coin and adding the coin to the
// appropriate solution to a smaller value of i. Check all coins this way to determine
// which solution is best.
via

best  ∞ // best contains the least number of coins that add up to i


d

for j  0 to m-1 do
e
ar
Sh
is
file

return M[n]

b. (6 points) What is the worst case running time of your algorithm from part a? (Be sure
is

your answer to part a is correct, in order to get full credit for this part of the question.)
Briefly explain your answer.
Th

We provide unlocked studymaterials from popular websites at affordable price, email enquiries to rishabhk28@live.com
Page 5 of 5

Question 4. (10 points) In the first lecture of TCSS 543, we discussed the Interval
Scheduling Problem, which can be described as follows:

Input: An array of n requests (consisting of two values si and fi, representing the start and
finish times of the request)
Output: A maximal set of requests that are compatible (for all pairs of distinct requests i

om
and j, the intervals [si, fi] and [sj, fj] have an empty intersection)

We made the claim in class that the following algorithm is a greedy algorithm that solves

r.c
this problem:

lur
1. Sort (using mergesort) the requests by fi, so that f0  f1  ...  fn-1.
2. Add request 0 to the set of accepted requests (call it A, implemented as a list).
3. last  0

nb
4. for i  1 to n – 1 do
if si  flast then

tu
add request i to A

ar
last  i
5. return A Sm
a. (5 points) What is the running time of this algorithm in the worst case? Explain and
justify your answer.
via
e d
ar
Sh

b. (5 points) Suppose we devised a different greedy algorithm that is the same as the one
above except that in step 1, it sorts the requests by si instead of fi. Give an example of an
is

input to the Interval Schedule Problem where this new algorithm would not produce the
correct output.
file

i si fi
is
Th

We provide unlocked studymaterials from popular websites at affordable price, email enquiries to rishabhk28@live.com

You might also like