September 17, 2008
Android's locking pattern
As you might already know, Android uses an innovative approach to lock your phone and prevent accidental dialing. Play the short video above to see how it works.
For the rest of this discussion, I will use the following convention to designate a locking pattern:
A pattern can be pretty much anything you want with the following constraints:
- It needs to contain at least four dots (and obviously, no more than nine).
- Once a dot is lit, it can't be used again.
- You can use one or several "knight moves", such as in [0 5 4 2]:
In this example, the knight move is between 0 and 5.
- You cannot go over an unlit dot without lighting it. For example, the pattern [0 2 1 4] is illegal, because moving your finger between 0 and 2 will light 1.
- Once a dot is lit, you can use it to reach another unlit dot. For example, both [0 4 3 5] and [0 4 5 3] are legal:
This example is a bit ambiguous but you get the idea: I trace [0 4 5] and then I slide over 4 again to reach 3.
With these constraints in place, I wondered how many combinations were actually possible, so I wrote a short program to simulate it. It turns out there are quite a few: 389,112. Here is a breakdown:
4 dots: 1624 solutions
5 dots: 7152 solutions
6 dots: 26016 solutions
7 dots: 72912 solutions
8 dots: 140704 solutions
9 dots: 140704 solutions
If you're not convinced, here is a full list of all the four dot patterns
(the page only works on Firefox and it uses a big canvas object, it might take a few seconds to load).
Still, I'm not sure my calculations are right, so I'd like you, dear reader, to confirm my numbers, and optionally explain how you coded your solution...
Posted by cedric at September 17, 2008 02:43 PM
Would [5 0 2 1] be a valid 4 dot solution? Elided from [5 0 5 2 1] using a combination of the 3rd and 5th rules? I don't see it in your list though? If it is valid then I'm getting figures such as:
4 dots: 2352 solutions
5 dots: 11760 solutions
And for completeness:
2 dots: 56 solutions (I know this one is right :-))
3 dots: 392 solutions
Nice small multiples diagram!
This unlock system looks cool, but it sure seems less secure than a PIN and no easier to remember.
@Charles: I don't believe that's the meaning of the fifth rule. In fact, I think rule 5 is redundant with rule 4. Rule 3 may be redundant as well. I would restate the rules as follows:
* A path must visit at least four dots.
* Once a dot is visited, it must not be visited again.
* You may only move between dots in straight lines, and may only change direction when visiting a dot.
* You may not pass over an unvisited dot without visiting it (but you may pass over a visited dot).
I can confirm those results, by means of a rather brute force search. I first created a function that takes the current dot and a set of all visited dots and reports which dots may be visited next. I further broke this down into "easy" cases and "hard" cases. For each dot, there is a set of dots that you can *always* visit next so long as you have not visited them before. These are the easy cases and can trivially be stored in a short table. The hard cases are the dots that can only be visited if the dot in the way has been visited first. For the corner dots, there are three such candidates, the dots in the other corners. For the dots in the middle of an edge, the candidate dot is the opposite dot. In all cases, the "blocking" dot is in between the two. The candidate dot can be visited next only when the blocking dot *is* in the visited set and the candidate dot *is not* in the visited set.
Once you have that function, it's easy to do a brute force search in a depth-first manner. There are some obvious ways to exploit the symmetry to chop down the search time a bit, and you can do most of the set operations with bitwise operations, but I'd be interested to know if there's a more elegant algorithm. I don't exploit the fact that, for example, the sets of possible suffixes for each of the prefixes 0134, 0314, 1034, 1304, 3014, 3104 are all identical.
My implementation could be cleaner, and given that it appears to run instantaneously, the bit twiddling optimisation may have been completely unnecessary. Still, hopefully it's not completely indecipherable.
I put the code in pastebin, but it seems I can't post the URL here. I'll try without the prefix: pastebin.com/f6e4a1689
This was the reason of the "code challenge"? Cool
I like the page with all possible combinations using 4 dots. You should post one showing all combinations using all 9 dots, or maybe just all possible combos! That would be SWEET!!!
This is exactly the answer to the question I had - and then some. But the title should be Android's unlocking pattern, not Android's locking pattern.
And yes, I see that the last comment is over a year old. Still a cool post though.
At first this is really cool ... but the smudges left behind might make it easy for someone to guess what pattern you are drawing.
A better implementation would have the dots spaced with random variations/distortions. For example, the dots could be uniformly stretched out or contracted, non-uniformly stretched/contracted (ex: one side a la trapezoid), or the entire grid could vary in its orientation or placement. All of this of course would be random.
As it exists, I personally would rather have the traditional pin/keypad.