902,094 Members 
Top Members by Rank
JOIN DANIWEB
MEMBER LOG IN
how to find thecomplexity of pro
Hardware & Software
Software Development
Web
Development
Internet Marketing
Business Exchange
Community Center
Software Development
>
Computer Science
> calculate time complexity of the algorithm
These search terms have been
highlighted:
how to find thecomplexity of program
(
Clear
)
Ad:
Run Better with SAP
Computer Science
Discussion Thread
Unsolved
Views:
54254
Page 1
2
3
Next
You are currently viewing
page 1
of this multi

page discussion thread
Similar Threads
Time complexity of algorithm
in Computer Science
Permalink
May 3rd, 2006
0
calculate time complexity of the algorithm
Expand Post »
Plz tell me
how
I would calculate time complexity
of
the
program
:
int i = N;
while (i > 0)
{
int Sum = 0;
int j;
for (j = 0; j < i; j++)
Sum++;
cout << Sum << endl;
i

;
}
thnx in advance
Rose Aashii
Newbie Poster
Offline
9 posts
since Apr 2005
Permalink
May 3rd, 2006
3
Re: calculate time complexity of the algorithm
Count the total number
of
basic operations, those which take a
constant amount
of
time. That's all
there is
to
it.
For example, the code
int Sum = 0;
is 1 basic operation. Then
j = 0;
is another basic operation.
Then
j < i
forms yet another basic operation. Count these, and you get your time complexity.
For example,
take the loop,
(
Toggle Plain Text
)
for (j = 0; j < i; j++)
Sum++;
In this loop, you end up evaluating
j = 0
once,
j < i
a total
of
i + 1 times,
j++
i times,
and
Sum++;
i times. So the total number
of
basic operations is: 1 + (i + 1) + i + i = 3i + 2.
Then take the block
of
code,
(
Toggle Plain Text
)
int Sum = 0;
int j;
for (j = 0; j < i; j++)
Sum++;
cout << Sum << endl;
i

;
Here, we have
int Sum = 0;
taking one operation,
for (j = 0; j < i; j++) Sum++;
taking
3i + 2 operations,
cout << Sum << endl;
taking 1 operation (or 2 operations, depending
on
how
you look at it, but the whole thing takes a constant amount
of
time anyway). Then
i

;
takes one operation. So that's a total
of
1 + (3i + 2) + 1 + 1 = 3i + 5.
Then take the block
of
code,
(
Toggle Plain Text
)
int i = N;
while (i > 0)
{
we calculated this
to
take 3i + 5 operations
}
Every time through the loop, 1 + (3i + 5) operations are performed (one is added for the
comparison
i > 0
).
Of
course, the value
of
i changes every time through the loop. So calculating the
number
of
operations here takes a little bit
of
math. The first time through, we take 3N + 6
operations. The second time, we take 3(N

1) + 6 operations. The third time, 3(N

2) + 6 operations.
And so on and so on, until we take 3(1) + 6 operations. So it's time for some math.
This number
of
operations is equivalent
to
. (It's good
to
know the formula N + (N

1) + ...
+ 2 + 1 = N(N+1)/2.)
Now, what I just described is a very long and drawn
out way
of
doing things. Nobody actually works
problems like these out, but it's something you can fall back on. There are many shortcuts you can
take. For example, it doesn't change anything if you mush all the constant values together (the way I
treated
the cout statement as just one operation). And whenever you see a while loop that runs N
times, just multiply N by the amount
of
operations done on the inside.
Here's
http://etern
allyconfuzzled.com/articles/bigo.html
which explains things a bit differently.
Rashakil Fol
Super Senior Demiposter
Offline
2,291 posts
since Jun 2005
Permalink
May 24th, 2006
0
Re: calculate time complexity of the algorithm
Very good work !!! Really well explained
brainbox
Light Poster
Offline
29 posts
since May 2006
Permalink
May 27th, 2006
1
Re: calculate time complexity of the algorithm
Hi
Could you please explain
how
did you come
to
the result
of
3/2N^2... as i cannot figure that out, and
i really want
to
know
how
to
caluclate the time complexity
of
a given algorithm. I quite understood
it
that you have
to
count the steps invovled in it, but
how
would i be able
to
get
to
the result, please
explain more mathematically.
Thanks
brainbox
Light Poster
Offline
29 posts
since May
2006
Permalink
May 28th, 2006
1
Re: calculate time complexity of the algorithm
Which step don't you get? Do you understand
how
I
got
to
?
Rashakil Fol
Super Senior Demiposter
Offline
2,291 posts
since Jun 2005
Permalink
May 28th, 2006
0
Re: calculate time complexity of the algorithm
Well i understand 3N, and 3(N

1) , 3(N

2)..... But i did not really understand, 3(2) + 6, 3(1) + 6 etc,
as i think this would be result
of
replacing the N's value {I am not sure
though}.
However, the real difficulty is calculating the actual result. 3/2N^2 + 15/2n, from this equation.
Thanks in advance
brainbox
Light Poster
Offline
29 posts
since May 2006
Permalink
May 28th, 2006
2
Re: calculate time complexity of the algorithm
The first time through the loop, the value
of
i is N, so we know it
takes 3N+6 operations in the loop.
The second time through the loop, the value
of
i is N

1, so it takes 3(N

1)+6 operations. The third
time through, the value
of
i is N

2, so it takes 3(N

2)+6 operations. And so on, until the last time
through the loop, th
e value
of
i is 1, so it takes 3*1+6 operations. (The second

to

last time through
the loop, the value
of
i is 2.)
That gives
operations.
Then I rearranged terms and pulled out the 3
to
get the second line.
Then, using the formula N+(N

1)+(N

2)+...+3+2
+1 = N(N+1)/2, I substituted.
Rashakil Fol
Super Senior Demiposter
Offline
2,291 posts
since Jun 2005
Permalink
Apr 18th, 2007
0
Re: calculate time complexity of the algorithm
How
To
Calculet Time Complexity
Of
An Algo
sun_kangane
Newbie Poster
Offline
1 posts
since Mar 2007
Permalink
Apr
20th, 2007
0
Re: calculate time complexity of the algorithm
Click
to
Expand / Collapse
Quote originally posted by sun_kangane ...
How
To
Calculet Time Complexity
Of
An
Algo
By reading the responses posted in this and the other link you hijacked.
WaltP
Posting Expert
Online
5,993 posts
since May 2006
Ads by Google
Love to Code?
Compete against other coders 24x7 C, Java, PHP, Perl, Ruby and more!
www.CodeChef.com
Permalink
Jun 29th, 2007
0
Re: calculate time complexity of the algorithm
Hello frnds ....
itz
really very well explained but it will be more clear if someone calculate for this one bcoz i was not
able
to
compute for this .... please guide me ....
thanx in advance ....
int flag,temp;
do {
flag=0;
for(int j=0;j<n;j++) {
if((j+1)==n)
continue;
if(a[j]>a[j+1]){
flag=1;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}while(flag==1);
nashieRbaba
Newbie Poster
Offline
1 posts
since Jun 2007
Page 1
2
3
Next
This thread is more than three months old
No one has posted to this discussion for at least three
months. Please let old threads die and do not
reply to them unless you feel you have something new and valuable to contribute that absolutely
must be added to make the discussion complete. Otherwise, please start a
new thread
in this forum
instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Computer Science Forum Timeline:
Is there a Poisson RNG that is seed

less?
Next Thread in Computer Science Forum Timeline:
operating systems
Tag Cloud for Computer Science
#java
#swing
0
agile
ai
algebra
algorithm
algorithms
analysis
application
applications
bi

cubic
big

o
binary
boolean
c++
certifcates
change
code
college
complexity
compress
computation
computer
comput
er

science
concept
cryptography
cut
database
databasesearch
de
adlock
decrypt
design
detect
developer
developm
ent
digital
download
easy
expressions
feature
final
find
gameshow
guidelines
hack
hash
helpwithhomework
hexadecimal
ibm
ims
how
intelligence
interpolation
jeopardy
kalman
language
ll
machine
mapping
matlab
mesh
mobile
multimedia
natural
notation
num
bers
opinion
optimization
paragraph
parse
parsing
password
physics
print
processing
programming
project
puzzle
python
readablity
regular
replace
representation
resourceallocator
robotics
roboticsprojects
science
scope
scoping
softwar
e
supercomputer
system
test
thesis
vb
visual
watson
website
words
year
About Us

Contact Us

Advertise

Acceptable Use Policy
Forum Index

Build Custom RSS Feed
Follow us on
© 2010 DaniWeb® LLC
Gain an Advantage with SAP
Business Intelligence and Software Solutions.
www.SAPRunBetter.com
You have to know

the algorithm

the shortest path through the algorithm

the longest path through the algorithm

how
the path lengths or numbers of iterations vary with the number of data items processed

what an "average" data set looks like.
The time complexity of the best case can be calculated by considering what happens to the
execution time or number of iteratio
ns when you increase the dataset by 1 item, by 2 items, by a
factor of 2, a factor of 3. If the execution time increase is linear with the number of data items,
we say the time complexity is O(N), that is, on the order of N, the number of data items. If th
e
execution time varies as the square of the number of data items, then we say the time
complexity is O(N
2
). The time complexity can be of other orders, exponential, or have some
other
relationship
to the
number of data items, for example, O(N
3/2
).
The same assessment of time complexity can be done for the worst and average cases.
Oddman
Great Answer
Report
Ads by Google
Reunite With Your Friends
www.
google.com/Orkut
Find All Your Old Friends & Get In
Touch Instantly. Join Orkut Today!
Experienced C engi
neer?
www.slideshare.net
Deep back

end experience needed.
Hottest web startup in Delhi
AS PER 1ST algorit
hm,
inside teh first while loop

>
we need to find out , what all basic
operation
which takes constant amount of time.
Here
k
←
0

is one basic
operation
k < n*n

assume , n*n = N ( any way it will be one number) one basic opertion
y
←
y + j

k

anotherbasic operation
k
←
k + 1

is one more basic operation
j
←
j

1

last basic operation inside 1st while loop
Now consider the time,
1+ N + N

1 + N

1 + 1 = 3N
inside while loop , takes 3N ( N is just a big number if we assume a big value of n)
Y
←
0
j
←
n
while j >= 1 do {
k
←
0
while
k < n*n do {
y
←
y + j

k
k
←
k + 1
}
j
←
j

1
}
Y
←
0
j
←
n
now take 1st algorithm as a whole,
now for n value 1 , the total alogorithm will take 3N times,
now for n value 2, the total algorithm will take 2*3N times
so on..............
For value
n
n * 3N =
3 N2 (three N square) so finally we can say, the oerder of this algorithm is n
square.
Anonymous
Great Answer
Report
Happy new year and peace to you all! Can you please explain to me what is the execution time
T(n) of the 3 algorithms?
Algorithm 1:
Y
←
0
j
←
n
while j >= 1 do {
k
←
0
whi
le k < n*n do {
y
←
y + j

k
k
←
k + 1
}
j
←
j

1
}
return y
Algorithm 2:
For i
←
1 to n do
for j
←
1 to i do
for k
←
j to i+j do
a
←
a + 1
end for
end for
end for
Algorithm 3:
For i
←
1 to m do
for j
←
1 to i2 do
for k
←
1 to j do
b
←
b + 1
end for
end for
end for
Anonymous
Great Answer
Report
@Oddman, you mean if only n is not divisible by 2 .. The loop will go for ever .. =)
Anonymous
Great Answer
Report
X=1;
i=1;
while(i
Anonymous
Great Answer
Report
For(i=0;i
Anonymous
Great Answer
Report
Enter the password to open this PDF file:
File name:

File size:

Title:

Author:

Subject:

Keywords:

Creation Date:

Modification Date:

Creator:

PDF Producer:

PDF Version:

Page Count:

Preparing document for printing…
0%
Comments 0
Log in to post a comment