Connecting Tech Pros Worldwide Help | Site Map

Time Complexity Question (Big Oh)

  #1  
Old October 5th, 2008, 05:46 AM
Newbie
 
Join Date: Oct 2008
Posts: 1
I have a problem for my homework where it asks me to:

Show that log(n!) = O(n log(n)). Do this by comparing n! with n^n.

I attempted to prove that n! is O(n), and I think I did that correctly, but I'm stuck here.

I generally don't understand Time Complexity. If you give me a couple loops, I can calculate the time complexity of the nested loops, etc. etc..

But on general principle questions like this one, I'm clueless... can someone help?

Thanks in advance,
Sam
  #2  
Old October 5th, 2008, 07:52 AM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: Time Complexity Question (Big Oh)


Have a look at Stirling's approximation for n!.

kind regards,

Jos
  #3  
Old October 5th, 2008, 05:25 PM
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,444

re: Time Complexity Question (Big Oh)


You can also use the fact that n! < n^n, and solve O(log(n^n)).
  #4  
Old October 5th, 2008, 05:51 PM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: Time Complexity Question (Big Oh)


Quote:
Originally Posted by Ganon11
You can also use the fact that n! < n^n, and solve O(log(n^n)).
It's extremely tricky to use an upper bound, e.g. n < c*n^2 so O(n) == O(n^2)
which obviously isn't true.

kind regards,

Jos
  #5  
Old October 5th, 2008, 08:53 PM
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,444

re: Time Complexity Question (Big Oh)


Well, O(n) != O(n^2), but you can guarantee that n will never grow faster than n^2, so isn't n still O(n^2)? Or maybe I'm confused as to what Big-Oh notation means.

For instance, since you know n! grows slower than n^n, an upper limit for n^n is STILL faster than n!, so it can serve as an upper limit unless a more accurate upper limit can easily be found.
  #6  
Old October 5th, 2008, 09:13 PM
Expert
 
Join Date: Sep 2007
Posts: 857

re: Time Complexity Question (Big Oh)


Quote:
Originally Posted by Ganon11
Well, O(n) != O(n^2), but you can guarantee that n will never grow faster than n^2, so isn't n still O(n^2)? Or maybe I'm confused as to what Big-Oh notation means.

For instance, since you know n! grows slower than n^n, an upper limit for n^n is STILL faster than n!, so it can serve as an upper limit unless a more accurate upper limit can easily be found.
All of this is correct. n < cn^2 for all n, so n is O(n^2).

In regards to the OP's question, if you show that n! < n^n, then you can take the log of both sides for log(n!) < nlog(n).
  #7  
Old October 6th, 2008, 08:11 AM
Lives Here
 
Join Date: Sep 2006
Posts: 12,085

re: Time Complexity Question (Big Oh)


None of you guys noticed that this is not really a Java question at all?

kind regards

r035198x(<--Non-mathematician surrounded by mathematicians)
  #8  
Old October 6th, 2008, 09:12 AM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: Time Complexity Question (Big Oh)


Quote:
Originally Posted by Ganon11
Well, O(n) != O(n^2), but you can guarantee that n will never grow faster than n^2, so isn't n still O(n^2)? Or maybe I'm confused as to what Big-Oh notation means.

For instance, since you know n! grows slower than n^n, an upper limit for n^n is STILL faster than n!, so it can serve as an upper limit unless a more accurate upper limit can easily be found.
I think you mean it the other way around, i,.e. n! < n^n and you can skip the
'easily' part; if you can find a function f(n) such that n! <= f(n) < n^n then
n^n doesn't make it for a big-Oh for n! (see my silly little example).

kind regards,

Jos
  #9  
Old October 6th, 2008, 02:16 PM
Expert
 
Join Date: Sep 2007
Posts: 857

re: Time Complexity Question (Big Oh)


Jos, just because there's a more accurate upper bound doesn't mean that the other upper bound isn't accurate. f(n)=n is still O(n^2) (and O(n^3) and O(n^4) and...) because n < n^2. By analogy, since n! < n^n (there's probably some interval where this isn't true, we're starting at the upper end of that interval). Even if there is a function g(n) that is in between them, n^n is still an upper bound on n!. It just isn't the least upper bound (or tightest fit, for a more big-Oh phrase) anymore.
  #10  
Old October 6th, 2008, 02:44 PM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: Time Complexity Question (Big Oh)


Quote:
Originally Posted by Laharl
Jos, just because there's a more accurate upper bound doesn't mean that the other upper bound isn't accurate. f(n)=n is still O(n^2) (and O(n^3) and O(n^4) and...) because n < n^2. By analogy, since n! < n^n (there's probably some interval where this isn't true, we're starting at the upper end of that interval). Even if there is a function g(n) that is in between them, n^n is still an upper bound on n!. It just isn't the least upper bound (or tightest fit, for a more big-Oh phrase) anymore.
Big-Oh wise speaking, we have to take the smallest function; otherwise the entire
thing would be moot:, e.g. searching through an unordered list can be done in
O(n^n), so it would be better to sort the thing first which takes O(n^42) and
binary search through the list which can be done in O(n*log(n!)). See the problem?

kind regards,

Jos
  #11  
Old October 6th, 2008, 03:08 PM
Newbie
 
Join Date: Oct 2008
Posts: 4

re: Time Complexity Question (Big Oh)


i am not very sure about this but i think that>>>

log(n!)=log(n)+log(n-1)+log(n-2)+......+log(1);

so you have n operations each calculating a logarithm
hence the order is n multiplied by something.

On the other hand

nlog(n)=log(n^n)

again you have n terms.
so maybe that's why the question requires you to compare n! with n^n.
  #12  
Old October 6th, 2008, 05:07 PM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: Time Complexity Question (Big Oh)


Quote:
Originally Posted by pdeb1234
i am not very sure about this but i think that>>>

log(n!)=log(n)+log(n-1)+log(n-2)+......+log(1);

so you have n operations each calculating a logarithm
hence the order is n multiplied by something.

On the other hand

nlog(n)=log(n^n)

again you have n terms.
so maybe that's why the question requires you to compare n! with n^n.
That reasoning is flawed; have a look at this:

2^n-1=2^(n-1)+2^(n-2)+2^(n-3)+ ... + 2^0

so there are n operations each calculating a power of two
but the order isn't n multiplied by something. It simply isn't true that if you have
an unkown function o(n) < f(n) (where f(n) is known) that the big-Oh of o(n) is O(f(n)).

kind regards,

Jos
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bidirectional iterators: signed distance? nottheartistinquestion@hotmail.com answers 9 September 6th, 2007 12:45 PM
reserving memory for an array vfunc@talktalk.net answers 49 September 25th, 2006 04:45 PM
Basic 'What can ASP do' question dgk answers 7 November 19th, 2005 07:47 AM
Comparing lists Odd-R. answers 41 October 18th, 2005 06:35 AM