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:
Transition Table :
In this table initial state is depicted by —> and final state is depicted by *.
STATES | INPUT (a) | INPUT (b) |
—> X | Y* | X |
Y* | Y* | Y* |
Python implementation:
#include <iostream> #include <string> // Function declarations void stateX( const std::string& n);
void stateY( const std::string& n);
// Function implementations void stateY( const std::string& n) {
if (n.empty()) {
std::cout << "string accepted" << std::endl;
} else if (n[0] == 'a' ) {
stateY(n.substr(1));
} else if (n[0] == 'b' ) {
stateY(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' ) {
stateX(n.substr(1));
}
} int main() {
// Assumed string
std::string assumedString = "bab" ;
// Call stateX function to check the assumed string
stateX(assumedString);
return 0;
} |
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:
Transition Table :
In this table initial state is depicted by —> and final state is depicted by *.
STATES | INPUT (a) | INPUT (b) |
—> Y * | Y* | Y* |
Implementation:
import java.util.*;
import java.io.*;
class GFG
{ public static void stateY(StringBuilder n)
{
// if length of n become 0
// then print accepted
if (n.length() == 0 )
System.out.println( "string accepted" );
else
{
// if at zero index
// 'a' found then
// print not accepted
if (n.charAt( 0 )== 'a' )
System.out.println( "String not accepted" );
// if at zero index
// 'b' found call
// stateY function
else if (n.charAt( 0 )== 'b' )
{
n.deleteCharAt( 0 );
stateY(n);
}
}
}
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
// take input
String n = sc.next();
StringBuilder b = new StringBuilder(n);
// call stateY function
// to check the input
stateY(b);
}
} // This code is contributed by aakash_k. |
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) |