Open In App

Designing Non-Deterministic Finite Automata (Set 5)

Improve
Improve
Like Article
Like
Save
Share
Report

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 ‘ab’ 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 ‘ab’ as the substring but the below language is not accepted by this NFA because some of the string of below language does not contain ‘ab’ as the substring.

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

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

Code Implementation:

C++




#include <iostream>
#include <string>
 
class DFA {
public:
    void processInput(const std::string& input) {
        // Initial state
        char currentState = 'A';
 
        // Iterate through each character in the input string
        for (char c : input) {
            if (currentState == 'A' && c == 'a') {
                // Transition to state B when 'a' is encountered
                currentState = 'B';
            } else if (currentState == 'B' && c == 'b') {
                // Stay in state B when 'b' is encountered
                currentState = 'C';
            } else if (currentState == 'C' && c == 'b') {
                // Stay in state C when another 'b' is encountered
                // This ensures every 'a' is followed by 'bb'
            } else {
                // If any invalid transition occurs, print "Not Accepted" and return
                std::cout << "Not Accepted" << std::endl;
                return;
            }
        }
 
        // After processing the entire string, if the final state is C, print "Accepted"
        if (currentState == 'C') {
            std::cout << "Accepted" << std::endl;
        } else {
            // Otherwise, print "Not Accepted"
            std::cout << "Not Accepted" << std::endl;
        }
    }
};
 
int main() {
    // Create an instance of the DFA class
    DFA dfa;
 
    // Test the DFA with different input strings
    std::string input1 = "abb";
    dfa.processInput(input1);
 
  
    return 0;
}


In the above NFA, the initial state ‘X’ on getting ‘a’ as the input it either remains in the state of itself or transit to a state ‘Y’ and on getting ‘b’ as the input it remains in the state of itself. The state ‘Y’ on getting ‘b’ as the input it transmits 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.

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 ‘ab’ 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 ‘ab’ as the substring But the below language is not accepted by this NFA because some of the string of below language is containing ‘ab’ 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 ‘X’ on getting ‘b’ as the input it remains in the state of itself and on getting ‘a’ as the input it transits to another final state ‘Y’. Another final state ‘Y’ on getting ‘a’ as the input it remains in the state of itself.



Last Updated : 02 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads