A code lock has 10 keys labeled by digits from 0 to 9. The lock is opened by entering a code of 4 keys that are not necessarily different. It is opened as soon as the correct keys are pressed consequently, no matter what keys have been pressed earlier. For example, if the code happens to be , the sequence will open the lock, but the sequence will not.
What is the minimal length of a sequence that will guarantee opening the lock regardless of what code is?
Solution
Answer: .
We need to find the shortest sequence of decimal digits that includes every 4-digit sequence as a (consecutive) subsequence. Since there are possible codes, and an -digit sequence has at most distinct 4-digit subsequences, the answer must be at least .
We will construct a sequence of length that fulfills the condition. We define a directed graph whose vertex set consists of all 3-digit sequences. The edge set contains all ordered pairs of the form . (This implies that also pairs are edges.) The in-degree of a vertex is defined as the number of edges directed toward the vertex, obviously, the in-degree of every vertex in is . Symmetrically, the out-degree of every vertex is also . Note also that is (strongly) connected.
By a well-known argument, a connected directed graph has an Euler circuit if and only if the in-degree of any vertex equals its out-degree. We can now construct the required sequence as follows. Start from any vertex, say , and begin the sequence by the digits of that vertex. Follow the Euler circuit, and at each edge add to the sequence. It is easily proved by induction that after the edge has been followed, the four last digits of the sequence are . Since the Euler circuit passes through every edge, the resulting sequence includes every 4-digit sequence. There are vertices, each of which is the starting point of edges, so all in all there are edges. Thus the length of the sequence is as required.