Ranked enumeration of MSO logic on words

29
Ranked enumeration of MSO logic on words Pierre Bourhis CNRS Lille, CRIStAL UMR 9189, University of Lille, INRIA Lille, France Alejandro Grez PUC & IMFD, Chile Louis Jachiet LTCI, IP Paris, France Cristian Riveros PUC & IMFD, Chile Abstract In the last years, enumeration algorithms with bounded delay have attracted a lot of attention for several data management tasks. Given a query and the data, the task is to preprocess the data and then enumerate all the answers to the query one by one and without repetitions. This enumeration scheme is typically useful when the solutions are treated on the fly or when we want to stop the enumeration once the pertinent solutions have been found. However, with the current schemes, there is no restriction on the order how the solutions are given and this order usually depends on the techniques used and not on the relevance for the user. In this paper we study the enumeration of monadic second order logic (MSO) over words when the solutions are ranked. We present a framework based on MSO cost functions that allows to express MSO formulae on words with a cost associated with each solution. We then demonstrate the generality of our framework which subsumes, for instance, document spanners and regular complex event processing queries and adds ranking to them. The main technical result of the paper is an algorithm for enumerating all the solutions of formulae in increasing order of cost efficiently, namely, with a linear preprocessing phase and logarithmic delay between solutions. The novelty of this algorithm is based on using functional data structures, in particular, by extending functional Brodal queues to suit with the ranked enumeration of MSO on words. 2012 ACM Subject Classification Theory of computation Database theory Keywords and phrases Persistent data structures, Query evaluation, Enumeration algorithms. 1 Introduction Managing and querying word structures such as texts has been one of the classical problems of different communities in computer science. In particular, this problem has been predominant in information extraction where the goal is to extract some subparts of a text. A logical approach that has brought a lot of attention in the database community is document spanners [15]. This logical framework provides a language for extracting subparts of a document. More specifically, regular spanners are based on regular expressions that fill relations with tuples of the texts’ subparts. These relations can afterwards be queried by conjunctive or datalog-like queries. The document spanners’ main algorithmic problem is the efficient evaluation of a spanner over a word. Recently, a novel approach has been to focus on the enumeration problem to obtain efficient evaluation algorithms. The principle of an enumeration algorithm is to create a representation of the set of answers efficiently depending only on the input word’s size and the query, and not in the number of answers. This time is called the preprocessing time. The second part of an enumeration algorithm is to enumerate the outputs one by one using the previous representation. The time between two consecutive outputs is called the delay. As for the preprocessing time, an efficient delay should not depend on the number of outputs, arXiv:2010.08042v1 [cs.FL] 15 Oct 2020

Transcript of Ranked enumeration of MSO logic on words

Ranked enumeration of MSO logic on wordsPierre BourhisCNRS Lille, CRIStAL UMR 9189, University of Lille, INRIA Lille, France

Alejandro GrezPUC & IMFD, Chile

Louis JachietLTCI, IP Paris, France

Cristian RiverosPUC & IMFD, Chile

AbstractIn the last years, enumeration algorithms with bounded delay have attracted a lot of attention

for several data management tasks. Given a query and the data, the task is to preprocess thedata and then enumerate all the answers to the query one by one and without repetitions. Thisenumeration scheme is typically useful when the solutions are treated on the fly or when we wantto stop the enumeration once the pertinent solutions have been found. However, with the currentschemes, there is no restriction on the order how the solutions are given and this order usuallydepends on the techniques used and not on the relevance for the user.

In this paper we study the enumeration of monadic second order logic (MSO) over words whenthe solutions are ranked. We present a framework based on MSO cost functions that allows toexpress MSO formulae on words with a cost associated with each solution. We then demonstrate thegenerality of our framework which subsumes, for instance, document spanners and regular complexevent processing queries and adds ranking to them. The main technical result of the paper is analgorithm for enumerating all the solutions of formulae in increasing order of cost efficiently, namely,with a linear preprocessing phase and logarithmic delay between solutions. The novelty of thisalgorithm is based on using functional data structures, in particular, by extending functional Brodalqueues to suit with the ranked enumeration of MSO on words.

2012 ACM Subject Classification Theory of computation → Database theory

Keywords and phrases Persistent data structures, Query evaluation, Enumeration algorithms.

1 Introduction

Managing and querying word structures such as texts has been one of the classical problems ofdifferent communities in computer science. In particular, this problem has been predominantin information extraction where the goal is to extract some subparts of a text. A logicalapproach that has brought a lot of attention in the database community is documentspanners [15]. This logical framework provides a language for extracting subparts of adocument. More specifically, regular spanners are based on regular expressions that fillrelations with tuples of the texts’ subparts. These relations can afterwards be queried byconjunctive or datalog-like queries.

The document spanners’ main algorithmic problem is the efficient evaluation of a spannerover a word. Recently, a novel approach has been to focus on the enumeration problem toobtain efficient evaluation algorithms. The principle of an enumeration algorithm is to createa representation of the set of answers efficiently depending only on the input word’s size andthe query, and not in the number of answers. This time is called the preprocessing time. Thesecond part of an enumeration algorithm is to enumerate the outputs one by one using theprevious representation. The time between two consecutive outputs is called the delay. Asfor the preprocessing time, an efficient delay should not depend on the number of outputs,

arX

iv:2

010.

0804

2v1

[cs

.FL

] 1

5 O

ct 2

020

2 Ranked enumeration of MSO logic on words

but only on the input size (i.e., word and query). In general, the most efficient enumerationalgorithms have linear preprocessing time and constant delay, both in the size of the input.

Several people have studied the enumeration problem over words following differentformalisms. For example, [3, 7, 27] studied the enumeration problem for MSO logic, [16, 2]for regular spanners (i.e. automata), and [20] for streaming evaluation in complex eventprocessing. For all these formalisms, it is shown that there exists an enumeration algorithmwith linear time preprocessing and delay constant in the size of the input word.

The interest of an efficient enumeration algorithm is to provide a process that can quicklygive the firsts answers. Unfortunately, these answers may not be relevant for the user; thatis, the enumeration process does not assume how the output will be ordered. A classicalmanner of considering the user’s preferences is to associate a score to each solution and thenrank them following this score. This approach has been used particularly in the contextof information extraction. Indeed, there have been several recent proposals [11, 10] toextend document spanners with annotations from a semi-ring. The proposed annotations aretypically useful to capture the confidence of each solution [11]. For instance, [10] proves thatthe enumeration of the answers following their scores’ order is possible with polynomial-timepreprocessing and polynomial delay.

In this paper, we are interested in establishing a framework for scoring outputs andimprove the bounds proved in [11]. We propose using what we called MSO cost functions,which are formulas in weighted logics [13] extended with open variables. These formulasprovide a simple formalism for defining the output and scoring with MSO logic. We showthat one can translate each MSO cost function to a cost transducer. These machines are arestricted form of weighted functional vset-automaton [11], for which there exists at mostone run for any word and any valuation. We use cost transducers to study the rankedenumeration problem: enumerate all outputs in increasing rank order. Specifically, the mainresult of the paper is an algorithm for enumerating all the solutions of a cost transducer inincreasing order efficiently; specifically, with a linear preprocessing phase and a logarithmicdelay between solutions.

Our approach generalizes an algorithm for enumerating solutions proposed in [20, 16].The preprocessing part builds a heap containing the answers with their score, and one step ofthe enumeration is simply a pop of the heap. For this, we use a general data structure thatwe called Heap of Words (HoW), having the classical heap operations of finding/deleting theminimal element, adding an element, and melding two heaps. We also need to add two newoperations that allow us to concatenate a letter to and increase the score of all elements ofthe heap. Finally, we require that this structure is fully-persistent [12], i.e., that each of theprevious operations returns a new heap without changing the previous one. To obtain therequired efficiency, we rely on a classical persistent data structure called Brodal queue thatwe extend in order to capture the new operations over the stored words and scores presentedabove. We call this extension an incremental Brodal queue.

Finally, for ranked query evaluation, there has been recent progress in the contextconjunctive queries: on the efficient computation of top-k queries [29] and the efficient rankedenumeration [28, 9]. These advances consider relational data (which is more general thanwords) and conjunctive queries (which is more restricted than MSO queries); they are thusincomparable to our work. However, it is important to note some similarities with our work,such as the need for an “advanced” priority queue (the Fibonacci heap [9]), which meansthat our incremental queues might be of great interest there.

Contributions The contributions of this paper are threefold: (i) we introduce MSO costfunctions, a framework to express MSO queries and scores, generalizing the proposals of

P. Bourhis, A. Grez, L. Jachiet, and C. Riveros 3

document spanners; (ii) we give a ranked enumeration scheme that has linear preprocessingtime and logarithmic delay in data complexity with a polynomial combined complexity;(iii) we introduce two new data structures for our scheme: the Heaps of Words and theincremental Brodal queues. Both of these structures might be of interest in other rankedenumerations schemes.Organization. Section 2 introduces the ranked enumeration problem for MSO queries onwords. Section 3 presents MSO cost functions which is our framework to rank MSO queries.Section 4 describes our enumeration scheme that rely on two data structures: the Heap ofWords described in Section 5 and the incremental Brodal queues presented in Section 6. Wefinish with some conclusions in Section 7.

2 Preliminaries

Words. We denote by Σ a finite alphabet, Σ∗ all words over Σ, and ε the empty-word of0 length. Give a word w = a1 . . . an, we write w[i] = ai. For two words u, v ∈ Σ∗ we write u ⋅ vas the concatenation of u and v. We denote by [n] = {1, . . . , n}.Ordered groups. A group is a pair (G,⊕,O) where G is a set of elements, ⊕ is a binaryoperation over G that is associative, O ∈ G is a neutral element for ⊕ (i.e. O⊕ g = g ⊕O = g)and every g ∈ G has an inverse with respect to ⊕ (i.e. g ⊕ g−1 = O for some g−1 ∈ G). Agroup is abelian if, in addition, ⊕ is commutative (i.e. g1 ⊕ g2 = g2 ⊕ g1). From now on,we assume that all groups are abelian. We say that (G,⊕,O,⪯) is an ordered group if(G,⊕,O) is a group and ⪯ is a total order over G that respects ⊕, namely, if g1 ⪯ g2 theng1 ⊕ g ⪯ g2 ⊕ g for every g, g1, g2 ∈ G. Examples of (abelian) ordered groups are (Z,+,0,≤)and (Zk,+, (0, . . . ,0),≤k) where ≤k represents the lexicographic order over Zk.MSO. We use monadic second-order logic for defining properties over words. As usual, weencode words as logical structures with an order predicate and unary predicates to representthe order and the letters of each positions of the word, respectively. More formally, fix analphabet Σ and let w ∈ Σ∗ be a word of length n. We encode w as a structure ([n],≤, (Pa)a∈Σ)where [n] is the domain, ≤ is the total order over [n], and Pa = {i ∣ w[i] = a}. By some abuseof notation, we also use w to denote its corresponding logical structure.

A MSO-formula ϕ over Σ is given by:

ϕ ∶= x ≤ y ∣ Pa(x) ∣ x ∈X ∣ ϕ ∧ ϕ ∣ ¬ϕ ∣ ∃x.ϕ ∣ ∃X.ϕ

where a ∈ Σ, x and y are first-order (FO) variables, and X is a monadic second order (MSO)variable (i.e. a set variable). We write ϕ(x̄, X̄) where x̄ and X̄ are the sets of free FO andMSO variables of ϕ, respectively. An assignment σ for w is a function σ ∶ x̄ ∪ X̄ → 2[n] suchthat ∣σ(x)∣ = 1 for every x ∈ x̄ (note that we treat FO variables as a special case of MSOvariables). As usual, we denote by dom(σ) = x̄ ∪ X̄ the domain of the function σ. Then wewrite (w,σ) ⊧ ϕ when σ is an assignment over w, dom(σ) = x̄ ∪ X̄, and w satisfies ϕ(x̄, X̄)when each variable in x̄ ∪ X̄ is instantiated by σ (see [23]). Given a formula ϕ(x̄, X̄), wedefine ⟦ϕ⟧(w) = {σ ∣ (w,σ) ⊧ ϕ(x̄, X̄)}. For the sake of simplification, from now on we willonly use X̄ to denote the free variables of ϕ(X̄) and use X ∈ X̄ for an FO or MSO variable.

For any assignment σ over w, we define the support of σ, denoted by supp(σ), as the setof positions mentioned in σ; formally, supp(σ) = {i ∣ ∃v ∈ dom(σ). i ∈ σ(v)}. Furthermore,we encode assignments as sequences over the support as follows. Let supp(σ) = {i1, . . . , im}such that ij < ij+1 for every j <m. Then we define the (word) encoding of σ as:

enc(σ) = (X̄1, i1)(X̄2, i2) . . . (X̄m, im)

4 Ranked enumeration of MSO logic on words

such that X̄j = {X ∈ dom(σ) ∣ ij ∈ σ(X)} for every j ≤ m. That is, we represent σ as anincreasing sequence of positions, where each position is labeled with the variables of σ where itbelongs. This is the standard encoding used to represent assignments for running algorithmsregarding MSO formulas [3, 7]. Finally, we define the size of σ as ∣ enc(σ)∣ = ∣dom(σ)∣ ⋅m.

Enumeration algorithms. Given a formula ϕ and a word w, the main goal of the paperis to study the enumeration of assignments in ⟦ϕ⟧(w). We give here a general definition ofenumeration algorithm and how we measure its delay. Later we use this to define the rankedenumeration problem of MSO.

As it is standard in the literature [3, 7, 27], we consider algorithms on Random AccessMachines (RAM) with uniform cost measure [1] equipped with addition and subtraction asbasic operations. A RAM has read-only input registers (containing the input I), read-writework memory registers and write-only output registers. We say that an algorithm E is anenumeration algorithm for MSO evaluation if E runs in two phases, for every MSO-formulaϕ and a word w.1. The first phase, called the preprocessing phase, does not produce output, but may prepare

data structures for use in the next phase.2. The second phase, called the enumeration phase, occurs immediately after the precompu-

tation phase. During this phase, the algorithm:writes # enc(σ1)# enc(σ2)# . . .# enc(σk)# to the output registers where # is a dis-tinct separator symbol, and σ1, . . ., σk is an enumeration (without repetition) of theassignments of ⟦ϕ⟧(w);it writes the first # as soon as the enumeration phase starts,it stops immediately after writing the last #.

The separation of E ’s operation into a preprocessing and enumeration phase is done to beable to make an output-sensitive analysis of E ’s complexity. Formally, we say that E haspreprocessing time f ∶ N2 → N if there exists a constant C such that the number of instructionsthat E executes during the preprocessing phase on input (ϕ,w) is at most C × f(∣ϕ∣, ∣w∣)for every MSO-formula ϕ and word w. Furthermore, we measure the delay as follows. Lettimei(ϕ,w) denote the time in the enumeration phase when the algorithm writes the i-th #(if it exists) when running on input (ϕ,w). Define delayi(ϕ,w) = timei+1(ϕ,w)− timei(ϕ,w).Further, let outputi(ϕ,w) denote the i-th element that is output by E when running oninput (ϕ,w), if it exists. We say that E has delay g ∶ N2 → N if there exists a constant Dsuch that, for all ϕ and w, it holds that:

delayi(ϕ,w) ≤ D × ∣outputi(ϕ,w)∣ × g(∣ϕ∣, ∣w∣)

for every i ≤ ∣⟦ϕ⟧(w)∣. Furthermore, if ⟦ϕ⟧(w) is empty, then delay1(ϕ,w) ≤ k, namely, itends in constant time. Finally, we say that E has preprocessing time f ∶ N → N and delayg ∶ N→ N in data complexity, if there exists a function c ∶ N→ N such that E has preprocessingtime c(∣ϕ∣) × f(∣w∣) and has delay c(∣ϕ∣) × g(∣w∣) (i.e., f and g describe the complexity onceϕ is considered as fixed).

It is important to notice that, although we fix a particular encoding for assignmentsand we restrict the enumeration algorithms to this encoding, we can use any encoding forthe assignments whenever there exists a linear transformation between enc(⋅) and the newencoding. Given the definition of delay, if we use an encoding enc′(σ) for σ, and thereexists a linear time transformation between enc(σ) and enc′(σ) for every σ, then the sameenumeration algorithm works for enc′(⋅). In particular, whenever the encoding dependslinearly over supp(σ) and ∣x̄ ∪ X̄ ∣, then the aforementioned property holds.

P. Bourhis, A. Grez, L. Jachiet, and C. Riveros 5

Ranked enumeration. For an MSO formula ϕ and w ∈ Σ∗, we consider the rankedenumeration of the set ⟦ϕ⟧(w). For this, we need to assign an order to the outputs andwe do this by mapping each element to a total order set. Fix a set C with a total order ⪯over C. A cost function is any partial function κ that maps words w ∈ Σ∗ and assignments σto elements in C. Without loss of generality, we assume that κ is defined only over pairs(w,σ) such that σ is an assignment over w.

Let ϕ be an MSO formula and κ a cost function over (C,⪯). We define the rankedenumeration problem of (ϕ,κ) as

Problem: RANK-ENUM[ϕ,κ]

Input: A word w ∈ Σ∗.Output: Enumerate all σ1, . . . , σk ∈ ⟦ϕ⟧(w) without

repetitions and such that κ(w,σi) ⪯ κ(w,σi+1).

Note that we consider the version of the problem in data-complexity where ϕ and κ are fixed.We say that RANK-ENUM[ϕ,κ] can be solved with preprocessing time f(n) and delayg(n) if there exists an enumeration algorithm E that runs with preprocessing time f(n) anddelay g(n) and, for every w ∈ Σ∗, E enumerates ⟦ϕ⟧(w) in increasing ordered according to κ.In the next section, we give a language to define cost functions and we state our main result.

3 MSO cost functions

To state our main result about ranked enumeration of MSO, first we need to choose aformalism to define cost functions. We do this by staying in the same setting of MSO logic byconsidering weighted logics over words [13, 14, 22]. Functions defined by extensions of MSOhas been studied by using weighted automata, but also people have found it counterparts byextending MSO with a semiring. We use here a fragment of weighted MSO parametrized byan ordered group to fit our purpose.

Fix an ordered group (G,⊕,O,⪯). A weighted MSO-formula α over Σ and G is given bythe following syntax:

α ∶= [ϕ↦ g] ∣ α⊕ α ∣ Σx.α

where ϕ is an MSO-formula, g ∈ G, and x is an FO variable. Further, we assume that theΣx quantifier cannot be nested. For example, (Σx.[ϕ ↦ g]) ⊕ (Σ y.[ϕ′ ↦ g′]) is a validformula but Σx.Σ y.[ϕ ↦ g] is not. Similar than for MSO formulas, we write α(x̄, X̄) tostate explicitly the sets of FO-variables x̄ and of MSO variables X̄ that are free in α.

Let σ be an assignment. For any FO-variable x and i ∈ N we denote by σ[x → i]the extension of σ with x assigned to i, namely, dom(σ[x → i]) = {x} ∪ dom(σ) such thatσ[x→ i](x) = {i} and σ[x→ i](y) = σ(y) for every y ∈ dom(σ)∖{x}. We define the semanticsof a weighted MSO formula α as a function from words and assignments to elements in G.Formally, for every w ∈ Σ and every assignment σ over w we define the output ⟦α⟧(w,σ)recursively as follows:

⟦[ϕ↦ g]⟧(w,σ) = { g (w,σ) ⊧ ϕO otherwise.

⟦α⊕ α′⟧(w,σ) = ⟦α⟧(w,σ)⊕ ⟦α′⟧(w,σ)

⟦Σx.α⟧(w,σ) =∣w∣

⊕i=1

⟦α⟧(w,σ[x→ i])

where ϕ is any MSO-formula, α and α′ are weighted MSO formulas, and g ∈ G. By someabuse of notation, in the following we will not make distinction between α and ⟦α⟧, that is,the cost function over G defined by α.

6 Ranked enumeration of MSO logic on words

I Example 1. Consider the alphabet {a, b} and suppose that we want to define a costfunction that counts the number of a-letters between two variables x and y. This can bedefined in weighted MSO over Z as follows:

α1 ∶= Σ z.[(x ≤ z ∧ z ≤ y ∧ Pa(z))↦ 1]

Here, α1 use z to count over all positions of the word and we count 1 whenever z is labeledwith a and is between x and y, and we count 0, otherwise, which is the identity of Z.

I Example 2. Consider again the alphabet {a, b} and suppose that we want a cost function tocompare assignments over variables (x, y) lexicographically. For this, we can write a weightedMSO-formula over Z2 that maps each assignment σ over x and y to a pair (σ(x), σ(y)). Thiscan be defined in weighted MSO over Z2 as follows:

α2 ∶= (Σ z1. [(z1 ≤ x)↦ (1,0)]) + (Σ z2. [(z2 ≤ y)↦ (0,1)])

Similar than for the previous example, we use the Σ -quantifier to add in the first and secondcomponent the value of x and y, respectively. In fact, for every assignment σ = {x→ i, y → j}over w ∈ Σ∗ it holds that ⟦α2⟧(w,σ) = (i, j).

Strictly speaking, the syntax and semantics of weighted MSO defined above is a restrictedversion of weighted logics [13], in the sense that weighted logics is usually defined over asemiring, which has two binary operations ⊕ and ⊙. Although it will be interesting to extendour results for weighted logics over semiring, we leave this for future work.

We are ready to state the main result of the paper about ranked enumeration of MSO.

I Theorem 3. Fix an alphabet Σ and an ordered group G. For every MSO formula ϕ over Σand every weighted MSO formula α over Σ and G the problem RANK-ENUM[ϕ,α] can besolved with linear preprocessing time and logarithmic delay.

We show applications of this result in the framework of document spanners [15, 11] andthe setting of complex event processing [20]. Due to space restrictions, we address theseapplications in the appendix.

As it is common for MSO logic over words, we prove this result by developing anenumeration algorithm using automata theory. Specifically, we define a weighted automatamodel, that we called cost transducer, and show that its expressibility is equivalent to thecombination of (boolean) MSO and weighted MSO logic.

From now on, fix an input alphabet Σ and an output alphabet Γ. Furthermore, fix anordered group (G,⊕,O,⪯). A cost transducer over G is a tuple T = (Q,∆, κ, I, F ), whereQ is the set of states, ∆ ⊆ Q ×Σ × 2Γ ×Q is the transition relation, κ ∶ ∆→ G is a functionthat associates a cost to every transition of ∆, and I ∶ Q → G, F ∶ Q → G are partialfunctions that associate a cost in G to (some) states in Q. The functions I and F are partialfunctions because they naturally define the set of initial and final states as dom(I) anddom(F ), respectively. A run of T over a word w = a1a2 . . . an is a sequence of transitionsρ ∶ q0

a1/X̄1ÐÐ→q1a2/X̄2ÐÐ→ . . . an/X̄nÐÐ→ qn such that q0 ∈ dom(I) and (qi−1, ai, X̄i, qi) ∈ ∆ for every i ≤ n.

We say that ρ is accepting if qn ∈ dom(F ).For every accepting run ρ as defined above, let {i1, . . . , im} ⊆ [n] be all the positions of ρ

such that X̄ij ≠ ∅ and ij < ij+1 for all j ≤m. Then we define the output of ρ as the sequence:

out(ρ) = (X̄i1 , i1)(X̄i2 , i2) . . . (X̄im , im)

P. Bourhis, A. Grez, L. Jachiet, and C. Riveros 7

Moreover, we extend κ over accepting runs ρ by adding the costs of all transitions of ρ plusthe initial and final cost, namely:

κ(ρ) = I(q0)⊕∣w∣

⊕i=1κ((qi−1, ai, X̄i, qi))⊕ F (qn).

Note that out(ρ) defines the encoding of some assignment σ over w with dom(σ) = Γ andout(ρ) = enc(σ). Of course, the opposite direction is not true: for some assignment σthere could be no run ρ that defines σ and, moreover, there could be two runs ρ1 and ρ2such that out(ρ1) = out(ρ2) = enc(σ), but κ(ρ1) ≠ κ(ρ2). For this reason, we impose anadditional restriction to cost transducers: we assume that all cost transducers in this paperare unambiguous, that is, for every w ∈ Σ∗ there does not exist two runs ρ1 and ρ2 of w suchthat out(ρ1) = out(ρ2). In other words, a cost transducers satisfies that for every w ∈ Σ∗ andassignment σ there exists at most one run ρ such that out(ρ) = enc(σ).

Given the unambiguous restriction of cost transducers, we can define a partial functionfrom pairs (w,σ) to G as costT (w,σ) = κ(ρ) whenever there exists a run ρ of w such thatout(ρ) = enc(σ). Otherwise costT (w,σ) is not defined. Given that for some pairs (w,σ) thefunction costT is not defined, we can define the set ⟦T ⟧(w) = {σ ∣ costT (w,σ) is defined} ofall outputs of T over w.

It is important to notice that, given w ∈ Σ∗, a cost transducer T is in charged of (1)defining the set of assignments ⟦T ⟧(w) and (2) assigning a cost ⟦T ⟧(w,σ) for each outputσ ∈ ⟦T ⟧(w). These two task are separated in our setting of ranked MSO enumeration byhaving a MSO formula ϕ that defines the outputs ⟦ϕ⟧ and a weighted MSO formula α toassign a cost to each pair (w,σ). In fact, one can show that cost transducers are equallyexpressive than combining MSO plus weighted MSO (see proof in appendix B):

I Proposition 4. For every cost transducer T , there exists a MSO formula ϕT and weightedMSO formula αT such that ⟦T ⟧ = ⟦ϕT ⟧ and costT (w,σ) = ⟦αT ⟧(w,σ) for every σ ∈ ⟦T ⟧(w).Moreover, for every MSO formula ϕ and weighted MSO formula α, there exists a costtransducer Tϕ,α such that ⟦ϕ⟧ = ⟦Tϕ,α⟧ and ⟦α⟧(w,σ) = costTϕ,α(w,σ) for every σ ∈ ⟦ϕ⟧(w).

By the previous result, we can represent pairs of formulas (ϕ,α) by using cost transducersand vice-versa. Similar than for MSO [26], there exists a non-elementary blow-up for goingfrom (ϕ,α) to a cost transducer and this blow-up cannot be avoided [18].

To solve the problem RANK-ENUM[ϕ,α] we can use a cost transducer Tϕ,α to enumerateall its outputs following the cost assigned by this machine. More concretely, we study thefollowing rank enumeration problem for cost transducers:

Problem: RANK-ENUM-TInput: A cost transducer T and a word w ∈ Σ∗.

Output: Enumerate all σ1, . . . , σk ∈ ⟦T ⟧(w) without repetitions andsuch that costT (w,σi) ⪯ costT (w,σi+1).

Note that for RANK-ENUM-T we consider the cost transducer as part of the input1.Indeed, for this case we can provide an enumeration algorithm with stronger guaranteesregarding the preprocessing time in terms of T . We now give the theorem formalizing themain result of this paper, which will be proven in the next section:

1 In Section 2 we introduce the setting of ranked enumeration for MSO formulas and cost functions. Onecan easily extend this setting and the definiton of enumeration algorithms for cost transducer.

8 Ranked enumeration of MSO logic on words

I Theorem 5. The problem RANK-ENUM-T can be solved with ∣T ∣ ⋅ ∣w∣ preprocessing timeand log(∣T ∣ ⋅ ∣w∣)-delay.

In the rest of the paper, we present the above mentioned ranked enumeration algorithm.We start by showing a general algorithm based on a novel data structure called a Heap ofWords. In Section 5, we provide the implementation of this structure. In Section 6, we showhow to implement the incremental Brodal queues, a technical data structure needed to obtainthe required efficiency.

4 Ranked enumeration algorithm

In this section, we will see how novel data structures can solve the ranked enumerationproblem for cost transducers on words. We provide an algorithm for the RANK-ENUM-Tproblem, which uses a structure called Heap of Words (HoW) as a black box. We specify theinterface of the HoW, to then present the full algorithm. The HoW structure is addressed indetail in the next section. This structure has the property of being fully-persistent. Giventhat this is a crucial property, we start with a brief introduction to this concept.

Fully-persistent data structures. A data structure is said fully-persistent [12] whenno operation can modify the data structure. In a fully-persistent data structure, all theoperations return new data structures, without changing the original ones. While this seemsto be a restriction on the possible operations, it allows “sharing”. For instance, with afully-persistent linked list data structure, we can keep two lists l1, l2 with l1 being some valuefollowed by the content of l2 and since no operation modifies the content of l1 or l2 there isno risk that an access to l1 modifies indirectly l2. In contrast, if we had allowed an operationthat modifies the first value of a list in place (i.e. without returning a new list containingthe modification), the applying this new operation on l2 would have modified both l1 and l2.

All data structures that we use in this paper are fully-persistent. We use these datastructures to store and enumerate the outputs of the cost transducer while, at the sametime, share and modify the outputs without any risk of losing them. For more informationof fully-persistent data structures, we refer the reader to [12].

The HoW data structure. A Heap of Words (HoW) over an ordered group (G,⊕,O,⪯)is a data structure h that stores a finite set {[w1 ∶g1], . . . , [wn ∶gn]} where each [wi ∶gi] is apair composed by a word wi ∈ Σ∗ and a priority gi ∈ G. Further, we assume that wi ≠ wjfor every i ≠ j, namely, the stored words form a set too. We define ⟦h⟧ = {w1, . . . ,wn} asthe content of h and, given the previous restriction, there is a one-to-one correspondencebetween [wi ∶ gi] and wi. Notice that we will usually write h = {[w1 ∶ g1], . . . , [wn ∶ gn]} todenote that h stores [w1 ∶g1], . . . , [wn ∶gn] but, strictly speaking, h is a data structure (i.e., aheap). Finally, we denote by ∅ the empty HoW.

The purpose of a HoW h is to store words and retrieve quickly the pair [w ∶ g] withminimum priority with respect to the order ⪯ of the group. We also want to manage hby deleting the word with minimum priority, adding new words, increasing the priority ofall elements by some g ∈ G, or extending all words with a new letter a ∈ Σ. Furthermore,we want to build the union of two HoWs. More formally, we consider the following set offunctions to manage HoWs. For HoWs h, h1, and h2, w ∈ Σ∗, g ∈ G, and a ∈ Σ we define:

w′ ∶= FindMin(h)h′ ∶= DeleteMin(h)h′ ∶= IncreaseBy(h, g)

h′ ∶= Meld(h1, h2) s.t. ⟦h1⟧ ∩ ⟦h2⟧ = ∅h′ ∶= Add(h, [w ∶g]) s.t. w ∉ ⟦h⟧h′ ∶= ExtendBy(h, a)

P. Bourhis, A. Grez, L. Jachiet, and C. Riveros 9

Algorithm 1 Preprocessing and enumeration phases for RANK-ENUM-T.input: T = (Q,∆, κ, I, F ) and w = a1 . . . an.1: procedure Preprocessing(T ,w)2: for each q ∈ dom(I) do3: h0

q ← Add(∅, [ε ∶I(q)])4: for i from 1 to n do5: for each t = (p, ai, X̄, q) ∈ ∆ do6: h← hi−1

p

7: if X̄ ≠ ∅ then8: h← ExtendBy(h, (X̄, i))9: h← IncreaseBy(h,κ(t))10: hiq ←Meld(hiq, h)11: for each q ∈ dom(F ) do12: h← IncreaseBy(hnq , F (q))13: hout ←Meld(hout, h)14: return hout

input: A heap of words h.1: procedure Enumeration(h)2: write #3: while h ≠ ∅ do4: write FindMin(h)5: h←DeleteMin(h)6: write #

where h′ is a new HoW and w′ ∈ Σ∗. In general, each of such functions receives a HoW andoutputs a HoW h′. As it was explained before, this data structure is fully-persistent and,therefore, after applying any of this function, both the output h′ and its previous version hare available. Now, we define the semantics of each operation. Let h = {[w1 ∶g1], . . . , [wn ∶gn]}.The FindMin of h returns a word w′ such that [w′ ∶ g′] is stored in h and g′ is minimalamong all the priorities stored, formally, [w′ ∶g′] ∈ h and g′ = min{g ∣ [w ∶g] ∈ h}. If there areseveral w′ satisfying this property, one is picked arbitrarily. Operation DeleteMin returnsa new HoW h′ that stores the set represented by h without the pair of the word returned byFindMin(h), that is, h′ = h∖ {[w′ ∶g′]} where w′ = FindMin(h) and g′ = min{g ∣ [w ∶g] ∈ h}.Finally, the functions Add, IncreaseBy, ExtendBy, and Meld are formally defined as:

Meld(h1, h2) ∶= h1 ∪ h2

Add(h, [w ∶g]) ∶= h ∪ {[w ∶g]}

IncreaseBy(h, g) ∶= {[w1 ∶(g1 ⊕ g)], . . . , [wn ∶(gn ⊕ g)]}

ExtendBy(h, a) ∶= {[(w1 ⋅ a) ∶g1], . . . , [(wn ⋅ a) ∶gn]}

We assume that Add, IncreaseBy, ExtendBy and Meld take constant time and FindMintakes O(∣w′∣) where w′ = FindMin(h). For DeleteMin(h), if h was built using n opera-tions Add, IncreaseBy, ExtendBy and Meld followed by some number of operationsDeleteMin then computing DeleteMin(h) takes O(∣w′∣ ⋅ log(n)) where w′ = FindMin(h).In the next section we show how to implement HoWs in order to satisfy these requirements.For now, we assume the existence of this data structure and use it to solve RANK-ENUM-T.The algorithm. In Algorithm 1, we show the preprocessing phase and the enumerationphase to solve RANK-ENUM-T. One one hand, the Preprocessing procedure receivesa cost transducer T = (Q,∆, κ, I, F ) and a word w ∈ Σ∗, and computes a HoW hout. Onthe other hand, the Enumeration procedure receives a HoW (i.e., hout) and enumeratesenc(σ1), . . . , enc(σk) such that {σ1, . . . , σk} = ⟦T ⟧(w) and costT (w,σi) ⪯ costT (w,σi+1).

In both procedures we use HoW to compute the set of answers. Indeed, for each q ∈ Qand each i ∈ {0, . . . , ∣w∣} we compute a HoW hiq, and also compute a hout to store the finalresults. We assume that all HoWs are empty (i.e., hout = ∅ and hiq = ∅) when the algorithmstarts. For each i, we call the set {hiq ∣ q ∈ Q} the i-level of HoW. Starting from the 0-level(lines 2-3), the preprocessing phase goes level by level, updating the i-level with the previous

10 Ranked enumeration of MSO logic on words

(i − 1)-level (lines 4-10). It is important to note here that the Meld(hiq, h) call (line 10) iswell-defined since T is unambiguous (i.e. ⟦hiq⟧ ∩ ⟦h⟧ = ∅). After reaching the last n-level,the algorithm joins all HoWs {hnq ∣ q ∈ dom(F )} into hout, by incrementing first their costwith F (q) and melding them into hout (lines 11-13). Finally, the preprocessing phase returnhout as output (line 14).

In order to understand the preprocessing algorithm, one has to notice that all theevaluation is based on a very simple fact. Let wi = a1 . . . ai and define the set RunT (q,wi) ofall partial runs of T over wi that end in state q. For any of such runs ρ = q0

a1/X̄1ÐÐ→ . . . ai/X̄iÐÐ→qi ∈RunT (q,wi), define the partial cost of ρ as κ∗(ρ) = I(q0)⊕⊕i

j=1 κ((qj−1, aj , X̄j , qj)). Afterexecuting Preprocessing, it will hold that: hiq = { [out(ρ) ∶κ∗(ρ)] ∣ ρ ∈ RunT (q,wi) }.This is certainly true for h0

q after lines 2-3 are executed. Then, if this is true for (i − 1)-level,after the i-th iteration of lines 5-10 we will have that hiq contains all pairs of the form[out(ρ) ⋅(X̄, i) ∶κ∗(ρ)⊕κ(t)] for each t = (p, ai, X̄, q) ∈ ∆, plus all pairs [out(ρ) ∶κ∗(ρ)⊕κ(t)]for each t = (p, ai,∅, q) ∈ ∆ and ρ ∈ RunT (p,wi−1). Given that each line takes constant time,we can conclude that the preprocessing phase takes time O(∣T ∣ ⋅ ∣w∣) as expected.

For the enumeration phase, we extract each output from hout, one by one, by alternatingbetween the FindMin and DeleteMin procedures. Since with DeleteMin we removethe minimum element of h after printing it, the correctness of the enumeration phase isstraightforward. Notice that this enumeration will print all outputs in increasing orderof priority. Furthermore, it will not print any output twice given that hout contains norepetitions. To bound the time, notice that the number of Add, IncreaseBy, ExtendByand Meld functions used during the pre-processing is at most O(∣T ∣ ⋅ ∣w∣). For this reason,the delay between each output w′ is bounded by O(log(∣T ∣ ⋅ ∣w∣) ⋅ ∣w′∣), satisfying the promiseddelay between outputs.

We want to finish this section by emphasizing that the ranked enumeration problemof cost transducers reduces to computing efficiently the HoW’s methods. Moreover, it iscrucial in this algorithm that this data structure is fully-persistent, and each operation takesconstant time. Indeed, this allows us to pass the outputs between levels very efficiently andwithout losing the outputs of the previous levels.

5 The implementation of HoW data structure

In this section we focus on the HoW data structure and explain its implementation usingyet another structure called incremental Brodal queue. We begin by explaining the generaltechnique we use to store sets of strings with priorities, and end by giving a full implementationof the functions to manage HoWs.

Let Σ be a possibly infinite alphabet and G = (G,⊕,O,⪯) an order group. A string-DAGover Σ and G is a DAG D = (V,E) where the edges are annotated with symbols in Σ∪{ε} andpriorities in G. Formally, each edge has the form e = (u, a, g, v), where u, v ∈ V , a ∈ Σ ∪ {ε}and g ∈ G. Given a path ρ = v1

a1,g1ÐÐÐ→ . . .ak,gkÐÐÐ→ vk, let [wρ ∶gρ] be the pair defined by ρ, where

wρ = a1 . . . ak and gρ = g1 ⊕ . . .⊕ gk. We make two more assumptions that any string-DAGmust satisfy. First, we assume that there is a special sink vertex � ∈ V that is reachable fromany v ∈ V , has no outgoing edges, and that all edges with ε must point to �. Second, weassume that, for every v ∈ V and every two different paths ρ and ρ′ from v to �, it holds thatwρ ≠ wρ′ . Given these two assumptions, we say that each v ∈ V encodes a set of pairs ⟦D⟧(v):for v = � this set is the empty set, while for all v ≠ � this set is defined by all the paths fromv to �, i.e., ⟦D⟧(v) = {[wρ ∶gρ] ∣ ρ is a path from v to �}. By these two assumptions, thereis a correspondence between the words in ⟦D⟧(v) and the paths from v to �. For instance,

P. Bourhis, A. Grez, L. Jachiet, and C. Riveros 11

n0 n1 n2

a,0

ε,5

e,4

b,1d,3

c,2

(a) A string-DAG D

n0 n1 n2

a,3

ε,5

e,6

b,3d,3

c,2

(b) The string-DAG prioritize(D)

Figure 1 A string-DAG D and the result of prioritize(D).

the strings associated with n0 in the string-DAG depicted in Figure 1a are ad with priority0 + 3 = 3, abc with priority 0 + 1 + 2 = 3, aec with priority 0 + 4 + 2 = 6 and ε with priority 5.

This structure is useful to store a big number of strings in a compressed manner. Further,since ε can only appear at the last edge of a path, by doing a DFS it can be used to retrieveall of them without repetitions and taking time linear in the length of each string. However,one can see that it is not very useful when we want to enumerate them by rank order. Thismotivates the following string-DAG construction. We define a function prioritize(D) thatreceives a string-DAG D = (V,E) and returns a string-DAG D′ = (V,E′) where each edge(u, a, g, v) of E is replaced by an edge (u, a, g⊕ g′, v) in E′, where g′ is the minimum priorityin ⟦D⟧(v). For instance, Figure 1b shows the string-DAG resulting after applying prioritizeto D of Figure 1a. Having prioritize(D) makes finding the string with minimum priority ofa vertex much easier: we simply need to follow recursively the edge with minimum priority.In n0 of Figure 1b we make the path n0

a,3Ð→ n1b,3Ð→ n2

c,2Ð→ � and compute the minimum pair[abc,3] (the priority is retrieved from the first edge).

Before presenting the HoW implementation, we need to introduce another fully-persistentdata structure. This structure is based on the Brodal queue [5], a known worst-case efficientpriority queue, which we extend with the new function increaseBy. Formally, an incrementalBrodal queue, or just a queue, is a fully-persistent data structure Q which stores a setP = {[E1 ∶g1] . . . [Ek ∶gk]}, where each Ei is a stored element and gi is its priority. As an abuseof notation, we often write Q = P . The functions to manage incremental Brodal queuesinclude all functions for HoW except ExtendBy, namely findMin, deleteMin, add, increaseByand meld; their definition also remains the same as for HoW. Note that we use differentfonts to distinguish the operations over HoWs versus the operations over incremental Brodalqueues. For example, we write FindMin for HoWs and findMin for queues. Further, thisqueue has two additional functions: isEmpty, that checks if the queue is the empty queue ∅;and minPrio, that returns the value g of the minimal priority among all the priorities stored.For the rest of this section we assume the existence of an incremental Brodal queue structuresuch that all functions run in time O(1) except for deleteMin, which runs in O(log(n)), wheren is the number of pairs stored in the queue. Finally, all these operations are fully-persistent.The in-detail explanation of this structure is derived to the next section.

With the previous intuition and the structure above, we can now present the implementa-tion for Heap of Words. A HoW h is implemented as an incremental Brodal queue Q thatstores a set {[(a1, h1) ∶ g1], . . . , [(ak, hk) ∶ gk]}, where each ai ∈ Σ ∪ {ε}, each hi is a HoWand each gk ∈ G. We write h = ⟨Q⟩ to make clear that we are talking about a HoW andnot the queue. The empty HoW is simply the empty queue ⟨∅⟩. Intuitively, the recursivereferences to HoWs are used to encode a string-DAG D; more specifically, we use it toencode prioritize(D) = (V,E) and store the edges using the queue structure. For every u ∈ V ,

12 Ranked enumeration of MSO logic on words

Algorithm 2 HoW’s implementation of Add, ExtendBy, FindMin and DeleteMin.1: procedure Add(⟨Q⟩, [a ∶g])2: return ⟨add(Q, [(a, ⟨∅⟩) ∶g])⟩

3: procedure ExtendBy(⟨Q⟩, a)4: if isEmpty(Q) then5: return ⟨∅⟩6: return ⟨add(∅, [(a, ⟨Q⟩) ∶minPrio(Q)])⟩

7: procedure FindMin(⟨Q⟩)8: (a, ⟨Q′⟩)← findMin(Q)9: if isEmpty(Q′) then

10: return a

11: return FindMin(⟨Q′⟩) ⋅ a

12: procedure DeleteMin(⟨Q⟩)13: if isEmpty(Q) then14: return ⟨∅⟩15: (a, ⟨R⟩)← findMin(Q)16: Q′ ← deleteMin(Q)17: ⟨R′⟩←DeleteMin(⟨R⟩)18: if isEmpty(R′) then19: return ⟨Q′⟩20: δ ← minPrio(R′)⊕ (minPrio(R))−1

21: g ← minPrio(Q)⊕ δ22: return ⟨add(Q′, [(a, ⟨R′⟩) ∶g])⟩

we define a HoW hu = ⟨Q⟩ such that each pair [(a, hv) ∶g] stored in Q represents an edge(u, a, g, v) ∈ E. For instance, continuing with the example of Figure 1b, we have a HoW foreach vertex: h� = ⟨∅⟩, hn2 = ⟨{[c, h� ∶2]}⟩, hn1 = ⟨{[(e, hn2) ∶6], [(b, hn2) ∶3], [(d, h�) ∶3]}⟩ andhn0 = ⟨{[(a, hn1) ∶3], [(ε, h�) ∶5]}⟩.

We now explain the implementation of the functions defined in Section 4 to manage HoW.Consider a HoW h = ⟨Q⟩. For each op ∈ {Meld, IncreaseBy}, the function is just applieddirectly to the queue, i.e., op(⟨Q⟩) = ⟨op(Q)⟩. The implementation of Add and the otherfunctions is now described and presented in Algorithm 2.

In the case of Add(h, a), an edge is added that points to ⟨∅⟩; this can be extendedto add a word w instead by allowing that edges keep words instead of single letters. Toimplement ExtendBy(⟨Q⟩, a), we simply need to create a new queue containing the element[(a, ⟨Q⟩) ∶ minPrio(Q)]. For FindMin(⟨Q⟩), to get the minimum element we recursivelyuse findMin(Q) to find the outgoing edge with minimum priority, as we explained whenthe prioritize function was introduced. For DeleteMin, in order to delete the string withminimum priority, we use the fact that the set of all paths, minus the one with minimalpriority, is composed by: (1) all the paths that do not start with the minimal edge, and (2) allthe paths starting with the minimal edge that are followed by any path minus the one withminimal priority. For instance, in Figure 1b, the minimal path from n0 is π = n0

aÐ→ n1dÐ→ �.

Then, the set of paths minus π is composed by (1) n0εÐ→ �, and (2) n0

aÐ→ n1eÐ→ n2

cÐ→ �,n0

aÐ→ n1bÐ→ n2

cÐ→ �. In procedure DeleteMin, ⟨Q′⟩ stores the paths of (1), while ⟨R′⟩ storesthe paths from (2) minus the first edge (lines 16-17). Further, since the minimal path wasremoved, a new priority needs to be computed for this edge, which is computed and stored asg (line 20-21). This priority is used to create an edge to R′, i.e. [(a, ⟨R′⟩) ∶g], which togetherrepresent the paths of (2). This is connected with the paths of (1), i.e. ⟨Q′⟩, and the resultis returned in line 22. The border case case where (2) is empty is managed by lines 18-19, inwhich case it simply returns ⟨Q′⟩.

We delegate the complexity proofs to the appendix C but this structures achieves thecomplexities given in Section 4. We end this section by arguing that the implementationof HoW is fully-persistent. For this, note that the performance of HoW relies on theimplementation of incremental Brodal queues. Indeed, given that these queues are fully-persistent and each method in Algorithm 2 creates new queues without modifying the previousones, the whole data structure is fully-persistent. Therefore, it is left to prove that we canextend Brodal queues as we already mentioned. We will show this in the next section.

P. Bourhis, A. Grez, L. Jachiet, and C. Riveros 13

6 Incremental brodal queues

In this section, we discuss how to implement an incremental Brodal queue, the last ingredientof our ranked enumeration algorithms for MSO cost functions. This data structure extendsBrodal queues [5] by including the increaseBy procedure. Indeed, our construction of incre-mental Brodal queues follows the same approach as in [5]. We start by defining what wecall an incremental binomial heap, for which most operations take logarithmic time, to thenshow how to extend it to lower the cost to constant time, except for deleteMin that takeslogarithmic time. The most relevant aspects for this extension to support increaseBy appearin the definition of the incremental binomial heap. For this reason and space restrictions,in this section we present only the implementation of the incremental binomial heap. Thedetails of how to extend it to an incremental Brodal queue can be found in the appendix.We start by introducing some notation to define then the data structure with the operations.

A multitree structure is a pair M = (V,first,next, v0) where V is a set of nodes, first ∶ V →V ∪ {�} and next ∶ V → V ∪ {�} are functions such that � ∉ V and v0 ∈ V is a special node.Further, we assume that the directed graph GM = (V,{(u, v) ∣ first(u) = v or next(u) = v})is a multitree, namely, it is a directed acyclic graph (DAG) in which the set of verticesreachable from any vertex induces a tree. Let Vv0 denotes the reachable nodes from v0 andGv0 = (Vv0 ,{(u, v) ∣ first(u) = v or next(u) = v}) the graph induced by Vv0 , which is a treeby definition. Note that Gv0 is using the first-child next-sibling encoding to form an orderedforest. To see this, let next∗(v) be the smallest subset of V such that v ∈ next∗(v) andnext(u) ∈ next∗(v) whenever u ∈ next∗(v). Then the set roots = next∗(v0) represents the rootsof the forest and for each v ∈ Vv0 the set children(v) = next∗(first(v)) are the children of thenode v in the forest where children(v) = ∅ when first(v) = �. Here both sets are ordered by thenext function, then we will usually write roots = v1, . . . , vj or children(v) = u1, . . . , uk to denoteboth the elements of the set and its order. Also, we write parent(v) = u if v ∈ children(u) andwe say that v is a leaf if first(v) = �. Note that in M a node could have different “parents”(i.e. GM is a DAG) depending on the node v0 that we start. We say that M forms a tree ifnext(v0) = �. Furthermore, for v ∈ V we denote by Mv the tree hanging from v, namely, Mv

is equal to M with the exception that v0Mv

= v and nextMv(v) = �. As it will clear below, thisencoding will be helpful to build the data structure and assure the persistent requirement.

A binomial tree of rank k is recursively defined as follows. A binomial tree of rank 0 is aleaf and a binomial tree of rank k + 1 is a multitree structure M that forms a tree such thatchildren(v0) = uk, . . . , u0 and Mui is a binomial tree of rank i. If M is a binomial tree wedenote its rank by rank(M). One can easily show by induction over the rank (see [6]) that forevery binomial tree M of ranked k, it holds that ∣Vv0 ∣ = 2k and, thus, the number of childrenof each node is of logarithmic size with respect to the size of T , i.e., ∣children(v)∣ ≤ log(∣Vv0 ∣)for every v ∈ Vv0 . We use this property several times throughout this section.

Fix an ordered group (G,⊕,O,⪯). An incremental binomial heap over G is definedas a pair H = (V,first,next, v0,∆, elem, δ0) where (V,first,next, v0) is a multitree structure,∆ ∶ V → G is the delta-priority function, elem ∶ V → E is the element function where E isthe set of elements that are stored, and δ0 ∈ G is an initial delta value. Further, if M isthe multitree structure defined by (V,first,next, v0) and roots = v1, . . . , vn are its roots, theneach Mvi is a binomial tree with rank(Mvi) < rank(Mvi+1) for each i < n. In other words,an incremental binomial heap has the same underlying structure than a standard binomialheap [6]. Usually in the literature [5], a binomial heap is imposed a min-heap property,meaning that a node always has lower priority than its children, which is crucial for dequeuingelements in order. Instead, we give to our heap a different semantics by keeping the difference

14 Ranked enumeration of MSO logic on words

between nodes with the ∆-function and computing the real priority function prv0 ∶ Vv0 → Gas follows: prv0(v) ∶= δ0 ⊕∆(v) whenever v is a root of the underlying multitree structure,and prv0(v) ∶= prv0(u)⊕∆(v) whenever parent(v) = u. Given that parent(v) depends on thestarting node v0, then prv0 also depends on v0. In addition, we assume that a min-heapproperty is satisfied over the real priority function, namely, prv0(u) ⪯ prv0(v) wheneverparent(v) = u. Then H is a heap where each node v ∈ V keeps a pair (elem(v),prv0(v)) whereelem(v) is the stored element and prv0(v) its priority in the heap. This principle of storingthe deltas between nodes instead of the real priority is crucial for supporting the increased-byoperation of the data structure.

Next, we show how to implement the operations of an incremental Brodal queue statedin Section 5, namely, isEmpty, increaseBy, findMin minPrio, deleteMin, add, and meld. Weimplement this with an incremental binomial heap where the only difference is that isEmptyand increaseBy will take constant time, and findMin minPrio, deleteMin, add, and meld willtake logarithmic time. In the appendix we show how to extend incremental binomial heapsto lower the complexity of findMin minPrio, deleteMin, and add to constant time, by usingthe same techniques as in [5]. Most operations of incremental binomial heaps are similar tothe operations on binomial heaps (see [6]), however, for the sake of completeness we explaineach one in detail, highlighting the main differences to manage the delta priorities.

From now on, fix an incremental binomial heap H = (V,first,next, v0,∆, elem, δ0). Giventhat all operations must be persistent, we will usually create a copy H ′ of H by extendingH with new fresh nodes. More precisely, we will say that H ′ is an extension of H (denotedby H ⊆H ′) iff VH ⊆ VH′ and opH′(v) = opH(v) for every v ∈ VH and op ∈ {first,next,∆, elem}(note that v0 and δ0 may change). Furthermore, for H ⊆ H ′ we will say that a nodev′ ∈ VH′ ∖ VH is a fresh copy of v ∈ VH if v′ in H ′ has the same structure as v in H whereonly the differences are defined explicitly, namely, we omit the functions that are the sameas for v. For example, if we say that “v′ is a fresh copy of v such that nextH′(v′) ∶= �”, thismeans that nextH′(v′) ∶= � and opH′(v′) = opH(v) for every op ≠ next.

The first operation, isEmpty(H), can easily be implemented in constant time, by justchecking whether v0 = � or not. Similarly, increaseBy(H,δ) can be implemented in constanttime by just updating δ0 to δ0 ⊕ δ, which is the purpose of having δ0. For findMin(H) orminPrio(H), a bit more of work is needed. Recall that a k-rank binomial tree with ∣V ∣ nodessatisfies ∣V ∣ = 2k. Given that roots = v1, . . . , vn is a sequence of binomial trees ordered by rank,one can easily see that n ∈ O(log(∣Vv0 ∣). Therefore, we need at most a logarithmic number ofsteps to find the node vi with the minimum priority and return elem(vi) or prv0(vi) wheneverfindMin(H) or minPrio(H) is asked, respectively.

For add(H,e, g) or deleteMin(H), we reduce them to melding two heaps. For the firstoperation, we create a heap H ′ whose multitree structure has one node, call it v, ∆H′(v) ∶= g,elemH′(v) ∶= e, and δ0

H′ ∶= O. Then we apply meld(H,H ′) obtaining a heap where the newnode (e, g) is added to H. For the second operation, we remove the minimum element bycreating two heaps and then apply the meld operation. Specifically, let roots = v1, . . . , vnbe the roots of H and vi be the root with the minimum priority. Then we build two heapsH1 and H2 such that H ⊆ Hi for i ∈ {1,2}. For H1, we extend H by creating fresh copiesof all vj , j ≠ i. Formally, define VH1 = VH ∪ {v′1, . . . , v′n} where each v′j is a fresh copy ofvj with the exception of v′i−1 that we set nextH1(v′i−1) ∶= v′i+1. Finally, define v0

H1= v′1 as

the starting node of H1. Now, for H2 we extend H by creating a copy of the children ofvi in H in reverse order and updating δ0

H to δ0H ⊕ ∆H(vi) (recall that the children of a

binomial tree are ordered by decreasing rank). Formally, if childrenH(vi) = u1, . . . , uk, thenVH2 = VH ∪ {u′1, . . . , u′k} where each u′j is a fresh copy of uj such that nextH2(u′j) ∶= u′j−1 for

P. Bourhis, A. Grez, L. Jachiet, and C. Riveros 15

j > 1 and nextH2(u′1) ∶= �. Finally, define v0H2

∶= u′k and δ0H2

∶= δ0H ⊕ ∆H(vi). The reader

can check that H1 and H2 are valid incremental binomial heaps and, furthermore, H1 is Hwithout vi and H2 contains only the children of vi in reverse order. Therefore, to computedeleteMin(H) we return meld(H1,H2). Given that the construction of H1 and H2 takesat most logarithmic time in the size of H (i.e. there is at most a log number of roots orchildren), then the procedure takes logarithmic time. Furthermore, H was never touchedand then the operation is fully-persistent.

For meld(H1,H2), we use the same algorithm as for melding two binomial heaps withtwo modifications that are presented here. For melding two binomial heaps, we point thereader to [6] in which this operation is well explained. For the first change, we need toupdate the link operation [6] of two binomial trees to support the use of the delta priorities.Given a incremental binomial heap H and its underlying multitree structure M , let v1 andv2 be two nodes in H such that ∆(v1) ⪯ ∆(v2) and Mv1 and Mv2 has the same rank k.Then the link of v1 and v2, denoted by link(H,v1, v2), outputs a pair (H ′, v′1) such thatH ′ is an extension of H and M ′

v′1is a binomial tree of rank k + 1 containing the nodes

of Mv1 and Mv2 . Formally, VH′ ∶= VH ∪ {v′1, v′2} and v′1 and v′2 are fresh copies of v1 andv2 such that firstH′(v′1) ∶= v′2, nextH′(v′2) ∶= firstH(v1) and ∆H′(v′2) ∶= ∆H(v1)−1 ⊕∆H(v2).Note that the new node v′1 defines a binomial tree M ′

v′1of rank k + 1 containing all nodes of

Mv1 and Mv2 , maintaining the priorities of H and such that prH′(u) ⪯ prH′(u′) wheneveru = parent(u′). The second change of the algorithm in [6] is that, before melding H1 andH2, we push each initial delta value to the roots of the corresponding data structures.For this, given an incremental binomial heap H we construct H↓ with H ⊆ H↓ as follows.Let rootsH = v1, . . . , vk. Then VH↓ = VH ∪ {v′1, . . . , v′k} where v′1, . . . , v′k are fresh copies ofv1, . . . , vk and ∆H↓(v′i) ∶= δ0

H ⊕∆H(vi). Furthermore, we define v0H↓ ∶= v′1 and δ0

H↓ ∶= O. Notethat in H↓ we can forget about the initial delta value given that this is included in the rootof each binomial tree. Finally, to meld H1 and H2 we construct H↓1 and H↓2 and then applythe melding algorithm of [6] with the updated version of the link function, link(H,v1, v2).Overall, the operation takes logarithmic time to build H↓1 and H↓2, and logarithmic time tomeld both heaps. Moreover, given that link(H,v1, v2) and the construction of H↓1 and H↓2do not modify the initial heap H, then the meld operation is persistent as well.

To finish this section, we recall that the next step is to extend the incremental binomialheap to an incremental Brodal queue. For this, we follow the same approach as in [5] tolower the time complexity of find-min, add, and meld operation from logarithmic to constanttime (see the appendix for further discussion).

7 Conclusions

This paper presented an algorithm to enumerate the answers of queries over words, in anorder defined by a cost function, that has a linear preprocessing and a logarithmic delay inthe size of the words. We first introduced the notion of MSO cost functions, to then presenta ranked enumeration scheme. This scheme relies on a particular data structure called HoW.The complexity of our algorithms depends mainly on the performance of the operations ofHoW. To implement them, we extend a well known persistent data structure called Brodalqueue. Thanks to this data structure, we obtain the bounds of our algorithm.

For future work, we would like to find a lower bound that justifies the logarithmic delayor whether one can achieve a better delay. We also plan to study how the introduceddata structures and algorithms could be used in other enumeration schemes (e.g., relationaldatabases). Finally, we would also like to validate our approach in practical settings.

16 Ranked enumeration of MSO logic on words

References1 Alfred V Aho and John E Hopcroft. The design and analysis of computer algorithms. Pearson

Education India, 1974.2 Antoine Amarilli, Pierre Bourhis, Stefan Mengel, and Matthias Niewerth. Constant-delay

enumeration for nondeterministic document spanners. In ICDT, pages 22:1–22:19, 2019.3 Guillaume Bagan. Mso queries on tree decomposable structures are computable with linear

delay. In International Workshop on Computer Science Logic, pages 167–181. Springer, 2006.4 Christoph Berkholz, Jens Keppeler, and Nicole Schweikardt. Answering conjunctive queries

under updates. In PODS, pages 303–318. ACM, 2017.5 Gerth Stølting Brodal and Chris Okasaki. Optimal purely functional priority queues. Journal

of Functional Programming, 6(6):839–857, 1996.6 Thomas H Cormen, Charles E Leiserson, Ronald L Rivest, and Clifford Stein. Introduction to

algorithms. MIT press, 2009.7 Bruno Courcelle. Linear delay enumeration and monadic second-order logic. Discrete Applied

Mathematics, 157(12):2675–2700, 2009.8 Gianpaolo Cugola and Alessandro Margara. Processing flows of information: From data stream

to complex event processing. ACM Computing Surveys (CSUR), 44(3):1–62, 2012.9 Shaleen Deep and Paraschos Koutris. Ranked enumeration of conjunctive query results. CoRR,

abs/1902.02698, 2019.10 Johannes Doleschal, Noa Bratman, Benny Kimelfeld, and Wim Martens. The complexity of

aggregates over extractions by regular expressions. CoRR, abs/2002.08828, 2020.11 Johannes Doleschal, Benny Kimelfeld, Wim Martens, and Liat Peterfreund. Weight annotation

in information extraction. In ICDT, volume 155, pages 8:1–8:18, 2020.12 James R Driscoll, Neil Sarnak, Daniel Dominic Sleator, and Robert Endre Tarjan. Making

data structures persistent. In STOC, pages 109–121, 1986.13 Manfred Droste and Paul Gastin. Weighted automata and weighted logics. In ICALP, volume

3580, pages 513–525, 2005.14 Manfred Droste, Werner Kuich, and Heiko Vogler. Handbook of weighted automata. Springer

Science & Business Media, 2009.15 Ronald Fagin, Benny Kimelfeld, Frederick Reiss, and Stijn Vansummeren. Document spanners:

A formal approach to information extraction. J. ACM, 62(2):12:1–12:51, 2015.16 Fernando Florenzano, Cristian Riveros, Martín Ugarte, Stijn Vansummeren, and Domagoj

Vrgoc. Efficient enumeration algorithms for regular document spanners. ACM Trans. DatabaseSyst., 45(1):3:1–3:42, 2020.

17 Dominik D Freydenberger, Benny Kimelfeld, and Liat Peterfreund. Joining extractions ofregular expressions. In Proceedings of PODS, pages 137–149, 2018.

18 Markus Frick and Martin Grohe. The complexity of first-order and monadic second-orderlogic revisited. Ann. Pure Appl. Log., 130(1-3):3–31, 2004.

19 Jonathan S Golan. Semirings and their Applications. Springer Science & Business Media,2013.

20 Alejandro Grez, Cristian Riveros, and Martín Ugarte. A Formal Framework for ComplexEvent Processing. In ICDT, pages 5:1–5:18, 2019.

21 Muhammad Idris, Martín Ugarte, Stijn Vansummeren, Hannes Voigt, and Wolfgang Lehner.General dynamic yannakakis: conjunctive queries with theta joins under updates. VLDB J.,29(2):619–653, 2020.

22 Stephan Kreutzer and Cristian Riveros. Quantitative monadic second-order logic. In LICS,pages 113–122, 2013.

23 Leonid Libkin. Elements of finite model theory. Springer Science & Business Media, 2013.24 Francisco Maturana, Cristian Riveros, and Domagoj Vrgoc. Document spanners for extracting

incomplete information: Expressiveness and complexity. In Proceedings of PODS, pages125–136. ACM, 2018.

P. Bourhis, A. Grez, L. Jachiet, and C. Riveros 17

25 EugeneWMyers. An applicative random-access stack. Information processing letters, 17(5):241–248, 1983.

26 Klaus Reinhardt. The complexity of translating logic to finite automata. In Automata logics,and infinite games, pages 231–238. Springer, 2002.

27 Luc Segoufin. Enumerating with constant delay the answers to a query. In Proceedings of the16th International Conference on Database Theory, pages 10–20, 2013.

28 Nikolaos Tziavelis, Deepak Ajwani, Wolfgang Gatterbauer, Mirek Riedewald, and XiaofengYang. Optimal algorithms for ranked enumeration of answers to full conjunctive queries.VLDB, 13(9):1582–1597, 2020.

29 Nikolaos Tziavelis, Wolfgang Gatterbauer, and Mirek Riedewald. Optimal join algorithmsmeet top-k. In SIGMOD, pages 2659–2665. ACM, 2020.

30 Martín Ugarte and Stijn Vansummeren. On the difference between complex event processingand dynamic query evaluation. In AMW, 2018.

18 Ranked enumeration of MSO logic on words

A Applications

In this section we show the application of our main result in two different settings related toMSO logic over words: document spanners and complex event processing.

A.1 Document spannersThe framework of document spanners was proposed in [15] as a formalization of ruled-based information extraction and has attracted a lot of attention both in terms of theformalism [17, 24] and the enumeration problem associated to it [16]. Recently, an extension ofdocument spanners has been proposed to enhance the extraction process with annotations [11].These annotations serve as auxiliary information of the extracted data such as confidence,support, or confidentiality measures. To extend spanners, this framework follows the approachof provenance semiring by annotating the output with elements from a semiring and propagatethe annotations by using the semiring operators. Next we give the core definitions of [11] tostate then the implications of our main results.

We start by defining the central elements of document spanners: documents and spans.Fix a finite alphabet Σ. A document over Σ (or just a document) is a string d = a1 . . . an ∈ Σ∗

and a span is pair s = [i, j⟩ with 1 ≤ i ≤ j ≤ n + 1. A span represents a continuous region of d,whose content is the substring of d from positions i to j − 1. Formally, the content of span[i, j⟩ is defined as d[i, j⟩ = ai . . . aj−1; if i = j, then d[i, i⟩ = ε. Fix a finite set of variables X.A mapping µ over d is a function from X to the spans of d. A document spanner (or justspanner) is a function that transforms each document d into a set of mappings over d.

To annotate mappings, we need to introduce semirings. A semiring (K,⊕,⊙,0,1) isan algebraic structure where K is a non-empty set, ⊕ and ⊙ are binary operations overK, and 0,1 ∈ K. Furthermore, ⊕ and ⊙ are associative, 0 and 1 are the identities of⊕ and ⊙ respectively, ⊕ is commutative, ⊙ distributes over ⊕, and 0 annihilates K (i.e.∀k ∈ K.0 ⊙ k = k ⊙ 0 = 0). We will use ⊕X or ⊙X for the ⊕- or ⊙-operation over allelements in some set X, respectively. An ordered semiring (K,⊕,⊙,0,1,⪯) is a semiringextended with a total order ⪯ over K such that ⪯ preserves ⊕ and ⊙, namely, k1 ⪯ k2implies k1 ∗ k ⪯ k2 ∗ k for ∗ ∈ {⊕,⊙}. From now on, we will assume that all semiringsare ordered. A semifield [19] is a semiring (K,⊕,⊙,0,1) where each k ∈ K ∖ {0} has amultiplicative inverse, i.e. (K ∖ {0},⊙,1) form a group. Examples of ordered semifields arethe tropical semiring (Z ∪ {∞},min,+,∞,0,≤) and the semiring of non-negative rationalnumbers (Q≥0,+,×,0,1,≤).

Fix a semiring (K,⊕,⊙,0,1). Let X be a set of variables and define C(X) = {x ⊢,⊣ x ∣ x ∈ X}. To define spanners with annotations, we use the formalism of weightedvariable set automata [11] which defines the class of all regular spanners with annotations,also called regular annotators. A weighted variable set automaton (wVA) over K is atuple A = (X,Q, δ, I, F ) such that X is a finite set of variables, Q is a finite set of states,δ ∶ Q × (Σ ∪ C(X)) ×Q→K is a weighted transition function and I ∶ Q→K and F ∶ Q→K

are the initial and final weight functions, respectively. A run ρ over a document d = a1⋯anis a sequence of the form:

ρ ∶= (q0, i0) o1ÐÐ→ (q1, i1) o2ÐÐ→ . . . omÐÐ→ (qm, im)

where (1) 1 = i0 ≤ i1 ≤ ⋯ ≤ im = n + 1, (2) each qj ∈ Q with I(q0) ≠ 0 ≠ F (qm), (3)δ(qj , oj+1, qj+1) ≠ 0, and (4) ij+1 = ij if oj+1 ∈ C(X) and ij+1 = ij + 1 otherwise. In addition,we say that a run ρ is valid if for every x ∈ X there exists exactly one index i with oi = x⊢,exactly one index j with oj =⊣ x, and i < j. We denote by RunA(d) the set of all valid

P. Bourhis, A. Grez, L. Jachiet, and C. Riveros 19

runs of A over d. Note that for some wVA A and document d there could exist runs of Aover d that are not valid. We say that A is functional if every run ρ of A over d is valid forevery document d. Given that decision problems associated to non-functional variable-setautomata have been shown to be NP-hard [17, 24], from now on we assume that all wVAare functional.

A valid run ρ like above naturally defines a mapping µρ over X that maps each x to thespan [ij , ij′⟩ where oij = x⊢ and oij′ =⊣x. Furthermore, we can associate a weight in K to ρby multiplying all the weights of the transitions, formally,

W (ρ) ∶= I(q0)⊙m

⊙j=1

δ(qj , oj+1, qj+1)⊙ F (qm)

We define the set of output mappings of A over d as ⟦A⟧(d) = {µρ ∣ ρ ∈ RunA(d)}. Givena mapping µ ∈ ⟦A⟧(d) we associate a weight WA,d(µ) = ⊕ρ∈RunA(d)∶µ=µρW (ρ). Intuitively,each µ ∈ ⟦A⟧(d) contains relevant data extracted by A from d and WA,d(µ) is the additionalinformation attached to µ obtained by A from d in the extraction process, e.g. confidence orsupport.

In [11], the problem of ranked annotator enumeration was proposed, which for the sakeof completeness we present next2:

Problem: RA-ENUMInput: A wVA A over an ordered semiring K

and a document d.Output: Enumerate all µ1, . . . , µk ∈ ⟦A⟧(d)

without repetitions and such thatWA,d(µ1) ⪯WA,d(µi+1).

RA-ENUM was studied in [11] and an enumeration algorithm was provided with polynomialpreprocessing and polynomial delay in terms of ∣A∣ and ∣d∣. By using the framework of MSOcost functions, we can give a better algorithm for a special case of RA-ENUM. We saythat a wVA A is unambiguous if, for every document d and µ ∈ ⟦A⟧(d), there exists at mostone run ρ ∈ RunA(d) such that µ = µρ. Of course, the similarity between cost transducersand wVA is more or less clear, although the former works over groups and wVA works oversemirings. For this reason, we restrict wVA to semifields and give the following result.

I Corollary 6. The problem RA-ENUM can be solved with ∣A∣ ⋅ ∣d∣ preprocessing time andlog(∣A∣ ⋅ ∣d∣)-delay when A is unambiguos and K is an ordered semifield.

Although the previous result is a restricted case of RA-ENUM and a direct consequenceof Theorem 5, to the best of our knowledge this is the first non-trivial ranked enumerationalgorithm proposed for the framework of document spanner.

A.2 Complex event processingIn the dynamic query evaluation [4, 21] setting, the goal is to maintain the result of a queryunder updates to the underlying database. The most used approach is to maintain a datastructure that represents the partial evaluation of the query. Then for each new update,

2 In [11] they considered positively ordered semiring, which is slightly more general that the notion ofordered semiring used here.

20 Ranked enumeration of MSO logic on words

type H T T H H T T T H . . .value 25 25 20 25 40 42 25 70 18 . . .index 1 2 3 4 5 6 7 8 9 . . . q1 q2 q3

P ∣ ●

TRUE ∣ ○ P ′ ∣ ● TRUE ∣ ○

P ∣ ●

Figure 2 At the left, a stream S of events measuring temperature and humidity. “value” containsdegrees and humidity for T - and H- events, respectively. At the right, a complex event automatonwhere P ∶= type[H] and P ′

∶= type[T ] ∧ value > 40.

the data structure is minimally updated (e.g., constant time), and the fresh outputs areenumerated with some strong guarantees in the delay (e.g. constant delay). Complex eventprocessing (CEP) is an orthogonal approach to dynamic query evaluation [30], in whichthe updates are represented by an infinite stream of data items, called events, and wherethe time, represented by the order of the events in the stream, plays a significant role. Forthese reasons, in CEP the stream resembles more an infinite string and the operators of thequery language are closely connected to regular expressions. Moreover, similar techniquesare used for its evaluation, in particular the use of intermediate automata models, and thetechniques developed for constant delay enumeration of MSO naturally apply in this context.We continue this subsection by introducing the main definitions of CEP, to then extend the(offline) ranked enumeration setting to an online version. Towards the end, we give the mainresults of our ranked enumeration algorithm in this context.

Let A be an infinite set of attribute names and D an infinite set of values. A databaseschema R is a finite set of relation names, where each relation name R ∈ R is associatedto a tuple of attributes denoted by att(R). If R is a relation name, then an R-tuple isa function t ∶ att(R) → D and define its type as type(t) = R. For any relation name R,tuples(R) denotes the set of all possible R-tuples. Similarly, tuples(R) = ⋃R∈R tuples(R)for any database schema R. Given R, a unary predicate is a set P ⊆ tuples(R). We denoteby U a finite and fix set of unary predicates over R. We also assume that U contains apredicate type[R] = {t ∣ type(t) = R} for every R ∈R, and U is closed under conjunction (i.e.,P ∩ P ′ ∈ U for every P,P ′ ∈ U). Furthermore, we assume that, for every tuple t and P ∈ U ,verifying if t ∈ P takes constant time in the RAM computational model3.

Fix a schema R. A stream S is just an infinite sequence S = t1t2 . . . where ti ∈ tuples(R).Given a position n, we write Sn to denote the prefix t1 . . . tn of S. In CEP, a tuple ti of S iscalled an event and a finite subset {ti1 , . . . , tik} is called a complex event of S. For the sakeof simplification, in this paper we define a complex event as non-empty finite set of positionsC ⊆ N ∖ {0}. This set C naturally defines the set of events as {ti ∣ i ∈ C}. Finally, we writemin(C) and max(C) for the first and last position in C, respectively.

To define complex events over streams, we use the model of complex event automata [20]which is expressible enough to define all (regular) complex event queries over streams [20]. Fixa schema R and a set of unary predicates U (defined as above). A complex event automaton(CEA) is a tuple A = (Q,∆, I, F ) where Q is a finite set of states, ∆ ⊆ Q × U × {○, ●} ×Q isa finite transition relation, and I,F ⊆ Q are the set of initial and final states, respectively.Intuitively, the elements {○, ●} indicate whether or not the element used to take the transitionwill be part of the output. Given a stream S = t1t2 . . ., a run ρ of A over S of length n

is a sequence of transitions ρ ∶ q0P1/m1ÐÐ→ q1

P2/m2ÐÐ→ ⋯ Pn/mnÐÐ→ qn such that q0 ∈ I, ti ∈ Pi and

3 In other words, we assume that the time cost of verifying if a tuple satisfies a predicate is not relevantfor the time cost of the algorithm.

P. Bourhis, A. Grez, L. Jachiet, and C. Riveros 21

(qi−1, Pi,mi, qi) ∈ ∆ for every i ∈ [1, n]. We say that ρ is accepting if qn ∈ F . We writeRunn(A, S) to denote the set of all accepting runs of A over S of length n. Further, we definethe complex event induced by ρ as Cρ = {i ∈ [1, n] ∣mi = ●}. Given a stream S and n > 0, wedefine the set of complex events of A over S at position n as ⟦A⟧n(S) = {Cρ ∣ ρ ∈ Runn(A, S)}.Finally, we say that A is unambiguous if for every stream S, position n, and C ∈ ⟦A⟧n(S),there exists exactly one run ρ of A over S of length n such that C = Cρ.

I Example 7. Consider the following example from [20] about a network of sensors measuringthe temperature and humidity in a farm. The stream of data is composed by two kinds ofevents: tuples of type T or H whose attributes “value” contain temperature or humidityvalues, respectively, measured by a sensor. The left side of Figure 2 shows an example of astream S generated by this network, where each column represents an event with its type,value, and index in the stream. The right side of Figure 2 shows an example of a CEA A0over the schema R = {T,H}. This CEA captures complex events whose first and last eventsare of type H and the events in-between are of type T with value greater than 40. Note thatthe transitions labeled with ○ have TRUE as predicate, meaning that A0 can decide to skipevents arbitrarily. If we run A0 over S, we can check that the complex events captured atposition 9 are ⟦A⟧9(S) = {{5,6,8,9},{4,6,8,9},{1,6,8,9},{5,6,9}, . . .}.

Given a CEA A and a stream S, the evaluation problem consists in enumerating ⟦A⟧n(S)at each position n. Of course, ranked enumeration can also be applied in this setting. Morespecifically, for any stream S = t1t2 . . . and for each position n we can define a structureSn = ([n],≤, (PS)P ∈U) such that PS(i) is true if, and only if, ti ∈ P . To define the cost ofeach complex event at position n, we can use a weighted MSO formula α(X) over U suchthat the cost of C ∈ ⟦A⟧n(S) is equal to ⟦α⟧(Sn, σ[C]) where σ[C] = σ[X ↦ C]. Then, givena weighted MSO formula α(X) over U we can state the ranked enumeration problem of CEAas follows.

Problem: RANK-ENUM-CEA[α]

Input: A CEA A and a stream S = t1t2 . . ..Output: After reading t1 . . . tn, enumerate all

C1, . . . ,Ck ∈ ⟦A⟧n(S) without repetitions andsuch that ⟦α⟧(Sn, σ[Ci]) ⪯ ⟦α⟧(Sn, σ[Ci+1]).

I Example 8. A complex event C can be placed arbitrarily far from the current positionn in the stream, which might give C less importance. Hence, a useful cost function is tomeasure the distance between the first event in C and the current position n. We can definethis distance with a weighted MSO formula over Z as follows:

α3 ∶= Σ z.[(∃x.x ∈X ∧ x ≤ z)↦ 1]

One can check that ⟦α3⟧(Sn, σ[X ↦ C]) = n − min(C) + 1 for every stream S, position n,and complex event C. Thus, if we enumerate ⟦A⟧n(S) ranked by α3, the complex eventsthat are “closer” to n in the stream will be enumerated first.

Although weighted MSO formulas and ranked enumeration can be naturally adjustedin this context, the guarantees of efficiency of the enumeration process is a bit more subtle.Intuitively, each time that a new event tn arrives, we do not want to preprocess the wholeprefix Sn−1 again to then start the enumeration phase. Given that we already processed

22 Ranked enumeration of MSO logic on words

Sn−1, we would like to keep a data structure D such that, whenever the new event tn arrives,we update D with tn taking time proportional to ∣tn∣, and then enumerate the new outputsthat have been found. More precisely, assume that the stream S is read by calling a specialinstruction yieldS that returns the next element of S each time it is called. Then, we saythat E is a streaming enumeration algorithm for a CEA A over a stream S if E maintains adata structure D in memory such that:1. between any two calls to yieldS , the data structure D is updated with t where t is the

tuple returned by the first of such calls, and2. after calling yieldS n times, the set ⟦A⟧n(S) can be enumerated from D.We assume here that the enumeration phase at position n is exactly the same as theenumeration phase of a (normal) enumeration algorithm (see Section 2). Furthermore, we saythat E has delay g ∶ N2 → N when there exists a constant F such that the delay delayi(A, Sn)between two complex events Ci and Ci+1 in ⟦A⟧n(S) is bounded by F × ∣Ci+1∣ × g(∣A∣, ∣Sn∣).We say that E has update-time f ∶ N2 → N if there exists a constant G such that the numberof instructions that E executes during the update of D with t is bounded by G × f(∣A∣, ∣t∣).In particular, the update of D does not depend on the number of events seen so far (i.e., doesnot depend on Sn). We say that RANK-ENUM-CEA[α] can be solved with update-timef and delay g if there exists a streaming enumeration algorithm E with update-time f anddelay g, that enumerates ⟦A⟧n(S) in increasing order according to α at each position n.

It is important to notice that the existence of an enumeration algorithm for the rankedenumeration problem of MSO does not imply the existence of a streaming enumerationalgorithm. However, shown in Section 4, our ranked enumeration algorithm maintains a datastructure to process the input in such a way that it is updated “one letter at a time” and,at any moment of the preprocessing phase, all outputs until that moment can be efficientlyenumerated. That is, we can derive the following result:

I Theorem 9. The problem RANK-ENUM-CEA[α] can be solved with 2∣A∣ ⋅ ∣t∣ update-timeand log(∣A∣ ⋅ ∣Sn∣)-delay. Furthermore, if A is unambiguous, then RANK-ENUM-CEA[α]can be solved with ∣A∣ ⋅ ∣t∣ update-time and log(∣A∣ ⋅ ∣Sn∣)-delay.

For this result, the preprocessing part of Algorithm 1 has to be modified to call theenumeration procedure after reading each event in the stream, instead of only at the end.

An important property of streams in CEP is that the relevance of events and complexevents rapidly decays over time. To illustrate this, recall the setting of Example 7. Arguably,a user would prefer the complex event C1 = {5,6,8,9} over C2 = {1,6,8,9} because its timeinterval is shorter and closer to the current time 9. For this reason, CEP query languagesusually include time operators that filter out the complex events that are not inside a slidingwindow [8]. Formally, given a CEA A and a number T (encoded in binary) consider thequery Q ∶= A WITHIN T such that, for every stream S and position n, Q defines the setof complex events ⟦A WITHIN T ⟧n(S) = {C ∈ ⟦A⟧n(S) ∣ n − min(C) ≤ T}. In other words,it considers only those C captured by A that are contained within the last T events ofthe stream. Interestingly, we can use Theorem 9 to efficiently evaluate queries of the formA WITHIN T . Indeed, if we evaluate A over a stream S, rank each complex event with thecost function α3 of Example 8, and enumerate all complex events in increasing order up tocost T , we will have enumerated all complex events in ⟦A WITHIN T ⟧n(S). Thus, we easilyget the following corollary for the evaluation of CEP queries over sliding windows.

I Corollary 10. For each CEA A and value T (in binary), A WITHIN T can be evaluated with2∣A∣ ⋅ ∣t∣ update-time and log(∣A∣ ⋅ ∣Sn∣)-delay. Furthermore, if A is restricted to be unambiguous,then it can be evaluated with ∣A∣ ⋅ ∣t∣ update-time and log(∣A∣ ⋅ ∣Sn∣)-delay.

P. Bourhis, A. Grez, L. Jachiet, and C. Riveros 23

It is important to clarify two facts about the previous result. First, the result can beextended to CEP queries over sliding windows when time is continuous (e.g. in seconds) byslightly modifying our evaluation algorithm. Second, the advantage of the previous result isthat the evaluation process does not depend on the length T of the sliding window. Althoughthe length T reduces the number of events that a query needs to “see” for the evaluation,given that T is in binary (or time is continuous), the sliding window could contain a hugenumber of events during the evaluation process.

B Proof of Proposition 4

The following is a proof taken from Theorems 4.1 and 5.3 of [22] and adapted to our setting.Instead of citing the results there, we decided to add the complete proof to keep the paperself-contained. First, lets fix an ordered group (G,⊕,O,⪯).

(⇒) Consider a cost transducer T = (Q,∆, κ, I, F ) and let V be the output alphabet. Wedefine formulas ϕT and αT with free variables V , where the former encodes a run of T andthe latter calculates the cost of such run. In the following, let ∆I be the set of transitionscoming from an initial state and ∆F be the set of transitions going to a final state. Moreover,for every U ∈ V , define the set ∆U = {(p, a, V ′, q) ∈ ∆ ∣ U ∈ V ′} of transitions that output U .

We begin by defining ϕT . First of all, we introduce some auxiliary formulas: first(x) ∶=∀y. x ≤ y; last(x) ∶= ∀y. y ≤ x; succ(x, y) ∶= x ≤ y ∧ y � x ∧ ∀z.(z ≤ x ∨ y ≤ z). These denotethe first element, last element and successor relation, respectively, according to the order ≤of the word structure. We encode a run ρ by defining, for each t = (p, a, V, q) ∈ ∆, a variableXt such that i ∈Xt if the i-th transition of ρ is t. Let X̄ = {Xt ∣ t ∈ ∆}. Now, we define thepredicate run(X̄, V ) that is satisfied only when X̄ defines an accepting run in T such thatits output defines an encoding equal to the assignment of V .

run(X̄, V ) ∶= ∀x. ⋁t∈∆

(x ∈Xt ∧ ⋀t′≠t

x ∉Xt′) ∧

⋀(p,a,U,q)∈∆

∀x.(x ∈X(p,a,U,q) → x ∈ Pa) ∧

∀x.∀y.( succ(x, y) → ⋁(p,a,U,q),(q,b,W,r)∈∆

x ∈X(p,a,U,q) ∧ y ∈X(q,b,W,r)) ∧

∃x.(first(x) ∧ ⋁t∈TI

x ∈Xt) ∧ ∃x.( last(x) ∧ ⋁t∈TF

x ∈Xt)

⋀U∈V

(∀x.x ∈ U ↔ ⋁t∈∆U

x ∈Xt)

The first line makes sure that exactly one transition is used per position; the second makessure that each transition is taken only if the input letter is correct; the third checks that thetransitions form a path; the fourth checks the initial and final states and the last checks thatthe assignment output by the run corresponds to the assignment of V .

Then, we define our first formula:

ϕT (V ) ∶= ∃X̄. run(X̄, V )

Note that ϕT guesses a run and, because T is unambiguous, that run is unique for everyassignment of V . Then it holds that, for every word w, ⟦ϕ⟧(w) = ⟦T ⟧(w).

Now, to define αT , we define cost formulas that capture the costs of the initial state, the

24 Ranked enumeration of MSO logic on words

transitions and the final states:

init(V ) ∶= ⊕(p,a,U,q)∈TI

(∃X̄.( run(X̄, V ) ∧ ∃x.(first(x) ∧ x ∈X(p,a,U,q)))↦ I(p))

trans(x,V ) ∶= ⊕(p,a,U,q)∈T

(∃X̄.( run(X̄, V ) ∧ x ∈X(p,a,U,q))↦ κ((p, a,U, q)))

final(V ) ∶= ⊕(p,a,U,q)∈TF

(∃X̄.( run(X̄, V ) ∧ ∃x.(last(x) ∧ x ∈X(p,a,U,q)))↦ F (q))

The trans formula is supposed to receive a position x of the domain and retrieve the cost ofthe x-th transition of the guessed run. Then, we define our second formula:

αT (V ) ∶= init(V ) ⊕ Σx. trans(x,V ) ⊕ final(V )

It is then easy to check that costT (w,σ) = ⟦αT ⟧(w,σ) for every word w and V -assignment σ.(⇐) Consider an MSO formula ϕ and a weighted MSO formula α. Given an alphabet Σ

and a set of (second and first order) variables V , let ΣV = Σ × 2V be the alphabet used toencode words and assignments. Moreover, by abuse of notation, let (w,σ) ∈ ΣV be the wordencoding w and σ, i.e., (w,σ) = (a1, V1)(a2, V2) . . . (an, Vn), where a1 . . . an = w and X ∈ Viiff i ∈ σ(X). The following Lemma is a well-known result that relates the expressibility ofMSO and DFA.

I Lemma 11. For every MSO formula ψ over Σ with free variables V there exists a DFAAψ over ΣV such that for every word w ∈ Σ∗ and assignment σ we have:

(w,σ) � ψ(V ) iff (w,σ) ∈ L(Aψ)

where L(A) is the set of words accepted by the DFA A. Therefore, regarding the MSOformula ϕ, because of Lemma 11 we know there exists a DFA Aϕ such that (w,σ) � ϕ(V )iff (w,σ) ∈ L(Aϕ). Next, we build a cost transducer Tα for α, then make the cross-productof it with Aϕ and obtain the final cost transducer.

In particular, the cost transducer Tα = (Qα,∆α, κα, Iα, Fα) is equivalent to α in the sensethat it satisfies ⟦α⟧(w,σ) = costTα(w,σ) for every word w and assignment σ. Moreover, ifal subformulas of α are of the form ϕ ↦ g or α1 ⊕ α2, then Tα is deterministic, meaningthat it has a single accepting state q0 ∈ dom(Iα) and, for every p ∈ Qα, a ∈ Σ and V ′ ⊆ Vthere is at most one transition (p, a, V ′, q) ∈ ∆α. Usually, we use notation T = (Q, δ, κ, I, F )to denote that T is deterministic. Otherwise, if α also contains operation Σ .x, then Tα isunambiguous.

We build Tα by induction over the structure of α. The base case is when α = [ϕ′ ↦ g],with ϕ′ being an MSO formula and g ∈ G. Consider the DFA Aϕ′ = (Q′, δ′, q′0, F

′) given byLemma 11. Then, we build Tα = (Q′, δα, κα, Iα, Fα) such that:

δα = {(p, a, V ′, q) ∣ (p, (a, V ′), q) ∈ δ′},κα(t) = O for every t ∈ δα,Iα(q′0) = O, andFα(q) = g for every q ∈ F ′.

It is not hard to see that T is deterministic and that for every word w and assignment σ,⟦α⟧(w,σ) = costTα(w,σ).

Now, we consider the case where α = α1 ⊕ α2. By induction, consider that there arecost transducers Ti = (Qi,∆i, κi, Ii, Fi) equivalent to αi, for i ∈ {1,2}. Then, we buildTα = (Qα,∆α, κα, Iα, Fα) as the cross product of T1 and T2 in the following way:

Qα = Q1 ×Q2,

P. Bourhis, A. Grez, L. Jachiet, and C. Riveros 25

∆α = {((p1, p2), a, V ′, (q1, q2)) ∣ (p1, a, V′, q1) ∈ ∆1 ∧ (p2, a, V

′, q2) ∈ ∆2},κα(((p1, p2), a, V ′, (q1, q2))) = κ1(p1, a, V

′, q1)⊕ κ2(p2, a, V′, q2),

Iα((q1, q2)) = O for all q1 ∈ dom(I1) and q2 ∈ dom(I2), andFα((q1, q2)) = F1(q1)⊕ F2(q2) for all q1 ∈ dom(F1) and q2 ∈ dom(F2).

Again, for every word w and assignment σ, ⟦α⟧(w,σ) = costTα(w,σ). Moreover, if T1 and T2are deterministic (unambiguous), then Tα is deterministic (unambiguous, respectively).

Finally, we consider the case where α = Σx.α′. Let T ′ = (Q′, δ′, κ′, I ′, F ′) be the costtransducer equivalent to α′. Because there are no nested Σx operators, we know that T ′ isdeterministic. Moreover, because of the construction, for every run ρ ending in some stateqf ∈ dom(F ′), all costs are added by the final state, i.e. κ′(t) = O for all t ∈ δ′, I(q) = O forall q ∈ dom(I), and κ(ρ) = F (qf). We make use of this property in the following construction.For simplicity, assume that V = {x}, so that transitions of T ′ either have the form (p, a,{x}, q)or (p, a,∅, q); we call the latter ∅-transitions. Next, we define Tα = (Qα,∆α, κα, Iα, Fα)equivalent to α:

Qα = Q′ × 2Q′×Q′

,Iα(q,∅) = O for all q ∈ dom(I ′)Fα(q,R) = O if R has the form R = {(q1, q1), . . . , (qk, qk)},t = ((q1,R1), a,∅, (q2,R2)) ∈ ∆α and κ(t) = s if there exists qf ∈ Q′ such thatF ′(qf) = s(q1, a,∅, q2) ∈ δ′, andR2 = {(p2, q

′f) ∈ Q2 ∣ ∃(p1, q

′f) ∈ R1.(p1, a,∅, p2) ∈ δ′} ∪ {(q, qf) ∣ (q1, a,{x}, q) ∈ δ′}.

The motivation behind this construction is that, for each (q,R) ∈ Qα, q keeps the state of the(only) run of T ′ considering that x has not been assigned, while R keeps pairs (p, qf) suchthat p follows a run that has already assigned x and qf is the “guessed” final state of suchrun. Moreover, every time a final state is guessed, its value F (qf) is added to the cost of therun. The guess then is verified by checking that each final state is reached as expected.

To check that Tα is unambiguous, consider by contradiction that there are two acceptingruns

ρi = (q0,∅) a1/∅ÐÐ→(q1, Ti1)a2/∅ÐÐ→⋯an/∅ÐÐ→(qn, T in)

for i ∈ {1,2}. Then, let j ≤ n be the smallest position at which both runs differ, namelyT 1j ≠ T 2

j and Tj−1 = T 1j−1 = T 2

j−1. By looking at the definition of T 1j and T 2

j , we cansee that both have the same set in the first part, defined completely by Tj−1, chich isS = {(p2, q

′f) ∈ Q2 ∣ ∃(p1, q

′f) ∈ Tj−1.(p1, a,∅, p2) ∈ δ′}). For the second part, let q1

f and q2f be

the final states guessed by the transitions in ρ1 and ρ2. Then, for i ∈ {0, 1}, T ij = S ∪{(q, qif) ∣(qj−1, aj ,{x}, q) ∈ δ′}. Because T ′ is deterministic, the only way for the sets to differ is ifq1f ≠ q2

f . Now, let p ∈ T ′ be the state reached from q after reading the remaining suffix of w,i.e. w[j + 1] . . .w[n], using only ∅-transitions. From the construction, (p, q1

f) must be in T 1n

and (p, q2f) must be in T 2

n . Moreover, because ρ1 and ρ2 are accepting, it must hold thatq1f = p = q2

f , reaching a contradiction.Now, consider a word w = a1 . . . an and the only accepting run

ρ = (q0,∅) a1/∅ÐÐ→s1

(q1, T1) a2/∅ÐÐ→s2

⋯an/∅ÐÐ→sn

(qn, Tn)

where each si is the cost of the i-th transition. It is not hard to see that, for every i ∈ [n],si = ⟦α′⟧(w,σ[x→ i]) and, therefore,

⟦α⟧(w,σ) =n

∑i=1si = costT (w,σ)

26 Ranked enumeration of MSO logic on words

Now that we have a CT Tα = (Qα,∆α, κα, Iα, Fα) and a DFA Aϕ = (Qϕ, δϕ, κϕ, qϕ0 , Fϕ)(over words in ΣV ), we build our final CT T = (Q,∆, κ, I, F ) by doing the cross-product ofboth as follows:

Q = Qα ×Qϕ∆ = {((p1, p2), a, V ′, (q1, q2)) ∣ (p1, a, V

′, q1) ∈ ∆α ∧ (p2, (a, V ′), q2) ∈ δϕ}κ(((p1, p2), a, V ′, (q1, q2))) = κα((p1, a, V

′, q1))I(q1, q

ϕ0 ) = Iα(q1) for all q1 ∈ dom(Iα)

F (q1, q2) = Fα(q1) for all (q1, q2) ∈ dom(Fα) × FϕFrom the above, one can check that ⟦ϕ⟧ = ⟦T ⟧ and ⟦α⟧(w,σ) = costT (w,σ) for every wordw and assignment σ ∈ ⟦ϕ⟧(w).

C Complexity of Heap of Words operations

We recall Meld and IncreaseBy are implemented using their incremental Brodal queueequivalent and thus inherit the constant time complexity of those. We now recall theimplementation of the other operations on HoW:

Algorithm 3 HoW’s implementation of Add, ExtendBy, FindMin and DeleteMin.1: procedure Add(⟨Q⟩, [a ∶g])2: return ⟨add(Q, [(a, ⟨∅⟩) ∶g])⟩

3: procedure ExtendBy(⟨Q⟩, a)4: if isEmpty(Q) then5: return ⟨∅⟩6: return ⟨add(∅, [(a, ⟨Q⟩) ∶minPrio(Q)])⟩

7: procedure FindMin(⟨Q⟩)8: (a, ⟨Q′⟩)← findMin(Q)9: if isEmpty(Q′) then

10: return a

11: return FindMin(⟨Q′⟩) ⋅ a

12: procedure DeleteMin(⟨Q⟩)13: if isEmpty(Q) then14: return ⟨∅⟩15: (a, ⟨R⟩)← findMin(Q)16: Q′ ← deleteMin(Q)17: ⟨R′⟩←DeleteMin(⟨R⟩)18: if isEmpty(R′) then19: return ⟨Q′⟩20: δ ← minPrio(R′)⊕ (minPrio(R))−1

21: g ← minPrio(Q)⊕ δ22: return ⟨add(Q′, [(a, ⟨R′⟩) ∶g])⟩

As we see the functions Add and ExtendBy makes a constant number of calls to theconstant time functions add, isEmpty and minPrio and thus they are constant time.

The function FindMin is recursive but it will make one recursive call for each letterin the output and at each recursive step it will make one call to findMin and to isEmpty,therefore the overall complexity of FindMin is linear in the returned word. Notice that weuse the ⋅ operator (to denote concatenation) on strings and we suppose that it takes constanttime. For this, we can encode strings as lists of individual letters in reversed order and thusappending a letter at the end of the word corresponds to appending at the beginning of alist and this takes constant time.

Let us now dive into the complexity of DeleteMin. We claimed that the complexityof DeleteMin(h) was O(∣w∣ × log(n)) where w = FindMin(h) and n is the number ofoperations that were used to build h without counting the DeleteMin (these DeleteMinshould only happen at the enumeration phase, hence after all the Add, IncreaseBy, Meldand ExtendBy). The complexity of DeleteMin is dominated by the calls to deleteMin,and there are ∣w∣ such calls. We thus need to prove that each of these deleteMin takesO(log(n)) time.

The branching factor of a HoW ⟨Q⟩, noted branch(⟨Q⟩), is recursively defined as themaximum between the number of elements in Q and the branching factors of HoW contained

P. Bourhis, A. Grez, L. Jachiet, and C. Riveros 27

in Q. By definition, any deleteMin operation triggered by DeleteMin(h) will take a timeat msot logarithmic in branch(h), thus it suffices to prove that branch(h) ≤ n

Let h0, h1, . . . , hk be a sequence of HoWs such that h0 = ∅ and each hi is the result of oneof the functions Add, IncreaseBy, ExtendBy, or Meld applied over any of the previousHoWs h0, . . . , hi−1. We will prove that for all i the queue hi contains less than i elements.Let us introduce Ei as the set of pairs (⟨Q⟩, a) for which there exists at least one g suchthat [(a, ⟨Q⟩) ∶g] belongs to one of the queues h0, . . . , hi−1. When hi is built with Add orExtendBy we have that ∣Ei+1 ∖Ei∣ ≤ 1, when hi is built with Meld or IncreaseBy wehave Ei+1 = Ei, all in all, we get ∣Ei∣ ≤ i. Now, clearly the queue hi contains less than ∣Ei∣elements and it is easy to see that branch(hn) is bounded by maxi≤n(∣hi∣) where ∣hi∣ is thenumber of elements in the queue of hn (queues that are not top queues are created by anExtendBy operation and never modified). Thus, we do have the expected branch(hn) ≤ nfor an hn built without using any DeleteMin.

When applying DeleteMin the branching factor can only decrease: DeleteMin(h)might add an edge to some of the queues in h but only after removing one in those queues.Therefore if h is obtained by using n operations Add, IncreaseBy, Meld and ExtendByfollowed by k operations DeleteMin, its branching factor is bounded by n. And that givesus the complexities requiered in Section 4.

D Building incremental Brodal queues

Here we continue the work of Section 6 and present the complete incremental Brodal queue.In the following, we use a similar approach as the one used in [5]: we start with theincremental binomial heap, which supports most operations in O(logn); then, we explaineach modification made to this structure until we have built the final structure.

D.1 Skew incremental binomial heapWe now explain how the time of add can be reduced toO(1), while maintaining the asymptotictime of the other operations. The technique is borrowed from [5] and modified to handleincreaseBy efficiently.

The motivation comes from the skew binary numbers [25], in particular the canonical skewbinary numbers, a variation of binary numbers in which all digits are 0 or 1 with the possibleexception of the lowest order non-zero digit, which might be 2. A skew binary numberα = αnαn−1 . . . α1 denotes the integer value ∑ni=1 ai(2i−1). This number representation avoidsthe carry cascading when adding 1 to a number. For example, the number 43 is representedby the skew binary number 10112, and adding 1 to it results in 10120, which is done with asingle carry operation.

A skew binomial tree, or skew tree, is a tree with the following definition:a skew tree of rank 0 is a leaf.a skew tree of rank r + 1 is formed in one of three ways:

a simple link, making a skew tree of rank r the leftmost child of another skew tree ofrank r;a type A link, making two skew trees of rank r the children of a skew tree of rank 0; ora type B link, making a skew tree of rank 0 and a skew tree of rank r the leftmostchildren of another skew tree of rank r.

Note that, unlike binomial trees, now a skew binomial tree of rank r has a less rigid structureand, in particular, the number of contained nodes n is not fixed. However, is not hard to see

28 Ranked enumeration of MSO logic on words

that it is bounded by 2r ≤ n ≤ 2r+1 −1. Moreover, given a tree T with root vT and rank(T) = r,the number of children of vT is proportional to r, and therefore children(vT) is O(log ∣VvT ∣).

A skew incremental binomial heap (skew heap for short) is defined as a tuple H =(V,first,next, v0,∆, elem, δ0), where all components are the same as for incremental binomialheaps, except that if roots = v1, . . . , vn are its roots, then each Mvi is an incremental skewbinomial tree with rank(Mvi) < rank(Mvi+1) for each 2 < i < n, and rank(Mv1) ≤ rank(Mv2).Namely, we allow the two smallest trees to have the same rank.

Consider a skew heap H = (V,first,next, v0,∆, elem, δ0) with roots = v1, . . . , vn. FunctionsisEmpty, increaseBy, findMin, minPrio and meld remain the same as for regular heaps.

For add(H,e, g), we create a new skew heap H ′ with H ⊆H ′ as follows. Let v be a freshnode with ∆H′(v) ∶= g⊕∆−1 and elemH′(v) ∶= e. If all trees Mvi have different rank, then weadd v at the beginning of the roots, namely v is made the root of H ′ and nextH′(v) = v0. Ifthere are two trees Mv1 , Mv2 with the same rank, we link v with v1 and v2 to form a tree ofrank r + 1 as follows. If ∆(v) < ∆(v1) and ∆(v) < ∆(v2), then, we make copies v′1, v′2 of v1and v2 with their priorities decreased by ∆H′(v), i.e. ∆H′(vi) = ∆H(vi)⊕∆H′(v)−1, makethem the children of v and make v the root of H ′ with nextH′(v) = nextH(v2). Otherwise,w.l.o.g. consider that ∆(v1) < ∆(v2). Then, we make copies v′1, v′2 of v1 and v2 and make v′2and v the leftmost children of v′1, but decreasing their priorities by ∆(v′1) in the same way.Note that the first case represents an A link and the latter a B link, and that the new treeis a valid skew tree of rank r + 1 that stores the same elements as v1 and v2, plus the newelement v. Moreover, since we allow two repeated ranks and we know that there is at mostone other tree of rank r + 1, adding it does not require a chain of links and thus takes timeO(1).

The case of deleteMin follows the same principle: we make a skew heap H1 to store copiesof the roots except for the minimal one, call it vi, make a skew heap H2 to store the childrenof vi, and build the result with meld(H1,H2). However, building H2 requires a little morework. Since M(vi) is a skew heap, among their children could appear up to logn skew heapsof rank 0. Then, we build H2 by taking the children with rank higher than 0, and then addthe remaining ones using add, taking time O(logn). Finally, we return meld(H1,H2), takingoverall time O(logn).

D.2 Incremental Brodal queue

Clearly, this structure alone is not enough for what we need, since most operations still taketime O(logn). To address this, we base one more time on the techniques of [5], this time onone called “bootstrapping”.

An incremental Brodal queue (queue for short) is either the empty queue Q∅ or a pairQ = ((e, p),H), where e is a stored element, p is its priority and H is a skew heap thatstores incremental Brodal queues. Given a queue Q, we denote its components with thesubscript Q, e.g., eQ, pQ, HQ. Consider some (Q,p) stored in H. Note that, whenever H isapplied an increaseBy operation, p is increased but the priorities inside Q are not; instead,we increase them only when (Q,p) is retrieved from H. We use this setting because theincreaseBy operation is able to increase priorities stored in H efficiently, but increasing allpriorities inside Q might result in an unbounded recursion. Priority p is then going to storethe real updated value of pQ considering how much the priorities of Q have been increasedby calling increaseBy over H. Note that p can be seen as having the form pQ ⊕ d for somed ∈ G that represents such increase and, thus, d = p−1

Q ⊕ p. Consequently, each priority in Qactually represents a priority p⊕d. The set of elements stored in a skew heap H and a queue

P. Bourhis, A. Grez, L. Jachiet, and C. Riveros 29

Q are defined accordingly by the following two-level recursive definition:

⟦H⟧ = ⋃(Q,p)∈[H]

{⟦Q⟧⊕(p−1Q⊕p)}

⟦Q⟧ = {(eQ, pQ)} ∪ ⟦H⟧

where ⟦Q⟧⊕d = {(e, p ⊕ d) ∣ (e, p) ∈ ⟦Q⟧} represents that all priorities of ⟦Q⟧ are increasedby d. This recursive definition just says that the ⟦Q⟧ consists of all elements stored in Q,including the ones at other queues inside Q. Moreover, Q is kept so that (eQ, pQ) is alwaysthe element with minimum priority of ⟦Q⟧.

Consider a queue Q = ((eQ, pQ),HQ), and let P = ((eP , pP ),HP ) be the resulting queueafter applying each operation over Q. Then, Q implements the same operations as a heap:

isEmpty(Q). It only checks if Q is equal to the empty queue Q∅ and returns accordingly.add(Q, [e′ ∶p′]). First, if Q is empty, we initialize a new queue P with (eP , pP ) = (e′, p′)and HP = H∅, where H∅ is the empty heap. Otherwise, if p′ < pQ, then e′ is the newminimum, so we set (eP , pP ) = (e′, p′), otherwise we set it to (eQ, pQ). In either case, weset HP =HQ. W.l.o.g., let (e′, p′) be the one that is not the minimum. In order to add(e′, p′) to HP , we create a new queue Q′ = ((e′, p′),H∅), and then add Q′ to HP withadd(HP , [Q′ ∶p′]).findMin(Q) and minPrio(Q). We just return eQ or pQ, respectively, and keep P = Q.deleteMin(Q). It is easy to find (eQ, pQ), but to delete it we need to replace it with the nextminimum from HQ afterwards. We find it by running R = findMin(HQ), u = minPrio(HQ),and then delete it with HP = deleteMin(HQ). Consider that the queue R has the form((eR, pR),HR) and recall that u stores the updated value of pR. Then, the minimumelement is (eR, u), so we set that as our new (eP , pP ). Notice that we removed queue R,meaning that we not only removed the minimum element, but also all elements stored inHR. In order to add them again, we first increase the priorities in HR by (p−1

R ⊕ u) andthen we meld it with HP , by doing HP = meld(increaseBy(HR, p

−1R ⊕ u),HP ).

meld(Q,R). Let queue R have the form ((eR, pR),HR). First, we select between (eQ, pQ)and (eR, pR) the one with the lowest priority; assume w.l.o.g. that it is (eQ, pQ). Then,we set (eP , pP ) = (eQ, pQ) and HP = add(HQ, [R ∶pR]).increaseBy(Q,g). We set eP = eQ, pP = pQ ⊕ g and HP = increaseBy(HQ, g), that is, justincrease pQ by g and apply increaseBy(H,g). Recall that the latter increases only thepriorities stored directly in HQ, namely each (Q′, p′) in HQ turns into (Q′, p′ ⊕ g) in HP .The priorities of elements inside Q′ remain unchanged, and are updated later when Q′ isretrieved from HP .

Again, the structure is fully persistent because none of the operations modify Q. It is nothard to see that now operations add, findMin, meld and increaseBy take time O(1), anddeleteMin takes time O(logn).