Search code examples
algorithmrandomfinite-automatadfastate-machine

What is the algorithm for generating a random Deterministic Finite Automata?


The DFA must have the following four properties:

  • The DFA has N nodes

  • Each node has 2 outgoing transitions.

  • Each node is reachable from every other node.

  • The DFA is chosen with perfectly uniform randomness from all possibilities

This is what I have so far:

  1. Start with a collection of N nodes.
  2. Choose a node that has not already been chosen.
  3. Connect its output to 2 other randomly selected nodes
  4. Label one transition 1 and the other transition 0.
  5. Go to 2, unless all nodes have been chosen.
  6. Determine if there is a node with no incoming connections.
  7. If so, steal an incoming connection from a node with more than 1 incoming connection.
  8. Go to 6, unless there are no nodes with no incoming connections

However, this is algorithm is not correct. Consider the graph where node 1 has its two connections going to node 2 (and vice versa), while node 3 has its two connection going to node 4 (and vice versa). That is something like:

1 <==> 2

3 <==> 4

Where, by <==> I mean two outgoing connections both ways (so a total of 4 connections). This seems to form 2 cliques, which means that not every state is reachable from every other state.

Does anyone know how to complete the algorithm? Or, does anyone know another algorithm? I seem to vaguely recall that a binary tree can be used to construct this, but I am not sure about that.


Solution

  • Strong connectivity is a difficult constraint. Let's generate uniform random surjective transition functions and then test them with e.g. Tarjan's linear-time SCC algorithm until we get one that's strongly connected. This process has the right distribution, but it's not clear that it's efficient; my researcher's intuition is that the limiting probability of strong connectivity is less than 1 but greater than 0, which would imply only O(1) iterations are necessary in expectation.

    Generating surjective transition functions is itself nontrivial. Unfortunately, without that constraint it is exponentially unlikely that every state has an incoming transition. Use the algorithm described in the answers to this question to sample a uniform random partition of {(1, a), (1, b), (2, a), (2, b), …, (N, a), (N, b)} with N parts. Permute the nodes randomly and assign them to parts.

    For example, let N = 3 and suppose that the random partition is

    {{(1, a), (2, a), (3, b)}, {(2, b)}, {(1, b), (3, a)}}.
    

    We choose a random permutation 2, 3, 1 and derive a transition function

    (1, a) |-> 2
    (1, b) |-> 1
    (2, a) |-> 2
    (2, b) |-> 3
    (3, a) |-> 1
    (3, b) |-> 2