Sure! Below is a C++ implementation of the Subset Construction algorithm to convert an NFA to a DFA. This example assumes that the NFA is defined with states, input symbols, transition functions, a start state, and accept states.
#include <bits/stdc++.h>
using namespace std;
// Structure to represent NFA
struct NFA {
int numStates;
vector<char> symbols;
map<pair<int, char>, vector<int>> transitions;
int startState;
vector<int> acceptStates;
};
// Structure to represent DFA
struct DFA {
int numStates;
vector<char> symbols;
map<pair<int, char>, int> transitions;
int startState;
vector<int> acceptStates;
};
// Function to perform subset construction
DFA subsetConstruction(const NFA& nfa) {
DFA dfa;
dfa.symbols = nfa.symbols;
dfa.startState = 0;
// Mapping from set of NFA states to DFA state ID
map<set<int>, int> stateMap;
// Queue for BFS
queue<set<int>> q;
// Start state
set<int> startSet = {nfa.startState};
stateMap[startSet] = 0;
q.push(startSet);
dfa.numStates = 1;
while(!q.empty()) {
set<int> currentSet = q.front();
q.pop();
int currentDFAState = stateMap[currentSet];
for(char symbol : nfa.symbols) {
set<int> nextSet;
for(int state : currentSet) {
pair<int, char> key = {state, symbol};
if(nfa.transitions.find(key) != nfa.transitions.end()) {
for(auto &s : nfa.transitions.at(key)) {
nextSet.insert(s);
}
}
}
if(nextSet.empty()) continue;
// Check if this set is already mapped to a DFA state
if(stateMap.find(nextSet) == stateMap.end()) {
stateMap[nextSet] = dfa.numStates++;
q.push(nextSet);
}
// Add transition
dfa.transitions[{currentDFAState, symbol}] = stateMap[nextSet];
}
}
// Determine accept states
for(auto &entry : stateMap) {
set<int> nfaStates = entry.first;
int dfaState = entry.second;
for(int acc : nfa.acceptStates) {
if(nfaStates.find(acc) != nfaStates.end()) {
dfa.acceptStates.push_back(dfaState);
break;
}
}
}
return dfa;
}
// Function to print DFA
void printDFA(const DFA& dfa) {
cout << "DFA States: " << dfa.numStates << endl;
cout << "Input Symbols: ";
for(char c : dfa.symbols) cout << c << ' ';
cout << "\nTransitions:\n";
for(auto &t : dfa.transitions) {
cout << "State " << t.first.first << " --" << t.first.second << "--> State " << t.second << endl;
}
cout << "Start State: " << dfa.startState << endl;
cout << "Accept States: ";
for(int s : dfa.acceptStates) cout << s << ' ';
cout << endl;
}
int main(){
// Example NFA
NFA nfa;
nfa.numStates = 3;
nfa.symbols = {'a', 'b'};
nfa.startState = 0;
nfa.acceptStates = {2};
// Define transitions: (state, symbol) -> [states]
nfa.transitions[{0, 'a'}] = {0, 1};
nfa.transitions[{0, 'b'}] = {0};
nfa.transitions[{1, 'b'}] = {2};
// Convert NFA to DFA
DFA dfa = subsetConstruction(nfa);
// Print DFA
printDFA(dfa);
return 0;
}
Explanation:
-
NFA Representation:
numStates: Total number of states in the NFA.
symbols: Input symbols.
transitions: Transition function represented as a map where the key is a pair of (state, symbol) and the value is a vector of possible next states.
startState: The starting state of the NFA.
acceptStates: List of accept (final) states.
-
DFA Representation:
- Similar to NFA but the
transitions map points to a single state since DFA has deterministic transitions.
-
Subset Construction Algorithm:
- Starts with the NFA's start state.
- Uses BFS to explore all possible state subsets.
- Maps each unique subset of NFA states to a unique DFA state.
- Determines DFA accept states based on whether any NFA accept state is in the subset.
-
Example NFA:
- States: 0, 1, 2
- Input Symbols: 'a', 'b'
- Transitions:
- From state 0, on 'a' → states 0 and 1
- From state 0, on 'b' → state 0
- From state 1, on 'b' → state 2
- Start State: 0
- Accept State: 2
-
Output:
- The program prints the resulting DFA's states, input symbols, transitions, start state, and accept states.
Usage:
- Compile and run the program using a C++ compiler to see the DFA conversion result based on the provided NFA example.