Procedures A() will be called 1 time, B() n-1 times and function what() will be called n times.

total T.C will be as shown below

The Gateway to Computer Science Excellence

First time here? Checkout the FAQ!

x

+19 votes

Consider the following algorithms. Assume, procedure $A$ and procedure $B$ take $O (1)$ and $O(1/n)$ unit of time respectively. Derive the time complexity of the algorithm in $O$-notation.

algorithm what (n) begin if n = 1 then call A else begin what (n-1); call B(n) end end.

+24 votes

Best answer

The recurrence relation for time complexity is

$T(n) = T(n-1) + \frac{1}{n} + c$ $(O(1/n)$ replaced with $1/n$ and so our answer will also be in $O$ only and not $\Theta)$

$T(n) = T(n-2) + \frac{1}{n-1} + \frac{1}{n} + 2c $

$\\\qquad=T(1) + \frac{1}{2} + \frac{1}{3}+ \dots + \frac{1}{n} + (n-1) c $

$\\\qquad=A(1) +\frac{1}{2} + \frac{1}{3} \dots + \frac{1}{n} + nc$

$ \\\qquad= 1 +\frac{1}{2} + \frac{1}{3} \ldots + \frac{1}{n} + nc$

$ \\\qquad= \log n + nc $

$($Sum of the first $n$ terms in harmonic series is $\Theta(\log n))$

So, our time complexity will be $O(n)$.

$T(n) = T(n-1) + \frac{1}{n} + c$ $(O(1/n)$ replaced with $1/n$ and so our answer will also be in $O$ only and not $\Theta)$

$T(n) = T(n-2) + \frac{1}{n-1} + \frac{1}{n} + 2c $

$\\\qquad=T(1) + \frac{1}{2} + \frac{1}{3}+ \dots + \frac{1}{n} + (n-1) c $

$\\\qquad=A(1) +\frac{1}{2} + \frac{1}{3} \dots + \frac{1}{n} + nc$

$ \\\qquad= 1 +\frac{1}{2} + \frac{1}{3} \ldots + \frac{1}{n} + nc$

$ \\\qquad= \log n + nc $

$($Sum of the first $n$ terms in harmonic series is $\Theta(\log n))$

So, our time complexity will be $O(n)$.

+5

@Srestha, Could you pls help why have we added constant 'c' in the recurrence? The question does not mention that on its own isnt it? I can understand that decrement of (n-1) or call function might take some constant time but shouldnt we follow the question exactly in such cases?

I mean to say the reccurrence should be

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

T(1) = 1

Solving we get **log n**

0

See here procedure A and procedure B takes O(1) and O(1/n) time.

But other than this what() is also doing it's work in some constant time.

It will call upto n times. So O(nc)

right?

But other than this what() is also doing it's work in some constant time.

It will call upto n times. So O(nc)

right?

+1

@Srestha Yes I understood that point. But my doubt is - Is it fair to assume that? In ideal case yes the operation what call() is going to take some constant time. But nothing is mentioned in question explicitly about that.

Usually in such questions they include this line - "**All the other operations take constant amount of time" **which is missing here.

+2

question is about,

" **time complexity of the algorithm **".

not the time complexity of A,B

So, u have to think about complexity of whole algo

+1

@srestha,@arjun sir

Please check two points:-

$Point1$

If i consider merge sort recreance

$T(n) = 2T(n/2) +0(n) +c $

Each call to merge will solve two subproblems,and then we have $(o(n)$ time to merge them by a fxn call.Now it will also do some constant operations which are represented by $c$.

But we take $n+c=n$ and write it as :-

$T(n) = 2T(n/2) +0(n) $

so, why cant i remove c here as @yg92 has aksed?

--------------------------------------------------------------------------------------------------------------------------

$Point2$

If i cannot be done then i think we can rewrite the given recreance as:-

$T(n)=T(n−1)+1/n+c$

$T(n)=T(n−1)+c1+c $// As 1/n is always less than 1,means some constant(c1)

$T(n)=T(n−1)+k; $// k is a constant ,k=c1+c

Now solve it we get $o(n)$ times.Please check once,?

But we cannot remove c,as i did in point 1,because $1/n$ can be less than 1(it cannot be even equal because if 1/n=1 means n=1 means base condition),but constant will always be +ve,may be one or more,so constant will dominate.

More strongly i can say that $1/n$ is strictly less than constant

+1

when same function is called , then constant will not change rt?

It will not c, c1.....

c will be same for each calling

It will not c, c1.....

c will be same for each calling

0

@srestha @Arjun sir I still didn't get the answer to my doubt after reading the comments as well.

My doubt is why add the constant part in the recurance relation?

My doubt is why add the constant part in the recurance relation?

- All categories
- General Aptitude 1.4k
- Engineering Mathematics 5.9k
- Digital Logic 2.3k
- Programming & DS 4.2k
- Algorithms 3.7k
- Theory of Computation 4.6k
- Compiler Design 1.7k
- Databases 3.4k
- CO & Architecture 2.9k
- Computer Networks 3.4k
- Non GATE 1.2k
- Others 1.3k
- Admissions 506
- Exam Queries 482
- Tier 1 Placement Questions 22
- Job Queries 64
- Projects 15

40,871 questions

47,532 answers

146,043 comments

62,298 users