I have tried determining the running time given by a recurrence relation, but my result is not correct.
Recurrence
T(n) = c + T(n-1) if n >= 1
= d if n = 0
My attempt
I constructed this recursion tree:
n
|
n-1
|
n-2
|
n-3
|
n-4
|
n-5
|
|
|
|
|
|
Till we get 1
Now at level i, the size of the sub problem should be, n-i
But at last we want a problem of size 1. Thus, at the last level, n-i=1 which gives, i=n-1.
So the depth of the tree becomes n-1 and the height becomes n-1+1= n.
Now the time required to solve this recursion = height of the tree*time required at each level which is :
n+(n-1)+(n-2)+(n-3)+(n-4)+(n-5)+ ...
==> (n+n+n+n+n+ ... )-(1+2+3+4+5+ ... )
==> n - (n(n+1)/2)
Now the time taken = n* ((n-n2)/2) which should give the order to be n2, but that is not the correct answer.
Yes, that is correct. But you’re assuming, that the runtime equals the sum of all the subproblem sizes. Just think about it, already summing the first two levels gives
n + (n - 1) = 2n - 1, why would the problem size increase? Disclaimer: A bit handwavy and not an entirely accurate statement.What the formula actually says
The formula says, solving it for some
ntakes the same time it takes to solve it for a problem size that is one less, plus an additional constantc:c + T(n - 1)Another way to put the above statement is this: Given the problem takes some time
tfor a certain problem size, it will taket + cfor a problem size, that is bigger by one.We know, that at a problem size of
n = 0, this takes timed. According to the second statement, for a size of one more,n = 1, it will taked + c. Applying our rule again, it thus takesd + c + cforn = 2. We conclude, that it must taked + n*ctime for anyn.This is not a proof. To actually prove this, you must use induction as shown by amit.
A correct recursion tree
Your recursion tree only lists the problem size. That’s pretty much useless, I’m afraid. Instead, you need to list the runtime for said problem size.
Every node in the tree corresponds to a certain problem size. What you write into that node is the additional time it takes for the problem size. I.e. you sum over all the descendants of a node plus the node itself to get the runtime for a certain problem size.
A graphical representation of such a tree would look like this
Formalizing: As already mentioned, the label of a node is the additional runtime it takes to solve for that problem size, plus all its descendants. The uppermost node represents a problem size of
n, bearing the labelcbecause that’s in addition toT(n-1), to which it is connected using a|.In a formula, you would only write this relation:
T(n) = c + T(n-1). Given that tree, you can see how this applies to everyn>=1. You could write this down like this:You can now expand the terms from bottom to top:
Summing
1 to nFrom the first line to the second line, you have reduced your problem from summing
1 to nto, well, summing1 to n-1. That’s not very helpful, because you’re stuck with the same problem.I’m not sure what you did on the third line, but your transition from the first to the second is basically correct.
This would have been the correct formula: