आपने ‘Good Algorithm’ के बारे में पढ़ा है कि जिसकी Time Complexity कम होती है वही Good Algorithm होता है | अगर अब आपको एक ही problem के 10 अल्गोरिथम दे दिए जाए तो आप कैसे पहचानेंगे कि कौन सा अल्गोरिथम कम time लेगा और कौन सा अल्गोरिथम ज्यादा time लेगा अगर अल्गोरिथम कि time complexity n variable के रूप में दी हुई है |

आसान लगता है ना, इन questions को solve करिए |

Que 1– For larger n, which grows faster log n or √n?

Ans – We take log n ≈ √n

हम दोनों तरफ log लेंगे

→ log (log n) ≈ log (√n)

→ log (log n) ≈ log (n1/2)

→ log (log n) ≈ ½ log (n)

So, log (log n) < constant.log n

√n ज्यादा fast grow करेगा log n से, और आप इसको hidden trial rule से भी check कर सकते हैं

 n = 1n = 4n = 16n = 64n = 256
Log n02468
√n124816

हम ये larger n के लिए देख रहे है। n = 16 पर log n भी 4 है और √n भी 4 है पर जब n > 16 होगा तब √n काफी fast grow करेगा log n से |

 

Que 2- For larger n, which grows faster n! or nn?

Ans –

n! = n * (n-1) * (n-2) * (n-3) * … * 2 * 1  (upto n terms)

nn = n * n * n * n * … * n (upto n terms)

जब n > 1 होगा, तब nn fast grow करेगा n! से.

Asymptotic Analysis

7 algorithms with time complexities

हम चाहते हैं की हमने जो अल्गोरिथम choose की है वो सबसे कम time ले | इस problem में हमारे पास 7 तरह की अल्गोरिथम हैं और उनकी time complexity भी दी हुई है, बस आपको पहचानना है कि किसकी time complexity सबसे कम होगी और हम उसी अल्गोरिथम को अपने program में लगायंगे |

A5 की time complexity सबसे कम है (log n), तो हम इसी अल्गोरिथम को अपने program में लगायंगे |

एक बात और, आपको ऊपर दो questions दिए गए है, सबसे ज्यादा problem log n और √n को पहचानने में होती है या फिर nn और n! को पहचानने में होती है | उसका answer मैने दे दिया है और अगर कोई भी problem हो तो आप एक sequence को याद कर सकते हैं |

For larger n, the growth of time complexity is

Log n < √n < n < n log n < n2 < n3 < 2n < 3n < n! < nn

Note – nn वाला question कभी नहीं आयेगा क्यूंकि इसकी complexity maximum होती है | ये बिलकुल वैसा ही है जैसे में आपसे पूछूँ की आप कहाँ रहते हैं? तो आप answer दे की मैं इस पृथ्वी पर रहता हूँ | हर कोई पृथ्वी पर रहता है उसमे कोई नयी बात नहीं है ।

Test Yourself

Q1- Arrange given terms in ascending order for large n.

Time complexities is – n2, nlogn, n, 2logn, 4logn, 3n, 2n, n√n

One thing should be in your mind. In algorithm, default log base is 2.

If we go through the input then we can say in full confidence

n < nlogn < n√n < n2 < 2n < 3n

but there has been a confusion between 2logn and 4logn.

-> formula is [alogax = x]

-> n = 2logn

Also,

-> 4logn = 22*logn

-> 4logn = 2logn^2

-> formula is [alogax = x]

-> 4logn = n2

The correct ascending sequence is

n = 2logn < nlogn < n√n < n2 = 4logn < 2n < 3n

Q2- Arrange given terms in ascending order for large n.

Time complexities is – √logn, (loglogn)2, logn*loglogn, logn/loglogn, √logn/loglogn

Let n is very large and a perfect square

Suppose n = 22^36

√logn = √236 = 218

(loglogn)2 = 362 = 24*2 = 28

logn*loglogn = 236*36 = 236*24 = 240

logn/loglogn = 236/36 = 236/24 = 232

√logn/loglogn = 218/24 = 214

So, the ascending order is

(loglogn)2 < √logn/loglogn < √logn < logn/loglogn < logn*loglogn

Q3- Arrange given terms in ascending order for large n.

Time complexities is –

  1. (√n)logn
  2. (logn)logn
  3. (loglogn)n
  4. (logn)n
  5. nloglogn
  6. n2

Taking log

  1. Logn*log√n,
  2. logn*loglogn,
  3. n*logloglogn,
  4. √n*loglogn,
  5. loglogn*logn,
  6. 2logn

Let n = 22^100

  1. 2100*(1/2)*2100 = 2199
  2. 2100*100 = 2107
  3. 22^100*log100 = 22^100*27
  4. 100*22^50 = 22^50*27
  5. 100*2100 = 2107
  6. 2*2100 = 2101

So, the ascending order is

n2 < nloglogn = (logn)logn < (√n)logn < (logn)n < (loglogn)n

Q4- Arrange given terms in ascending order for large n.

Time complexities is

-> 1, 2, n1/n, n1/logn, (logn)1/logn

Taking log

-> Log1, log2, logn/n, logn/logn, loglogn/logn

Let’s say n = 22^100

-> 0, 1, (2^100)/(22^100), 1, 100/2100

The ascending order of time complexities is

-> 1 < n1/n < (logn)1/logn < 2 < n1/logn