11
$\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 = ? $

  • 0
    @omgzor: Knowing that the algorithm has time in $O(n!)$ is not enough to calculate $n$. It depends greatly on information ignored by big-O notation. For example, $f(x) = 1000 \cdot n!$ and $g(x) = 0.000001 \cdot n!$ are both in $O(n!)$. However $f(x) = 1000$ has a solution that differs by a huge amount from that of $g(x) = 1000$, and these examples can be exaggerated: you can't even make a close guess if you don't know the multiplicative constant. You need to know more about your function, even for an approximate solution, if I'm understanding the question properly.2011-03-18

2 Answers 2

6

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.

  • 0
    Thanks, I'll do a binary search to solve it as you propose.2011-03-18
1

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?

  • 0
    It's a theoretical exercise.2011-03-18