in Algorithms edited by
14,928 views
45 votes
45 votes

If $T_1 = O(1)$, give the correct matching for the following pairs:
$$\begin{array}{l|l}\hline \text{(M) $T_n = T_{n-1} + n$}  &  \text{(U) $T_n = O(n)$} \\\hline  \text{(N) $T_n = T_{n/2} + n$} & \text{(V) $T_n = O(n \log n)$} \\\hline  \text{(O) $T_n = T_{n/2} + n \log n$} & \text{(W) $T_n = O(n^2)$} \\\hline  \text{(P) $T_n = T_{n-1} + \log n$} & \text{(X) $T_n = O(\log^2 n)$} \\\hline \end{array}$$

  1. $\text{M-W, N-V, O-U, P-X}$
  2. $\text{M-W, N-U, O-X, P-V}$
  3. $\text{M-V, N-W, O-X, P-U}$
  4. $\text{M-W, N-U, O-V, P-X}$
in Algorithms edited by
14.9k views

4 Comments

can you please tell me how to solve T(n) = 2T( sqt(n) + log(2^k)  ( sorry i dont know how to use square root symbol) ???
0
0

Can someone tell me how to solve 3rd question using tree method

0
0
@prateek

T(n) = T(n/2) +  (n logn)

T(n/2) = T(n/4) + (n/2) log (n/2)

T(n/4) = T(n/8)  + (n/4 )log (n/4)

T(n) = T(n/8) +(n/4) log (n/4) + (n/2) log (n/2) + n log n

……...after k terms

T(n) = T(n/2^k) + (n/2^(k-1)) log ((n/2^(k-1)) + (n/2^(k-2)) log ((n/2^(k-2)) +…...+  (n/4) log (n/4) + (n/2) log (n/2) + n log n   

Base Condition say s,

n=2^k

k = log n

T(n) = T(n/2^k) + (n/2^(k-1)) log ((n/2^(k-1)) + (n/2^(k-2)) log ((n/2^(k-2)) +…...+  (n/4) log (n/4) + (n/2) log (n/2) + n log n

becomes

T(n) = (n/2^(k-1)) log ((n/2^(k-1)) + (n/2^(k-2)) log ((n/2^(k-2)) +…...+  (n/4) log (n/4) + (n/2) log (n/2) + n log n <= nlogn + nlogn +nlogn + ……….. nterms

T(n) < = O(n logn )
1
1

8 Answers

56 votes
56 votes
Best answer
(M) $T(n)$ = Sum of first n natural numbers = $\frac{n(n+1)}{2} = O(n^2)$

(N) $T(n) = \Theta(n)  =O(n)$, third case of Master theorem

($f(n) = n = \Omega \left(n^{\log_b a+ \epsilon}\right) = \Omega \left(n^{\log_2 1 + \epsilon}\right) = \Omega\left(n^{0 + \epsilon}\right) $, satisfied for any positive $\epsilon \leq 1$. Also, $af\left(\frac{n}{b}\right) < cf(n) \implies f\left(\frac{n}{2}\right) < cf(n) \implies \frac{n}{2} < cn$, satisfied for any $c$ greater than $0.5$)

(O) $T(n) = \Theta(n \log n) = O (n \log n)$, third case of Master theorem

($f(n) = n \log n =\Omega\left(n^{\log_b a+ \epsilon}\right) = \Omega \left(n^{\log_2 1 + \epsilon}\right) = \Omega\left(n^{0.5 + \epsilon}\right) $, satisfied for positive $\epsilon = 0.5$. Also, $af\left(\frac{n}{b}\right) < cf(n) \implies f\left(\frac{n}{2}\right) < cf(n) \implies \frac{n}{2} \log \frac{n}{2} < cn \log n$, satisfied for $c = 0.5$)

(P) Like in (M), here we are adding the log of the first n natural numbers. So,

$T_n = \log 1 + \log 2 + \log 3 + \dots + \log n$

$=\log (1\times 2\times \dots \times n)$

$=\log(n!)$

$=\Theta(n \log n)$  (Stirling's Approximation)
edited by
by

4 Comments

I solved recurrence (O) without the $n$ in $nlogn$ and was wondering why the answer isn't $O(log^2n)$ lol. 🤦
0
0
Small technical correction :

Instead of "satisfied for any c between 0 and 0.5" , it should be  "satisfied for any c between 0.5 and 1".
2
2
Thanks Deepak :)
0
0
15 votes
15 votes
M-W, N-U and Both O and P will have V..I guess wrong choices!

3 Comments

Yes. O and P must be $O(n \log n)$.
5
5
@Arjun sir Answer is D selected but no option is actually matched
2
2
yes, no option matches
4
4
8 votes
8 votes

1)  by substitution = c)  O(n^2)

2)  by master's theorem case 2 = a)  O(n)

3)  by master's theorem case 2 =  b)  O(nlogn)

4.T(n) = T(n-1) + logn

T(n) = T(n - 1) + log n

= T(n - 2) + log (n - 1) + log n

= T(n - 3) + log (n - 2) + log (n - 1) + log n

= ...

= T(0) + log 1 + log 2 + ... + log (n - 1) + log n

= T(0) + log n!     = Θ(n log n)

1 comment

How u got T(0)??????

Plz explain
0
0
2 votes
2 votes

Please see the solution for the 3rd and 4th one using the substitution method --

3)    T(n)=T(n/2)+nlogn

           =   nlogn + (n/2)log(n/2)+(n/4)log(n/4)+--------+T(n/2^k)

         =  nlogn + (n/2)(logn-1)+(n/4)(logn-2)+(n/8)(logn-3)+-----------+T(n/2^k)

         =  nlogn + (n/2)logn - n/2 + (n/4)logn - n/2 + (n/8)logn - 3n/8 + -------------

           =  nlogn(1 + 1/2 + 1/4 + 1/8 + -------------------) - n(1/2 + 1/2 + 3/8 + 1/4 + 5/32 + --------)

For the infinite series (1 + 1/2 + 1/4 + 1/8 + -------------------) solution will be 2.

           =  nlogn*2  - n*(very small value near around 2)

           = O(nlogn)

4) T(n) = T(n-1) + logn

          =  logn + log(n-1) + log(n-2) + log(n-3) + log(n-4) + ------------

      = logn + logn + logn + logn + ------------------------------ + logn (we are taking logn as upper bound for Big Oh notation)

= O(logn * logn)

= O(log^2n) 

4 Comments

@piyushkr

How it becomes log n * log n = log^2 n

please explain !

0
0
can someone explain the third one from step from that log second step onwards clearly
0
0
logn will be added n times so answer is nlogn , T(n)=aT(n-b)+f(n), a=1 then TC=(n*f(n))
0
0
Answer:

Related questions