Program to build a DFA that checks if a string ends with “01” or “10”

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:
DFA- image

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:



  1. Define the minimum number of states required to make the state diagram. Use functions to define various states.
  2. List all the valid transitions. Each state must have a transition for every valid symbol.
  3. Define the final states by applying the base condition.
  4. Define all the state transitions using state function calls.
  5. Define a returning condition for the end of the string.

For the given DFA Machine:

  1. Q0, Q1, Q2, Q3, Q4 are defined as the number of states.
  2. 0 and 1 are valid symbols. Each state has transitions for 0 and 1.
  3. Q3 and Q4 are defined as the final states.
  4. 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.
  5. If the program reaches the end of the string, the output is made according to the state, the program is at.

Implementation:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to DFA that accepts string ending
// with 01 or 10.
  
#include <bits/stdc++.h>
using namespace std;
  
// Various states of DFA machine are defined
// using functions.
void q1(string, int);
void q2(string, int);
void q3(string, int);
void q4(string, int);
  
// End position is checked using the string
// length value.
// q0 is the starting state.
// q1 and q2 are intermediate states.
// q3 and q4 are final states.
void q1(string s, int i)
{
    cout << "q1->";
  
    if (i == s.length()) {
        cout << "NO \n";
        return;
    }
  
    // state transitions
    // 0 takes to q1, 1 takes to q3
    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;
    }
  
    // state transitions
    // 0 takes to q4, 1 takes to q2
    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;
    }
  
    // state transitions
    // 0 takes to q4, 1 takes to q2
    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;
    }
  
    // state transitions
    // 0 takes to q1, 1 takes to q3
    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;
    }
  
    // state transitions
    // 0 takes to q1, 1 takes to q2
    if (s[i] == '0')
        q1(s, i + 1);
    else
        q2(s, i + 1);
}
  
// Driver Code
int main()
{
    string s = "010101";
    // all state transitions are printed.
    // if string is accpetable, YES is printed.
    // else NO is printed
    cout << "State transitions are ";
    q0(s, 0);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to DFA that accepts string ending 
// with 01 or 10. 
class GFG 
{
      
    // End position is checked using the string 
    // length value. 
    // q0 is the starting state. 
    // q1 and q2 are intermediate states. 
    // q3 and q4 are final states. 
    static void q1(String s, int i) 
    
        System.out.print("q1->"); 
        if (i == s.length()) 
        
            System.out.println("NO"); 
            return
        
      
        // state transitions 
        // 0 takes to q1, 1 takes to q3 
        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
        
      
        // state transitions 
        // 0 takes to q4, 1 takes to q2 
        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
        
      
        // state transitions 
        // 0 takes to q4, 1 takes to q2 
        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
        
      
        // state transitions 
        // 0 takes to q1, 1 takes to q3 
        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
        
      
        // state transitions 
        // 0 takes to q1, 1 takes to q2 
        if (s.charAt(i) == '0'
            q1(s, i + 1); 
        else
            q2(s, i + 1); 
    
      
    // Driver Code 
    public static void main (String[] args) 
    
        String s = "010101"
          
        // all state transitions are printed. 
        // if string is accpetable, YES is printed. 
        // else NO is printed 
        System.out.print("State transitions are "); 
        q0(s, 0); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to DFA that accepts string ending 
# with 01 or 10. 
  
# End position is checked using the string 
# length value. 
# q0 is the starting state. 
# q1 and q2 are intermediate states. 
# q3 and q4 are final states. 
def q1(s, i) : 
  
    print("q1->", end=""); 
  
    if (i == len(s)) : 
        print("NO"); 
        return
  
    # state transitions 
    # 0 takes to q1, 1 takes to q3 
    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
  
    # state transitions 
    # 0 takes to q4, 1 takes to q2 
    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
  
    # state transitions 
    # 0 takes to q4, 1 takes to q2 
    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
  
    # state transitions 
    # 0 takes to q1, 1 takes to q3 
    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
  
    # state transitions 
    # 0 takes to q1, 1 takes to q2 
    if (s[i] == '0') :
        q1(s, i + 1); 
    else :
        q2(s, i + 1);
  
# Driver Code 
if __name__ == "__main__"
    s = "010101"
      
    # all state transitions are printed. 
    # if string is accpetable, YES is printed. 
    # else NO is printed 
    print("State transitions are", end = " "); 
    q0(s, 0); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to DFA that accepts string ending 
// with 01 or 10.
using System;
  
class GFG 
{
      
    // End position is checked using the string 
    // length value. 
    // q0 is the starting state. 
    // q1 and q2 are intermediate states. 
    // q3 and q4 are final states. 
    static void q1(string s, int i) 
    
        Console.Write("q1->"); 
        if (i == s.Length )
        
            Console.WriteLine("NO"); 
            return
        
      
        // state transitions 
        // 0 takes to q1, 1 takes to q3 
        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
        
      
        // state transitions 
        // 0 takes to q4, 1 takes to q2 
        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
        
      
        // state transitions 
        // 0 takes to q4, 1 takes to q2 
        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
        
      
        // state transitions 
        // 0 takes to q1, 1 takes to q3 
        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
        
      
        // state transitions 
        // 0 takes to q1, 1 takes to q2 
        if (s[i] == '0'
            q1(s, i + 1); 
        else
            q2(s, i + 1); 
    
      
    // Driver Code 
    public static void Main() 
    
        string s = "010101"
          
        // all state transitions are printed. 
        // if string is accpetable, YES is printed. 
        // else NO is printed 
        Console.Write("State transitions are "); 
        q0(s, 0); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

State transitions are q0->q1->q3->q4->q3->q4->q3->YES

Complexity: O(n) where a string of length n requires traversal through n states.

There can be more than one possible DFA for a problem statement.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01