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 on n
vertices with m
edges. Let X_k
be the number of edges added while there are k
connected components until there are k-1
connected components. For example, initially there are n
connected components, so adding the first edge results in n-1
connected components so X_n = 1
. Similarly, the second edge also removes a component (though this happens in one of two ways) so X_n-1 = 1
as well. Define
X = X_n + X_n-1 + ... + X_2
The goal is to compute E(X)
, the expected value of X
. By additivity, we have
E(X) = E(X_n) + E(X_n-1) + ... + E(X_2)
It's not too hard to show that the probability that an edge added while there are k
components reduces the number of components has a lower bound of (k-1)/(n-1)
. Since X_k
is the random variable with probability of success given by this amount, the lower bound gives an upper bound for the expectation of X_k
:
E(X_k) <= (n-1)/(k-1)
Combining this, we get an asymptotic upper bound for E(X)
by n log n
.
With a bit more work and some hints from the Erdos-Renyi paper, you can probably deduce an exact formula.
OP's is a great solution and with a slight modification to the formula, it will always pass.
$f = fopen('php://stdin', 'r');
$n = intval(fgets($f));
echo round(1.249 * $n * log($n, 10));// constant factor is changed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With