Comp Sci analysis Assignment4-Solutions
Comp Sci analysis Assignment4-Solutions
a
1. This question refers to the following code. Assume that the / symbol represents integer division: a/b = b .
Let A be an integer array.
Let n = high − low + 1. Assume that localFnc(A, low, high) performs some operation on the entries
A[low], . . ., A[high] inclusive, and that if this function acts on n entries, its cost is n2 .
(5 marks) (a) Let T (n) be the cost of recursiveFnc() as a function of the input size n. Find the recurrence relation
that defines T (n), and show your work.
You will need to use the floor and/or ceiling functions. The only variable should be n: the indices low
and high should not appear in your final answer. Once you have eliminated low and high in favor of n,
you do not need to simplify your expressions any further.
Solution: We have n = high − low + 1. The recursive case occurs when low < high − 5, or
equivalently, when n = high − low + 1 > 6. The base cases occur when n ≤ 6. In the base cases,
the function takes 1 step to check the if condition on line 3.
Let the size of the inputs in line 5 and 6 be A and B, respectively. In the recursive case, the function
takes 1 step to check the if condition on line 3, then n2 steps to execute line 4, then T (A) steps to
execute line 5, then T (B) steps to execute line 6. This gives a total of T (A) + T (B) + n2 + 1.
Therefore the cost function T (n) satisfies a recurrence relation with the following form:
§
1, n≤6
T (n) =
T (A) + T (B) + n2 + 1, n>6
Solution:
(3) The integer 3 r can be written as a product of r 3’s. By the Fundamental Theorem of
Arithmetic, the integer 3 r cannot be written as a product containing the prime factor 2, which
implies that 3 r is not divisible by 2.
(4) Since 3 r is not divisible by 2, it is odd. Subtracting 1 from an odd number yields an even
number, so 3 r − 1 is even, and it follows that (3 r − 1)/2 is an integer, as desired.
(5) Next, we prove that (3s − 3)/6 is an integer. We give a proof by cases.
(7) In the case s ≥ 2, observe that (3s − 3)/6 = 3(3s−1 − 1)/6 = (3s−1 − 1)/2.
(8) Since s − 1 ≥ 1, we can apply our result from step (4) with r = s − 1 to conclude that
(3s−1 − 1)/2 is an integer.
(9) From the outcomes of the previous two steps, we have shown that (3s − 3)/6 = (3s−1 − 1)/2 is
an integer, as desired.
(10) We have considered all possible cases since every integer s ≥ 1 is either equal to 1 or is at least
2. So we proved that (3s − 3)/6 is an integer in all cases, which completes the proof.
Solution:
(2) Let P(r) be the predicate: (3 r − 1)/2 is an integer. We prove that P(r) is true for all r ≥ 1 by
induction on r.
(3) Base Case: Consider r = 1. Then (3 r − 1)/2 = (3 − 1)/2 = 2/2 = 1, which is an integer.
(4) Induction Hypothesis: For some r ≥ 2, assume that P(1) ∧ · · · ∧ P(r − 1) is true.
3r − 1 3r − 3 + 2
=
2 2
3(3 r−1 − 1) + 2
=
2
(3 r−1 − 1)
=3· +1
2
= 3k + 1 for some integer k, by the induction hypothesis
(6) This completes the inductive step, which completes the proof that (3 r − 1)/2 is an integer.
You may use the fact stated in part (b) in your proof.
Solution: Assume that n = 3 r for some integer r ≥ 0. From part (b), we know that the quantities
n−1 n−3
2 and 6 are both integers under our assumption about n.
Using these properties of n, the expression A can be simplified as
n−1 n−1
A = −
2 6
n−1 n−3+2
= −
2 6
n−1 n−3 1
= − +
2 6 3
n−1 n−3
= −
2 6
n
= .
3
Similarly, B can be simplified as
2n − 2 n−1
B = −
3 3
2n 2 1
n
= + − − + −
3 3 3 3
2n n
= −1− +1
3 3
n
= .
3
Note that n = 1 and n = 3 both correspond to base cases, and the recursion case occurs when n ≥ 9.
Consequently, the expression 3n is guaranteed to be an integer when it appears in the input sizes A
and B.
Thus, in the special case where n is a power of 3, the recurrence relation from (a) simplifies to
1, n≤6
T (n) = n
2T 3 + n + 1, n > 6
2
(6 marks) (a) Use the substitution method to derive a closed-form expression for this recurrence relation. Consider
the case where n is even only. You do not need to repeat your calculation for the case where n is odd.
Simplify your answer until no summation notation remains.
= T (n − 4) + (n − 2)2 + n2
= T (n − 6) + (n − 4)2 + (n − 2)2 + n2
= ...
After j substitutions, we guess that
j−1
X
T (n) = T (n − 2 j) + (n − 2k)2 .
k=0
Since n is even, n − 2, n − 4, . . . are also even numbers. The process ends when n − 2 j, the value
passed to T (), is one of the base cases. The even base case is n = 2, so the substitution process ends
when n − 2 j = 2, which occurs when j = 2n − 1. With this substitution,
n/2−2
X n/2−2
X
T (n) = T (2) + (n − 2k) = 4 +
2
(n − 2k)2 .
k=0 k=0
There are a few ways to evaluate this expression. One is by brute force:
n/2−2
X n/2−2
X
4+ (n − 2k)2 = 4 + n2 − 4nk + 4k2
k=0 k=0
n/2−2
X n/2−2
X n/2−2
X
= 4 + n2 1 − 4n k+4 k2
k=0 k=0 k=0
n/2
X
= (2m)2
m=1
n/2
X
= 4 m2
m=1
1 n n
= 4· · + 1 (n + 1) (using summation identity)
6 2 2
n(n + 1)(n + 2)
= .
6
Solution:
(1) Let P(n) be the statement T (n) = 16 n(n + 1)(n + 2). We will show that P(n) is true for all n ≥ 1.
(2) Base cases. When n = 1, we have T (1) = 1 from the recurrence relation, and 16 n(n+1)(n+2) = 1.
Thus P(1) is true.
When n = 2, we have T (2) = 4 from the recurrence relation, and 16 n(n + 1)(n + 2) = 4. Thus P(2) is
also true.
(3) Induction step. Let n > 2 be arbitrary, and assume that T (k) = 16 k(k + 1)(k + 2) for all k < n.
We will show that T (n) = 61 n(n + 1)(n + 2).
(6 marks) (a) Draw the recursion tree for the recurrence relation. Include at least three rows of the tree. Calculate the
sum of each row, and then derive an expression for the sum of all entries in the tree. Use the result to
conjecture a function g(n) so that T (n) ∈ Θ(g(n)).
You should remove unnecessary constants and additive terms from your g(n), that is: if you have an
expression g1 (n), and you can obtain an expression g2 (n) by deleting some constants and additive terms
from g1 such that g2 (n) = Θ(g1 (n)), then you should use g2 (n) for your answer.
Solution:
Row Sums
p p
row 1 2 n 2 n
p
2 9n
Æ Æn Æn
row 2 2 9 2 9 2 n
Æ n
Æ n
Æ n
Æ n
Æ n
Æ n
Æ n
Æ n
Æ n p
row 3 2 81 2 81 2 81 2 81 2 81 2 81 2 81 2 81 2 81 2 n
...
Since n is a power of 9, the recursion ends once the input size has been reduced to a power of 9 that
corresponds to a base case. The first such value is 9.
n
Using the labeling of row numbers shown above in the diagram, the input size in row j is j−1 , and
9
the last row occurs when n/9 j−1 = 9. Solving for j: we see that n = 9 j , so j = log9 n.
p
Therefore, there are log9 n rows, each row sum is 2 n, so the total cost over the entire tree is
p
2 n log9 n.
p
We use this result to conjecture that T (n) ∈ Θ( n log n).
(2 marks) (b) Explain which case of the Master Theorem applies to the recurrence relation, and use that case to prove
your conjecture from part (a).
(2 marks) (a)
1, n≤2
T (n) = n
6T 3 + n2 log(n), n>2
Solution: We have a = 6 and b = 3, so log b (a) = log3 (6) ≈ 1.6. The local cost function is
f (n) = n2 log(n), which satisfies f (n) ∈ Ω(nlog6 (3)+ϵ ) for some ϵ > 0 (for example, ϵ = 0.1). Further,
for any n ≥ 1,
n n 2 n 2 2 n 2 2
af =6 log = n log ≤ n2 log(n) = f (n).
b 3 3 3 3 3 3
Therefore the conditions of Case 3 of the Master Theorem are satisfied. The conclusion of Case 3 is
that T (n) ∈ Θ(n2 log(n)).
(2 marks) (b)
n=1
1,
T (n) = n
8T 2 + 3n3 + n2 , n>1
Solution: We have a = 8 and b = 2, so log b (a) = log2 (8) = 3. The local cost function is f (n) =
3n3 + n2 , which satisfies f (n) ∈ Θ(nlog2 (8) ). Therefore the conditions of Case 2 of the Master Theorem
are satisfied. The conclusion of Case 2 is that T (n) ∈ Θ(n3 log(n)).
(2 marks) (c)
1, n≤3
T (n) = n
7T 4 + n + log(n), n>3
Solution: We have a = 7 and b = 4, so log b (a) = log4 (7) ≈ 1.4. The local cost function is
f (n) = n + log(n), which satisfies f (n) ∈ O(nlog4 (7)−ϵ ) for some ϵ > 0 (for example, ϵ = 0.1).
Therefore the conditions of Case 1 of the Master Theorem are satisfied. The conclusion of Case 1 is
that T (n) ∈ Θ(nlog4 (7) ).
Solution: We have a = 3,b = 3, so log b a = log3 3 = 1, which means that nlog b a = n. The local cost
function is f (n) = n/ log n.
First, we prove that Case 1 cannot be applied, by showing that f (n) ̸∈ O(n1−ϵ ) for all ϵ > 0.
(3) By the definition of little-oh, this means ∀M > 0, ∃n0 > 0, ∀n > n0 , log n ≤ M · nϵ . Re-arranging
the inequality, we conclude ∀M > 0, ∃n0 > 0, ∀n > n0 , n1ϵ ≤ M · log1 n .
1 1
(4) By the definition of little-oh and the outcome of previous step, we conclude that nϵ ∈o log n .
(5) By Fact (5.1), n ∈ Θ(n), and applying Fact (3.1) gives us that n ∈ O(n).
1 1
(6) Applying Fact (5.4) to the outcome of the previous two steps, we get that n · nϵ ∈ o n· log n , which
simplifies to n1−ϵ ∈ o logn n .
n
(7) Applying Fact (3.3) to the outcome of the previous step, we get log n ∈ ω n1−ϵ . Applying Fact
(3.7) gives logn n ̸∈ O n1−ϵ , as desired.
Next, we prove that Case 2 cannot be applied, by showing that f (n) ̸∈ Θ(n).
(2) By the definition of little-oh, this means ∀M > 0, ∃n0 > 0, ∀n > n0 , 1 ≤ M · log n. Re-arranging the
inequality, we conclude ∀M > 0, ∃n0 > 0, ∀n > n0 , log1 n ≤ M .
1
(3) By the definition of little-oh and the outcome of previous step, we conclude that log n ∈ o (1).
(4) By Fact (5.1), n ∈ Θ(n), and applying Fact (3.1) gives us that n ∈ O(n).
1
(5) Applying Fact (5.4) to the outcome of the previous two steps, we get n · log n ∈ o (n · 1), which
simplifies to logn n ∈ o (n).
n
(6) Applying Fact (3.6) to the outcome of the previous step, we get log n ̸∈ Ω (n). Applying Fact (3.1)
gives logn n ̸∈ Θ (n), as desired.
Finally, we prove that Case 3 cannot be applied, by showing that f (n) ̸∈ Ω(n1+ϵ ) for all ϵ > 0.
(2) By Fact (1.1) we know 1 ∈ o(log n), and by Fact (1.2) we know log n ∈ o(nϵ ). By Fact (2.3), this
implies that 1 ∈ o(nϵ ). Fact (3.4) implies that 1 ∈ O(nϵ ).
n
(3) We proved in step ((5)) above that log n ∈ o (n).
(4) Applying Fact (5.4) to the outcome of the previous two steps, we get n
log n · 1 ∈ o (n · nϵ ), which
simplifies to logn n ∈ o n1+ϵ .
n
(5) Applying Fact (3.6) to the outcome of the previous step, we conclude that log n ̸∈ Ω n1+ϵ , as
desired.
Floors/Ceilings
For any real number x and any real number y ̸= 0:
• −⌊x⌋ = ⌈−x⌉ and −⌈x⌉ = ⌊−x⌋
• for any integer k: ⌊k⌋ = ⌈k⌉ = k
• for any integer k: k + ⌊x/ y⌋ = ⌊k + x/ y⌋
• for any integer k: k + ⌈x/ y⌉ = ⌈k + x/ y⌉
• for any integer k: ⌊(k + 1)/2⌋ = ⌈k/2⌉ and ⌈(k − 1)/2⌉ = ⌊k/2⌋
⌊x/a⌋ x ⌈x/a⌉ x
• for any integers a, b: ⌊ b ⌋ = ⌊ ab ⌋ and ⌈ b ⌉ = ⌈ ab ⌉
Summation Identities
b n
X X n(n + 1)
1= b−a+1 j=
j=a j=1
2
n n
X
2 n(n + 1)(2n + 1) X n2 (n + 1)2
j = j3 =
j=1
6 j=1
4
n ∞
X q n+1 − 1 X 1
∀q > 0, q ̸= 1, qj = ∀q, 0 < q < 1, qj =
j=0
q−1 j=0
1−q
Xb Xb Xb b
X b
X
( f ( j) + g( j)) = f ( j) + g( j) c · f ( j) = c · f ( j) for any constant c
j=a j=a j=a j=a j=a
Group 1: the hierarchy summarized. For all constants a > 0 and b > 0 and c > 0:
(1.1) if a > 1, then c ∈ o(loga (n))
(1.2) if a > 1, then loga (n) ∈ o(n b )
(1.3) if a < b, then na ∈ o(n b )
(1.4) if b > 1, then na ∈ o(b n )
(1.5) if 1 < a < b, then a n ∈ o(b n )
(1.6) a n ∈ o(n!)
Group 2: transitivity.
(2.1) if f ∈ O(g) and g ∈ O(h), then f ∈ O(h)
(2.2) if f ∈ Ω(g) and g ∈ Ω(h), then f ∈ Ω(h)
(2.3) if f ∈ o(g) and g ∈ o(h), then f ∈ o(h)
(2.4) if f ∈ ω(g) and g ∈ ω(h), then f ∈ ω(h)
Group 4: addition.
(4.1) if f ∈ O(h) and g ∈ O(h), then f + g ∈ O(h)
(4.2) if f ∈ Ω(h), then f + g ∈ Ω(h)
(4.3) if f ∈ o(h) and g ∈ o(h), then f + g ∈ o(h)
(4.4) if f ∈ ω(h), then f + g ∈ ω(h)
Group 5: multiplication.
(5.1) for any constant c > 0, c f ∈ Θ( f )
(5.2) if f1 ∈ O(g1 ) and f2 ∈ O(g2 ) then f1 f2 ∈ O(g1 g2 )
(5.3) if f1 ∈ Ω(g1 ) and f2 ∈ Ω(g2 ) then f1 f2 ∈ Ω(g1 g2 )
(5.4) if f1 ∈ O(g1 ) and f2 ∈ o(g2 ) then f1 f2 ∈ o(g1 g2 )
(5.5) if f1 ∈ Ω(g1 ) and f2 ∈ ω(g2 ) then f1 f2 ∈ ω(g1 g2 )