Proofs: Wilson's algorithm
These are the human proofs of Wilson's algorithm, at full length.
Wilson's algorithm returns each spanning tree with probability proportional to the product of its weights full
This proof shows: correctness, termination.
Claim. Let be a finite connected undirected multigraph with vertices and a weight on each edge; parallel edges and self-loops are allowed. For each root and each order of the vertices in line L2, WILSON stops with probability 1, and its output satisfies
for each spanning tree , the sum over all spanning trees . With all weights 1, .
Setup
For a vertex , let be the sum of over the edges at , with a self-loop counted twice. A step at chooses an edge at with probability (a self-loop with probability ) and moves to its other end. An arrow at is an edge at read as pointing from to its other end.
A rooted spanning tree at is a choice of one arrow at each such that following arrows from any vertex reaches . Forgetting the directions gives a bijection between rooted spanning trees at and spanning trees of : in a spanning tree, each has exactly one edge on its path to , and that edge must be . So it is enough to show , because .
Termination
Each walk of lines L4 to L6 is a random walk on a finite connected graph, and it stops when it reaches a vertex in the tree, which contains . From any vertex, some path of at most steps reaches , and the walk follows it with probability at least some . So the probability that a walk has not stopped after steps is at most . There are at most walks, so the algorithm stops with probability 1.
The stack model
Before the run, attach to each vertex an infinite stack of arrows at , all independent, each with the law of a step at . Let line L5 at vertex use the next unused entry of . The run is then a function of the stacks, and it has the same law as before, since each use of line L5 gets a fresh independent step.
At each time, the visible graph has, for each , the topmost unused entry of as its arrow. Each has out-degree 1 and has out-degree 0. So the visible graph is either a rooted spanning tree at , or it has a directed cycle; a self-loop arrow is a cycle of length 1. Popping a visible cycle marks the top entry of each vertex on it as used, so the next entry of each of these stacks becomes visible.
A colored cycle is a cycle together with the level of the stack entry at each of its vertices. Popping makes sense for colored cycles: the colored cycle is poppable when each of its entries is at the top of its stack and they form a cycle.
Step 1: the run pops cycles
Lemma 1. The run of WILSON on fixed stacks is a sequence of cycle pops, and when no cycle is visible the arrows of the tree it returns are the visible graph.
Proof. We keep the invariant: at the start of each pass of line L2, the visible arrows of the tree vertices other than are the arrows that the tree uses, and they lead to .
During a walk from , keep the current loop-erased path of distinct vertices not in the tree, where each is the visible arrow of . The walk stands at , and line L5 reads the visible arrow of . If it leads to a vertex in the tree, the walk stops. If it leads to a new vertex, that vertex is appended to the path. If it leads to a vertex of the path (with for a self-loop), the visible arrows of form a cycle; we pop it at once. The walk now stands at , the path becomes , and the visible arrow of is the next entry of its stack, which is exactly the entry that line L5 reads at the next step. The entries of are popped too; if the walk comes back to one of them later, line L5 reads its next entry, which is again the visible one. So the walk with these pops reads the same entries as line L5, and each read entry is visible when it is read.
When the walk reaches the tree, the visible arrows of lead into the tree, and they equal , since always holds the last entry read at . Lines L8 to L11 add exactly these vertices with these arrows. A vertex that was on an erased loop and not on the final path stays out of the tree, and its visible arrow is an entry not yet read. So the invariant holds after the pass. When the run ends, every vertex is in the tree, so the visible graph is the returned tree, which has no cycle, and the run was a sequence of pops of visible colored cycles.
Step 2: the order of popping does not matter
Lemma 2. Fix the stacks. Suppose some sequence of pops, each of a poppable colored cycle, ends with no visible cycle after pops. Then every sequence of pops of poppable colored cycles ends after exactly pops, pops the same set of colored cycles, and leaves the same visible tree.
Proof. Two different cycles visible at the same time share no vertex, since each vertex has one visible arrow. A colored cycle , once poppable, stays poppable until one of its vertices has its top entry used, and the only pop that uses the top entry of a vertex of while is visible is the pop of itself.
Induction on . If , no cycle is visible, so no pop is possible, and the claim holds. Let and let be the given sequence. Let be any poppable colored cycle at the start. The cycle is visible at the start and the sequence ends with no visible cycle, so some uses an entry of ; take the least such . Before step , no pop touched , so was still visible, and shares a vertex with , so . The cycles are disjoint from , so popping first does not change whether they are poppable. Hence is also a valid sequence that ends with no visible cycle, with the same set of colored cycles. After popping , the rest has pops, so by induction every sequence from that state has pops, the same set, and the same final tree. Each sequence starts with some poppable , so the claim holds for all sequences.
Step 3: the law of the tree
The run stops with probability 1, so by Lemma 1, with probability 1 the stacks admit a finite popping sequence. Call the set of colored cycles popped and the final tree the outcome. By Lemma 2 the outcome depends only on the stacks.
Fix a finite set of colored cycles that occurs as the popped set for some stacks with positive probability, and fix a rooted spanning tree . For each vertex , let be the number of cycles in through . The outcome is if and only if
- for each colored cycle in , the stack entries at its positions are the arrows of the cycle, and
- for each , the entry is .
Only if is clear: after popping , the top of is the entry at level , and it is the arrow of the final tree. If: since occurs for some stacks, its colored cycles can be popped in some order when the entries of condition 1 hold, because poppability depends only on these entries. After these pops, the visible graph is , which has no cycle. So this is a popping sequence that ends, and by Lemma 2 the outcome is .
The two conditions fix different stack entries, which are independent. Writing for the probability of an arrow (with the factor 2 for a self-loop), we get
Tree arrows are never self-loops, so the factor 2 does not appear in the second product. The first factor depends only on , and the set of possible does not depend on , since condition 1 alone decides whether can be popped. Summing over ,
The first two factors do not depend on , so for the spanning tree of . The probabilities sum to 1 over all spanning trees, so .
The order of the vertices in line L2 changes only the order of popping, so by Lemma 2 it does not change the outcome for fixed stacks, and the law is the same for every order.
Disconnected graphs
The implementation picks a root in each component. A walk never leaves its component, so the runs in different components use disjoint stacks and are independent, and each gives a tree of its component with the law above. The forest then has probability proportional to among the spanning forests with one tree in each component, and with all weights 1 it is uniform among them.