Proofs: Walktrap algorithm
These are the human proofs of Walktrap algorithm, at full length.
Walktrap merges by the least increase of sigma and ends with one community per component full
This proof shows: correctness, termination.
We use the notation of the page. After line L1, is symmetric with positive diagonal, for every , and . Let , and for a set let . Then by linearity, so and . For a set write , so that .
Lemma 1 (Ward's identity). For disjoint non-empty sets , .
Proof. For any set and any point , , because the cross term is 0. Let , , , and . Splitting the sum over and applying the identity to and to with gives . Now and , so the two extra terms add to .
So merging and changes by exactly , since the other communities are unchanged.
Call two disjoint sets adjacent if some edge of (not a loop) joins them.
Lemma 2 (invariants). Before each test of line L5: (a) the current communities partition ; (b) each community has stored vector ; (c) the table holds an entry for a pair of current communities exactly when they are adjacent, and the entry equals of the pair; (d) each community induces a connected subgraph of .
Proof. Initially (lines L2 and L3) the communities are the singletons, with ; the pairs in the table are the edges, with value , which is ; and singletons are connected. Suppose the invariants hold and lines L6 to L8 merge and , which are adjacent by (c). (a) The new set of communities is again a partition. (b) Line L7 sets the vector of to , and the other vectors are unchanged. (c) The entries with or are removed. A community is adjacent to exactly when it is adjacent to or to , and line L8 adds an entry for each such , with value by (b). Entries between two other communities are unchanged and still correct, as their communities and vectors did not change. (d) and are connected and joined by an edge, so their union is connected.
Lemma 3 (termination). The loop of lines L5 to L9 runs at most times, and when it stops, the communities are exactly the connected components of .
Proof. Each round reduces the number of communities by one, and there is at least one community while , so there are at most rounds. When the loop stops, no two communities are adjacent. By Lemma 2(d) each community lies inside one component. If a component contained two communities, then, the component being connected, some edge would join two different communities inside it, and they would be adjacent, a contradiction.
Theorem. WALKTRAP stops. Each merge it performs joins the pair of adjacent communities whose union increases the least (the smallest pair of ids among equal increases), and the dendrogram ends with one community per connected component. The returned partition is the one of largest modularity among the partitions of the dendrogram, the earliest among equal ones.
Proof. Termination and the final partition are Lemma 3. By Lemma 2(c) the table at line L6 holds the value for exactly the adjacent pairs, and by Lemma 1 this value is the increase of caused by the merge, so line L6 picks the pair with the least increase, with the stated tie rule. Line L9 records every partition of the dendrogram, and line L10 scans them in order and keeps the first maximum of modularity.