We are given an rectangular board, where each cell is either empty or full (). We define the distance between cells and to be .
In addition, there are rabbits who randomly assign themselves to the empty cells so that there is at most one rabbit per empty cell. After being assigned, we consider a graph with nodes, each node corresponding to a different rabbit. For each rabbit we add an edge to the closest rabbit . More specifically, if a rabbit is assigned to cell , we consider the rabbit who is assigned to such that the distance between and is minimal (as defined above), and we add an edge between the two corresponding nodes. If there is a tie, we select the rabbit whose is lexicographically smallest. We do this for each rabbit (in particular, there are exactly edges in this graph; there may be duplicate edges, but there will be no "self-loop" edges).
Given the description of the board, and the integer , find the expected number of connected components in this graph.
(Please see the problem statement for further clarification)
- Grid / Checker-board
- Grids bipartite graph?
- DP?
- Matching?
- Probability / Expectation / Linearity of Expectation, etc.
- Random Permutation (i.e.: order doesn't matter)
- MST / Minimum Spanning Tree?
- Forests with Cycles
Notation: If rabbit is the closest to rabbit (under the tie-breaking rules), and we add an edge between rabbit and rabbit , then we say points to .
IdeaConsider the cycle-structure of the graph
This section describes some key observations needed to come to the solution. For a concise description of the final algorithm, you may skip this and go to the Solution Summary below. Otherwise, please continue reading if you would like to follow the problem-solving process.
Looking at examples or drawing some pictures of the graphs are extremely useful for understanding the problem and finding obvious patterns. In this case, it was fairly helpful; drawing a few examples of boards (the sample test-cases) and their related graphs led to the following observation.
Every component of the graph will contain exactly one cycle.
Start with a single rabbit. Follow its "closest-rabbit" edge to another rabbit. Then follow this rabbit's "closest-rabbit" edge. And so on... Eventually you will run out of unique rabbits, so the process must cycle. This shows that every component has at least one cycle. And a similar argument shows that there is no more than one cycle in each component. (See the Related Problems section below for other problems / theorems related to this concept)
Note that the graph always has edges and nodes. Any graph with this property must consist of a collection of components, each containing exactly one cycle. Knowing this requires having some experience with Graph Theory.
From the above observation, we get a good characterization:
The number of components is exactly equal to the number of cycles in the graph.
At this point, I began looking at the structure of the cycles (based on the above characterization). Intuitively, based on the fact that the edges are to the "closest" neighbours, it seemed like there can't be very long cycles. For example, consider a triangle of rabbits (a cycle of length three) . Then is the closest neighbour of , and is the closest neighbour of , and is the closest neighbour of . Intuitively, it seems like they all must be closest neighbours of each other. It has to be an equilateral triangle.
We can actually formalize and prove this.
Consider any cycle in the resulting graph. Then all the rabbits/nodes on this cycle must have the same distance to each of the other rabbits.
Suppose we have a cycle of nodes . Without loss of generality, assume that points to who points to and so on (where "points to" is defined above). Then is the closest neighbour to . In particular, this tells us that (where is the euclidean distance between the corresponding cells of and ). Similarly, is the closest neighbour to , so we know that .
In general, we get that . Applying all of these inequalities we get: . But we have already stated that , so this tells us that and (by symmetry) , and so on.
Altogether, this says that all edges must be equal on this cycle.
This shows some nice structure to the graph. All the cycles are "equilateral". Based on this, intuitively, it seems like there cannot be very long cycles. For example, have you ever seen a four-sided figure (in two dimensions) where all the points are the same distance to each other? Also, even a triangle doesn't seem to work because of the "always pick the neighbour with the lowest row" rule (stated in the problem-statement). Altogether, we have the following key observation.
No cycle in the graph can have more than two nodes on it.
This is actually a result of the previous lemma. Suppose you have any cycle. Consider the node in the cycle whose cell has the lowest row in the original grid (and lowest column, in case of ties). Then all other nodes on this cycle would "point to" this node (by the tie-breaking rule). This node would point only to one other node, so there can only be a cycle of two.
I wasn't immediately sure about whether this was true. I simply wrote it down as a "hunch" (an intuition), and informally proved it afterwards. This is sometimes useful to explicitly write down your lemmas.
These observations about the cycles are really nice. Altogether we have discovered that:
- There is exactly one cycle per component, so
- The number of components is equal to the number of cycles; and
- Every cycle must have exactly two nodes on it
So we can make the final observation:
The number of components is exactly equal to the number of pairs of rabbits that mutually point to each other.
Follows from everything prior.
We have transformed the problem from a question of "components" to a question about "edges".
These are really beautiful observations. There are still a few steps needed to take this idea to a final solution, but I encourage the reader to attempt to solve the problem now if you have read this far!
IdeaProbability and the Linearity of ExpectationAC
Experience solving probability problems is (obviously) extremely helpful in solving this problem. You should likely be familiar with the terms: "Random Variable", "Sample Space", "Expected Value / Expectation", and so on. Also, one of the most powerful results from Probability theory is the "Linearity of Expectation" rule. In particular, if and are two random variables, (where denotes expected value). Many problems can be solved using these fundamental techniques.
If you have no idea about any of these terms, consult Introduction to Algorithms by Thomas H. Cormen, et al. Appendix C (of the Third Edition, at least) has a very nice review of Probability and Counting.
Based on this "experience", the next step in this problem is to try and compute this Expectation (i.e.: the Expected number of components in the graph) using these probability techniques
We want to find the expected number of components in the graph. It would be nice if we could break this number down into the sum of other random variables, and apply the Linearity of Expectation. To begin, we make the following key observation (note: this is the key result of the previous Idea Section):
The number of components in the final graph is exactly equal to the number of pairs of rabbits that mutually point to each other.
Omitted. See the previous Idea Section if you would like to see the proof.
This gives us a nice characterization of the solution. To further exploit this, let's introduce some notation.
Consider the final arrangement of rabbits. We let the random variable be the number of components in the resulting graph.
Now, the rabbits are basically symmetric. If two rabbits mutually point to each other, then it is because they are in two cells with no other rabbits in a closer cell (to either one). So we get the following:
Consider the final arrangement of rabbits. Let and be two empty cells. And let denote the "indicator" for whether the rabbits in and consider each other to be mutually closest. In other words:
Note: By convention, if there is no rabbit in or if there is no rabbit in .
With these two definitions, we can now write out the formula for the expectation:
It now remains to solve a simpler problem: how do we find for two empty cells and ?
This can actually be done by counting.
If we have two cells and , consider all the cells that would be close enough to or to cause either or to point to . If there is a rabbit in any of these cells , then this will cause either or to point away from each other (to ), and they will not form the two-cycle. Otherwise, if there is no rabbit in any of these cells , then and must point to each other, and form the two-cycle.
So, the variable will be if and only if none of these cells are assigned a rabbit!
How do we find these cells? Well, by manually checking! We can check all other empty cells against and . For each cell , we check if it is close enough (or causes a tie to be broken in a certain way) so that would point to if had a rabbit in it. This will generate a set of "bad points". Then the probability of is exactly the probability that this set has no rabbits in it. If there are bad cells, and is the set of empty cells in total (including and ), then there are
ways of assigning rabbits to keep them empty (and to make sure and are occupied). And there are
ways in total of assigning rabbits randomly.
So altogether, we get the following lemma.
For any two empty cells :
where is the number of rabbits, is the set of empty cells, and is the number of bad cells for the pair .
See previous key-observation. We also use the fact that (for variables)
Altogether, this actually gives us our final algorithm (after combining this result with the corollary above): For each pair of empty cells and , compute the probability that there are rabbits in and and that they point to each other (this from the formula above). Sum these up to get the final answer. See the Solution Summary below for the final pseudo-code and analysis.
Review
Summary
Most of my initial observations were useless (although they were not necessarily incorrect). The most helpful ones ended up being the later ones, which I summarize as : "We have to use probability tricks, along with exploiting the structure of the graph."
With most probability problems, there is often a "random variable" whose expectation we are trying to find. Often-times by writing it out, you can find nice patterns. Usually, this one "random variable" turns into the sum of many smaller / simpler random variables (often variables that can only be equal to or ). You can then find the original expected-value as the sum of expected values of these variables (the "Linearity of Expectation"). (See below for more resources on probability problems).
First, we must be able to write down the random variable (the number of components) in a nice form. We start by looking at the structure of the components; and we notice that, because the graph has nodes and edges (where is the number of rabbits), every component of the graph contains a single cycle with "arms" (paths) attached. (Drawing a picture helps here.)
Upon further inspection, we notice that there cannot be any "long" cycles in this graph. This is because each edge corresponds to a "closest neighbour" for some node / rabbit, and we can prove that all the nodes on a given cycle must mutually be same distance to each other. (This was proved in Idea 1 above.) Because of the "tie-breaking" rule, if all of these nodes are equidistant to each other then they would all pick the same node. Using this argument we can show that there won't be a cycle of length three or more, because this would lead to some kind of contradiction. So every cycle has length exactly two (as there are no self-edges either).
The above arguments tell us: 1) There is exactly one cycle in every component; and 2) Every cycle has length exactly two (i.e.: it is a pair of nodes, that share a duplicate-edge). Altogether, this is enough to yield the characterization we need.
Let denote the number of cycles (this is a random variable that depends on the final arrangement of rabbits). For a pair of rabbits in cell and cell , we define if and only if the two rabbits are mutually closest to each other, or we set otherwise. Based on the above arguments, in any configuration, we have that over all pairs of distinct empty cells and . Since our problem is to find (the expected value of ), we can now (finally) use the Linearity of Expectation to get that:
over all pairs of distinct empty cells and .
It is fairly easy to compute by counting. Particularly, for two cells and , consider a cell so that would point to instead of if both cells have a rabbit (for simplicity, we say that prefers over ). If has a rabbit on it, then (obviously, since does not point to ). A similar truth holds if prefers over . On the other hand, if we cannot find any such cell then must be 1, since and must point to each other. So altogether, if we let , then we can write as:
where is the set of all empty cells, is the set as described now, and is the number of rabbits. Note that the denominator is just the total number of configurations, and the numerator is the number of configurations that result in and mutually pointing to each other.
Altogether, our algorithm is to sum up the values and return this number. For a fixed we only need to find the set (as described above). This can be done by manually checking all other empty cells to see if either or prefer . Here is the pseudo-code.
<code class="python">
choose(n,k):
return n! / k! / (n-k)!;
Y(a, b):
if (a==b): return 0
bad_cells = 0
for each empty cell c:
# Check if a prefers c over b or if b prefers c over a
a_prefers_c = false
b_prefers_c = false
if distance(a,c) < distance(a,b) or (c beats b in the tie-breaker): a_prefers_c = true
if distance(b,c) < distance(b,a) or (c beats a in the tie-breaker): b_prefers_c = true
if a_prefers_c or b_prefers_c:
bad_cells ++
S := (total empty cells including a and b)
R := (total number of rabbits including those on a and b)
return (double) choose(S-2-bad_cells, R-2) / choose(S,R)
getExpected(board, R):
answer = 0.0
for each pair (a,b) of empty cells in board:
answer += Y(a,b)
return answer
</code>Where the answer is
getExpected(board,R).
The running time of the algorithm is , since we essentially check every triplet of cells once in the worst case. Also, be careful about precision. I would NOT use the "choose" function as described above, and also make sure to use doubles or long doubles, because the numbers can get pretty large.
- There is exactly one cycle in each component. This comes from the structure of the graph. Also, any graph with nodes and edges must have this property (I think).
- No cycle can contain more than two nodes. This can be found by exploiting the symmetry of cycles and the "nearest neighbour" graph.
- Rabbits in cells and are mutually "closest" if and only if there is no other cell such that prefers or prefers .
- **Let be the set of empty cells. Let be a pair of distinct empty cells. Let be as defined earlier. Let be the number of components, then , and