Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get all one symbol words from NFA #301

Merged
merged 8 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/mata/nfa/strings.hh
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ private:
*/
std::set<Word> get_shortest_words(const Nfa& nfa);

/**
* @brief Get all the one symbol words accepted by @p nfa.
*/
std::set<Symbol> get_accepted_symbols(const Nfa& nfa);

/**
* @brief Get the lengths of all words in the automaton @p aut. The function returns a set of pairs <u,v> where for each
* such a pair there is a word with length u+k*v for all ks. The disjunction of such formulae of all pairs hence precisely
Expand Down
14 changes: 14 additions & 0 deletions src/strings/nfa-strings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ void ShortestWordsMap::update_current_words(LengthWordsPair& act, const LengthWo
act.first = dst.first + 1;
}

std::set<mata::Symbol> mata::strings::get_accepted_symbols(const Nfa& nfa) {
std::set<mata::Symbol> accepted_symbols;
for (State init : nfa.initial) {
for (const SymbolPost& symbol_post_init : nfa.delta[init]) {
mata::Symbol sym = symbol_post_init.symbol;
auto symbol_it = accepted_symbols.lower_bound(sym);
if ((symbol_it == accepted_symbols.end() || *symbol_it != sym)
&& nfa.final.intersects_with(symbol_post_init.targets)) {
accepted_symbols.insert(symbol_it, sym);
}
}
}
return accepted_symbols;
}

std::set<std::pair<int, int>> mata::strings::get_word_lengths(const Nfa& aut) {
Nfa one_letter;
Expand Down
40 changes: 40 additions & 0 deletions tests/strings/nfa-string-solving.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,43 @@ TEST_CASE("mata::nfa::create_single_word_nfa()") {
}
}
}

TEST_CASE("mata::strings::get_accepted_symbols()") {
Nfa x;
std::set<mata::Symbol> symbols;

SECTION("basic") {
create_nfa(&x, "a|bc");
symbols = {'a'};
CHECK(get_accepted_symbols(x) == symbols);
}

SECTION("basic 2") {
create_nfa(&x, "");
CHECK(get_accepted_symbols(x).empty());
}

SECTION("basic 3") {
CHECK(get_accepted_symbols(x).empty());
}

SECTION("advanced 1") {
create_nfa(&x, "a*|c+|(db)*");
symbols = {'a', 'c'};
CHECK(get_accepted_symbols(x) == symbols);
}

SECTION("advanced 2") {
x.delta.add(0, 'a', 1);
x.delta.add(0, 'b', 1);
x.delta.add(2, 'c', 3);
x.delta.add(2, 'd', 4);
x.delta.add(4, 'e', 2);
x.delta.add(2, 'f', 2);
x.delta.add(5, 'g', 1);
x.initial = {0, 2, 4};
x.final = {1, 3, 2};
symbols = {'a', 'b', 'c', 'e', 'f'};
CHECK(get_accepted_symbols(x) == symbols);
}
}