Skip to content

Commit

Permalink
ndfaToDfa bugs solved
Browse files Browse the repository at this point in the history
  • Loading branch information
ignacioDias committed Apr 10, 2024
1 parent b5a0ecb commit 58b72cc
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 26 deletions.
4 changes: 2 additions & 2 deletions TP/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int main() {

NotDeterministicFiniteAutomata ndfa;
ndfa.setInitialState(10);
ndfa.setE({1,2}); //a, b
ndfa.setE({-1,1,2}); //a, b
ndfa.setK({10,11,12,13});
ndfa.addPath(10,1,11);
ndfa.addPath(11,2,11);
Expand All @@ -44,7 +44,7 @@ int main() {
ndfa.addPath(13,1,13);
ndfa.addPath(13,2,13);
ndfa.addFinalState(12);
//cout << Parser::ndfaToString(ndfa);
// cout << Parser::ndfaToString(ndfa);
DeterministicFiniteAutomata dfa = ndfa.nfaToDfa();
cout << Parser::dfaToString(dfa);
return 0;
Expand Down
9 changes: 3 additions & 6 deletions TP/automata/dfa/DeterministicFiniteAutomata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,9 @@ bool DeterministicFiniteAutomata :: isFinalNode(const set<int>& node) {

set<int> DeterministicFiniteAutomata::calculateWaysToGo(const set<int>& set1, const set<int>& set2) {
set<int> ret;
for(int letter : this->E) {
pair<set<int>,int> pair;
pair.first = set1;
pair.second = letter;
set<int> delta = calculateDelta(pair);
if(!delta.empty() && CollectionsOperators::contained(set2, delta))
for (int letter : this->E) {
set<int> delta = calculateDelta(make_pair(set1, letter));
if (!delta.empty() && CollectionsOperators::contained(set2, delta))
ret.insert(letter);
}
return ret;
Expand Down
19 changes: 12 additions & 7 deletions TP/automata/ndfa/NotDeterministicFiniteAutomata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,18 @@ DeterministicFiniteAutomata NotDeterministicFiniteAutomata :: nfaToDfa() {
convertedAutomata.setF(calculateFinal(newK));
return convertedAutomata;
}
void NotDeterministicFiniteAutomata :: calculateNewK(set<set<int>> newK, DeterministicFiniteAutomata dfa) {
void NotDeterministicFiniteAutomata :: calculateNewK(set<set<int>>& newK, DeterministicFiniteAutomata dfa) {
set<set<int>> unvisitedNodes = newK;
for(const auto& currentNode : unvisitedNodes) {
while(!unvisitedNodes.empty()) {
auto currentNode = *unvisitedNodes.begin();
for (auto currentNumber: getE()) {
set<int> currentSet = move(currentNode, currentNumber);
currentSet = getSymbolClosure(currentSet);
newK.insert(currentSet);
unvisitedNodes.insert((currentSet));
dfa.addPath(currentNode, currentNumber,currentSet);
if (newK.count(currentSet) <= 0) {
newK.insert(currentSet);
unvisitedNodes.insert(currentSet);
dfa.addPath(currentNode, currentNumber, currentSet);
}
}
unvisitedNodes.erase(currentNode);
}
Expand All @@ -108,6 +111,8 @@ set<int> NotDeterministicFiniteAutomata ::getSymbolClosure(const set<int>& Q) {
unvisited_states.erase(curr_state);
visited_states.insert(curr_state);
set<int> reachable_states = 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())
Expand All @@ -128,7 +133,7 @@ set<int> NotDeterministicFiniteAutomata :: move(const set<int>& Q, int a) {
}
return ret;
}
set<set<int>> NotDeterministicFiniteAutomata :: calculateFinal(set<set<int>> k) {
set<set<int>> NotDeterministicFiniteAutomata :: calculateFinal(const set<set<int>>& k) {
set<set<int>> newF;
for(const auto& currentSet : k) {
for(auto currentNumber : currentSet) {
Expand All @@ -139,4 +144,4 @@ set<set<int>> NotDeterministicFiniteAutomata :: calculateFinal(set<set<int>> k)
}
}
return newF;
}
}
4 changes: 2 additions & 2 deletions TP/automata/ndfa/NotDeterministicFiniteAutomata.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class NotDeterministicFiniteAutomata : public AutomataInterface {
private:
set<int> getSymbolClosure(const set<int>& Q);
set<int> move(const set<int>& Q, int a);
set<set<int>> calculateFinal(set<set<int>> k);
void calculateNewK(set<set<int>> newK, DeterministicFiniteAutomata dfa);
set<set<int>> calculateFinal(const set<set<int>>& k);
void calculateNewK(set<set<int>>& newK, DeterministicFiniteAutomata dfa);
};
#endif //TPOB1_NOTDETERMINISTICFINITEAUTOMATA_H
6 changes: 3 additions & 3 deletions TP/auxiliarmethods/CollectionsOperators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ bool CollectionsOperators :: belongs(int elem, const set<int>& set) {
}
return false;
}
bool CollectionsOperators :: setContained(set<set<int>> firstSet, set<int> biggerSet) {
bool CollectionsOperators :: setContained(const set<set<int>>& firstSet, const set<int>& biggerSet) {
bool ret = true;
for(auto currentSet : firstSet) {
for(const auto& currentSet : firstSet) {
if(!contained(currentSet,biggerSet))
return false;
}
return true;
}
void CollectionsOperators :: insertAll(set<int> setToBeModify, const set<int>& otherSet) {
void CollectionsOperators :: insertAll(set<int>& setToBeModify, const set<int>& otherSet) {
for(auto elem : otherSet) {
setToBeModify.insert(elem);
}
Expand Down
4 changes: 2 additions & 2 deletions TP/auxiliarmethods/CollectionsOperators.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class __attribute__((unused)) CollectionsOperators {
public:
static bool contained(const set<int>& firstSet, const set<int>& biggerSet);
static bool belongs(int elem, const set<int>& set);
static bool setContained(set<set<int>> firstSet, set<int> biggerSet);
static void insertAll(set<int> setToBeModify, const set<int>& otherSet);
static bool setContained(const set<set<int>>& firstSet, const set<int>& biggerSet);
static void insertAll(set<int>& setToBeModify, const set<int>& otherSet);
};

#endif //TPOB1_COLLECTIONSOPERATORS_H
5 changes: 3 additions & 2 deletions TP/parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ string Parser::toStringSet(const set<int>& set) {
return ret;
}
void Parser::toStringStatesDFA(DeterministicFiniteAutomata dfa, std::string &ret) {
for(auto set1 : dfa.getK()) {
for(auto set2 : dfa.getK()) {
for(const auto& set1 : dfa.getK()) {
for(const auto& set2 : dfa.getK()) {
set<int> label = dfa.calculateWaysToGo(set1, set2);
if(!label.empty()) {
printf("dasdasd\n");
ret += toStringSet(set1) + " -> " + toStringSet(set2) + " [label = \"";
for(auto elem : label)
ret += to_string(elem) + ",";
Expand Down
Binary file modified cmake-build-debug/CMakeFiles/tpOb1.dir/TP/Main.cpp.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified cmake-build-debug/CMakeFiles/tpOb1.dir/TP/parser/Parser.cpp.o
Binary file not shown.
4 changes: 2 additions & 2 deletions cmake-build-debug/Testing/Temporary/LastTest.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Start testing: Apr 10 11:58 -03
Start testing: Apr 10 18:20 -03
----------------------------------------------------------
End testing: Apr 10 11:58 -03
End testing: Apr 10 18:20 -03
Binary file modified cmake-build-debug/tpOb1
Binary file not shown.

0 comments on commit 58b72cc

Please sign in to comment.