Matrix Exponentiation
A linear recurrence advances by a fixed linear rule, so one step is a matrix–vector product and steps are a matrix power. Packaging Fibonacci, and any -term recurrence, into a transition matrix lets us jump to the -th term in by exponentiation by squaring — the same doubling trick from modular exponentiation, now over matrices.
╌╌╌╌
Modular exponentiation computed in
multiplications by reading the bits of and repeatedly squaring. That
lesson noted that the doubling idea is not special to integers: replace
multiply
with matrix multiply
and the same loop computes the -th
Fibonacci number in steps. This lesson makes that precise and general.
A linear recurrence — a rule that builds each term from a
fixed linear combination of the previous few — is repeated application of one
fixed matrix. Computing the -th term is then computing the -th power of that matrix,
and exponentiation by squaring does it in logarithmic time.
A recurrence is a matrix
Start with Fibonacci: , , and . The naive loop is additions, and the closed form (Binet's formula) involves irrational powers of the golden ratio, awkward to evaluate exactly. Instead, package two consecutive terms into a state vector and ask what one step does to it. The update together with the trivial is a linear map:
Call that matrix . One multiplication by advances the state by one term. Two multiplications advance it by two. Applying a total of times to the initial state lands on . So the whole question reduces to computing the matrix power .
The top-right entry of is directly, so once we can raise to the -th power we can read off with no vector multiply at all. It remains to compute fast.
Exponentiation by squaring, over matrices
Matrix multiplication is associative, which is the only property repeated squaring ever used. So the entire argument from the modular-exponentiation lesson carries over verbatim with the scalar product replaced by the matrix product and the scalar replaced by the identity matrix . Write in binary as ; then
where the powers are the repeated squares of , each the square of the previous. Sweeping the bits of from low to high, we keep a running square and fold it into an accumulator whenever the bit is set.
There are squarings and at most that many accumulator multiplications, so matrix multiplications in all. Each multiplication of two matrices is by the schoolbook method, giving the headline cost.
For Fibonacci , so this is a flat — exponentially faster than the loop once is large, and exact (only integer arithmetic, no floating-point golden ratio). The schoolbook per product can be shaved to with Strassen's algorithm, but for the small typical of recurrences the constant-factor simplicity of the cubic method wins.
- 1identity, the accumulator
- 2while do
- 3if thenlow bit set
- 4fold running square into result
- 5square for next bit
- 6
- 7return
The loop is byte-for-byte the iterative from the previous lesson, with
the scalar identity swapped for the matrix identity and $\cdot$ now meaning
matrix multiply. To compute a recurrence modulo a prime — the usual demand when the
true terms overflow — reduce every entry mod after each multiply, exactly as
reduces after each scalar product.
Worked example ( by squaring). Take and compute ; the top-right entry is . The exponent is , so the set bits are at positions and . Build the repeated squares:
Each is the square of the previous — for instance has top-left . The bits select and , so
and reading the entries confirms , , — the Fibonacci identity in a single squaring chain of four multiplies instead of ten additions. The gap only widens with : reaching is multiplies, not additions.
The general -term recurrence
Nothing above used . Suppose a sequence obeys an order- linear recurrence with fixed coefficients ,
Carry the last terms as the state vector .
The new top entry is the recurrence itself; every other entry is just a copy shifted down
by one. That compute the combination on top, shift the rest down
rule is a single
matrix, the companion matrix of the recurrence:
The top row holds the coefficients and produces ; the sub-diagonal of s copies each old term down one slot, discarding the oldest. The figure makes the two roles visible: one row that mixes, and a staircase of s that shifts.
Then where holds the given initial terms, and delivers 's power in . The pattern generalizes: counting problems whose answer satisfies a fixed linear recurrence — tilings, constrained strings, walk counts — are all handled this way. Climbing Stairs is literally Fibonacci; Count Vowels Permutation and Student Attendance Record II track a constant number of states whose transitions are linear, so each is a small companion (or transfer) matrix raised to the -th power.1
Worked example (a tiling count). The number of ways to tile a board with dominoes obeys with — a shifted Fibonacci. A richer case is the -term tribonacci count with , whose companion matrix is
The top row adds the last three terms; the two sub-diagonal s shift the window. Starting from , one multiply gives , so ; another gives , then . To leap to directly, raise to the power by : is matrix multiplies of a fixed matrix, independent of how large the terms grow.
Graph walks: the same matrix, a different reading
The companion matrix is one instance of a broader principle: whenever a process advances by a fixed linear rule, its -step behaviour is a matrix power. The cleanest other instance is counting walks in a graph. Let be the adjacency matrix of a graph, so when there is an edge . Then the matrix power counts paths:
Worked example (counting length- walks). Take the triangle-with-a-tail graph on vertices with edges , , (an undirected triangle), adjacency matrix
Read : there are exactly two length- walks from vertex back to itself, namely and . The off-diagonal counts the single length- walk . Every entry is a sum over the intermediate vertex , exactly , and raising to higher powers counts longer walks with no new idea — just more multiplies, each delivered in of them by .
So how many length- walks?
is answered in by raising to the
-th power with — and the companion-matrix recurrence above is just
this theorem applied to the small state-transition graph of the recurrence. The same
power, taken over the boolean semiring (replace with OR,
with AND), computes reachability within steps; over the min-plus semiring (replace
with , with ) it computes shortest-path lengths, which
underlies the all-pairs shortest-path algorithms.
Faster recurrence solvers and the semiring view
Matrix powering is the textbook route to a linear recurrence's -th term, but it is neither the only route nor always the fastest.
Faster than cubic: Kitamasa and Fiduccia. The cost is dominated by full multiplies. But the answer is a fixed linear combination of the first terms, and the coefficients of that combination are the residue of modulo the recurrence's characteristic polynomial. Computing that polynomial by repeated squaring of polynomials — the Fiduccia / Kitamasa method — costs with schoolbook polynomial multiplication, and when the polynomial products use the FFT.2 For a recurrence of large order this is a decisive improvement over the cubic matrix approach.
Eigen-decomposition and Binet's formula. If the companion matrix is diagonalizable, with diagonal, then and is just the eigenvalues raised to the -th power. For Fibonacci the eigenvalues are the golden ratio and its conjugate, which recovers Binet's formula exactly. The closed form is exact on paper but numerically fragile (the irrational powers must be carried to high precision), which is why the integer matrix power, exact by construction, is the algorithm of choice.3
One idea, three algorithms. The semiring view unifies several algorithms in this course. Over the ordinary ring, counts walks; over the boolean semiring it computes reachability, and the repeated-squaring version is essentially transitive closure; over the min-plus (tropical) semiring it computes shortest walks of bounded length, and squaring the distance matrix times gives the repeated-squaring all-pairs shortest paths algorithm that precedes Floyd–Warshall. The doubling trick on this page is the same one used in those graph algorithms.
Takeaways
- A linear recurrence of order is one fixed matrix applied repeatedly: pack the last terms into a state vector, and one step is a multiply by the companion matrix (top row = coefficients, sub-diagonal of s = shift).
- Computing the -th term is computing , done in matrix multiplications by exponentiation by squaring — the identical bit-scanning loop as , with scalar replaced by matrix and by .
- Total cost is (cubic per multiply, logarithmically many multiplies); for Fibonacci this is a flat , exact and overflow-free under a modulus.
- The same matrix power counts length- walks in a graph via , and over other semirings (boolean, min-plus) computes reachability and shortest paths — one algebraic idea behind recurrences, walk-counting, and transitive closure.
Footnotes
- Skiena, § — Number Theory / Dynamic Programming: linear recurrences advanced by powering a transition matrix in matrix multiplications; the companion-matrix construction. ↩
- C. M. Fiduccia,
An efficient formula for linear recurrences,
SIAM J. Computing 14(1), 1985 — computing via modulo the characteristic polynomial; the same method is widely known in competitive programming as Kitamasa's algorithm. ↩ - CLRS, Ch. 4 — Divide-and-Conquer: matrix powering for recurrences; and Problem 31-3 (Binet-style closed forms via eigenvalues) for the golden-ratio decomposition. ↩
╌╌ END ╌╌