Brain teaser: locks, keys and gifts

Yesterday evening, a brain teaser was spread into Yao class WeChat group. Originally, the provider of the problem didn’t tell us the source of the problem. When asked, he said that the problem was asked by someone, and that she (she’s a she) didn’t provide an answer. With repeated request of recursive source tracing, we finally got the source of the problem so that I could cite it here. The problem is the technical problem of application to SPARC. You might be able to find the problem on this Google Docs form before the form is closed. Here’s a transcription:

Question 3: Locks, Boxes, Gifts, and Friend *

Suppose that for $1 it is possible to purchase two keys and two matching locks. Either key can be used to open either lock, but once a key is used, the key is destroyed. You also have access to an unlimited supply of boxes of every size, each of which can be secured with a single lock. You have 100 different gifts to give to a friend. You want to lock up the items in such a way that your friend can get whichever set of 50 items they choose, but can’t access any set of 51 items. You make some locks, put them on some boxes (some of which contain your items), and give your friend some of the keys and all the boxes. How few dollars can you spend and still accomplish your task?

What we’ve found

Mathematical/computer science methodology mandates us to:

  • think the problem in a general-value setting, where 100, 50 is replaced by 2n,n2n,n;
  • try to achieve optimum by progressively approaching it, which means we want first a construction then some optimisation, and first asymptotic bound then concrete bound.

I did my first trial on smaller numbers. For 1-out-of-2, this is easy: just pack the two items in two boxes, locked with the same locks, and throw away one key. For 2-out-of-4, it is also straightforward to: pack item 3, 4 in two boxes locked with lock 0, pack item 1 [resp. 2] in a box locked with lock 1 [resp. 2], pack the two keys to lock 0 in two boxes, locked with lock 1 and lock 2, respectively, and finally throw away one key to lock 1 and one key to lock 2. If your friend wants item 1 and item 2, he can just open the boxes with their corresponding keys. If your friend wants item 3 or item 4, he must abandon one of item 1 and item 2, which in turn allows him to open a box containing the key to lock 0, which indeed allows him to acquire item 3 or item 4. For 3-out-of-6, it takes some time. The reader is invited to check the correctness of the illustration shown below. The colour of each box represents the lock on it, and the colour of each key represents the lock it opens. (I am sorry that it won’t work for those with visual impairment.)

One possible solution to 3-out-of-6
One possible solution to 3-out-of-6

These constructions are quite ad-hoc, as they employ the specialty of small numbers that are hard to extend.

In the WeChat group, Kefan Lyu mentioned that one could virtually duplicate a key by locking it in a box and using the two keys to the box (the other lock is thrown away). It is also natural (mentioned by Wenhao Hong) that one can put boxes inside boxes. These two techniques already give a solution.

Viability Construction Since there are (2nn)\binom{2n}{n} possible outcomes, one can build a decision tree by putting boxes in boxes and use appropriate locks and keys (throw away one key to ensure choice of only one path), and let’s call the leaf boxes the “final decision boxes”, where we will put keys corresponding to that nn-subset. One stores the items in boxes with different locks, and duplicate their keys until there are sufficient ones to put into the “final decision boxes”.

The consumption of money of this solution is Ω((2nn))\Omega\left(\binom{2n}{n}\right). Not quite satisfactory.

Polynomial Solution I mentioned in the WeChat group that we could consider a gadget that allows one to choose 1 key out of 2n2n, using a decision tree. We need nn such gadgets. This way, we only need nn keys to each item. The consumption is O(n2)\mathcal{O}\left(n^2\right). I initially thought it would be O(nlogn)\mathcal{O}\left(n\log n\right), but actually it is not. Currently this solution is the best we can do in the asymptotic sense. See Update later today for better solution.

Later, there were discussions on using divide-and-conquer or partitioning (blockwise processing) to further reduce the consumption. However, they were found no good. Somehow they failed because we could duplicate keys, not locks or the items.

I think the idea of this problem is intuitively similar to that of kk-out-of-nn OT.

Chat show program ingredients

During the discussion in the WeChat group, we sometimes might come up with some humour lines. These are something that deviates from the academic setting, which is often about considering the part of a concrete real-world situation that has been abstracted away from the problem. Someone coined the term “chat show program ingredients” (综艺节目元素) for this. Here’s one:

Can I lock my friend in a box?

Here’s another:

A Since (10050)\binom{100}{50} isn’t a power of two, you might as well adversarially make your friend end up with nothing.

B You can also talk your friend into opening up the wrong box.

C Shouldn’t we assume transparency of the boxing scheme?

D Let’s not mix in chat show program ingredients!

And here’s a third one:

A If you assume your friend is stupid, cure him first.

B Should we assume all the boxes that can be opened in a choice will be opened?

Me Let’s for sure not mix in char show program ingredients!

Update later today

A discussion later today revealed better constructions. Let’s talk about Kefan Lyu’s O(n1.5)\mathcal{O}\left(n^{1.5}\right) construction first.

Kefan Lyu’s construction

For the ease of notation, let’s consider nn items. Group the items into gg groups so that each group has n/gn/g items.

For each group, we construct a gadget that allows us to make a choice out of n/gn/g, where the choice represents the item chosen from this group. For each group, we need n/gn/g such gadgets. Each gadget costs n/gn/g keys and needs one equivalent key to each item in the group. Therefore, the cost for each group is Θ(n2/g2)\Theta\left(n^2/g^2\right). Since we have gg groups, the cost for this part is Θ(n2/g)\Theta\left(n^2/g\right).

Let Gi,jG_{i,j} be the jj-th copy of gadget for group ii. We build the following n/2n/2 gadgets C1,...,n/2C_{1,...,n/2}:

  • Choose one out of G1,1,...,Gg,1G_{1,1},...,G_{g,1};
  • Choose one out of G1,2,...,Gg,2G_{1,2},...,G_{g,2};
  • ……
  • Choose one out of G1,n/g,...,Gg,n/gG_{1,n/g},...,G_{g,n/g};
  • Choose one out of G1,1,...,Gg,1G_{1,1},...,G_{g,1};
  • Choose one out of G1,2,...,Gg,2G_{1,2},...,G_{g,2};
  • ……
  • Choose one out of G1,n/g,...,Gg,n/gG_{1,n/g},...,G_{g,n/g};
  • ……

If the GG’s are secured, the friend clearly cannot access more than n/2n/2 items.

Let the chosen items from each group form the set SiS_i, then Sin/g\left|S_i\right|\leq n/g. Since any consecutive n/gn/g gadgets in CC allows us to choose distinct gadgets for one group, the friend can simply:

  • Choose the G1,G_{1,\star} branch in the first S1\left|S_1\right| gadgets in CC;
  • Choose the G2,G_{2,\star} branch in the following S2\left|S_2\right| gadgets in CC;
  • ……
  • Choose the Gg,G_{g,\star} branch in the last Sg\left|S_g\right| gadgets in CC.

I.e., the construction allows the friend to choose any n/2n/2 items he wants.

The cost of building these gadgets is Θ(ng)\Theta\left({ng}\right), including the cost of duplicating keys to secure GG’s. Therefore, the total cost is Θ(ng+n2/g)\Theta\left({ng+n^2/g}\right), and letting g=Θ(n)g=\Theta\left(\sqrt n\right) gives the optimum among this family of constructions, Θ(n1.5)\Theta\left(n^{1.5}\right).

Quasilinear construction

Following Kefan’s idea, we can further achieve better bounds by nesting groups. For example, grouping items into small groups and small groups into large groups gives us an O(n4/3)\mathcal{O}\left(n^{4/3}\right) construction. If we group at k=O(logn)k=\mathcal{O}\left(\log n\right) levels, we achieve O(kn1+1/k)\mathcal{O}\left(kn^{1+1/k}\right). By doing exactly Θ(logn)\Theta\left(\log n\right) levels of grouping (equivalently, making the size of each group constant), we achieve O(nlogn)\mathcal{O}\left(n\log n\right).

The construction can also be understood as index permutation of FFT.

This is really a compelling bound. To consider a lower bound, if we use tt dollars, we will have at most 2t2^t different outcomes. By Sitrling’s formula,(2nn)=2Θ(n),\binom{2n}{n}=2^{\Theta\left(n\right)},we need at least Θ(n)\Theta\left(n\right) keys. There’s still a gap there.

Please enable JavaScript to view the comments powered by Disqus.