# Designing Non-Deterministic Finite Automata (Set 4)

**Prerequisite:** Finite Automata Introduction

In this article, we will see some designing of Non-Deterministic Finite Automata (NFA).

**Problem-1:** Construction of a minimal NFA accepting a set of strings over {a, b} in which each string of the language contain ‘a’ as the substring.**Explanation:** The desired language will be like:

L1 = {ab, abba, abaa, ...........}

Here as we can see that each string of the above language contains ‘a’ as the substring. But the below language is not accepted by this NFA because some of the string of below language does not contain ‘a’ as the substring.

L2 = {bb, b, bbbb, .............}

The state transition diagram of the desired language will be like below:

In the above NFA, the initial state ‘X’ on getting ‘a’ as the input it transits to a final state ‘Y’ and on getting ‘b’ as the input it remains in the state of itself. The final state ‘Y’ on getting either ‘a’ or ‘b’ as the input it remains in the state of itself. Refer for DFA of above NFA.

### Transition Table :

In this table initial state is depictedd by —> and final state is depicted by *.

STATES | INPUT (a) | INPUT (b) |

—> X | Y* | X |

Y* | Y* | Y* |

### Python implementation:

`def` `stateX(n):` ` ` `#if length of n become 0 ` ` ` `#then print not accepted` ` ` `if` `(` `len` `(n)` `=` `=` `0` `):` ` ` `print` `(` `"string not accepted"` `)` ` ` ` ` `else` `: ` ` ` `#if at zero index ` ` ` `#'a' found then call` ` ` `#stateY function` ` ` `if` `(n[` `0` `]` `=` `=` `'a'` `):` ` ` `stateY(n[` `1` `:]) ` ` ` ` ` `#if at zero index ` ` ` `#'b' then call` ` ` `#stateX function` ` ` `elif` `(n[` `0` `]` `=` `=` `'b'` `):` ` ` `stateX(n[` `1` `:])` ` ` `def` `stateY(n):` ` ` `#if length of n become 0 ` ` ` `#then print accepted` ` ` `if` `(` `len` `(n)` `=` `=` `0` `):` ` ` `print` `(` `"string accepted"` `)` ` ` ` ` `else` `: ` ` ` `#if at zero index ` ` ` `#'a' found call` ` ` `#stateY function ` ` ` `if` `(n[` `0` `]` `=` `=` `'a'` `):` ` ` `stateY(n[` `1` `:]) ` ` ` ` ` `#if at zero index` ` ` `#'b' found call` ` ` `#stateY function` ` ` `elif` `(n[` `0` `]` `=` `=` `'b'` `):` ` ` `stateY(n[` `1` `:]) ` ` ` ` ` `#take input` `n` `=` `input` `()` ` ` `#call stateA function` `#to check the input` `stateX(n)` |

**Problem-2:** Construction of a minimal NFA accepting a set of strings over {a, b} in which each string of the language is not containing ‘a’ as the substring.**Explanation:** The desired language will be like:

L1 = {b, bb, bbbb, ...........}

Here as we can see that each string of the above language is not containing ‘a’ as the substring But the below language is not accepted by this NFA because some of the string of below language is containing ‘a’ as the substring.

L2 = {ab, aba, ababaab..............}

The state transition diagram of the desired language will be like below:

In the above NFA, the initial and final state ‘Y’ on getting ‘b’ as the input it remains in the state of itself.

### Transition Table :

In this table initial state is depictedd by —> and final state is depicted by *.

STATES | INPUT (a) | INPUT (b) |

—> Y * | Y* | Y* |

### Python Implementation:

`def` `stateY(n):` ` ` `#if length of n become 0 ` ` ` `#then print accepted` ` ` `if` `(` `len` `(n)` `=` `=` `0` `):` ` ` `print` `(` `"string accepted"` `)` ` ` ` ` `else` `: ` ` ` `#if at zero index ` ` ` `#'a' found then ` ` ` `#print not accepted ` ` ` `if` `(n[` `0` `]` `=` `=` `'a'` `):` ` ` `print` `(` `"String not accepted"` `)` ` ` ` ` `#if at zero index` ` ` `#'b' found call` ` ` `#stateY function` ` ` `elif` `(n[` `0` `]` `=` `=` `'b'` `):` ` ` `stateY(n[` `1` `:]) ` ` ` ` ` `#take input` `n` `=` `input` `()` ` ` `#call stateY function` `#to check the input` `stateY(n)` |

Attention reader! Don’t stop learning now. Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in **GATE Test Series Course**.

Learn all **GATE CS concepts with Free Live Classes** on our youtube channel.