GATE / CS ARCHIVE

SIMILAR QUESTIONS / ALGORITHMS

Count all calls in an overlapping recursive computation

2 questions solved the same way, newest first. Back to Algorithms

How these are solved

Count root plus both recursive subtrees, retaining base calls; evaluate a call-count recurrence rather than the returned arithmetic value. 1991 is Fibonacci at7;1997 is two-parameter lattice/Pascal recursion and asks a symbolic count.

1 mark · Descriptive
Report issue

Count recursive function activations

Asymptotic Analysis & Recurrences

Question 15(c), GATE 1997: For positive integers n,m, consider the directly recursive, nonmemoized function
function F(n,m:integer):integer;
begin
  if (n<=0) or (m<=0) then F:=1
  else F:=F(n-1,m)+F(n,m-1)
end.
Use C(s,t)=C(s−1,t)+C(s−1,t−1).
How many calls to F occur when evaluating F(n,m), including the original call?
Question 15(c), GATE 1997: For positive integers n,m, consider the directly recursive, nonmemoized function
function F(n,m:integer):integer;
begin
  if (n<=0) or (m<=0) then F:=1
  else F:=F(n-1,m)+F(n,m-1)
end.
Use C(s,t)=C(s−1,t)+C(s−1,t−1).
How many calls to F occur when evaluating F(n,m), including the original call?
Tap to enlarge
Answer & solution

Answer: 2C(n+m,n)−1. (no official key was published)

The execution forms a full binary recursion tree. Each nonbase call executes exactly two child calls, and each base call has no children. Every base call returns 1, while every internal call returns the sum of its two children's values. The root's return value is consequently the number L of leaf activations. From the recurrence solution, L=F(n,m)=C(n+m,n).

In a full binary tree with I internal nodes and L leaves, the number of edges is 2I because every internal node has two children. It is also I+L−1 because a tree has one fewer edge than nodes. Equating gives I=L−1, so the total number of call activations is I+L=2L−1. Therefore the requested count, including the root call, is

2C(n+m,n)−1.

For n=m=1, the root calls F(0,1) and F(1,0), giving three activations; the formula gives 2·2−1=3. For n=1,m=2 it gives five. If only calls initiated recursively were requested, one would subtract the original root and obtain 2C(n+m,n)−2, but that is not the wording here.

This counts the actual nonmemoized execution tree, including repeated argument pairs as separate calls. Counting only distinct states, or assuming a dynamic-programming implementation, would answer a different complexity question.

Original paper, page 8

2 marks · Other
Report issue

Recursive Fibonacci call count

Asymptotic Analysis & Recurrences

Question 1(x), GATE 1991: fib(0)=fib(1)=1; for n≥2, fib(n)=fib(n−1)+fib(n−2), using ordinary recursive calls. Count all calls evaluating fib(7), including the initial call.
Question 1(x), GATE 1991: fib(0)=fib(1)=1; for n≥2, fib(n)=fib(n−1)+fib(n−2), using ordinary recursive calls. Count all calls evaluating fib(7), including the initial call.
Tap to enlarge
Answer & solution

Answer: 41 (no official key was published)

Step through it (23 steps)

Let C(n) count function invocations. Each base case requires one call, so C(0)=C(1)=1. A nonbase invocation counts itself plus both recursive subtrees: C(n)=1+C(n−1)+C(n−2). Successive values for n=2, …, 7 are 3, 5, 9, 15, 25, 41. This count includes repeated evaluation of identical arguments, as the supplied recursion does not memoize them. It is different from the numeric return value fib(7)=21; counting only leaves or only additions would also answer a different question.

Original paper, page 1