is_unambiguous: rewrite more efficiently

Avoid calling scc_info::determine_unknown_acceptance on the product, as
suggested in #188.

* spot/twaalgos/isunamb.cc (is_unambiguous): Rewrite.
* tests/core/unambig.test: Add the automaton from #188.
* NEWS: Mention the improved function.
* spot/twaalgos/mask.cc,
spot/twaalgos/mask.hh (mask_keep_accessible_states): New function.
This commit is contained in:
Alexandre Duret-Lutz 2016-10-18 17:37:55 +02:00
parent 9b3451c52e
commit 5384a3b89a
5 changed files with 188 additions and 12 deletions

View file

@ -19,25 +19,114 @@
#include <spot/twaalgos/isunamb.hh>
#include <spot/twaalgos/product.hh>
#include <spot/twaalgos/sccfilter.hh>
#include <spot/twaalgos/sccinfo.hh>
#include <spot/twaalgos/mask.hh>
#include <set>
#include <list>
namespace spot
{
// Conceptually, aut is unambiguous if the useful part of aut has
// the same size as the useful part of aut*aut.
//
// However calling scc_info::determine_unknown_acceptance(), which
// is needed to decide which states are actually useless, is costly.
// We do it on aut, but we avoid doing it on prod.
//
// This optimization, which requires much more code than what
// we used to have, was motivated by issue #188.
bool is_unambiguous(const const_twa_graph_ptr& aut)
{
trival u = aut->prop_unambiguous();
if (u.is_known())
return u.is_true();
auto clean_a = scc_filter_states(aut);
if (clean_a->num_edges() == 0)
if (aut->num_edges() == 0)
return true;
auto prod = product(clean_a, clean_a);
auto clean_p = scc_filter_states(prod);
return (clean_a->num_states() == clean_p->num_states()
&& clean_a->num_edges() == clean_p->num_edges());
scc_info sccmap(aut);
sccmap.determine_unknown_acceptance();
unsigned autsz = aut->num_states();
std::vector<bool> v;
v.reserve(autsz);
bool all_useful = true;
for (unsigned n = 0; n < autsz; ++n)
{
bool useful = sccmap.is_useful_state(n);
all_useful &= useful;
v.push_back(useful);
}
// If the input automaton comes from any /decent/ source, it is
// unlikely that it has some useless states, so do not bother too
// much optimizing this case.
if (!all_useful)
return is_unambiguous(mask_keep_accessible_states
(aut, v, aut->get_init_state_number()));
// Reuse v to remember which states are in an accepting SCC.
for (unsigned n = 0; n < autsz; ++n)
v[n] = sccmap.is_accepting_scc(sccmap.scc_of(n));
auto prod = product(aut, aut);
auto sprod =
prod->get_named_prop<std::vector<std::pair<unsigned,
unsigned>>>("product-states");
assert(sprod);
// What follow is a way to compute whether an SCC is useless in
// prod, without having to call
// scc_map::determine_unknown_acceptance() on scc_map(prod),
// because prod has potentially a large acceptance condition.
//
// We know that an SCC of the product is accepting iff it is the
// combination of two accepting SCCs of the original automaton.
//
// So we can just compute the acceptance of each SCC this way, and
// derive the usefulness from that.
scc_info sccmap_prod(prod);
unsigned psc = sccmap_prod.scc_count();
std::vector<bool> useful;
useful.reserve(psc);
for (unsigned n = 0; n < psc; ++n)
{
unsigned one_state = sccmap_prod.states_of(n).front();
bool accepting =
v[(*sprod)[one_state].first] && v[(*sprod)[one_state].second];
if (accepting)
{
useful[n] = true;
continue;
}
bool uf = false;
for (unsigned j: sccmap_prod.succ(n))
if (useful[j])
{
uf = true;
break;
}
useful[n] = uf;
}
// Now we just have to count the number of states && edges that
// belong to the useful part of the automaton.
unsigned np = prod->num_states();
v.resize(np);
unsigned useful_states = 0;
for (unsigned n = 0; n < np; ++n)
{
bool uf = useful[sccmap_prod.scc_of(n)];
v[n] = uf;
useful_states += uf;
}
if (aut->num_states() != useful_states)
return false;
unsigned useful_edges = 0;
for (const auto& e: prod->edges())
useful_edges += v[e.src] && v[e.dst];
return aut->num_edges() == useful_edges;
}
bool check_unambiguous(const twa_graph_ptr& aut)