Professional Documents
Culture Documents
Meesha Munir Lab 04
Meesha Munir Lab 04
EXPERIMENT#01
Submitted To:
Iqra Muneer
Submitted By:
Meesha Munir (2020-CS-517)
DFA to NFA
Introduction:
A DFA (deterministic finite automaton) is a finite state machine that processes input symbols, one at a time, moving through
a series of states according to a predefined set of rules. A DFA (Deterministic Finite Automaton) and an NFA
(Nondeterministic Finite Automaton) are both theoretical models used in automata theory, a branch of theoretical computer
science. They are used to recognize and classify languages in the realm of formal languages and automata .
A DFA is formally defined as a quintuple (Q, Σ, δ, q₀, F), where:
Example:
Consider a simple DFA that recognizes strings over the alphabet {0, 1} where the number of 0's is even. The DFA is defined
as follows:
States: {q0, q1}
Alphabet: {0, 1}
Transition Table:
| Current State | Input Symbol | Next State |
|---------------|--------------|------------|
| q0 |0 | q1 |
| q0 |1 | q0 |
| q1 |0 | q0 |
| q1 |1 | q1 |
Initial State: q0
Accepting State: q0
Code:
#include <iostream>
#include <vector>
#include <set>
using namespace std;
class DFA {
private:
int numStates;
int numSymbols;
vector<vector<int>> transitions;
set<int> acceptingStates;
public:
DFA(int states, int symbols, const vector<vector<int>>& trans, const set<int>& accepts)
: numStates(states), numSymbols(symbols), transitions(trans), acceptingStates(accepts) {}
void convertToNFA() {
// Create new start state and add transition
numStates++;
transitions.push_back(vector<int>(numSymbols, numStates - 1));
int main() {
// Example DFA
int states = 2;
int symbols = 2;
vector<vector<int>> transitions = {{1, 0}, {0, 1}};
set<int> acceptingStates = {0};
DFA dfa(states, symbols, transitions, acceptingStates);
dfa.convertToNFA();
return 0;
}
Output: