solving recurrence T(n) = T(n/2) + T(n/2 - 1) + n/2 + 2 - big-o

Need some help on solving this runtime recurrence, using Big-Oh:
T(n) = T(n/2) + T(n/2 - 1) + n/2 + 2
I don't quite get how to use the Master Theorem here

For n big enough you can assume T(n/2 - 1) == T(n/2), so you can change
T(n) = T(n/2) + T(n/2 - 1) + n/2 + 2
into
T(n) = 2*T(n/2) + n/2 + 2
And use Master Theorem (http://en.wikipedia.org/wiki/Master_theorem) for
T(n) = a*T(n/b) + f(n)
a = 2
b = 2
f(n) = n/2 + 2
c = 1
k = 0
log(a, b) = 1 = c
and so you have (case 2, since log(a, b) = c)
T(n) = O(n**c * log(n)**(k + 1))
T(n) = O(n * log(n))

Related

The Recurrence T(n)= 2T(n-1) + (2^n)

Can someone please help me with this ?
Use iteration method to solve it. T(n)= 2T(n-1) + (2^n) , T(0) = 1
Explanation of steps would be greatly appreciated.
I tried to solve the recursion as follows
T(n)= 2T(n-1) + 2^n
T(n-1) = 2T(n-2) + 2^(n-1)
T(n-2) = 2T(n-3) + 2^(n-2)
T(n-3) = 2T(n-4) + 2^(n-3)
...
T(0) = 1
Then:
T(n) = 2^k * T(n-k) + ... Here's where I get stuck.
Well, let's compute some values for small n:
T(0) = 1
T(1) = 4
T(2) = 12
T(3) = 32
T(4) = 80
T(5) = 192
the function seems to be exponetial; we have 2^n term that's why let's check if
T(n) = f(n) * 2^n
where f(n) is some unknown function. If we divide by 2^n we have f(n) = T(n) / 2^n
T(0) / 2^0 = 1
T(1) / 2^1 = 2
T(2) / 2^2 = 3
T(3) / 2^3 = 4
Looks quite clear that f(n) = n + 1 and
T(n) = (n + 1) * 2^n
Now let's prove it by induction.
Base: it holds for n = 0: (0 + 1) * 2^0 = 1
Step: from T(n - 1) = n * 2^(n - 1) we have
T(n) = 2 * T(n - 1) + 2^n =
= 2 * n * 2^(n - 1) + 2^n =
= n * 2^n + 2^n =
= (n + 1) * 2^n
So, if T(n - 1) holds, T(n) holds as well.
Q.E.D.
Closed formula for
T(n) = 2T(n-1) + (2^n)
T(0) = 1
Is
T(n) = (n + 1) * 2^n
Cheating way: try oeis (on-line encyclopedia of integer sequences) and you'll find A001787

Solving recurrence relations where they cannot be easily put in MT form

I have the following recurrence relation:
T(n) = T(n/2) + T(n/2) + n
In this instance, I believe the solution is found by:
T(n) = T(n/2) + T(n/2) + n = 2T(n/2) + n
Here, the Master Theorem can be applied:
a = b = 2, f(n) = n
n^(log2(2)) vs n --> n vs n
Thus, the solution would be:
Theta(n log n)
How would I find the solution for the following situations:
T(n) = T(n/2) + T(n/4) + n
T(n) = T(n/2) + T(n/3) + n^2
Those don't look like they can be easily put into MT form, so I am not quite sure what to do.
Those are of a form that can be handled by Akra--Bazzi: https://en.m.wikipedia.org/wiki/Akra%E2%80%93Bazzi_method
Since T(n) = T(n/2) + T(n/4) + n satisfies the criteria of Akra-Bazzi method[see CRLS, pages 112 - 113 ], than you can use that method to solve the recurrence.
To solve the recurrence, we first need to find the unique real number p such that
(1/2)^p + (1/4)^p = 1
<=> (1/2)^p + (1/2^2)^p = 1
<=> (1/2)^p + (1/2)^2p = 1
<=> (1/2)^p * [1 + (1/2)^p] = 1
Let x = (1/2)^p, then we have an equation of the following form
x * (1 + x) = 1
<=> x + x^2 = 1
<=> x^2 + x - 1 = 0
=> x = (-1 + sqrt(5)) / 2 -> x = 1
=> (1/2)^p = 1
<=> (1/2)^p = (1/2)^0
<=> p = 0
The solution to the recurrence is then
T(n) = 𝚯(x^p * (1 + integral-from-1-to-n-(u/u^(p+1))du))
<=> T(n) = 𝚯(x^0 * (1 + integral-from-1-to-n-(u/u^(0+1))du))
<=> T(n) = 𝚯(1 * (1 + integral-from-1-to-n-(1)du))
<=> T(n) = 𝚯(1 * (1 + (n - 1)))
<=> T(n) = 𝚯(1 * (n))
<=> T(n) = 𝚯(n)
The same way can you solve T(n) = T(n/2) + T(n/3) + n^2

solving this relation using the Recurrence method?

I don't know how to continue in this recurrence cause I don't see any pattern, any help??
T(n) = 2n + T(n/2)
= 3n + T(n/4)
= 7n/2 + T(n/8)
= 15n/4 + T(n/16)
and so on...
As I understand it's just simple reccurence.
T(n) = 2n + T(n/2)
Your notation can makes someone think different. For me it should be:
T(n) = 2n + T(n/2) ....(1)
T(n/2) = 2(n/2) + T(n/2/2) = n + T(n/4)
T(n) = 2n + n + T(n/4) = 3n + T(n/4) ....(2)
T(n/4) = 2(n/4) + T(n/4/2) = n/2 + T(n/8)
T(n) = 2n + n + n/2 + T(n/8) = 7n/2 + T(n/8) ....(3)
T(n/8) = 2(n/8) + T(n/8/2) = n/4 + T(n/16)
T(n) = 2n + n + n/2 + n/4 + T(n/16) = 15n/4 + T(n/16) ....(4)
T(n/16) = 2(n/16) + T(n/16/2) = n/8 + T(n/32)
T(n) = 15n/4 + n/8 + T(n/32) = 31n/4 + T(n/32) ....(5)
and so on...
This is a usual recurrence relation - if you are a CS student, you will soon know the result by heart.
If you want to find the result by hand, make a geometric sum appears from the recurrence:
T(n) = 2n + n + n/2 + ... + n/2^(k+1) + T(0)
= 2n(1 + 1/2+ ... + 1/2^(k+2)) + T(0)
Where k = INT(log2(n))
You can see a geometric sum of general term 1/2 appears
1 + 1/2 + ... + 1/2^(k+2) = (1 - 1/2^(k+3)) / (1 - 1/2)
Observing that 2^(k+2) = 8 * 2^(log2(n)) = 8n and simplifying
T(n) = 4n + T(0) - 1/2 = Theta(4n)
In addition to the expanding series till T(0) way shown by Alexandre Dupriez, You can also apply master theorem to solve it
For the recurrence equation
T(n) = 2n + T(n/2)
Master Theorem:
For recurrences of form,
T(n) = a T(n/b) + f(n)
where a >= 1 and b > 1
If f(n) is O(nc) then
If c < logba, then T(n) = O(n * logba)
If c = logba, then T(n) = O(nc * log n)
if c > logba, then T(n) = O(nc)
we have a = 1, b = 2, c = 1 and c > logba (case 3)[as c = 1 and log 1 (any base) = 0]
Therefore, T(n) = O (n1)
T(n) = O (n)

Solving the similar recurrence: T(n) = 3T(n/3) + n/3

Given..
T(0) = 3 for n <= 1
T(n) = 3T(n/3) + n/3 for n > 1
So the answer's suppose to be O(nlogn).. Here's how I did it and it's not giving me the right answer:
T(n) = 3T(n/3) + n/3
T(n/3) = 3T(n/3^2) + n/3^2
Subbing this into T(n) gives..
T(n) = 3(3T(n/3^2) + n/3^2) + n/3
T(n/3^2) = 3(3(3T(n/3^3) + n/3^3) + n/3^2) + n/3
Eventually it'll look like..
T(n) = 3^k (T(n/3^k)) + cn/3^k
Setting k = lgn..
T(n) = 3^lgn * (T(n/3^lgn)) + cn/3^lgn
T(n) = n * T(0) + c
T(n) = 3n + c
The answer's O(n) though..What is wrong with my steps?
T(n) = 3T(n/3) + n/3
T(n/3) = 3T(n/9) + n/9
T(n) = 3(3T(n/9) + n/9) + n/3
= 9T(n/9) + 2*n/3 //statement 1
T(n/9)= 3T(n/27) + n/27
T(n) = 9 (3T(n/27)+n/27) + 2*n/3 // replacing T(n/9) in statement 1
= 27 T (n/27) + 3*(n/3)
T(n) = 3^k* T(n/3^k) + k* (n/3) // eventually
replace k with log n to the base 3.
T(n) = n T(1) + (log n) (n/3);
// T(1) = 3
T(n) = 3*n + (log n) (n/3);
Hence , O (n* logn)
Eventually it'll look like.. T(n) = 3^k (T(n/3^k)) + cn/3^k
No. Eventually it'll look like
T(n) = 3^k * T(n/3^k) + k*n/3
You've opened the parenthesis inaccurately.
These types of problems are easily solved using the masters theorem. In your case a = b = 3, c = log3(3) = 1 and because n^c grows with the same rate as your f(n) = n/3, you fall in the second case.
Here you have your k=1 and therefore the answer is O(n log(n))
This question can be solved by Master Theorem:
In a recursion form :
where a>=1, b>1, k >=0 and p is a real number, then:
if a > bk, then
if a = bk
a.) if p >-1, then
b.) if p = -1, then
c.) if p < -1, then
3. if a < bk
a.) if p >=0, then
b.) if p<0, then T(n) = O(nk)
So, the above equation
T(n) = 3T(n/3) + n/3
a = 3, b = 3, k =1, p =0
so it fall into 2.a case, where a = bk
So answer will be
O(n⋅log(n))

Easy: Solve T(n)=T(n-1)+n by Iteration Method

Can someone please help me with this ?
Use iteration method to solve it. T(n) = T(n-1) +n
Explanation of steps would be greatly appreciated.
T(n) = T(n-1) + n
T(n-1) = T(n-2) + n-1
T(n-2) = T(n-3) + n-2
and so on you can substitute the value of T(n-1) and T(n-2) in T(n) to get a general idea of the pattern.
T(n) = T(n-2) + n-1 + n
T(n) = T(n-3) + n-2 + n-1 + n
.
.
.
T(n) = T(n-k) + kn - k(k-1)/2 ...(1)
For base case:
n - k = 1 so we can get T(1)
=> k = n - 1
substitute in (1)
T(n) = T(1) + (n-1)n - (n-1)(n-2)/2
Which you can see is of Order n2 => O(n2).
Expand it!
T(n) = T(n-1) + n = T(n-2) + (n-1) + n = T(n-3) + (n-2) + (n-1) + n
and so on, until
T(n) = 1 + 2 + ... + n = n(n+1)/2 [= O(n^2)]
provided that T(1) = 1
In pseudo code using iteration:
function T(n) {
int result = 0;
for (i in 1 ... n) {
result = result + i;
}
return result;
}
Another solution:
T(n) = T(n-1) + n
= T(n-2) + n-1 + n
= T(n-3) + n-2 + n-1 + n
// we can now generalize to k
= T(n-k) + n-k+1 + n-k+2 + ... + n-1 + n
// since n-k = 1 so T(1) = 1
= 1 + 2 + ... + n //Here
= n(n-1)/2
= n^2/2 - n/2
// we take the dominating term which is n^2*1/2 therefor 1/2 = big O
= big O(n^2)
Easy Method:
T (n) = T (n - 1) + (n )-----------(1)
//now submit T(n-1)=t(n)
T(n-1)=T((n-1)-1)+((n-1))
T(n-1)=T(n-2)+n-1---------------(2)
now submit (2) in (1) you will get
i.e T(n)=[T(n-2)+n-1]+(n)
T(n)=T(n-2)+2n-1 //simplified--------------(3)
now, T(n-2)=t(n)
T(n-2)=T((n-2)-2)+[2(n-2)-1]
T(n-2)=T(n-4)+2n-5---------------(4)
now submit (4) in (2) you will get
i.e T(n)=[T(n-4)+2n-5]+(2n-1)
T(n)=T(n-4)+4n-6 //simplified
............
T(n)=T(n-k)+kn-6
**Based on General form T(n)=T(n-k)+k, **
now, assume n-k=1 we know T(1)=1
k=n-1
T(n)=T(n-(n-1))+(n-1)n-6
T(n)=T(1)+n^2-n-10
According to the complexity 6 is constant
So , Finally O(n^2)

Resources