Counting Recursive Calls
The time used by recursion depends on the number of invocations and the cost inside each one. Stack depth measures only the longest active path.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Count a regular tree
A full recursion tree with branching two and depth three has level counts 1, 2, 4, and 8. That gives 15 invocations and at most four simultaneously active along a path. Call stack traces show the active path for specific operations.
Inspect which nodes actually recur
If one branch terminates early for certain data, its descendants no longer occur. Use recursion trees to count the nodes of the real branching rule rather than assuming a full tree.[1]
Count different levels before choosing a growth rate
A recurrence that starts at depth zero and spawns two calls per node through depth three visits one, two, four and eight nodes at successive depths. The deepest path is four frames long. These are different costs, so name memory and execution counts separately. See recursion call stack traces.
Depth 0: 1 call
Depth 1: 2 calls
Depth 2: 4 calls
Depth 3: 8 calls
Total: 15 calls, maximum active depth: 4
Include the nonrecursive work in a runtime claim
The full binary recursion has \(2^{d+1}-1\) calls through depth \(d\). If each call scans the original input before deciding its next calls, the work per node is larger than a constant. Count leaf costs and internal work under the same model.