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

Fix counterexamples for inclusion #442

Merged
merged 8 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
17 changes: 17 additions & 0 deletions include/mata/nfa/nfa.hh
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,25 @@ public:
*/
Nfa& trim(StateRenaming* state_renaming = nullptr);

/**
* @brief Returns vector ret where ret[q] is the length of the shortest path from any initial state to q
*/
std::vector<State> distances_from_initial() const;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if we have some general BFS/DFS method parameterized by callbacks (in a similar fashion as for Tarjan). All these functions such as distances_from_initial or distances_to_final could be easily expressible by this and you wouldn't need to write BFS/DFS all over again.

Copy link
Collaborator

@Adda0 Adda0 Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created an issue from this: #446.


/**
* @brief Returns vector ret where ret[q] is the length of the shortest path from q to any final state
*/
std::vector<State> distances_to_final() const;

/**
* @brief Get some shortest accepting run from state @p q
*
* Assumes that @p q is a state of this automaton and that there is some accepting run from q
jurajsic marked this conversation as resolved.
Show resolved Hide resolved
*
* @param distances_to_final Vector of the lengths of the shortest runs from states (can be computed using distances_to_final())
*/
Run get_shortest_accepting_run_from_state(State q, const std::vector<State>& distances_to_final) const;

/**
* Remove epsilon transitions from the automaton.
*/
Expand Down
41 changes: 30 additions & 11 deletions src/nfa/inclusion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,21 @@ bool mata::nfa::algorithms::is_included_naive(
} else {
bigger_cmpl = complement(bigger, *alphabet);
}
Nfa nfa_isect = intersection(smaller, bigger_cmpl);

return nfa_isect.is_lang_empty(cex);
std::unordered_map<std::pair<State,State>,State> prod_map;
Nfa nfa_isect = intersection(smaller, bigger_cmpl, Limits::max_symbol, &prod_map);

bool result = nfa_isect.is_lang_empty(cex);
if (cex != nullptr && !result) {
std::unordered_map<State,State> nfa_isect_state_to_smaller_state;
for (const auto& prod_map_item : prod_map) {
nfa_isect_state_to_smaller_state[prod_map_item.second] = prod_map_item.first.first;
}
for (State& path_state : cex->path) {
path_state = nfa_isect_state_to_smaller_state[path_state];
}
}
return result;
} // is_included_naive }}}


Expand Down Expand Up @@ -69,8 +81,8 @@ bool mata::nfa::algorithms::is_included_antichains(
//Is |S| < |S'| for the inut pairs (q,S) and (q',S')?
// auto smaller_set = [](const ProdStateType & a, const ProdStateType & b) { return std::get<1>(a).size() < std::get<1>(b).size(); };

std::vector<State> distances_smaller = revert(smaller).distances_from_initial();
std::vector<State> distances_bigger = revert(bigger).distances_from_initial();
std::vector<State> distances_smaller = smaller.distances_to_final();
std::vector<State> distances_bigger = bigger.distances_to_final();

// auto closer_dist = [&](const ProdStateType & a, const ProdStateType & b) {
// return distances_smaller[a.first] < distances_smaller[b.first];
Expand Down Expand Up @@ -118,7 +130,7 @@ bool mata::nfa::algorithms::is_included_antichains(
if (smaller.final[state] &&
are_disjoint(bigger.initial, bigger.final))
{
if (cex != nullptr) { cex->word.clear(); }
if (cex != nullptr) { cex->word.clear(); cex->path = {state}; }
return false;
}

Expand Down Expand Up @@ -160,20 +172,27 @@ bool mata::nfa::algorithms::is_included_antichains(
for (const State& smaller_succ : smaller_move.targets) {
const ProdStateType succ = {smaller_succ, bigger_succ, min_dst(bigger_succ)};

if (lengths_incompatible(succ) || (smaller.final[smaller_succ] &&
!bigger.final.intersects_with(bigger_succ)))
if (lengths_incompatible(succ) ||
(smaller.final[smaller_succ] && !bigger.final.intersects_with(bigger_succ)))
{
if (cex != nullptr) {
cex->word.clear();
cex->word.push_back(smaller_symbol);
cex->path.push_back(smaller_state);
ProdStateType trav = prod_state;
while (paths[trav].first != trav)
while (paths.at(trav).first != trav)
{ // go back until initial state
cex->word.push_back(paths[trav].second);
trav = paths[trav].first;
cex->word.push_back(paths.at(trav).second);
cex->path.push_back(std::get<0>(paths.at(trav).first));
trav = paths.at(trav).first;
Adda0 marked this conversation as resolved.
Show resolved Hide resolved
}

std::reverse(cex->word.begin(), cex->word.end());
std::reverse(cex->path.begin(), cex->path.end());

// it is poosible that lengths_incompatible(succ) was true, which means that cex is not finished, we need to add some shortest accepting run from smaller_suc
Run leftover = smaller.get_shortest_accepting_run_from_state(smaller_succ, distances_smaller);
cex->word.insert(cex->word.end(), leftover.word.begin(), leftover.word.end());
cex->path.insert(cex->path.end(), leftover.path.begin(), leftover.path.end());
}

return false;
Expand Down
19 changes: 19 additions & 0 deletions src/nfa/nfa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,25 @@ std::vector<State> Nfa::distances_from_initial() const {
return distances;
}

std::vector<State> Nfa::distances_to_final() const {
return revert(*this).distances_from_initial();
}

Run Nfa::get_shortest_accepting_run_from_state(State q, const std::vector<State>& distances_to_final) const {
Run result{{}, {q}};
while (!final[q]) {
for (Move move : delta[q].moves()) {
if (distances_to_final[move.target] < distances_to_final[q]) {
result.word.push_back(move.symbol);
result.path.push_back(move.target);
q = move.target;
break;
}
}
}
return result;
}

Nfa& Nfa::trim(StateRenaming* state_renaming) {
#ifdef _STATIC_STRUCTURES_
BoolVector useful_states{ useful_states() };
Expand Down
Loading
Loading