Mod-1_Assignment_Final_1
Mod-1_Assignment_Final_1
Q.2 Consider the following 2 conditions (i) and (ii) for the function f(n), g(n) and h(n).
(𝑖) 𝑓(𝑛) = 𝑂(𝑔(𝑛)) 𝑎𝑛𝑑 𝑔(𝑛) ≠ 𝑂(𝑓(𝑛))
(𝑖𝑖) 𝑔(𝑛) = 𝑂(ℎ(𝑛)) 𝑎𝑛𝑑 ℎ(𝑛) = 𝑂(𝑔(𝑛))
Check which of the following statements are TRUE/FALSE, justify?
a) 𝑓(𝑛) = Θ(ℎ(𝑛))
b) 𝑓(𝑛) ∗ ℎ(𝑛) = 𝑂(ℎ(𝑛) ∗ 𝑔(𝑛))
c) ℎ(𝑛) = Θ(max {𝑓(𝑛), 𝑔(𝑛)})
d) ℎ(𝑛) ∗ 𝑔(𝑛) = Ω(𝑓(𝑛) ∗ 𝑔(𝑛))
e) 𝑓(𝑛) + 𝑔(𝑛) = 𝑂(ℎ(𝑛))
f) 2𝑓(𝑛) = 𝑂(2𝑔(𝑛) )
Q.3: Analyze the time complexity of the following Non-recursive and Recursive algorithm
(code).
a) for(i = 1; i n; i + +) c) 𝑓𝑢𝑛( )
count = 0; {
for(i = 1; i n; i* = 2) for( j = 1; j n; j + +) 𝑖𝑛𝑡 𝑖 = 1; 𝑠 = 1;
𝑤ℎ𝑖𝑙𝑒(𝑠 <= 𝑛)
for( j = 1; j n; j* = 2) j = j * 2; {
count = count + 1; 𝑖 + +;
𝑠 = 𝑠 + 𝑖;
𝑝𝑟𝑖𝑛𝑡𝑓("𝐴𝑚𝑖𝑡𝑦 𝑈𝑛𝑖𝑣𝑒𝑟𝑠𝑖𝑡𝑦");
}
}
d) 𝑒) 𝑖𝑛𝑡 𝑓𝑎𝑐𝑡(𝑖𝑛𝑡 𝑛) 𝑓)
A() { 𝑣𝑜𝑖𝑑 𝑇𝑒𝑠𝑡(𝑖𝑛𝑡 𝑛)
{ 𝑖𝑓(𝑛 == 0) {
int i,j,k,n 𝑟𝑒𝑡𝑢𝑟𝑛 1; 𝑖𝑓(𝑛 > 1)
for(i=1; i<=n;i++) 𝑒𝑙𝑠𝑒 {
{ 𝑟𝑒𝑡𝑢𝑟𝑛 𝑛 ∗ 𝑓𝑎𝑐𝑡(𝑛 − 1); 𝑓𝑜𝑟(𝑖 = 0; 𝑖 < 𝑛; 𝑖 + +)
for(j=1; j<=i2;j++) } {
{ 𝑝𝑟𝑖𝑛𝑡𝑓("Amity University");
for(k=1;k<=n/2;k++) }
{ 𝑇𝑒𝑠𝑡(𝑛/2);
printf("Amity"); 𝑇𝑒𝑠𝑡(𝑛/2); -
} } }
} } }
Q.6 Use a Recursion tree (for Q.6(i) and 6(ii)) and Master method (for Q.6(iii), (iv)) and
change of variable for (6 (v)) to give an asymptotic tight solution to the recurrence
𝑛
(i) 𝑇(𝑛) = 3𝑇 ( 4) + Θ(𝑛2 )
(ii) 𝑇(𝑛) = 𝑇(𝑛 − 1) + 𝑇(𝑛 − 2) 𝑤𝑖𝑡ℎ 𝑇(0) = 0 𝑎𝑛𝑑 𝑇(1) = 1
𝑛
(iii) 𝑇(𝑛) = 2𝑇 ( 2) + 𝑛2 𝑙𝑜𝑔2 𝑛
𝑛 𝑛2
(iv) 𝑇(𝑛) = 2𝑇 (2) + 𝑙𝑜𝑔𝑛
1 𝑛=2
(v) 𝑇(𝑛) = {
√𝑛𝑇(√𝑛) + 𝑛 𝑛>2
Q.7 The recurrence 𝑇(𝑛) = 𝟗𝑇(𝑛/𝟐) + 𝑛2 describes the running time of algorithm A.
A competing algorithm A' has a running time of 𝑇′(𝑛) = 𝑎𝑇′(𝑛/𝟒) + 𝑛2 . What is the
largest integer value for 𝒂 such that A' is asymptotically faster than A?
***************************************************