Time complexity of nested while with changing condition
up vote
2
down vote
favorite
I'm trying to solve the complexity of this loop
for(int i= 0; i < n; i++) {
c = i;
while(c > 1){
O(1);
c = c / 2;
}
}
as the while condition changes in every loop I don't know how to calculate that strange series.
I mean, if the loop where
for(int i= 0; i < n; i++) {
c = n;
while(c > 1){
O(1);
c = c / 2;
}
}
I know the while has a complexity of O(logn) and it repeats itself n times, so the complexity would be O(nlogn).
The problem I have with previous loop is "c=i". As c=i, first time (c=0) the loop would reproduce 0 times, when c=1 it would reproduce 0 times again, when c=2 it would reproduce 1 time, then the series would follow and it is 0, 0, 1, 2, 2, 3, 3... (while reproductions each time of for loop)
O(logn) would not repeat itself n times, would repeat a number of times I can't come up with, so I don't know how to solve it.
algorithm time-complexity
add a comment |
up vote
2
down vote
favorite
I'm trying to solve the complexity of this loop
for(int i= 0; i < n; i++) {
c = i;
while(c > 1){
O(1);
c = c / 2;
}
}
as the while condition changes in every loop I don't know how to calculate that strange series.
I mean, if the loop where
for(int i= 0; i < n; i++) {
c = n;
while(c > 1){
O(1);
c = c / 2;
}
}
I know the while has a complexity of O(logn) and it repeats itself n times, so the complexity would be O(nlogn).
The problem I have with previous loop is "c=i". As c=i, first time (c=0) the loop would reproduce 0 times, when c=1 it would reproduce 0 times again, when c=2 it would reproduce 1 time, then the series would follow and it is 0, 0, 1, 2, 2, 3, 3... (while reproductions each time of for loop)
O(logn) would not repeat itself n times, would repeat a number of times I can't come up with, so I don't know how to solve it.
algorithm time-complexity
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm trying to solve the complexity of this loop
for(int i= 0; i < n; i++) {
c = i;
while(c > 1){
O(1);
c = c / 2;
}
}
as the while condition changes in every loop I don't know how to calculate that strange series.
I mean, if the loop where
for(int i= 0; i < n; i++) {
c = n;
while(c > 1){
O(1);
c = c / 2;
}
}
I know the while has a complexity of O(logn) and it repeats itself n times, so the complexity would be O(nlogn).
The problem I have with previous loop is "c=i". As c=i, first time (c=0) the loop would reproduce 0 times, when c=1 it would reproduce 0 times again, when c=2 it would reproduce 1 time, then the series would follow and it is 0, 0, 1, 2, 2, 3, 3... (while reproductions each time of for loop)
O(logn) would not repeat itself n times, would repeat a number of times I can't come up with, so I don't know how to solve it.
algorithm time-complexity
I'm trying to solve the complexity of this loop
for(int i= 0; i < n; i++) {
c = i;
while(c > 1){
O(1);
c = c / 2;
}
}
as the while condition changes in every loop I don't know how to calculate that strange series.
I mean, if the loop where
for(int i= 0; i < n; i++) {
c = n;
while(c > 1){
O(1);
c = c / 2;
}
}
I know the while has a complexity of O(logn) and it repeats itself n times, so the complexity would be O(nlogn).
The problem I have with previous loop is "c=i". As c=i, first time (c=0) the loop would reproduce 0 times, when c=1 it would reproduce 0 times again, when c=2 it would reproduce 1 time, then the series would follow and it is 0, 0, 1, 2, 2, 3, 3... (while reproductions each time of for loop)
O(logn) would not repeat itself n times, would repeat a number of times I can't come up with, so I don't know how to solve it.
algorithm time-complexity
algorithm time-complexity
asked Nov 7 at 12:13
Dgrm
386
386
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
This need a bit of math involved.Given that log is well defined for a and b:
log(a) + log(b) = log(ab)
Here you have
log(1) + log(2) +....+ log(n) = log(1*....*n) = log(n!)
There is a mathematical approximation for log(n!), namely
log(n!) ~ nlog(n) - n + 1
which reveal O(log(n!)= O(nlog(n))
why the complexity is "log(1) + log(2) +....+ log(n)"? shouldn't it be "log(1) + log(2) +....+ log(n-1)" as 'for' condition is i<n?
– Dgrm
Nov 7 at 18:33
@Dgrm what's the difference. I can use the convention j = i+1. j goes from 1 to n while i goes from 0 to n-1
– UmNyobe
Nov 8 at 10:21
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
This need a bit of math involved.Given that log is well defined for a and b:
log(a) + log(b) = log(ab)
Here you have
log(1) + log(2) +....+ log(n) = log(1*....*n) = log(n!)
There is a mathematical approximation for log(n!), namely
log(n!) ~ nlog(n) - n + 1
which reveal O(log(n!)= O(nlog(n))
why the complexity is "log(1) + log(2) +....+ log(n)"? shouldn't it be "log(1) + log(2) +....+ log(n-1)" as 'for' condition is i<n?
– Dgrm
Nov 7 at 18:33
@Dgrm what's the difference. I can use the convention j = i+1. j goes from 1 to n while i goes from 0 to n-1
– UmNyobe
Nov 8 at 10:21
add a comment |
up vote
4
down vote
accepted
This need a bit of math involved.Given that log is well defined for a and b:
log(a) + log(b) = log(ab)
Here you have
log(1) + log(2) +....+ log(n) = log(1*....*n) = log(n!)
There is a mathematical approximation for log(n!), namely
log(n!) ~ nlog(n) - n + 1
which reveal O(log(n!)= O(nlog(n))
why the complexity is "log(1) + log(2) +....+ log(n)"? shouldn't it be "log(1) + log(2) +....+ log(n-1)" as 'for' condition is i<n?
– Dgrm
Nov 7 at 18:33
@Dgrm what's the difference. I can use the convention j = i+1. j goes from 1 to n while i goes from 0 to n-1
– UmNyobe
Nov 8 at 10:21
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
This need a bit of math involved.Given that log is well defined for a and b:
log(a) + log(b) = log(ab)
Here you have
log(1) + log(2) +....+ log(n) = log(1*....*n) = log(n!)
There is a mathematical approximation for log(n!), namely
log(n!) ~ nlog(n) - n + 1
which reveal O(log(n!)= O(nlog(n))
This need a bit of math involved.Given that log is well defined for a and b:
log(a) + log(b) = log(ab)
Here you have
log(1) + log(2) +....+ log(n) = log(1*....*n) = log(n!)
There is a mathematical approximation for log(n!), namely
log(n!) ~ nlog(n) - n + 1
which reveal O(log(n!)= O(nlog(n))
answered Nov 7 at 12:23
UmNyobe
17.5k73775
17.5k73775
why the complexity is "log(1) + log(2) +....+ log(n)"? shouldn't it be "log(1) + log(2) +....+ log(n-1)" as 'for' condition is i<n?
– Dgrm
Nov 7 at 18:33
@Dgrm what's the difference. I can use the convention j = i+1. j goes from 1 to n while i goes from 0 to n-1
– UmNyobe
Nov 8 at 10:21
add a comment |
why the complexity is "log(1) + log(2) +....+ log(n)"? shouldn't it be "log(1) + log(2) +....+ log(n-1)" as 'for' condition is i<n?
– Dgrm
Nov 7 at 18:33
@Dgrm what's the difference. I can use the convention j = i+1. j goes from 1 to n while i goes from 0 to n-1
– UmNyobe
Nov 8 at 10:21
why the complexity is "log(1) + log(2) +....+ log(n)"? shouldn't it be "log(1) + log(2) +....+ log(n-1)" as 'for' condition is i<n?
– Dgrm
Nov 7 at 18:33
why the complexity is "log(1) + log(2) +....+ log(n)"? shouldn't it be "log(1) + log(2) +....+ log(n-1)" as 'for' condition is i<n?
– Dgrm
Nov 7 at 18:33
@Dgrm what's the difference. I can use the convention j = i+1. j goes from 1 to n while i goes from 0 to n-1
– UmNyobe
Nov 8 at 10:21
@Dgrm what's the difference. I can use the convention j = i+1. j goes from 1 to n while i goes from 0 to n-1
– UmNyobe
Nov 8 at 10:21
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189257%2ftime-complexity-of-nested-while-with-changing-condition%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown