Docsity

Exam 1 Answer Key - Algorithms - 2009 | CSCD 320, Exams of Algorithms and Programming

Toggle sidebar
bg1
CScD-501, Algorithms
Exam 1: 23 February 2009
Answer Key
1. (16 points) Give either an algorithm or a programming problem that exemplifies each of the following
algorithmic strategies or for which we examined use of the algorithmic strategy.
Greedy: Change making with US currency; real-valued knapsack; Huffman
codes
Divide and Conquer: Merge Sort, Quick Sort, Binary Search, MaxMin, Search for the k’th
element
Backtracking: Can be used for 0/1 knapsack, change back. Queens problem. One
approach to Pascal’s Travels.
Dynamic Programming: Calculation of Fibonacci numbers, change back, binomial
coefficients. Another approach to Pascal’s Travels.
2. (4 points) When using the greedy approach to the knapsack problem (to obtain the optimal value when
the approach succeeds or to obtain a first estimate when it doesn't), in what order should the items be
considered in terms of their values and weights?
They should be descending order by value per weight [value density] (or equivalently
increasing by weight per value).
3. (10 points) Below is the recursive pseudocode for the simultaneous calculation of the maximum and
minimum values in an array. For comparisons performed, identify the base cases and the recurrence as
Cmp(n). You may restrict n to n = 2k for integer k > 0.
Pair MaxMin(array segment)
if segment size = 1
return element as both max and min
else if segment size = 2
one comparison to determine max and min
return that pair
else // segment size presumably > 2
recurse for max and min of left half
recurse for max and min of right half
one comparison determines true max of the two candidates
one comparison determines true min of the two candidates
return the pair of max and min
NOTE: TWO-SIDED PAGES Page 1 Printed 2020-Dec-03 20:14
pf3
pf4
pf5

Partial preview of the text

Download Exam 1 Answer Key - Algorithms - 2009 | CSCD 320 and more Exams Algorithms and Programming in PDF only on Docsity!

CScD-501, Algorithms

Exam 1: 23 February 2009

Answer Key

1. (16 points) Give either an algorithm or a programming problem that exemplifies each of the following

algorithmic strategies or for which we examined use of the algorithmic strategy.

Greedy:

Change making with US currency; real-valued knapsack; Huffman

codes

Divide and Conquer:

Merge Sort, Quick Sort, Binary Search, MaxMin, Search for the k’th

element

Backtracking:

Can be used for 0/1 knapsack, change back. Queens problem. One

approach to Pascal’s Travels.

Dynamic Programming:

Calculation of Fibonacci numbers, change back, binomial

coefficients. Another approach to Pascal’s Travels.

2. (4 points) When using the greedy approach to the knapsack problem (to obtain the optimal value when

the approach succeeds or to obtain a first estimate when it doesn't), in what order should the items be

considered in terms of their values and weights?

They should be descending order by value per weight [value density] (or equivalently increasing by weight per value).

3. (10 points) Below is the recursive pseudocode for the simultaneous calculation of the maximum and

minimum values in an array. For comparisons performed, identify the base cases and the recurrence as

Cmp( n ). You may restrict n to n = 2 k^ for integer k > 0.

Pair MaxMin(array segment)

if segment size = 1

return element as both max and min

else if segment size = 2

one comparison to determine max and min

return that pair

else // segment size presumably > 2

recurse for max and min of left half

recurse for max and min of right half

one comparison determines true max of the two candidates

one comparison determines true min of the two candidates

return the pair of max and min

NOTE: TWO-SIDED PAGES Page 1 Printed 2020-Dec-03 20:

Winter 2009: Exam 1

Base cases: Cmp(1) = Cmp(2^0 ) = 0 Cmp(2) = Cmp(2^1 ) = 1 Recurrence: Note: This portion (6 points) was discarded and converted to

extra credit

Cmp(n>2) = Cmd (n / 2) + Cmp (n/2) + 2 — for general n Cmp(2k>1) = 2 * Cmd ( 2k–1) + 2 — for the restricted range of n = 2k

4. (30 points — 3 each) For each of the following, gives its worst-case (Big-O) and best-case (Big Ω) )

behavior [though they may be the same ]. You should not require the explicit code to answer this, but it

does contain information about the variation on the algorithm being discussed, where appropriate — as

for binary search (no early exit) and quick sort ( not an intelligent partition method).

a) Binary Search: Worst case: O( log n ) Best case: Ω( log n ) that is,  ( log n )

static int binSearch ( Comparable[] x, int n, Comparable val) { int lo = 0, hi = n-1;

while (lo < hi) // algorithm without an early exit.

{ int mid = (lo + hi) / 2; if (val.compareTo(x[mid]) > 0) lo = mid + 1; else hi = mid; } // end while if ( val.compareTo(x[hi]) > 0 ) // Belongs at the VERY end return n; else // Where val is (or belongs) return hi; } // end binSearch()

b) Linear Search: Worst case: O( n ) Best case: Ω( 1 )

static int linSearch ( Object[] x, int n, Object val ) { int j; for ( j = 0; n > j; j++ ) if ( val.equals(x[j]) // Single statement, no braces return j; return -1; } // end linSearch()

c) Optimized Bubble Sort: O( n^2 ) Best case: Ω ( n )

public static void bubbleSort(Comparable[] a)

{ int last; // Position of the last swap for early exit

for (last = a.length-1;last>0;/no decrement/) { int mark = 0;// retain last swap position without changing last for (int k = 0; k < last; ++k) { if (a[k].compareTo(a[k+1]) > 0) { Comparable temp = a[k+1]; a[k+1] = a[k]; a[k] = temp; mark = k; //mark top of the swap } // end if } // end for last = mark; // update last } // end for } // end bubbleSort

Winter 2009: Exam 1

i) Optimal binomial coefficient: Worst case: O( k ) Best case: Ω ( k ) that is,  (k)

This one counts for two, because it is a functionality in either n, or k, or both n and k:

public static long binom(int n, int k) { long answer = 1; // C(n-k, k-k) = C(n-k, 0) for (int ni = n-k+1, ki = 1; ki <= k ; ni++, ki++) answer = ( answer * ni ) / ki; return answer; } // end binom()

5. (10 points) The function f(n) is asymptotically smaller than g(n) if and only if the following holds:

lim 

  gn

f n

n

— See note on back page

Put the following functions into order as asymptotically smallest to asymptotically largest. Just state the

order; you do not have to prove the relationship.

1, n, 2 n , n

, n

, √n, n

, n n , log 2 (n), n

log(n) 1, log 2 (n), √n, n, n

, n, n

log(n)

, n

, 2 n , n n

Seat-of-the-pants: log 2 (1024) = 10, √1024 = 32; log 2 (22k) = 2k, √22k^ = 2k^ , and 2k < 2 k, k > 0

Formal proof:

 

ln( 2 )

ln( 2 )

1 / ln( 2 )

lim log ( ) [log ( )] /

1 / 2 1 / 2 1 / 2 1 / 2 1

  ^ n n

n

n

n

d n dn

d n dn

n

n

n

6. (5 points) Indicate whether for each of the following problems an optimal solution can be obtained using

a greedy approach — yes/no or true/false:

no Making change with the smallest number of coins based on 1¢, 10¢, 25¢, and 50¢. [Consider 30¢]

yes Making change with the smallest number of coins based on 1¢, 5¢, 10¢, 25¢, and 50¢.

yes Real-valued knapsack to obtain always the highest value for the knapsack’s capacity.

no Integer-valued knapsack to obtain always the highest value for the knapsack’s capacity.

no 0/1 knapsack to obtain always the highest value for the knapsack’s capacity.

Winter 2009: Exam 1

7. (5 points) Of which of the four algorithmic strategies we discussed is the following pseudocode an

example?

Pair MaxMin(array segment)

if segment size = 1

return element as both max and min

else if segment size = 2

one comparison to determine max and min

return that pair

else // segment size presumably > 2

recurse for max and min of left half

recurse for max and min of right half

one comparison determines true max of the two candidates

one comparison determines true min of the two candidates

return the pair of max and min

This is an example of Divide & Conquer

8. (10 points) Using the table driven dynamic programming algorithm developed in class for the

integer knapsack, develop the table (of six rows and 12 columns) that generates the solution for the

following problem. For your convenience, the zeroes for column zero and row zero are already entered.

MaxWt = 11

5 items: [1] (1 wt, 1 value), [2] (2 wt, 5 value), [3] (5 wt, 18 value), [4] (6 wt, 22 value), [5] (7 wt, 28 value)

(^1) A note from calculus:

In simple cases, L'Hôpital's rule states that for functions f ( x ) and g ( x ), if

or , then:

where the prime (') denotes the derivative.

Also note: Derivative of logb

xln(b )

log (x )

dx

d

b ^ given logb(x) = ln(x)/ln(b) and the definition  (^)  x

x

x

x

1

d

ln( )

Or, with regard to log 2 (n) and √n, you can go by the seat of your pants.