// **Question 2a: Convert the regular expression r = (a(a|bb)*)* into NFA using Thompson’s construction.**
// **Thompson’s Construction Steps:**
// 1. For each basic symbol, create an NFA.
// 2. Use ε-transitions to combine NFAs for concatenation, alternation, and Kleene star.
// **NFA Representation:**
// States are numbered uniquely. Transitions are represented as (State, Input) -> NextState(s).
struct NFA_State {
int id;
std::vector<std::pair<char, int>> transitions; // Pair of (Input, NextState)
};
std::vector<NFA_State> constructNFA() {
std::vector<NFA_State> states;
int state_id = 0;
// Helper lambda to create a new state
auto new_state = [&]( ) -> int {
states.push_back(NFA_State{state_id, {}});
return state_id++;
};
// Construct NFA for 'a'
int a_start = new_state();
int a_end = new_state();
states[a_start].transitions.emplace_back('a', a_end);
// Construct NFA for 'a|bb'
int alt_start = new_state();
int alt_end = new_state();
// 'a' branch
states[alt_start].transitions.emplace_back('ε', a_start);
states[a_end].transitions.emplace_back('ε', alt_end);
// 'bb' branch
int b1 = new_state();
int b2 = new_state();
states[alt_start].transitions.emplace_back('ε', b1);
states[b1].transitions.emplace_back('b', b2);
states[b2].transitions.emplace_back('b', alt_end);
// Combine 'a' and 'bb' with Kleene star
int star_start = new_state();
int star_end = new_state();
states[star_start].transitions.emplace_back('ε', alt_start);
states[star_start].transitions.emplace_back('ε', star_end);
states[alt_end].transitions.emplace_back('ε', alt_start);
states[alt_end].transitions.emplace_back('ε', star_end);
// Construct NFA for 'a(a|bb)*'
int concat_start = new_state();
int concat_end = new_state();
states[concat_start].transitions.emplace_back('a', star_start);
states[star_end].transitions.emplace_back('ε', concat_end);
// Apply Kleene star to entire expression
int final_start = new_state();
int final_end = new_state();
states[final_start].transitions.emplace_back('ε', concat_start);
states[final_start].transitions.emplace_back('ε', final_end);
states[concat_end].transitions.emplace_back('ε', concat_start);
states[concat_end].transitions.emplace_back('ε', final_end);
return states;
}
// **Question 2b: Convert the obtained NFA into DFA using Subset Construction.**
// **Subset Construction Steps:**
// 1. Start with the ε-closure of the NFA start state.
// 2. For each subset of NFA states, determine transitions for each input symbol.
// 3. Repeat until all DFA states are processed.
#include <vector>
#include <map>
#include <set>
#include <queue>
struct DFA_State {
int id;
std::map<char, int> transitions; // Input -> NextState
bool is_accept;
};
std::vector<DFA_State> convertNFAtoDFA(const std::vector<NFA_State>& nfa, int nfa_start, int nfa_end) {
auto epsilon_closure = [&](const std::set<int>& states) -> std::set<int> {
std::set<int> closure = states;
std::queue<int> q;
for(auto s : states) q.push(s);
while(!q.empty()) {
int state = q.front(); q.pop();
for(auto &[input, next] : nfa[state].transitions) {
if(input == 'ε' && closure.find(next) == closure.end()) {
closure.insert(next);
q.push(next);
}
}
}
return closure;
};
auto move = [&](const std::set<int>& states, char input) -> std::set<int> {
std::set<int> result;
for(auto s : states) {
for(auto &[i, next] : nfa[s].transitions) {
if(i == input) result.insert(next);
}
}
return result;
};
std::vector<DFA_State> dfa;
std::map<std::set<int>, int> state_mapping;
std::queue<std::set<int>> q;
std::set<int> start_set = epsilon_closure({nfa_start});
q.push(start_set);
state_mapping[start_set] = 0;
dfa.push_back(DFA_State{0, {}, false});
if(start_set.find(nfa_end) != start_set.end()) dfa[0].is_accept = true;
while(!q.empty()) {
std::set<int> current = q.front(); q.pop();
int current_id = state_mapping[current];
// Assume input symbols are 'a' and 'b'
for(char input : {'a', 'b'}) {
std::set<int> moved = move(current, input);
std::set<int> closure = epsilon_closure(moved);
if(closure.empty()) continue;
if(state_mapping.find(closure) == state_mapping.end()) {
int new_id = dfa.size();
state_mapping[closure] = new_id;
bool is_accept = (closure.find(nfa_end) != closure.end());
dfa.push_back(DFA_State{new_id, {}, is_accept});
q.push(closure);
}
dfa[current_id].transitions[input] = state_mapping[closure];
}
}
return dfa;
}
// **Question 2c: Minimize the obtained DFA from part 2b.**
// **DFA Minimization Steps (Hopcroft’s Algorithm):**
// 1. Partition states into accept and non-accept.
// 2. Refine partitions based on transitions.
// 3. Merge equivalent states.
#include <algorithm>
std::vector<DFA_State> minimizeDFA(const std::vector<DFA_State>& dfa) {
int n = dfa.size();
// Initial partition: accept and non-accept
std::vector<std::set<int>> partitions;
std::set<int> accept;
std::set<int> non_accept;
for(int i = 0; i < n; ++i){
if(dfa[i].is_accept) accept.insert(i);
else non_accept.insert(i);
}
if(!accept.empty()) partitions.push_back(accept);
if(!non_accept.empty()) partitions.push_back(non_accept);
bool changed = true;
while(changed){
changed = false;
std::vector<std::set<int>> new_partitions;
for(auto &group : partitions){
// Split group based on transitions
std::map<std::string, std::set<int>> splitter;
for(auto state : group){
std::string key;
for(auto &[input, next] : dfa[state].transitions){
// Find the partition index of the next state
int part = -1;
for(int p = 0; p < partitions.size(); ++p){
if(partitions[p].find(next) != partitions[p].end()){
part = p;
break;
}
}
key += std::to_string(part) + ",";
}
splitter[key].insert(state);
}
if(splitter.size() > 1){
changed = true;
for(auto &[k, s] : splitter){
new_partitions.push_back(s);
}
}
else{
new_partitions.push_back(group);
}
}
partitions = new_partitions;
}
// Create new DFA states
std::map<int, int> state_mapping;
std::vector<DFA_State> min_dfa;
for(int i = 0; i < partitions.size(); ++i){
bool is_accept = false;
for(auto state : partitions[i]){
if(dfa[state].is_accept){
is_accept = true;
break;
}
}
min_dfa.push_back(DFA_State{i, {}, is_accept});
for(auto state : partitions[i]){
state_mapping[state] = i;
}
}
// Define transitions
for(int i = 0; i < partitions.size(); ++i){
if(partitions[i].empty()) continue;
int repr = *partitions[i].begin();
for(auto &[input, next] : dfa[repr].transitions){
min_dfa[i].transitions[input] = state_mapping[next];
}
}
return min_dfa;
}