Docsity

Assignment 1 with Solution - Algorithms | CMSC 351, Assignments of Algorithms and Programming

Toggle sidebar
bg1
Fall 2002 Khuller & Postow
CMSC 351
Solutions to Homework 1
Solution 1.
n
X
i=1
3i+1
X
j=i
1 =
n
X
i=1
(2i+ 2) =
n
X
i=1
2i+
n
X
i=1
2 = 2 ·n(n+ 1)
2+ 2 ·n=n(n+ 3)
Solution 2. Sort the numbers. Now assume that x1is the smallest, x2the next smallest
and so on. Note that we should pair x2n(largest number) with x1the smallest number.
To see this formally, suppose that the OPT solution pairs x2nwith some number xp
and x1with xj. By doing an exchange, and producing the pairing (x2n, x1) and (xp, xj)
we get a solution with lower cost, since x2n+x1≤x2n+xpand xp+xj≤x2n+xp.
We now continue to do the pairing in this way. The maximum pair that we produce
is our cost.
Solution 3. We only give the solution for part (a). If you sort all the numbers using
merge sort, then all the duplicate numbers appear consecutively. It is easy to compute
the mode in a single scan of the numbers now. This algorithm takes time O(nlog n).
Solution 4. It is easy to verify the claim for n= 1. The LHS is 1, and the RHS is
1.2.3
6= 1. We now assume that the claim is true for n=mand we will use this to
prove that the claim is true for n=m+1. We need to prove that (setting n=m+ 1)
m+1
X
i=1
i2=(m+ 1)(m+ 2)(2m+ 3)
6
Consider the LHS. m
X
i=1
i2+ (m+ 1)2.
This is (m)(m+1)(2m+1)
6+ (m+ 1)2. Simplifying, gives us (m+ 1)2m2+7m+6
6. This is
the same as the RHS. (Since (m+ 2)(2m+ 3) = 2m2+ 7m+ 6.)

Partial preview of the text

Download Assignment 1 with Solution - Algorithms | CMSC 351 and more Assignments Algorithms and Programming in PDF only on Docsity!

Fall 2002 Khuller & Postow

CMSC 351 Solutions to Homework 1

Solution 1.

∑^ n i=

(^3) ∑i+

j=i

∑^ n i=

(2i + 2) =

∑^ n i=

2 i +

∑^ n i=

n(n + 1) 2

  • 2 · n = n(n + 3)

Solution 2. Sort the numbers. Now assume that x 1 is the smallest, x 2 the next smallest and so on. Note that we should pair x 2 n (largest number) with x 1 the smallest number. To see this formally, suppose that the OPT solution pairs x 2 n with some number xp and x 1 with xj. By doing an exchange, and producing the pairing (x 2 n, x 1 ) and (xp, xj ) we get a solution with lower cost, since x 2 n + x 1 ≤ x 2 n + xp and xp + xj ≤ x 2 n + xp. We now continue to do the pairing in this way. The maximum pair that we produce is our cost.

Solution 3. We only give the solution for part (a). If you sort all the numbers using merge sort, then all the duplicate numbers appear consecutively. It is easy to compute the mode in a single scan of the numbers now. This algorithm takes time O(n log n).

Solution 4. It is easy to verify the claim for n = 1. The LHS is 1, and the RHS is

    1. 3 6 = 1. We now assume that the claim is true for^ n^ =^ m^ and we will use this to prove that the claim is true for n = m + 1. We need to prove that (setting n = m + 1) m∑+

i=

i^2 = (m + 1)(m + 2)(2m + 3) 6

Consider the LHS. (^) m ∑

i=

i^2 + (m + 1)^2.

This is (m)(m+1)(2 6 m+1)+ (m + 1)^2. Simplifying, gives us (m + 1) 2 m (^2) +7m+ 6.^ This is the same as the RHS. (Since (m + 2)(2m + 3) = 2m^2 + 7m + 6.)