Comp Sci analysis Assignment4-Questions
Comp Sci analysis Assignment4-Questions
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.
(3 marks) (c) In the special case where n = 3 r for some integer r ≥ 0, prove that your recurrence relation from part
(a) can be simplified to the following form:
d, n≤c
T (n) =
aT nb + f (n), n > c
You may use the fact stated in part (b) in your proof.
(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.
(4 marks) (b) Prove that your closed-form expression in part (a) is correct for all n ≥ 1 using mathematical induction.
(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.
(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).
4. For each of the following recurrence relations, state which case of the Master Theorem can be applied and
justify your answer, then use the Master Theorem to find a function g(n) such that T (n) ∈ Θ(g(n)).
(2 marks) (a)
1, n≤2
T (n) = n
6T 3 + n log(n),
2
n>2
(2 marks) (b)
n=1
1,
T (n) = n
8T 2 + 3n3 + n2 , n>1
(2 marks) (c)
1, n≤3
T (n) = n
7T 4 + n + log(n), n>3
(6 marks) 5. Give a complete proof that none of the 3 cases of the Master Theorem apply to the following recurrence
relation.
1, n≤1
T (n) =
3T 3n + logn n , n > 1
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 )