VOOZH about

URL: https://www.geeksforgeeks.org/dsa/matrix-exponentiation/

⇱ Matrix Exponentiation - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Matrix Exponentiation

Last Updated : 23 Jul, 2025

Matrix Exponentiation is a technique used to calculate a matrix raised to a power efficiently, that is in logN time. It is mostly used for solving problems related to linear recurrences.

Idea behind Matrix Exponentiation:

Similar to Binary Exponentiation which is used to calculate a number raised to a power, Matrix Exponentiation is used to calculate a matrix raised to a power efficiently.

Let us understand Matrix Exponentiation with the help of an example:


We can calculate matrix M^(N - 2) in logN time using Matrix Exponentiation. The idea is same as Binary Exponentiation:

When we are calculating (MN), we can have 3 possible positive values of N:

  • Case 1: If N = 0, whatever be the value of M, our result will be Identity MatrixI.
  • Case 2: If N is an even number, then instead of calculating (MN), we can calculate ((M2)N/2) and the result will be same.
  • Case 3: If N is an odd number, then instead of calculating (MN), we can calculate (M * (M(N – 1)/2)2).

Use Cases of Matrix Exponentiation:

Finding nth Fibonacci Number:

The recurrence relation for Fibonacci Sequence is F(n) = F(n - 1) + F(n - 2) starting with F(0) = 0 and F(1) = 1.


Below is the implementation of above idea:


Output
2

Time Complexity: O(logN), because fast exponentiation takes O(logN) time.
Auxiliary Space: O(1)

Finding nth Tribonacci Number:

The recurrence relation for Tribonacci Sequence is T(n) = T(n - 1) + T(n - 2) + T(n - 3) starting with T(0) = 0, T(1) = 1 and T(2) = 1.


Below is the implementation of above idea:


Output
4

Time Complexity: O(logN), because fast exponentiation takes O(logN) time.
Auxiliary Space: O(1)

Applications of Matrix Exponentiation:

Matrix Exponentiation has a variety of applications. Some of them are:

  • Any linear recurrence relation, such as the Fibonacci Sequence, Tribonacci Sequence or linear homogeneous recurrence relations with constant coefficients, can be solved using matrix exponentiation.
  • The RSA encryption algorithm involves exponentiation of large numbers, which can be efficiently handled using matrix exponentiation techniques.
  • Dynamic programming problems, especially those involving linear recurrence relations, can be optimized using matrix exponentiation to reduce time complexity.
  • Matrix exponentiation is used in number theory problems involving modular arithmetic, such as finding large powers of numbers modulo some value efficiently.

Advantages of Matrix Exponentiation:

Advantages of using Matrix Exponentiation are:

  • Matrix Exponentiation helps in finding Nth term of linear recurrence relations like Fibonacci or Tribonacci Series in log(N) time. This makes it much faster for large values of N.
  • It requires O(1) space if we use the iterative approach as it requires constant amount of extra space.
  • Large numbers can be handled without integer overflow using modulo operations.

Disadvantages of Matrix Exponentiation:

Disadvantages of using Matrix Exponentiation are:

  • Matrix Exponentiation is more complex than other iterative or recursive methods. Hence, it is harder to debug.
  • Initial conditions should be handled carefully to avoid incorrect results.
Comment