Program to construct a DFA to check if a given integer is unsigned or not


In this problem, we need to check whether the given number is an unsigned integer using the DFA. We need to construct the DFA using the 2D array to solve the problem.

Problem statement – We have given string str of length N. By constructing the DFA, we need to check whether the str represents the unsigned integer. In the output, print ‘unsinged integer’ or ‘Not an unsinged integer’ according to whether the number is unsinged or not.

Sample examples

Input– str = "1729"

Output– “Unsigned integer.”

Explanation– As the number doesn’t contain any sign, it is an unsigned integer.

Input– str = “-1729”

Output– “Not an Unsigned integer.”

Explanation– The number contains a sign at the start, so it is not an unsigned integer.

Approach 1

Before we start with the solution, let’s understand how to construct the DFA to find an unsigned integer.

Here are some examples of unsigned integers.

  • 1234

  • 1234.4567

  • 1234E+215

  • 1234E-215

  • 123.456e+215

  • 123.456e+215

The above examples show that singed integers can contain digits, dot, E/e, + or -, and digits again.

Now, let’s construct the DFA, as shown below.

  • In the above DFA, we start from the 0th state. The number can start with any digit and contain multiple digits as we move to 1st state.

  • After 1st state, we can move to 2nd state if we get other characters or 3rd state if we get ‘.’ in decimal numbers.

  • After the 3rd state, we need at least 1 or more digits, and we can move to the 4th state.

  • From the 4th state, we can move to the 5th state if we get other characters to accept the digit and the 5th state if we get E/e.

  • We can go to the 6th state if we get ‘+’ or ‘-‘ after E/e. Here, E/e represents the exponential; after that, we need to write positive or negative power.

  • After that, we need to add power in digits. So, we can move to the 8th state.

  • If we get any character from the 8th state, we can move to the 9th state.

Now, we need to construct a DFA in the code.

In a 2D array, the first dimension represents the current state, and 2nd dimension represents the character type, as shown below.

  • Oth index is for digits.

  • 1st index is ‘+/-‘ sign.

  • 2nd index is for ‘.’

  • 3rd index is for other characters.

  • 4th index is for ‘e/E’.

Approach

  • Define the ‘digits’ string and add all digits. Also, define the ‘sign’, ‘dot’, and ‘ex’ variables and initialize them with respected values

  • Define the ‘dfa’ array to store the state and next state. Also, Define the constructDFA() function to build DFA using a 2D array.

  • In the constructDFA() function, Initialize the array with ‘10’ as initially, all states are unreachable.

  • We need to update the array value by their next state. For example, initially, we are at state 0 and can only move ahead if the string starts with a digit. So, update dfa[0][0] = 1.

  • In the isUnsignedInt() function, execute the constructDFA() function to build the DFA first.

  • Initialize the ‘current_state’ variable with 0 to track the state.

  • Start traversing the string.

  • Now, update the value of the ‘current_state’ to the next state by taking the value from the 2D array. For example, if the current character is a digit, update the ‘current_state’ variable’s value with dfa[current_state][0]. If the current character is a ‘+/-’ sign, update the current_state variable’s value with dfa[current_state][1].

  • Finally, if the value of ‘current_State’ is 1, 4, or 8. It means the given number is a valid signed integer.

Example

#include "bits/stdc++.h"
using namespace std;
string digits = "0123456789", sign = "+-";
string dot = ".", ex = "eE";
int dfa[11][5];
// function to construct DFA
void constructDFA() {
    // Initially, all states are unreachable from state 0, so connect all states to state 10 (dead state).
    for (int i = 0; i < 11; i++)
        for (int j = 0; j < 5; j++)
            dfa[i][j] = 10;
    // If the state is zero and we get a digit, then change the state to 1.
    dfa[0][0] = 1;
    // If the state is 1, and we get different characters, set the array value accordingly.
    dfa[1][0] = 1;
    dfa[1][2] = 3;
    dfa[1][3] = 2;
    dfa[1][4] = 6;
    // Also, Initialize the array value for states 3, 6, 7, 8.
    dfa[3][0] = 4;
    dfa[4][0] = 4;
    dfa[4][3] = 5;
    dfa[4][4] = 6;
    dfa[6][0] = 8;
    dfa[6][1] = 7;
    dfa[7][0] = 8;
    dfa[8][0] = 8;
    dfa[8][3] = 9;
}
// function to check if the given string is an unsigned integer or not
string isUnsinedInt(string s) {
    // Build the DFA
    constructDFA();
    // Initially, the current state is 0.
    int current_state = 0;
    // Traverse the string
    for (int i = 0; i < s.size(); i++) {
        // Check the type of the current character and change the state accordingly
        // If a digit occurs
        if (digits.find(s[i]) != digits.npos)
            // Change the state to 0.
            current_state = dfa[current_state][0];
        // If a sign occurs, change the state to 1.
        else if (sign.find(s[i]) != sign.npos)
            current_state = dfa[current_state][1];
        // If a dot occurs, change the state to 3.
        else if (dot.find(s[i]) != dot.npos)
            current_state = dfa[current_state][2];
        // If e/E or exponent sign occurs, change the state to 4.
        else if (ex.find(s[i]) != ex.npos)
            current_state = dfa[current_state][4];
        // If any other character occurs, change the state to 3.
        else
            current_state = dfa[current_state][3];
    }
    // If the current state is 1, 4, 8, then the number is an unsigned integer
    if (current_state == 1 || current_state == 4 || current_state == 8) {
       return "Unsigned integer";
    }
    else {
       return "Not an unsigned integer";
    }
}
int main() {
    string str = "1729";
    cout << "The given number is " << isUnsinedInt(str);
    return 0;
}

Output

The given number is Unsigned integer

Time complexity – O(N) as we traverse the string.

Space complexity – O(1) as we use constant space to store the DFA states.

In this tutorial, we construct the DFA to check whether a given string is an unsigned integer. Also, programmers can visualize the DFA via a given image and get a better understanding of DFA. After that, we constructed the same DFA using a 2D array. To get the result, we change the value of ‘current_state’ by the next state according to the current character. Programmers can try to solve other problems containing DFA construction.

Updated on: 18-Aug-2023

103 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements