Docsity

CS/MCS 401 Week #3-4 Exercises Solutions: Analysis of Algorithms, Assignments of Algorithms and Programming

Toggle sidebar
bg1
Solutions to CS / MCS 401 Week #3-4 Exercises (Spring 2008)
Exercise 2.3-1
Exercise D.
C(0) = 0 = d⋅0, so the result holds when n = 0.
Let n ≥ 1, and assume the result holds for all i with i < n. C(n) = d + C(k) + C(n
−
1−k) where
0 ≤ k ≤ n−1. Note n−k−1 ≤ n−1. By the inductive hypothesis,
C(n) = d + dk + d(n−k−1) = d(1 + k + (n−k−1)) = dn,
so the result also holds for n. By induction it holds for all nonnegative integers.
Exercise E.
Let L, M, and R be sorted arrays of length n/3 (possibly ⎣n/3⎦ or ⎡n/3⎤ , so the sum of the
lengths is n). For simplicity, assume n is a power of 3, so always L, M, and R have length n/3.
Assume that each array has an extra element ∞ at the end. We can merge L, M, and R into a
single sorted array A of length n using the algorithm below. Here i, j, and k represent the
positions of the current elements in L, M, and R respectively; and x represents the smallest
element not yet merged from M or R, provided xValid is true. As usual, indentation indicates
nesting of blocks.
3 41 52 26 38 57 9 49
3 41 38 57 26 52 9 49
3 26 41 52 9 38 49 57
3 9 26 38 41 49 52 57
pf3

Partial preview of the text

Download CS/MCS 401 Week #3-4 Exercises Solutions: Analysis of Algorithms and more Assignments Algorithms and Programming in PDF only on Docsity!

Solutions to CS / MCS 401 Week #3-4 Exercises (Spring 2008)

Exercise 2.3-

Exercise D.

C(0) = 0 = d ⋅0, so the result holds when n = 0.

Let n ≥ 1, and assume the result holds for all i with i < n. C( n ) = d + C( k ) + C( n − 1 − k ) where

0 ≤ k ≤ n −1. Note n − k − 1 ≤ n −1. By the inductive hypothesis,

C( n ) = d + dk + d ( n − k −1) = d (1 + k + ( n − k −1)) = dn ,

so the result also holds for n. By induction it holds for all nonnegative integers.

Exercise E. Let L, M, and R be sorted arrays of length n /3 (possibly ⎣ n /3⎦ or ⎡ n /3⎤ , so the sum of the lengths is n ). For simplicity, assume n is a power of 3, so always L, M, and R have length n /3. Assume that each array has an extra element ∞ at the end. We can merge L, M, and R into a single sorted array A of length n using the algorithm below. Here i , j , and k represent the positions of the current elements in L, M, and R respectively; and x represents the smallest element not yet merged from M or R, provided xValid is true. As usual, indentation indicates nesting of blocks.

i = 1; j = 1; k = 1; xValid = false ; for ( q = 1,2, ..., n ) if ( not xValid ) if ( M[ j ] ≤ R[ k ] ) () x = M[ j ]; j = j + 1; else x = R[ k ]; k = k + 1; xValid = true ; if ( L[ i ] ≤ x ) (*) A[ q ] = L[ i ]; i = i + 1; else A[ q ] = x ; xValid = false ;

Comparisons are performed in the lines () and (). The comparison in line () is performed on each pass through the loop — a total of n times. The comparison on line () is always performed on the first pass ( q = 1). On the remaining passes, it is performed if the element merged to A on the previous pass came from M or R, but not if it came from L. Thus the total number of comparisons in line (*) is

n − (number of elements merged from L on the first n −1 passes) = n – ( n /3 or n /3–1) = 2/3 n or 2/3 n + 1

times. The total number of comparisons performed by the algorithm is 5/3 n or 5/3 n + 1.

Exercise F.

C( n ) = 3C( n /3) + 5/3 n , C(1) = 0. We assume n = 3 k , so k = log (^) 3( n ).

C( n ) = 3C( n /3) + 5/3 n

= 3 ( 3C( n /3^2 ) + (5/3)( n /3)) + 5/3 n

= 3 2 C( n /3 2 ) + 2(5/3 n )

= 3 2 ( 3C( n /3 3 ) + (5/3)( n /3^2 )) + 2(5/3 n )

= 3 3 C( n /3 3 ) + 3(5/3 n ) . . . = 3 k^ C( n /3 k^ ) + k (5/3 n ) = n C(1) + 5/3 n log (^) 3( n ) = 5/3 n log 3 ( n )

The exact solution when n is a power of 3 is C( n ) = 5/3 n log (^) 3( n ) ≈ 1.052 n lg ( n ).

By contrast, ordinary (2-way) mergesort uses approximately n lg ( n ) comparisons.