Matrix

This article introduces a very important topic in linear algebra—matrices (Matrix). It mainly explains the properties and operations of matrices, as well as some applications of matrix multiplication.

Vectors and Matrices

In linear algebra, vectors are divided into column vectors and row vectors.

Warning

In Taiwan, China, the translations of 「column」 and 「row」 happen to be the opposite of those used in mainland China. OI Wiki follows the convention used in mainland China and uses column and row.

The main objects studied in linear algebra are column vectors, which by convention are denoted by bold lowercase letters. In linear algebra involving many vectors and matrices, when there is no risk of confusion, the vector notation above a letter may be omitted when writing by hand.

A vector is also a special kind of matrix. To represent a row vector, write a transpose symbol at the upper right of a bold lowercase letter. Row vectors generally represent equations in linear algebra.

Introduction

Matrices arise from systems of linear equations. Like vectors, matrices embody the idea of “packing” data for processing.

For example, write the system of linear equations:

{7x1+8x2+9x3=134x1+5x2+6x3=12x1+2x2+3x3=11\begin{equation} \begin{cases} 7x_1+8x_2+9x_3=13 \\ 4x_1+5x_2+6x_3=12 \\ x_1+2x_2+3x_3=11 \end{cases} \end{equation}

Matrices are generally represented with parentheses or square brackets. Extracting the coefficients above, write it in the form of matrix multiplication:

(789456123)(x1x2x3)=(131211)\begin{equation} \begin{pmatrix} 7 & 8 & 9 \\ 4 & 5 & 6 \\ 1 & 2 & 3 \end{pmatrix}\begin{pmatrix} x_1 \\ x_2 \\ x_3 \end{pmatrix}=\begin{pmatrix} 13 \\ 12 \\ 11 \end{pmatrix} \end{equation}

For short, write:

Ax=bAx=b

That is, left-multiply the column vector x of unknowns by a matrix A to obtain the column vector b. This equation can be regarded as the basic form of linear algebra.

The main operation model studied in linear algebra is the inner product. An inner product first multiplies and then adds; it is the process of left-multiplying a row vector by a column vector to obtain a number.

Matrix multiplication is an extension of the inner product. Matrix multiplication is equivalent to taking one row from the left matrix and one column from the right matrix, taking their inner product, and obtaining the corresponding element of the result matrix. The mnemonic is “left row, right column”.

When the object being studied is the column vector on the right, matrix multiplication amounts to left-multiplying the column vector. From the perspective of left multiplication, a matrix is a transformation of column vectors: transform every column vector of the matrix on the right in the matrix multiplication, obtaining the corresponding column vector in the result matrix.

A matrix can transform one column vector, or “pack” the transformation of a group of column vectors, and can even transform the whole space—that is, all column vectors. When a matrix is viewed as a transformation of the whole space, it is detached from the space and becomes a purely transformational object.

Definition

For a matrix AA, the main diagonal refers to the elements Ai,iA_{i,i}.

II generally denotes the identity matrix, whose main diagonal consists of 1 and whose other positions are 0.

Matrices of the Same Type

Two matrices are called matrices of the same type if their numbers of rows and columns correspondingly agree.

Square Matrix

A matrix whose number of rows equals its number of columns is called a square matrix. A square matrix is a special kind of matrix. The customary phrase “an nn-order matrix” actually refers to an nn-order square matrix. Square matrices with the same order are matrices of the same type.

General matrices are used when studying systems of equations, vector groups, and the rank of matrices. Square matrices are used when studying eigenvalues and eigenvectors, and quadratic forms.

Main Diagonal

In a square matrix, the elements whose row and column numbers are equal form the main diagonal.

Symmetric Matrix

If the elements of a square matrix are symmetric about the main diagonal—that is, for any ii and jj, the element in row ii, column jj equals the element in row jj, column ii—then the square matrix is called a symmetric matrix.

Diagonal Matrix

A square matrix whose elements outside the main diagonal are all 00 is called a diagonal matrix, generally written as:

diag⁡{λ1,⋯ ,λn}\operatorname{diag}\{\lambda_1,\cdots,\lambda_n\}

The λ1,⋯ ,λn\lambda_1,\cdots,\lambda_n in the expression are the elements on the main diagonal.

A diagonal matrix is a symmetric matrix.

If all elements of a diagonal matrix are 11, it is called the identity matrix and denoted by II. As long as multiplication is possible, multiplying any matrix by the identity matrix leaves it unchanged, regardless of its shape.

Triangular Matrix

If all elements below the main diagonal of a square matrix are 00, it is called an upper triangular matrix. If all elements above the main diagonal are 00, it is called a lower triangular matrix.

The product of two upper (lower) triangular matrices is still an upper (lower) triangular matrix. If all diagonal elements are not 00, an upper (lower) triangular matrix is invertible, and its inverse is also upper (lower) triangular.

Unit Triangular Matrix

If the diagonal of an upper triangular matrix AA consists entirely of 11s, then AA is called a unit upper triangular matrix. If the diagonal of a lower triangular matrix AA consists entirely of 11s, then AA is called a unit lower triangular matrix.

The product of two unit upper (lower) triangular matrices is still a unit upper (lower) triangular matrix, and the inverse of a unit upper (lower) triangular matrix is also a unit upper (lower) triangular matrix.

Operations

Linear Operations on Matrices

The linear operations on matrices are addition and subtraction, and scalar multiplication; all of them are performed element by element. Only matrices of the same type can be added or subtracted correspondingly.

Transpose of a Matrix

The transpose of a matrix is indicated by writing a transpose “T” symbol at the upper right of the matrix, meaning that the rows and columns of the matrix are exchanged.

A symmetric matrix remains unchanged before and after transposition.

Matrix Multiplication

Matrix multiplication is a generalization of the inner product of vectors.

The product of matrices is meaningful only when the number of columns in the first matrix equals the number of rows in the second matrix.

Let AA be a P×MP \times M matrix, BB be an M×QM \times Q matrix, and let matrix CC be the product of matrix AA and matrix BB.

In matrix CC, the element in row ii, column jj can be written as:

Ci,j=∑k=1MAi,kBk,jC_{i,j} = \sum_{k=1}^MA_{i,k}B_{k,j}

In matrix multiplication, in the result matrix CC, the number at row ii, column jj is obtained from matrix AA's row ii containing MM numbers and matrix BB's column jj containing MM numbers by multiplying and then adding. Here, multiplying and then adding is the inner product of vectors. The number in row ii, column jj of the product matrix is exactly the inner product of multiplier matrix AA's ii-th row vector and multiplier matrix BB's jj-th column vector; the mnemonic is left row, right column.

The vectors studied in linear algebra are mostly column vectors. Based on this definition of matrix multiplication, we often study left multiplication of a column vector by a matrix. It also illustrates the idea of “packed processing”: processing the inner products of many vectors at once.

Matrix multiplication is associative, but does not generally satisfy the commutative law.

Using associativity, matrix multiplication can be optimized using the idea of binary exponentiation.

In contests, since linear recurrences can be expressed in the form of matrix multiplication, matrix binary exponentiation is also commonly used to find a term of a linearly recurrent sequence.

Optimization

First, for relatively small matrices, consider manually unrolling the loops directly to reduce the constant factor.

The loops can be reordered to improve spatial locality. This optimization does not change the time complexity of matrix multiplication, but gives a constant-factor improvement.

// Taking the reference code below as an example
mat operator*(const mat& T) const {
  mat res;
  for (int i = 0; i < sz; ++i)
    for (int j = 0; j < sz; ++j)
      for (int k = 0; k < sz; ++k) {
        res.a[i][j] += mul(a[i][k], T.a[k][j]);
        res.a[i][j] %= MOD;
      }
  return res;
}

// Rather than
mat operator*(const mat& T) const {
  mat res;
  int r;
  for (int i = 0; i < sz; ++i)
    for (int k = 0; k < sz; ++k) {
      r = a[i][k];
      for (int j = 0; j < sz; ++j)
        res.a[i][j] += T.a[k][j] * r, res.a[i][j] %= MOD;
    }
  return res;
}

Inverse of a Square Matrix

For a square matrix AA, its inverse matrix PP is a matrix satisfying A×P=IA \times P = I.

An inverse matrix does not necessarily exist. If it exists, Gaussian elimination can be used to find it.

Determinant of a Square Matrix

The determinant is an operation on a square matrix.

Reference Code

Generally, a two-dimensional array can be used to simulate a matrix.

struct mat {
  LL a[sz][sz];

  mat() { memset(a, 0, sizeof a); }

  mat operator-(const mat& T) const {
    mat res;
    for (int i = 0; i < sz; ++i)
      for (int j = 0; j < sz; ++j) {
        res.a[i][j] = (a[i][j] - T.a[i][j]) % MOD;
      }
    return res;
  }

  mat operator+(const mat& T) const {
    mat res;
    for (int i = 0; i < sz; ++i)
      for (int j = 0; j < sz; ++j) {
        res.a[i][j] = (a[i][j] + T.a[i][j]) % MOD;
      }
    return res;
  }

  mat operator*(const mat& T) const {
    mat res;
    int r;
    for (int i = 0; i < sz; ++i)
      for (int k = 0; k < sz; ++k) {
        r = a[i][k];
        for (int j = 0; j < sz; ++j)
          res.a[i][j] += T.a[k][j] * r, res.a[i][j] %= MOD;
      }
    return res;
  }

  mat operator^(LL x) const {
    mat res, bas;
    for (int i = 0; i < sz; ++i) res.a[i][i] = 1;
    for (int i = 0; i < sz; ++i)
      for (int j = 0; j < sz; ++j) bas.a[i][j] = a[i][j] % MOD;
    while (x) {
      if (x & 1) res = res * bas;
      bas = bas * bas;
      x >>= 1;
    }
    return res;
  }
};

Two Perspectives on Systems of Linear Equations

There are two perspectives for viewing matrix A, or transformation A.

The first perspective is to look by rows and observe every row of A. In this way, regard A as a system of equations. This gives the process of solving equations by elimination.

The second perspective is to look by columns and observe every column of A. A itself is also composed of column vectors. This is equivalent to regarding transformation A itself as a group of column vectors, with x as the unknown coefficients, and asking whether the group of column vectors in A can be combined with the unknowns to form column vector b.

For example, the opening example becomes:

(741)x1+(852)x2+(963)x3=(131211)\begin{equation} \begin{pmatrix} 7 \\ 4 \\ 1 \end{pmatrix}x_1+\begin{pmatrix} 8 \\ 5 \\ 2 \end{pmatrix}x_2+\begin{pmatrix} 9 \\ 6 \\ 3 \end{pmatrix}x_3=\begin{pmatrix} 13 \\ 12 \\ 11 \end{pmatrix} \end{equation}

Solving the equations becomes the question of whether, by adjusting the three coefficients x, the given three basis vectors can be combined to form the resulting vector.

Looking by columns is more novel than looking by rows. From the column perspective, linear independence and linear dependence can be studied.

Applications of Matrix Multiplication

Matrix-Accelerated Recurrence

Take the Fibonacci sequence as an example. In the Fibonacci sequence, F1=F2=1F_1 = F_2 = 1 and Fi=Fi−1+Fi−2(i≥3)F_i = F_{i - 1} + F_{i - 2}(i \geq 3).

If a problem asks you to find the nn-th term of the Fibonacci sequence, the simplest method is direct recurrence. But if the range of nn reaches the order of 101810^{18}, recurrence is no longer feasible, so we can consider matrix-accelerated recurrence.

According to the matrix form of the recurrence formula for the Fibonacci sequence:

[Fn−1Fn−2][1110]=[FnFn−1]\begin{bmatrix} F_{n-1} & F_{n-2} \end{bmatrix} \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix} = \begin{bmatrix} F_n & F_{n-1} \end{bmatrix}

Define the initial matrix ans=[F2F1]=[11],base=[1110]\text{ans} = \begin{bmatrix}F_2 & F_1\end{bmatrix} = \begin{bmatrix}1 & 1\end{bmatrix}, \text{base} = \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}. Then FnF_n is the element in row 1, column 1 of the matrix ansbasen−2\text{ans} \text{base}^{n-2}, namely the element in row 1, column 1 of [11][1110]n−2\begin{bmatrix}1 & 1\end{bmatrix} \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}^{n-2}.

Note

Matrix multiplication is not commutative, so you must not write the element in row 1, column 1 of [1110]n−2[11]\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}^{n-2} \begin{bmatrix}1 & 1\end{bmatrix}. In addition, for n≤2n \leq 2, simply output 11; there is no need to perform matrix binary exponentiation.

Why multiply by the base\text{base} matrix's n−2n-2-th power rather than the nn-th power? Because F1,F2F_1, F_2 can be obtained without matrix multiplication. In other words, one multiplication is enough to obtain F3F_3. If it is still unclear why the exponent is n−2n-2, try calculating it by hand.

Here is sample code for finding the nn-th Fibonacci number modulo 109+710^9+7 (core part).

constexpr int mod = 1000000007;

struct Matrix {
  int a[3][3];

  Matrix() { memset(a, 0, sizeof a); }

  Matrix operator*(const Matrix &b) const {
    Matrix res;
    for (int i = 1; i <= 2; ++i)
      for (int j = 1; j <= 2; ++j)
        for (int k = 1; k <= 2; ++k)
          res.a[i][j] = (res.a[i][j] + a[i][k] * b.a[k][j]) % mod;
    return res;
  }
} ans, base;

void init() {
  base.a[1][1] = base.a[1][2] = base.a[2][1] = 1;
  ans.a[1][1] = ans.a[1][2] = 1;
}

void qpow(int b) {
  while (b) {
    if (b & 1) ans = ans * base;
    base = base * base;
    b >>= 1;
  }
}

int main() {
  int n = read();
  if (n <= 2) return puts("1"), 0;
  init();
  qpow(n - 2);
  println(ans.a[1][1] % mod);
}

This is a slightly more complicated example.

f1=f2=0fn=7fn−1+6fn−2+5n+4×3n\begin{gathered} f_{1} = f_{2} = 0\\ f_{n} = 7f_{n-1}+6f_{n-2}+5n+4\times 3^n \end{gathered}

We see that fnf_n is related to fn−1,fn−2,nf_{n-1}, f_{n-2}, n, so we consider constructing a matrix to describe the state.

However, if the matrix contains only the three elements [fnfn−1n]\begin{bmatrix}f_n& f_{n-1}& n\end{bmatrix}, it is difficult to construct the transition equation, because exponentiation and +1+1 cannot be described by a matrix.

Therefore, consider constructing a larger matrix.

[fnfn−1n3n1]\begin{bmatrix}f_n& f_{n-1}& n& 3^n & 1\end{bmatrix}

We want to construct a recurrence matrix that can transition to

[fn+1fnn+13n+11]\begin{bmatrix} f_{n+1}& f_{n}& n+1& 3^{n+1} & 1 \end{bmatrix}

The transition matrix is

[71000600005010012003050101]\begin{bmatrix} 7 & 1 & 0 & 0 & 0\\ 6 & 0 & 0 & 0 & 0\\ 5 & 0 & 1 & 0 & 0\\ 12 & 0 & 0 & 3 & 0\\ 5 & 0 & 1 & 0 & 1 \end{bmatrix}

Matrix-Expression Updates

「THUSCH 2017」The Great Magician

The great magician Little L makes nn magic crystal balls, each with energy values for the water, fire, and earth attributes. Little L arranges these nn crystal balls in a line on the ground from front to back, and begins today's magic performance.

Let Ai, Bi, CiA_i,\ B_i,\ C_i denote, respectively, the energy values of water, fire, and earth in the ii-th crystal ball from the front (indices start at 11).

Little L plans to cast mm spells. Each time, he chooses an interval [l,r][l, r] and casts one of the following 33 major classes and 77 kinds of spells:

  1. Magical stimulation: the energy of a specific attribute in every crystal ball in the interval erupts, thereby enhancing the energy of another specific attribute. Specifically, there are three possible forms:

    • Fire-element stimulation of water-element energy: set Ai=Ai+BiA_i = A_i + B_i.

    • Earth-element stimulation of fire-element energy: set Bi=Bi+CiB_i = B_i + C_i.

    • Water-element stimulation of earth-element energy: set Ci=Ci+AiC_i = C_i + A_i.

      Note that enhancing the energy of one attribute does not change the energy of the other attribute; for example, Ai=Ai+BiA_i = A_i + B_i does not increase or decrease BiB_i.

  2. Magical enhancement: Little L waves his staff and consumes vv points of his own mana to change the energy of a specific attribute in every crystal ball in the interval. Specifically, there are three possible forms:

    • Fixed-value enhancement of fire-element energy: set Ai=Ai+vA_i = A_i + v.
    • Doubling enhancement of water-element energy: set Bi=Bi⋅vB_i=B_i \cdot v.
    • Absorption and fusion of earth-element energy: set Ci=vC_i = v.
  3. Magical release: Little L gathers the energy of all crystal balls in the interval, fuses it into a new crystal ball, and gives it to an audience member outside the performance area. The energy value of each attribute in the generated crystal ball equals the algebraic sum of the corresponding energy values of all crystal balls in the interval. Note that the magical release process does not actually change the energy of the crystal balls in the interval.

It is worth mentioning that the raw materials of the crystal balls made and fused by Little L are customized OI factory crystals, so these crystal balls have an energy threshold of 998244353998244353. When the energy value of an attribute in a crystal ball is greater than or equal to this threshold, the energy value is automatically reduced modulo the threshold to prevent the crystal ball from exploding.

Little W is Little L's (only) audience member. He watched the entire performance and received every crystal ball fused by Little L during the performance. Little W wants to know the energy values of the three attributes contained in these crystal balls.

Because matrix multiplication satisfies associativity and distributivity, a single-point update can naturally be generalized to an interval: after deriving the matrix, maintain the product of interval matrices with a segment tree.

Here are a few examples.

The transition for Ai=Ai+vA_i = A_i + v

[ABC1][100001000010v001]=[A+vBC1]\begin{bmatrix} A & B & C & 1 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0\\ v & 0 & 0 & 1\\ \end{bmatrix}= \begin{bmatrix} A+v & B & C & 1\\ \end{bmatrix}

The transition for Bi=Bi⋅vB_i=B_i \cdot v

[ABC1][10000v0000100001]=[AB⋅vC1]\begin{bmatrix} A & B & C & 1 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 & 0\\ 0 & v & 0 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 1\\ \end{bmatrix}= \begin{bmatrix} A & B \cdot v & C & 1\\ \end{bmatrix}
「LibreOJ 6208」Queries on a Tree

There is a tree with nn nodes, rooted at node 11. Each node has two weights ki,tik_i, t_i, both initially 00.

There are three operations:

  1. Add⁡(x,d)\operatorname{Add}( x , d ) operation: for every point on the path from xx to the root, set ki←ki+dk_i\leftarrow k_i + d.

  2. Mul⁡(x,d)\operatorname{Mul}( x , d ) operation: for every point on the path from xx to the root, set ti←ti+d×kit_i\leftarrow t_i + d \times k_i.

  3. Query⁡(x)\operatorname{Query}( x ) operation: query point xx; its weight is txt_x.

    n, m≤100000, −10≤d≤10n,~m \leq 100000, ~-10 \leq d \leq 10

If considered directly, it is not easy to reason about pushing operations down and maintaining information. But matrices can express this easily.

[kt1][100010d01]=[k+dt1][kt1][1d0010001]=[kt+d×k1]\begin{aligned} \begin{bmatrix}k & t & 1 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ d & 0 & 1 \end{bmatrix} &= \begin{bmatrix}k+d & t & 1 \end{bmatrix}\\ \begin{bmatrix}k & t & 1 \end{bmatrix} \begin{bmatrix} 1 & d & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} &= \begin{bmatrix}k & t+d \times k & 1 \end{bmatrix} \end{aligned}

Fixed-Length Path Statistics

Problem Statement

Given an nn-order directed graph whose every edge has weight 11, and an integer kk, your task is to find, for every pair of vertices (u,v)(u,v), the number of paths from uu to vv of length kk (the path need not be simple; vertices or edges on it may be traversed multiple times).

Represent this graph with its adjacency matrix GG (for an edge (u→v)(u\to v) in the graph, set G[u,v]=1G[u,v]=1 and all other entries to 00; if there are multiple edges, let G[u,v]G[u,v] be their number). The algorithm below also applies when the graph has self-loops.

Obviously, this adjacency matrix gives the answer for k=1k=1.

Suppose we know the matrix formed by the numbers of paths of length kk, denoted by CkC_k, and want to find Ck+1C_{k+1}. The DP transition is clearly

Ck+1[i,j]=∑p=1nCk[i,p]⋅G[p,j]C_{k+1}[i,j] = \sum_{p = 1}^{n} C_k[i,p] \cdot G[p,j]

We can view this as matrix multiplication, so the transition above can be described as

Ck+1=Ck⋅GC_{k+1} = C_k \cdot G

Expanding this recurrence gives

Ck=G⋅G⋯G⏟k 次=GkC_k = \underbrace{G \cdot G \cdots G}_{k \text{ 次}} = G^k

To compute this matrix power, we can use binary exponentiation, computing the result in O(n3log⁡k)O(n^3 \log k) time.

Fixed-Length Shortest Paths

Problem Statement

Given an nn-order weighted directed graph and an integer kk, find, for every pair of vertices (u,v)(u,v), the length of the shortest path from uu to vv containing exactly kk edges. (The path need not be simple; vertices or edges on it may be traversed multiple times.)

Again construct the adjacency matrix GG of this graph, where G[i,j]G[i,j] represents the weight of the edge from ii to jj. If there is no edge between i,ji,j, then G[i,j]=∞G[i,j]=\infty. (For multiple edges, take the minimum edge weight.)

Obviously, the matrix above gives the answer for k=1k=1. Again suppose we know the answer for kk, denoted by matrix LkL_k. We now want the answer for k+1k+1. The transition is clearly

Lk+1[i,j]=min⁡1≤p≤n{Lk[i,p]+G[p,j]}L_{k+1}[i,j] = \min_{1\le p \le n} \left\{L_k[i,p] + G[p,j]\right\}

In fact, we can compare this with matrix multiplication: the transition above merely changes the sum of products in matrix multiplication into addition followed by taking the minimum. Thus define this operation as ⊙\odot, namely

A⊙B=C  ⟺  C[i,j]=min⁡1≤p≤n{A[i,p]+B[p,j]}A \odot B = C~~\Longleftrightarrow~~C[i,j]=\min_{1\le p \le n}\left\{A[i,p] + B[p,j]\right\}

Thus

Lk+1=Lk⊙GL_{k+1} = L_k \odot G

Expanding the recurrence gives

Lk=G⊙…⊙G⏟k 次=G⊙kL_k = \underbrace{G \odot \ldots \odot G}_{k\text{ 次}} = G^{\odot k}

We can still compute the expression with matrix binary exponentiation, because it clearly satisfies associativity. The time complexity is O(n3log⁡k)O(n^3 \log k).

Bounded-Length Path Counting/Shortest Paths

The algorithm above applies only when the number of edges is fixed. However, we can improve it to handle the case where the number of edges is at most kk. Specifically, consider the following problem:

Problem Statement

Given an nn-order directed graph with edge weights 11 and an integer kk, your task is to find, for every pair of vertices (u,v)(u,v), the number of paths from uu to vv whose length is at most kk (the path need not be simple; vertices or edges on it may be traversed multiple times).

For every vertex vv, create a virtual vertex v′v' to record the answer, and add the two edges (v,v′)(v,v') and (v′,v′)(v',v') to the graph. Then, for a pair (u,v)(u,v), the number of paths from uu to vv with at most kk edges equals the number of paths from uu to v′v' with exactly k+1k+1 edges. This is because for every path with m(m≤k)m(m \le k) edges (p0=u)→p1→p2→⋯→pm−1→(pm=v)(p_0=u)\to p_1\to p_2 \to \dots \to p_{m-1} \to (p_m=v), there is a corresponding path with k+1k+1 edges (p0=u)→p1→p2→⋯→pm−1→(pm=v)→v′→⋯→v′(p_0=u)\to p_1 \to p_2 \to \dots \to p_{m-1} \to (p_m=v) \to v' \to \dots \to v'.

To find shortest paths with at most kk edges, simply add a self-loop of weight 00 to every vertex.

Exercises

Part of this page is translated from the blog posts Кратчайшие пути фиксированной длины, количества путей фиксированной длины and its English translation Number of paths of fixed length/Shortest paths of fixed length. The Russian version is licensed under Public Domain + Leave a Link; the English version is licensed under CC-BY-SA 4.0.

This content comes from OI Wiki: Matrix. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.