Name:
Surname:
ID Number:
Faculty of Engineering Signature:
Software Engineering Department
SE 328 – Algorithms and Optimization Methods
HOMEWORK #1
Academic Year: Spring 2023-24
Due Date: 10.03.2024
Instructor: Hürevren Kılıç
Assistant: Mahmut Furkan Bakal
Question 1: (20 + 5 = 25 points)
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 𝜃(𝑛).
NEW(A) Line Times in Worst-Case
Cost
1 a←3; C1 1
2 b←0; C2 1
3 result ←0 ; C3 1
4 for i ← 1 to lenght[A] do ; C4 n+1
5 b ← RANDOM(); C5 𝜃(𝑛)
6 if(a<2) C6 1
7 for j ← 1 to length[A] do C7 0
8 for k ← 1 to b do C8 0
9 result ← result +1; C9 0
10 else C10 1
11 if(b<a) C11 1
12 for j ← 0 to length[A]+1 do C12 n+3
13 result ← result -1; C13 n+2
14 else C14 1
15 result ← a; C15 1
16 return result; C16 1
a. For each of the line above write down line cost and times of its execution in worst-case.
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
So, when 𝑛 → ∞ , T(n) ∈ 𝜃(𝑛)
Question 2: (20 + 5 = 25 points)
Consider the recursive definition of 𝑇(𝑛) given as follows.
𝑇(1) = 5
𝑇(𝑛) = 4𝑇 (⌊n/4⌋)+ 5𝑛, 𝑛 ≥ 4
Solve the recurrence equation by using Substitution Method. Take 𝑂(𝑛𝑙𝑔𝑛) as your initial
guess. Show all steps of your solution.
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
≤ cnlgn holds for c≥ 5/2
Inductive part of the proof is OK!
For base case,
T(1)=5 → 5≤ c1lg1 → 5≤0 does not hold
T(2) = 10 → 10≤ c2lg2 → holds for c≥5
T(3)=15 → 15≤ c3lg3 → holds for c≥5
T(4)= 4T(1) + 20 ≤ 4clg4 → 40≤8c → holds for c≥5
Thus, for c≥5 and when 𝑛 → ∞ guess holds !
Question 3: (12,5 + 12,5 = 25 points)
Use the Master’s Theorem to find the time complexity of the following recurrence.
a. T(n) = 16T(n/4) + n2
𝑎 = 16, 𝑏 = 4, 𝑓(𝑛) = n2 then
𝑛 log ba = 𝑛 log 416 = n2
Since 𝑓(n) = n2 = 𝜃(log 416) = 𝜃(n2) Case 2 then according to the
Master Theorem: 𝑇(n) = 𝜃(nlog 416 lgn )= 𝜃(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
𝑓(n) = n3 = Ω( n (log22+∈) ) where ∈>0 and
𝑎𝑓(𝑛/𝑏) ≤ 𝑐𝑓(𝑛) → 2*(n/2)3 ≤ c* n3
(n3/4) ≤ c n3
1/4 ≤ c So we have some constant 𝑐 < 1
T(n)= 𝜃(n3)
Question 4: (12,5 + 12,5 = 25 points)
Answer the following questions.
a. Prove or disprove that (3n3+9) ∈ 𝜃 (𝑛3) by using formal definition of 𝜃 notation.
f(n)= 3n3+9 g(n)= 𝑛3 So, we need to find c1, c2 and n0 that satisfies
0≤ c1 n3 ≤ 3n3 +9 ≤ c2 n3 for all n≥ n0
divide by 𝑛3 0≤ c1 ≤ 3 +9/n3 ≤ c2
holds for 0≤ c1 ≤ 3 and n0=1
holds for c2 ≥ 12 and n0=1
We are able to find
c1 = 2 , c2 = 12 and n0=1 so the inequality holds. Proof is completed.
b. Prove or disprove that (33n) ∈ O (3n) by using formal definition of O notation.
f(n)= 33n g(n)= 3n So, we need to show that
33n ≤ c*3n → 32n ≤ c
However, there is no such c value that satisfies. So it is disproven.
• You are required to work alone. Please, do your homework by yourself.
• Submit your hand-written homework as a soft copy on moodle until March 10, 2024, Hr:
23:59.
• Assignments sent via e-mail will not be evaluated.
• Also you have to submit your same hand-written homework (without any change) as a hard
copy face-to-face, manually until March 11, 2024, Hr: 16:30.
• Your submission should be made to course assistant Mahmut Furkan Bakal (C Block, Floor (0)
Room #: Z-15)