2

Consider the alphabet {0, 1}. How do I find the regular expression for the set of all strings such that each block of five consecutive symbols contains at least two 0's?

Here, by block I mean a given set of concurrent substrings. For example, for a string "0011010", the first five characters are "00110", which satisfies the above condition. The next block leaves out only the first character of the given string, giving me "01101", which again satisfies the condition.

I also need help in converting the regular expression into an NFA.

Ramon Zarate
  • 23
  • 1
  • 3

3 Answers3

2

The states of your DFA are pairs of integers $(i,j)$ with $1 \le i \le j \le 5$, plus a $\mathrm{REJECT}$ state. Intuitively state $(i,j)$ means that the two most recent $0$s have been encountered when $i$-th to last and $j$-th to last character were read.

The transition function $\delta$ satisfies $\delta(\mathrm{REJECT}, x)=\mathrm{REJECT}$ for $x \in \{0,1\}$ while, for any other state $(i,j)$, $\delta$ is defined as follows: $$ \delta( (i,j), x) = \begin{cases} (1, i+1) & \mbox{if } x=0 \mbox{ and } i<5\\ \mathrm{REJECT} & \mbox{if } x=0 \mbox{ and } i=5\\ (i+1, j+1) & \mbox{if } x=1 \mbox{ and } j<5\\ \mathrm{REJECT} & \mbox{if } x=1 \mbox{ and }j=5. \end{cases} $$

The initial state is $(1,1)$. All states, except for $\mathrm{REJECT}$, are accepting states.

Steven
  • 29,724
  • 2
  • 29
  • 49
1

Let's try to understand the structure of words in your language.

Consider any word in the language whose length is at least $5$ (we can enumerate over shorter words separately). We can write it as $$ 1^{i_1} 0 1^{i_2} 0 \cdots 0 1^{i_m}. $$ Our assumption on the length implies that $m \geq 2$, since otherwise the word is in $1^*$, so has to be shorter than $5$ letters. You can check that a word of this form is in the language iff $i_j + i_{j+1} \leq 3$ for all $j$.

Let $r$ be a regular expression for all words of the form above (but without the requirements that the length be at least $5$ and that $m \geq 2$) satisfying additionally the condition $i_j \leq 2$. Then a regular expression for your language is $$ (0+1+\epsilon)^4 + (11100+\epsilon)r(0011100r)^*(00111+\epsilon). $$ Let $s$ be a regular expression for all words of the form above satisfying additionally the condition $i_j \leq 1$. A regular expression for $r$ is $$ r = 11 + (110+\epsilon)s(0110s)^*(011+\epsilon). $$ Finally, a regular expression for $s$ is $$ s = (0+10)^*(1+\epsilon). $$

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514
0

To construct a Deterministic Finite Automaton (DFA) that accepts strings of 'a's and 'b's, where each block of 5 consecutive symbols has at least two 'a's, follow these steps:

Step 1: Define the States

Create 6 states (q0 to q5) representing the number of 'a's encountered in the current 5-symbol block.

Step 2: Define the Transitions

  • δ(q, a) = q + 1 (if q < 5)
  • δ(q, b) = 0 (if q = 0 or q = 5)
  • δ(q, b) = q (otherwise)

Step 3: Define the Initial and Final States

Initial State: q0 Final States: q2, q3, q4, q5

Step 4: Construct the DFA

States a b
q0 q1 q0
q1 q2 q1
q2 q3 q2
q3 q4 q2
q4 q5 q2
q5 q3 q0

The DFA ensures that each block of 5 consecutive symbols has at least two 'a's.

Rajendra
  • 1
  • 1