$\text{#NFA}$ admits an FPRAS: Efficient Enumeration, Counting, and Uniform Generation for Logspace Classes
Marcelo Arenas, Luis Alberto Croquevielle, Rajesh Jayaram, Cristian, Riveros

TL;DR
This paper introduces efficient algorithms for enumeration, counting, and uniform generation in logspace classes, proving that the fundamental #NFA problem admits a fully polynomial randomized approximation scheme, thus advancing understanding of complexity in these classes.
Contribution
It demonstrates that #NFA admits an FPRAS, solving an open problem, and extends this result to functions in SpanL, providing new algorithms for logspace-based classes.
Findings
#NFA admits an FPRAS, solving an open problem.
Functions in SpanL admit an FPRAS.
Efficient enumeration, counting, and uniform generation algorithms are developed for logspace classes.
Abstract
In this work, we study two simple yet general complexity classes, based on logspace Turing machines, which provide a unifying framework for efficient query evaluation in areas like information extraction and graph databases, among others. We investigate the complexity of three fundamental algorithmic problems for these classes: enumeration, counting and uniform generation of solutions, and show that they have several desirable properties in this respect. Both complexity classes are defined in terms of non-deterministic logspace transducers (NL transducers). For the first class, we consider the case of unambiguous NL transducers, and we prove constant delay enumeration, and both counting and uniform generation of solutions in polynomial time. For the second class, we consider unrestricted NL transducers, and we obtain polynomial delay enumeration, approximate counting in polynomial…
Peer Reviews
No public reviews on file for this paper yet. If you reviewed it on a platform where reviews are public (OpenReview, ICLR, NeurIPS, ICML), you can paste yours below so the community can read it here.
Videos
No videos yet. Explain this paper in a talk, walkthrough, or lecture? Add one.
Taxonomy
TopicsAlgorithms and Data Compression · Machine Learning and Algorithms · Natural Language Processing Techniques
#NFA admits an FPRAS: Efficient Enumeration, Counting, and Uniform Generation for Logspace Classes
Marcelo Arenas
Pontificia Universidad Católica & IMFDSantiagoChile
,
Luis Alberto Croquevielle
Pontificia Universidad Católica & IMFDSantiagoChile
,
Rajesh Jayaram
Carnegie Mellon UniversityPittsburghUnited States
and
Cristian Riveros
Pontificia Universidad Católica & IMFDSantiagoChile
(2018)
Abstract.
In this work, we study two simple yet general complexity classes, based on logspace Turing machines, which provide a unifying framework for efficient query evaluation in areas like information extraction and graph databases, among others. We investigate the complexity of three fundamental algorithmic problems for these classes: enumeration, counting and uniform generation of solutions, and show that they have several desirable properties in this respect.
Both complexity classes are defined in terms of non-deterministic logspace transducers (NL-transducers). For the first class, we consider the case of unambiguous NL-transducers, and we prove constant delay enumeration, and both counting and uniform generation of solutions in polynomial time. For the second class, we consider unrestricted NL-transducers, and we obtain polynomial delay enumeration, approximate counting in polynomial time, and polynomial-time randomized algorithms for uniform generation. More specifically, we show that each problem in this second class admits a fully polynomial-time randomized approximation scheme (FPRAS) and a polynomial-time Las Vegas algorithm (with preprocessing) for uniform generation. Remarkably, the key idea to prove these results is to show that the fundamental problem #NFA admits an FPRAS, where #NFA is the problem of counting the number of strings of length (given in unary) accepted by a non-deterministic finite automaton (NFA). While this problem is known to be #P-complete and, more precisely, SpanL-complete, it was open whether this problem admits an FPRAS. In this work, we solve this open problem, and obtain as a welcome corollary that every function in SpanL admits an FPRAS.
Enumeration, counting, uniform generation.
††copyright: acmcopyright††journalyear: 2018††doi: 10.1145/1122445.1122456††journal: JACM††journalvolume: 37††journalnumber: 4††publicationmonth: 8††ccs: Information systems Query optimization††ccs: Information systems Information extraction††ccs: Information systems Semi-structured data††ccs: Theory of computation Turing machines††ccs: Theory of computation Complexity classes††ccs: Theory of computation Regular languages††ccs: Theory of computation Probabilistic computation
1. Introduction
Arguably, query answering is the most fundamental problem in databases. In this respect, developing efficient query answering algorithms, as well as understanding when this cannot be done, is of paramount importance for database theory and applications. In the most classical view of this problem, one is interested in computing all the answers, or solutions, to a query. However, as the quantity of data becomes enormously large, the number of answers to a query could also be enormous, so computing the complete set of solutions can be prohibitively expensive. In order to overcome this limitation, the idea of enumerating the answers to a query with a small delay has been recently studied in the database area (Segoufin, 2013). More specifically, the idea is to divide the computation of the answers to a query into two phases. In a preprocessing phase, some data structures are constructed to accelerate the process of computing answers. Then in an enumeration phase, the answers are enumerated with a small delay between them. In particular, in the case of constant delay enumeration algorithms, the preprocessing phase should take polynomial time, while the time between consecutive answers should be constant.
Constant delay enumeration algorithms allow users to retrieve a fixed number of answers very efficiently, which can give them a lot of information about the solutions to a query. In fact, the same holds if users need a linear or a polynomial number of answers. However, because of the data structures used in the preprocessing phase, these algorithms usually return answers that are very similar to each other (Bagan et al., 2007; Segoufin, 2013; Florenzano et al., 2018); for example, tuples with elements where only the first few coordinates are changed in the first answers that are returned. In this respect, other approaches can be used to return some solutions efficiently but improving their variety. Most notably, the possibility of generating an answer uniformly, at random, is a desirable condition if it can be done efficiently. Notice that returning varied solutions has been identified as an important property not only in databases, but also for algorithms that retrieve information in a broader sense (Abiteboul et al., 2016).
Efficient algorithms for either enumerating or uniformly generating the answers to a query are powerful tools to help in the process of understanding the answers to a query. But how can we know how long these algorithms should run, and how complete the set of computed answers is? A third tool that is needed then is an efficient algorithm for computing, or estimating, the number of solutions to a query. Then, taken together, enumeration, counting and uniform generation techniques form a powerful attacking trident when confronting the problem of answering a query.
In this paper, we follow a principled approach to study the problems of enumerating, counting and uniformly generating the answers to a query. More specifically, we begin by following the guidance of (Jerrum et al., 1986), which urges the use of relations to formalize the notion of solution to a given input of a problem (for instance, to formalize the notion of answer to an input query over an input database). While there are many ways of formalizing this notion, most such formalizations only make sense for a specific kind of queries, e.g. a subset of the integers is well-suited as the solution set for counting problems, but not for sampling problems. We want a general framework, so by following (Jerrum et al., 1986), we represent a problem as a relation , and we say that is a solution for an input if .111For the sake of presentation, we assume relations to be defined over the binary alphabet . The results of this article also hold if we consider relations defined over an arbitrary finite alphabet . Note that the problem of enumerating the solutions to a given input corresponds to the problem of enumerating the elements of the set , while the counting and uniform generation problems correspond to the problems of computing the cardinality of and uniformly generating, at random, a string in this set, respectively.
Second, we study two simple yet general complexity classes for relations, based on non-deterministic logspace transducers (NL-transducers), which provide a unifying framework for studying enumeration, counting and uniform generation. More specifically, an NL-transducer is a non-deterministic Turing Machine with input and output alphabet , a read-only input tape, a write-only output tape and a work-tape of which, on input , only the first cells can be used. Moreover, a string is said to be an output of on input , if there exists a run of on input that halts in an accepting state with as the string in the output tape. Finally, assuming that all outputs of on input are denoted by , a relation is said to be accepted by if for every input , it holds that .
The first complexity class of relations studied in this paper consists of the relations accepted by unambiguous NL-transducers. More precisely, an NL-transducer is said to be unambiguous if for every input and , there exists exactly one run of on input that halts in an accepting state with as the string in the output tape. For this class, we are able to achieve constant delay enumeration, and both counting and uniform generation of solutions in polynomial time. For the second class, we consider (unrestricted) NL-transducers, and we obtain polynomial delay enumeration, approximate counting in polynomial time, and polynomial-time randomized algorithms for uniform generation. More specifically, we show that each problem in this second class admits a fully polynomial-time randomized approximation scheme (FPRAS) (Jerrum et al., 1986) and a polynomial-time Las Vegas algorithm (with preprocessing) for uniform generation. It is important to mention that the key idea to prove these results is to show that the fundamental problem #NFA admits an FPRAS, where #NFA is the problem of counting the number of strings of length (given in unary) accepted by a non-deterministic finite automaton (NFA). While this problem is known to be #P-complete and, more precisely, SpanL-complete (Álvarez and Jenner, 1993), it was open whether it admits an FPRAS, and only quasi-polynomial time randomized approximation schemes (QPRAS) were known for it (Kannan et al., 1995; Gore et al., 1997). In this work, we solve this open problem, and obtain as a welcome corollary that every function in SpanL admits an FPRAS. Thus, to the best of our knowledge, we identify SpanL as the first complexity class with a simple and robust definition based on Turing Machines, that contains #P-complete problems and where each problem admits an FPRAS.
Proviso. This paper is an extended version of the article “Efficient Logspace Classes for Enumeration, Counting, and Uniform Generation” that was published in the 38 ACM SIGMOD-SIGACT-SIGAI Symposium on Principles of Database Systems (PODS 2019). For this extended version, we have made many changes. In particular, we have completely reworked the proof that #NFA admits a fully polynomial-time randomized approximation schema, which is the main result of this work. More specifically, we have carefully restructured this proof for the sake of readability, obtaining a simpler and more efficient algorithm. Besides, for the more general class of relations defined in terms of (unrestricted) NL-transducers, this result allows us to prove that each problem in this class admits a polynomial-time Las Vegas uniform generator. Such a randomized algorithm needs a preprocessing phase, which was not properly made explicit in the conference paper. We have solved this issue by introducing, and studying, the notion of preprocessing polynomial-time Las Vegas uniform generator.
Organization of the paper. The main terminology used in the paper is given in Section 2. In Section 3, we define the two classes studied in this paper and state our main results. In Section 4, we show how these classes can be used to obtain positive results on query evaluation in information extraction, graph databases, and binary decision diagrams. The complete proofs of our results are presented in Sections 5 and 6, and Appendix A. In particular, we explain the algorithmic techniques used to obtain an FPRAS for the #NFA problem in Section 6, where we also provide a detailed proof of this result. Finally, some concluding remarks are given in Section 7.
2. Preliminaries
Given natural numbers , we use notation for the set . Beside, we use to refer to the logarithm of to base .
2.1. Relations and problems
As usual, denotes the set of all strings over the binary alphabet , denotes the length of a string , denotes the concatenation of two strings , and denotes the set of all strings such that . A problem is represented as a relation . For every pair , we interpret as being the encoding of an input to some problem, and as being the encoding of a solution to that input. For each , we define the set , and call it the set of solutions for . Also, if , we call a solution to .
This is a very general framework, so we work with -relations (Jerrum et al., 1986). Formally, a relation is a -relation if (1) there exists a polynomial such that implies that and (2) there exists a deterministic Turing Machine that receives as input , runs in polynomial time and accepts if, and only if, . Without loss of generality, from now on we assume that for a -relation , there exists a polynomial such that for every . This is not a strong requirement, since all solutions can be made to have the same length through padding.
2.2. Enumeration, counting and uniform generation
There are several computational problems associated to a relation . For example, given a relation and an input , we could consider the existence problem of deciding whether there are any solutions for . In this paper, given a -relation , we are interested in the following problems:
Problem:
Input:
A word
Output:
Enumerate all without repetitions
Problem:
Input:
A word
Output:
The size
Problem:
Input:
A word
Output:
Generate uniformly, at random, a word in
Given that for every , we have that is finite and these three problems are well defined. Notice that in the case of , we do not assume a specific order on words, so that the elements of can be enumerated in any order (but without repetitions). Moreover, in the case of , we assume that is encoded in binary and, therefore, the size of the output is logarithmic in the size of . Finally, in the case of , we generate a word with probability if the set is not empty; otherwise, we return a special symbol to indicate that .
2.3. Enumeration with polynomial and constant delay
An enumeration algorithm for is a procedure that receives an input and, during the computation, it outputs each word in , one by one and without repetitions. The time between two consecutive outputs is called the delay of the enumeration. In this paper, we consider two restrictions on the delay: polynomial delay and constant delay. Polynomial delay enumeration is the standard notion of polynomial time efficiency in enumeration algorithms (Johnson et al., 1988) and is defined as follows. An enumeration algorithm is of polynomial delay if there exists a polynomial such that for every input , the time between the beginning of the algorithm and the initial output, between any two consecutive outputs, and between the last output and the end of the algorithm, is bounded by .
Constant delay enumeration is another notion of efficiency for enumeration algorithms that has attracted a lot attention in the recent years (Bagan, 2006; Courcelle, 2009; Segoufin, 2013). This notion has stronger guarantees compared to polynomial delay: the enumeration is done in a second phase after the processing of the input and taking constant-time between two consecutive outputs in a very precise sense. Several notions of constant delay enumeration has been given, most of them in database theory where it is important to divide the analysis between query and data. In this paper, we want a definition of constant delay that is agnostic of the distinction between query and data (i.e. combined complexity) and, for this reason, we use a more general notion of constant delay enumeration than the one in (Bagan, 2006; Courcelle, 2009; Segoufin, 2013).
Constant delay enumeration cannot be achieved in general with a standard Turing Machine, because merely moving the head through the tape will take up more than constant time. So, as it is standard in the literature (Segoufin, 2013), for the notion of constant delay enumeration we consider enumeration algorithms on Random Access Machines (RAM) with addition and uniform cost measure (Aho and Hopcroft, 1974). Given a relation , an enumeration algorithm for has constant delay if runs in two phases over the input .
- (1)
The first phase (precomputation), which does not produce output. 2. (2)
The second phase (enumeration), which occurs immediately after the precomputation phase, where all words in are enumerated without repetitions and satisfying the following conditions, for a fixed constant :
- (a)
the time it takes to generate the first output is bounded by ; 2. (b)
the time between two consecutive outputs and is bounded by and does not depend on ; and 3. (c)
the time between the final element that is returned and the end of the enumeration phase is bounded by ,
We say that is a constant delay algorithm for with precomputation phase , if has constant delay and the precomputation phase takes time . Moreover, we say that can be solved with constant delay if there exists a constant delay algorithm for with precomputation phase for some polynomial .
Our notion of constant delay algorithm differ from the definitions in (Segoufin, 2013) in two aspects. First, in our definition the input is not divided into some components, so the preprocessing phase must take polynomial time on the size of the entire input. In the case of constant delay algorithms for query answering, the input is usually divided into the data and the query, and the preprocessing phase is only asked to take polynomial time on the size of the data (as the query is usually assumed to be fixed, which is referred to as data complexity (Vardi, 1982)). Second, our definition of constant delay is what in (Courcelle, 2009; Bagan, 2006) is called linear delay in the size of the output, namely, writing the next output is linear in its size and does not depend on the size of the input. This is a natural assumption, since each output must at least be written down to return it to the user. Notice that, given an input and an output , the notion of polynomial delay above means polynomial in and, instead, the notion of linear delay from (Courcelle, 2009; Bagan, 2006) means linear in , i.e., constant in the size of . Thus, we have decided to call the two-phase enumeration from above “constant delay”, as it does not depend on the size of the input , and the delay is just what is needed to write the output (which is the minimum requirement for such an enumeration algorithm).
2.4. Approximate counting and Las Vegas uniform generation with preprocessing
Given a relation , the problem can be solved efficiently if there exists a polynomial-time algorithm that, given , computes . In other words, if we think of as a function that maps to the value , then can be computed efficiently if , the class of functions that can be computed in polynomial time. As such a condition does not hold for many fundamental problems, we also consider the possibility of efficiently approximating the value of the function . More precisely, is said to admit a fully polynomial-time randomized approximation scheme (FPRAS) (Jerrum et al., 1986) if there exists a randomized algorithm and a polynomial such that for every and , it holds that:
[TABLE]
and the time needed to compute is at most . Thus, approximates the value with a relative error of , and it can be computed in polynomial time in the size of and the value .
The problem can be solved efficiently if there exists a polynomial-time randomized algorithm that, given , generates an element of with uniform probability distribution (if , then it returns ). However, as in the case of , the existence of such a generator is not guaranteed for many fundamental problems, so we also consider a relaxed notion of generation that has a probability of failing in returning a solution. More precisely, is said to admit a preprocessing polynomial-time Las Vegas uniform generator (PPLVUG) if there exists a pair of randomized algorithms , and a pair of polynomials , such that for every and :
- (1)
The preprocessing algorithm receives as inputs and , and runs in time bounded by . If , then returns a string such that is good-for-generation with probability . If , then returns . 2. (2)
The generator algorithm receives as input and runs in time bounded by . Moreover, if is good-for-generation then:
- (a)
returns fail with a probability at most , and 2. (b)
conditioned on not returning fail, returns a truly uniform sample , i.e. with a probability for each .
Otherwise, if is not good-for-generation, then outputs a string without any guarantee.
In line with the notion of constant delay enumeration algorithm, we allow the previous concept of uniform generator to have a preprocessing phase. If there is no solution for the input (that is, ), then the preprocessing algorithm returns the symbol . Otherwise, the invocation returns a string in , namely, a data structure or “advice” for the generation procedure . The output of the invocation is used by the generator algorithm to produce a solution of with uniform distribution (that is, with probability ). If the output of is not good-for-generation (which occurs with probability ), then we have no guarantees on the output of the generator algorithm . Otherwise, we know that returns an element of with uniform distribution, or it returns fail. Furthermore, we can repeat as many times as needed, generating each time a truly uniform sample from whenever .
Notice that by condition (2a), we know that this probability of failing is smaller than , so that by invoking several times we can make this probability arbitrarily small (for example, the probability that returns fail in 1000 consecutive independent invocations is at most ). Moreover, we have that can be computed in time , so we can consider an exponentially small value of such as
[TABLE]
and still obtain that can be computed in time polynomial in . Notice that with such a value of , the probability of producing a good-for-generation string is at least
[TABLE]
which is an extremely high probability . Finally, it is important to notice that the size of is at most , so that can be computed in time polynomial in and . Therefore, can be computed in time polynomial in even if we consider an exponentially small value for such as .
It is important to notice that the notion of preprocessing polynomial-time Las Vegas uniform generator imposes stronger requirements than the notion of fully polynomial-time almost uniform generator introduced in (Jerrum et al., 1986). In particular, the latter not only has a probability of failing, but also considers the possibility of generating a solution with a probability distribution that is almost uniform, that is, an algorithm that generates an string with a probability in an interval for a given error .
3. NLOGSPACE transducers: definitions and our main results
The goal of this section is to provide simple yet general definitions of classes of relations with good properties in terms of enumeration, counting and uniform generation. More precisely, we are first aiming at providing a class of relations that has a simple definition in terms of Turing Machines and such that for every relation , it holds that can be solved with constant delay, and both and can be solved in polynomial time. Moreover, as it is well known that such good conditions cannot always be achieved, we are then aiming at extending the definition of to obtain a simple class, also defined in terms of Turing Machines and with good approximation properties. It is important to mention that we are not looking for an exact characterization in terms of Turing Machines of the class of relations that admit constant delay enumeration algorithms, as this may result in an overly complicated model. Instead, we are looking for simple yet general classes of relations with good properties in terms of enumeration, counting and uniform generation, and which can serve as a starting point for the systematic study of these three fundamental properties.
A key notion that is used in our definitions of classes of relations is that of transducer. An NL-transducer is a non-deterministic Turing Machine with input and output alphabet , a read-only input tape, a write-only output tape where the head is always moved to the right once a symbol is written in it (so that the output cannot be read by ), and a work-tape of which, on input , only the first cells can be used, where . A string is said to be an output of on input , if there exists a run of on input that halts in an accepting state with as the string in the output tape. The set of all outputs of on input is denoted by (notice that can be empty). Finally, the relation accepted by , denoted by , is defined as .
Definition 3.1.
A relation is in RelationNL if, and only if, there exists an NL-transducer such that .
Cycles are forbidden in NL-transducers to have polynomial-size solutions for each input (Álvarez and Jenner, 1993). However, we do not need to impose this restriction here as we only work with -relations in this paper (see Section 2).
The class RelationNL should be general enough to contain some natural and well-studied problems. A first such a problem is the satisfiability of a propositional formula in DNF. As a relation, this problem can be represented as follows:
[TABLE]
Thus, we have that corresponds to the problem of enumerating the truth assignments satisfying a propositional formula in DNF, while and correspond to the problems of counting and uniformly generating such truth assignments, respectively. It is not difficult to see that . In fact, assume that we are given a propositional formula of the form , where each is a conjunction of literals, that is, a conjunction of propositional variables and negation of propositional variables. Moreover, assume that each propositional variable in is of the form , where is a binary number, and that , , are the variables occurring in . Notice that with such a representation, we have that is a string over the alphabet .222For the sake of presentation, we consider a non-binary alphabet in this case, although it is easy to see how SAT-DNF can be represented by using the binary alphabet. We define as follows an NL-transducer such that is the set of truth assignments satisfying . On input , the NL-transducer non-deterministically chooses a disjunct , which is represented by two indexes indicating the starting and ending symbols of in the string . Then it checks whether is satisfiable, that is, whether does not contain complementary literals. Notice that this can be done in logarithmic space by checking for every , whether and are both literals in . If is not satisfiable, then halts in a non-accepting state. Otherwise, returns a satisfying truth assignment of as follows. A truth assignment for is represented by a string of length over the alphabet , where the -th symbol of this string is the truth value assigned to variable . Then for every , if is a conjunct in , then writes the symbol 1 in the output tape, and if is a conjunct in , then writes the symbol 0 in the output tape. Finally if neither nor is a conjunct in , then non-deterministically chooses a symbol , and it writes in the output tape.
Given that is a #P-complete problem (Provan and Ball, 1983), we cannot expect to be solvable in polynomial time for every . However, admits an FPRAS (Karp and Luby, 1983), so we can still hope for to admit an FPRAS for every . It turns out that proving such a result involves providing an FPRAS for another natural and fundamental problem: #NFA. More specifically, #NFA is the problem of counting the number of words of length accepted by a non-deterministic finite automaton without epsilon transitions (NFA), where is given in unary (that is, is given as a string ). It is known that #NFA is #P-complete (Álvarez and Jenner, 1993), but it is open whether it admits an FPRAS; in fact, the best randomized approximation scheme known for #NFA runs in time (Kannan et al., 1995). In our notation, this problem is represented by the following relation:
[TABLE]
that is, we have that . It is easy to see that . Hence, we give a positive answer to the open question of whether #NFA admits an FPRAS by proving the following general result about RelationNL.
Theorem 3.2.
If , then can be solved with polynomial delay, admits an FPRAS, and admits a PPLVUG.
It is worth mentioning a fundamental consequence of this result in computational complexity. The class of functions SpanL was introduced in (Álvarez and Jenner, 1993) to provide a characterization of some functions that are hard to compute. More specifically, a function is in SpanL if there exists an NL-transducer with input alphabet such that for every . The complexity class SpanL is contained in #P, and it is a hard class in the sense that if , then (Álvarez and Jenner, 1993), where FP is the class of functions that can be computed in polynomial time. In fact, SpanL has been instrumental in proving that some functions are difficult to compute (Álvarez and Jenner, 1993; Hemaspaandra and Vollmer, 1995; Arenas et al., 2012; Losemann and Martens, 2013). It is not difficult to see that #NFA belongs to SpanL (Álvarez and Jenner, 1993).
Given functions , is said to be parsimoniously reducible to in polynomial-time if there exists a polynomial-time computable function such that, for every , it holds that . It is known that #NFA is SpanL-complete under polynomial-time parsimonious reductions, which in particular implies that if #NFA can be computed in polynomial time, then (Álvarez and Jenner, 1993). Moreover, given that #NFA admits an FPRAS and parsimonious reductions preserve the existence of FPRAS, we obtain the following corollary from Theorem 3.2.
Corollary 3.3.
Every function in SpanL admits an FPRAS.
Although some classes containing #P-complete functions and for which every admits an FPRAS have been identified before (Saluja et al., 1995; Arenas et al., 2017), to the best of our knowledge this is the first such a class with a simple and robust definition based on Turing Machines.
A tight relationship between the existence of an FPRAS and the existence of a schema for almost uniform generation was proved in (Jerrum et al., 1986), for the class of relations that are self-reducible. Thus, one might wonder whether the existence of a PPLVUG for in Theorem 3.2 is just a corollary of our FPRAS for along with the result in (Jerrum et al., 1986). However, as the notion of PPLVUG asks for a uniform generator without any distributional error , it is not clear how to infer its existence from the results in (Jerrum et al., 1986). Thus, we prove in Section 6 that admits an FPRAS and admits a PPLVUG, for a relation , without utilizing the aforementioned result from (Jerrum et al., 1986).
A natural question at this point is whether a simple syntactic restriction on the definition of RelationNL gives rise to a class of relations with better properties in terms of enumeration, counting and uniform generation. Fortunately, the answer to this question comes by imposing a natural and well-studied restriction on Turing Machines, which allows the definition of a class that contains many natural problems. More precisely, we consider the notion of UL-transducer, where the letter “U” stands for “unambiguous”. Formally, is a UL-transducer if is an NL-transducer such that for every input and , there exists exactly one run of on input that halts in an accepting state with as the string in the output tape. Notice that this notion of transducer is based on well-known classes of decision problems (e.g. UP (Valiant, 1976) and UL (Reinhardt and Allender, 2000)) adapted to our case, namely, adapted to problems defined as relations.
Definition 3.4.
A relation is in RelationUL if, and only if, there exists a UL-transducer such that .
For the class RelationUL, we obtain the following result.
Theorem 3.5.
If , then can be solved with constant delay, there exists a polynomial-time algorithm for , and there exists a polynomial-time randomized algorithm for .
In particular, it should be noticed that given and an input , the solutions for can be enumerated, counted and uniformly generated efficiently. In the following section, we provide examples of relations in this class.
Classes of problems definable by machine models and that can be enumerated with constant delay have been proposed before. In (Amarilli et al., 2017), it is shown that if a problem is definable by a d-DNNF circuit, then the solutions of an instance can be listed with linear preprocessing and constant delay enumeration. Still, to the best of our knowledge, RelationUL is the first such a class with a simple and robust definition based on Turing Machines.
On the relationship of RelationNL and RelationUL with known complexity classes.
It is well-known that a function is in if and only if there exists a -relation such that (recall the definition of -relation from Section 2). In the same way, there exists a tight relationship between SpanL and RelationNL, as it is easy to see that a function is in SpanL if and only if there exists a relation such that . Hence, the reader may wonder why it is necessary to introduce RelationNL and RelationUL, considering further that such classes are defined in terms of well-known Turing Machine models. The key issue to consider here is that function complexity classes, such as and SpanL, are not appropriate to state results about the enumeration and uniform generation problems. For instance, it would not be correct to state that every function in SpanL admits a PPLVUG, as the definition of SpanL does not provide a unique notion of solution for an input, which is the object to be generated in this case. In this respect, we introduce RelationNL and RelationUL to have a unified framework to study the counting, enumeration and uniform generation problems. The definition of such classes should not be considered as a contribution of this paper. In fact, they should only be seen as our way of following the guidance of (Jerrum et al., 1986), which, as mentioned before, urges the use of relations to formalize the notion of solution for an input of a problem.
4. Applications of the Main Results
Before providing the proofs of Theorems 3.2 and 3.5, we give some implications of these results. In particular, we show how NL- and UL-transducers can be used to obtain positive results on query evaluation in areas like information extraction, graph databases, and binary decision diagrams.
4.1. Information extraction
In (Fagin et al., 2015), the framework of document spanners was proposed as a formalization of ruled-based information extraction. In this framework, the main data objects are documents and spans. Formally, given a finite alphabet , a document is a string and a span is pair with . A span represents a continuous region of the document , whose content is the substring of from positions to . Given a finite set of variables X, a mapping is a function from X to the spans of .
Variable set automata (VA) are one of the main formalisms to specify sets of mappings over a document. Here, we use the notion of extended VA (eVA) from (Florenzano et al., 2018) to state our main results. We only recall the main definitions, and we refer the reader to (Florenzano et al., 2018; Fagin et al., 2015) for more intuition and further details. An eVA is a tuple such that is a finite set of states, is the initial state, and is the final set of states. Further, is the transition relation consisting of letter transitions , or variable-set transitions , where and . The symbols and are called markers, and they are used to denote that variable is opened or closed by , respectively. A run over a document is a sequence of the form: q_{0}\ \raisebox{-1.0pt}[10.0pt][0.0pt]{\overset{X_{1}}{\underset{}{\raisebox{0.0pt}[3.0pt][0.0pt]{}}}}\ p_{0}\ \raisebox{-1.0pt}[10.0pt][0.0pt]{\overset{a_{1}}{\underset{}{\raisebox{0.0pt}[3.0pt][0.0pt]{}}}}\ q_{1}\ \raisebox{-1.0pt}[10.0pt][0.0pt]{\overset{X_{2}}{\underset{}{\raisebox{0.0pt}[3.0pt][0.0pt]{}}}}\ p_{1}\ \raisebox{-1.0pt}[10.0pt][0.0pt]{\overset{a_{2}}{\underset{}{\raisebox{0.0pt}[3.0pt][0.0pt]{}}}}\ \ldots\ \ \raisebox{-1.0pt}[10.0pt][0.0pt]{\overset{a_{n}}{\underset{}{\raisebox{0.0pt}[3.0pt][0.0pt]{}}}}\ q_{n}\ \raisebox{-1.0pt}[10.0pt][0.0pt]{\overset{X_{n+1}}{\underset{}{\raisebox{0.0pt}[3.0pt][0.0pt]{}}}}\ p_{n} where each is a (possible empty) set of markers, , and , whenever , and otherwise (that is, when ). We say that a run is valid if for every there exists exactly one pair such that and . A valid run naturally defines a mapping that maps to the only span such that and . We say that is accepting if . Finally, the semantics of over is defined as the set of all mappings where is a valid and accepting run of over .
In (Freydenberger, 2017; Maturana et al., 2018), it was shown that the decision problem related to query evaluation, namely, given an eVA and a document deciding whether , is NP-hard. For this reason, in (Florenzano et al., 2018) a subclass of eVA is considered in order to recover polynomial-time evaluation. An eVA is called functional if every accepting run is valid. Intuitively, a functional eVA does not need to check validity of the run given that it is already known that every run that reaches a final state will be valid.
For the query evaluation problem of functional eVA (i.e. to compute ), one can naturally associate the following relation:
[TABLE]
It is not difficult to show that EVAL-eVA is in RelationNL. Hence, by Theorem 3.2 we get the following results.
Corollary 4.1.
* can be enumerated with polynomial delay, admits an FPRAS, and admits a PPLVUG.*
In (Florenzano et al., 2018), it was shown that every functional RGX or functional VA (not necessarily extended) can be converted in polynomial time into an functional eVA. Therefore, Corollary 4.1 also holds for these more general classes. Notice that in (Freydenberger et al., 2018), a polynomial delay enumeration algorithm for was provided. Thus, only the results about and can be considered as new.
Regarding efficient enumeration and exact counting, a constant delay algorithm with polynomial preprocessing was given in (Florenzano et al., 2018) for the class of deterministic functional eVA. Here, we can easily extend these results for a more general class, that we called unambiguous functional eVA. Formally, we say that an eVA is unambiguous if for every two valid and accepting runs and , it holds that . In other words, each output of an unambiguous eVA is witnessed by exactly one run. As in the case of EVAL-eVA, we can define the relation EVAL-UeVA, by restricting the input to unambiguous functional eVA. By using UL-transducers and Theorem 3.5, we can then extend the results in (Florenzano et al., 2018) for the unambiguous case.
Corollary 4.2.
* can be solved with constant delay, there exists a polynomial-time algorithm for , and there exists a polynomial-time randomized algorithm for .*
Notice that this result gives a constant delay algorithm with polynomial preprocessing for the class of unambiguous functional eVA. Instead, the algorithm in (Florenzano et al., 2018) has linear preprocessing over documents, restricted to the case of deterministic eVA. This leaves open whether there exists a constant delay algorithm with linear preprocessing over documents for the unambiguous case.
4.2. Query evaluation in graph databases
Enumerating, counting, and generating paths are relevant tasks for query evaluation in graph databases (Angles et al., 2017). Given a finite set of labels, a graph database is a pair where is a finite set of vertices and is a finite set of labeled edges. Here, vertices represent pieces of data and edges specify relations between them (Angles et al., 2017). One of the core query languages for posing queries on graph databases are regular path queries (RPQ). An RPQ is a triple where are variables and is a regular expression over . As usual, we denote by all the strings over that conform to . Given an RPQ , a graph database , and vertices , one would like to retrieve, count, or uniformly generate all paths333Notice that the standard semantics for RPQs is to retrieve pair of vertices. Here we consider a less standard semantics based on paths which is also relevant for graph databases (Arenas et al., 2012; Losemann and Martens, 2013; Angles et al., 2017). in going from to that satisfy . Formally, a path from to in is a sequence of vertices and labels of the form , such that , , and . A path is said to satisfy if the string . The length of is defined as . Clearly, between and there can be an infinite number of paths that satisfy . For this reason, one usually wants to retrieve all paths between and of at most a certain length , namely, one usually considers the set of all paths from to in such that satisfies and . This naturally defines the following relation representing the problem of evaluating an RPQ over a graph database:
[TABLE]
Using this relation, fundamental problems for RPQs such as enumerating, counting, or uniform generating paths can be naturally represented. It is not difficult to show that EVAL-RPQ is in RelationNL, from which the following corollary can be obtained by using Theorem 3.2.
Corollary 4.3.
* can be enumerated with polynomial delay, admits an FPRAS, and admits a PPLVUG.*
It is important to mention that giving a polynomial delay enumeration algorithm for EVAL-RPQ is straightforward, but the existence of an FPRAS and a PPLVUG for EVAL-RPQ was not known before when queries are part of the input (that is, in combined complexity (Vardi, 1982)).
4.3. Binary decision diagrams
Binary decision diagrams are an abstract representation of Boolean functions which are widely used in computer science and have found many applications in areas like formal verification (Bryant, 1992). A binary decision diagram (BDD) is a directed acyclic graph where each vertex is labeled with a variable and has at most two edges going to children and . Intuitively, and represent the next vertices when takes values [math] and , respectively. contains only two terminal, or sink vertices, labeled by [math] or , and one initial vertex called . We assume that every path from to a terminal vertex does not repeat variables. Then given an assignment from the variables in to , we have that naturally defines a path from to a terminal vertex [math] or . In this way, defines a Boolean function that gives a value in to each assignment ; in particular, corresponds to the sink vertex reached by starting from and following the values in . For Ordered BDDs (OBDDs), we also have a linear order over the variables in such that, for every with a child of , it holds that . Notice that not necessarily all variables appear in a path from the initial vertex to a terminal vertex [math] or . Nevertheless, the promise in an OBDD is that variables will appear following the order .
An OBDD defines the set of assignments such that . Then can be considered as a succinct representation of the set , and one would like to enumerate, count and uniformly generate assignments given . This motivates the relation:
[TABLE]
Given in EVAL-OBDD, there is exactly one path in that witnesses . Therefore, one can easily show that EVAL-OBDD is in RelationUL. By Theorem 3.5, we obtain that:
Corollary 4.4.
* can be enumerated with constant delay, there exists a polynomial-time algorithm for , and there exists a polynomial-time randomized algorithm for .*
The above results are well known. Nevertheless, they show how easy and direct it is to use UL-transducers to realize the good algorithmic properties that a data structure like OBDD has.
Some non-deterministic variants of BDDs have been studied in the literature (Amarilli et al., 2018). In particular, an nOBDD extends an OBDD with vertices without variables (i.e. ) and without labels on its children. Thus, an nOBDD is non-deterministic in the sense that given an assignment , there can be several paths that bring from the initial vertex to a terminal vertex with labeled [math] or . Without lost of generality, nOBDDs are assumed to be consistent in the sense that, for each , all paths of in can reach [math] or , but not both.
As in the case of OBDDs, we can define a relation EVAL-nOBDD that pairs an nOBDD with an assignment that evaluate to (i.e. ). Contrary to OBDDs, an nOBDD looses the single witness property, and now an assignment can have several paths from the initial vertex to the terminal vertex. Thus, it is not clear whether EVAL-nOBDD is in RelationUL. Still one can easily show that , from which the following results follow.
Corollary 4.5.
* can be solved with polynomial delay, admits an FPRAS, and admits a PPLVUG.*
It is important to stress that the existence of an FPRAS and a PPLVUG for EVAL-nOBDD was not known before, and one can easily show this by using NL-transducers and then applying Theorem 3.2.
5. Completeness, Self-reducibility, and their Implications for the Class RelationUL
The goal of this section is to establish the good algorithmic properties of RelationUL, that is, to prove Theorem 3.5. To this end, we start by introducing a simple notion of reduction for the classes RelationNL and RelationUL, which will allow for much simpler proofs.
A natural question to ask is which notions of “completeness” and “reduction” are appropriate for our framework. Notions of reductions for relations have been proposed before, in particular in the context of search problems (Daskalakis et al., 2009). However, we do not intend to discuss them here; instead, we use an idea of completeness that is very restricted, but that turns out to be useful in this context.
Let be a complexity class of relations and , and recall that is defined as the set of solutions for input , that is, . We say is reducible to if there exists a function , computable in polynomial time, such that for every : . Also, if is reducible to for every , we say is complete for . Notice that this definition is very restricted, since the notion of reduction requires the set of solutions to be exactly the same for both relations (it is not sufficient that they have the same size, for example). The benefit of this kind of reduction is that it preserves all the properties of efficient enumeration, counting and uniform generation that we introduced in Sections 2 and 3, as stated in Proposition 5.1.
Proposition 5.1.
If a relation can be reduced to a relation , then:
- •
If can be solved with constant (resp. polynomial) delay, then can be solved with constant (resp. polynomial) delay.
- •
If there exists a polynomial-time algorithm (resp. an FPRAS) for , then there exists a polynomial-time algorithm (resp. an FPRAS) for .
- •
If there exists a polynomial-time randomized algorithm (resp. a PPLVUG) for , then there exists a polynomial-time randomized algorithm (resp. a PPLVUG) for .
Proof.
We go into some detail, but the idea of the proof is very simple. Because our notion of reduction is so strong, all efficient algorithms for apply immediately for , provided we add a preprocessing phase where we compute a function reducing from to . Since that takes only polynomial time, it preserves the overall complexity of all the types of algorithms we have discussed.
Now, with more detail and formality. Since can be reduced to , there exist a polynomial and a function such that for every input string , and can be computed in time . First, suppose can be solved with constant (resp. polynomial) delay, so there is an algorithm that enumerates with constant (resp. polynomial) delay and with precomputation phase of time for some polynomial . Now, consider the following procedure for on input . First, we compute in time . Then, we run , which enumerates all solutions in , that is, it enumerates all solutions in . So, the precomputation time of the procedure takes time , which is polynomial in . The enumeration phase is the same as for , so it has constant (resp. polynomial) delay. We conclude that can be solved with constant (resp. polynomial) delay.
Now, suppose there exists a polynomial-time algorithm for , let be the polynomial that characterizes its complexity, and consider the following procedure for on input . First, we construct in time . Next, we run , which computes , that is, it computes . So, the procedure calculates and takes time , which is polynomial in . We conclude that has a polynomial-time algorithm. The proof for the case of an FPRAS is completely analogous.
Finally, suppose there exists a polynomial-time randomized algorithm for , and let be the polynomial that characterizes its complexity. Now, consider the following procedure for on input . First, we construct in time . Next, we run , which outputs a solution from , that is, a solution from , uniformly at random. So, the procedure generates an element from uniformly at random and takes time , which is polynomial in . We conclude that has a polynomial-time randomized algorithm. The proof for the case of a PPLVUG is completely analogous. ∎
Therefore, by finding a complete relation for a class under the notion of reduction just defined, we can study the aforementioned problems for knowing that the obtained results will extend to every relation in the class . In what follows, we identify complete problems for the classes RelationNL and RelationUL, and use them first to establish the good algorithmic properties of RelationUL. Moreover, we prove that the identified problems are self-reducible (Jerrum et al., 1986), which will be useful for establishing some of the results of this section as well as for some of the results proved in Section 6 for the class RelationNL.
5.1. Complete problems for RelationNL and RelationUL
The notion of reduction just defined is useful for us because RelationNL and RelationUL admit natural complete problems under this notion. These complete relations are defined in terms of NFAs and we call them MEM-NFA and MEM-UFA. We already introduced MEM-NFA in Section 3, and we now define MEM-UFA as
[TABLE]
where an NFA is said to be unambiguous if there exists exactly one accepting run for every string accepted by it.
Recall from Section 3 that . Besides, it is easy to see that . To see why these relations are complete for our classes, consider the following. Take a relation in RelationNL (the case for RelationUL is the same). We know there is an NL-transducer that characterizes it. Run now on some given input . Since works in logarithmic space, there is only a polynomial number of different configurations that can ever be in (polynomial in ). Hence, we can consider the set of possible configurations as the states of an NFA , which then has only polynomial size. The transitions of are determined by the transitions between the configurations of . Moreover, a symbol output by the transducer is interpreted as a symbol read by the automaton . In this way, accepts exactly the language . We formalize this idea in the following result.
Proposition 5.2.
MEM-NFA* is complete for RelationNL and MEM-UFA is complete for RelationUL.*
We will prove the result only for the case of RelationUL and MEM-UFA, as the other case is completely analogous. The following lemma is the key ingredient in our argument. The proof of this lemma is given in Appendix A.1.
Lemma 5.3.
Let be a relation in RelationUL. Then there exists a polynomial-time algorithm that, given , produces an unambiguous NFA such that if and only if is accepted by .
Proof of Proposition 5.2.
Let be a relation in RelationUL and be a string in . We know by Lemma 5.3 that we can construct in polynomial time an unambiguous NFA such that if and only if is accepted by . Now, since is a -relation, there exists a polynomial such that for all . Thus, we have that all words accepted by have the same length . We conclude that . Since this works for every and every input , by definition of completeness we deduce that MEM-UFA is complete for RelationUL. ∎
In Section 3, we show that . Thus, a fundamental question is whether SAT-DNF is complete for the class RelationNL under the notion of reduction considered in this work. Notice that if this holds, then we will obtain that is SpanL-complete under the notion of polynomial-time parsimonious reduction (introduced in Section 3). However, is only known to be SpanL-complete under polynomial-time Turing reductions, and it is unknown whether is complete for SpanL under polynomial-time parsimonious reductions. In fact, it is not even known whether is SpanL-complete under some notion of reduction that preserves the existence of an FPRAS, so that the existence of an FPRAS for cannot be used to infer the existence of an FPRAS for #NFA. Hence, we leave as an open problem whether SAT-DNF is complete for RelationNL in the sense studied in this article.
5.2. MEM-NFA and MEM-UFA are self-reducible
Self-reducibility is a property of many natural relations, and it plays a key role in proving some important results, like the tight relationship between counting and uniform generation established in (Jerrum et al., 1986). There are different ways of formalizing this concept, and they can get rather technical, but the intuition is pretty straightforward. We say that a (decision) problem is self-reducible if it can be solved by referring to smaller instances of the same problem. For example, SAT is self-reducible. Given a propositional formula , consider its satisfiability problem. We can easily reduce that problem to smaller instances of SAT as follows. Take the first variable of and replace it by [math] to get a new formula . Do the same with to get a new formula . Notice that is satisfiable if and only if or is satisfiable. Moreover, both and have one less variable than , so they are smaller instances.
Now, self-reducibility does not imply the existence of a polynomial-time solution for a problem, as SAT well illustrates. It is true that the instances get smaller, until they eventually become trivially easy to solve. But the number of instances is multiplied, so recursively applying self-reducibility can lead to an exponential number of smaller instances to solve. Rather than a solution method, self-reducibility is thought of as a structural feature of a problem.
Now, definitions (and proofs) of self-reducibility can get very technical, partly because they have to formalize the notion of “smaller instance”. Hence, they crucially depend on the way that problems are encoded.444Thus, saying something like “SAT is self-reducible” is slightly inaccurate. We need to specify the way in which the problem, inputs and solutions are encoded, before we can assert something like that. We now state the main result of this subsection.
Proposition 5.4.
MEM-NFA* and MEM-UFA are self-reducible.*
To see the intuition behind this result, consider first a deterministic finite automaton (DFA) over the alphabet , and suppose it accepts a string , where . Then assuming that is the initial state of , we know that the accepting run for moves from to a state by reading symbol [math], and then it continues processing from . Now, if we change the initial state to to get a new DFA , then accepts the string . In other words, if is the language accepted by , then we have that:
[TABLE]
where DFA is defined in the same way as . Besides, notice that if the length of the strings to be accepted by is given as a parameter, as in the case of MEM-NFA, then we can assume does not contain any cycles, and each automaton () can be made smaller than by removing and updating the transition function of accordingly. Hence, the above equality shows that the language accepted by can be defined in terms of the languages accepted by smaller deterministic finite automata. The same idea can be applied to an NFA , although constructing each NFA () is a little more complicated as there can be several transitions from a state that read the same symbol. Intuitively, this shows that MEM-NFA is self-reducible. The precise definition of self-reducibility (with all its technicalities) and the complete proof of Proposition 5.4 can be found in Appendix A.2.
5.3. Establishing the good algorithmic properties of RelationUL
Theorem 3.5 is a consequence of Propositions 5.1 and 5.2, and the following result.
Proposition 5.5.
* can be solved with constant delay, there exists a polynomial-time algorithm for , and there exists a polynomial-time randomized algorithm for .*
To sum up all the results just mentioned: MEM-UFA is complete for RelationUL, it has good algorithmic properties, and our notion of reduction (and completeness) preserves all the algorithmic properties we have discussed. In what follows, we prove each of the three results stated in Proposition 5.5.
5.3.1. can be solved with constant delay
We now provide a sketch of the constant delay algorithm. The idea is conceptually simple. Remember what we want: to output all strings of a certain length accepted by an unambiguous NFA, without repetition. We may use a preprocessing phase of polynomial time, but afterwards, there can be at most linear time between one string and the next.
Now, let be the input, and consider Figure 1 with as an example. To do constant delay enumeration, we do a depth-first traversal of the NFA, starting from the initial state. As we traverse the NFA, we read the symbols from the transitions, and store them in a partial string. When the partial string reaches length , if we happen to be in a final state, we output the string.
Basically, that is all you have to do, but there are a few technicalities remaining. First of all, we mentioned depth-first traversal even though we are not analyzing a graph, but an NFA. The clarification is simple. We will use the preprocessing phase to get a labeled directed acyclic graph (DAG) from and , and do the depth-first traversal on . The DAG is obtained by first unrolling in the following way:
- (1)
Cluster all final states of into a single final state. This is easy to do: create a new state , make it the unique final state, and create an -transition from all previous final states to the new one. 2. (2)
Remove all -transitions (this is a standard procedure for an NFA). 3. (3)
Unroll the NFA times. That is, for each state create copies , and for each transition in , create the transitions in the unrolled automaton. Keep a unique initial state and a unique final state . 4. (4)
Remove all nodes that are not a part of an accepting run from the initial to the final state.
See Figure 2 for an example of this kind of transformation. It is easy to see that this can be done in polynomial time and that it produces a new NFA (alternatively, a labeled DAG) that is still unambiguous and accepts the same words of length as . Since there are no -transitions, each string of length accepted by can be interpreted as a path of length in from the initial to the final state, and vice versa. Thus, a depth-first traversal of will go through all words of length accepted by , and no more.
The second technicality concerns the following question: is the enumeration truly repetition-free? It is, for the following reason. Each path of length from the initial state to the final state is only traversed once (by definition of a depth-first traversal of a graph). Moreover, each one of those paths corresponds to a different string since and are both unambiguous automata.
Finally, does the enumeration phase really have constant delay? That is, does it take time between one solution and the next? The answer is yes. Notice that it takes time to traverse from the initial state to the final state, and from one final state visit to the next, because the traversal is depth-first. Also, recall that we removed (in step (4) above) all nodes that were not part of an accepting run from the initial to the final state. Thus, there is no time wasted: each traversal to the final state produces a new string that we can output.
5.3.2. There exists a polynomial-time algorithm for
Consider the graph as defined in Section 5.3.1. As we already pointed out, the number of paths of length from the initial to the final state is exactly what we want to compute: the number of strings of length accepted by (the unambiguity assumption is crucial here). Since is a DAG, we know the number of paths between two given nodes can be computed exactly in polynomial time by dynamic programming. Hence, there exists a polynomial-time algorithm for .
5.3.3. There exists a polynomial-time randomized algorithm for
Consider an input for the problem . Moreover, as for the case of , consider the graph defined in Section 5.3.1, which can also be seen as an automaton. Assume that is the initial state of , and that , , is the set of states in reachable from by following an edge with label 0. Let be the number of strings of length that start with the symbol 0 and are accepted by . Then we can compute in polynomial time by using the counting algorithm mentioned in the previous section, starting from each one of the states in , , . Notice that this algorithm works properly as is an unambiguous NFA. In the same way, we can compute in polynomial time the number of strings of length that start with the symbol 1 and are accepted by . Given and , the first symbol of the string to be generated is chosen according to the probabilities:
[TABLE]
Then the algorithm continues in the same way choosing , , . It is easy to prove that this algorithm generates uniformly, at random, a string accepted by of length .
Notice that the previous idea is essentially the same as the one in (Jerrum et al., 1986), that is, we use the fact that the relation MEM-UFA is self-reducible and its counting problem can be solved efficiently. However, a clarifying note should be included here. We claim a polynomial-time randomized algorithm for , while an almost-uniform generator is claimed in (Jerrum et al., 1986). Our result is stronger for two reasons. First of all, we have a stronger counting result (exact polynomial-time algorithm instead of an FPRAS) to use as the basis of our uniform generation algorithm. Second, the computational model considered in (Jerrum et al., 1986) (the Probabilistic Turing Machine) is a bit different from the one considered in this work. It cannot, for example, simulate a Bernoulli experiment with a success probability of exactly . Essentially, it makes it impossible to get an exact uniform generation algorithm. We are less strict with our computational model, so we are able to get a polynomial-time randomized algorithm for .
6. #NFA Admits a Fully Polynomial-Time Randomized Approximation Scheme, and its Implications to the Class RelationNL
The goal of this section is to provide a proof of Theorem 3.2, which considers the class RelationNL defined in terms of NL-transducers. Given that we showed in Proposition 5.2 that MEM-NFA is complete for RelationNL, we have by Propositions 5.1 that Theorem 3.2 is a consequence of the following result.
Theorem 6.1.
* can be solved with polynomial delay, admits an FPRAS, and admits a PPLVUG.*
The existence problem for MEM-NFA has as input an NFA and a value given in unary (as the string ), and the question to answer is whether (that is, whether there are any solutions for according to the relation MEM-NFA). It is easy to prove such a task can be solved in polynomial time, as the nonemptiness problem for NFA can be solved in polynomial time. Moreover, we proved in Section 5.2 that MEM-NFA is a self-reducible relation. With all that, a polynomial delay algorithm for can be derived from the folklore result that such an enumeration algorithm exists for a self-reducible relation, if the associated existence problem for this relation can be solved in polynomial time (a precise statement of this result can be found in Lemma 4.10 in (Schmidt, 2009)). In this section, we focus on the remaining part of the proof of Theorem 6.1. More specifically, we provide an algorithm that approximately counts the number of words of a given length accepted by an NFA, where this length is given in unary. This constitutes an FPRAS for , as formally stated in the following theorem:
Theorem 6.2.
#NFA* (and, thus, ) admits a fully polynomial-time randomized approximation scheme.*
The algorithm mentioned in this theorem works by simultaneously counting and doing uniform generation of solutions. Then its existence not only gives us an FPRAS for , but also a PPLVUG for , as formally stated in the following theorem:
Theorem 6.3.
* admits a preprocessing polynomial-time Las Vegas uniform generator.*
In the rest of this section, we prove Theorems 6.2 and 6.3. More specifically, we start by providing in Section 6.1 an overview of the algorithmic techniques used in the proof of Theorem 6.2. Then we present in Section 6.2 the template for the FPRAS for #NFA, whose main components are given in Sections 6.3 and 6.4. A complete version of the FPRAS for #NFA is finally given in Section 6.6, where its correctness and polynomial-time complexity are established. Moreover, the proof of Theorem 6.3 is also given in Section 6.6.
6.1. An overview of the algorithmic techniques
We start by providing a high-level overview of our FPRAS for the #NFA problem. To this end, we first set the necessary terminology to refer to this counting problem.
A non-deterministic finite automaton (NFA) over the alphabet is given as a tuple , where is a finite set of states, is the transition relation, is a set of initial states and is a set of final states. The language of the strings in that are accepted by is denoted by . Moreover, given a natural number , the language is defined as . With this terminology we define the counting problem #NFA as follows. The input of #NFA is an NFA with states over the alphabet and a natural number , and the task is to return . Here, is given in unary (that is, is given as the string ) 555As mentioned before, it is known that #NFA belongs to #P. Notice that the fact that is given in unary is necessary to show this property. If is given as a binary number, then the value can be double exponential in the size of this input, since can be equal to . Hence, #NFA cannot be in #P if the input is given as a binary number, as if a function is in #P, then there exists a polynomial such that for every , it holds that ..
To illustrate the difficulty of #NFA, we first consider the simpler problem of counting the number of strings of length contained in the language accepted by a deterministic finite automaton (DFA) . Note that if , there is exactly one accepting path in the DFA for . So to count , one can simply compute the total number of paths of length in the DFA, which can be done in polynomial time by a dynamic program. However, if is instead the language accepted by an NFA, then can have exponentially many accepting paths, and so counting paths does not lead to a good estimate of for an NFA.
One natural approach to overcome the aforementioned issue is to design an algorithm to estimate the ambiguity of the NFA. For instance, the following procedure produces an unbiased estimator of . First, sample a random path of length in the NFA, and let be the string accepted on that path. Second, count the number of accepting paths that has in the NFA, and also count the total number of paths of length in the NFA. Repeat this process times, and report the average value of . The resulting estimator is indeed unbiased. However, the number of paths can differ by an exponential factor for different strings , thus the variance of this estimator is exponential. Therefore, this algorithm requires exponentially many samples to obtain a good estimate. Several other similar estimators exist (see e.g. (Kannan et al., 1995)), which all unfortunately do not lead to polynomial time algorithms for the general #NFA problem.
The basic approach of our FPRAS is to incrementally estimate, for each state in the NFA, the number of distinct strings for which there is a path of length from the starting states to labeled by . Call this set of strings . Our high level approach is similar to dynamic programming. Namely, to estimate , we first estimate for each state such that there is a transition from in the NFA, where . However, one cannot simply declare
[TABLE]
because a single string can be in many of the sets , which would result in over-counting. Therefore, we must also estimate the intersections of the sets . This is challenging, as these sets themselves can be exponentially large, so we cannot afford to write them down. Moreover, there are possible sets which can arise as the intersection of sets of the form for , thus we cannot store an estimate of each. The main insight of our FPRAS is to sketch the intermediate states of the dynamic program, by replacing the set with a small (polynomial-sized) uniformly sampled set . Here, the sketch acts as a compact representation of the (possibly) larger set . For instance, to see how such a sketch could be useful, if there were exactly two preceding states and , to estimate the relative size of the intersection , it will suffice to use the approximation . Notice that the quantity can be computed in time polynomial in , by checking for each if is contained in , which can be accomplished in polynomial time by a membership query for NFAs. If are our estimates of , then we can therefore obtain an estimate of by , avoiding the issue of overcounting the intersection.
The main technical hurdle that remains is to determine how to uniformly sample a string from a set to construct our sketches . This is accomplished by sampling the string bit by bit. We first partition into the set of strings with last bit equal to [math] and . We then estimate the size of both partitions, and choose a partition with probability proportional to its size. Finally, we store the bit corresponding to the sampled partition, append it to a suffix of the string and then recurse onto the next bit. In essence, we sample a string by growing a suffix of .
To estimate the size of the partitions, we use our sketches of for all and states . Unfortunately, because of the error in estimating the sets , there will be some error in the distribution of our sampler. To correct this, and avoid and exponential propagation of this error, we use a rejection sampling technique of Jerrum, Valiant, and Vazirani (Jerrum et al., 1986), which normalizes the distribution and results in a perfectly uniform sample. This allows for our construction of the sketches , and also gives an algorithm for the uniform generation of strings of length from an NFA.
6.2. The algorithm template
The input of #NFA is an NFA with states, a string that represents a natural number given in unary, and an error . The problem then is to return a value such that is a -approximation of , that is,
[TABLE]
Besides, such an approximation should be returned in time polynomial in , and .
Our algorithm for approximating first involves the construction of a labelled directed acyclic graph from the NFA . We call this graph , as it is obtained by unrolling times the NFA . Specifically, for every state create copies of , and include them as vertices of . Moreover, for every transition in , create the edge in , for every . We refer to the set as the -th layer of . Furthermore, for every set , we denote by the copy of in the -th layer of . This means that refers to the initial states of at the first layer, and refers to the final states of at the last layer. For the sake of presentation, we will use the terms vertex and state interchangeably to refer to the vertices of . Moreover, from now on we assume that is pruned, that is, for every and every , there exists a path from some vertex of to . In other words, all states in are connected to some initial state. The pruning of can be done in a pre-processing step in polynomial-time in , without changing the overall time of the algorithm. We remark that in the remainder of the section, whenever we state that we run a procedure for with and some layer, it is implicitly assumed that all pruned states have already been removed. Thus, for the remainder, we will not consider the pruned states at any point, since they cannot be used to derive words of length in the language.
Given a state and a layer , we define as the set of all strings such that there exists a path labeled with from some vertex in to . Notice that for every , and also that since is pruned. We extend this notation to every set , namely, . The sets of strings will be crucial for our algorithm. Indeed, finding an approximation for is reduced to finding an estimate for , where represents the set of final states of at the last layer.
The components of our approximation algorithm are as follows. Fix the value and assume that and (if or , then the problem can be easily solved in polynomial time). Then for each layer and each state with in , store a number and a set such that:
- •
is a -approximation of , and
- •
is a uniform sample from of size .
For the first requirement, we mean that
[TABLE]
In particular, if , we should have that . For the last requirement, we mean that each is a uniform and independent sample from . Given this condition on the samples, it is possible that we will obtain duplicates of a given . Besides, if , then we know that has to contain duplicate elements. Therefore, we allow to be a multiset (meaning that the strings in are not necessarily distinct). The number and the set can be understood as a “sketch” of that will be used to compute other estimates for .
The algorithm proceeds like a dynamic programming algorithm, computing and for every state in in a breadth-first search ordering. We first compute for all states at layer [math]. Then, given , we compute for each vertex . So the value and the set are computed layer by layer. The final estimate for is . We summarize this algorithmic template in Algorithm 6.2.
For the rest of this section, we show how to instantiate the template of our algorithm. For a layer , we show in Section 6.3 how to compute the estimate given estimates and sets for all . In fact, for this we need to assume a strong condition (introduced in the next section), which states that the samples in our set satisfy good concentration properties. Next, given and the prior estimates and sets , we demonstrate in Section 6.4 how to generate a uniform sample from the set , proving how to compute . In particular, again, we will show that the strong condition used as an induction hypothesis holds for the sets with exponentially large probability over (Section 6.5). In the last section we put all pieces together and show the correctness of the algorithm.
6.3. Computing an estimate for a set of vertices
Recall that the input of the problem is an NFA with states and a string , and that we assume that and .
Then fix a layer , and define a sketch data structure such that . Moreover, assume that has already been computed. In particular, is a -approximation of , and is a uniform sample from of size for each . The goal of this section is twofold; we first show how to compute an estimate of for every , which is denoted by , and then we show how to compute an estimate for . These values will play a crucial role for computing not only , but also the set of uniform samples and the final estimate for (see Sections 6.4 and 6.5).
Let be a non-empty subset of , and suppose that we want to find an estimate for . If is deterministic, then the sets are disjoint, and then we can easily compute the size of as . Unfortunately, given that can be non-deterministic, this sum will over-approximate the size of , and we need to find a way to deal with the intersections of the sets . For this, fix a total order over the set , and consider the following way to compute :
[TABLE]
With the ratio , we removed from its intersection with all sets such that . In fact, one can easily check that and, thus, equation ( ‣ 6.3) trivially holds. We call the above ratio the intersection rate of in given (or just the intersection rate of ).
Inspired by equation ( ‣ 6.3), we can estimate by using to estimate and to estimate the intersection rate of . More precisely, we define the estimate for as follows:
[TABLE]
It is important to note that can be computed in polynomial time in the size of . Indeed, the set can be computed by iterating over each string and checking whether and . Given that verifying if a string is in and can be done in polynomial time, computing takes polynomial time as well. We call the ratio the estimate of the intersection rate of .
To show that is a good estimate for , we need that the estimate of the intersection rate is a good approximation of the real intersection rate in each layer. By a good approximation, we mean that the following condition holds at level :
[TABLE]
This condition is crucial for the next results and most of our analysis in this and next section will assume that this condition holds. Towards the end, in Section 6.6 we will show that, by Hoeffding’s inequality, the condition holds for all layers with exponentially high probability over . Next, we prove that, if condition holds, then is a good estimate for .
Proposition 6.4.
Assume that holds and is a -approximation of for every . Then is a -approximation of for every .
Proof.
Given that condition holds, we know that for each :
[TABLE]
Moreover, given that is a -approximation of , it holds that:
[TABLE]
Putting these two bounds together, we obtain the following bounds from the definition of in equation ( ‣ 6.3):
[TABLE]
Recall from the discussion of the intersection rate that . Moreover, given that and , we have that . Replacing both statements in the previous inequality, we obtain
[TABLE]
which is equivalent to
[TABLE]
This concludes the proof of the proposition. ∎
With the estimates of for every at the -th layer, we are ready to give a good estimate for the size of a single vertex in the next layer . Let be an arbitrary vertex at layer . For , define the set of vertices is an edge in , namely, the set of all vertices in the -th layer from which can be reached by reading symbol . Notice that sets and partition in the following sense:
[TABLE]
where given two sets of strings, is defined as the set consisting of the concatenation of each string of with each string of (in particular, with for ). Equation (1) implies that . Notice that, if we assume holds, then by Proposition 6.4 we have that is a -approximation of for , from which we obtain that is a -approximation of . Therefore, we can derive an estimate for by using previous estimates .
Note that the computation of is deterministic, by assuming that holds for all . Specifically, the estimates are exact for the initial layer. Next, for each layer we assume that holds and we can compute by using (in fact, by using ). Then, we asumme that holds and so on. Therefore, by filling the sets with uniform samples and assuming that holds for all we can compute each estimate . Moreover, we can guarantee that it is a -approximation of . We summarize this fact in the following proposition.
Proposition 6.5.
Assume that holds for all . Then is a -approximation of for every .
After all, at some point we will reach the last layer and we would like to compute the -approximation for . For this, we can use for estimating , which reach the ultimate goal of our algorithm.
Proposition 6.6.
If holds for all , then is a -approximation for .
Proof.
Assume that is a -approximation of , that is,
[TABLE]
But we have that:
[TABLE]
and we also have that:
[TABLE]
Thus, we conclude that:
[TABLE]
∎
In the following section, we show how to compute the set using , namely, how to generate a uniform sample from . Specifically, we show that assuming holds for all , we can obtain uniform samples from the sets such that property will hold with high probability.
6.4. Uniform sampling from a vertex
To carry out our main approximation algorithm, we must implement the algorithm template given in Algorithm 6.2, whose input is assumed to be an NFA with states and a string , where and . In the previous section, we implemented Step 2 (a) of this algorithm and, thus, the goal of this section is to implement the sampling subroutine in Step 2 (b). This procedure is based on a sample technique proposed in (Jerrum et al., 1986), but modified to suit our setting.
Take a state and layer , and assume that for all layers the condition holds. Notice that by Proposition 6.5, once we have and estimates for all levels , we immediately get the estimates for the level as well.
The procedure to sample a uniform element of the set is as follows. We initialize a string to be the empty string. Then we construct a sequence of strings , , , , , where each element is of the form with , and we define the result of the sample procedure to be . In other words, we sample a string of by building a suffix of the sample, bit by bit. To ensure that is an element of chosen uniformly, we also consider a sequence of sets , , , , constructed as follows. The first set is . Then we consider the set of vertices at layer that can reach the set by reading letter , namely, for define:
[TABLE]
Notice that, although we use the superscript , the set is a subset of vertices in the -th layer. Similar to the previous section, the sets and induce a partition of the set in the following sense:
[TABLE]
Therefore, our sampling algorithm estimates the size of for , and chooses one of , with probability proportional to its size, namely, and . Say we choose . Then we define , append the bit as a prefix of to obtain , define as , and continue with the recursion on and . Hence, we have that is the set of vertices such that there exists a path labeled by that connects some state of with . Notice that for every layer (and, thus, ). Indeed, given that is pruned, we know that . By induction, if for some level we have that (similar when ), then and the next level will be chosen with probability . In particular, .
Since there could be an error in estimating the sizes of the partitions, it may be the case that some items were chosen with slightly larger probability than others. To remedy this and obtain a perfectly uniform sampler, at every step of the algorithm we store the probability with which we chose a partition. Thus at the end, we have computed exactly the probability with which we sampled the string . We can then reject this sample with probability proportional to , which gives a perfect sampler. As long as no string is too much more likely than another to be sampled, the probability of rejection will be a constant, and we can simply run our sampler -times to get a sample with probability for every .
This procedure then is given in Algorithm 6.4. We call it with the initial parameters Sample, where is the empty string, corresponding to the goal of sampling a uniform element of . Here, is a value that we will later choose. Notice that at every step of Algorithm 6.4, we have that is precisely the number of strings in which have the suffix , as is the set of strings such that . Observe then that the set depends on the random string , so in fact we could write instead of . For notational simplicity we omit the subscript, and it is then understood that is a function of .
To get some intuition of Algorithm 6.4, assume for the moment that we can compute each exactly, namely, . Now the probability of choosing a given element can be computed as follows. Ignoring for a moment the possibility of returning fail, we have that is the string returned by Sample. Thus, the probability we choose is:
[TABLE]
Now at the point of return, we also have that . Thus, if , then the probability that is output is simply . The following is then easily seen:
Fact 1.
Assume that each probability in Algorithm 6.4 satisfies that
[TABLE]
If and is the output of Algorithm 6.4, then for every , it holds that . Moreover, the algorithm outputs with probability .
This shows that, conditioned on not failing, the above is a uniform sampler. Repeating the procedure times, we get a sample with probability since:
[TABLE]
However, Fact 1 was obtained under the strong assumption that each probability can be computed exactly. Hence, in what follows we focus on showing that with high probability the same result holds if we approximate with (instead of assuming that ).
Proposition 6.7.
Assume that condition holds for every layer . If is the output of Sample, then for every :
[TABLE]
Moreover, the algorithm outputs fail with probability at most . Thus, conditioned on not failing, Sample returns a uniform element .
Proof.
First, we show that every recursive call to Sample satisfies that . Since and with each call does not decrease (because it is divided by a probability), we know that at each subsequent call. It remains to show that for every recursive call to the Sample procedure. Since does not decrease after each recursive call, it suffices to show this for the final value of . Notice that at the call , with from to , we have that is divided by a factor
[TABLE]
where is the -th letter of . So in the final call, has the value:
[TABLE]
Given that condition holds for every layer , by Proposition 6.4 we know that is a -approximation of for all and (recall that is a subset of states at layer ). It follows that at the final recursive call to Sample, we have that:
[TABLE]
Recall that for every . Also note that , since is the set of strings such that , and is the subset of with the last bit equal to (of which there is just one). Thus, given that , we have that:
[TABLE]
and so
[TABLE]
Furthermore, is a -approximation of by Proposition 6.5. Then we know that and, therefore,
[TABLE]
where the last inequality holds because , and for every . Hence, we know that under the assumptions stated for this proposition, on each call and, in particular, on the last call, we have that .
As a second step in the proof of the proposition, we show that the algorithm outputs fail with probability at most . Notice that this probability is only due to Step (1) in Algorithm 6.4. That is, the probability we output fail is at most , where is as computed in the previous part of the proof. Thus, to show that the failure probability is at most , we compute a lower bound for in a similar way as we computed an upper bound for it:
[TABLE]
Note that in the fourth step we use the fact that is a -approximation of by Proposition 6.5 (indeed, implied by the assumption that holds for each ), so that .
As the final step of the proof, we need to show that if the output of the algorithm is , then for every . Now, the probability of being a particular is given by the following expression:
[TABLE]
as desired. This concludes the proof of the proposition. ∎
We would like to remark that, in order for Proposition 6.7 to be correct, we need that holds for every layer . Indeed, the sampling procedure uses values for approximating the real probabilities . For this, we need that each value is a good estimate for and this is implied by Proposition 6.5 if holds for every layer . In the next section, we prove that indeed this happen with exponentially high probability.
6.5. Bounding the probability of breaking the main assumption
As it was previously discussed, the computation of the sketch composed by the estimates and sets is subject that conditions hold for all layers . Therefore, this section is aimed to bound the probability that is false for some layer and show that, indeed, this probability is exponentially low.
First, assume that we are back to a layer , condition holds for all layers , and we want to check the probability that holds for . In other words, we want to bound , for which we need Hoeffding’s inequality.
Proposition 6.8 (Hoeffding’s inequality (Hoeffding, 1963)).
*Let be independent random variables bounded by the interval such that . Then for every , it holds that *
[TABLE]
For the first layer , the condition certainly holds. Now if we are at any layer and holds, then we know by Proposition 6.7 that for each , it is possible to fill with uniform samples of . Consider the case of any subset , and let be the uniform sample of of size . For each , consider the random variable such that if , and [math] otherwise. Then we have that:
[TABLE]
Therefore, by Hoeffding’s inequality we infer that:
[TABLE]
Note that in the previous inequality the condition does not change the assumptions of Hoeffding’s inequality. We can bound by taking the union bound over all states and all possible subsets :
[TABLE]
We conclude that, at layer , the probability .
To extend these chain of implications over all layers, we can use that:
[TABLE]
Moreover, we have that:
[TABLE]
We conclude by stating the main purpose of this section in the following proposition.
Proposition 6.9.
The probability that holds for all layers is bounded below by .
6.6. The main algorithm, its correctness and its complexity
In Algorithm 6.6, we give all the steps of the FPRAS that has been discussed in the previous subsections. This algorithm follows the same structure of Algorithm 6.2, but now the computation of and is fully described. The algorithm proceeds as mentioned before, layer by layer, computing the estimates and the sample sets . For each state at the initial layer , the pair , is computed without considering any additional information (Step (4) of Algorithm 6.6). Then for each layer and each vertex , we compute , as was discussed in Section 6.3. After is computed, the set is filled with uniform and independent samples. For this, Sample is run at most times or until a string is output. If , then the algorithm terminates and outputs [math]. Otherwise, the sample is added to , and the computation continues. Finally, the algorithm computes and returns as it was discussed in Section 6.3.
Given that the value is polynomial in , and , it is clear that Algorithm 6.6 works in time polynomial in , and . Hence, it only remains to show that Algorithm 6.6 is correct, namely, that is a -approximation of with probability greater than . First, assume that the algorithm returns a good estimate, that is, condition holds for all layers during the run of the algorithm and is never equal to fail after Step (i) of the algorithm. Then by Proposition 6.6, we obtain that is a -approximation of as desired.
To conclude the proof of the correctness of Algorithm 6.6, we need to bound the probability that the algorithm does not give a good estimate, namely, that either condition is false for some layer or the sampling algorithm fails times at Step (i), where is the number of repetitions performed in this step. Therefore, it remains to show that this probability of giving a wrong output is at most considering a value for . Let be the event that the call Sample fails consecutive times at layer , state , and the -th sample of , where . We know by Proposition 6.7 that the probability that Sample fails is at most and, therefore, . Furthermore, by Proposition 6.9 we already know that . By the union bound, we conclude that the probability that the algorithm gives the wrong output is:
[TABLE]
Finally, if we take
[TABLE]
we obtain that . Therefore, we have that the probability that the algorithm returns a wrong estimate is at most . As is , this concludes the proof of the correctness of Algorithm 6.6 as stated in Theorem 6.2.
To finish this section, we need to prove Theorem 6.3, that is, we need to show that admits a preprocessing polynomial-time Las Vegas uniform generator (PPLVUG). Notice that our algorithm results in a sampler satisfying the conditions of Theorem 6.3.
Specifically, given an NFA and a natural number , Algorithm 6.6 builds a structure . Furthermore, conditioned on all the above events (i.e., , , , and ) we can use for getting a uniform sample from by using Algorithm 6.4, and this algorithm fails with probability . In other words, Algorithm 6.6 and Algorithm 6.4 are the randomized algorithms and , respectively, and is the string that is good-for-generation (i.e., the advice) from the PPLVUG’s definition. The probability of success of the above events can be amplified to for any by scaling up by a factor of . The overall runtime is now polynomial in as needed. Note that for the purposes of a uniform sampler, the parameter can be set to a constant, as it does not appear in the definition of the sampler in Theorem 6.3. Now by Proposition 6.7, we can obtain truly uniform samples from the set in time polynomial in time. The probability that fail is returned by the sampler is at most , which can be amplified to at most by repeating it a constant number of times to satisfy the condition required in Theorem 6.3, which completes the proof of this theorem.
7. Concluding Remarks
We consider this work as a first step towards the definition of classes of problems with good properties in terms of enumeration, counting and uniform generation of solutions. In this sense, there is plenty of room for extensions and improvements, and many problems need to be studied further. First, for each one of the classes RelationNL and RelationUL, we have identified a single problem that is complete for it. An important question then is whether such classes admit other natural and well-studied complete problems; for instance, we leave as an open problem whether SAT-DNF is complete for RelationNL under the notion of reduction introduced in Section 5. Second, the different components of the FPRAS for #NFA were designed to facilitate its proof of correctness. As such, we already know of some optimizations that significantly reduce its runtime, and we plan to develop more such optimizations so to make this FPRAS usable in practice. Finally, an interesting area to explore is to extend the results of this article to the class of context-free languages. In particular, it will be interesting to understand if relations based on context-free languages have good properties regarding enumeration, counting, and uniform generation. Here, it is natural to ask whether the problem (i.e., to count the number of words of a given length accepted by a context-free grammar) admits an FPRAS or not.
Appendix A Proofs of Intermediate Results
A.1. Proof of Lemma 5.3
Let be any element in . Since is in RelationUL, we know there exists a UL-transducer such that . Without loss of generality, we can assume that has only one accepting state, so it can be written as a tuple . If it has more than one accepting state, say a set of accepting states, we can define a new transducer that is identical to with one difference. It has only one final state and whenever it reaches a state in , it makes one last transition to and stops. It is clear that so we do not lose any generality with this assumption.
Let , be the function that bounds the number of cells in the work tape that can be used, and assume that is . Consider now an execution of on input . Since the input tape never changes (its content is always ), we can completely characterize the configuration of the machine at any given moment as a tuple where
- •
stores the state the machine is in.
- •
indicates the position of the head on the input tape.
- •
indicates the position of the head on the work tape.
- •
stores the contents of the work tape.
With the previous notation, the initial configuration of on input is represented by , that is, is in its initial state, the heads are at the first position of their respective tapes, and the work tape is empty (that is, it only contains the blank symbol B). The accepting configuration is represented by a tuple of the form . Notice that without loss of generality, we can assume the accepting configuration to be unique, by changing so that it runs for a little longer in order to reach it. If is the set of possible configuration tuples then we have that
[TABLE]
which is polynomial in . Recall the notation for NFAs introduced in Section 6.1. We now define the NFA where , and are defined as above and the transition relation is constructed in the following way:
- •
Let . Consider any possible run of on input . Suppose there is a valid transition, during that run, that goes from to while outputting symbol . Then, is in .
- •
Let . Consider any possible run of on input . Suppose there is a valid transition, during that run, that goes from to while making no output. Then, is in .
We already showed that has polynomial size in , and it clearly can be constructed explicitly in polynomial time. The same is true for . Given a pair of configurations , it can be checked in polynomial time whether there is a possible transition from to during an execution of on input (it suffices to check , the transition relation for ). And there are just such pairs of configurations that we need to check, so the whole construction of can be done in polynomial time. It only rests to show that and that is unambiguous.
Let . That means there is an accepting run of on input that yields as output. Equivalently, there is a sequence of configurations and a sequence of symbols such that:
- •
.
- •
.
- •
For each , the transition from to is valid on input given the transition relation of .
- •
For each , we have that is equal to the symbol output when going from configuration to if a symbol was output. Otherwise, .
- •
.
By definition, this means that is accepted by . That is, and so we can conclude that . Since all the previous implications are clearly equivalences, we can also conclude that . Hence as needed. What the previous argument is saying is that every accepting run of that outputs a string has a unique corresponding accepting run of on input . That implies that is unambiguous. Otherwise, there would be some such that two different runs of accept . But that would mean that there are two different runs of on input that output , which cannot occur, since is a UL-transducer.
Finally, notice that is actually not an NFA (under the definition given in Section 3), since we explicitly allowed for the possibility of -transitions. But recall that the -transitions of any NFA can be removed in polynomial time without changing the accepted language, which is a standard result from automata theory. This concludes the proof of the lemma.
A.2. Proof of Proposition 5.4
We focus on the case of MEM-NFA (it extends easily to MEM-UFA). To show this result, we need to include a little more detail in our definition of MEM-NFA, to consider some corner cases. First of all, we have to consider the cases where the string in unary is empty. That is, the case where in input . This just amounts to the following: if the starting state is a final state, we consider that the automaton does accept the empty string. So, if , and is an NFA that has all the properties stated in the definition of MEM-NFA, plus its starting state is the accepting state, then . Also, we need to consider the cases where does not have all the properties stated in the definition of MEM-NFA. In those cases, we consider that , for any , does not have any solutions. Also, and this gets more technical, we consider that any input that has an invalid encoding does not have any solutions either. We will not be completely precise about which encoding should be used (although during the proof we will mention some important points regarding that). But we will ask that the correctness of the encoding can be checked in polynomial time (this is a mild requirement as any reasonable encoding will allow for it). And it is important to have in mind that for some technical concepts like self-reducibility, the encoding of the problem is critical.
We use the notion of self-reducibility stated in (Schmidt, 2009), adapted to our situation, since (Schmidt, 2009) uses a slightly different framework to define an enumeration problem. We say a relation is self reducible if there exist polynomial-time computable functions , and such that for every :
- (1)
if , then , 2. (2)
if , it can be tested in polynomial time in , whether the empty string is a solution for . 3. (3)
, 4. (4)
if and only if , 5. (5)
, 6. (6)
, and 7. (7)
.
The last condition intuitively says that all solutions for a given input can be constructed from the solutions of (a polynomial number of) smaller instances. It can be equivalently stated in the following way, which is how we will use it:
- (8)
if , it holds that .
As we already stated, the empty string is a solution only when the input is correctly encoded and the initial and final states of the automaton coincide. So condition (2) from above is satisfied regardless of our definition of . We will focus from now on on the other six conditions. Let \mathcal{N}=\{N\>|\>N\text{ is an NFA with a unique final state and no \varepsilon-transitions}\}. Following the previous notation, we define the functions , and that characterize self-reducibility. The only interesting cases, of course, are those where the automaton in the input is in (and the input is correctly encoded). In all the others, the input is not correct, so the set of solutions is empty, and we do not need to worry about self-reducibility. That said, we define
[TABLE]
Both functions are clearly computable in polynomial time. The definition of is just saying that on input , any solution will have length , which comes directly from the definition of MEM-NFA. The definition of indicates that, for any input, as long as its solutions have positive length, we can create another input that has the same solutions, but with the first character removed. Notice that with these definitions, conditions (3) and (4) for self-reducibility are trivially met. Condition (1) is also met, which is easy to see from the definitions of MEM-NFA and . The only task left is to define and prove conditions (5), (6) and (8). We now proceed in that direction.
Let be an automaton in . Notice we are making the assumption that has a unique final state, since it makes the idea clearer and the proof only has to be modified slightly for the general case. We will mention some points about the exact encoding soon (which is key for condition (5) to hold). But first, consider an input which is incorrectly encoded or where is not in . Then, it has no solutions and it is enough to set for all (which is clearly computable in polynomial time). In that case, notice that condition (5) is trivially true. Also, notice that since is not in (or is encoded in an incorrect format), we have , so for any it holds that
[TABLE]
so condition (6) is also true. And given that , condition (8) amounts to checking that for every , it holds that if and only if , which is obviously true. Now, consider the case of an input that is correctly encoded and where is in . There are two main cases to consider.
First, the case where . This case is also simple, because we can set for all (which is computable in polynomial time and implies that condition (5) is trivially true), and since , it is possible to prove as before that conditions (6) and (8) hold. Second, we need to consider the case where . Then we have , so only needs to be defined when is a single symbol. Then, for both , we set , where is defined as follows. Let be the set
[TABLE]
Thus, is the set of states that can be reached (with one transition) from the initial state, by reading the symbol . Now, we define where is a new state not contained in , and:
[TABLE]
Notice that this construction takes only polynomial time. What we are doing, basically, is the following. Imagine as a first “layer” of states reachable from in one step. We want to merge all of in a single new initial state , while ensuring that from we can reach the same states as were previously reachable from . The definitions are a little complicated because we have to account for some special cases. For example, we would maybe want to remove (since now we have a new initial state) but there is the possibility that is part of the acceptance runs of some strings, and not only as an initial state. The same goes for the states in , and that is why we have many different cases to consider in the definition of . We have to make sure not to lose any accepting runs with the removal of .
Now, we make some observations about . To construct , we are removing at least one state from , but we are adding at most one new state, . This implies that (notation here indicates set cardinality). Similarly for the construction of . Notice that each transition we add to construct (besides the ones that come directly from ) corresponds to a transition that already existed and that involved at least one state from . So, all in all, we have not really added any new transitions, just simulated the ones where states in appeared. That means that . So, as a whole, contains at most as many states and transitions as , and maybe less. Does that mean that (notation here indicates encoding sizes) ? It will depend on the type of encoding used, of course. So we will consider that the NFA in the input is encoded in the following (natural) way. First, a list of all states, followed by the list of all tuples in the transition relation, and at the end the initial and final states. Also, we assume that all states have an encoding of the same size (which is easy to achieve through padding). And the same goes for all transitions. With that encoding, since has less (or equal) number of states and transitions than , it is clear that . Of course, it is also true that . We can then conclude that , that is, condition (5) is satisfied. We also have by definition of that and . Since , condition (6) is also true:
[TABLE]
Finally, we turn to condition (8). Let . Since , condition (8) amounts to checking that
[TABLE]
where is constructed by considering , that is, . Notice that if , then both sides of the equivalence above are immediately false (and thus the equivalence is true), so we need only consider the case where . We will now prove both directions of the equivalence. First, suppose . Then, by definition, we know there is an accepting run of on input such that
[TABLE]
where , and for all . Now, we will show that , that is, is accepted by . To do that, we first show by induction the following property: for all there is a valid run of on input (although the run is not necessarily accepting) that looks like this:
[TABLE]
where and for all , we have that and
[TABLE]
To prove this fact by induction, consider first the case of . By definition, we know that and . There are now two different possibilities. First, if , then by definition of , we know that . Second, if , then by definition of , we know that . So the property is true when .
Now, suppose the property holds for some , and consider the case for . By the induction hypothesis, we know there is a valid run such that
[TABLE]
where and for all . Now, by the induction hypothesis, there are four possibilities (where each possibility is represented in one of the four sets that form the definition of ):
- •
and . In that case, if we set , by definition we know that .
- •
and . In that case, if we set , by definition we know that .
- •
and . In that case, if we set , by definition we know that .
- •
and . In that case, if we set , by definition we know that .
All that means that we can add one more transition to to form a valid run given by
[TABLE]
where and for all , we have that and
[TABLE]
The property is thus proved. Now, consider a valid run of that type for that looks like
[TABLE]
where and for all , we have that . Now, by the property just proved, we know there are two possibilities. First, if , we know . Since , we have that and thus is an accepting run, which means that is accepted by . Second, if , we know . And since , we have that and thus is again an accepting run, which means that is accepted by . All this proves that if , then . The proof for the other direction is analogous.
We conclude this section by pointing out that the same proof works for the case of MEM-UFA. That is, the same definitions of , and work for that proof. The only difference is that we also need to show that produces a valid automaton for the relation, that is, an unambiguous NFA. But that is not hard to show from the previous proof. Making similar use of the notation of valid runs, it can be shown that if had two different accepting runs for some word , then would have two different accepting runs for , and so it would not be unambiguous.
The reference list from the paper itself. Each links out to its DOI / PubMed record.
- 1(1)
- 2Abiteboul et al . (2016) Serge Abiteboul, Gerome Miklau, Julia Stoyanovich, and Gerhard Weikum. 2016. Data, Responsibly (Dagstuhl Seminar 16291). Dagstuhl Reports 6, 7 (2016), 42–71.
- 3Aho and Hopcroft (1974) Alfred V Aho and John E Hopcroft. 1974. The design and analysis of computer algorithms . Pearson Education India.
- 4Álvarez and Jenner (1993) Carme Álvarez and Birgit Jenner. 1993. A very hard log-space counting class. Theoretical Computer Science 107, 1 (1993), 3–30.
- 5Amarilli et al . (2017) Antoine Amarilli, Pierre Bourhis, Louis Jachiet, and Stefan Mengel. 2017. A Circuit-Based Approach to Efficient Enumeration. In Proceedings of ICALP . 111:1–111:15.
- 6Amarilli et al . (2018) Antoine Amarilli, Florent Capelli, Mikaël Monet, and Pierre Senellart. 2018. Connecting Knowledge Compilation Classes and Width Parameters. Co RR abs/1811.02944 (2018).
- 7Angles et al . (2017) Renzo Angles, Marcelo Arenas, Pablo Barceló, Aidan Hogan, Juan Reutter, and Domagoj Vrgoč. 2017. Foundations of modern query languages for graph databases. ACM Computing Surveys (CSUR) 50, 5 (2017), 68.
- 8Arenas et al . (2012) Marcelo Arenas, Sebastián Conca, and Jorge Pérez. 2012. Counting beyond a Yottabyte, or how SPARQL 1.1 property paths will prevent adoption of the standard. In Proceedings of WWW . 629–638.
