0% found this document useful (0 votes)
12 views5 pages

To C Assignment 12023 I MT 074

The document contains two assignments related to the Theory of Computation. The first assignment implements two Deterministic Finite Automata (DFAs) to check conditions on binary strings: one for even counts of '0's or '1's, and another for differing first and last symbols. The second assignment focuses on creating a DFA class that minimizes the DFA based on given states and transitions, including methods for partitioning and printing the minimized DFA.

Uploaded by

Shreya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

To C Assignment 12023 I MT 074

The document contains two assignments related to the Theory of Computation. The first assignment implements two Deterministic Finite Automata (DFAs) to check conditions on binary strings: one for even counts of '0's or '1's, and another for differing first and last symbols. The second assignment focuses on creating a DFA class that minimizes the DFA based on given states and transitions, including methods for partitioning and printing the minimized DFA.

Uploaded by

Shreya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

THEORY OF COMPUTATION

ASSIGNMENT-1

BY-SHREYA PANDEY
2023IMT074
QUESTION 1
CODE:
#include <iostream>
#include <string>

using namespace std;

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 even number of 0s or 1s: "


<< (dfa1.isAccepted(input) ? "Accepted" : "Rejected") << endl;

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>

using namespace std;

class DFA {
public:
int numStates;
set<int> finalStates;
map<pair<int, char>, int> transition;

DFA(int n, set<int> finals, vector<tuple<int, char, int>>


transitions) {
numStates = n;
finalStates = finals;
for (auto &[from, input, to] : transitions) {
transition[{from, input}] = to;
}
}

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;
}

void printMinimizedDFA(vector<set<int>> &partitions) {


cout << "Minimized DFA States: " << partitions.size() << endl;
for (int i = 0; i < partitions.size(); i++) {
cout << "State Group " << i << ": ";
for (int state : partitions[i]) cout << state << " ";
cout << endl;
}
}
};
int main() {
int n, t, f;
cout << "Enter number of states: ";
cin >> n;

cout << "Enter number of final states: ";


cin >> f;
set<int> finalStates;
cout << "Enter final states: ";
for (int i = 0, x; i < f; i++) {
cin >> x;
finalStates.insert(x);
}

cout << "Enter number of transitions: ";


cin >> t;
vector<tuple<int, char, int>> transitions;
cout << "Enter transitions (from, input, to):" << endl;
for (int i = 0, from, to; i < t; i++) {
char input;
cin >> from >> input >> to;
transitions.emplace_back(from, input, to);
}

DFA dfa(n, finalStates, transitions);


dfa.minimize();

return 0;
}

OUTPUT:

You might also like