Draw the recursion tree and add up its cost.
The cost of a recursion is the sum, over the levels of its tree, of the calls on a level times the work of one call.
Use it when a function calls itself, and you want to know how long it runs. Signal words: a function that calls itself twice, how many calls, draw the tree.
Draw: one node for each call, with the size on it. Mark the leaves.
Count: the calls on each level, by running the function with a counter on small inputs, or by the rule: calls = 1 + the calls of the children.
Add up: for each level, the calls times the work of one call. Add the levels.
Compare: the calls with the different questions, and the depth with the limit.
weight(n) it is about 1.6 to the power n. For a scan and two halves it is about n log n.weight(n), and about log n for the halving.def count_calls(n):
# [1] Base case: a call on 0 or 1 is a leaf, and it counts as one call.
if n < 2:
return 1
# [2] Recursive case: ask for the calls under n - 1 and under n - 2.
left = count_calls(n - 1)
right = count_calls(n - 2)
# [3] Total for this call: itself plus both subtrees.
return 1 + left + rightpublic int countCalls(int n) {
// [1] Base case: a call on 0 or 1 is a leaf, and it counts as one call.
if (n < 2) {
return 1;
}
// [2] Recursive case: ask for the calls under n - 1 and under n - 2.
int left = countCalls(n - 1);
int right = countCalls(n - 2);
// [3] Total for this call: itself plus both subtrees.
return 1 + left + right;
}int countCalls(int n) {
// [1] Base case: a call on 0 or 1 is a leaf, and it counts as one call.
if (n < 2) {
return 1;
}
// [2] Recursive case: ask for the calls under n - 1 and under n - 2.
int left = countCalls(n - 1);
int right = countCalls(n - 2);
// [3] Total for this call: itself plus both subtrees.
return 1 + left + right;
}function countCalls(n) {
// [1] Base case: a call on 0 or 1 is a leaf, and it counts as one call.
if (n < 2) {
return 1;
}
// [2] Recursive case: ask for the calls under n - 1 and under n - 2.
const left = countCalls(n - 1);
const right = countCalls(n - 2);
// [3] Total for this call: itself plus both subtrees.
return 1 + left + right;
}def level_work(n):
# [1] Level 1 has one call, on all n items. The work of a level is its calls times the size of each.
calls = 1
size = n
total = 0
while size > 1:
# [2] A call on more than one item scans all its items, so the level adds calls * size.
total += calls * size
# [3] The next level has twice as many calls, each on half the items, rounded down.
calls *= 2
size //= 2
# [4] The last level holds calls on one item, and each is one unit of work.
return total + callspublic int levelWork(int n) {
// [1] Level 1 has one call, on all n items. The work of a level is its calls times the size of each.
int calls = 1;
int size = n;
int total = 0;
while (size > 1) {
// [2] A call on more than one item scans all its items, so the level adds calls * size.
total += calls * size;
// [3] The next level has twice as many calls, each on half the items, rounded down.
calls *= 2;
size /= 2;
}
// [4] The last level holds calls on one item, and each is one unit of work.
return total + calls;
}int levelWork(int n) {
// [1] Level 1 has one call, on all n items. The work of a level is its calls times the size of each.
int calls = 1;
int size = n;
int total = 0;
while (size > 1) {
// [2] A call on more than one item scans all its items, so the level adds calls * size.
total += calls * size;
// [3] The next level has twice as many calls, each on half the items, rounded down.
calls *= 2;
size /= 2;
}
// [4] The last level holds calls on one item, and each is one unit of work.
return total + calls;
}function levelWork(n) {
// [1] Level 1 has one call, on all n items. The work of a level is its calls times the size of each.
let calls = 1;
let size = n;
let total = 0;
while (size > 1) {
// [2] A call on more than one item scans all its items, so the level adds calls * size.
total += calls * size;
// [3] The next level has twice as many calls, each on half the items, rounded down.
calls *= 2;
size = Math.floor(size / 2);
}
// [4] The last level holds calls on one item, and each is one unit of work.
return total + calls;
}This estimate says that a function with one recursive rule makes about as many calls as its input is large.
This estimate multiplies the work of the first call, n, by the number of calls. It treats every call as if it were as large as the root.
This estimate reads the depth of the stack as the cost, because the stack is where the recursion lives.
The tree of weight(40) has 331,160,281 nodes. If the function remembers each answer the first time, the second call on the same n returns at once, and the tree shrinks. Each different n does its work once.
systemdesignmastery.dev/dsa/cheatsheets/recursion-trees-and-cost