HW1 Answer Key
HW1 Answer Key
Surname:
ID Number:
Faculty of Engineering Signature:
HOMEWORK #1
Academic Year: Spring 2023-24
Given the following algorithm written in pseudocode. Assume that input to the algorithm NEW are an
array A of length 𝑛 . The time complexity of the function RANDOM (A) is assumed to be 𝜃(𝑛).
b. Compute the algorithm’s worst-case running time complexity (i.e. 𝑇(𝑛)) and decide its order of
time complexity when 𝑛 → ∞.
T(n)= c1 + c2 + c3 + c4*(n+1) + c5*𝜃(𝑛) + c6 + c10 + c11 + c12*(n+3) + c13*(n+2) + c14 + c15 + c16
𝑇(1) = 5
Guess is given as 𝑂(𝑛𝑙𝑔𝑛). We must show T(n) ≤ cnlgn for suitable c>0.
T(n) ≤ cnlgn
T(⌊n/4⌋)≤ c(⌊n/4⌋)lg(⌊n/4⌋)
T(n) ≤ 4c*(⌊n/4⌋)*lg(⌊n/4⌋)+5n
≤ 4c*(n/4)*lg(n/4)+5n
= cn*(lgn-2)+5n
= cnlgn-2cn+5n
Use the Master’s Theorem to find the time complexity of the following recurrence.
a. T(n) = 16T(n/4) + n2
b. T(n) = 2T(n/2) + n3
𝑎 = 2, 𝑏 = 2, 𝑓(𝑛) = n3 then
nlog b = nlog 22 = n
a
Since 𝑓(n) = n3 > 𝜃(nlog 22) = 𝜃(n) Case 3 then according to the Master Theorem
(n3/4) ≤ c n3
T(n)= 𝜃(n3)
Question 4: (12,5 + 12,5 = 25 points)
f(n)= 3n3+9 g(n)= 𝑛3 So, we need to find c1, c2 and n0 that satisfies
divide by 𝑛3 0≤ c1 ≤ 3 +9/n3 ≤ c2