CMPS 260 Spring 2024
HW #1: Designing DFA's
Sample Solutions

For each problem, you are to present a DFA (in the form of a transition diagram) that accepts the described language. If the alphabet is not explicitly indicated, assume that it is {a,b}.

1. The language containing precisely those strings whose first and last symbols are different (i.e., one of them is a and the other is b).

Solution:
Logic:
Let xy denote the input string, where x is that prefix of the input string that the DFA has read so far. If x = λ, the DFA will be in state λ. Otherwise, the DFA will be in that state whose two-symbol name is formed using the first and last symbols in x, respectively.


2. The language containing precisely those strings having an even number of occurrences of a and at least three occurrences of b.

Expressed using a set former: { x ∈ {a,b}*  |  #a(x) is even  ∧  #b(x) is at least three }

Solution:
Logic:
Let xy denote the input string, where x is that prefix of the input string that the DFA has read so far. Then the DFA will be in state [k,m], where
  • k = #a(x) (mod 2) and
  • m = max(3, #b(x)).


3. The language containing precisely those strings having aab as a substring.

Expressed using a set former: { xaaby  |  x,y ∈ {a,b}* }

Solution:
Logic:
Let xy denote the input string, where x is that prefix of the input string that the DFA has read so far. If aab (the "target" string) is a substring of x (and hence of xy), the DFA will be in the lone accepting state. Otherwise, the DFA will be in that state whose name corresponds to the longest prefix of aab that is also a suffix of x.


4. The language containing precisely those strings not having aba as a substring.

Solution:
Logic:
Let xy denote the input string, where x is that prefix of the input string that the DFA has read so far. If aba (the "target" string) is a substring of x (and hence of xy), the DFA will be in the lone non-accepting state. Otherwise, the DFA will be in that state whose name corresponds to the longest prefix of aba that is also a suffix of x.


5. The intersection of the languages in Problems 3 and 4. That is, the set of strings having aab as a substring but not having aba as a substring.

Solution:
Logic:
Let xy denote the input string, where x is that prefix of the input string that the DFA has read so far.
  • If x has aba as a substring, then the DFA is in (the dead) state aba.
  • If neither aab nor aba is a substring of x, then the DFA is in a state whose name z does not include brackets, and z is the longest suffix of x that is a prefix of either of the two "target" strings (aab or aba).
  • If aab is a substring of x but aba is not, then the DFA is in the state with name [z] (with brackets), where z is the longest suffix of x that is also a prefix of aba.