2
$\begingroup$

I'm having a difficult time understanding time complexities of algorithm. I know that a polynomial time is of the form O(n^m) where m is a constant. Consider the following case where A is a list of elements:

Foo(A)
for each element a in A:
 for each element b in A-a:
 Polynomial-Time-Algorithm-Bar(A, a, b)

If n is the size of A,

1) Am I right in understanding that Foo(A) is also a polynomial time algorithm , because the function Polynomial-Time-Algorithm-Bar is called (n-1)*n times ?

2) When would an algorithm making calls to another polynomial time algorithm become exponential. I understand that if it has a running time of O(n^m) and m is variable and n is constant, then it would be exponential. Can you give me an example of such a case in the above example?

Raphael
73.3k31 gold badges184 silver badges406 bronze badges
asked Oct 26, 2016 at 23:39
$\endgroup$
0

1 Answer 1

3
$\begingroup$

I assume $n$ is the number of entries in array/set A.

  1. Yes, since polynomials are closed against multiplication; specifically, $n(n-1) \cdot p(n)$ is always polynomially bounded as long as $p(n)$ is polynomially bounded.

  2. As a consequence, the only way to achieve exponential running-time by calling a polynomial-time algorithm with parts of the original input is to call it exponentially often.

    Example:

    Foo(A)
     if |A| >= 2
     a = A[0]
     b = A[1]
     Polynomial-Time-Algorithm-Bar(A, a, b)
     Foo(A - a)
     Foo(A - b)
    

    The number of calls to PTA-Bar is given by

    $\qquad\begin{align*} C(0) &= 0,\\ C(1) &= 0,\\ C(n) &= 2C(n-1) + 1, \qquad n \geq 2, \end{align*}$

    which solves to $C(n) = 2^{n-1} - 1$ for $n \geq 1$. Ergo, the running time of Foo is in $\Omega\bigl( 2^n \cdot p(n) \bigr)$ if PTA-Bar takes time $p(n)$.

answered Oct 27, 2016 at 0:06
$\endgroup$
4
  • $\begingroup$ Calling it on inputs of size 2^(n^(Ω(1))) is another "way to achieve exponential running-time by calling a polynomial-time algorithm". ​ ​ $\endgroup$ Commented Oct 27, 2016 at 2:12
  • $\begingroup$ @RickyDemer Add an answer? $\endgroup$ Commented Oct 27, 2016 at 9:34
  • $\begingroup$ @RickyDemer So it doesn't have to be a recursive algorithm to be an exponential? It would be great if you can elaborate with an answer. $\endgroup$ Commented Oct 27, 2016 at 18:16
  • $\begingroup$ @MadhavanRP You can always replace recursion with loops; I just found this example approachable and realistic. (Consider, for instance, for i = 1 .. 2^n do PTA-Bar(...) end -- trivial.) $\endgroup$ Commented Oct 27, 2016 at 18:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.