0

Algorithm solves a problem of size 𝑛 as follows: recursively solves 3 subproblems of size 𝑛 - 2, and then constructs the answer for the original problem in time 𝑂(1).

It seems obvious that the Master theorem cannot be applied here. So, I thought of drawing a recursion tree, but what bothers me is that do I need to consider two cases: when n is odd/even? Or the result sum and hence the running time wouldn't depend on this at all? Thanks in advance.

2
  • Hint: can you solve this if the subproblems are of size n-1? How does it change if the subproblems have size n-2? Commented Oct 13, 2020 at 17:30
  • @Henry I suppose that on every new level of tree we need to subtract 2 more from n (T(n-2), T(n-4), T(n-6), etc). And the sum limit changes also Commented Oct 13, 2020 at 18:35

2 Answers 2

2

We'll have to assume that the base case of the recursion occurs when 𝑛 is either 0 or 1. It could also be that the base case occurs when 𝑛 is either 1 or 2, 𝑛 is not allowed to be 0. We don't really know, but it is not that relevant.

In the first case, the number of operations for a given 𝑛 that is odd, is the same as the number of operations for 𝑛−1. In the second case it would be the same number of operations as for 𝑛+1.

So, for determining the asymptotic complexity we can look at just the even numbers (or only the odd numbers).

The recursion tree is a perfect 3-ary tree with a height of (𝑛+1)/2 or (𝑛+2)/2 (again: depending on the base case).

In a perfect 3-ary tree of height h, we have (3h-1)/2 nodes, so that is O(3h), which in terms of 𝑛 is O(3𝑛/2) = O((√3)𝑛).

Sign up to request clarification or add additional context in comments.

3 Comments

Still don't exactly understand why we can look at just even or odd numbers (not both of them)
Asymptotic means looking at the trend for increasing n. Two consecutive n will have same tree size. Then next increases, then equal again. Etc......
Did this answer your question?
0

Another (possibly simpler) solution in addition to the one proposed by @trincot :

T(n)=3T(n-2)+O(1)=...=3k(T(n-2k)+O(1)).

Let's find out the stopping condition for this recurrence relation, i.e. need to find k for which the following holds: n-2k=0 --> k=n/2 --> T(n)=O(3n/2)=O(√3n)

Comments

Your Answer

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.