Docsity

CSCI 6212 - Homework 1 - Solution: Big-O Notation, Recursion, Algorithm Analysis, Exercises of Design and Analysis of Algorithms

Toggle sidebar
bg1
George Washington University
Department of Computer Science
CSCI 6212 - Homework 1 - Solution
Given: January 22, 2024
Due: 11:59PM, February 4, 2024 (submission through BB)
1. (10 points)
(a) Show that n2−10n−100 = O(n2).
Sol. n2−10n−100 ≤n2for any n≥1. Thus, by the definition of the big-Onotation,
n2−10n−100 = O(n2).
(b) Show that n+dn
2e=O(n).
Sol. n+dn
2e ≤ 2nfor any n≥1. Thus, by the definition of the big-Onotation, n+dn
2e=O(n).
2. (10 points) List the functions below from the lowest order to highest order. If any two (or
more) are of the same order, indicate which.
n2nnlog nln n
n+n3log n√n en
n2+ log n n22n−1log log n
2n3(log n)2n!n1+where 0 < < 1.
22n2n+1
Sol. log log n < log n= ln n < (log n)2<√n<n<nlog n<n1+where 0 <<1
< n2=n2+ log n<n+n3= 2n3<2n−1= 2n= 2n+1 < en<22n< n!
3. (10 points) Consider the following function f(n).
function f(n)
if n >1 :
print line(“still going”)
f(n/3)
f(n/3)
f(n/3)
How many lines, as a function of n, does f(n) print? Write a recursive function and solve it.
You may assume nis a power of 3 (i.e., n= 3kfor an integer k).
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download CSCI 6212 - Homework 1 - Solution: Big-O Notation, Recursion, Algorithm Analysis and more Exercises Design and Analysis of Algorithms in PDF only on Docsity!

George Washington University Department of Computer Science CSCI 6212 - Homework 1 - Solution

Given: January 22, 2024 Due: 11:59PM, February 4, 2024 (submission through BB)

  1. (10 points)

(a) Show that n^2 − 10 n − 100 = O(n^2 ). Sol. n^2 − 10 n − 100 ≤ n^2 for any n ≥ 1. Thus, by the definition of the big-O notation, n^2 − 10 n − 100 = O(n^2 ).

(b) Show that n + d n 2 e = O(n). Sol. n+d n 2 e ≤ 2 n for any n ≥ 1. Thus, by the definition of the big-O notation, n+d n 2 e = O(n).

  1. (10 points) List the functions below from the lowest order to highest order. If any two (or more) are of the same order, indicate which.

n 2 n^ n log n ln n n + n^3 log n

n en n^2 + log n n^2 2 n−^1 log log n 2 n^3 (log n)^2 n! n1+^ where 0 <  < 1. 22 n^2 n+

Sol. log log n < log n = ln n < (log n)^2 < √n < n < n log n < n1+^ where 0 <  < 1 < n^2 = n^2 + log n < n + n^3 = 2n^3 < 2 n−^1 = 2n^ = 2n+1^ < en^ < 22 n^ < n!

  1. (10 points) Consider the following function f (n). function f(n) if n > 1 : print line(“still going”) f(n/3) f(n/3) f(n/3) How many lines, as a function of n, does f (n) print? Write a recursive function and solve it. You may assume n is a power of 3 (i.e., n = 3k^ for an integer k).

Sol. Let L(n) denote the number of lines f (n) prints where L(1) = 0.

L(n) = 1 + 3L( n 3

( 1 + 3L(

n 32 )

) = (1 + 3) + 3^2 L(

n 32 ) = (1 + 3) + 3^2

( 1 + 3L( n 33

) = (1 + 3 + 3^2 ) + 3^3 L( n 33

= (1 + 3 + 3^2 + · · · + 3k−^1 ) + 3kL( n 3 k^

= (1 + 3 + 3^2 + · · · + 3k−^1 ) + 3kL(1)

= 1 −^3

k 1 − 3

k (^) − 1 2

  1. (10 points) Let f (n) = O(s(n)) and g(n) = O(t(n)).

Sol. Let f (n) ≤ c 1 s(n) for any n ≥ n 1 where c 1 and n 1 are some integers. Let g(n) ≤ c 2 t(n) for any n ≥ n 2 where c 2 and n 2 are some integers.

(a) Prove that 2f (n) + 3g(n) = O(s(n) + t(n)).

Sol.

2 f (n) + 3g(n) ≤ 2 c 1 s(n) + 3c 2 t(n) for any n ≥ max{n 1 , n 2 } ≤ c(s(n) + t(n)) for any n ≥ max{n 1 , n 2 }, where c = max{ 2 c 1 , 3 c 2 } = O(s(n) · t(n))

(b) Prove that 2f (n) · 3 g(n) = O(s(n) · t(n))

Sol.

2 f (n) · 3 g(n) ≤ 2 c 1 s(n) · 3 c 2 t(n) for any n ≥ max{n 1 , n 2 } ≤ c(s(n) · t(n)) for any n ≥ max{n 1 , n 2 }, where c = 2c 1 · 3 c 2 = O(s(n) · t(n))

(c) Prove that 2f (n) − 3 g(n) 6 = O(s(n) − t(n))

Sol. Counterexample: Let f (n) = 2n^2 , s(n) = n^2 + 3n, g(n) = n^2 , t(n) = n^2 + 2n, where f (n) = O(s(n)) and g(n) = O(t(n)). Then, 2 f (n) − 3 g(n) = n^2 and s(n) − t(n) = n, where 2f (n) − 3 g(n) 6 = O(s(n) − t(n)).

(d) Prove that 23 fg^ ((nn)) 6 = O( s t((nn)) )

Sol. Counterexample: Let f (n) = n^3 , s(n) = n^3 , g(n) = n, t(n) = n^2 , where f (n) = O(s(n)) and g(n) = O(t(n)). Then, 2 f (n) 3 g(n) =^

2 3 n^2 and^

s(n) t(n) =^ n, where^

2 f (n) 3 g(n) 6 =^ O(^

s(n) t(n) ).

p(n) = aknk^ + ak− 1 nk−^1 + · · · + a 1 n + a 0 ≥ aknk^ − (|ak− 1 |nk−^1 + · · · + |a 1 |n + |a 0 |) ≥ aknk^ − b(nk−^1 + nk−^2 + · · · + n + 1) = aknk^ − bnk−^1 (n^0 + n−^1 + n−^2 + · · · + n−(k−2)^ + n−(k−1)) ≥ aknk^ − bnk−^1 since n−i^ ≤ 1, for 0 ≤ i ≤ k − 1. If ak > b, then we have p(n) ≥ (ak − b)nk; hence, p(n) = Ω(nk). So assume that ak ≤ b, and let c be a constant such that 0 < c < ak. Then, p(n) ≥ aknk^ − bnk−^1 = (ak − c)nk^ + cnk^ − bnk−^1. It is clear that cnk^ − bnk−^1 ≥ 0 for any n ≥ n 1 for some constant n 1. Therefore, p(n) ≥ (ak − c)nk^ for n ≥ n 1 which concludes that p(n) = Ω(nk).

  1. (10 points) What value is returned by each of the following functions? Express your answer as a function of n. Give the worst-case running time using the big-Oh notation.

(a)

f unction f (n)

  1. r := 0
  2. f or i := 1 to n do
  3. f or j := 1 to n do
  4. f or k := 1 to n do
  5. r := r + 1
  6. return(r)

Sol. It returns n^3. The time complexity is O(n^3 )

(b)

f unction g(n)

  1. r := 0
  2. f or i := 1 to n do
  3. f or j := 1 to i do
  4. f or k := j to i + j do
  5. f or l := 1 to i + j − k do
  6. r := r + 1
  7. return(r)

Sol.

  • For the innermost loop (for l = 1 to i + j − k), the result is r 1 (i, j, k) = i + j − k.
  • For the second loop (for k = j to i + j), the result is

r 2 (i, j) = r 1 (i, j, j) + r 1 (i, j, j + 1) + r 1 (i, j, j + 2) + · · · + r 1 (i, j, i + j − 1) + r 1 (i, j, i + j) = (i + j − j) + (i + j − (j + 1)) + (i + j − (j + 2)) + · · · + (i + j − (i + j − 1)) +(i + j − (i + j)) = i + (i − 1) + (i − 2) + · · · + 1 = i(i+1) 2 = i^22 +i

  • For the third loop (for j = 1 to i), the result is

r 3 (i) = i · r 2 (i, j) = i^3 + 2 i^2.

  • The fourth, and outermost, loop (for i = 1 to n), the result is

r 4 (n) = ∑ni=1 r 3 (i) = ∑ni=1 i^3 + 2 i^2 = 12 (∑ni=1 i^3 + ∑ni=1 i^2 ) = 12 ( n

(^2) (n+1) 2 4 +^

n(n+1)(2n+1) 6 ) = n

(^2) (n+1) 2 8 +^

n(n+1)(2n+1) 12 Notes:

∑n i=1 i^2 =^

n(n+1)(2n+1) 6 and^

∑n i=1 i^3 =^

n^2 (n+1)^2

  • Given n, the returned value

r = r 4 (n) =

n^2 (n + 1)^2 8 +^

n(n + 1)(2n + 1) 12

  • For example, consider n = 2. Then, r = 48 ·^9 + 2 · 123 ·^5 = 7. To verify r = 7 when n = 2, we consider i = 1, 2. Initially, r = 0. (i) i = 1. In this case, j = k = l = 1; hence, r = 1. (ii) i = 2. (ii-1) j = 1. (ii-1-1) k = 1. (ii-1-1-1) l = 1: r = 2. (ii-1-1-2) l = 2: r = 3. (ii-1-2) k = 2. (ii-1-2-1) l = 1: r = 4. (ii-1-3) k = 3: l is not defined. (ii-2) j = 2. (ii-2-1) k = 2. (ii-2-1-1) l = 1: r = 5. (ii-2-1-2) l = 2: r = 6. (ii-2-2) k = 3. (ii-2-2-1) l = 1: r = 7. (ii-2-3) k = 4: l is not defined. (See Figure 1.)

increase j until B[j] > B[j − 1] } else j = j + 1 endwhile

It is clear that this algorithm works in O(n) time.

  1. (10 points) Consider the following numerical questions game. In this game, player 1 thinks of a number in the range 1 to n. Player 2 has to figure out this number by asking the fewest number of true/false questions. For example, a question may be “Is your number larger than x?” Assume that nobody cheats.

(a) What is an optimal strategy if n in known?

Sol. A basic idea is that Player 2 simply apply Binary search using three variables i, j and m as follows. Let m = b i+ 2 jc. Initially, set i = 1 and j = n. Then, recursively ask whether the number is ≤ m. If answer is YES, then let j = m else let i = m + 1; repeat this process until the number is found. In order to ensure the search space is reduced by at least a half in each iteration, we modify the algorithm as follows. When i + j is even (i.e., the size of the search space is odd), if the answer is YES, Player 2 ask one more question whether the number is ≤ m − 1; if answer is YES, let j = m − 1 else the number must be m, FOUND. In all other cases, no modification is needed. It is clear that in each the search space is reduced by at least a half. Therefore, this algorithm works in O(log n) time.

(b) What is a good strategy is n is not known?

Sol. Let i be an index variable, initially i = 0. Player 2 ask recursively whether the number is ≤ 2 i. If the answer is NO, let i = i + 1 and repeat the question until the answer is YES. Now, let i 0 be such that 2i^0 −^1 < n ≤ 2 i^0. We then let i = 2i^0 −^1 + 1 and j = 2i^0 , and apply the algorithm discussed in part (a). Note that the number of elements in the search space just before applying algorithm in part (a) is 2i^0 − 2 i^0 −^1 ≤ 2 i^0 −^1 ≤ n. Therefore, this algorithm works in O(log n) time as i 0 can be also found in O(log n) time.

  1. (10 points) The input is a sequence of real numbers X = (x 1 , x 2 , · · · , xn) in a non-decreasing order where n is even. The problem is to find a partition of X into n 2 pairs that minimizes the maximum value in S = {s 1 , s 2 , · · · , s n 2 } where each si ∈ S denotes the sum of two numbers in pair i. For example, let X = (1, 2 , 2 , 4 , 6 , 6) and consider two partitions P 1 = ((1, 2), (2, 4), (6, 6)) and P 2 = ((1, 6), (2, 6), (2, 4)). Then, the maximum value of S 1 = { 3 , 6 , 12 } resulted from P 1 is 12 and the maximum value of S 2 = { 7 , 8 , 6 } resulted from P 2 is 8. Describe an O(n) time algorithm to solve the problem, and prove the correctness of your algorithm. Sol. We sort the sequence in non-decreasing order which can be done in O(n log n) time. Let the sorted list be such that x 1 ≤ x 2 ≤ · · · ≤ xn. We then pair x 1 with xn, x 2 with xn− 1 , · · ·,

and pair x n 2 with x n 2 +1, that is, pair xi with xn−i+1 for each i, 1 ≤ i ≤ n 2. Now, we will prove that this algorithm is optimal.

Let Psym denote the result of pairings using the above algorithm. Suppose Psym is not an optimal solution, and let Popt denote the results of pairings using the optimal algorithm. In Popt, let s be the smallest index such that xs is paired with xp, where p 6 = n − s + 1, i.e., s + p 6 = n + 1. It is noted that for each i, 1 ≤ i < s, xi is paired with xn−i+1. It is also noted that p > s by the definition of s. There are two cases to consider.

Case 1. s + p > n + 1. This condition implies that n − p + 1 < s which is impossible by the definition of s. Therefore, this case does not exist, and we only have the following case. Case 2. s + p < n + 1. Consider the number xn−s+1, and assume that xn−s+1 is paired with xq, for some q 6 = s, in Popt. Clearly, q > s by the definition of s. So we have (i) n − s + 1 > p and (ii) q > s, which imply that xp + xq < nn−s+1 + xq by (i) and xs + xn−s+1 < xq + xn−s+1 by (ii). Therefore, we can construct a new Pnew from Popt such that xs is paired with xn−s+ and xp is paired with xq, and all other pairings remain same. Clearly, the maximum sum of Pnew has not been increased from the maximum sum of Popt. So, if Pnew is still not an optimal solution, we can repeat this process until we obtain Psym in which the maximum sum is same as the maximum sum of Popt. Hence, the algorithm produce an optimal solution.