Translate DP to the matrix form
Short description
Sometimes, we can rewrite our dynamic programming transition formulae using matrices and operations between them (most remarkably multiplication). Then, we can exploit this form in some way.
Prerequisites
Basic familiarity with dynamic programming and matrices (linear algebra in general does not required).
General description
We shall not follow the most general setting here, instead focusing on the following DP (linear rerecurrenceelation):
Then, one can notice that the right hand side can be expressed as the dot product
These vectors on their own are very natural: the first one is our coefficients and the second one keeps our recent values of DP. The key idea now is to obtain the next vector of recent values, namely \( \begin{pmatrix} dp_n & dp_{n-1} & \dots & dp_{n-k+1} \end{pmatrix}^T \). To do so, we need to apply our previous relation, and then shift all the other values (as well as eliminate \(dp_{n-k}\) because we will not need it further). Is it actually easy to shift, so we have the following identity
Or, in a more compact form (\(A\) is the large matrix on the left hand side)
where \(recent_n\) is the vector \((dp_n, dp_{n-1}, \dots, dp_{n-k+1}\) which contains the last \(k\) dp values. We can inductively deduce
Matrix multiplication can be done in \(O(k^3)\), and using binary exponentiation we obtain \(O(k^3 \log n)\) algorithm.
Example 1 (+constant)
Given \(dp_n = A dp_{n-1} + B dp_{n-2} + C\), find \(dp_N\) in \(O(\log N)\) time.
We can use the same idea here,
only adding a column to our transition matrix and the constant to our \(recent_n\) vector.
Example 2 (twisted DPs)
Sometimes one needs to evaluate several twisted but simple DPs, something like (some random relations just for the sake of illustration):
Then one can also use some kind of matrix transformation but with more convoluted \(recent\) vectors.