3

I'm trying to find an algorithm which "breaks the safe" by typing the keys 0-9. The code is 4 digits long. The safe will be open where it identifies the code as substring of the typing. meaning, if the code is "3456" so the next typing will open the safe: "123456". (It just means that the safe is not restarting every 4 keys input).

Is there an algorithm which every time it add one digit to the sequence, it creates new 4 digits number (new combinations of the last 4 digits of the sequence\string)?

thanks, km.

Editing (I post it years ago): The question is how to make sure that every time I set an input (one digit) to the safe, I generate a new 4 digit code that was not generated before. For example, if the safe gets binary code with 3 digits long then this should be my input sequence:

0001011100 

Because for every input I get a new code (3 digit long) that was not generated before:

000 -> 000
1 -> 001
0 -> 010
1 -> 101
1 -> 011
1 -> 111
0 -> 110
0 -> 100
elirandav
  • 1,913
  • 19
  • 27
  • 1
    What language? Platform? Throw us a bone. – TheZ Jul 03 '12 at 19:34
  • can't you just "remember" the last 3 chars ? – Nir Alfasi Jul 03 '12 at 19:35
  • @Thez, exactly as 'gbtimmon' noted, it doesn't matter. I'm looking for an abstract idea. (although solution in code is not bad at all) – elirandav Jul 03 '12 at 19:42
  • @Thez, I think this is a purely algorithmic question, which means platform and language would be moot points. I think he asking how to generate every 4 digit combination, by writing one string and repeating no 4 digit combinations. A sample solution for 3 digits with the numbers 0-1 might be '1000101110'. It a very interesting question. Is this some kind of acm programming challange? – gbtimmon Jul 03 '12 at 19:45
  • @alfasin: actually, i "remember" all digits which were pressed and it order. Can save it by a string for example. Every time I'm pressing a key, I'm adding it as the last char to the string. – elirandav Jul 03 '12 at 19:47
  • @kernelMode so in this case you don't need any `algorithm` - just check the new character along with the last 3 that were entered. sounds like a simple for/while loop, no ? – Nir Alfasi Jul 03 '12 at 19:52
  • @alfasin just because it a 'simple for/while loop' doesn't mean its not an algorithm. Also a simple for while loop is not sufficent. You can not blindly travel, you could very easily hit a point where the are no possibilities for creating a 'new' 4 digit combination, and yet not have hit all of the combinations. The key is figuring out how to traverse the numbers, hit them all once, but none twice. Its is not a trival problem at all. You need to read closer. – gbtimmon Jul 03 '12 at 19:57
  • @gbtimmon if u mean academy by 'acm', so it isn't. It was part of an interview exam. – elirandav Jul 03 '12 at 20:03
  • I mean http://www.acm.org/, They used to have programming competitions that I competed in when I was in college and this sounds like one of the questions they would ask. – gbtimmon Jul 03 '12 at 20:05
  • @gbtimmon do you mean that if the code is '1345' you still have to be able to unlock ? cause it's not very clear from the given example. – Nir Alfasi Jul 03 '12 at 20:09
  • 1
    @alfasin I think the question is saying you have to produce one sequence, which contains every 4 digit number as a sub sequence, but contains no 4 digit sub sequence twice. so the sequence 123451234 would be illegal since it contains 1234 twice. – gbtimmon Jul 03 '12 at 20:48
  • @gbtimmon in that case - I don't understand the question. The specifications are not clear (to me). – Nir Alfasi Jul 03 '12 at 22:46

3 Answers3

2

I found a reduction to your problem:

Lets define directed graph G = (V,E) in the following way:

V = {all possible combinations of the code}.

E = {< u,v > | v can be obtained from u by adding 1 digit (at the end), and delete the first digit}.

|V| = 10^4.

Din and Dout of every vertex equal to 10 => |E| = 10^5.

You need to prove that there is Hamilton cycle in G - if you do, you can prove the existence of a solution.

EDIT1:

The algorithm:

  1. Construct directed graph G as mentioned above.

  2. Calculate Hamilton cycle - {v1,v2,..,vn-1,v1}.

  3. Press every number in v1.

  4. X <- v1.

  5. while the safe isn't open:

    5.1 X <- next vertex in the Hamilton path after X.

    5.2 press the last digit in X.

We can see that because we use Hamilton cycle, we never repeat the same substring. (The last 4 presses).

EDIT2:

Of course Hamilton path is sufficient.

barak1412
  • 972
  • 7
  • 17
  • it's not `all possible combinations` - only `sub sequence of the typing` if I got it right – Nir Alfasi Jul 03 '12 at 20:11
  • @alfasin I meant all possible combinations at length 4. The Hamilton cycle is exactly what he needs. – barak1412 Jul 03 '12 at 20:13
  • I understood what you said. and I'm saying that we don't need to check `all` the combinations, only the `sub-sequences` – Nir Alfasi Jul 03 '12 at 20:16
  • Thanks, it's good direction. But now the problem is that we don't have enough edges per node (by [Dirac](http://en.wikipedia.org/wiki/Hamiltonian_path), each vertex has degree n/2 or greater. We don't). But proving the graph is Hamilton will absolutely solve it existent of solution. – elirandav Jul 03 '12 at 20:16
  • @alfasin I think he meant substring and not sub sequence – barak1412 Jul 03 '12 at 20:19
  • @barak - so it's even easier! as you only have to remember the last 3 characters – Nir Alfasi Jul 03 '12 at 20:27
  • @alfasin The problem is that you need to avoid repeating substrings that you have seen before. Every step you need to exlpore new substring. That's why I thought about Hamilton cycle -> you reach every vertex (substring) once. – barak1412 Jul 03 '12 at 20:42
  • I dont think this works, you have 10^4 vertexes meaning you have each digit 4 times, whats to prevent your from travling 1234, twice on two discrecte sets of vertex 1234, that is you have `(1a,2a,3a ... 1b,2b,cb, .... 1c,2c,... 9d)`, and you could travel both 1a,2a,3a,4a, and 1b,2c,3c,4d in your Hamiltonian cycle and it would satisfy your constraints but not the problem constraints. – gbtimmon Jul 03 '12 at 21:00
  • Each vertex represent 4 digits. Thus, in Hamilton cycle you will travel each vertex only 1 time -> you visit each combination of 4 digits only 1 time. For example: you start with "1234", if according to the Hamilton cycle you need to reach "2341", it means you add the digit 1 and ect'.I pretty sure my reduction is correct. – barak1412 Jul 03 '12 at 21:12
1

Here in summary is the problem I think you are trying to solve and some explanation on how i might approach solving it. http://www.cs.swan.ac.uk/~csharold/cpp/SPAEcpp.pdf

You have to do some finessing to make it fit into the chinese post man problem however... Imagine solving this problem for the binary digits, three digits strings. Assume you have the first two digits, and ask your self what are my options to move to? (In regards to the next two digit string?) You are left with a Directed Graph.

 /-\
/   V    
\-  00 ----> 01
      ^  /   ^|
       \/    ||
       /\    ||
      V  \   |V
 /-- 11 ---> 10 
 \   ^         
  \-/

Solve the Chinese Postman, you will have all combinations and will form one string The question is now, is the Chinese postman solvable? There are algorithms which determine weather or not a DAG is solvable for the CPP, but i don't know if this particular graph is necessarily solvable based on the problem alone. That would be a good thing to determine. You do however know you could find out algorithmically weather it is solvable and if it is you could solve it using algorithms available in that paper (I think) and online.

Every vertex here has 2 incoming edges and 2 outgoing edges. There are 4 (2^2) vertexes.

In the full sized problem there are 19683( 3 ^ 9 ) vertexs and every vertex has 512 ( 2 ^ 9 ) out going and incoming vertexes. There would be a total of

19683( 3 ^ 9 ) x 512 (2 ^ 9) = 10077696 edges in your graph. 

Approach to solution:

1.) Create list of all 3 digit numbers 000 to 999.
2.) Create edges for all numbers such that last two digits of first number match first
two digits of next number. 

ie 123 -> 238 (valid edge) 123 -> 128 (invalid edge)

3.) use Chinese Postman solving algorithmic approaches to discover if solvable and
solve
gbtimmon
  • 4,238
  • 1
  • 21
  • 36
0

I would create an array of subsequences which needs to be updates upon any insertion of a new digit. So in your example, it will start as:

array = {1}

then

array = {1,2,12}

then

array = {1,2,12,3,13,23,123}

then

array = {1,2,12,3,13,23,123,4,14,24,124,34,134,234,1234}

and when you have a sequence that is already at the length=4 you don't need to continue the concatenation, just remove the 1st digit of the sequence and insert the new digit at the end, for example, use the last item 1234, when we add 5 it will become 2345 as follows:

array = {1,2,12,3,13,23,123,4,14,24,124,34,134,234,1234,5,15,25,125,35,135,235,1235,45,145,245,1245,345,1345,2345,2345}

I believe that this is not a very complicated way of going over all the sub-sequences of a given sequence.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • How do you then produce a single string out of this? And this is not a sequence.... http://en.wikipedia.org/wiki/Sequence. You are producing Subsets of a set which are of length 4 from the `set` of numbers 1-9. There are far easier way to do this and this does not solve the problem. The difference in a sequence, order is important, in a set it is not, and since you rearrange order in your example, you are not looking at a subsequence. Good thinking though, I like to see people trying to solve difficult problems – gbtimmon Jul 03 '12 at 20:32
  • @gbtimmon you're right! in a sequence/sub-sequence the order matters indeed(and that was my intention). I got the feeling that the problem is not defined very well. I got the impression that we're talking about sequence and sub-sequence becuase of his last sentence: `(new combinations of the last 4 digits of the sequence\string)?` – Nir Alfasi Jul 03 '12 at 22:42