Random Walks and Markov Chains on Graphs
A random walk hops from node to node, picking a neighbor at random each step. Its probabilities update by one matrix multiplication per step, and they settle into a stationary distribution: the eigenvector of the transition matrix with eigenvalue 1, which on an undirected graph is just each node's degree over the total.
What the symbols mean
- Distribution at step k
A row vector: the probability the walker is at each node after k steps.
- Transition matrix
Entry (u, v) is the probability of stepping from u to v; each row adds to 1.
- Normalized adjacency
Each row of A divided by that node's degree: pick a neighbor uniformly.
What the symbols mean
- Stationary distribution
A distribution that one more step leaves unchanged: a left eigenvector of P with eigenvalue 1.
- Degree
How many edges touch node v.
- Number of edges
Each edge adds 1 to two degrees, so the degrees add to 2|E|.
What the symbols mean
- Rank vector
The long-run share of time a random surfer spends on each page.
- Damping factor
The chance the surfer follows a link (typically 0.85) instead of jumping to a random page.
- Number of pages
The jump lands on each page with probability 1/n.
Explanation
A random walk on a graph is simply a token that hops from node to node, each step moving to a neighbor picked at random. It's a Markov chain (named after the mathematician Andrey Markov): where it goes next depends only on where it is now, not on how it got there. The formula looks abstract, but it has two pieces: (a row of probabilities, one per node, saying where the walker might be after steps) and (the transition matrix, whose entry is the chance of stepping from to ). One step of the walk is one matrix multiplication. Everything else in this lesson is about what happens when you keep multiplying.
The transition matrix. If the walker picks a neighbor uniformly, it steps from to each neighbor with probability , so . That's the same averaging matrix as in Graph Neural Networks, and it's no accident. Why a row vector times ? The chance of being at next is the sum over every node of (chance of being at now) times (chance of stepping ):
That's the law of total probability from Probability Rules, written as a matrix product.
Settling down. A distribution with doesn't change when you take another step: it's stationary. Read the equation again and you'll recognize it: is an eigenvector of with eigenvalue (a left eigenvector, since it multiplies from the left; equivalently an eigenvector of ). See Eigenvectors and Eigenvalues. Why does the walk head there from any start? Write the starting distribution in terms of 's eigenvectors. Each step multiplies the part along an eigenvector by its eigenvalue. On a connected graph that isn't bipartite, every other eigenvalue has , so those parts shrink like and only the eigenvalue-1 part survives. It's the long-run analysis from Discrete Dynamical Systems. The second-largest sets the speed: in the animation further down it's about , and since that eigenvalue is negative, the probabilities overshoot back and forth as they settle.
On an undirected graph, it's just the degrees. Guess and check it:
Each neighbor sends the same amount, , because its higher chance of holding the walker exactly cancels its splitting that chance among more edges. So busy intersections get visited more, in proportion to their number of roads. A nice consequence: the average time for the walker to return to is .
When it doesn't settle. On a bipartite graph (a square, say) the walker alternates sides forever, so the probabilities flip back and forth instead of settling; is an eigenvalue. On a disconnected graph the walker never leaves its piece, so the answer depends on where it started. On directed graphs, a node with no way out can trap all the probability.
PageRank. Treat the web as a directed graph, with links as edges, and imagine a surfer who clicks a random link on each page. Pages where the surfer spends a lot of time are important: lots of pages point to them, or important pages do. To fix dead ends and traps, the surfer follows a link with probability (about ) and otherwise jumps to a page chosen uniformly at random. The rank vector is the stationary distribution of that walk, , a linear system you can solve or simply iterate.
Node embeddings from walks. Here's a clever use in machine learning. Run many short random walks and treat each one like a sentence whose words are node names. Then train word embeddings on these "sentences," so nodes that tend to show up near each other in walks get vectors with high dot products (see Vectors, Embeddings and Similarity). Nodes in the same tightly knit community end up close together, and the embeddings can feed any classifier.
A common mistake. It's tempting to think a random walk visits every node equally often in the long run ("it's random, so it's fair"). It doesn't. On an undirected graph a node with 3 edges is visited three times as often as a node with 1. The stationary distribution is uniform only when every node has the same degree. A smaller slip: here 's rows add to 1 and the distribution is a row vector on the left. If you write distributions as columns, use .
See it
Worked examples
The graph has edges A–B, A–C, B–C and C–D. A walker starts at D. Find its distribution after 3 steps, and check that is stationary.
- 1
Find the degrees: A has 2 neighbors (B, C), B has 2 (A, C), C has 3 (A, B, D) and D has 1 (C).
A visitor on a news site is either reading articles (state 1) or watching a video (state 2). Each minute, a reader starts a video with probability , and a video watcher goes back to reading with probability . In the long run, what fraction of minutes is spent reading?
- 1
Write the transition matrix, rows as "from": . Each row adds to 1.
Practice
Type your answer and check it. Solve them all to complete the lesson.
A random walk runs on a star graph: one center joined to 4 leaves (and no other edges). In the long run, what fraction of the time is the walker at the center?
A server is either up or down. Each hour, an up server goes down with probability , and a down server comes back up with probability . In the long run, what fraction of hours is it down? Give 4 decimal places.
A chain has and current distribution . Find .
A walker starts at one corner of a square (the 4-cycle). Why doesn't its distribution converge?
On the graph with edges A–B, A–C, B–C and C–D, a walker leaves D. On average, how many steps until it first returns to D?
A two-state chain has , stationary distribution and second eigenvalue . Starting surely in state 1, what is the probability of being in state 1 after 3 steps?
A method embeds the nodes of a social network by running many short random walks and training word embeddings on them as if they were sentences. Which nodes end up with similar embeddings?
A security drone patrols a site with 12 checkpoints joined by 20 two-way paths, choosing a random path out of each checkpoint. One checkpoint has 5 paths. In the long run, what fraction of its stops are at that checkpoint?
The hardest one here. Get it, and you've got this lesson.
Three web pages link as follows: page 1 links to page 2, page 2 links to page 3, and page 3 links to pages 1 and 2. Find the PageRank vector with damping . Enter to 4 decimal places.
Where it's used
Search engines rank pages with stationary distributions of link graphs, and the same math ranks the most influential accounts in a social network or the most critical papers in a citation graph. Recommendation systems run random walks on user–item graphs ("people who pinned this also pinned that"), and node embeddings learned from walks power friend suggestions and fraud detection. Language models with a fixed context are Markov chains over tokens, and Markov chain Monte Carlo methods sample from complicated probability distributions by designing a walk whose stationary distribution is exactly the one you want.