# Designing Non-Deterministic Finite Automata (Set 3)

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 starts with ‘ab’.Â
Explanation: The desired language will be like:Â
Â

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

Here as we can see that each string of the above language starts with ‘ab’ and end with any alphabet either ‘a’ or ‘b’.Â
But the below language is not accepted by this NFA because none of the string of below language starts with ‘ab’.Â
Â

`L2 = {ba, ba, babaaa..............}`

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 state ‘Y’. The state ‘Y’ on getting ‘b’ as the input it transits to a final state ‘Z’. The final state ‘Z’ on getting either ‘a’ or ‘b’ as the input it remains in the state of itself.Â
Â

Â

## Python3

 `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 call``        ``#stateY function    ``        ``if` `(n[``0``]``=``=``'a'``):``            ``stateY(n[``1``:])``        ` `        ``#if at zero index ``        ``#'b' then print ``        ``#not accepted``        ``elif` `(n[``0``]``=``=``'b'``):``            ``print``("string ``not` `accepted")   ``       ` `def` `stateY(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' then print ``        ``#not accepted   ``        ``if` `(n[``0``]``=``=``'a'``):``            ``print``("string ``not` `accepted") ``            ` `        ``#if at zero index ``        ``#'b' found call``        ``#stateZ function    ``        ``elif` `(n[``0``]``=``=``'b'``):``            ``stateZ(n[``1``:]) ``            ` `def` `stateZ(n):``    ``#if length of n become 0 ``    ``#then print accepted``    ``if``(``len``(n)``=``=``0``):``        ``print``("string accepted")``        ` `    ``else``:  ``        ``#if at zero index ``        ``#'b' found call``        ``#stateZ function    ``        ``if` `(n[``0``]``=``=``'a'``):``            ``stateZ(n[``1``:]) ``            ` `        ``#if at zero index ``        ``#'b' found call``        ``#stateZ function    ``        ``elif` `(n[``0``]``=``=``'b'``):``            ``stateZ(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 starting with ‘ab’.Â
Explanation: The desired language will be like:Â
Â

`L1 = {ba, bba, bbaa, ...........}`

Here as we can see that each string of the above language is not starting with ‘ab’ but can end with either ‘a’ or ‘b’.Â
But the below language is not accepted by this NFA because some of the string of below language starts with ‘ab’.Â
Â

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

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

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

Â

## C++

 `#include ``#include ` `// Function declarations``void` `stateX(``const` `std::string& n);``void` `stateY(``const` `std::string& n);``void` `stateZ(``const` `std::string& n);` `// Function implementations``void` `stateZ(``const` `std::string& n) {``    ``if` `(n.empty()) {``        ``std::cout << ``"string accepted"` `<< std::endl;``    ``} ``else` `if` `(n[0] == ``'a'``) {``        ``stateZ(n.substr(1));``    ``} ``else` `if` `(n[0] == ``'b'``) {``        ``stateZ(n.substr(1));``    ``}``}` `void` `stateY(``const` `std::string& n) {``    ``if` `(n.empty()) {``        ``std::cout << ``"string not accepted"` `<< std::endl;``    ``} ``else` `if` `(n[0] == ``'a'``) {``        ``std::cout << ``"string not accepted"` `<< std::endl;``    ``} ``else` `if` `(n[0] == ``'b'``) {``        ``stateZ(n.substr(1));``    ``}``}` `void` `stateX(``const` `std::string& n) {``    ``if` `(n.empty()) {``        ``std::cout << ``"string not accepted"` `<< std::endl;``    ``} ``else` `if` `(n[0] == ``'a'``) {``        ``stateY(n.substr(1));``    ``} ``else` `if` `(n[0] == ``'b'``) {``        ``std::cout << ``"string not accepted"` `<< std::endl;``    ``}``}` `int` `main() {``    ``// Example input``    ``std::string input = ``"bba"``;``    ``stateX(input);``    ``return` `0;``}`

## Java

 `import` `java.util.Scanner;` `public` `class` `StateMachine {` `    ``// State Z``    ``static` `void` `stateZ(String n) {``        ``// If the string is empty, print "string accepted"``        ``if` `(n.isEmpty()) {``            ``System.out.println(``"string accepted"``);``        ``} ``else` `if` `(n.charAt(``0``) == ``'a'``) {``            ``// If 'a' is found at the beginning, transition to state Z with the rest of the string``            ``stateZ(n.substring(``1``));``        ``} ``else` `if` `(n.charAt(``0``) == ``'b'``) {``            ``// If 'b' is found at the beginning, transition to state Z with the rest of the string``            ``stateZ(n.substring(``1``));``        ``}``    ``}` `    ``// State Y``    ``static` `void` `stateY(String n) {``        ``// If the string is empty, print "string not accepted"``        ``if` `(n.isEmpty()) {``            ``System.out.println(``"string not accepted"``);``        ``} ``else` `if` `(n.charAt(``0``) == ``'a'``) {``            ``// If 'a' is found at the beginning, print "string not accepted"``            ``System.out.println(``"string not accepted"``);``        ``} ``else` `if` `(n.charAt(``0``) == ``'b'``) {``            ``// If 'b' is found at the beginning, transition to state Z with the rest of the string``            ``stateZ(n.substring(``1``));``        ``}``    ``}` `    ``// State X``    ``static` `void` `stateX(String n) {``        ``// If the string is empty, print "string not accepted"``        ``if` `(n.isEmpty()) {``            ``System.out.println(``"string not accepted"``);``        ``} ``else` `if` `(n.charAt(``0``) == ``'a'``) {``            ``// If 'a' is found at the beginning, transition to state Y with the rest of the string``            ``stateY(n.substring(``1``));``        ``} ``else` `if` `(n.charAt(``0``) == ``'b'``) {``            ``// If 'b' is found at the beginning, print "string not accepted"``            ``System.out.println(``"string not accepted"``);``        ``}``    ``}` `    ``public` `static` `void` `main(String[] args) {``        ``// Example input``        ``String input = ``"bba"``;``        ` `        ``// Call stateX function to check the input``        ``stateX(input);``    ``}``}`

## Python3

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

Previous
Next