12
$\begingroup$

I'm dealing with a time-complexity problem in which I know the running time of an algorithm:

$$t = 1000 \mathrm{ms} .$$

I also know that the algorithm is upper bounded by $O(n!)$.

I want to know the approximate size of the input $n$ based on this:

$$ f(n) = n! = t $$ $$ f^{-1}(t) = n = ? $$

$\endgroup$
11
  • 3
    $\begingroup$ See mathoverflow.net/questions/12828/inverse-gamma-function $\endgroup$
    – Shai Covo
    Commented Mar 17, 2011 at 14:43
  • 1
    $\begingroup$ Do you know that the constant out in front of $n!$ is one? $\endgroup$
    – cardinal
    Commented Mar 17, 2011 at 14:52
  • 3
    $\begingroup$ Stirling's approximation, maybe? $\endgroup$ Commented Mar 17, 2011 at 15:00
  • $\begingroup$ You don't know it's one, that's why he says "approximate", presumably. $\endgroup$
    – leif
    Commented Mar 17, 2011 at 15:05
  • 2
    $\begingroup$ @leif, my point was that without the constant, you only know the size up to a scale factor. So, if the OP is looking for a bound (which is what I would consider more akin to "approximate"), then he needs to know the leading constant or have some handle on it. $\endgroup$
    – cardinal
    Commented Mar 17, 2011 at 16:52

2 Answers 2

7
$\begingroup$

You can use Stirling' approximation formula:

$$\log n! = n\log n - n + \log (2\pi n)/2 + \mathcal{O}(1/n)$$

So you can take the approximation as

$$\log n! \approx n\log n - n + \log (2\pi n)/2 $$

Now given $n! = c$, you can compute an approximate value for $n$ by doing a simple binary search on the above formula.

If you want a direct formula to compute and approximate value for $n$, Shai Covo has given you a link to a mathoverflow thread.

$\endgroup$
5
  • $\begingroup$ Thanks, but could you elaborate? I don't understand how I would apply a binary search on the formula or how the inverse Gamma function would be applied, I'm not familiar with it. $\endgroup$ Commented Mar 18, 2011 at 15:41
  • 1
    $\begingroup$ @omg: Start with an estimate of $n=1$ and keep doubling your estimate till you go over $\log(c)$. It is as if you have an infinite sorted array where the $n^{th}$ element is given by that formula, and you are searching for $\log(c)$ in that array. $\endgroup$
    – Aryabhata
    Commented Mar 18, 2011 at 15:46
  • $\begingroup$ btw, the log on this approximation formula is base 2 right? $\endgroup$ Commented Mar 18, 2011 at 15:50
  • $\begingroup$ @omg: No. It is natural log, base e. $\endgroup$
    – Aryabhata
    Commented Mar 18, 2011 at 15:50
  • $\begingroup$ Thanks, I'll do a binary search to solve it as you propose. $\endgroup$ Commented Mar 18, 2011 at 15:53
1
$\begingroup$

It also depends on the algorithm. You would need an algorithm that can translate to an "oblivious TM", a Turing Machine where the head movements depend only on the time step we are currently at. If your algorithm running time is different for different inputs of the same size, that is if you use loops that can be terminated abruptly (like when we search for one or more occurences of an item) or conditionals where each case has different running times (e.g. some heuristics), I believe approximating the input size is even more difficult.

I am however curious about your motive. Why isn't the input size known beforehand?

$\endgroup$
1
  • $\begingroup$ It's a theoretical exercise. $\endgroup$ Commented Mar 18, 2011 at 15:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .