1

In Probability of opening all piggy banks the following question was asked.

We have $n$ keys and $n$ piggy banks. Each key fits only one piggy bank. We randomly put exactly one key in each piggy bank. Then we randomly shatter $1≤k≤n$ piggy banks. What is the probability that we are able to open all other piggy banks?

the answer was shown to be $k/n$. my question is the following.

Question. We have $n$ keys to $n$ piggy banks, and we randomly put each key in a piggy bank, with repetitions allowed. is it still true that if we shatter $k$ piggy banks the probability that we are able to open all the others is $k/n$?

tomm
  • 293

1 Answers1

1

As the answers to my question on Puzzling SE show, the probability is still $k/n$. Allow me to summarize my two favorite solutions.

Tyler Seacrest's solution

To make the problem easier, we make it more general. Suppose that the $n$ keys are distributed arbitrarily into $n$ pouches, possibly with multiple keys in one pouch, possibly even maliciously distributed. These pouches are then placed into the boxes randomly, one pouch per box. When you open $k$ boxes, what is the probability they keys from the pouches you get allow you to open the rest?

If the keys happen to be distributed into all different pouches, then this just becomes the original problem that was solved with Knuth's canonical cycle representation. Otherwise, there will exist at least one empty pouch, in some box $B$. Box $B$ is now useless to us, so we ignore box $B$ and its key. This leaves $n-1$ boxes, each with a pouch, with $n-1$ keys total, allowing us to proceed by induction. If $B$ was one of the open boxes, we have only effectively opened $k-1$ boxes. If $B$ was not opened, then we have effectively opened $k$ boxes. Therefore, $$ \begin{align} P(\text{open all}) =\,\,\,&P(\text{open all}\mid B\text{ opened})P(B\text{ opened}) +P(\text{open all}\mid B\text{ not opened})P(B\text{ not opened})\\ &= \frac{k-1}{n-1}\cdot \frac{k}n+\frac{k}{n-1}\cdot \frac{n-k}{n}=\frac kn. \end{align} $$ This works for any distribution of keys into pouches, including a random distribution, therefore solving your problem.

My solution

Just like the original problem used a clever representation of permutations, this modified problem can be solved with a clever representation of arbitrary functions. Let $[n]$ denote $\{1,\dots,n\}$. Any function $f:[n]\to [n]$ will be represented as a list $L=(L_1,\dots,L_n)$ of length $n$ with entries between $1$ and $n$.

The first part of this bijection resembles the Prüfer sequence which encodes labeled trees. Maintain a set $D$, where initially $D$ is the entire set $[n]$. First, find the smallest number $k_1\in D$ which is not in the image $f(D)$, and set $L_1=f(k_1)$. Then, remove $k$ from $D$, and repeat that same process using the smaller $D$, finding the smallest $k_2\notin f(D)$ and setting $L_2=f(k_2)$, and so on. This continues until $f(D)$ is all of $D$, meaning that $f$ restricted to $D$ is a bijection. Call this list we have found so far $P$ (for Prüfer).

Since $f:D\to D$ is now a bijection, we can compute its canonical cycle representation, call it $K$ (for Knuth). Recall this is a list of length $|D|$ of distinct elements of $D$. This list is found by computing the cycles of $f$, writing each cycle with its smallest element first, lining up the cycles in decreasing order of their smallest elements, then concatenating them into one list.

Finally, the bijection we will use is $$ L=P\oplus \text{reverse}(K),\qquad \oplus \text{ is concatenation} $$ First, let me say why this is useful. If we imagine the boxes are numbered $1$ to $n$, and the boxes initially opened are the ones numbered $1$ to $k$, then all boxes can be opened if any only if every cycle of $f$ contains a number between $1$ and $k$. Just as before, this occurs if and only if the last entry of $L$ (which is the first entry of $K$) is between $1$ and $k$. Since this correspondence between functions and lists is bijective, and $f$ is a random function, $L$ is a random list, so the last entry of $L$ is between $1$ and $k$ with probability $k/n$.

To show the correspondence $f\mapsto L$ is bijective, we just need to show how to recover $f$ from $L$. Given $L$, you can easily recover $P$ and $K$, because $K$ is the reverse of the longest suffix of $L$ whose entries are distinct. Using $K$, you can recover the cycles of $f$, as in the other solution, so you now know $f(x)$ for all $x$ appearing in $K$. For the rest, we simply reverse the procedure used to construct $P$. Initially a set $D$, initially all of $[n]$. Repeatedly find the smallest element $k$ of $D$ not appearing in $L$, set $f(k)=L_1$, and remove $k$ from $D$.

Mike Earnest
  • 84,902