This commit is contained in:
MaxanRus 2023-07-16 09:28:07 +03:00
parent 59521e3be2
commit 687e6adb3b
8 changed files with 52 additions and 12 deletions

View file

@ -3,6 +3,7 @@
#include <iostream>
#include <fstream>
extern std::map<size_t, std::string> names;
namespace DFA {
DFAGraph::DFAGraph(DFAGraph&& another) {
std::swap(count_vertexes_, another.count_vertexes_);
@ -131,14 +132,14 @@ void DFAGraph::CreateDotFile(const std::string& filename) const {
for (auto& i: vertexes_) {
for (auto& j: i.second.transitions_) {
out << i.first << "->" << j.second << "[label=" << j.first << "]\n";
out << "\"" << i.first << "\"" << "->\"" << j.second << "\"[label=" << j.first << "]\n";
}
if (i.second.IsStart() && i.second.IsFinal()) {
out << " " << i.first << " [shape=star];\n";
out << " \"" << i.first << "\" [shape=star];\n";
} else if (i.second.IsStart()) {
out << " " << i.first << " [shape=rarrow];\n";
out << " \"" << i.first << "\" [shape=rarrow];\n";
} else if (i.second.IsFinal()) {
out << " " << i.first << " [shape=Msquare];\n";
out << " \"" << i.first << "\" [shape=Msquare];\n";
}
}

View file

@ -26,7 +26,6 @@ const std::map<char, std::set<size_t>>& Vertex::GetBackTransitions() const {
void Vertex::AddEdge(char symbol, size_t number) {
transitions_[symbol].insert(number);
owner_->GetVertex(number).back_transitions_[symbol];
owner_->GetVertex(number).back_transitions_[symbol].insert(number_);
}

View file

@ -18,7 +18,7 @@ DFAGraph DFAGraphToFDFAGraph(DFAGraph&& graph, const std::vector<char>& alphabet
const auto& transitions = graph.GetVertex(i).GetTransitions();
for (const auto& t: transitions) {
result.GetVertex(number_vertex_in_DFA[i]).AddEdge(t.first, t.second);
result.GetVertex(number_vertex_in_DFA[i]).AddEdge(t.first, number_vertex_in_DFA[t.second]);
}
}

View file

@ -66,7 +66,6 @@ DFAGraph DFAGraphToMinDFAGraph(DFAGraph&& graph) {
}
}
/*
for (int i = 0; i < n; ++i) {
for (int k = 0; k < n; ++k) {
for (int j = 0; j < alphabet.size() + 1; ++j) {
@ -76,7 +75,6 @@ DFAGraph DFAGraphToMinDFAGraph(DFAGraph&& graph) {
}
std::cout << std::endl;
}
*/
std::vector<std::vector<size_t>>& last_layer(table.back());
size_t count_vertex = 0;

View file

@ -41,8 +41,8 @@ std::string DFAGraphToRegular(DFAGraph&& graph) {
const bool print_table = true;
auto PrintTable = [n, &transitions](int i) {
std::ofstream out("ConvertToRegular/" + std::to_string(n - i) + ".dot");
auto PrintTable = [cnt, &transitions](int i) {
std::ofstream out("ConvertToRegular/" + std::to_string(cnt - i) + ".dot");
out << "digraph G {\n";
for (int j = 0; j < i; ++j) {
@ -158,7 +158,10 @@ std::string DFAGraphToRegular(DFAGraph&& graph) {
}
if (start_to_end.size())
start_to_end.pop_back();
transitions[0][0].clear();
transitions[1][0].clear();
transitions[1][0].insert(loop + "(" + start_to_end + ")");
return loop + "(" + start_to_end + ")";
}
}

View file

@ -101,7 +101,7 @@ NFAGraph DeleteTransitionsByOneLetter(NFAGraph&& nfa_graph) {
number_vertex_in_result_tree[{i}] = result_tree.AddNewVertex();
}
const bool print_table = false;
const bool print_table = true;
const size_t length_set = 10;
if (print_table) {
std::cout << std::setw(length_set) << "vertex" << " | ";
@ -191,7 +191,9 @@ DFAGraph NFAGraphToDFAGraph(NFAGraph&& nfa_graph) {
nfa_graph = AddAllEpsilonTransitions(std::move(nfa_graph));
nfa_graph = AddAllPossibleFinalVertexes(std::move(nfa_graph));
nfa_graph = DeleteEpsilonTransitions(std::move(nfa_graph));
nfa_graph.CreateDotFile("5.dot");
nfa_graph = DeleteTransitionsByOneLetter(std::move(nfa_graph));
nfa_graph.CreateDotFile("6.dot");
const int n = nfa_graph.GetCountVertexes();
DFAGraph result;

View file

@ -44,6 +44,16 @@ NFAGraph RegularToNFA(std::unique_ptr<Node, deleter> node) {
const auto& start_vertexes = result.GetStartVertexes();
const auto& end_vertexes = result.GetFinalVertexes();
size_t start = result.AddNewVertex();
for (auto to: start_vertexes) {
result.GetVertex(start).AddEdge(' ', to);
}
auto t = result.GetStartVertexes();
for (auto to: t) {
result.RemoveStartVertex(to);
}
result.AddStartVertex(start);
if (node->modifier == Node::Modifier::Plus) {
for (auto start: start_vertexes) {
for (auto end: end_vertexes) {

View file

@ -63,6 +63,33 @@ std::string GenerateRandomString(std::vector<char> alphabet, size_t len) {
}
return res;
}
std::map<size_t, std::string> names;
int main() {
NFAGraph nfa;
for (int i = 0; i < 10; ++i)
nfa.AddNewVertex();
auto AddEdge = [&](char symbol, int u, int v) {
nfa.GetVertex(u).AddEdge(symbol, v);
};
AddEdge('a', 1, 1);
AddEdge('a', 1, 2);
AddEdge('b', 2, 1);
AddEdge('b', 1, 3);
AddEdge('a', 3, 1);
AddEdge('b', 3, 4);
AddEdge('a', 4, 3);
AddEdge('a', 0, 1);
nfa.GetVertex(0).SetStart(true);
nfa.GetVertex(1).SetFinal(true);
nfa.CreateDotFile("3.dot");
DFAGraph dfa = NFAGraphToDFAGraph(std::move(nfa));
dfa = DFAGraphToFDFAGraph(std::move(dfa), {'a', 'b'});
dfa = InvertFDFAGraph(std::move(dfa));
dfa.CreateDotFile("1.dot");
dfa.CreateDotFile("8.dot");
dfa = DFAGraphToMinDFAGraph(std::move(dfa));
dfa.CreateDotFile("2.dot");
}