To C Assignment 12023 I MT 074
To C Assignment 12023 I MT 074
ASSIGNMENT-1
BY-SHREYA PANDEY
2023IMT074
QUESTION 1
CODE:
#include <iostream>
#include <string>
class DFA_EvenCount {
public:
bool isAccepted(const string &s) {
int count0 = 0, count1 = 0;
for (char c : s) {
if (c == '0') count0++;
else if (c == '1') count1++;
}
return (count0 % 2 == 0 || count1 % 2 == 0);
}
};
class DFA_FirstLastDifferent {
public:
bool isAccepted(const string &s) {
if (s.length() < 2) return false;
return s.front() != s.back();
}
};
int main() {
string input;
cout << "Enter binary string: ";
cin >> input;
DFA_EvenCount dfa1;
DFA_FirstLastDifferent dfa2;
cout << "DFA for first and last symbols different: "
<< (dfa2.isAccepted(input) ? "Accepted" : "Rejected") << endl;
return 0;
}
OUTPUT:
QUESTION 2:
CODE:
#include <iostream>
#include <vector>
#include <map>
#include <set>
class DFA {
public:
int numStates;
set<int> finalStates;
map<pair<int, char>, int> transition;
void minimize() {
vector<set<int>> partitions = {finalStates, {}};
for (int i = 0; i < numStates; i++) {
if (!finalStates.count(i)) partitions[1].insert(i);
}
bool changed;
do {
changed = false;
vector<set<int>> newPartitions;
for (auto &p : partitions) {
map<pair<int, int>, set<int>> split;
for (int state : p) {
pair<int, int> sig = {
getPartitionIndex(state, partitions, '0'),
getPartitionIndex(state, partitions, '1')
};
split[sig].insert(state);
}
for (auto &[_, newSet] : split)
newPartitions.push_back(newSet);
if (split.size() > 1) changed = true;
}
partitions = newPartitions;
} while (changed);
printMinimizedDFA(partitions);
}
private:
int getPartitionIndex(int state, vector<set<int>> &partitions, char
input) {
int nextState = transition.count({state, input}) ?
transition[{state, input}] : -1;
for (int i = 0; i < partitions.size(); i++) {
if (partitions[i].count(nextState)) return i;
}
return -1;
}
return 0;
}
OUTPUT: