Connecting Tech Pros Worldwide Help | Site Map

time complexity of algorithm that is a worst idea to draw a

Newbie
 
Join Date: Dec 2007
Posts: 20
#1: Oct 15 '08
Can any one help me what will be the time complexity in worst case i.e Big Oh of the following algo I need to resolve it i have worked on it but am not able to come up with the final solution .the below algorithm is going to draw a circle any help or suggestion regarding this algo will be hidhly appreciatable

circle (radius)
for i =10 to -10
for j =-10 to 10
d = sqrt (i+j)
if radius -0.5 < d < radius + 0.5
puetpixel (i,j,1)
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Oct 15 '08

re: time complexity of algorithm that is a worst idea to draw a


There are two loops, one nested in the other. The outermost loop iterates n times
and the innermost loop iterates m times so you take O(n*m) steps. If m == n
then you take O(n^2) steps.

kind regards,

Jos
Newbie
 
Join Date: Dec 2007
Posts: 20
#3: Oct 16 '08

re: time complexity of algorithm that is a worst idea to draw a


You have given The time complexity of the whole Algorithm
Thanks

But i dont understand that u have not calculated the timetime complexity of every statment i.e

d = sqrt (i+j)
if radius -0.5 < d < radius + 0.5
puetpixel (i,j,1)

Can u give more detail regarding following steps
First calculate the time complexity of each statment
Then add up or multiply (depending on the statment ) the time coplexity of each statment
Which will give our final Time complexity>

But i am not able to understand is that What will be the time complexity of each Statment


Any help or suggestion regarding this algo will be hidhly appreciatable
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,387
#4: Oct 16 '08

re: time complexity of algorithm that is a worst idea to draw a


Big O notation is useful when analyzing algorithms for efficiency. For example, the time (or the number of steps) it takes to complete a problem of size n might be found to be T(n) = 4n² − 2n + 2.

As n grows large, the n² term will come to dominate, so that all other terms can be neglected — for instance when n = 500, the term 4n² is 1000 times as large as the 2n term. Ignoring the latter would have negligible effect on the expression's value for most purposes.

Let me repeat that... ignoring the 2n term will have negligible effect on calculating the time it takes to complete an algorithm as n approaches infinity, therefore calculating the time each operation takes is irrelevant since they have virtually no effect on the overall run time of your function.
Newbie
 
Join Date: Dec 2007
Posts: 20
#5: Oct 22 '08

re: time complexity of algorithm that is a worst idea to draw a


what will be the time complexity of the following code

for(i=0 to i<10) =>n+1 (total loop iteration+ one to exit the loop)
j=i+1;

is the above right
so time.complexity will be
O(n)
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Oct 22 '08

re: time complexity of algorithm that is a worst idea to draw a


Yep, that's correct.

kind regards,

Jos
Newbie
 
Join Date: Dec 2007
Posts: 20
#7: Oct 25 '08

re: time complexity of algorithm that is a worst idea to draw a


What will be the time complexity of the following code?

Expand|Select|Wrap|Line Numbers
  1. for (i=0 to i<10)     =>n+1
  2.   for(j=0 to j<10)    =>n(n+1)
  3.       x=i+j              => n
  4.  
Is the above time complexity of each statment correct?
What will be the total time complexity of whole code?

n+1+n(n+1)+n

OR

(n+1)*n(n+1)*n

Which is correct? or both are wrong
What will be the correct time complexity?and how?


Thanks
kinnan
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#8: Oct 25 '08

re: time complexity of algorithm that is a worst idea to draw a


Quote:

Originally Posted by kinannawaz

What will be the time complexity of the following code?

Expand|Select|Wrap|Line Numbers
  1. for (i=0 to i<10)     =>n+1
  2.   for(j=0 to j<10)    =>n(n+1)
  3.       x=i+j              => n
  4.  
Is the above time complexity of each statment correct?
What will be the total time complexity of whole code?

n+1+n(n+1)+n

OR

(n+1)*n(n+1)*n

Which is correct? or both are wrong
What will be the correct time complexity?and how?


Thanks
kinnan

If the first loop runs n times so will the second loop; the second loop is started
n times and the innermost statement will be executed O(n*n) times.

kind regards,

Jos
Newbie
 
Join Date: Dec 2007
Posts: 20
#9: Oct 26 '08

re: time complexity of algorithm that is a worst idea to draw a


Thanks fo the answers

here is tricky one

Expand|Select|Wrap|Line Numbers
  1. for(i=0 to i<10)
  2.  for(j=0 to j<i)
  3.     x=i+j
  4.  
what will be time complexity of each statment and whole code?
please give some elaboration also.

Thank you very much
kinnan
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#10: Oct 26 '08

re: time complexity of algorithm that is a worst idea to draw a


Quote:

Originally Posted by kinannawaz

Thanks fo the answers

here is tricky one

Expand|Select|Wrap|Line Numbers
  1. for(i=0 to i<10)
  2.  for(j=0 to j<i)
  3.     x=i+j
  4.  
what will be time complexity of each statment and whole code?
please give some elaboration also.

Thank you very much
kinnan

Ok, last one: the body of the inner loop runs 1+2+3 ... +n times which makes
(n*n+n)/2 times which is O(n^2)

kind regards,

Jos
Newbie
 
Join Date: Dec 2007
Posts: 20
#11: Oct 28 '08

re: time complexity of algorithm that is a worst idea to draw a


OKAY

But i donot get the answer?

Can i get more detailed answer please

Expand|Select|Wrap|Line Numbers
  1. for(i=0 to i<10)  => n
  2.  for(j=0 to j<i)    => (n+n)/2
  3.     x=i+j            => ???
  4.  
Is the above correct?
Can i get more explanation on the time complexity of the inner loop?
Have u applied some Arithematic series formula?


Thanks
kinnan
Reply