83% found this document useful (6 votes)
5K views

Implementing A DFA (Deterministic Finite Automata) in C++

The experiment implements a DFA with 3 states (q0, q1, q2) and alphabet {0, 1} to recognize strings starting with 1. The transition table and a regular expression are given. Strings 10011 and 0011 are tested, with 10011 accepted and 0011 rejected as expected. The DFA operates by transitioning between states deterministically based on input symbols, starting at the initial state and accepting if the final state is reached. DFAs are useful for applications like validating input formats.

Uploaded by

Rizul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
83% found this document useful (6 votes)
5K views

Implementing A DFA (Deterministic Finite Automata) in C++

The experiment implements a DFA with 3 states (q0, q1, q2) and alphabet {0, 1} to recognize strings starting with 1. The transition table and a regular expression are given. Strings 10011 and 0011 are tested, with 10011 accepted and 0011 rejected as expected. The DFA operates by transitioning between states deterministically based on input symbols, starting at the initial state and accepting if the final state is reached. DFAs are useful for applications like validating input formats.

Uploaded by

Rizul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment 1

AIM: To implement a DFA of your choice using C++.

Theory
In DFA, for each input symbol, one can determine the state to which the machine will move.
Hence, it is called Deterministic Automaton. As it has a finite number of states, the machine
is called Deterministic Finite Machine or Deterministic Finite Automaton.

Formal Definition of a DFA

A DFA can be represented by a 5-tuple (Q, ∑, δ, q0, F) where −

• Q is a finite set of states.

• ∑ is a finite set of symbols called the alphabet.

• δ is the transition function where δ: Q × ∑ → Q

• q0 is the initial state from where any input is processed (q0 ∈ Q).

• F is a set of final state/states of Q (F ⊆ Q).

A DFA is represented by digraphs called state diagram.

• The vertices represent the states.

• The arcs labelled with an input alphabet show the transitions.

• The initial state is denoted by an empty single incoming arc.

• The final state is indicated by double circles.

1. Make a DFA.

3 states.
q0 -> Initial State
q1 -> Final State

Alphabet (Σ) -> {0, 1}


The DFA has 2 arrows coming out of every state as there are 2 characters in the
alphabet.

2. Make transition table


Next State
Present State 0 1
->q0 q2 q1*
q1* q1* q1*
q2 q2 q2

3. Understand the DFA

Regular Expression (using intuition) = 1(1 + 0)*

Using Arden’s theorem:


q0 = Λ (lambda)
q1 = q01 + q1(1 + 0)
q1 = 1 + q1(1 + 0)
q1 = 1(1 + 0)*
Language of the DFA
As the Regular expression clearly shows, the DFA accepts the language on {0, 1}
which only contains the strings starting with 1.

4. Choose 2 strings

W1 = 10011 -> should be accepted by the DFA


W2 = 0011 -> should not be accepted by the DFA

These strings are checked for acceptance by using them as input for the DFA that we
will create on the computer.
The results of their acceptance will give us an intuition on whether the DFA is
working correctly or not.
Code

#include<iostream>
#include<string>
using namespace std;

int main()
{
int table[3][2] = {{2, 1}, {1, 1}, {2, 2}};

string input;
int state = 0;

cout<<"Program that accepts strings starting with 1:\nEnter String: ";


cin>>input;
cout<<input<<endl;

cout<<"Transitions: \n";
for(int i = 0; i < input.length(); i++)
{
cout<<"(q"<<state<<", "<<input[i]<<") -> q";
state = table[state][input[i] - '0'];
cout<<state<<endl;
}

if(state == 1)
cout<<"\nString accepted!";
else
cout<<"\nNot accepted!";
return 0;
}

Output

#1

#2
Discussion and Result

As it can be seen from the output obtained, we get the result that we expected.
W = 10011 was accepted by the DFA as it starts with 1, while,
W = 0011 was not accepted by it as it starts with 0 and not 1.

The automaton takes a finite sequence of 0s and 1s as input. For each state, there is a
transition arrow leading out to a next state for both 0 and 1. Upon reading a symbol, a DFA
jumps deterministically from one state to another by following the transition arrow.

For example, if the automaton is currently in state q0 and the current input symbol is
1, then it deterministically jumps to state q1.

A DFA has a start state (denoted graphically by an arrow coming in from nowhere)
where computations begin, and a set of accept states (denoted graphically by a
double circle) which help define when a computation is successful.

A DFA is defined as an abstract mathematical concept, but is often implemented in


hardware and software for solving various specific problems.

For example, a DFA can model software that decides whether or not online user input
such as email addresses are valid.

DFAs recognize exactly the set of regular languages, which are, among other things, useful
for doing lexical analysis and pattern matching.

You might also like