Search code examples
algorithmdfa

Find all matches of a DFA


Given a DFA (Deterministic Finite Automaton), what algorithm can I use to produce a stream of all the input sequences that are accepted by the automaton, sorted in ascending length?


Solution

  • If you have access to the DFA's attributes (i.e., states, transitions, start state, accepting states), then let the algorithm turn that information into a graph, where states are vertices, transitions are labelled edges (labelled with the input symbol).

    Perform a breadth-first traversal from the start node, and allow nodes to be revisited. Whenever a node is visited that represents an accepting state, output the string that corresponds to the path. Either keep track of the formed string while expanding the search, or keep a back reference to the preceding node in the path, so that the string can be rebuilt from that linked list.

    If the output must be ordered for equally sized strings, then make sure the nodes have their outgoing edges ordered by label (symbol).

    This is not a memory efficient method.

    If the DFA accepts an infinite number of inputs, then this algorithm will run infinitely, but practically it will run until it runs out of memory.