This is Interviewstreet puzzle:
We have a country containing N cities. Each day we choose 2 cities such that there is no road between them and build a road between them. We choose each pair of nonadjacent cities with equal probability. Let X be the number of days before we obtain a connected country. What is the expected value of X? Output the integer part of answer.
What they are really asking is what number of edges m is needed (on average) for a random graph G(n, m) to become connected.
After writing a program that actually performed the experiment, I came up with this ‘solution’ that passes 9/10 tests
$f = fopen('php://stdin', 'r');
$n = intval(fgets($f));
echo round(1.25 * $n * log($n, 10));
So can it be solved with a single formula? What is the right way of finding likelihood of connectedness of random graph?
You should check out the classic paper of Erdos and Renyi from 1960 entitled “On the evolution of random graphs”. It contains complete probabilistic bounds for number of components, size of the largest components, etc.
Here’s a bit of the math set-up to get you started.
Let
G(n,m)be the simple random graph onnvertices withmedges. LetX_kbe the number of edges added while there arekconnected components until there arek-1connected components. For example, initially there arenconnected components, so adding the first edge results inn-1connected components soX_n = 1. Similarly, the second edge also removes a component (though this happens in one of two ways) soX_n-1 = 1as well. DefineThe goal is to compute
E(X), the expected value ofX. By additivity, we haveIt’s not too hard to show that the probability that an edge added while there are
kcomponents reduces the number of components has a lower bound of(k-1)/(n-1). SinceX_kis the random variable with probability of success given by this amount, the lower bound gives an upper bound for the expectation ofX_k:Combining this, we get an asymptotic upper bound for
E(X)byn log n.With a bit more work and some hints from the Erdos-Renyi paper, you can probably deduce an exact formula.