Random walks on graphs

This page introduces random walks on graphs from the perspectives of grid graphs, sparse graphs, and general graphs, presenting several solution methods and comparing their advantages and disadvantages.

Definition

Given a simple directed graph G=(V,E)(V={v1,v2,⋯ ,v∣V∣})G=(V, E)(V=\{v_1, v_2, \cdots, v_{|V|}\}) and start vertex s∈Vs \in V, terminal vertex t∈Vt \in V, let every edge e=(x,y)e=\left(x, y\right) have a positive weight wew_e satisfying ∀x∈V\{t}\forall x \in V \backslash\left\{t\right\}, ∑(x,y)∈Ew(x,y)=1\sum_{\left(x, y\right) \in E} w_{\left(x, y\right)}=1. Also, assume that for every vertex xx there is a path from xx to tt. A token starts from the start vertex and, each second, from its current vertex xx, with probability w(x,y)w_{(x, y)}, chooses an outgoing edge (x,y)\left(x, y\right) and moves to yy. It stops on reaching the terminal vertex. Find the expected time taken.

In fact, this problem can also be written in matrix form. Define the matrix PP by:

Px,y={w(x,y)if (x,y)∈E and x≠t0if (x,y)∉E or x=tP_{x, y}= \begin{cases} w_{(x, y)} & \text{if } (x, y) \in E \text{ and } x \neq t \\ 0 & \text{if } (x, y) \notin E \text{ or } x=t \\ \end{cases}

The required answer is:

∑k≥0k×(Pk)s,t\sum_{k \geq 0} k \times\left(P^k\right)_{s, t}

Here, (Pk)s,t\left(P^k\right)_{s, t} is the probability of first reaching the terminal vertex after kk steps. When the graph is finite and every vertex can reach the terminal vertex, the definition of PP shows that all its eigenvalues are less than one, so the answer is guaranteed to converge.

For convenience, unless otherwise specified, this page uses nn to denote ∣V∣|V| and mm to denote ∣E∣|E|.

In addition, a sparse graph on this page means a graph whose numbers of edges and vertices are of the same order.

Grid graphs

Example 1: Circles of Waiting

A token starts at the point (0,0)(0,0) in the Cartesian plane and moves randomly every second. If it is currently at (x,y)(x, y), then in the next second it moves with probability p1p_1 to (x−1,y)(x-1, y), with probability p2p_2 to (x,y−1)(x, y-1), with probability p3p_3 to (x+1,y)(x+1, y), and with probability p4p_4 to (x,y+1)(x, y+1). It is guaranteed that p1+p2+p3+p4=1p_1+p_2+p_3+p_4=1. Find the expected time until it moves to a position whose Euclidean distance from the origin is greater than RR. 0≤R≤500 \leq R \leq 50, p1,p2,p3,p4>0p_1, p_2, p_3, p_4>0, and the answer is taken modulo 109+710^9+7.

Naive approach

Let f(i,j)f(i, j) denote the expected time, starting from (i,j)(i, j), to reach a position whose Euclidean distance from the origin is greater than RR. The recurrence is:

f(i,j)={p1f(i−1,j)+p2f(i,j−1)+p3f(i+1,j)+p4f(i,j+1)+1i2+j2≤R20i2+j2>R2f(i, j)= \begin{cases} p_1 f(i-1, j) + p_2 f(i, j-1) + p_3 f(i+1, j) + p_4 f(i, j+1) + 1 & i^2 + j^2 \leq R^2 \\ 0 & i^2 + j^2 > R^2 \end{cases}

Because the transitions do not have a topological order, Gaussian elimination is required. The time complexity is O(R6)O\left(R^6\right), which is too high for this problem.

Direct elimination

Notice that most coefficients in the equations to be eliminated are zero. The complexity can be reduced by computing only positions with nonzero values during elimination.

Consider the elimination process. Eliminate the equations from top to bottom in the coordinate system, and from left to right within each row. Color eliminated equations yellow, points adjacent to yellow points green, and all other points black, as shown below:

graph-random-walk-1
graph-random-walk-1

Next, eliminate the equation corresponding to the next green cell. In this equation, only the coefficients of the variables corresponding to the green cell and the first black cell below it may be nonzero; likewise, only the equations corresponding to those two cells may have a nonzero coefficient for the variable corresponding to the current cell.

There are only O(R)O(R) green cells, so eliminating one equation takes O(R2)O\left(R^2\right) time. Since there are only O(R2)O\left(R^2\right) equations, the total time complexity is reduced to O(R4)O\left(R^4\right), which is sufficient for this problem.

Pivot method

There are O(R2)O\left(R^2\right) equations and variables. If the size can be reduced to O(R)O(R), naive Gaussian elimination will be sufficient.

Take the variable corresponding to the leftmost cell in each row as a pivot; there are 2R+12 R+1 pivots in total. Express the variables corresponding to all other cells as linear functions of these pivots. Consider the columns from left to right. For each cell (i,j)(i, j) in the current column, f(i,j),f(i−1,j),f(i,j−1),f(i,j+1)f(i, j), f(i-1, j), f(i, j-1), f(i, j+1) are known linear functions of the pivots. Rearranging the recurrence gives:

f(i+1,j)=f(i,j)−p1f(i−1,j)−p2f(i,j−1)−p4f(i,j+1)−1p3f(i+1, j)=\frac{f(i, j)-p_1 f(i-1, j)-p_2 f(i, j-1)-p_4 f(i, j+1)-1}{p_3}

This gives a linear-function representation of f(i+1,j)f(i+1, j) in terms of the pivots. If (i+1,j)(i+1, j) is already at Euclidean distance greater than RR from the origin, we obtain the equation f(i+1,j)=0f(i+1, j)=0. In the end, there are 2R+12 R+1 equations, on which we can perform Gaussian elimination.

During the stage of recursively deriving the linear functions of the pivots, there are O(R2)O\left(R^2\right) variables and deriving each variable takes O(R)O(R) time; afterward, the problem size has been reduced to O(R)O(R). Both parts take O(R3)O\left(R^3\right) time, so the total time complexity is also O(R3)O\left(R^3\right), which is sufficient for this problem.

Comparison of the two approaches

The two approaches are compared below from several perspectives:

In terms of time complexity, the worst-case complexity of the pivot method on a grid graph is O(nn)O(n \sqrt{n}) (it is highest when both the length and width of the grid graph are of order O(n)O(\sqrt{n})), while the worst-case complexity of direct elimination is O(n2)O\left(n^2\right). The pivot method is better.

In terms of precision, direct elimination is more accurate than the pivot method for problems requiring real-number calculations rather than calculations modulo a number.

In terms of applicability, the two approaches are suited to different situations.

When the grid graph contains obstacles or some edges have probability 00, the pivot method needs one additional pivot for every obstacle or edge with probability 00. When the number of obstacles or edges with probability 00 exceeds O(R)O(R), the time complexity of the pivot method increases, while the time complexity of direct elimination remains unchanged.

However, the pivot method can also eliminate recurrences similar to those on grid graphs, such as f(i,j)=p1f(i+1,j)+p2f(i,j+1)+p3f(pre⁡(i,j))+1f(i, j)=p_1 f(i+1, j)+p_2 f(i, j+1)+p_3 f(\operatorname{pre}(i, j))+1, where pre⁡(i,j)=(x,y)(x≤i,y≤j)\operatorname{pre}(i, j)=(x, y)(x \leq i, y \leq j) is given by the problem. The complexity analysis for direct elimination does not apply to this model.

In addition, the determinant of a grid graph's adjacency matrix cannot be computed with the pivot method; only direct elimination can optimize its time complexity.

In summary, each approach has its strengths, and the appropriate one should be chosen after analyzing the specific problem.

Sparse graphs

Example 2: Expected Value

Given a simple, undirected, connected sparse graph G=(V,E)G=(V, E), a token starts at v1v_1. Each second it chooses uniformly among the edges incident to its current vertex and moves to the vertex at the other end. Find the expected time to reach vnv_n. n≤2000n \leq 2000, and the answer is taken modulo pp, where pp is a randomly generated prime in the interval [109,1.01×109]\left[10^9, 1.01 \times 10^9\right].

Prerequisites

Definition 4.1. Every polynomial satisfying p(A)=0p(A) = 0, namely p(λ)p(λ), is called an annihilating polynomial of matrix AA.

Definition 4.2. Let InI_n denote the nn-th order identity matrix. The characteristic polynomial of an n×nn × n matrix AA is defined as p(λ)=det⁡(λIn−A)p(λ) = \det(λI_n - A), where det⁡\det denotes the determinant of a matrix. It is easy to see that the characteristic polynomial of an nn-th order matrix AA has degree at most nn.

Theorem 4.2. (Cayley–Hamilton theorem) The characteristic polynomial of any matrix is an annihilating polynomial for that matrix.

Therefore, the degree of the annihilating polynomial of least degree for an nn-th order matrix is also at most nn.

Solving the original problem

Notice that the expected time E(t)=∑i≥0Pr⁡[t>i]E(t)=\sum_{i\geq0}\Pr[t>i] is the sum of the probabilities that the walk has not ended after ii steps. If we can find these probabilities, summing over all i≥0i ≥ 0 gives the answer.

Let f(i,j)f(i, j) denote the probability that after ii steps the token is currently at jj and has never reached nn. Then:

f(i,j)=∑(k,j)∈Ef(i−1,k)deg⁡k(j≠n)f(i,j)=\sum_{(k,j)\in E}\frac{f(i-1,k)}{\deg_k}(j\neq n)

Here deg⁡k\deg_k denotes the degree of kk.

The transition of ff is independent of ii, so one transition can be viewed as multiplication by a matrix, namely fi+1=fiMf{i+1}=f_iM. Since the least-degree annihilating polynomial of MM has degree at most nn, the shortest recurrence for ff also has length at most nn. Therefore, the shortest recurrence for Pr⁡[t>i]=∑j=1n−1f(i,j)\Pr[t>i]=\sum_{j=1}^{n-1}f(i,j) also has length at most nn. We can, in O(nm)O(nm) time, compute Pr⁡[t>0],Pr⁡[t>1],⋯ ,Pr⁡[t>3n]\Pr[t>0],\Pr[t>1],\cdots,\Pr[t>3n]; then, in O(n2)O(n^2) time, use the Berlekamp–Massey algorithm to find the shortest recurrence for Pr⁡[t>i]\Pr[t > i].

Consider a kk-th order linear recurrence sequence aa. Suppose that for i≥i0i ≥ i_0, ai=∑j=1kcjai−ja_i=\sum_{j=1}^kc_ja_{i-j}. Let the generating functions of aa and cc be A(x)A(x) and C(x)C(x), respectively. Then A(x)=A(x)C(x)+A0(x)A(x)=A(x)C(x)+A_0(x), where A0(x)A_0(x) is determined by the terms with i<i0i < i_0.

Returning to the original problem, once we know the shortest recurrence for Pr⁡[t>i]\Pr[t > i], we can find C(x)C(x) and A0(x)A_0(x) (with the same definitions as in the preceding paragraph). Rearranging gives A(x)=A0(x)1−C(x)A(x)=\frac{A_0(x)}{1-C(x)}. We seek ∑i≥0[xi]A(x)\sum_{i\geq0}[x^i]A(x), which is clearly A(1)A(1). Substituting x=1x = 1 solves the original problem. Since the modulus is a random prime, we may assume the denominator is not 00.

Thus, the problem is solved in O(nm+n2)O(nm+n^2) time. If the numbers of vertices and edges of GG are of the same order, the time complexity here can be regarded as O(n2)O(n^2).

General graphs

Example 3: Frank

Given a simple strongly connected directed graph G=(V,E)G = (V, E), for all 1≤s≤n1 ≤ s ≤ n, 1≤t≤n1 ≤ t ≤ n, and s≠ts ≠ t, answer the following question: A token starts at vsv_s. Each second it chooses uniformly among the outgoing edges of its current vertex and moves to the vertex at the other end. Find the expected time to reach vtv_t. 3≤n≤4003 ≤ n ≤ 400.

Analysis and transformation

Let pi,jp_{i, j} be the probability, when the token is at ii, of choosing the outgoing edge (i,j)(i, j) and moving to jj; in particular, it is 00 when the edge does not exist. Let fi,jf_{i,j} be the expected time for a random walk from ii to jj; in particular, fi,i=0f_{i,i} = 0. When i≠ji ≠ j, the recurrence is:

fi,j=1+∑1≤k≤npi,kfk,jf_{i,j}=1+\sum_{1\leq k\leq n}p_{i,k}f_{k,j}

When i=ji = j, let gig_i denote the expected time for a random walk starting at ii to return to ii for the first time. Then:

fi,i=1−gi+∑1≤k≤npi,kfk,if_{i,i}=1-g_i+\sum_{1\le k\le n}p_{i,k}f_{k,i}

For clarity, write the recurrence in matrix form. Let PP be the transition matrix of the graph, FF the answer matrix, II the nn-th order identity matrix, JJ the all-ones nn-th order matrix with entries 11, and GG an nn-th order matrix satisfying Gi,i=giG_{i,i} = g_i and having 00 elsewhere. Then:

F=J−G+PFF=J-G+PF

If we can find GG, we only need to solve:

(I−P)F=J−G(I − P)F = J − G

Finding G

Definition 5.1. The stationary distribution of an nn-th order transition matrix PP is an nn-dimensional vector ππ satisfying ∑i=1nπi=1\sum_{i=1}^{n}\pi_{i}=1 and πP=ππP = π. Every component of ππ lies in the interval [0,1][0,1].

The meaning of a stationary distribution is easy to see. If at some moment the token has probability πiπ_i of being at viv_i, then it has the same distribution at every later moment. We can, in O(n3)O(n^3) time, find ππ by solving a system with Gaussian elimination. What is the relationship between ππ and GG?

Theorem 5.1. For every 1≤i≤n1 ≤ i ≤ n, πigi=1π_ig_i = 1.

Proof

From F=J−G+PFF = J - G + PF, rearranging gives:

G=PF+J−FG = PF + J − F

Multiplying both sides on the left by ππ gives:

πG=πPF+πJ−πFπG = πPF + πJ − πF

By the definition of ππ, we have πP=ππP = π, so:

πG=πJπG = πJ

Therefore:

πigi=∑j=1nπj=1\pi_ig_i=\sum_{j=1}^n\pi_j=1

This proves the original proposition.

Thus, by introducing the stationary distribution, in O(n3)O(n^3) time we can find GG.

Solving the original problem

While solving the system, we encounter a problem: (I−P)(I - P) is rank-deficient, so the system cannot be solved by multiplying by an inverse matrix.

Definition 5.2. A directed spanning tree of G=(V,E)G = (V,E) rooted at r∈Vr\in V is a subgraph of GG, namely T=(V,A)T = (V,A), satisfying:

  1. For every i≠ri ≠ r, the out-degree of ii is 11.
  2. The out-degree of rr is 00.
  3. TT contains no cycles.

Lemma 5.1. (The directed matrix-tree theorem) For a directed graph GG, let DD be its out-degree matrix, namely Di,i=diD_{i,i} = d_i and Di,j=0(i≠j)D_{i,j} = 0(i ≠ j), where did_i is the out-degree of ii, and let AA be its adjacency matrix. The number of directed spanning trees rooted at rr is the determinant of D−AD - A with row rr and column rr deleted.

Theorem 5.2. For a strongly connected graph G=(V,E)G = (V,E) and its transition matrix PP, the rank of (I−P)(I - P) is n−1n - 1.

Proof

Multiplying a row of a matrix by a nonzero constant does not change its rank, so, for (I−P)(I - P), multiply row ii by the out-degree of viv_i to obtain a new matrix LL. It suffices to prove that the rank of LL is n−1n - 1. Since every row of LL sums to 00, summing all column vectors of LL gives the zero vector, so these vectors are linearly dependent. so the rank of LL is not nn.
It is easy to see that LL equals the out-degree matrix of graph GG minus its adjacency matrix. By Lemma 5.1, LL with row ii and column ii deleted yields a determinant equal to the number of directed spanning trees rooted at viv_i.
Since G is strongly connected, the number of directed spanning trees rooted at any vertex is nonzero, namely not 00. Thus, after deleting LL's row ii and column ii, the matrix remains full rank. Since adding a column cannot decrease rank, LL with row ii deleted has linearly independent row vectors. Hence the rank of LL is n−1n - 1. Return to the original problem and consider solving its system. For convenience, write it as AX=BAX = B, where AA and BB are known and XX is unknown. Since AA is rank-deficient, there are infinitely many solutions; first find one particular solution. Perform Gaussian elimination on AA and BB together. Reduce AA by eliminating its first n−1n - 1 rows so that only the main diagonal and the nn-th column can have nonzero entries, and reduce the last row to all 00, obtaining the following form:

[100⋯0a1010⋯0a2001⋯0a3⋮⋮⋮⋱⋮⋮000⋯1an−1000⋯00]X=[b1,1b1,2b1,3⋯b1,n−1b1,nb2,1b2,2b2,3⋯b2,n−1b2,nb3,1b3,2b3,3⋯b3,n−1b3,n⋮⋮⋮⋱⋮⋮bn−1,1bn−1,2bn−1,3⋯bn−1,n−1bn−1,n000⋯00]\begin{bmatrix} 1 & 0 & 0 & \cdots & 0 & a_1 \\0&1&0&\cdots&0&a_2\\0&0&1&\cdots&0&a_3\\ \vdots&\vdots&\vdots&\ddots&\vdots&\vdots \\0&0&0&\cdots&1&a_{n-1}\\0&0&0&\cdots&0&0 \end{bmatrix} X= \begin{bmatrix} b_{1,1}&b_{1,2}&b_{1,3}&\cdots&b_{1,n-1}&b_{1,n} \\b_{2,1}&b_{2,2}&b_{2,3}&\cdots&b_{2,n-1}&b_{2,n} \\b_{3,1}&b_{3,2}&b_{3,3}&\cdots&b_{3,n-1}&b_{3,n} \\\vdots&\vdots&\vdots&\ddots&\vdots&\vdots \\b_{n-1,1}&b_{n-1,2}&b_{n-1,3}&\cdots&b_{n-1,n-1}&b_{n-1,n} \\0&0&0&\cdots&0&0 \end{bmatrix}

Set Xn,i=0X_{n,i} = 0 to obtain a particular solution, denoted by YY. Next adjust this particular solution to obtain an actual solution.
Since Xn,i=0X_{n,i} = 0, the combinatorial interpretation gives Yi,j=1+Yj,j+Pi,kXk,jY_{i,j} = 1 + Y_{j,j} + P_{i,k}X_{k,j}. It is easy to solve for Xi,j=Yi,j−Yj,jX_{i,j} = Y_{i,j} - Y_{j,j}. This solves the problem in O(n3)O(n^3) time.

References

  1. A brief discussion of random walks on graph models. IOI2019 Chinese National Team Candidate Collection of Papers (pp. 17–26)

This content comes from OI Wiki: Random walks on graphs. 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.