This post summarizes how one uses the repertoire method (as presented in Concrete Mathematics by Graham, Knuth and Patashnik). First we look at the repertoire method without the need for a radix-based solution and afterwards we discuss the solution given in the book for Exercise 16.
General method
Suppose we are given the recursion
T ( 0 ) = α T ( n ) = β n 2 + γ n + δ + T ( n − 1 ) .
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
\begin{aligned}
T(0) &= \alpha \\
T(n) &= \beta n^2 + \gamma n + \delta + T(n-1).
\end{aligned}
T ( 0 ) T ( n ) = α = β n 2 + γn + δ + T ( n − 1 ) .
Starting the recursion, we find that T ( n )
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
T(n) T ( n ) is of the form T ( n ) = α A ( n ) + β B ( n ) + γ C ( n ) + δ C ( n )
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
T(n) = \alpha A(n) + \beta B(n) + \gamma C(n) + \delta C(n) T ( n ) = α A ( n ) + βB ( n ) + γ C ( n ) + δ C ( n ) and we can use the repertoire method.
There a two ways to proceed: One is to guess values for the parameters and to find fitting functions for A ( n ) , B ( n ) , C ( n ) , D ( n )
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
A(n),B(n), C(n), D(n) A ( n ) , B ( n ) , C ( n ) , D ( n ) , the other one is to guess functions and to find fitting values for α , β , γ , δ
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
\alpha,\beta,\gamma,\delta α , β , γ , δ . We proceed by guessing functions.
Function guessing
The idea here is to guess functions of which a linear combination results in a closed form solution to the recurrence given above. We start by guessing that
T ( n ) = 1.
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
\begin{aligned}T(n) &= 1.\end{aligned} T ( n ) = 1.
It follows that
T ( 0 ) = α = 1
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
\begin{aligned}T(0) = \alpha = 1\end{aligned} T ( 0 ) = α = 1
and
T ( n ) = β n 2 + γ n + δ + T ( n − 1 ) 1 = β n 2 + γ n + δ + 1.
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
\begin{aligned}T(n) &= \beta n^2+\gamma n + \delta + T(n-1) \\
1 &= \beta n^2+\gamma n + \delta + 1.\end{aligned} T ( n ) 1 = β n 2 + γn + δ + T ( n − 1 ) = β n 2 + γn + δ + 1.
By comparing the coefficients for all n
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
n n we see that β = γ = δ = 0
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
\beta=\gamma=\delta=0 β = γ = δ = 0 . Hence, A ( n ) = 1
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
A(n) = 1 A ( n ) = 1 .
From setting T ( n ) = n
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
T(n) = n T ( n ) = n , we find that ( α , β , γ , δ ) = ( 0 , 0 , 0 , 1 )
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
(\alpha,\beta,\gamma,\delta) = (0,0,0,1) ( α , β , γ , δ ) = ( 0 , 0 , 0 , 1 ) . Hence, D ( n ) = n
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
D(n) = n D ( n ) = n .
Setting T ( n ) = n 2
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
T(n) = n^2 T ( n ) = n 2 gives us ( α , β , γ , δ ) = ( 0 , 0 , 2 , − 1 )
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
(\alpha,\beta,\gamma,\delta) = (0,0,2,-1) ( α , β , γ , δ ) = ( 0 , 0 , 2 , − 1 ) . Hence,
n 2 = 2 C ( n ) − D ( n ) = 2 C ( n ) − n C ( n ) = n 2 + n 2 .
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
\begin{aligned}
n^2 &= 2C(n) - D(n) \\
&= 2C(n) - n \\
C(n) &= \frac{n^2+n}{2}.
\end{aligned}
n 2 C ( n ) = 2 C ( n ) − D ( n ) = 2 C ( n ) − n = 2 n 2 + n .
Finally, we set T ( n ) = n 3
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
T(n) = n^3 T ( n ) = n 3 and get ( α , β , γ , δ ) = ( 0 , 3 , − 3 , 1 )
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
(\alpha,\beta,\gamma,\delta) = (0,3,-3,1) ( α , β , γ , δ ) = ( 0 , 3 , − 3 , 1 ) . Hence,
n 3 = 3 B ( n ) − 3 C ( n ) + D ( n ) B ( n ) = 1 3 n 3 + 1 2 n 2 + 1 6 n ,
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
\begin{aligned}
n^3 &= 3 B(n) - 3 C(n) + D(n) \\
B(n) &= \frac 1 3 n^3 + \frac 1 2 n^2 + \frac 1 6 n,
\end{aligned}
n 3 B ( n ) = 3 B ( n ) − 3 C ( n ) + D ( n ) = 3 1 n 3 + 2 1 n 2 + 6 1 n ,
solving our recurrence. Comparing the result with this video we set ( α , β , γ , δ ) = ( 7 , 2 , 0 , 7 )
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
(\alpha,\beta,\gamma,\delta) = (7,2,0,7) ( α , β , γ , δ ) = ( 7 , 2 , 0 , 7 ) . This gives
T ( n ) = 7 A ( n ) + 2 B ( n ) + 0 C ( n ) + 7 D ( n ) = 7 + 2 ( 1 3 n 3 + 1 2 n 2 + 1 6 n ) + 7 n = 7 + 2 3 n 3 + n 2 + 22 3 n
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
\begin{aligned}
T(n) &= 7 A(n)+ 2 B(n) + 0 C(n) + 7 D(n) \\
&= 7 + 2 \left( \frac 1 3 n^3 + \frac 1 2 n^2 + \frac 1 6 n\right) + 7 n \\
&= 7 + \frac 2 3 n^3 + n^2 + \frac{22}{3} n
\end{aligned}
T ( n ) = 7 A ( n ) + 2 B ( n ) + 0 C ( n ) + 7 D ( n ) = 7 + 2 ( 3 1 n 3 + 2 1 n 2 + 6 1 n ) + 7 n = 7 + 3 2 n 3 + n 2 + 3 22 n
as in the video.
Radix-based solution
On page 16 the book states that a recurrence of the form
f ( j ) = α , for 1 ≤ j < d ; f ( d n + j ) = c f ( n ) + β j for 0 ≤ j < d and n ≥ 1
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
\begin{aligned}
f(j) &= \alpha, \qquad\qquad\;\text{for } 1 \leq j < d; \\
f(dn + j) &= cf(n) + \beta_j \quad \text{for } 0 \leq j < d \text{ and } n \geq 1
\end{aligned}
f ( j ) f ( d n + j ) = α , for 1 ≤ j < d ; = c f ( n ) + β j for 0 ≤ j < d and n ≥ 1
has the radix-changing solution
f ( ( b m b m − 1 … b 1 b 0 ) d ) = ( α b m β b m − 1 β b m − 2 … β b 1 β b 0 ) c .
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
f((b_m b_{m-1}\dots b_1 b_0)_d) = (\alpha_{b_m}\beta_{b_{m-1}}\beta_{b_{m-2}}\dots\beta_{b_1}\beta_{b_0})_c.
f (( b m b m − 1 … b 1 b 0 ) d ) = ( α b m β b m − 1 β b m − 2 … β b 1 β b 0 ) c .
Parameter guessing
Armed with this knowledge we can now solve exercies 1.16:
1.16 Use the repertoire method to solve the general four-parameter recurrence
g ( 1 ) = α g ( 2 n + j ) = 3 g ( n ) + γ n + β j , for j = 0 , 1 and n ≥ 1.
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
\begin{aligned}
g(1) &= \alpha \\
g(2n+j) &= 3g(n) + \gamma n + \beta_j, \quad \text{for } j = 0,1 \text{ and } n \geq 1.
\end{aligned}
g ( 1 ) g ( 2 n + j ) = α = 3 g ( n ) + γn + β j , for j = 0 , 1 and n ≥ 1.
Hint: Try the function g ( n ) = n
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
g(n) = n g ( n ) = n .
Let's start by using the hint given in the exercise. Using the same approach as before, setting g ( n ) = n
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
g(n) = n g ( n ) = n implies
g ( 1 ) = 1 = α 2 n = 3 n + γ n + β 0 2 n + 1 = 3 n + γ n + β 1 .
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
\begin{aligned}
g(1) &= 1 = \alpha \\
2n &= 3n + \gamma n + \beta_0 \\
2n + 1 &= 3n + \gamma n + \beta_1.
\end{aligned}
g ( 1 ) 2 n 2 n + 1 = 1 = α = 3 n + γn + β 0 = 3 n + γn + β 1 .
The values ( 1 , 0 , 1 , − 1 )
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
(1,0,1,-1) ( 1 , 0 , 1 , − 1 ) for the parameters ( α , β 0 , β 1 , γ )
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
(\alpha,\beta_0,\beta_1,\gamma) ( α , β 0 , β 1 , γ ) satisfy these equations for all n.
Thus n = A ( n ) + C ( n ) − D ( n )
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
n = A(n) + C(n) - D(n) n = A ( n ) + C ( n ) − D ( n ) .
Now, if we, by an educated guess, let ( α , β 0 , β 1 , γ )
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
(\alpha,\beta_0,\beta_1,\gamma) ( α , β 0 , β 1 , γ ) be ( 1 , 0 , 1 , 0 )
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
(1,0,1,0) ( 1 , 0 , 1 , 0 ) we get
g ( 1 ) = 1 g ( 2 n ) = 3 g ( n ) g ( 2 n + 1 ) = 3 g ( n ) + 1.
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
\begin{aligned}
g(1) &= 1 \\
g(2n) &= 3g(n) \\
g(2n+1) &= 3g(n) + 1.
\end{aligned}
g ( 1 ) g ( 2 n ) g ( 2 n + 1 ) = 1 = 3 g ( n ) = 3 g ( n ) + 1.
This looks exactly like the recurrence to which we already know the solution and gives us A ( n ) , B ( n )
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
A(n), B(n) A ( n ) , B ( n ) and C ( n )
\renewcommand{\R}{\mathbb R}
\newcommand{\E}{\mathbb E}
\newcommand{\Var}{\mathrm{Var}}
C(n) C ( n ) .
Combining both results we find the closed form.