-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactorización de conversión + se resolvieron los bugs de salida de …
…dfa a string
- Loading branch information
1 parent
4ac04cc
commit d4841eb
Showing
35 changed files
with
1,564 additions
and
1,077 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// Created by ignaciodias on 12/04/24. | ||
// | ||
#define LAMBDA -1 | ||
#include "ConvertionOfAutomatas.h" | ||
ConvertionOfAutomatas :: ConvertionOfAutomatas() : ndfa(), dfa(){ | ||
} | ||
void ConvertionOfAutomatas :: setNDFA(NotDeterministicFiniteAutomata ndfa) { | ||
this->ndfa = ndfa; | ||
} | ||
void ConvertionOfAutomatas :: setDFA(DeterministicFiniteAutomata dfa) { | ||
this->dfa = std::move(dfa); | ||
} | ||
DeterministicFiniteAutomata ConvertionOfAutomatas :: getDFA() { | ||
return dfa; | ||
} | ||
void ConvertionOfAutomatas :: convertFromNDFA() { | ||
set<int> q0AsSet; | ||
q0AsSet.insert(ndfa.getInitialState()); | ||
set<int> Q0 = getSymbolClosure(q0AsSet); | ||
dfa.setInitialState(Q0); | ||
set<set<int>> newK; | ||
newK.insert(Q0); | ||
|
||
dfa.setAlphabet(ndfa.getAlphabet()); | ||
|
||
dfa.setStates(newK); | ||
calculateNewK(); | ||
dfa.setFinalStates(calculateFinal(dfa.getStates())); | ||
} | ||
void ConvertionOfAutomatas::calculateNewK() { | ||
set<set<int>> unvisitedNodes = dfa.getStates(); | ||
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); | ||
} | ||
} | ||
set<int> ConvertionOfAutomatas::getSymbolClosure(const set<int>& Q) { | ||
set<int> result; | ||
set<int> visited_states; | ||
set<int> 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); | ||
for (int reachable_state : reachable_states) { | ||
result.insert(reachable_state); | ||
if (visited_states.find(reachable_state) == visited_states.end()) | ||
unvisited_states.insert(reachable_state); | ||
} | ||
} | ||
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); | ||
} | ||
return ret; | ||
} | ||
|
||
set<set<int>> ConvertionOfAutomatas :: calculateFinal(const set<set<int>>& k) { | ||
set<set<int>> newF; | ||
for(const auto& currentSet : k) { | ||
for(auto currentNumber : currentSet) { | ||
if(ndfa.getFinalSates().count(currentNumber) > 0) { | ||
newF.insert(currentSet); | ||
break; | ||
} | ||
} | ||
} | ||
return newF; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// Created by ignaciodias on 12/04/24. | ||
// | ||
|
||
#ifndef TPOB1_CONVERTIONOFAUTOMATAS_H | ||
#define TPOB1_CONVERTIONOFAUTOMATAS_H | ||
#include "../dfa/DeterministicFiniteAutomata.h" | ||
#include "../ndfa/NotDeterministicFiniteAutomata.h" | ||
|
||
class ConvertionOfAutomatas { | ||
private: | ||
DeterministicFiniteAutomata dfa; | ||
NotDeterministicFiniteAutomata ndfa; | ||
public: | ||
ConvertionOfAutomatas(); | ||
void setNDFA(NotDeterministicFiniteAutomata ndfa); | ||
void setDFA(DeterministicFiniteAutomata dfa); | ||
void convertFromNDFA(); | ||
DeterministicFiniteAutomata getDFA(); | ||
private: | ||
set<int> getSymbolClosure(const set<int>& Q); | ||
set<int> move(const set<int>& Q, int a); | ||
set<set<int>> calculateFinal(const set<set<int>>& k); | ||
void calculateNewK(); | ||
}; | ||
|
||
|
||
#endif //TPOB1_CONVERTIONOFAUTOMATAS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.