0% found this document useful (0 votes)
14 views4 pages

Daa Assignment

HELPS TO CLEAR THE CONCEPTS OF DAA

Uploaded by

shawalahmad97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Daa Assignment

HELPS TO CLEAR THE CONCEPTS OF DAA

Uploaded by

shawalahmad97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Question 1: Master Theorem Applications

For each recurrence relation, we will apply the Master Theorem:

a) T(n) = 4T(n/2) + n^2

 Here, a=4a = 4a=4, b=2b = 2b=2, and d=2d = 2d=2.


 According to the Master Theorem:
o a=bda = b^da=bd, i.e., 4=224 = 2^24=22, so this is case 2 of the theorem.
o Time complexity is O(n2log⁡n)O(n^2 \log n)O(n2logn).

b) T(n) = 2nT(n/2) + n^n

 Here, a=2na = 2na=2n, b=2b = 2b=2, and there is an exponential factor nnn^nnn.
 This doesn't fit the basic form of the Master Theorem because of the term nnn^nnn, and
its complexity is exponential.
 Time complexity is O(nn)O(n^n)O(nn).

c) T(n) = 2T(n/2) + n \log n

 Here, a=2a = 2a=2, b=2b = 2b=2, and the additional term nlog⁡nn \log nnlogn complicates
things.
 Using a modified form of the Master Theorem, the time complexity is O(nlog⁡2n)O(n \
log^2 n)O(nlog2n).

d) T(n) = 3T(n/3) + \sqrt{n}

 Here, a=3a = 3a=3, b=3b = 3b=3, and the non-recursive term is n\sqrt{n}n.
 Using the Master Theorem, this falls under case 1, where log⁡ba=1\log_b a = 1logba=1
and d=1/2d = 1/2d=1/2.
 Time complexity is O(n)O(n)O(n).

e) T(n) = \sqrt{2} T(n/2) + \log n

 This recurrence doesn't fit the exact form of the Master Theorem, but based on analysis:
 Time complexity is O(log⁡2n)O(\log^2 n)O(log2n).

Question 2: Various Algorithm Analyses

a) Towers of Hanoi (move function)

 The Towers of Hanoi has a well-known time complexity of O(2n)O(2^n)O(2n), since


each call to move recursively breaks down into two smaller problems.
b) IsBST function

 The function isBST recursively checks each node and its children to ensure it follows the
BST property.
 Time complexity: Since it checks each node exactly once, it runs in O(n)O(n)O(n), where
nnn is the number of nodes in the tree.

c) Recurrence T(n) = T(n/3) + O(n)

 This recurrence fits the Master Theorem, where a=1a = 1a=1, b=3b = 3b=3, and d=1d =
1d=1 (for the O(n)O(n)O(n) term).
 Time complexity is O(n)O(n)O(n).

d) Recurrence T(n) = 2T(n/2) + O(n \log n)

 This is a modification of the standard form of the Master Theorem.


 Time complexity is O(nlog⁡2n)O(n \log^2 n)O(nlog2n).

e) Recurrence T(n) = T(n/2) + T(n/4) + O(n)

 This is a combination of divide-and-conquer steps.


 Time complexity: Solving this using the recursion tree method gives O(n)O(n)O(n).

Question 3: Asymptotic Notations

a)

cpp
Copy code
int sum = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j > 0; j /= 2) {
sum++;
}
}

 Outer loop runs nnn times. The inner loop runs in O(log⁡i)O(\log i)O(logi) time.
 Total complexity: O(nlog⁡n)O(n \log n)O(nlogn).

b)

cpp
Copy code
int sum = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
for (int k = j; k < n; k += j) {
sum++;
}
}
}

 The outer loop runs nnn times. The second loop runs approximately O(n)O(n)O(n) times,
but the third loop has a decreasing step k+=jk += jk+=j, making it logarithmic in terms of
jjj.
 Time complexity: O(n2log⁡n)O(n^2 \log n)O(n2logn).

c)

cpp
Copy code
k = m = 0;
for(i = 1; i < n; i *= 2) {
k++;
}
for(j = 0; j < k * k; j *= 2) {
m++;
}

 First loop runs in O(log⁡n)O(\log n)O(logn). Second loop runs O(log⁡2n)O(\log^2


n)O(log2n) times.
 Time complexity: O(log⁡2n)O(\log^2 n)O(log2n).

d)

cpp
Copy code
i = n, tot = 0;
while (i > 1) {
tot += i;
i = i / 2;
}

 This is a logarithmic loop, cutting iii in half at each step.


 Time complexity: O(log⁡n)O(\log n)O(logn).

e)

cpp
Copy code
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; j++) {
for(int k = 0; k < 10; k++) {
cout << "bawa" << endl;
}
}
}

 The outer two loops run O(n2)O(n^2)O(n2), and the inner loop runs a constant 10 times.
 Time complexity: O(n2)O(n^2)O(n2).
f)

cpp
Copy code
void function(int n) {
if (n < s) {
cout << n << endl;
} else {
for (int i = 0; i < n; i = i / k) {
cout << i << endl;
}
}
}

 The first part (when n <s) is constant, but in the else part, the loop reduces i by dividing
by k, leading to a logarithmic runtime.
 Time complexity: Minimum O(1) Maximum O(log n)

You might also like