← All problems

ClosestRabbit

TopCoder · SRM 636 (Div. 1) · 500 Pt

Problem

We are given an N×MN \times M rectangular board, where each cell is either empty or full (1N,M201 \leq N,M \leq 20). We define the distance between cells (i1,j1)(i_1,j_1) and (i2,j2)(i_2,j_2) to be (i1i2)2+(j1j2)2(i_1-i_2)^2 + (j_1-j_2)^2.

In addition, there are RR 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 RR nodes, each node corresponding to a different rabbit. For each rabbit aa we add an edge to the closest rabbit bb. More specifically, if a rabbit is assigned to cell (i1,j1)(i_1,j_1), we consider the rabbit who is assigned to (i2,j2)(i_2,j_2) such that the distance between (i1,j1)(i_1,j_1) and (i2,j2)(i_2,j_2) 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 (i2,j2)(i_2,j_2) is lexicographically smallest. We do this for each rabbit (in particular, there are exactly RR 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 RR, find the expected number of connected components in this graph.

(Please see the problem statement for further clarification)

Initial Observations
  1. Grid / Checker-board
  2. Grids \Rightarrow bipartite graph?
  3. DP?
  4. Matching?
  5. Probability / Expectation / Linearity of Expectation, etc.
  6. Random Permutation (i.e.: order doesn't matter)
  7. MST / Minimum Spanning Tree?
  8. Forests with Cycles

Notation: If rabbit yy is the closest to rabbit xx (under the tie-breaking rules), and we add an edge between rabbit xx and rabbit yy, then we say xx points to yy.

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.

Work an Example

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.

Lemma.

Every component of the graph will contain exactly one cycle.

Proof.

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)

From Experience

Note that the graph always has RR edges and RR 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:

Corollary.

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) [a,b,c][a,b,c]. Then bb is the closest neighbour of aa, and cc is the closest neighbour of bb, and aa is the closest neighbour of cc. 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.

Lemma.

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.

Proof.

Suppose we have a cycle of nodes [v1,v2,...,vk,v1][v_1,v_2,...,v_k,v_1]. Without loss of generality, assume that v1v_1 points to v2v_2 who points to v3v_3 and so on (where "points to" is defined above). Then v2v_2 is the closest neighbour to v1v_1. In particular, this tells us that v1v2v1vk|v_1v_2| \leq |v_1v_k| (where vivj|v_iv_j| is the euclidean distance between the corresponding cells of viv_i and vjv_j). Similarly, v3v_3 is the closest neighbour to v2v_2, so we know that v2v3v1v2|v_2v_3| \leq |v_1v_2|.

In general, we get that vivi+1vi1vi|v_iv_{i+1}| \leq |v_{i-1}v_i|. Applying all of these inequalities we get: v1v2vkv1vk1vk...v2v3|v_1v_2| \leq |v_kv_1| \leq |v_{k-1}v_k| \leq ... \leq |v_{2}v_3|. But we have already stated that v2v3v1v2|v_2v_3| \leq |v_1v_2|, so this tells us that v1v2=v2v3|v_1v_2| = |v_2v_3| and (by symmetry) v2v3=v3v4|v_2v_3| = |v_3v_4|, 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.

Corollary.

No cycle in the graph can have more than two nodes on it.

Proof.

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.

Write It Out

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:

  1. There is exactly one cycle per component, so
  2. The number of components is equal to the number of cycles; and
  3. Every cycle must have exactly two nodes on it

So we can make the final observation:

Key Observation

The number of components is exactly equal to the number of pairs of rabbits that mutually point to each other.

Proof.

Follows from everything prior.

Transform the Problem

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
From Experience

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 XX and YY are two random variables, E[X+Y]=E[X]+E[Y]E[X+Y] = E[X] + E[Y] (where E[]E[\cdot] 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):

Lemma.

The number of components in the final graph is exactly equal to the number of pairs of rabbits that mutually point to each other.

Proof.

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.

Definition.

Consider the final arrangement of rabbits. We let the random variable CC 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:

Definition.

Consider the final arrangement of rabbits. Let pp and qq be two empty cells. And let YpqY_{pq} denote the "indicator" for whether the rabbits in pp and qq consider each other to be mutually closest. In other words:

Ypq={1:if the rabbit in cell p points to the rabbit in cell q and vice-versa0:otherwiseY_{pq} = \left\{\begin{array}{ll} 1 & : \text{if the rabbit in cell $$p$$ points to the rabbit in cell $$q$$ and vice-versa} \\ 0 & : \text{otherwise} \end{array}\right.

Note: By convention, Ypq=0Y_{pq} = 0 if there is no rabbit in pp or if there is no rabbit in qq.

With these two definitions, we can now write out the formula for the expectation:

answer=E[C]=pS  qS,qpE[Yab]\text{answer} = E[C] = \sum_{p \in S} \ \ \sum_{q \in S, q \neq p}{E[Y_{ab}]}
Simplify

It now remains to solve a simpler problem: how do we find E[Yab]E[Y_{ab}] for two empty cells aa and bb?

Key Observation

This can actually be done by counting.

If we have two cells aa and bb, consider all the cells cc that would be close enough to aa or bb to cause either aa or bb to point to cc. If there is a rabbit in any of these cells cc, then this will cause either aa or bb to point away from each other (to cc), and they will not form the two-cycle. Otherwise, if there is no rabbit in any of these cells cc, then aa and bb must point to each other, and form the two-cycle.

So, the variable YabY_{ab} will be 11 if and only if none of these cells cc are assigned a rabbit!

How do we find these cells? Well, by manually checking! We can check all other empty cells against aa and bb. For each cell cc, we check if it is close enough (or causes a tie to be broken in a certain way) so that aa would point to cc if cc had a rabbit in it. This will generate a set of "bad points". Then the probability of Pr(Yab=1)Pr(Y_{ab} = 1) is exactly the probability that this set has no rabbits in it. If there are BB bad cells, and SS is the set of empty cells in total (including aa and bb), then there are

(S2BR2)\binom{|S|-2-B}{R-2}

ways of assigning RR rabbits to keep them empty (and to make sure aa and bb are occupied). And there are

(SR)\binom{|S|}{R}

ways in total of assigning RR rabbits randomly.

So altogether, we get the following lemma.

Lemma.

For any two empty cells a,bSa,b \in S:

E[Yab]=(S2BR2)(SR)E[Y_{ab}] = \frac{\binom{|S|-2-B}{R-2}}{\binom{|S|}{R}}

where RR is the number of rabbits, SS is the set of empty cells, and BB is the number of bad cells for the pair a,ba,b.

Proof.

See previous key-observation. We also use the fact that E[Yab]=Pr(Yab=1)E[Y_{ab}] = Pr(Y_{ab} = 1) (for 010-1 variables)

Altogether, this actually gives us our final algorithm (after combining this result with the corollary above): For each pair of empty cells aa and bb, compute the probability that there are rabbits in aa and bb and that they point to each other (this E[Yab]E[Y_{ab}] 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 00 or 11). 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 RR nodes and RR edges (where RR 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 CC 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 aa and cell bb, we define Yab:=1Y_{ab} := 1 if and only if the two rabbits are mutually closest to each other, or we set Yab=0Y_{ab} = 0 otherwise. Based on the above arguments, in any configuration, we have that C=YabC = \sum Y_{ab} over all pairs of distinct empty cells aa and bb. Since our problem is to find E[C]E[C] (the expected value of CC), we can now (finally) use the Linearity of Expectation to get that:

E[C]=E[Yab]E[C] = \sum E[Y_{ab}]

over all pairs of distinct empty cells aa and bb.

It is fairly easy to compute E[Yab]=Pr(Yab=1)E[Y_{ab}] = Pr(Y_{ab} = 1) by counting. Particularly, for two cells aa and bb, consider a cell cc so that aa would point to cc instead of bb if both cells have a rabbit (for simplicity, we say that aa prefers cc over bb). If cc has a rabbit on it, then Yab=0Y_{ab} = 0 (obviously, since aa does not point to bb). A similar truth holds if bb prefers cc over aa. On the other hand, if we cannot find any such cell cc then YabY_{ab} must be 1, since aa and bb must point to each other. So altogether, if we let B:={c:a prefers c or b prefers c}B := \{c : a \text{ prefers } c \text{ or } b \text{ prefers } c\}, then we can write Pr(Yab=1)Pr(Y_{ab} = 1) as:

E[Yab]=(S2BR2)(SR)E[Y_{ab}] = \frac{\binom{|S|-2-B}{R-2}}{\binom{|S|}{R}}

where SS is the set of all empty cells, BB is the set as described now, and RR is the number of rabbits. Note that the denominator (SR)\binom{|S|}{R} is just the total number of configurations, and the numerator is the number of configurations that result in aa and bb mutually pointing to each other.

Altogether, our algorithm is to sum up the E[Yab]E[Y_{ab}] values and return this number. For a fixed YabY_{ab} we only need to find the set BB (as described above). This can be done by manually checking all other empty cells cc to see if either aa or bb prefer cc. Here is the pseudo-code.

Algorithm
<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 Θ((NM)3)\Theta((NM)^3), 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.

  1. There is exactly one cycle in each component. This comes from the structure of the graph. Also, any graph with nn nodes and nn edges must have this property (I think).
  2. No cycle can contain more than two nodes. This can be found by exploiting the symmetry of cycles and the "nearest neighbour" graph.
  3. Rabbits in cells aa and bb are mutually "closest" if and only if there is no other cell cc such that aa prefers cc or bb prefers cc.
  4. **Let SS be the set of empty cells. Let a,ba,b be a pair of distinct empty cells. Let BB be as defined earlier. Let CC be the number of components, then E[C]=E[Yab]E[C] = \sum E[Y_{ab}], and
E[Y_{ab}] = \frac{\binom{|S|-2-B}{R-2}}{\binom{|S|}{R}} $$** 1. The first couple of key observations can easily be found by looking at example grids and/or example graphs. 2. Alternatively, you can find out these observations by focusing on the constraints (the structure of the closest-neighbour function) 3. Knowledge of Graph Theory is pretty useful here in being able to recognize the structure of the graph. Also, knowledge of Probability (such as Random Variables, Linearity of Expectation) was crucial to being able to write down the final solution. 4. With probability problems, it's often helpful to wrote down the random variables to find out exactly what the "expected value" corresponds to. Then we can use other techniques to modify and simplify the formula. It's also useful to write down every little observation you come across; they can be useful later. 5. We transformed our problem on components to a problem on cycles. Later, we transformed this to a problem of pairs of vertices. 6. I would say the entire "Linearity of Expectation" rule is an example of simplifying a problem: instead of trying to find $$E[C]$$, we broke it down into smaller variables: $$Y_{ab}$$ for each the expected values were easy to compute. This often works in general. - This is the second time (for me) that I have come across a problem with Probability and Graph Theory in the last little while (I will write the other problem in the "Related Problems" section below). Both times, the solution was to reduce the Expected Value of one variable to the sum of expected values over pairs of nodes (i.e.: over potential edges in the graph). Maybe this is common? - Linearity of Expectation is extremely powerful. Whenever you can write one random variable as the sum of other random variables: do it! It often reduces to the sum of "0-1" variables (often called "Indicator Variables"), and we can exploit the fact that $$E[X] == Pr(X=1)$$ for 0-1 variables. - Probability / Expectation / Expected Value / Linearity of Expectation / 0-1 Indicator Variables - Combinatorics / Counting / Number of Ways / Binomial Coefficients / Counting with Probability - Graph Theory / Graph Construction / Closest Neighbour - Random Graphs / Random Graph Construction - Greedy / Observations / Exploit Structure and Conditions - Math / Formulas and Proofs [TopCoder SRM 503 (Div. 1), Level Two - KingdomXCitiesandVillages.](http://community.topcoder.com/stat?c=problem_statement&pm=11063) An *amazing* follow-up to this problem. It is another Graph Construction problem with random choices. You are asked to find the expected total length of all edges after a graph is constructed. [TopCoder SRM 627 (Div. 1), Level Two - GraphInversions.](http://community.topcoder.com/stat?c=problem_statement&pm=13275) Another problem dealing with a graph with $$N$$ vertices and $$N$$ edges. The graph is connected. Can you exploit the structure of the graph to solve the problem? [TopCoder SRM 611 (Div. 1), Level Two - Egalitarianism2.](http://community.topcoder.com/stat?c=problem_statement&pm=13008) A nice problem dealing with Graph Theory and "statistical computation". This time you are dealing with "Standard-Deviation" rather than "Expectation". And it is a "minimization" problem, rather than a counting problem. [HackerRank - Connect the Country.](https://www.hackerrank.com/challenges/connect-the-country) One of the earliest problems I remember solving that dealt with Probability and Graphs. This is a similar problem and it is very good practice. [ACM ICPC NWERC 2009, Problem G - Room Assignments.](https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=362&page=show_problem&problem=2616)A really hard problem. Can you represent the *people* and the *rooms* in terms of a graph? What kind of graph is this? What are some other properties of this graph? Are there any similarities to the present problem? What about differences? How do we compute the probabilities/expectations? Can we even compute them? How do we ensure that the organizer maximizes expected rating? Even simpler, how do we even ensure that the organizer picks a valid pair of rooms?