I want to show that

$\qquad\displaystyle O = \{M : M \text{ is a DFA}, |L(M)| = 1\}$.

Here $|L(M)|=1$ means the DFA contains only one state. I really don't know where to get started in this problem.

Should I do something like this?

M = On input (M',w) where M' is a DFA and w is a string:

 - If M' accept only one member, accept.
 - If more than one or less than one accepted, reject.

But this will loop the program forever for the first step as it keeps testing every member after one member is selected. First step just doesn't seem right.

I think these questions might be relating to this: 1, 2

  • 2
    $\begingroup$ Are you sure that $|L(M)| = 1$ is meant to mean $M$ has only one state? It would normally mean that the language accepted by $M$ contains exactly one string, which is quite a different property. $\endgroup$ Feb 27 '15 at 0:03
  • $\begingroup$ I agree. Checking that there's only one state is trivial and requires no insight at all. $\endgroup$
    – Raphael
    Feb 27 '15 at 7:48
  • 1
    $\begingroup$ I don't think the question is well formulated. What is $O$? $\endgroup$ Feb 27 '15 at 15:07
  • $\begingroup$ This question is not clear. What do you want to show? If you want to show that $O=\text{something}$, you need to give us a definition of $O$. If you meant that as a definition of $O$, then you need to tell us what you want to show about $O$. Also, I think you need to review DFA: if I gave you a DFA, can you think of any algorithm to check whether it accepts more than one possible word? The purpose of exercises is to help you recognize areas where you need to study more, and to give you practice. It looks like this has helpfully highlighted an area for you to study in greater depth.... $\endgroup$
    – D.W.
    Feb 27 '15 at 22:12

The approach you have given is normally used for demonstrating a language is undecidable (by using the language to decided an known undecidable language).

What you need to do is (implicitly or explicitly) construct an algorithm that decides the problem.

As I mentioned in a comment, your notation is unclear, but both possible interpretations are decidable anyway.

  1. If you actually mean the property to be decided is that the language of $M$ contains one string (which is what $|L(M)|=1$ would normally mean), then you need to give an algorithm that decides whether there is exactly one, loopless path from the start state to a final state or not.
  2. If the property is that $M$ has only one state (normally written something like $|Q| = 1$ where $Q$ is the set of states of $M$), then you need to give an algorithm that decides whether the DFA has one state or not.

Given that (2) is essentially trivial, I assume that (1) is the actual question you've been asked, so you need to think of how, given, for example, a state diagram sitting in front of you, you would work out that there's exactly one path from the start state to exactly one of the final states, that's essentially all an algorithm really is.


This is the lazy-person approach:

  • Minimize the DFA
  • The Automaton describes a 1-word language if and only if
    1. it has exactly 1 final state, and
    2. every non-final state has one outgoing transition.

Note: This is far from being the most efficient solution - its just a short one.

  • $\begingroup$ 2. depends on the definition of DFA; some require the automaton to be total, in which case you'd have to check for the sink state. $\endgroup$
    – Raphael
    Feb 28 '15 at 11:24

The case of 1-state DFA

Accepting one word is unrelated to having one state.

If there is only one state, it is necessarily starting and accepting state, unless there is no accepting state which make the language empty.

An automaton that has only one state which is accepting is necessarily accepting a language equal to $\Sigma_1^*$ for some subset $\Sigma_1$ of the input alphabet $\Sigma$.

Then the automaton accepts exactly one word if and only if it is the empty word $\epsilon$, which is the case when $\Sigma_1=\emptyset$

Multi-state DFA

Now, assuming you can have more than one state (I would think that you did not intend to have such a restriction).

You look at your DFA as a directed graph.

Acceptance of a word is equivalent to finding a path from initial state to a final state. Since the automaton is deterministic there is a bijection between such path and the words of the language. Furthermore, the length of the word is equal to the length of the path.

Finding one path between the initial state and a final state can be done simply by tracing, starting with the initial state. You start with a set $U$ (for untraced) containing only the initial node, and a set $T$ (for traced once) initially empty.

Then for any state $q$ remaining in $U$, you add all nodes $q'$ with a transition $q\to q'$ to $U$ unless they are already in $U\cup T$, and you transfer $q$ from $U$ to $T$. You terminate when $U$ is empty, and then all nodes in $T$ are reachable from the initial state. Note that this is the gist of tracing garbage collection algorithms. The complexity is linear if inclusion of a node in a set is checkable in constant time (for example by marking the node).

Now, in order to check for multiple paths, you modify a bit the algorithm, replacing $T$ by two sets $T_1$ (for traced once) and $T_2$ (for traced twice). You proceed as before, with the following changes, that depend on whether $q$ is in $T_1$ or not.

  1. When $q\notin T_1$ ( $q$ was never traced, nor accessed from two paths), consider successively every transition $q\to q'$, making sure you process first a transition $q\to q$ if any exists:

    • if $q'$ is already in $T_2$, you ignore it

    • else, if it is in both $U$ and $T_1$, you ignore it

    • else, if it is in $T_1$, but not in $U$, you add it to $U$ (leaving it in $T_1$ too).

    • else, if it is in $U$, but not in $T_1$, you add it to $T_1$ (leaving it in $U$ too).

    • else, if $q'$ is in none of the sets, you add it to $U$.

    Then you move the node/state $q$ to $T_1$ if it is not already in $T_1$, or to $T_2$ if it is in $T_1$ (you may remove it from $T_1$ if convenient for set encoding, but it does not matter). Note that $q$ may already be in $T_1$ if one of the transitions just processed is of the form $q\to q$.

  2. When $q\in T_1$ (there are at least two paths to $q$), consider successively every transition $q\to q'$:

    • if $q'$ is already in $T_2$, you ignore it

    • else, put $q'$ in both in both $U$ and $T_1$ (it may be there already)

    Then you move the node/state $q$ to $T_2$ (and you may remove it from $T_1$, if you wish).

When you terminate (with $U$ empty), you have at least two paths from the initial state to a final state if and only if you have an accepting state in $T_2$, or two accepting states in $T_1$. You have at least one path if $T_1\cup T_2$ contains an accepting state, and no path it it does not.

So the necessary and sufficient condition for the existence of exactly a single word in the language is that there is a single accepting state in $T_1$ and none in $T_2$.

With proper encoding of the sets, this works in linear time (in the size of the DFA), and can be quite fast.

Note: the conditionals have been written for better readability. They can be better organized for efficiency.

  • $\begingroup$ I am wondering whether there could be any practical use for such an algorithm. Someone has an idea ? $\endgroup$
    – babou
    Feb 27 '15 at 18:33

First, as Luke has said, if the problem is to decide that a FA (deterministic or not) has only one state, it's trivial. If I gave you a description, $\langle\,M\,\rangle$, of a finte auomaton $M$, could you look at that description and determine whether $M$ had only one state? Surely you could.

The more interesting problem is to decide, given $\langle\,M\,\rangle$, whether $M$ accepted exactly one string, which we would write as $|L(M)| = 1$. The design of the decider isn't immediately obvious, but we can get some help from this

Theorem: If $M$ is a DFA with $n$ states, then $L(M)$ is infinite if and only if $M$ accepts a string of length $l$ where $n\le l < 2n$.

Part of this is an immediate consequence of the Pumping Lemma for regular languages: if $M$ accepts a word, $w$, of length greater than or equal to $n$, then we can pump $w$ to be as long as we wish, so $L(M)$ would contain all these (infinitely many) strings. The upper bound is a trifle more difficult to prove, but it furnishes a decider for infinite languages. Now we can use this result to make a decider for the language we might call

$\qquad\qquad SINGLETON_{TM} = \{\langle M\rangle \mid M\text{ is a DFA and } |L(M)| = 1\}$

by doing this:

D(<M>) =
   if |L(M)| is infinite                // we can decide this in finite time
      return false
   else                                // now we know the language is finite
      count = 0
      for all strings x of length < n  // only finitely many strings to test
         if M accepts x
            increment count
      return (count = 1)

So $SINGLETON_{DFA}$ is decidable, since we've built a decider for it.

  • $\begingroup$ This seems a bit excessive for a problem that can be solved by a simple graph traversal. $\endgroup$
    – Raphael
    Feb 27 '15 at 7:49

A DFA accepts more than one word if and only if there is more than one path from the starting state to a final state.

Figuring out if this is the case does not require more than a graph traversal and some checks. Formulated as a decider for $O$ on input $A = (Q, \Sigma, \delta, q_0, Q_F)$:

  1. Perform DFS from $q_0$ and count for each vertex how often it has been reached (in an array $c$).
  2. Reject if $\sum_{q \in Q_F} c[q] \neq 1$.
  3. Denote with $q_f$ the one final state with $c[q_f] = 1$.
    Reject if $c[q] > 1$ for any $q$ on the path from $q_0$ to $q_f$.
  4. Accept

After the first step, $c[q]$ is the number of simple paths from $q_0$ to $q$ in $A$. Clearly, if any of the final states have more than one, or more than one final state have any of these paths, the automaton accepts more than one word. (If there are none at all, it accepts no word.) So, rejecting in step 2 is the right call.

If we do not reject here, there is exactly one final state for which there is exactly one simple path from $q_0$. Now we have to check if there is a cycle intersecting this path; this is what step 3 does. Note that we get the path from the DFS in step 1.

If there is no such cycle there is indeed only one accepting path in $A$ and we can accept.

Clearly, the algorithm runs in time $O(|\Sigma| \cdot |Q|)$.

  • $\begingroup$ what happens in case we have loops? does this algorithm stop? $\endgroup$
    – User584322
    Jul 31 at 8:59
  • $\begingroup$ @User584322 Why don't you try it out? And which step do you suspect to loop? $\endgroup$
    – Raphael
    Aug 2 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.