# Random minimum spanning trees

I just ran across a post by John Baez pointing to an article by Alan Frieze on random minimum spanning trees.

Here’s the problem.

1. Create a complete graph with n nodes, i.e. connect every node to every other node.
2. Assign each edge a uniform random weight between 0 and 1.
3. Find the minimum spanning tree.
4. Add up the edges of the weights in the minimum spanning tree.

The surprise is that as n goes to infinity, the expected value of the process above converges to the Riemann zeta function at 3, i.e.

ζ(3) = 1/1³ + 1/2³ + 1/3³ + …

Incidentally, there are closed-form expressions for the Riemann zeta function at positive even integers. For example, ζ(2) = π² / 6. But no closed-form expressions have been found for odd integers.

## Simulation

Here’s a little Python code to play with this.

```    import networkx as nx
from random import random

N = 1000

G = nx.Graph()
for i in range(N):
for j in range(i+1, N):

T = nx.minimum_spanning_tree(G)
edges = T.edges(data=True)

print( sum([e[2]["weight"] for e in edges]) )
```

When I ran this, I got 1.2307, close to ζ(3) = 1.20205….

I ran this again, putting the code above inside a loop, averaging the results of 100 simulations, and got 1.19701. That is, the distance between my simulation result and ζ(3) went from 0.03 to 0.003.

There are two reasons I wouldn’t get exactly ζ(3). First, I’m only running a finite number of simulations (100) and so I’m not computing the expected value exactly, but only approximating it. (Probably. As in PAC: probably approximately correct.) Second, I’m using a finite graph, of size 1000, not taking a limit as graph size goes to infinity.

My limited results above suggest that the first reason accounts for most of the difference between simulation and theory. Running 100 replications cut the error down by a factor of 10. This is exactly what you’d expect from the central limit theorem. This suggests that for graphs as small as 1000 nodes, the expected value is close to the asymptotic value.

You could experiment with this, increasing the graph size and increasing the number of replications. But be patient. It takes a while for each replication to run.

## Generalization

The paper by Frieze considers more than the uniform distribution. You can use any non-negative distribution with finite variance and whose cumulative distribution function F is differentiable at zero. The more general result replaces ζ(3) with ζ(3) / F ‘(0). We could, for example, replace the uniform distribution on weights with an exponential distribution. In this case the distribution function is 1 – exp(-x), at its derivative at the origin is 1, so our simulation should still produce approximately ζ(3). And indeed it does. When I took the average of 100 runs with exponential weights I got a value of 1.2065.

There’s a little subtlety around using the derivative of the distribution at 0 rather than the density at 0. The derivative of the distribution (CDF) is the density (PDF), so why not just say density? One reason would be to allow the most general probability distributions, but a more immediate reason is that we’re up against a discontinuity at the origin. We’re looking at non-negative distributions, so the density has to be zero to the left of the origin.

When we say the derivative of the distribution at 0, we really mean the derivative at zero of a smooth extension of the distribution. For example, the exponential distribution has density 0 for negative x and density exp(-x) for non-negative x. Strictly speaking, the CDF of this distribution is 1 – exp(-x) for non-negative x and 0 for negative x. The left and right derivatives are different, so the derivative doesn’t exist. By saying the distribution function is simply exp(-x), we’ve used a smooth extension from the non-negative reals to all reals.

# Selecting things in Emacs

You can select blocks of text in Emacs just as you would in most other environments. You could, for example, drag your mouse over a region. You could also hold down the Shift key and use arrow keys. But Emacs also has a number of commands that let you work in larger semantic units. That is, instead of working with an undifferentiated set of characters, you can select meaningful chunks of text, the meaning depending on context.

When you’re editing English prose, the semantic units you are concerned with might be words, sentences, or paragraphs. When you’re editing programming language source code, you care about functions or various “balanced expressions” such as the content between two parentheses or two curly brackets.

The following table gives some of the selection commands built into Emacs.

UnitCommandKey binding
word`mark-word`M-@
paragraph`mark-paragraph`M-h
page`mark-page`C-x C-p
buffer`mark-whole-buffer `C-x h
function`mark-defun`C-M-h
balanced expression`mark-sexp`C-M-@

The `expand-region` package offers an alternative to several of these commands. More on that later.

The command for selecting a word does just what you expect. Likewise, the commands for selecting a page or a buffer require little explanation. But the meaning of a “paragraph” depends on context (i.e. editing mode), as do the meanings of “function” and “balanced expression.”

When editing source code, a “paragraph” is typically a block of code without blank lines. However, each language implements its own editing mode and could interpret editing units differently. Function definition syntax varies across languages, so `mark-defun` has to be implemented differently in each language mode.

Balanced expressions could have a different meanings in different contexts, but they’re fairly consistent. Content between matching delimiters—quotation marks, parentheses, square brackets, curly braces, etc.—is generally considered a balanced expression.

Here’s where `expand-region` comes in. It’s typically bound to `C-=`. It can be used as a substitute for `mark-word` and `mark-sexp`. And if you use it repeatedly, it can replace `mark-defun`.

Each time you call `expand-region` it takes in more context. For example, suppose you’re in text mode with your cursor is in the middle of a word. The first call to `expand-region` selects to the end of the word. The second call selects the whole word, i.e. expanding backward to the beginning. The next call selects the enclosing sentence and the next call the enclosing paragraph.

The `expand-region` function works analogously when editing source code. Suppose you’re editing the bit of Emacs Lisp below and have your cursor on the slash between `eshell` and `pwd`.

```(setq eshell-prompt-function
(lambda nil
(concat
(eshell/pwd)
" \$ ")))
```

Here’s what sequential invocations of `expand-region` will select.

1. `/pwd`
2. `/pwd/)`
3. `(eshell/pwd)`
4. `(eshell/pwd) " \$ ")`
5. `(concat (eshell/pwd) " \$ ")`
6. `(concat (eshell/pwd) " \$ "))`
7. `(lambda nil (concat (eshell/pwd) " \$ "))`
8. `(lambda nil (concat (eshell/pwd) " \$ ")))`
9. `(setq eshell-prompt-function (lambda nil (concat (eshell/pwd) " \$ ")))`

This is kinda tedious in this particular context because there are a lot of delimiters in a small region. In less dense code you’ll select larger blocks of code with each invocation of `expand-region`. Since each invocation requires only a single key (i.e. hold down Control and repeatedly type `=`) it’s easy to call `expand-region` over and over until you select the region you’d like.

Related posts: