Details
A call graph is a directed acyclic graph whose vertices and edges are respectively a set of fun values and the immediate dependencies between them. The graph starts from one or more ni on its initial vertices, and follows another edge whenever it references another smaller value (by recursing). It terminates when it encounters a condition from inits.
The function fun must be specified as a rule: head[var]→form[var, head], where var and head are free symbols for the integer domain and range of a recursive function in some particular form.
The
inits must also be specified as a rule or as a list of rules of the form:
conditioni[
var] →
vali. When an integer value
m is assigned to
var, each
conditioni[
m] is checked by
TrueQ. When one
conditioni[
m] evaluates to
True, the function value is set as
head[
m]=
vali.
The initial value
vali can also be a function of
var, i.e.
conditioni[
var] →
vali[
var], in which case a
True conditioni[
m] leads to the assignment
head[
m]=
vali[
m].
For example,
fun=f[n]→f[n-1]+f[n-2] is the form of a typical linear recurrence with
head=f,
var=n, and
form=Function[#2[#1-1]+#2[#1-2]]. The
Fibonacci numbers have a special initial condition,
inits={
n<=1→n}, where
condition=Function[#1<=1] with
val=n. The initial condition can also be written as
inits={n==0→0,n==1→1}, with two
condition expressions and two
val values.
Nested recursive functions are recursive functions where the right-hand-side of fun contains a term like head[…] that again contains one or more instances of head[…] somewhere in its argument pattern. For example, the Hofstadter G function is defined by fun=f[n]→n-f[f[n-1]] with inits={n==0→0}. ResourceFunction["RecursiveFunctionCallGraph"] supports such functions.
Linear recurrences such as
Fibonacci are simple examples, whose call
Graphs are typically the
GraphUnion of a set of
PathGraphs. Nested recursive functions lead to more complex graph structures such as trees with fractal or pseudorandom branching.
ResourceFunction["RecursiveFunctionCallGraph"] takes all options of
Graph, and three more options related to computation and display:
Method,
"AddFigures", and "Bounding".
When
"AddFigures" is set to
True, the vertices of the returned
Graph are transformed into framed labels containing particular function values.
To avoid runaway iterations, it is generally assumed that evaluation of
f[n] does not depend on any
f[m] satisfying
0<n<m. If and when this condition becomes invalid, a
Failure object is thrown. This safety feature is nice to have when exploring function spaces. Setting option "Bounding" to
False, removes the safety guard to potentially enable more unusual results.