-
Notifications
You must be signed in to change notification settings - Fork 3
/
min_dfa.cpp
334 lines (245 loc) · 11.1 KB
/
min_dfa.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <iostream>
#include <fstream>
#include <string>
#include "dfa_state.h"
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <vector>
#include<algorithm>
#include "partition.h"
#include<sstream>
vector<Partition *> partitions;
vector<string> symbolList;
std::vector<string> transitionsEnumerator(DFAState *root) {
unordered_map<string, DFAState *> neighbours = root->get_neighbours();
unordered_map<string, DFAState *>::iterator neighboursIterator = neighbours.begin();
//enumerate all possible transitions
std::vector<string> transitions;
while (neighboursIterator != neighbours.end()) {
transitions.push_back(neighboursIterator->first);
neighboursIterator++;
}
return transitions;
}
string getPartition(DFAState *state) {
vector<Partition *>::iterator partitionsIterator;
for (partitionsIterator = partitions.begin(); partitionsIterator < partitions.end(); partitionsIterator++) {
vector<DFAState *> members = (*partitionsIterator)->getMembers();
vector<DFAState *>::iterator membersIterator = members.begin();
for (membersIterator = members.begin(); membersIterator < members.end(); membersIterator++) {
if ((*membersIterator) == state) {
stringstream ss;
ss << (*partitionsIterator)->getId();
string s;
ss >> s;
return s;
}
}
}
return "";
}
template<typename T>
void remove_duplicates(std::vector<T> &vec) {
std::sort(vec.begin(), vec.end());
vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
}
void setMark(unordered_set<DFAState *> listofstates) {
vector<Partition *>::iterator partitionsIterator;
for (partitionsIterator = partitions.begin(); partitionsIterator < partitions.end(); partitionsIterator++) {
vector<DFAState *> members = (*partitionsIterator)->getMembers();
vector<DFAState *>::iterator membersIterator = members.begin();
for (membersIterator = members.begin(); membersIterator < members.end(); membersIterator++) {
unordered_map<string, DFAState *> neigbours = (*membersIterator)->get_neighbours();
unordered_map<string, DFAState *>::iterator neighboursItr = neigbours.begin();
string symbol = "";
while (neighboursItr != neigbours.end()) {
DFAState *neighbour = neighboursItr->second;
symbol += getPartition(neighbour) + ",";
neighboursItr++;
}
symbol = symbol + (*partitionsIterator)->getPartitionsymbol();
(*membersIterator)->setCombiningsymbol(symbol);
symbolList.push_back(symbol);
}
}
remove_duplicates(symbolList);
}
DFAState ***constructTransitiontable(DFAState *root, unordered_set<DFAState *> listofstates, const string &out_file_relative_path) {
unordered_map<string, DFAState *> neighbours = root->get_neighbours();
unordered_map<string, DFAState *>::iterator neighboursIterator = neighbours.begin();
std::vector<string> transitions = transitionsEnumerator(root);
/*
//enumerate all possible transitions
std::vector<string> transitions;
while (neighboursIterator != neighbours.end())
{
transitions.push_back(neighboursIterator->first);
neighboursIterator++;
}*/
//Construct the transition table
DFAState ***matrix = new DFAState **[listofstates.size()]; // each element is a pointer to an array.
unordered_set<DFAState *>::iterator statesIterator = listofstates.begin();
for (size_t i = 0; i < listofstates.size(); ++i)
matrix[i] = new DFAState *[transitions.size()]; // build rows
//cout << listofstates.size()<<endl;
std::ofstream transition_table_file;
transition_table_file.open(out_file_relative_path);
transition_table_file << " ";
for (auto &&input: transitions) {
transition_table_file << input << " ";
}
transition_table_file << "\n";
for (size_t stateIndex = 0; stateIndex < listofstates.size(); stateIndex++) {
transition_table_file << (*statesIterator)->get_id() << " ";
for (size_t transitionIndex = 0; transitionIndex < transitions.size(); transitionIndex++) {
unordered_map<string, DFAState *> currentNeighbour = (*statesIterator)->get_neighbours();
matrix[stateIndex][transitionIndex] = currentNeighbour[transitions[transitionIndex]];
transition_table_file << currentNeighbour[transitions[transitionIndex]]->get_id() << " ";
}
transition_table_file << "\n";
statesIterator++;
}
transition_table_file.close();
return matrix;
//dont forget to remove matrix if unused
/*for(size_t i = 0; i < N; i++)
delete matrix[i];
delete matrix;*/
}
/*
Myhill Nerode Algorithm failed :)
bool ** createFillingtable(DFAState* root, unordered_set<DFAState *> listofstates)
{
//building the base of filling table with accept state xoring mechanism values
bool ** matrix = new bool*[listofstates.size()]; // each element is a pointer to an array.
unordered_set<DFAState *>::iterator rowsIterator = listofstates.begin();
unordered_set<DFAState *>::iterator columnsIterator = listofstates.begin();
for (size_t i = 0; i < listofstates.size(); i++)
matrix[i] = new bool[listofstates.size()]; // build rows
for (size_t rawIndex = 0; rawIndex < listofstates.size(); rawIndex++)
{
//cout << (*rowsIterator)->getId() << ' ';
for (size_t columnIndex = 0; columnIndex < listofstates.size(); columnIndex++)
{
matrix[rawIndex][columnIndex] = (*rowsIterator)->isAccepting_state()^ (*columnsIterator)->isAccepting_state();
cout << matrix[rawIndex][columnIndex];
columnsIterator++;
}
cout << endl;
columnsIterator = listofstates.begin();
rowsIterator++;
}
return matrix;
}
bool ** fillTable(DFAState* root ,bool ** fillingTable, unordered_set<DFAState *> listofstates)
{
unordered_set<DFAState *>::iterator rowsIterator = listofstates.begin();
unordered_set<DFAState *>::iterator columnsIterator = listofstates.begin();
std::vector<string> transitions = transitionsEnumerator(root);
for (size_t i = 0; i < listofstates.size(); i++)
{
for (size_t j = 0; j < i; j++)
{
if (fillingTable[i][j] == false) //skip true cases
{
//fillingTable[i][j] = true;
//cout << fillingTable[i][j];
}
columnsIterator++;
}
cout << endl;
columnsIterator = listofstates.begin();
rowsIterator++;
}
return fillingTable;
}
bool checkNeighbours(DFAState * rowState, DFAState * columnState , bool ** fillingTable )
{
std::vector<string> transitions = transitionsEnumerator(rowState);
for (size_t transitionIndex = 0; transitionIndex < transitions.size(); transitionIndex++)
{
unordered_map<string, DFAState *> rowNeigbours = rowState->getNeighbours();
unordered_map<string, DFAState *> columnNeigbours = columnState->getNeighbours();
}
}
*/
void putProperPartition(DFAState *State) {
vector<Partition *>::iterator partitionsIterator;
for (partitionsIterator = partitions.begin(); partitionsIterator < partitions.end(); partitionsIterator++) {
if (State->getCombiningsymbol().compare((*partitionsIterator)->getPartitionsymbol()) == 0)
(*partitionsIterator)->add(State);
}
}
unordered_set<DFAState *> partitioning(DFAState ***transitionTable, unordered_set<DFAState *> listofstates) {
Partition *accept = new Partition("x");
Partition *normal = new Partition("y");
partitions.push_back(normal);
partitions.push_back(accept);
//first step is to classify states to 2 states_partitions
for (unordered_set<DFAState *>::iterator itr = listofstates.begin(); itr != listofstates.end(); itr++) {
if ((*itr)->IsAcceptingState())
accept->add((*itr));
else
normal->add((*itr));
}
int oldParitionsize = 0;
while (oldParitionsize != partitions.size()) {
oldParitionsize = partitions.size();
//mark all states with combining symbols
setMark(listofstates);
//create new states_partitions equal to # of symbols and link them to states_partitions list
for (size_t i = 0; i < symbolList.size(); i++) {
partitions.push_back(new Partition((*(symbolList.begin() + i))));
}
//iterate through old states_partitions and put every state in the proper partition
vector<Partition *>::iterator partitionsIterator = partitions.begin();
for (size_t i = 0; i < partitions.size() - symbolList.size(); i++) {
vector<DFAState *> members = (*partitionsIterator)->getMembers();
vector<DFAState *>::iterator membersIterator = members.begin();
for (size_t j = 0; j < (*partitionsIterator)->getMembers().size(); j++) {
// DFAState * currentState = ;
putProperPartition((*membersIterator));
membersIterator++;
}
partitionsIterator++;
}
//remove old paritions
partitionsIterator = partitions.begin();
int partSize = partitions.size();
for (size_t i = 0; i < partSize - symbolList.size(); i++)
partitionsIterator = partitions.erase(partitionsIterator);
symbolList.clear();
}
vector<Partition *>::iterator partitionsIterator;
unordered_set<DFAState *> newDFA;
//set euiv states property to all states
for (partitionsIterator = partitions.begin(); partitionsIterator < partitions.end(); partitionsIterator++) {
{
vector<DFAState *> members = (*partitionsIterator)->getMembers();
vector<DFAState *>::iterator membersIterator = members.begin();
DFAState *equivState = (*membersIterator);
newDFA.insert(equivState);
for (membersIterator = members.begin(); membersIterator < members.end(); membersIterator++) {
(*membersIterator)->setEquivstate(equivState);
}
}
}
//setting neighbours for the minimized dfa to their equivilant
for (size_t i = 0; i < newDFA.size(); i++) {
unordered_set<DFAState *>::iterator statesIterator;
//iterate through neighbours
for (statesIterator = newDFA.begin(); statesIterator != newDFA.end(); statesIterator++) {
unordered_map<string, DFAState *> neighbours = (*statesIterator)->get_neighbours();
unordered_map<string, DFAState *>::iterator neighboursIterator = neighbours.begin();
for (neighboursIterator = neighbours.begin();
neighboursIterator != neighbours.end(); neighboursIterator++) {
(*statesIterator)->UpdateNeighbours((neighboursIterator)->first,
(neighboursIterator)->second->getEquivstate());
}
}
}
return newDFA;
}
//it returns the symbol generated from id transitions of neighbours
//it returns the partition id of the given state