




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





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)
(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).
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!
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
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).
(a)
f unction f (n)
Sol. It returns n^3. The time complexity is O(n^3 )
(b)
f unction g(n)
Sol.
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
r 3 (i) = i · r 2 (i, j) = i^3 + 2 i^2.
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
r = r 4 (n) =
n^2 (n + 1)^2 8 +^
n(n + 1)(2n + 1) 12
increase j until B[j] > B[j − 1] } else j = j + 1 endwhile
It is clear that this algorithm works in O(n) time.
(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.
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.