1

I was reading a time complexity calculation related question on SO but I can't comment there (not enough reps). What's the time complexity of this algorithm for Palindrome Partitioning?

I have a question regarding going from 1st to 2nd equation here:

Now you can write the same expression for H(n-1), then substitute back to simplify:

H(n) = 2 H(n-1) + O(n) =========> Eq.1

And this solves to

H(n) = O(n * 2^n) =========> Eq.2

Can someone illustrate how he got Eq.2 from Eq.1? Thank you.

Community
  • 1
  • 1
DanSoren
  • 173
  • 2
  • 9

3 Answers3

1

Eq 1. is a recurrence relation. See the link for a tutorial on how to solve these types of equations, but we can solve via expansion as below:

H(n) = 2H(n-1) + O(n)
H(n) = 2*2H(n-2) + 2O(n-1) + O(n)
H(n) = 2*2*2H(n-3) + 2*2O(n-2) + 2O(n-1) + O(n)
...
H(n) = 2^n*H(1) + 2^(n-1)*O(1) + ... + 2O(n-1) + O(n)

since H(1) = O(n) (see the original question)
H(n) = 2^n*O(n) + 2^(n-1)*O(1) + ... + 2O(n-1) + O(n)
H(n) = O(n * 2^n)
elhefe
  • 3,404
  • 3
  • 31
  • 45
  • This is incorrect. The correct solution is `O(2^n)` without `n` – Salvador Dali Apr 03 '16 at 00:51
  • @SalvadorDali - I just looked over the derivation again and it looks ok to me. If you can point out where I made a mistake I'll be happy to update the answer. – elhefe Apr 03 '16 at 00:57
  • 2
    @SalvadorDali: I think I know your mistake. Do not try to verify the solution by "plugging back" the O() terms, as this makes no sense. The equation must be first solved *exactly*, with O() term replaced by symbols; *this* is the solution that you can verify bu plugging back. In this case, solve H(n)=2H(n-1)+K, H(1)=M; the solution is H(n)=(M+K)×2^n-K. Plugging H(n-1)=(M+K)×2^(n-1)-K into the original formula, obtain H(n) = 2((M+K)×2^(n-1)-K)+K = (M+K)×2^n-2K+K = (M+K)×2^n-K = H(n), so the solution is right. Only *now* you can plug M=O(n) and K=O(n), and that yields H(n)=O(n×2^n). Makes sense? – kkm inactive - support strike Apr 03 '16 at 01:09
  • Please look at my answer. I highly doubt that I have any mistake at all. – Salvador Dali Apr 03 '16 at 01:11
1

We need to homogenize the equation, in this simple case just by adding a constant to each side. First, designate O(n) = K to avoid ealing with the O notation at this stage:

H(n) = 2 H(n-1) + K

Then add a K to each side:

H(n) + K = 2 (H(n-1) + K)

Let G(n) = H(n) + K, then

G(n) = 2 G(n-1)

This is a well-known homogeneous 1-st order recurrence, with the solution

G(n) = G(0)×2n = G(1)×2n-1

Since H(1) = O(n), G(1) = H(1) + K = O(n) + O(n) = O(n),

G(n) = O(n)×2n-1 = O(n×2n-1) = O(n×2n)

and

H(n) = G(n) - K = O(n×2n) - O(n) = O(n×2n)

1

They are wrong.

Let's assume that O refers to a tight bound and substitute O(n) with c * n for some constant c. Unrolling the recursion you will get:

enter image description here

When you finish to unroll recursion n = i and b = T(0).

Now finding the sum:

enter image description here

Summing up you will get:

enter image description here

So now it is clear that T(n) is O(2^n) without any n


For people who are still skeptical about the math:

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • @elhefe added explanation. – Salvador Dali Apr 03 '16 at 01:15
  • 1
    Since in the linked question, H(1) = O(n), and you've changed H to T, b=T(1)=H(1)=O(n). So you actually have O(n)2^n + c(...) = O(n*2^n) – elhefe Apr 03 '16 at 01:17
  • @elhefe This does not make any sense. How can a function that does not depend on `n` be `O(n)`? How can F(1)` or `F(0)` or `F(15)` be `O(n)`? – Salvador Dali Apr 03 '16 at 01:21
  • @elhefe is correct: your solution is essentially same as their, only you missed the last step of solving for the _b_. – kkm inactive - support strike Apr 03 '16 at 01:21
  • @SalvadorDali I'd suggest reading the linked question and asking your question there... I just solved the equations I was given. – elhefe Apr 03 '16 at 01:21
  • @elhefe and I just say that this does not make any sense. You can't say: here is the function which does not depend on `n` and it is `O(n)` (basically depends on `n`) – Salvador Dali Apr 03 '16 at 01:22
  • 1
    We are not computing the complexity of the algorithm H(n)! H(n) is already a complexity metric. It might have been more proper to be speaking of O(h(n)), and H(n) is just a convenient shortcut for it. This is why I am warning against introducing the big Os into the solution prematurely, as @elhefe did: you can verify your solution in h(n) but not in H(n). Does that finally make sense? – kkm inactive - support strike Apr 03 '16 at 01:33