Program to build a DFA that checks if a string ends with “01” or “10”
Last Updated :
14 Sep, 2022
DFA or Deterministic Finite Automata is a finite state machine which accepts a string(under some specific condition) if it reaches a final state, otherwise rejects it.
Problem: Given a string of ‘0’s and ‘1’s character by character, check for the last two characters to be “01” or “10” else reject the string. Also print the state diagram irrespective of acceptance or rejection. Since in DFA, there is no concept of memory, therefore we can only check for one character at a time, beginning with the 0th character. The input set for this problem is {0, 1}. For each character in the input set, each state of DFA redirects to another valid state.
DFA Machine: For the above problem statement, we must first build a DFA machine. DFA machine is similar to a flowchart with various states and transitions. DFA machine corresponding to the above problem is shown below, Q3 and Q4 are the final states:
Examples:
Input: 010101
Output:
State transitions are q0->q1->q3->q4
->q3->q4->q3->YES
Explanation : 010101 ends with "01".
Input: 0100
Output:
State transitions are q0->q1->q3->q4->q1->NO
Explanation : 0100 ends with "00",
which is not equal to any of "01" or "10".
Algorithm:
- Define the minimum number of states required to make the state diagram. Use functions to define various states.
- List all the valid transitions. Each state must have a transition for every valid symbol.
- Define the final states by applying the base condition.
- Define all the state transitions using state function calls.
- Define a returning condition for the end of the string.
For the given DFA Machine:
- Q0, Q1, Q2, Q3, Q4 are defined as the number of states.
- 0 and 1 are valid symbols. Each state has transitions for 0 and 1.
- Q3 and Q4 are defined as the final states.
- Suppose at state Q0, if 0 comes, the function call is made to Q1. So, if 1 comes, the function call is made to Q2.
- If the program reaches the end of the string, the output is made according to the state, the program is at.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void q1(string, int );
void q2(string, int );
void q3(string, int );
void q4(string, int );
void q1(string s, int i)
{
cout << "q1->" ;
if (i == s.length()) {
cout << "NO \n" ;
return ;
}
if (s[i] == '0' )
q1(s, i + 1);
else
q3(s, i + 1);
}
void q2(string s, int i)
{
cout << "q2->" ;
if (i == s.length()) {
cout << "NO \n" ;
return ;
}
if (s[i] == '0' )
q4(s, i + 1);
else
q2(s, i + 1);
}
void q3(string s, int i)
{
cout << "q3->" ;
if (i == s.length()) {
cout << "YES \n" ;
return ;
}
if (s[i] == '0' )
q4(s, i + 1);
else
q2(s, i + 1);
}
void q4(string s, int i)
{
cout << "q4->" ;
if (i == s.length()) {
cout << "YES \n" ;
return ;
}
if (s[i] == '0' )
q1(s, i + 1);
else
q3(s, i + 1);
}
void q0(string s, int i)
{
cout << "q0->" ;
if (i == s.length()) {
cout << "NO \n" ;
return ;
}
if (s[i] == '0' )
q1(s, i + 1);
else
q2(s, i + 1);
}
int main()
{
string s = "010101" ;
cout << "State transitions are " ;
q0(s, 0);
}
|
Java
class GFG
{
static void q1(String s, int i)
{
System.out.print( "q1->" );
if (i == s.length())
{
System.out.println( "NO" );
return ;
}
if (s.charAt(i) == '0' )
q1(s, i + 1 );
else
q3(s, i + 1 );
}
static void q2(String s, int i)
{
System.out.print( "q2->" );
if (i == s.length())
{
System.out.println( "NO " );
return ;
}
if (s.charAt(i) == '0' )
q4(s, i + 1 );
else
q2(s, i + 1 );
}
static void q3(String s, int i)
{
System.out.print( "q3->" );
if (i == s.length())
{
System.out.println( "YES" );
return ;
}
if (s.charAt(i) == '0' )
q4(s, i + 1 );
else
q2(s, i + 1 );
}
static void q4(String s, int i)
{
System.out.print( "q4->" );
if (i == s.length())
{
System.out.println( "YES" );
return ;
}
if (s.charAt(i) == '0' )
q1(s, i + 1 );
else
q3(s, i + 1 );
}
static void q0(String s, int i)
{
System.out.print( "q0->" );
if (i == s.length())
{
System.out.println( "NO" );
return ;
}
if (s.charAt(i) == '0' )
q1(s, i + 1 );
else
q2(s, i + 1 );
}
public static void main (String[] args)
{
String s = "010101" ;
System.out.print( "State transitions are " );
q0(s, 0 );
}
}
|
Python3
def q1(s, i) :
print ( "q1->" , end = "");
if (i = = len (s)) :
print ( "NO" );
return ;
if (s[i] = = '0' ) :
q1(s, i + 1 );
else :
q3(s, i + 1 );
def q2(s, i) :
print ( "q2->" , end = "");
if (i = = len (s)) :
print ( "NO" );
return ;
if (s[i] = = '0' ) :
q4(s, i + 1 );
else :
q2(s, i + 1 );
def q3(s, i) :
print ( "q3->" , end = "");
if (i = = len (s)) :
print ( "YES" );
return ;
if (s[i] = = '0' ) :
q4(s, i + 1 );
else :
q2(s, i + 1 );
def q4(s, i) :
print ( "q4->" , end = "");
if (i = = len (s)) :
print ( "YES" );
return ;
if (s[i] = = '0' ) :
q1(s, i + 1 );
else :
q3(s, i + 1 );
def q0( s, i) :
print ( "q0->" , end = "");
if (i = = len (s)) :
print ( "NO" );
return ;
if (s[i] = = '0' ) :
q1(s, i + 1 );
else :
q2(s, i + 1 );
if __name__ = = "__main__" :
s = "010101" ;
print ( "State transitions are" , end = " " );
q0(s, 0 );
|
C#
using System;
class GFG
{
static void q1( string s, int i)
{
Console.Write( "q1->" );
if (i == s.Length )
{
Console.WriteLine( "NO" );
return ;
}
if (s[i] == '0' )
q1(s, i + 1);
else
q3(s, i + 1);
}
static void q2( string s, int i)
{
Console.Write( "q2->" );
if (i == s.Length)
{
Console.WriteLine( "NO " );
return ;
}
if (s[i] == '0' )
q4(s, i + 1);
else
q2(s, i + 1);
}
static void q3( string s, int i)
{
Console.Write( "q3->" );
if (i == s.Length)
{
Console.WriteLine( "YES" );
return ;
}
if (s[i] == '0' )
q4(s, i + 1);
else
q2(s, i + 1);
}
static void q4( string s, int i)
{
Console.Write( "q4->" );
if (i == s.Length)
{
Console.WriteLine( "YES" );
return ;
}
if (s[i] == '0' )
q1(s, i + 1);
else
q3(s, i + 1);
}
static void q0( string s, int i)
{
Console.Write( "q0->" );
if (i == s.Length)
{
Console.WriteLine( "NO" );
return ;
}
if (s[i] == '0' )
q1(s, i + 1);
else
q2(s, i + 1);
}
public static void Main()
{
string s = "010101" ;
Console.Write( "State transitions are " );
q0(s, 0);
}
}
|
Javascript
<script>
function q1(s, i) {
document.write( "q1->" );
if (i === s.length) {
document.write( "NO" );
return ;
}
if (s[i] === "0" ) q1(s, i + 1);
else q3(s, i + 1);
}
function q2(s, i) {
document.write( "q2->" );
if (i === s.length) {
document.write( "NO " );
return ;
}
if (s[i] === "0" ) q4(s, i + 1);
else q2(s, i + 1);
}
function q3(s, i) {
document.write( "q3->" );
if (i === s.length) {
document.write( "YES" );
return ;
}
if (s[i] === "0" ) q4(s, i + 1);
else q2(s, i + 1);
}
function q4(s, i) {
document.write( "q4->" );
if (i === s.length) {
document.write( "YES" );
return ;
}
if (s[i] === "0" ) q1(s, i + 1);
else q3(s, i + 1);
}
function q0(s, i) {
document.write( "q0->" );
if (i === s.length) {
document.write( "NO" );
return ;
}
if (s[i] === "0" ) q1(s, i + 1);
else q2(s, i + 1);
}
var s = "010101" ;
document.write( "State transitions are " );
q0(s, 0);
</script>
|
Output:
State transitions are q0->q1->q3->q4->q3->q4->q3->YES
Time Complexity: O(n) where a string of length n requires traversal through n states.
Auxiliary Space: O(n)
There can be more than one possible DFA for a problem statement.
Share your thoughts in the comments
Please Login to comment...