Professional Documents
Culture Documents
Nfla 4
Nfla 4
#include<vector>
#include<fstream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string>
#include "MetaNodeObject.cpp"
//Reads in raw data from a given text file to set into a graph object
Graph ReadIn(std::string fileName) {
Graph toReturn;
std::ifstream G(fileName);
std::string Line;
if (G.is_open()) {
std::getline(G, Line); //Buffer, gets rid of Byte Order Mark
//Gets rid of any weird unicode characters or white spaces from the file
read-in
for (int i = 0; i<toReturn.graphData.size(); i++)
for (int j = 0; j<toReturn.graphData[i].size(); j++)
if (!isdigit(toReturn.graphData[i][j].back()) && !
isalpha(toReturn.graphData[i][j].back()))
toReturn.graphData[i][j].pop_back();
return toReturn;
}
Right.back().push_back(MNode.PNode[l]);
std::sort(Right.back().begin(),
Right.back().end());
}
}
} if (Left[CIndex] == Left.back()) { toLoop = false; }
for (int k = 0; k<Right.size(); k++) {
if (!(std::find(Left.begin(), Left.end(), Right[k]) !=
Left.end()))
if (!Right[k].empty()) {
Left.push_back(Right[k]);
toLoop = true;
}
} CIndex++;
}
//Uses the left and right 2d vectors to generate the transition data of the
DFA graph object
for (int n = 0; n<Left.size(); n++) {
for (int o = 0; o<DFA.Alphabet.size(); o++) {
if (!(Right[(n*DFA.Alphabet.size()) + o].empty())) {
DFA.graphData.push_back(std::vector<std::string>());
//Creates transition
std::string toPushLeft = "";
for (int p = 0; p<Left[n].size(); p++)
toPushLeft.append(std::to_string(Left[n][p]));
DFA.graphData.back().push_back(toPushLeft);
//Pushes original node
for (int q = 0; q<DFA.Alphabet.size(); q++)
if (o % DFA.Alphabet.size() == q)
DFA.graphData.back().push_back(DFA.Alphabet[q]); //Pushes alphabet value for
transition
std::string toPushRight = "";
for (int r = 0; r<Right[(n*DFA.Alphabet.size()) +
o].size(); r++)
toPushRight.append(std::to_string(Right[(n*DFA.Alphabet.size()) + o][r]));
//Pushes node being transitioned to
DFA.graphData.back().push_back(toPushRight);
}
}
}
return DFA;
}
splicingIndexes.push_back(std::make_pair(Nodes.size(), -1));
for (int k = tempIndexes[i].first; k <=
tempIndexes[i].second; k++)
if (tempTable[k][1] == tempTable[j][1] &&
tempTable[k][2] == tempTable[j][2])
newHopTable.push_back(hopTable[k]);
splicingIndexes.back().second = newHopTable.size() -
1;
}
}
}
if (Decider = splicingIndexes.size()) toLoop = false;
hopTable = newHopTable;
}
Nodes.clear();
for (int j = 0; j < hopTable.size(); j++) Nodes.push_back(hopTable[j][0]);
int main() {
Graph NFA = ReadIn("Data/Graph1.txt");
if (!NFA.checkValidity())
printf("Validity Error. Closing...");
else {
NFA.generateMatrix();
Graph DFA = NFA_to_DFA(NFA);
if (!DFA.checkValidity())
printf("Validity Error. Closing...");
else {
Graph Min_DFA = HopCroft(DFA);
std::cout << "DFA:\n"; DFA.showGraphData();
std::cout << "\n\nMinimized DFA:\n"; Min_DFA.showGraphData();
}
}
std::cin.get();
return 0;
}