3

Is the following language regular?

$$L = \{x \in \{a, b\}^* \mid \text{in every prefix \(w\) of \(x\), } 0 \le |w|_a − |w|_b \le 2\}$$

If so, give a DFA for it with as few states as possible. If you claim that it is not regular give a pumping lemma proof that it is not regular.

I'm doing exam prep and have tried pumping this to no avail. I think I created a DFA that works but I'm not entirely sure because pumping similar examples (i.e. if the number of a's is strictly greater than b's) yielded results that are not regular. Is my DFA correct? Or is there something I can pump that will fail the lemma?

DFA

4 Answers4

4

Hint: as you read each letter of the input, imagine trying to keep track of the difference between the number of $a$'s and the number of $b$'s. What is the set of possible values that this could possibly take on, if the input is in $L$? What's the largest this difference could be? What's the smallest it could be?

D.W.
  • 167,959
  • 22
  • 232
  • 500
3

Since your automaton is non-deterministic, it is hard to tell whether it is doing what it is supposed to. When trying to construct an automaton in order to prove a given language to be regular, I remind myself that these machines have a very limited memory, so I try to find out what is the variable to keep in track when reading the input string, and what are its possible "states". This is the reason why this language is regular, whereas the other one you mention (number of a's is strictly greater than b's) is not : there is something small to keep in track (with constant possible states) when recognizing its strings. It is not very different from the language of strings in $\{0,1\}^*$ with an even number of 1s, or the language of strings with an equal number of '01's and '10's substrings.

2

Building an automaton for a language is usually not enough to convince a professor. Worse, your comments show it is not enough to convince yourself it works.

So, your task is now to assign a meaning to each of the states: what do they represent?

I think your automaton still needs some attention besides that. For instance, it is not deterministic.

Hendrik Jan
  • 31,459
  • 1
  • 54
  • 109
1

This language is regular. A minimal DFA will have one state corresponding to each of the following possibilities:

  1. The prefix has $\#_a = \#_b + 0$. This is the start state for the DFA. This is an accepting state.
  2. The prefix has $\#_a = \#_b + 1$. This is an accepting state.
  3. The prefix has $\#_a = \#_b - 1$. This is an accepting state.
  4. The prefix has $\#_a = \#_b + 2$. This is an accepting state.
  5. The prefix has $\#_a = \#_b - 2$. This is an accepting state.
  6. The prefix satisfies none of the conditions given above. This is not an accepting state.

Transitions will be as follows: while in the state corresponding to $\#_a = \#_b + i$, where $-2 \le i \le 2$, transition to the state corresponding to $\#_a = \#_b + i + 1$ on seeing $a$ and the state corresponding to $\#_a = \#_b + i - 1$ on seeing $b$. If the target state is not one of the ones given in items 1-5 above, then transition instead to the dead state described in item 6; when in the dead state, all transitions are onto the dead state itself.

Patrick87
  • 12,924
  • 1
  • 45
  • 77