Created
August 23, 2011 15:00
-
-
Save Netpilgrim/1165353 to your computer and use it in GitHub Desktop.
Analyzing Android Pattern Lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def pattern-graph | |
[[0 1 0 1 1 1 0 1 0] | |
[1 0 1 1 1 1 1 0 1] | |
[0 1 0 1 1 1 0 1 0] | |
[1 1 1 0 1 0 1 1 1] | |
[1 1 1 1 0 1 1 1 1] | |
[1 1 1 0 1 0 1 1 1] | |
[0 1 0 1 1 1 0 1 0] | |
[1 0 1 1 1 1 1 0 1] | |
[0 1 0 1 1 1 0 1 0]]) | |
(defn simple-paths | |
"Set of simple paths (paths w/o repeating vertices) for the given graph. | |
The graph must be given as a vector of adjacency seqs." | |
[g] | |
(letfn | |
[(search-tree [v p paths] | |
(if ((set p) v) | |
paths | |
(let [p+ (cons v p)] | |
(search (g v) p+ (conj paths p+))))) | |
(search [vs p paths] | |
(if (empty? vs) | |
paths | |
(reduce #(search-tree %2 p %1) paths vs)))] | |
(search (range 0 (count g)) [] #{}))) | |
(defn adj-lists | |
"A vector with the adjacency lists for the given adjacency matrix" | |
[m] | |
(vec (for [r m] (keep-indexed #(if (= 1 %2) %1) r)))) | |
(defn print-pattern-paths-info [] | |
"Prints information about the possible paths on an Android pattern lock" | |
(let [paths (simple-paths (adj-lists pattern-graph)) | |
length-counts (frequencies (map count paths)) | |
valid-lengths (range 4 10)] | |
(doseq [l valid-lengths] | |
(println (str "Paths of length " l ":") | |
(length-counts l))) | |
(println "Overall number of valid paths:" | |
(reduce + (for [l valid-lengths] (length-counts l)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment