Number of possible paths in android pattern lock

荒凉一梦 提交于 2019-12-13 19:21:57

问题


How many paths possible in android pattern lock?

I thought it can be calculated simply by factorial, with formula (9!)/(9-length)!

Examples:

For length 9, there are 9*8*7*6*5*4*3*2*1 paths.

For length 8, there are 9*8*7*6*5*4*3*2 paths.

For length 7, there are 9*8*7*6*5*4*3 paths.

etc.

Here is the code for calculating this:

def paths_of_length(number_of_staring_points, length_of_path):
    print("number_of_staring_points", number_of_staring_points, "length_of_path", length_of_path)
    different_paths = 1
    for choosing_from in range(number_of_staring_points, 
                               number_of_staring_points - length_of_path,
                               -1):
        different_paths = different_paths * choosing_from

    return different_paths

def android_paths():
    """
    Returns number of different android lockscreen paths
    """
    different_paths = 0
    minimum_length = 4
    maximum_length = 9
    number_of_staring_points = 9
    for length in range(minimum_length,maximum_length + 1):
        different_paths += paths_of_length(number_of_staring_points,length)

    return different_paths

if __name__ == '__main__':
    import doctest
    doctest.testmod()

print(android_paths())

Is my method, and the code correct? Or am I calculating it wrong?

Thanks in advance.


回答1:


Update: Found this has been covered in another post android lock password combinations

The allowed moves include adjacent (including diagonals), knights (e.g. 1->6) and pegged moves (e.g. 1->3 if 2 already in the path).

So a quick brute force in python:

pegs = {
    1: {3:2, 7:4, 9:5},
    2: {8:5},
    3: {1:2, 7:5, 9:6},
    4: {6:5},
    5: {},
    6: {4:5},
    7: {1:4, 3:5, 9:8},
    8: {2:5},
    9: {1:5, 3:6, 7:8}
}

def next_steps(path):
    return (n for n in range(1,10) if (not path or n not in path and 
                                       (n not in pegs[path[-1]] 
                                        or pegs[path[-1]][n] in path)))

def patterns(path, steps, verbose=False):
    if steps == 0:
        if verbose: print(path)
        return 1
    return sum(patterns(path+[n], steps-1, verbose) for n in next_steps(path))

[(steps, patterns([], steps)) for steps in range(1,10)]

Output:

[(1, 9),
 (2, 56),
 (3, 320),
 (4, 1624),
 (5, 7152),
 (6, 26016),
 (7, 72912),
 (8, 140704),
 (9, 140704)]

So the total for android (4-9) is:

>>> sum(patterns([], steps) for steps in range(4,10))
389112



回答2:


Your calculation is wrong, because not every node has edge to another node, and some nodes have some edges only enabled by some conditions.

For example: To reach from top-left node to top-right node, top-middle node should be visited before.

You can't calculate it simply by multiplying some numbers. You need to use a path finding algorithm.

Good news, I wrote one.


Code

This is a utility class:

import java.util.ArrayList;
import java.util.HashMap;

public class Node
{

    private String name;

    private HashMap<Node, Node> conditionalNeigbors = new HashMap<>();

    private ArrayList<Node> neigbors = new ArrayList<>();
    private boolean visited = false;

    public Node(String name)
    {
        this.name = name;
    }

    void addNeigbor(Node n)
    {
        this.neigbors.add(n);
    }

    void addConditionalNeigbor(Node condition, Node n)
    {
        conditionalNeigbors.put(condition, n);
    }

    ArrayList<Node> getNeigbors(ArrayList<Node> path)
    {

        ArrayList<Node> toReturn = new ArrayList<>();
        ArrayList<Node> conditionals = new ArrayList<>();
        for (int i = 0; i < path.size(); i++)
        {
            if(conditionalNeigbors.containsKey(path.get(i)))
            {
                conditionals.add(conditionalNeigbors.get(path.get(i)));
            }
        }

        toReturn.addAll(neigbors);
        toReturn.addAll(conditionals);

        return toReturn;
    }

    void setVisited(boolean b)
    {
        visited = b;
    }

    boolean getVisited()
    {
        return visited;
    }

    public String getName()
    {
        return name;
    }


}

And the main class:

import java.util.ArrayList;

public class Pathfinder
{

    static boolean debug = false;

    /**
     * A B C
     *
     * D E F
     *
     * G H J
     */
    public static void main(String[] args)
    {

        Node a = new Node("A");
        Node b = new Node("B");
        Node c = new Node("C");
        Node d = new Node("D");
        Node e = new Node("E");
        Node f = new Node("F");
        Node g = new Node("G");
        Node h = new Node("H");
        Node j = new Node("J");

        a.addNeigbor(b);
        a.addNeigbor(d);
        a.addNeigbor(e);
        a.addNeigbor(h);
        a.addNeigbor(f);
        a.addConditionalNeigbor(b, c);
        a.addConditionalNeigbor(d, g);
        a.addConditionalNeigbor(e, j);

        b.addNeigbor(a);
        b.addNeigbor(d);
        b.addNeigbor(e);
        b.addNeigbor(f);
        b.addNeigbor(c);
        b.addNeigbor(g);
        b.addNeigbor(j);
        b.addConditionalNeigbor(e, h);

        c.addNeigbor(b);
        c.addNeigbor(e);
        c.addNeigbor(f);
        c.addNeigbor(d);
        c.addNeigbor(h);
        c.addConditionalNeigbor(b, a);
        c.addConditionalNeigbor(e, g);
        c.addConditionalNeigbor(f, j);

        d.addNeigbor(a);
        d.addNeigbor(b);
        d.addNeigbor(e);
        d.addNeigbor(g);
        d.addNeigbor(h);
        d.addNeigbor(c);
        d.addNeigbor(j);
        d.addConditionalNeigbor(e, f);

        e.addNeigbor(a);
        e.addNeigbor(b);
        e.addNeigbor(c);
        e.addNeigbor(d);
        e.addNeigbor(f);
        e.addNeigbor(g);
        e.addNeigbor(h);
        e.addNeigbor(j);

        f.addNeigbor(c);
        f.addNeigbor(b);
        f.addNeigbor(e);
        f.addNeigbor(h);
        f.addNeigbor(j);
        f.addNeigbor(a);
        f.addNeigbor(g);
        f.addConditionalNeigbor(e, d);

        g.addNeigbor(d);
        g.addNeigbor(e);
        g.addNeigbor(h);
        g.addNeigbor(b);
        g.addNeigbor(f);
        g.addConditionalNeigbor(d, a);
        g.addConditionalNeigbor(e, c);
        g.addConditionalNeigbor(h, j);

        h.addNeigbor(d);
        h.addNeigbor(e);
        h.addNeigbor(f);
        h.addNeigbor(g);
        h.addNeigbor(j);
        h.addNeigbor(a);
        h.addNeigbor(c);
        h.addConditionalNeigbor(e, b);

        j.addNeigbor(f);
        j.addNeigbor(e);
        j.addNeigbor(h);
        j.addNeigbor(d);
        j.addNeigbor(b);
        j.addConditionalNeigbor(h, g);
        j.addConditionalNeigbor(f, c);
        j.addConditionalNeigbor(e, a);

        ArrayList<Node> graph = new ArrayList<>();
        graph.add(a);
        graph.add(b);
        graph.add(c);
        graph.add(d);
        graph.add(e);
        graph.add(f);
        graph.add(g);
        graph.add(h);
        graph.add(j);

        int sum = 0;

        System.out.println(countPaths(b, 3, new ArrayList<>()));

        for (int k = 1; k < 10; k++)
        {
            for (int i = 0; i < graph.size(); i++)
            {
                sum += countPaths(graph.get(i), k, new ArrayList<>());
            }

            System.out.println("Number of all paths with length of " + k + ": " + sum);
            sum = 0;
        }
    }

    /*
        Finds number of all possible paths of given length, starting from given node
     */
    static int countPaths(Node start, int length, ArrayList<Node> path)
    {

        start.setVisited(true);
        path.add(start);

        ArrayList<Node> neigbors = start.getNeigbors(path);
        int neigborCount = neigbors.size();
        ArrayList<Node> unvisitedNeighbors = new ArrayList<>();

        for (int i = 0; i < neigborCount; i++)
        {
            Node temp = neigbors.get(i);

            if (temp.getVisited() == false)
            {
                unvisitedNeighbors.add(temp);
            }
        }

        int unvisitedNeighborCount = unvisitedNeighbors.size();

        if (length == 1) // Base case, no more moves, a path found, return 1
        {
            if (debug)
            {
                for (int i = 0; i < path.size(); i++)
                {
                    System.out.print(path.get(i).getName());
                }
                System.out.println("");
            }

            start.setVisited(false); // Backtrack
            path.remove(path.size() - 1);

            return 1;
        } else // There are still moves
        {
            int sum = 0;
            for (int i = 0; i < unvisitedNeighborCount; i++)
            {
                sum += countPaths(unvisitedNeighbors.get(i), length - 1, path);
            }

            start.setVisited(false); // Backtrack
            path.remove(path.size() - 1);

            return sum;
        }
    }

}

No, you don't have to run this. I have calculated all for you:

Number of all paths with length of 1: 9
Number of all paths with length of 2: 56
Number of all paths with length of 3: 320
Number of all paths with length of 4: 1624
Number of all paths with length of 5: 7152
Number of all paths with length of 6: 26016
Number of all paths with length of 7: 72912
Number of all paths with length of 8: 140704
Number of all paths with length of 9: 140704

Explanation

I turned the problem into a undirected cyclic graph search problem.

A  B  C
D  E  F 
G  H  J
  • Points are represented as Nodes
  • Legal moves are represented as Edges
  • Every Node has a visited property
  • There are two types of edges: Always available ones, and conditional ones. An example to conditional move: A-C possible only when B is visited.
  • Search starts from a given node for given length of paths, with empty path. In each iteration, algorithm obtains possible edges(taking account of conditional edges) and recursively calls a sub-search starting from next nodes.

Example

This is an example call trace, for searching paths length of 3, starting from node B.

_\ countPaths(B, 3, null)
   _\ countPaths(A, 2, B)
      _\ countPaths(C, 1, BA)
      _\ countPaths(D, 1, BA)
      _\ countPaths(E, 1, BA)
      _\ countPaths(F, 1, BA)
      _\ countPaths(H, 1, BA)
   _\ countPaths(C, 2, B)
      _\ countPaths(A, 1, BC)
      _\ countPaths(D, 1, BC)
      _\ countPaths(H, 1, BC)
      _\ countPaths(E, 1, BC)
      _\ countPaths(F, 1, BC)
   _\ countPaths(D, 2, B)
      _\ countPaths(A, 1, BD)
      _\ countPaths(E, 1, BD)
      _\ countPaths(G, 1, BD)
      _\ countPaths(H, 1, BD)
      _\ countPaths(C, 1, BD)
      _\ countPaths(J, 1, BD)
   _\ countPaths(E, 2, B)
      _\ countPaths(A, 1, BE)
      _\ countPaths(C, 1, BE)
      _\ countPaths(D, 1, BE)
      _\ countPaths(F, 1, BE)
      _\ countPaths(G, 1, BE)
      _\ countPaths(H, 1, BE)
      _\ countPaths(J, 1, BE)
   _\ countPaths(F, 2, B)
      _\ countPaths(C, 1, BF)
      _\ countPaths(E, 1, BF)
      _\ countPaths(H, 1, BF)
      _\ countPaths(J, 1, BF)
      _\ countPaths(A, 1, BF)
      _\ countPaths(G, 1, BF)
   _\ countPaths(G, 2, B)
      _\ countPaths(D, 1, BG)
      _\ countPaths(E, 1, BG)
      _\ countPaths(H, 1, BG)
      _\ countPaths(F, 1, BG)
   _\ countPaths(J, 2, B)
      _\ countPaths(F, 1, BJ)
      _\ countPaths(E, 1, BJ)
      _\ countPaths(H, 1, BJ)
      _\ countPaths(D, 1, BJ)

So it simply divides problems into smaller sub-problems, until it gets a problem with length of 1 where solution is 1(base case).

So after finding all path from a given node, all we need to do is to enumerate this operation for all 9 nodes, which is done by a simple for loop in main() method, simply by calling countPaths() methods.



来源:https://stackoverflow.com/questions/34604605/number-of-possible-paths-in-android-pattern-lock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!