Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

PROJECT REPORT OF “NFA to DFA”

Submitted in partial fulfillment of the requirements for the award degree


of
Bachelor of Technology
In
Computer Science and Engineering

Bundelkhand Institute of Engineering and Technology, Jhansi (U.P)


(BIET Jhansi)
ACADEMIC SESSION 2022-23

Submitted By
Vivek Kumar (2100430100059)
Shrawan Kumar (2100430100050)
Nikhil Kumar (2100430100033)
Abhinav Sharma(2200430109001)

Under the guidance of


Prof. Shashank Gupta

1
ACKNOWLEDGEMENT

We are indebted to our respected Head of the Department Dr. Yashpal Singh for
guiding us. The team is also grateful to our project guide Prof. Shashank Gupta, for their
invaluable guidance for this project. We also thank our respected faculty members,
Dr. R.N. Verma, Dr. Sanjay Kumar Gupta of the Computer Science and Engineering
department for their indomitable contribution and guidance without which this project
would have been impossible to complete.
Our sincerest thanks to all the teachers, seniors and colleagues whose help and
guidance brought this project to successful completion.

Submitted by:

Vivek Kumar (2100430100059)


Shrawan Kumar (2100430100050)
Nikhil Kumar (2100430100033)
Abhinav Sharma (2200430109001)

2
ABSTRACTION
This project based upon the subject of “Theory of Automata and Formal Languages”. In

this project, We make the program of convert NFA to DFA. The code written in C++

language. In this project we use an efficient algorithm to convert Non-Deterministic

Finite Automata (NFA) to Deterministic Finite Automata (DFA) for language

recognition and simplification.

3
LIST OF FIGURES

S.NO. TABLE

1. Introduction

2. Source Code

3. Reference

4
Introduction

Converting a Non-Deterministic Finite Automaton (NFA) to a Deterministic Finite


Automaton (DFA) is a fundamental concept in automata theory and is known as
the subset construction method. The process involves transforming an NFA into an
equivalent DFA that recognizes the same language. The conversion is essential as
DFAs are often easier to implement and analyze than NFAs.
Here's a step-by-step theory for converting an NFA to a DFA:
1. NFA representation: The NFA consists of a finite set of states, a finite
alphabet of input symbols, a transition function that maps a state and input
symbol to a set of possible states, a start state, and a set of accept states (or
final states).
2. DFA representation: The DFA will have the same finite set of states and
alphabet as the NFA. However, its transition function will map a state and
input symbol to a single state (not a set of states), it will have a single start
state, and a state or set of states will be designated as the accept state(s).
3. Initial step: The initial step in converting an NFA to a DFA is to create the
DFA's start state, which is the ε-closure (epsilon-closure) of the NFA's start
state. The ε-closure of a state is the set of states that can be reached from the
initial state using only epsilon (ε) transitions.
4. Transitions: For each state in the DFA and for each input symbol in the
alphabet, follow these sub-steps: a. Apply the ε-closure to the current state to
get a set of NFA states. b. For each NFA state in the set, determine the set of
states that can be reached by consuming the current input symbol. c. Apply
the ε-closure to the set obtained in step b to get the next state in the DFA.

6
5. Accept states: Any DFA state that contains at least one NFA accept state
will also be an accept state in the DFA.
6. Repeat: Repeat steps 4 and 5 until no new states can be added to the DFA.
7. Minimization (optional): After converting the NFA to a DFA, you may
apply a state minimization algorithm to eliminate any redundant states and
optimize the DFA.
The result is an equivalent DFA that recognizes the same language as the original
NFA. The conversion process can be implemented algorithmically, and various
programming languages can be used to perform the conversion. Note that in some
cases, the DFA may have more states than the original NFA due to the subset
construction process.

7
CODE:

#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef set<int> StateSet;


typedef map<pair<StateSet, char>, StateSet> TransitionMap;

StateSet epsilonClosure(const StateSet& states, const TransitionMap& transitions) {


StateSet closure = states;
queue<int> stateQueue;

for (int state : states) {


stateQueue.push(state);
}

while (!stateQueue.empty()) {
int currentState = stateQueue.front();
stateQueue.pop();

auto epsilonTransitions = transitions.find({currentState, 'e'});


if (epsilonTransitions != transitions.end()) {
for (int nextState : epsilonTransitions->second) {
if (closure.find(nextState) == closure.end()) {
closure.insert(nextState);
stateQueue.push(nextState);
}
}
}
}

return closure;
}

StateSet move(const StateSet& states, char symbol, const TransitionMap& transitions) {


8
StateSet nextStateSet;

for (int state : states) {


auto symbolTransitions = transitions.find({state, symbol});
if (symbolTransitions != transitions.end()) {
for (int nextState : symbolTransitions->second) {
nextStateSet.insert(nextState);
}
}
}

return nextStateSet;
}

void nfaToDfa(const vector<StateSet>& nfaStates, const TransitionMap& nfaTransitions, const


StateSet& nfaStartState,
const set<int>& nfaAcceptStates, const set<char>& alphabet,
vector<StateSet>& dfaStates, TransitionMap& dfaTransitions,
StateSet& dfaStartState, set<StateSet>& dfaAcceptStates) {
map<StateSet, int> stateIndices;
queue<StateSet> stateQueue;

dfaStates.push_back(epsilonClosure(nfaStartState, nfaTransitions));
stateIndices[dfaStates[0]] = 0;
dfaStartState = dfaStates[0];

stateQueue.push(dfaStates[0]);

while (!stateQueue.empty()) {
StateSet currentStateSet = stateQueue.front();
stateQueue.pop();

for (char symbol : alphabet) {


StateSet nextStateSet = move(currentStateSet, symbol, nfaTransitions);
StateSet epsilonClosureSet = epsilonClosure(nextStateSet, nfaTransitions);

if (!epsilonClosureSet.empty()) {
if (stateIndices.find(epsilonClosureSet) == stateIndices.end()) {
int newStateIndex = dfaStates.size();
dfaStates.push_back(epsilonClosureSet);
stateIndices[epsilonClosureSet] = newStateIndex;

stateQueue.push(epsilonClosureSet);
}

9
dfaTransitions[{currentStateSet, symbol}] = epsilonClosureSet;
}
}
}

for (const auto& stateSet : dfaStates) {


for (int acceptState : nfaAcceptStates) {
if (stateSet.find(acceptState) != stateSet.end()) {
dfaAcceptStates.insert(stateSet);
break;
}
}
}
}

int main() {
// Example NFA
vector<StateSet> nfaStates = {{0}, {1}, {2}, {3}};
TransitionMap nfaTransitions = {
{{0, '0'}, {0}},
{{0, '1'}, {0}},
{{0, 'e'}, {1}},
{{1, '0'}, {1}},
{{1, '1'}, {1}},
{{1, 'e'}, {2}},
{{2, '0'}, {2}},
{{2, '1'}, {2}},
{{2, 'e'}, {3}},
{{3, '0'}, {3}},
{{3, '1'}, {3}}
};
StateSet nfaStartState = {0};
set<int> nfaAcceptStates = {3};
set<char> alphabet = {'0', '1'};

// Convert NFA to DFA


vector<StateSet> dfaStates;
TransitionMap dfaTransitions;
StateSet dfaStartState;
set<StateSet> dfaAcceptStates;

nfaToDfa(nfaStates, nfaTransitions, nfaStartState, nfaAcceptStates, alphabet,


dfaStates, dfaTransitions, dfaStartState, dfaAcceptStates);

// Print the DFA states and transitions


10
cout << "DFA States: ";
for (const auto& stateSet : dfaStates) {
for (int state : stateSet) {
cout << state << " ";
}
cout << "| ";
}
cout << endl;

cout << "DFA Transitions:" << endl;


for (const auto& transition : dfaTransitions) {
const pair<StateSet, char>& from = transition.first;
const StateSet& to = transition.second;

for (int state : from.first) {


cout << state << " ";
}
cout << "(" << from.second << ") -> ";

for (int state : to) {


cout << state << " ";
}
cout << "| ";
}
cout << endl;

cout << "DFA Start State: ";


for (int state : dfaStartState) {
cout << state << " ";
}
cout << endl;

cout << "DFA Accept States: ";


for (const auto& stateSet : dfaAcceptStates) {
for (int state : stateSet) {
cout << state << " ";
}
cout << "| ";
}
cout << endl;

cout << "DFA Alphabet: ";


for (char symbol : alphabet) {
cout << symbol << " ";
}
11
cout << endl;

return 0;
}

12
Reference:
https://www.geeksforgeeks.org/
https://www.tutorialspoint.com/index.htm

13

You might also like