Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factorial-time algorithms and P/NP

It's quite easy to see that n! grows slower than almost anything to the N power (say, 100^N) and so, if a problems is considered NP complete and one happened upon a n! algorithm that approximates the solution, one would do the Snoopy dance.

I have 2 questions about this situation:

  1. Would the n! algorithm be considered a solution in polynomial time? A factorial certainly doesn't appear to be a term raised to a power.
  2. If finding a n! solution means we have a decently fast algorithm and since n! grows faster than 2^N, then does this mean that some NP-complete problems do not need heuristic/approximation algorithms (except for obscure cases)?

Of course, these two questions rely on the first paragraph being true; if I've erred, please let me know.

like image 697
Zian Choy Avatar asked Jun 04 '11 05:06

Zian Choy


People also ask

What is P and NP in algorithm?

P is set of problems that can be solved by a deterministic Turing machine in Polynomial time. • NP is set of problems that can be solved by a Non-deterministic Turing Machine in Polynomial time.

What is the difference between P and NP problems?

P = the set of problems that are solvable in polynomial time by a Deterministic Turing Machine. NP = the set of decision problems (answer is either yes or no) that are solvable in nondeterministic polynomial time i.e can be solved in polynomial time by a Nondeterministic Turing Machine[4].

Is factorial considered polynomial time?

The result of n! has more than n bits, but the input only has log(n) bits, so just writing the result requires exponential time. This means that it cannot be computed in polynomial or non-deterministic polynomial time.


1 Answers

  1. No. factorial time is not polynomial time. Polynomial time normally means an equation of the form O(Nk), where N = number of items being processed, and k = some constant. The important part is that the exponent is a constant -- you're multiplying N by itself some number of that's fixed -- not dependent on N itself. A factorial-complexity algorithm means the number of multiplications is not fixed -- the number of multiplications itself grows with N.

  2. You seem to have the same problem here. N2 would be polynomial complexity. 2N would not be. Your basic precept is mistaken as well -- a factorial-complexity algorithm does not mean "we have a decently fast algorithm", at least as a general rule. If anything, the conclusion is rather the opposite: a factorial algorithm may be practical in a few special cases (i.e., where N is extremely small) but becomes impractical very quickly as N grows.

Let's try to put this in perspective. A binary search is O(log N). A linear search is O(N). In sorting, the "slow" algorithms are O(N2), and the "advanced" algorithms O(N lg N). A factorial-complexity is (obviously enough) O(N!).

Let's try to put some numbers to that, considering (for the moment) only 10 items. Each of these will be roughly how many times longer processing should take for 10 items instead of 1 item:

O(log N): 2
O(N):10
O(N log N): 23
O(N2): 100
O(N!): 3,628,800

For the moment I've cheated a bit, and use a natural logarithm instead of a base 2 logarithm, but we're only trying for ballpark estimates here (and the difference is a fairly small constant factor in any case).

As you can see, the growth rate for the factorial-complexity algorithm is much faster than for any of the others. If we extend it to 20 items, the difference becomes even more dramatic:

O(log N): 3
O(n): 20
O(N log N): 60
O(N2): 400
O(N!): 2,432,902,008,176,640,000

The growth rate for N! is so fast that they're pretty much guaranteed to be impractical except when the number of items involves is known to be quite small. For grins, let's assume that the basic operations for the processes above can each run in a single machine clock cycle. Just for the sake of argument (and to keep the calculations simple) let's assume a 10 GHz CPU. So, the base is that processing one item takes .1 ns. In that case, with 20 items:

O(log N) = .3 ns
O(N) = 2 ns
O(N log N) = 6 ns
O(N2) = 40 ns
O(N!) = 7.7 years.

like image 87
Jerry Coffin Avatar answered Oct 19 '22 15:10

Jerry Coffin