Skip to content

Commit

Permalink
cambio en convertion para calcular los nuevos estados
Browse files Browse the repository at this point in the history
  • Loading branch information
larchadrg committed Apr 13, 2024
1 parent e6b3d35 commit c747067
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 68 deletions.
23 changes: 11 additions & 12 deletions TP/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,21 @@ int main() {

NotDeterministicFiniteAutomata ndfa;
ndfa.setInitialState(10);
ndfa.setStates({10,11,12,13,14,15});
ndfa.setAlphabet({-1,1,2,3});
ndfa.setAlphabet({1,2}); //a, b
ndfa.setStates({10,11,12,13});
ndfa.addPath(10,1,11);
ndfa.addPath(11,3,12);
ndfa.addPath(12,-1,15);
ndfa.addPath(15,-1,10);
ndfa.addPath(10,-1,13);
ndfa.addPath(13,2,14);
ndfa.addPath(13,2,15);
ndfa.addPath(14,2,14);
ndfa.addPath(14,2,15);
ndfa.setFinalState({4,5});
// cout << Parser::ndfaToString(ndfa);
ndfa.addPath(11,2,11);
ndfa.addPath(11,2,12);
ndfa.addPath(11,1,13);
ndfa.addPath(13,1,13);
ndfa.addPath(13,2,13);
ndfa.addFinalState(12);
cout << "Automata Original: " << endl;
cout << Parser::ndfaToString(ndfa);
ConvertionOfAutomatas convertor;
convertor.setNDFA(ndfa);
convertor.convertFromNDFA();
cout << "Nuevo automata: " << endl;
cout << Parser::dfaToString(convertor.getDFA());
return 0;
}
128 changes: 78 additions & 50 deletions TP/automata/convertion/ConvertionOfAutomatas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
#define LAMBDA -1
#include "ConvertionOfAutomatas.h"

ConvertionOfAutomatas :: ConvertionOfAutomatas() : ndfa(), dfa(){
}
void ConvertionOfAutomatas :: setNDFA(NotDeterministicFiniteAutomata ndfa) {
Expand All @@ -15,57 +16,49 @@ DeterministicFiniteAutomata ConvertionOfAutomatas :: getDFA() {
return dfa;
}
void ConvertionOfAutomatas :: convertFromNDFA() {
set<int> q0AsSet;
q0AsSet.insert(ndfa.getInitialState());
set<int> Q0 = getSymbolClosure(q0AsSet);
dfa.setInitialState(Q0);
using dfaState = set<int>;
using ndfaState = int;

dfaState newInitialState = getLambdaClosure({ndfa.getInitialState()});
dfa.setInitialState(newInitialState);
dfa.setAlphabet(ndfa.getAlphabet());

dfa.setStates({Q0});
calculateNewK();
dfa.setFinalStates(calculateFinal(dfa.getStates()));
}
void ConvertionOfAutomatas::calculateNewK() {
set<set<int>> unvisitedNodes;
unvisitedNodes.insert(dfa.getInitialState());
while(!unvisitedNodes.empty()) {
set<int> currentNode = *unvisitedNodes.begin();
unvisitedNodes.erase(currentNode);
for(auto letter : ndfa.getAlphabet()) {
set<int> destination = getSymbolClosure(move(currentNode, letter));
if(unvisitedNodes.count(destination) <= 0) {
unvisitedNodes.insert(destination);
set<dfaState> unmarkedStates;
set<dfaState> dfaStates;

unmarkedStates.insert(newInitialState);
dfaStates.insert(newInitialState);
while(!unmarkedStates.empty()){
for(auto it = unmarkedStates.begin(); it != unmarkedStates.end();) {
dfaState S = *it; //evita segmentation fault
it = unmarkedStates.erase(it);
for(int symbol: ndfa.getAlphabet()){
dfaState M = getLambdaClosure(move(S, symbol));
//if M not in T
if(dfaStates.find(M) == dfaStates.end()){
unmarkedStates.insert(M);
dfaStates.insert(M);
}
dfa.addPath(S,symbol,M);
}
dfa.insertSate(currentNode);
dfa.addPath(currentNode, letter, destination);
}
}
// while(!unvisitedNodes.empty()) {
// auto currentNode = *unvisitedNodes.begin();
// for (auto currentNumber : ndfa.getAlphabet()) {
// set<int> currentSet = move(currentNode, currentNumber);
// currentSet = getSymbolClosure(currentSet);
// if (dfa.getStates().count(currentSet) <= 0) {
// dfa.insertSate(currentSet);
// unvisitedNodes.insert(currentSet);
// dfa.addPath(currentNode, currentNumber, currentSet);
// }
// }
// unvisitedNodes.erase(currentNode);
// }
dfa.setStates(dfaStates);
dfa.setFinalStates(calculateFinal(dfa.getStates()));
}
set<int> ConvertionOfAutomatas::getSymbolClosure(const set<int>& Q) {
set<int> result;
set<int> visited_states;
set<int> unvisited_states = Q;

set<int> ConvertionOfAutomatas::getLambdaClosure(const set<int>& Q) {
using state = int;
set<state> result;
set<state> visited_states;
set<state> unvisited_states = Q;

while (!unvisited_states.empty()) {
int curr_state = *unvisited_states.begin();
unvisited_states.erase(curr_state);
visited_states.insert(curr_state);
set<int> reachable_states = ndfa.calculateDelta({curr_state, LAMBDA});
if(reachable_states.empty())
result.insert(curr_state);
set<int> reachable_states = getLambdaReachableStates(curr_state);
result.insert(curr_state);
for (int reachable_state : reachable_states) {
result.insert(reachable_state);
if (visited_states.find(reachable_state) == visited_states.end())
Expand All @@ -74,17 +67,41 @@ set<int> ConvertionOfAutomatas::getSymbolClosure(const set<int>& Q) {
}
return result;
}
set<int> ConvertionOfAutomatas :: move(const set<int>& Q, int a) {
set<int> ret;
for(auto elem : Q) {
pair<int,int> actualPair;
actualPair.first = elem;
actualPair.second = a;
set<int> actualSet = ndfa.calculateDelta(actualPair);
if(!actualSet.empty())
CollectionsOperators::insertAll(ret, actualSet);

set<int> ConvertionOfAutomatas::getLambdaReachableStates(int state) {
set<int> result;
queue<int> to_visit;
set<int> visited;

to_visit.push(state);
visited.insert(state);

while (!to_visit.empty()) {
int current_state = to_visit.front();
to_visit.pop();
set<int> lambda_reachable_states = ndfa.calculateDelta({current_state, LAMBDA});
for (int lambda_reachable_state : lambda_reachable_states) {
if (visited.find(lambda_reachable_state) == visited.end()) {
result.insert(lambda_reachable_state);
to_visit.push(lambda_reachable_state);
visited.insert(lambda_reachable_state);
}
}
}
return ret;

return result;
}

set<int> ConvertionOfAutomatas :: move(set<int> states, int symbol) {
using state = int;
set<state> result;
for(state s: states) {
set<state> possibleMovements = ndfa.calculateDelta(make_pair(s,symbol));
for(state s: possibleMovements){
result.insert(s);
}
}
return result;
}

set<set<int>> ConvertionOfAutomatas :: calculateFinal(const set<set<int>>& k) {
Expand All @@ -99,3 +116,14 @@ set<set<int>> ConvertionOfAutomatas :: calculateFinal(const set<set<int>>& k) {
}
return newF;
}

void showStates(set<set<int>> states){
using dfaState = set<int>;
using ndfaState = int;
using std::cout;
for(dfaState state: states){
for(int substate: state){
cout << substate << ' ';
}
}
}
8 changes: 4 additions & 4 deletions TP/automata/convertion/ConvertionOfAutomatas.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class ConvertionOfAutomatas {
void setDFA(DeterministicFiniteAutomata dfa);
void convertFromNDFA();
DeterministicFiniteAutomata getDFA();
private:
set<int> calculateDelta(const set<int>& state, int symbol);
set<int> getSymbolClosure(const set<int>& Q);
set<int> move(const set<int>& Q, int a);
set<int> getLambdaClosure(const set<int>& Q);
set<int> getLambdaReachableStates(int state);
set<int> move(set<int> states, int symbol);
set<set<int>> calculateFinal(const set<set<int>>& k);
void calculateNewK();
void showStates(set<set<int>> states);
};


Expand Down
2 changes: 1 addition & 1 deletion TP/testing/fileToNdfa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../parser/Parser.h"
using namespace std;

const string filename = "TP/testing/input.txt";
const string filename = "TP/testing/output.txt";

int main() {
ifstream file(filename); // Abrir el archivo
Expand Down
2 changes: 1 addition & 1 deletion TP/testing/ndfaToFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ int main() {
string filename = "TP/testing/output.txt";

NotDeterministicFiniteAutomata ndfa;
ndfa.setInitialState(0);
ndfa.setInitialState(10);
ndfa.setE({1,2}); //a, b
ndfa.setK({10,11,12,13});
ndfa.addPath(10,1,11);
Expand Down
28 changes: 28 additions & 0 deletions TP/testing/testClausuraLambda.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "../automata/dfa/DeterministicFiniteAutomata.h"
#include "../automata/ndfa/NotDeterministicFiniteAutomata.h"
#include "../automata/convertion/ConvertionOfAutomatas.h"

int main(){
using std::cout;
NotDeterministicFiniteAutomata ndfa;

ConvertionOfAutomatas conversor;
ndfa.setInitialState(10);
ndfa.setAlphabet({1,2}); //a, b
ndfa.setStates({10,11,12,13});
ndfa.addPath(10,1,11);
ndfa.addPath(10,-1,11);
ndfa.addPath(11,-1,12);
ndfa.addPath(11,2,11);
ndfa.addPath(11,2,12);
ndfa.addPath(11,1,13);
ndfa.addPath(13,1,13);
ndfa.addPath(13,2,13);
ndfa.addFinalState(12);
conversor.setNDFA(ndfa);

set<int> clausura = conversor.getLambdaClosure({10});
for (int state : clausura){
std::cout << state << ' ';
}
}
28 changes: 28 additions & 0 deletions TP/testing/testMover.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "../automata/dfa/DeterministicFiniteAutomata.h"
#include "../automata/ndfa/NotDeterministicFiniteAutomata.h"
#include "../automata/convertion/ConvertionOfAutomatas.h"

int main(){
using std::cout;
NotDeterministicFiniteAutomata ndfa;

ConvertionOfAutomatas conversor;
ndfa.setInitialState(10);
ndfa.setAlphabet({1,2}); //a, b
ndfa.setStates({10,11,12,13});
ndfa.addPath(10,1,11);
ndfa.addPath(10,-1,11);
ndfa.addPath(11,-1,12);
ndfa.addPath(11,2,11);
ndfa.addPath(11,2,12);
ndfa.addPath(11,1,13);
ndfa.addPath(13,1,13);
ndfa.addPath(13,2,13);
ndfa.addFinalState(12);
conversor.setNDFA(ndfa);

set<int> clausura = conversor.move({11},2);
for (int state : clausura){
std::cout << state << ' ';
}
}
33 changes: 33 additions & 0 deletions TP/testing/testParserFunctions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <bits/stdc++.h>
#include "../automata/dfa/DeterministicFiniteAutomata.h"
#include "../automata/ndfa/NotDeterministicFiniteAutomata.h"
#include "../parser/Parser.h"
using namespace std;

const string filename = "TP/testing/output.txt";

int main() {
ifstream file(filename); // Abrir el archivo
Parser parser = *new Parser();

if (!file.is_open()) { // Verificar si el archivo se abrió correctamente
cerr << "No se pudo abrir el archivo." << endl;
return -1;
}

string line;

if (!getline(file, line)) { // Leer la primera línea
cerr << "No se pudo leer la primera línea." << endl;
return -1;
}
if(!Parser::validateTransitionLine(line)){
cerr << "Formato invalido del archivo." << endl;
return -1;
}

file.close();

//NotDeterministicFiniteAutomata nda = parser.getNDA();
//nda.show(); //TODO: IMPLEMENTAR
}

0 comments on commit c747067

Please sign in to comment.