Automata theory. An algorithmic approach. Lecture Notes. Javier Esparza

Size: px
Start display at page:

Download "Automata theory. An algorithmic approach. Lecture Notes. Javier Esparza"

Transcription

1 Automt theory An lgorithmic pproch 0 Lecture Notes Jvier Esprz My 3, 2016

2 2

3 3 Plese red this! Mny yers go I don t wnt to sy how mny, it s depressing I tught course on the utomt-theoretic pproch to model checking t the Technicl University of Munich, sing it on lectures notes for nother course on the sme topic tht Moshe Vrdi hd recently tught in Isrel. Between my lectures I extended nd polished the notes, nd sent them to Moshe. At tht time he nd Orn Kupfermn were thinking of writing ook, nd the ide cme up of doing it together. We mde some progress, ut life nd other work got in the wy, nd the project hs een postponed so mny times tht it I don t dre to predict completion dte. Some of the work tht got in the wy ws the stndrd course on utomt theory in Munich, which I hd to tech severl times. The syllus contined oth utomt on finite nd infinite words, nd for the ltter I used our notes. Ech time I hd to tech the course gin, I took the opportunity to dd some new mteril out utomt on finite words, which lso required to reshpe the chpters on infinite words, nd the notes kept growing nd evolving. Now they ve reched the point where they re in sufficiently good shpe to e shown not only to my students, ut to lrger udience. So, fter getting Orn nd Moshe s very kind permission, I ve decided to mke them ville here. Despite severl ttempts I hven t yet convinced Orn nd Moshe to pper s co-uthors of the notes. But I don t give up: prt from the mteril we wrote together, their influence on the rest is much lrger thn they think. Actully, my secret hope is tht fter they see this mteril in my home pge we ll finlly mnge to gther some morsels of time here nd there nd finish our joint project. If you think we should do so, tell us! Send n emil to: [email protected], [email protected], nd [email protected]. Sources I hven t yet compiled creful list of the sources I ve used, ut I m listing here the min ones. I pologize in dvnce for ny omissions. The chpter on utomt for fixed-length lnguges ( Finite Universes ) ws very influenced y Henrik Reif Andersen s eutiful introduction to Binry Decision Digrms, ville t The short chpter on pttern mtching is influenced y Dvid Eppstein s lecture notes for his course on Design nd Anlysis of Algorithms, see eppstein/tech.html. As mentioned ove, the chpters on opertions for Büchi utomt nd pplictions to verifiction re hevily sed on notes y Orn Kupfermn nd Moshe Vrdi. The chpter on the emptiness prolem for Büchi utomt is sed on severl reserch ppers:

4 4 Jen-Michel Couvreur: On-the-Fly Verifiction of Liner Temporl Logic. World Congress on Forml Methods 1999: Jen-Michel Couvreur, Alexndre Duret-Lutz, Denis Poitrenud: On-the-Fly Emptiness Checks for Generlized Bchi Automt. SPIN 2005: Kthi Fisler, Rnn Frer, Gil Kmhi, Moshe Y. Vrdi, Zijing Yng: Is There Best Symolic Cycle-Detection Algorithm? TACAS 2001: Jco Geldenhuys, Antti Vlmri: More efficient on-the-fly LTL verifiction with Trjn s lgorithm. Theor. Comput. Sci. (TCS) 345(1):60-82 (2005) Stefn Schwoon, Jvier Esprz: A Note on On-the-Fly Verifiction Algorithms. TACAS 2005: The chpter on Liner Arithmetic is hevily sed on the work of Bernrd Boigelot, Pierre Wolper, nd their co-uthors, in prticulr the pper An effective decision procedure for liner rithmetic over the integers nd rels, pulished in ACM. Trns. Comput. Logic 6(3) in Acknowledgments First of ll, thnks to Orn Kupfermn nd Moshe Vrdi for ll the resons explined ove (if you hven t red the section Plese red this yet, plese do it now!). Mny thnks to Jörg Kreiker, Jn Kretinsky, nd Michel Luttenerger for mny discussions on the topic of this notes, nd for their contriutions to severl chpters. All three of them helped me to tech the utomt course of different ocssions. In prticulr, Jn contriuted lot to the chpter on pttern mtching. Breno Fri helped to drw mny figures. He ws funded y progrm of the Computer Science Deprtment Technicl University of Munich. Thnks lso to Fio Bove, Birgit Engelmnn, Moritz Fuchs, Stefn Krusche, Philipp Müller, Mrtin Perzl, Mrcel Ruegenerg, Frnz Sller, Hyk Shoukourin, nd Dniel Weißuer, who provided very helpful comments.

5 Contents 1 Introduction nd Outline Outline I Automt on Finite Words 15 2 Automt Clsses nd Conversions Regulr expressions: lnguge to descrie lnguges Automt clsses Using DFAs s dt structures Conversion Algorithms etween Finite Automt From NFA to DFA From NFA-ε to NFA Conversion lgorithms etween regulr expressions nd utomt From regulr expressions to NFA-ε s From NFA-ε s to regulr expressions A Tour of Conversions Minimiztion nd Reduction Miniml DFAs Minimizing DFAs Computing the lnguge prtition Quotienting Hopcroft s lgorithm Reducing NFAs The reduction lgorithm A Chrcteriztion of the Regulr Lnguges Opertions on Sets: Implementtions Implementtion on DFAs Memership

6 6 CONTENTS Complement Binry Boolen Opertions Emptiness Universlity Inclusion Equlity Implementtion on NFAs Memership Complement Union nd intersection Emptiness nd Universlity Inclusion nd Equlity Applictions I: Pttern mtching The generl cse The word cse Lzy DFAs Opertions on Reltions: Implementtions Encodings Trnsducers nd Regulr Reltions Implementing Opertions on Reltions Projection Join, Post, nd Pre Reltions of Higher Arity Finite Universes Fixed-length Lnguges nd the Mster Automton A Dt Structure for Fixed-length Lnguges Opertions on fixed-length lnguges Determiniztion nd Minimiztion Opertions on Fixed-length Reltions Decision Digrms Decision Digrms nd Kernels Opertions on Kernels Applictions II: Verifiction The Automt-Theoretic Approch to Verifiction Progrms s Networks of Automt Prllel Composition Asynchonous Product

7 CONTENTS Stte- nd event-sed properties Concurrent Progrms Expressing nd Checking Properties Coping with the Stte-Explosion Prolem On-the-fly verifiction Compositionl Verifiction Symolic Stte-spce Explortion Sfety nd Liveness Properties Automt nd Logic First-Order Logic on Words Expressive power of FO(Σ) Mondic Second-Order Logic on Words Expressive power of MSO(Σ) Applictions III: Presurger Arithmetic Syntx nd Semntics An NFA for the Solutions over the Nturls Equtions An NFA for the Solutions over the Integers Equtions Algorithms II Automt on Infinite Words Clsses of ω-automt nd Conversions ω-lnguges nd ω-regulr expressions Büchi utomt From ω-regulr expressions to NBAs nd ck Non-equivlence of NBA nd DBA Generlized Büchi utomt Other clsses of ω-utomt Co-Büchi Automt Muller utomt Rin utomt Boolen opertions: Implementtions Union nd intersection Complement The prolems of complement Rnkings nd rnking levels

8 8 CONTENTS A (possily infinite) complement utomton The size of A Emptiness check: Implementtions Algorithms sed on depth-first serch The nested-dfs lgorithm The two-stck lgorithm Algorithms sed on redth-first serch Emerson-Lei s lgorithm A Modified Emerson-Lei s lgorithm Compring the lgorithms Applictions I: Verifiction nd Temporl Logic Automt-Bsed Verifiction of Liveness Properties Checking Liveness Properties Liner Temporl Logic From LTL formuls to generlized Büchi utomt Stisfction sequences nd Hintikk sequences Constructing the NGA for n LTL formul Size of the NGA Automtic Verifiction of LTL Formuls Applictions II: Mondic Second-Order Logic nd Liner Arithmetic Mondic Second-Order Logic on ω-words Expressive power of MSO(Σ) on ω-words Liner Arithmetic Encoding Rel Numers Constructing n NBA for the Rel Solutions A NBA for the Solutions of x F β Solutions to exercises 307

9 Why this ook? There re excellent textooks on utomt theory, rnging from course ooks for undergrdutes to reserch monogrphies for specilists. Why nother one? During the lte 1960s nd erly 1970s the min ppliction of utomt theory ws the development of lexicogrphic nlyzers, prsers, nd compilers. Anlyzers nd prsers determine whether n input string conforms to given syntx, while compilers trnsform strings conforming to syntx into equivlent strings conforming to nother. With these pplictions in mind, it is nturl to look t utomt s strct mchines tht ccept, reject, or trnsform input strings, nd this view deply influenced the textook presenttion of utomt theory. Results out the expressive power of mchines, equivlences etween models, nd closure properties, received much ttention, while constructions on utomt, like the powerset or product construction, often plyed suordinte rôle s proof tools. To give simple exmple, in mny textooks of the time nd in lter textooks written in the sme style the product construction is not introduced s n lgorithm tht, given two NFAs recognizing lnguges L 1 nd L 2, constructs third NFA recognizing their intersection L 1 L 2. Insted, the text contins theorem stting tht regulr lnguges re closed under intersection, nd the product construction is hidden in its proof. Moreover, it is not presented s n lgorithm, ut s the mthemticl, sttic definition of the sets of sttes, trnsition reltion, etc. of the product utomton. Sometimes, the simple ut computtionlly importnt fct tht only sttes rechle from the initil stte need e constructed is not even mentioned. I clim tht this presenttion style, summrized y the slogn utomt s strct mchines, is no longer dequte. In the second hlf of the 1980s nd in the 1990s progrm verifiction emerged s new nd exciting ppliction of utomt theory. Automt were used to descrie the ehviour or intended ehviour of hrdwre nd softwre systems, not their syntx, nd this shift from syntx to semntics hd importnt consequences. While utomt for lexicl or syntcticl nlysis typiclly hve t most some thousnds of sttes, utomt for semntic descriptions cn esily hve tens of millions. In order to hndle utomt of this size it ecme impertive to py specil ttention to efficient constructions nd lgorithmic issues, nd reserch in this direction mde gret progress. Moreover, utomt on infinite words, clss of utomt models originlly introduced in the 60s to solve strct prolems in logic, ecme necessry to specify nd verify liveness properties of softwre. These utomt run over words of infinite length, nd so they cn hrdly e seen s mchines ccepting or rejecting n input: they could only do so fter infinite time! 9

10 10 CONTENTS This ook intends to reflect the evolution of utomt theory. Modern utomt theory puts more emphsis on lgorithmic questions, nd less on expressivity. This chnge of focus is cptured y the new slogn utomt s dt structures. Just s hsh tles nd Fioncci heps re oth dequte dt structures for representing sets depending when the opertions one needs re those of dictionry or priority queue, utomt re the right dt structure for represent sets nd reltions when the required opertions re union, intersection, complement, projections nd joins. In this view the lgorithmic implementtion of the opertions gets the limelight, nd, s consequence, they constitute the spine of this ook. The shpe of the ook is lso very influenced y two further design decisions. First, experience tells tht utomt-theoretic constructions re est explined y mens of exmples, nd tht exmples re est presented with the help of pictures. Automt on words re lessed with grphicl representtion of instntneous ppel. We hve invested much effort into finding illustrtive, nontrivil exmples whose grphicl representtion stillfits in one pge. Second, for students lerning directly from ook, solved exercises re lessing, n esy wy to evlute progress. Moreover, thy cn lso e used to introduce topics tht, for expository resons, cnnot e presented in the min text. The ook contins lrge numer of solved exercises rnging from simple pplictions of lgorithms to reltively involved proofs.

11 Chpter 1 Introduction nd Outline Courses on dt structures show how to represent sets of ojects in computer so tht opertions like insertion, deletion, lookup, nd mny others cn e efficiently implemented. Typicl representtions re hsh tles, serch trees, or heps. These lecture notes lso del with the prolem of representing nd mnipulting sets, ut with respect to different set of opertions: the oolen opertions of set theory (union, intersection, nd complement with respect to some universe set), some tests tht check sic properties (if set is empty, if it contins ll elements of the universe, or if it is contined in nother one), nd opertions on reltions. Tle 1.1 formlly defines the opertions to e supported, where U denotes some universe of ojects, X, Y re susets of U, x is n element of U, nd R, S U U re inry reltions on U: Oserve tht mny other opertions, for exmple set difference, cn e reduced to the ones ove. Similrly, opertions on n-ry reltions for n 3 cn e reduced to opertions on inry reltions. An importnt point is tht we re not only interested on finite sets, we wish to hve dt structure le to del with infinite sets over some infinite universe. However, simple crdinlity rgument shows tht no dt structure cn provide finite representtions of ll infinite sets: n infinite universe hs uncountly mny susets, ut every dt structure mpping sets to finite representtions only hs countly mny instnces. (Loosely speking, there re more sets to e represented thn representtions ville.) Becuse of this limittion every good dt structure for infinite sets must find resonle compromise etween expressiility (how lrge is the set of representle sets) nd mnipulility (which opertions cn e crried out, nd t which cost). These notes present the compromise offered y word utomt, which, s shown y 50 yers of reserch on the theory of forml lnguges, is the est one ville for most purposes. Word utomt, or just utomt, represent nd mnipulte sets whose elements re encoded s words, i.e., s sequences of letters over n lphet 1. Any kind of oject cn e represented y word, t lest in principle. Nturl numers, for 1 There re generliztions of word utomt in which ojects re encoded s trees. The theory of tree utomt is lso very well developed, ut not the suject of these notes. So we shorten word utomton to just utomton. 11

12 12 CHAPTER 1. INTRODUCTION AND OUTLINE Opertions on sets) Complement(X) : returns U \ X. Intersection(X, Y) : returns X Y. Union(X, Y) : returns X Y. Tests on sets Memer(x, X) : returns true if x X, flse otherwise. Empty(X) : returns true if X =, flse otherwise. Universl(X) : returns true if X = U, flse otherwise. Included(X, Y) : returns true if X Y, flse otherwise. Equl(X, Y) : returns true if X = Y, flse otherwise. Opertions on reltions Projection 1(R) : returns the set π 1 (R) = {x y (x, y) R}. Projection 2(R) : returns the set π 2 (R) = {y x (x, y) R}. Join(R, S ) : returns the reltion R S = {(x, z) y X (x, y) R (y, z) S } Post(X, R) : returns the set post R (X) = {y U x X (x, y) R}. Pre(X, R) : returns the set pre R (X) = {y U x X (y, x) R}. Tle 1.1: Opertions nd tests for mnipultion of sets nd reltions instnce, re represented in computer science s sequences of digits, i.e., s words over the lphet of digits. Vectors nd lists cn lso e represented s words y conctenting the word representtions of their elements. As mtter of fct, whenever computer stores n oject in file, the computer is representing it s word over some lphet, like ASCII or Unicode. So word utomt re very generl dt structure. However, while ny oject cn e represented y word, not every oject cn e represented y finite word, tht is, word of finite length. Typicl exmples re rel numers nd non-terminting executions of progrm. When ojects cnnot e represented y finite words, computers usully only represent some pproximtion: flot insted of rel numer, or finite prefix insted of non-terminting computtion. In the second prt of the notes we show how to represent sets of infinite ojects exctly using utomt on infinite words. While the theory of utomt on finite words is often considered gold stndrd of theoreticl computer science powerful nd eutiful theory with lots of importnt pplictions in mny fields utomt on infinite words re hrder, nd their theory does not chieve the sme degree of perfection. This gives us structure for Prt II of the notes: we follow the steps of Prt I, lwys compring the solutions for infinite words with the gold stndrd. 1.1 Outline Prt I presents dt structures nd lgorithms for the well-known clss of regulr lnguges.

13 1.1. OUTLINE 13 Chpter 2 introduces the clssicl dt structures for the representtion of regulr lnguges: regulr expressions, deterministic finite utomt (DFA), nondeterministic finite utomt (NFA), nd nondeterministic utomt with ε-trnsitions. We refer to ll of them s utomt. The chpter presents some exmples showing how to use utomt to finitely represent sets of words, numers or progrm sttes, nd descries conversions lgorithms etween the representtions. All lgorithms re well known (nd cn lso e found in other textooks) with the exception of the lgorithm for the elimintion of ɛ-trnsitions. Chpter 3 ddress the issue of finding smll representtions for given set. It shows tht there is unique miniml representtion of lnguge s DFA, nd introduces the clssicl minimiztion lgorithms. It then shows how to the lgorithms cn e extended to reduce the size of NFAs. Chpter 4 descries lgorithms implementing oolen set opertions nd tests on DFAs nd NFAs. It includes recent, simple improvement in lgorithms for universlity nd inclusion. Chpter 5 presents first, clssicl ppliction of the techniques nd results of Chpter 4: pttern mtching. Even this ppliction gets new twist when exmined from the utomt-s-dtstructures point of view. The chpter presents the Knuth-Morris-Prtt lgorithm s the design of new dt structure, lzy DFAs, for which the memership opertion cn e performed very efficiently. Chpter 6 shows how to implement opertions on reltions. It discusses the notion of encoding (which requires more cre for opertions on reltrions thn for opertions on sets), nd introduces trnsducers s dt structure. Chpter 7 presents utomt dt structures for the importnt specil cse in which the universe U of ojects is finite. In this cse ll ojects cn e encoded y words of the sme length, nd the set nd reltion opertions cn e optimized. In prticulr, one cn then use miniml DFAs s dt structure, nd directly implement the lgorithms without using ny minimiztion lgorithm. In the second prt of the chpter, we show tht (ordered) Binry Decision Digrms (BDDs) re just further optimiztion of miniml DFAs s dt structure. We introduce slightly more generl clss of deterministic utomt, nd show tht the miniml utomton in this more generl clss (which is lso unique) hs t most s mny sttes s the miniml DFA. We then show how to implement the set nd reltion opertions for this new representtion. Chpter 8 pplies nerly ll the constructions nd lgorithms of previous chpter to the prolem of verifying sfety properties of sequentil nd concurrent progrms with ounded-rnge vriles. In prticulr, the chpter shows how to model concurrent progrms s networks of utomt, how to express sfety properties using utomt or regulr expressions, nd how to utomticlly check the properties using the lgorithmic constructions of previous chpters. Chpter 9 introduces first-order logic (FOL) nd mondic-second order logic (MSOL) on words s representtion llowing us to descried regulr lnguge s the set of words stisfying property. The chpter shows tht FOL cnnot descrie ll regulr lnguges, nd tht MSOL does. Chpter 10 introduces Presurger rithmetic, nd the lgorithm tht computes n utomton encoding ll the solutions of given formul. In prticulr, it presents n lgorithm to compute n utomton for the solutions of liner inequlity over the nturls or over the integers.

14 14 CHAPTER 1. INTRODUCTION AND OUTLINE Prt II presents dt structures nd lgorithms for ω-regulr lnguges. Chpter 11 introduces ω-regulr expressions nd severl different clsses of ω-utomt: deterministic nd nondterministic Büchi, generlized Büchi, co-büchi, Muller, Rin, nd Street utomt. It explins the dvntges nd disdvntges of ech clss, in prticulr whether the utomt in the clss cn e determinized, nd presents conversion lgorithms etween the clsses. Chpter 12 presents implementtions of the set opertions (union, intersection nd complementtion) for Büchi nd generlized Büchi utomt. In prticulr, it presents in detil complementtion lgorithm for Büchi utomt. Chpter?? presents different implementtions of the emptiness test for Büchi nd generlized Büchi utomt. the first prt of the chpter presents two liner-time implementtions sed on depth-first-serch (DFS): the nested-dfs lgorithm nd the two-stck lgorithm, modifiction of Trjn s lgorithm for the computtion of strongly connected components. The second prt presents further implemnttions sed on redth-first-serch. Chpter 14 pplies the lgorithms of previous chpters to the prolem of verifying liveness properties of progrms. After n introductory exmple, the chpter introduces Liner Temporl Logic s property specifiction formlism, nd shows how to lgorithmiclly otin Büchi utomton recognizing the lnguge of ll words stisfying the formul. The verifiction lgorithm cn then e reduced to comintion of the oolen opertions nd emptiness check. Chpter 15 extends the logic pproch to regulr lnguges studied in Chpters 9 nd 10 to ω- words. The first prt of the chpter introduces mondic second-order logic on ω-words, nd shows how to construct Büchi utomton recognizing the set of words stisfying given formul. The second prt introduces liner rithnetic, the frist-order theory of therel numers with ddition, nd shows how to construct Büchi utomton recognizing the encodings of ll the rel numers stisfying given formul.

15 Prt I Automt on Finite Words 15

16

17 Chpter 2 Automt Clsses nd Conversions In Section 2.1 we introduce sic definitions out words nd lnguges, nd then introduce regulr expressions, textul nottion for defining lnguges of finite words. Like ny other forml nottion, it cnnot e used to define ech possile lnguge. However, the next chpter shows tht they re n dequte nottion when deling with utomt, since they define exctly the lnguges tht cn e represented y utomt on words. 2.1 Regulr expressions: lnguge to descrie lnguges An lphet is finite, nonempty set. The elements of n lphet re clled letters. A finite, possily empty sequence of letters is word. A word n hs length n. The empty word is the only word of length 0 nd it is written ε. The conctention of two words w 1 = 1... n nd w 2 = 1... m is the word w 1 w 2 = 1... n 1... m, sometimes lso denoted y w 1 w 2. Notice tht ε w = w = w ε = w. For every word w, we define w 0 = ε nd w k+1 = w k w. Given n lphet Σ, we denote y Σ the set of ll words over Σ. A set L Σ of words is lnguge over Σ. The complement of lnguge L is the lnguge Σ \ L, which we often denote y L (notice tht this nottion implicitly ssumes the lphet Σ is fixed). The conctention of two lnguges L 1 nd L 2 is L 1 L 2 = {w 1 w 2 Σ w 1 L 1, w 2 L 2 }. The itertion of lnguge L Σ is the lnguge L = i 0 L i, where L 0 = {ε} nd L i+1 = L i L for every i 0. Definition 2.1 Regulr expressions r over n lphet Σ re defined y the following grmmr, where Σ r ::= ε r 1 r 2 r 1 + r 2 r The set of ll regulr expressions over Σ is written RE(Σ). The lnguge L(r) Σ of regulr expression r RE(Σ) is defined inductively y 17

18 18 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS L( ) = L(r 1 r 2 ) = L(r 1 ) L(r 2 ) L(r ) = L(r) L(ε) = {ε} L(r 1 + r 2 ) = L(r 1 ) L(r 2 ) L() = {} A lnguge L is regulr if there is regulr expression r such tht L = L(r). We often use lnguge, nd identify regulr expression nd its lnguge. For instnce, when there is no risk of confusion we write the lnguge r insted of the lnguge L(r). Exmple 2.2 Let Σ = {0, 1}. Some exmples of lnguges expressile y regulr expressions re: The set of ll words: (0 + 1). We often use Σ s n revition of (0 + 1), nd so Σ s n revition of Σ. The set of ll words of length t most 4: ( ε) 4. The set of ll words tht egin nd end with 0: 0Σ 0. The set of ll words contining t lest one pir of 0s exctly 5 letters prt. Σ 0Σ 4 0Σ. The set of ll words contining n even numer of 0s: ( ). The set of ll words contining n even numer of 0s nd n even numer of 1s: ( ( )( ) ( ) ). 2.2 Automt clsses We riefly recpitulte the definitions of deterministic nd nondeterministic finite utomt, s well s nondeterministic utomt with ε-trnsitions nd regulr expressions. Definition 2.3 A deterministic utomton (DA) is tuple A = (Q, Σ, δ, q 0, F), where Q is set of sttes, Σ is n lphet, δ: Q Σ Q is trnsition function, q 0 Q is the initil stte, nd F Q is the set of finl sttes.

19 2.2. AUTOMATA CLASSES A run of A on input n 1 is sequence q 0 q 1 q 2... q n, such tht q i Q for 0 i n, nd δ(q i, i ) = q i+1 for 0 i < n 1. A run is ccepting if q n F. A ccepts word w Σ, if there is n ccepting run on input w. The lnguge recognized y A is the set L(A) = {w Σ w is ccepted y A}. A deterministic finite utomton (DFA) is DA with finite set of sttes. Notice tht DA hs exctly one run on given word. Given DA, we often sy the word w leds from q 0 to q, mening tht the unique run of the DA on the word w ends t the stte q. Grphiclly, non-finl sttes of DFA re represented y circles, nd finl sttes y doule circles (see the exmple elow). The trnsition function is represented y leled directed edges: if δ(q, ) = q then we drw n edge from q to q leled y. We lso drw n edge into the initil stte. Exmple 2.4 Figure 2.4 shows the grphicl representtion of the DFA A = (Q, Σ, δ, q 0, F), where Q = {q 0, q 1, q 2, q 3 }, Σ = {, }, F = {q 0 }, nd δ is given y the following tle The runs of A on nd re n 1 δ(q 0, ) = q 1 δ(q 1, ) = q 0 δ(q 2, ) = q 3 δ(q 3, ) = q 2 δ(q 0, ) = q 3 δ(q 1, ) = q 2 δ(q 2, ) = q 1 δ(q 3, ) = q 0 q 0 q 0 q 1 q 1 q 0 q 2 q 3 q 1 q 0 q 2 The first one is ccepting, ut the second one is not. The DFA recognizes the lnguge of ll words over the lphet {, } tht contin n even numer of s nd n even numer of s. The DFA is in the sttes on the left, respectively on the right, if it hs red n even, respectively n odd, numer of s. Similrly, it is in the sttes t the top, respectively t the ottom, if it hs red n even, respectively n odd, numer of s. q 0 q 1 q 3 q 2 Figure 2.1: A DFA

20 20 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS Trp sttes. Consider the DFA of Figure 2.2 over the lphet {,, c}. The utomton recognizes the lnguge {, }. The pink stte on the left is often clled trp stte or grge collector: if run reches this stte, it gets trpped in it, nd so the run cnnot e ccepting. DFAs often hve trp stte with mny ingoing trnsitions, nd this mkes difficult to find nice grphicl representtion. So when drwing DFAs we often omit the trp stte. For instnce, we only drw the lck prt of the utomton in Figure 2.2. Notice tht no informtion is lost: if stte q hs no outgoing trnsition leled y, then we know tht δ(q, ) = q t, where q t is the trp stte., c,, c,, c, c c Figure 2.2: A DFA with trp stte Using DFAs s dt structures Here re four exmples of how DFAs cn e used to represent interesting sets of ojects. Exmple 2.5 The DFA of Figure 2.6 (drwn without the trp stte!) recognizes the strings over the lphet {,, 0, 1,..., 9} tht encode rel numers with finite deciml prt. We wish to exclude 002, 0, or , ut ccept 37, , or s correct encodings. A description of the strings in English is rther long: string encoding numer consists of n integer prt, followed y possily empty frctionl prt; the integer prt consists of n optionl minus sign, followed y nonempty sequence of digits; if the first digit of this sequence is 0, then the sequence itself is 0; if the frctionl prt is nonempty, then it strts with., followed y nonempty sequence of digits tht does not end with 0; if the integer prt is 0, then the frctionl prt is nonempty. Exmple 2.6 The DFA of Figure recognizes the inry encodings of ll the multiples of 3. For instnce, it recognizes 11, 110, 1001, nd 1100, which re the inry encodings of 3, 6, 9, nd 12, respectively, ut not, sy, 10 or 111. Exmple 2.7 The DFA of Figure 2.5 recognizes ll the nonnegtive integer solutions of the ineqution 2x y 2, using the following encoding. The lphet of the DFA hs four letters,

21 2.2. AUTOMATA CLASSES ,..., 9 1,..., 9 1,..., 9 0 1,..., 9 0 1,..., 9 0 Figure 2.3: A DFA for deciml numers Figure 2.4: A DFA for the multiples of 3 in inry nmely A word like [ ] 0, 0 [ ] 0, 1 [ ] 1, nd 0 [ ] 1 1 [ ] [ ] [ ] [ ] [ ] [ ] encodes pir of numers, given y the top nd ottom rows, nd The inry encodings strt with the lest significnt it, tht is encodes = 13, nd encodes = 50 We see this s n encoding of the vlution (x, y) := (13, 50). This vlution stisfies the ineqution, nd indeed the word is ccepted y the DFA. Exmple 2.8 Consider the following progrm with two oolen vriles x, y:

22 22 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS [ 0 [ [ 0 0, [ 1 [ [ 0 1, [ 1 [ [ 1 1, [ [ ] [ 0 1 0, 1 [ [ 0 0, [ [ 1 1, [ ] [ [ ] [ Figure 2.5: A DFA for the solutions of 2x y 2. 1 while x = 1 do 2 if y = 1 then 3 x 0 4 y 1 x 5 end A configurtion of the progrm is triple [l, n x, n y ], where l {1, 2, 3, 4, 5} is the current vlue of the progrm counter, nd n x, n y {0, 1} re the current vlues of x nd y. The initil configurtions re [1, 0,, [1, 0,, [1, 1,, [1, 1,, i.e., ll configurtions in which control is t line 1. The DFA of Figure 2.6 recognizes ll rechle configurtions of the progrm. For instnce, the DFA ccepts [5, 0,, indicting tht it is possile to rech the lst line of the progrm with vlues x = 0, y = , 1 0 0, Figure 2.6: A DFA for the rechle configurtions of the progrm of Exmple 2.8 Definition 2.9 A non-deterministic utomton (NA) is tuple A = (Q, Σ, δ, Q 0, F), where Q, Σ,

23 2.2. AUTOMATA CLASSES 23 nd F re s for DAs, Q 0 is set of initil sttes nd δ: Q Σ P(Q) is trnsition reltion. 0 1 A run of A on input n is sequence p 0 p 1 p 2... p n, such tht p i Q for 0 i n, p 0 Q 0, nd p i+1 δ(p i, i ) for 0 i < n 1. Tht is, run strts t some initil stte. A run is ccepting if p n F. A ccepts word w Σ, if there is n ccepting run on input w. The lnguge recognized y A is the set L(A) = {w Σ w is ccepted y A}. The runs of NAs re defined s for DAs, ut sustituting p 0 Q 0 for p i+1 δ(p i, i ) for δ(p i, i ) = p i+1. Acceptnce nd the lnguge recognized y NA re defined s for DAs. A nondeterministic finite utomton (NFA) is NA with finite set of sttes. A stte of NFA my hve zero, one, or mny outgoing trnsitions leled y the sme letter. Also, NFA my hve zero, one, or mny runs on the sme word. Oserve, however, tht the numer of runs on word is finite. We often identify the trnsition function δ of DA with the set of triples (q,, q ) such tht q = δ(q, ), nd the trnsition reltion δ of NFA with the set of triples (q,, q ) such tht q δ(q, ); so we often write (q,, q ) δ, mening q = δ(q, ) for DA, or q δ(q, ) for NA. If NFA hs severl initil sttes, then its lnguge is the union of the sets of words ccepted y runs strting t ech initil stte. Exmple 2.10 Figure 2.7 shows NFA A = (Q, Σ, δ, Q 0, F) where Q = {q 0, q 1, q 2, q 3 }, Σ = {, }, Q 0 = {q 0 }, F = {q 3 }, nd the trnsition reltion δ is given y the following tle δ(q 0, ) = {q 1 } δ(q 1, ) = {q 1 } δ(q 2, ) = δ(q 3, ) = {q 3 } δ(q 0, ) = δ(q 1, ) = {q 1, q 2 } δ(q 2, ) = {q 3 } δ(q 3, ) = {q 3 } A hs no run for ny word strting with. It hs exctly one run for, nd four runs for, nmely q 0 q 1 q 1 q 1 q 1 q 0 q 1 q 1 q 1 q 2 q 0 q 1 q 1 q 2 q 3 q 0 q 1 q 2 q 3 q 3 Two of these runs re ccepting, the other two re not. L(A) is the set of words tht strt with nd contin two consecutive s. n 1,, q 0 q 1 q 2 q 3 Figure 2.7: A NFA. Definition 2.11 A non-deterministic utomton with ε-trnsitions (NA-ε) is tuple A = (Q, Σ, δ, Q 0, F), where Q, Σ, q 0, nd F re s for NAs nd

24 24 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS δ: Q (Σ {ε}) P(Q) is trnsition reltion. The runs nd ccepting runs of NA-ε re defined s for NAs. A ccepts word 1... n Σ if A hs n ccepting run on ε k 0 1 ε k 1... ε k n 1 n ε k n (Σ {ε}) for some k 0, k 1,..., k n 0. A nondeterministic finite utomton with ε-trnsitions (NFA-ε) is NA-ε with finite set of sttes. Notice tht, if some cycle of the utomton only ε-trnsitions, the numer of runs of NFA-ε on word my e even infinite. Exmple 2.12 Figure 2.8 shows NFA-ε ε ε Figure 2.8: A NFA-ε. Definition 2.13 Let A = (Q, Σ, δ, Q 0, F) e n utomton. A stte q Q is rechle from q Q if q = q or if there exists run q 1 n... q on some input 1... n Σ. A is in norml form if every stte is rechle from the initil stte. Convention: Unless otherwise stted, we ssume tht utomt re in norml form. All our lgorithms preserve norml forms, i.e., when the output is n utomton, the utomton is in norml form. We extend NAs to llow regulr expressions on trnsitions. Such utomt re clled NA-reg nd they re oviously generliztion of oth regulr expressions nd NA-εs. They will e useful to provide uniform conversion etween utomt nd regulr expressions. Definition 2.14 A non-deterministic utomton with regulr expression trnsitions (NA-reg) is tuple A = (Q, Σ, δ, Q 0, F), where Q, Σ, q 0, nd F re s for NAs, nd where δ: Q RE(Σ) P(Q) is reltion such tht δ(q, r) = for ll ut finite numer of pirs (q, r) Q RE(Σ). Accepting runs re defined s for NFAs. A ccepts word w Σ if A hs n ccepting run on r 1... r k such tht w = L(r 1 )... L(r k ). A nondeterministic finite utomton with regulr expression trnsitions (NFA-reg) is NA-reg with finite set of sttes.

25 2.3. CONVERSION ALGORITHMS BETWEEN FINITE AUTOMATA Conversion Algorithms etween Finite Automt We recll tht ll our dt structures cn represent exctly the sme lnguges. Since DFAs re specil cse of NFA, which re specil cse of NFA-ε, it suffices to show tht every lnguge recognized y n NFA-ε cn lso e recognized y n NFA, nd every lnguge recognized y n NFA cn lso e recognized y DFA From NFA to DFA. The powerset construction trnsforms n NFA A into DFA B recognizing the sme lnguge. We first give n informl ide of the construction. Recll tht NFA my hve mny different runs on word w, possily leding to different sttes, while DFA hs exctly one run on w. Denote y Q w the set of sttes q such tht some run of A on w leds from its initil stte q 0 to q. Intuitively, B keeps trck of the set Q w : its sttes re sets of sttes of A, with {q 0 } s initil stte, nd its trnsition function is defined to ensure tht the run of B on w leds from {q 0 } to Q w (see elow). It is then esy to ensure tht A nd B recognize the sme lnguge: it suffices to choose the finl sttes of B s the sets of sttes of A contining t lest one finl stte, ecuse for every word w: B ccepts w iff Q w is finl stte of B iff Q w contins t lest finl stte of A iff some run of A on w leds to finl stte of A iff A ccepts w. Let us now define the trnsition function of B, sy. Keeping trck of the set Q w mounts to stisfying (Q w, ) = Q w for every word w. But we hve Q w = q Q w δ(q, ), nd so we define (Q, ) = q Q δ(q, ) for every Q Q. Notice tht we my hve Q = ; in this cse, is stte of B, nd, since (, ) = for every, trp stte. Summrizing, given A = (Q, Σ, δ, Q 0, F) we define the DFA B = (Q, Σ,, Q 0, F) s follows: Q = P(Q); (Q, ) = δ(q, ) for every Q Q nd every Σ; q Q Q 0 = {q 0 }; nd F = {Q Q Q F }.

26 26 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS NFAtoDFA(A) Input: NFA A = (Q, Σ, δ, Q 0, F) Output: DFA B = (Q, Σ,, Q 0, F) with L(B) = L(A) 1 Q,, F ; 2 W = {Q 0 } 3 while W do 4 pick Q from W 5 dd Q to Q 6 if Q F then dd Q to F 7 for ll Σ do 8 Q δ(q, ) q Q 9 if Q Q then dd Q to W 10 dd (Q,, Q ) to Tle 2.1: NFAtoDFA(A) Notice, however, tht B my not e in norml form: it my hve mny sttes non-rechle from Q 0. For instnce, ssume A hppens to e DFA with sttes {q 0,..., q n 1 }. Then B hs 2 n sttes, ut only the singletons {q 0 },..., {q n 1 } re rechle. The conversion lgorithm shown in Tle 16 constructs only the rechle sttes. It is written in pseudocode, with strct sets s dt structure. Like nerly ll the lgorithms presented in the next chpters, it is workset lgorithm. Workset lgorithms mintin set of ojects, the workset, witing to e processed. Like in mthemticl sets, the elements of the workset re not ordered, nd the workset contins t most one copy of n element (i.e., if n element lredy in the workset is dded to it gin, the workset does not chnge). For most of the lgorithms in this ook, the workset cn e implemented s hsh tle. In NFAtoDFA() the workset is clled W, in other lgorithms just W (we use clligrphic font to emphsize tht in this cse the ojects of the workset re sets). Workset lgorithms repetedly pick n oject from the workset (instruction pick Q from W), nd process it; notice tht picking n oject removes it from the workset. Processing n oject my generte new ojects tht re dded to the list. The lgorithm termintes when the workset is empty. Since ojects removed from the list my generte new ojects, workset lgorithms my potentilly fil to terminte, even if the set of ll ojects is finite, ecuse the sme oject might e dded to nd removed from the workset infinitely mny times. Termintion is gurnteed y mking sure tht no oject tht hs een removed from the list once is ever dded to it gin. For this, ojects picked from the workset re stored (in NFAtoDFA() they re stored in Q), nd ojects re dded to the workset only if they hve not een stored yet.

27 2.3. CONVERSION ALGORITHMS BETWEEN FINITE AUTOMATA 27 Figure 2.9 shows n NFA t the top, nd some snpshots of the run of NFAtoDFA() on it. The sttes of the DFA re lelled with the corresponding sets of sttes of the NFA. The lgorithm picks sttes from the workset in order {1}, {1, 2}, {1, 3}, {1, 4}, {1, 2, 4}. Snpshots ()-(d) re tken right fter it picks the sttes {1, 2}, {1, 3}, {1, 4}, nd {1, 2, 4}, respectively. Snpshot (e) is tken t the end. Notice tht out of the 2 4 = 16 susets of sttes of the NFA only 5 re constructed, ecuse the rest re not rechle from {1}. Complexity. If A hs n sttes, then the output of NFAtoDFA(A) cn hve up to 2 n sttes. To show tht this ound is essentilly rechle, consider the fmily {L n } n 1 of lnguges over Σ = {, } given y L n = ( + ) ( + ) (n 1). Tht is, L n contins the words of length t lest n whose n-th letter strting from the end is n. The lnguge L n is ccepted y the NFA with n+1 sttes shown in Figure 2.10(): intuitively, the utomton chooses one of the s in the input word, nd checks tht it is followed y exctly n 1 letters efore the word ends. Applying the suset construction, however, yields DFA with 2 n sttes. The DFA for L 3 is shown on the left of Figure 2.10(). The sttes of the DFA hve nturl interprettion: they store the lst n letters red y the utomton. If the DFA is in the stte storing n nd it reds the letter n+1, then it moves to the stte storing 2... n+1. Sttes re finl if the first letter they store is n. The interpreted version of the DFA is shown on right of Figure 2.10(). We cn lso esily prove tht ny DFA recognizing L n must hve t lest 2 n sttes. Assume there is DFA A n = (Q, Σ, δ, q 0, F) such tht Q < 2 n nd L(A n ) = L n. We cn extend δ to mpping ˆδ: Q {, } Q, where ˆδ(q, ε) = q nd ˆδ(q, w σ) = δ(ˆδ(q, w), σ) for ll w Σ nd for ll σ Σ. Since Q < 2 n, there must e two words u v 1 nd u v 2 of length n for which ˆδ(q 0, u v 1 ) = ˆδ(q 0, u v 2 ). But then we would hve tht ˆδ(q 0, u v 1 u) = ˆδ(q 0, u v 2 u); tht is, either oth u v 1 u nd u v 2 u re ccepted y A n or neither do. Since, however, v 1 u = v 2 u = n, this contrdicts the ssumption tht A n consists of exctly the words with n t the n-th position from the end From NFA-ε to NFA. Let A e NFA-ε over n lphet Σ. In this section we use to denote n element of Σ, nd α, β to denote elements of Σ {ε}. Loosely speking, the conversion first dds to A new trnsitions tht mke ll ε-trnsitions redundnt, without chnging the recognized lnguge: every word ccepted y A efore dding the new trnsitions is ccepted fter dding them y run without ε-trnsitions. The conversion then removes ll ε-trnsitions, delivering n NFA tht recognizes the sme lnguge s A. The new trnsitions re shortcuts: If A hs trnsitions (q, α, q ) nd (q, β, q ) such tht α = ε or β = ε, then the shortcut (q, αβ, q ) is dded. (Notice tht either αβ = for some Σ, or αβ = ε.) Shortcuts my generte further shortcuts: for instnce, if αβ = nd A hs further trnsition (q, ε, q ), then new shortcut (q,, q ) is dded. We cll the process of dding ll possile shortcuts sturtion. Oviously, sturtion does not chnge the lnguge of A, nd if

28 28 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS, 1 2 3, 4 1 1, 2 1 1, 2 () () 1, 3 1 1, 2 1 1, 2 1, 4 1, 3 1, 2, 4 (c) 1, 4 1, 3 1, 2, 4 (d) 1 1, 2 1, 4 1, 3 1, 2, 4 (e) Figure 2.9: Conversion of NFA into DFA.

29 2.3. CONVERSION ALGORITHMS BETWEEN FINITE AUTOMATA 29,...,,, 1 2 n n + 1 () NFA for L n. 1 1, 4 1, 2 1, 3 1, 2, 3 1, 2, 4 1, 2, 3, 4 1, 3, 4 () DFA for L 3 nd interprettion. Figure 2.10: NFA for L n, nd DFA for L 3.

30 30 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS efore sturtion A hs run ccepting nonempty word, for exmple q 0 ε q 1 ε q 2 q 3 ε q 4 q 5 ε q 6 then fter sturtion it hs run ccepting the sme word, nd visiting no ε-trnsitions, nmely q 0 q 4 q 6 However, we cnnot yet remove ε-trnsitions. The NFA-ε of Figure 2.11() ccepts ε. After sturtion we get the NFA-ε of Figure 2.11(). However, removing ll ε-trnsitions yields n NFA tht no longer ccepts ε. To solve this prolem, if A ccepts ε from some initil stte, then ε ε () NFA-ε ccepting L(0 1 2 ) ε 0, 1 ε 1, 2 0, 1, 2 () After sturtion , 1 1, 2 0, 1, 2 (c) After mrking the initil stte nd finl nd removing ll ε-trnsitions. Figure 2.11: Conversion of n NFA-ε into n NFA y shortcutting ε-trnsitions. we mrk tht stte s finl, which clerly does not chnge the lnguge. To decide whether A ccepts ε, we check if some stte rechle from some initil stte y sequence of ε-trnsitions is finl. Figure 2.11(c) shows the finl result. Notice tht, in generl, fter removing ε-trnsitions

31 2.3. CONVERSION ALGORITHMS BETWEEN FINITE AUTOMATA 31 the utomton my not e in norml form, ecuse some sttes my no longer e rechle. So the nïve procedure runs in three phses: sturtion, ε-check, nd normliztion. However, it is possile to crry ll three steps in single pss. We give workset implementtion of this procedure in which the check is done while sturting, nd only the rechle sttes re generted (in the pseudocode α nd β stnd for either letter of Σ or ɛ, nd stnds for letter of Σ). Furthermore, the lgorithm voids constructing some redundnt shortcuts. For instnce, for the NFA-ε of Figure 2.11() the lgorithm does not construct the trnsition leled y 2 leding from the stte in the middle to the stte on the right. NFAεtoNFA(A) Input: NFA-ε A = (Q, Σ, δ, Q 0, F) Output: NFA B = (Q, Σ, δ, q 0, F ) with L(B) = L(A) 1 Q 0 Q 0 2 Q Q 0 ; δ ; F F Q 0 3 δ ; W {(q, α, q ) δ q Q 0 } 4 while W do 5 pick (q 1, α, q 2 ) from W 6 if α ε then 7 dd q 2 to Q ; dd (q 1, α, q 2 ) to δ ; if q 2 F then dd q 2 to F 8 for ll q 3 δ(q 2, ε) do 9 if (q 1, α, q 3 ) δ then dd (q 1, α, q 3 ) to W 10 for ll Σ, q 3 δ(q 2, ) do 11 if (q 2,, q 3 ) δ then dd (q 2,, q 3 ) to W 12 else / α = ε / 13 dd (q 1, α, q 2 ) to δ ; if q 2 F then dd q 1 to F 14 for ll β Σ {ε}, q 3 δ(q 2, β) do 15 if (q 1, β, q 3 ) δ δ then dd (q 1, β, q 3 ) to W The correctness proof is conceptully esy, ut the different cses require some cre, nd so we devote it proposition. Proposition 2.15 Let A e NFA-ε, nd let B = NFAεtoNFA(A). Then B is NFA nd L(A) = L(B). Proof: Notice first tht every trnsition tht leves W is never dded to W gin: when trnsition (q 1, α, q 2 ) leves W it is dded to either δ or δ, nd trnsition enters W only if it does not elong to either δ or δ. Since every execution of the while loop removes trnsition from the workset, the lgorithm eventully exits the loop nd termintes. To show tht B is NFA we hve to prove tht it only hs non-ε trnsitions, nd tht it is in norml form, i.e., tht every stte of Q is rechle from q 0 in B. For the first prt, oserve tht

32 32 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS trnsitions re only dded to δ in line 7, nd none of them is n ε-trnsition ecuse of the gurd in line 6. For the second prt, we need the following invrint, which cn e esily proved y inspection: for every trnsition (q 1, α, q 2 ) dded to W, if α = ε then q 1 Q 0, nd if α ε, then q 2 is rechle in B (fter termintion). Now, since new sttes re dded to Q only t line 7, pplying the invrint we get tht every stte of Q is rechle in B from some stte in Q 0. It remins to prove L(A) = L(B). The inclusion L(A) L(B) follows from the fct tht every trnsition dded to δ is shortcut, which cn e proved y inspection. For the inclusion L(A) L(B), we first prove ε ε q n e run of A such tht q n F. If tht ε L(A) implies ε L(B). Let q 0 q 1... q n 1 n = 0 (i.e., q n = q 0 ), then we re done. If n > 0, then we prove y induction on n tht trnsition (q 0, ε, q n ) is eventully dded to W (nd so eventully picked from it), which implies tht q 0 is eventully dded to F t line 13. If n = 1, then (q 0, ε, q n ) is dded to W t line 3. If n > 1, then y hypothesis (q 0, ε, q n 1 ) is eventully dded to W, picked from it t some lter point, nd so (q 0, ε, q n ) is dded to W t line 15. We now prove tht for every w Σ +, if w L(A) then w L(B). Let w = n with n 1. Then A hs run q 0 ε... ε q m1 1 q m1 +1 ε... ε q mn n q mn +1 ε... ε q m such tht q m F. We hve just proved tht trnsition (q 0, ε, q m1 ) is eventully dded to W. So (q 0, 1, q m1 +1) is eventully dded t line 15, (q 0, 1, q m+2 ),..., (q 0, 1, q m2 ) re eventully dded t line 9, nd (q m2, 2, q m2 +1) is eventully dded t line 11. Iterting this rgument, we otin tht 1 2 q 0 q m2 q m3... q mn n q m is run of B. Moreover, q m is dded to F t line 7, nd so w L(B). Complexity. Oserve tht the lgorithm processes pirs of trnsitions (q 1, α, q 2 ), (q 2, β, q 3 ), where (q 1, α, q 2 ) comes from W nd (q 2, β, q 3 ) from δ (lines 8, 10, 14). Since every trnsition is removed from W t most once, the lgorithm processes t most Q Σ δ pirs (ecuse for fixed trnsition (q 2, β, q 3 ) δ there re Q possiilities for q 1 nd Σ possiilities for α). The runtime is dominted y the processing of the pirs, nd so it is O( Q Σ δ ). 2.4 Conversion lgorithms etween regulr expressions nd utomt To convert regulr expressions to utomt nd vice vers we use NFA-regs s introduced in Definition Both NFA-ε s nd regulr expressions cn e seen s suclsses of NFA-regs: n NFA-ε is n NFA-reg whose trnsitions re leled y letters or y ε, nd regulr expression r is the NFA-reg A r hving two sttes, the one initil nd the other finl, nd single trnsition leled r leding from the initil to the finl stte. We present lgorithms tht, given n NFA-reg elonging to one of this suclsses, produces sequence of NFA-regs, ech one recognizing the sme lnguge s its predecessor in the sequence, nd ending in n NFA-reg of the other suclss.

33 2.4. CONVERSION ALGORITHMS BETWEEN REGULAR EXPRESSIONS AND AUTOMATA From regulr expressions to NFA-ε s Given regulr expression s over lphet Σ, it is convenient do some preprocessing y exhustively pplying the following rewrite rules: r r r + r + r r ε Since the left- nd right-hnd-sides of ech rule denote the sme lnguge, the result of the preprocessing is regulr expression for the sme lnguge s the originl one. Moreover, if r is the resulting regulr expression, then either r =, or r does not contin ny occurrence of the symol. In the former cse, we cn directly produce n NFA-ε. In the second, we trnsform the NFA-reg A r into n equivlent NFA-ε y exhustively pplying the trnsformtion rules of Figure Automton for the regulr expression, where Σ {ε} r 1 r 2 r 1 r 2 Rule for conctention r 1 + r 2 r 1 Rule for choice r 2 r r ε ε Rule for Kleene itertion Figure 2.12: Rules converting regulr expression given s NFA-reg into n NFA-ε. It is esy to see tht ech rule preserves the recognized lnguge (i.e., the NFA-regs efore nd fter the ppliction of the rule recognize the sme lnguge). Moreover, since ech rule splits regulr expression into its constituents, we eventully rech n NFA-reg to which no rule cn e pplied. Furthermore, since the initil regulr expression does not contin ny occurrence of the symol, this NFA-reg is necessrily n NFA-ε.

34 34 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS Exmple 2.16 Consider the regulr expression ( + c) d. The result of pplying the trnsformtion rules is shown in Figure 2.13 on pge 35. Complexity. It follows immeditely from the rules tht the finl NFA-ε hs the two sttes of A r plus one stte for ech occurrence of the conctention or the Kleene itertion opertors in r. The numer of trnsitions is liner in the numer of symols of r. The conversion runs in liner time From NFA-ε s to regulr expressions Given n NFA-ε A, we trnsform it into the NFA-reg A r for some regulr expression r. It is gin convenient to pply some preprocessing to gurntee tht the NFA-ε hs single initil stte without ingoing trnsitions, nd single finl stte without outgoing trnsitions: Add new initil stte q 0 nd ε-trnsitions leding from q 0 from ech initil stte, nd replce the set of initil sttes y {q 0 }. Add new stte q f nd ε-trnsitions leding from ech finl stte to q f, nd replce the set of finl sttes y {q f }.... q 0 ε q f ε ε ε Rule 1 After preprocessing, the lgorithm runs in phses. Ech phse consist of two steps. The first step yields n utomton with t most one trnsition etween ny two given sttes: Repet exhustively: replce pir of trnsitions (q, r 1, q ), (q, r 2, q ) y single trnsition (q, r 1 + r 2, q ). r 1 r 2 Rule 2 r 1 + r 2 The second step reduces the numer of sttes y one, unless the only sttes left re the initil stte nd the finl stte q f.

35 2.4. CONVERSION ALGORITHMS BETWEEN REGULAR EXPRESSIONS AND AUTOMATA35 ( + c) d ( + c) d + c ε ε d ε ε d c ε ε d c ε ε ε ε ε ε d c Figure 2.13: The result of converting ( + c) d into n NFA-ε.

36 36 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS Pick non-finl nd non-initil stte q, nd shortcut it: If q hs self-loop (q, r, q) 1, replce ech pir of trnsitions (q, s, q), (q, t, q ) (where q q q, ut possily q = q ) y shortcut (q, sr t, q ); otherwise, replce it y the shortcut (q, st, q ). After shortcutting ll pirs, remove q.... r 1 s s 1... r 1 s s 1 r 1 s s m r n s m r n s s 1 r n s s m Rule 3 At the end of the lst phse we hve now n NFA-reg with exctly two sttes, the unique initil stte q 0 nd the unique finl stte q f. Moreover, q 0 hs no ingoing trnsitions nd q f hs no outgoing trnsitions, ecuse it ws initilly so nd the ppliction of the rules cnnot chnge it. After pplying Rule 2 exhustively, there is exctly one trnsition from q 0 to q f. The complete lgorithm is: NFAtoRE(A) Input: NFA-ε A = (Q, Σ, δ, Q 0, F) Output: regulr expression r with L(r) = L(A) 1 pply Rule 1 2 while Q \ (F {q 0 }) do 3 pick q from Q \ (F {q 0 }) 4 pply exhustively Rule 2 5 pply Rule 3 to q 6 pply exhustively Rule 2 7 return the lel of the (unique) trnsition Exmple 2.17 Consider the utomton of Figure 2.14() on pge 37. The rest of the figure shows some snpshots of the run of NFAtoRE() on this utomton. Snpshot () is tken right fter pplying rule 1. Snpshots (c) to (e) re tken fter ech execution of the ody of the while loop. Snpshot (f) shows the finl result. Complexity. The complexity of this lgorithm depends on the dt structure used to store regulr expressions. If regulr expresions re stored s strings or trees (following the syntx tree of the expression), then the complexity cn e exponentil. To see this, consider for ech n 1 the NFA 1 Notice tht it cn hve t most one, ecuse otherwise we would hve two prllel edges.

37 2.4. CONVERSION ALGORITHMS BETWEEN REGULAR EXPRESSIONS AND AUTOMATA37 () () ε ε (c) (d) ε ε ε ε (e) ε ε + + ( + )( + ) ( + ) (f) ( + + ( + )( + ) ( + ) ) Figure 2.14: Run of NFA-εtoRE() on DFA

38 38 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS A = (Q, Σ, δ, Q 0, F) where Q = {q 0,..., q n 1 }, Σ = { i j 0, i, j n 1}, δ = {(q i, i j, q j ) 0, i, j n 1}, nd F = {q 0 }. By symmetry, the runtime of the lgorithm is independent of the order in which sttes re eliminted. Consider the order q 1, q 2,..., q n 1. It is esy to see tht fter eliminting the stte q i the NFA-reg contins some trnsitions leled y regulr expressions with 3 i occurrences of letters. The exponentl lowup cnnot e voided: It cn e shown tht every regulr expression recognizing the sme lnguge s A contins t lest 2 (n 1) occurrences of letters. If regulr expressions re stored s cyclic directed grphs (the result of shring common suexpressions in the syntx tree), then the lgorithm works in polynomil time, ecuse the lel for new trnsition is otined y conctenting or strring lredy computed lels. 2.5 A Tour of Conversions We present n exmple illustrting ll conversions of this chpter. We strt with the DFA of Figure 2.14() recognizing the words over {, } with n even numer of s nd n even numer of s. The figure converts it into regulr expression. Now we convert this expression into NFA-ε: Figure 2.15 on pge 39 shows four snpshots of the process of pplying rules 1 to 4. In the next step we convert the NFA-ε into n NFA. The result is shown in Figure 2.16 on pge 40. Finlly, we trnsform the NFA into DFA y mens of the suset construction. The result is shown in Figure Oserve tht we do not recover the DFA we strted with, ut nother one recognizing the sme lnguge. A lst step llowing us to close the circle is presented in the next chpter. Exercises Exercise 1 Give regulr expression for the lnguge of ll words over Σ = {, }... (1)... eginning nd ending with the sme letter. (2)... hving two occurrences of t distnce 3. (3)... with no occurrences of the suword. (4)... contining exctly two occurrences of. (5)... tht cn e otined from y deleting letters. Exercise 2 Prove or disprove: the lnguges of the regulr expressions (1 + 10) nd 1 (101 ) re equl. Exercise 3 (Inspired y P. Rossmnith) Give syntctic chrcteriztions of the regulr expressions r stisfying () L(r) =, () L(r) = {ε}, (c) ε L(r), (d) the impliction L(rr) = L(r) L(r) = L(r ).

39 2.5. A TOUR OF CONVERSIONS 39 ( + + ( + )( + ) ( + ) ) () ( + )( + ) ( + ) ε ε () + ε ε ε ε + + ε ε ε ε (c) (d) Figure 2.15: Constructing NFA-ε for ( + + ( + )( + ) ( + ))

40 40 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS Figure 2.16: NFA for the NFA-ε of Figure 2.15(d) Exercise 4 Extend the syntx nd semntics of regulr expressions s follows: If r nd r re regulr expressions over Σ, then r nd r r re lso regulr expressions, where L(r) = L(r ) nd L(r r ) = L(r) L(r ). A lnguge L Σ is clled str-free if there exists n extended regulr expression r without ny occurrence of Kleene str opertion such tht L = L(r). For exmple, Σ is str-free, ecuse Σ = L( ). Show tht the lnguges () (01) nd () L(( ) ) re str-free. Exercise 5 Given lnguge L, let L pref nd L suf denote the lnguges of ll prefixes nd ll suffixes, respectively, of words in L. E.g. for L = {c, d}, we hve L pref = {c,,, ε, d} nd L suf = {c, c, c, ε, d}. (1) Given n NFA A, construct NFAs A pref nd A suf so tht L(A pref ) = L(A) pref nd L(A suf ) = L(A) suf. (2) Consider the regulr expression r = ( + ) cd. Give regulr expression r pref so tht L(r pref ) = L(r pref ). (3) More generlly, give n lgorithm tht gets regulr expression r s input nd returns regulr expression r pref so tht L(r pref ) = L(r) pref. Exercise 6 Trnsform the DFA (trp stte omitted)

41 2.5. A TOUR OF CONVERSIONS 41 2, 6 4, 5 1 3, 7 9, 12 8, Figure 2.17: DFA for the NFA of Figure 2.16 into n equivlent regulr expression, then trnsform this expression into n NFA (with ε-trnsitions), remove the ε-trnsitions, nd determinize the utomton. Exercise 7 The reverse of word w, denoted y w R, is defined s follows: if w = ε, then w R = ε, nd if w = n for n 1, then w R = n n The reverse of lnguge L is the lnguge L R = {w R w L}. (1) Give regulr expression for the reverse of ( (1 + 01) 01(0 + 1) ) 01. (2) Give n lgorithm tht tkes s input regulr expression r nd returns regulr expression r R such tht L(r R ) = ( L(r) )R. (3) Give n lgorithm tht tkes s input NFA A nd returns NFA A R such tht L(A R ) = ( L(A) ) R. Exercise 8 Prove or disprove: Every regulr lnguge is recognized y NFA... (1)... hving one single initil stte. (2)... hving one single finl stte. (3)... whose sttes re ll initil. (4)... whose sttes re ll finl.

42 42 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS (5)... whose initil sttes hve no incoming trnsitions. (6)... whose finl sttes hve no outgoing trnsitions. (7)... such tht ll input trnsitions of stte (if ny) crry the sme lel. (8)... such tht ll output trnsitions of stte (if ny) crry the sme lel. Which of the ove hold for DFAs? Which ones for NFA-ɛ? Exercise 9 Prove tht every finite lnguge (i.e., every lnguge contining finite numer of words) is regulr y defining DFA tht recognizes it. Exercise 10 Let Σ n = {1, 2,..., n}, nd let L n e the set of ll words w Σ n such tht t lest one letter of Σ n does not pper in w. So, for instnce, 1221, 32, 1111 L 3, nd 123, 2231 L 3. (1) Give NFA for L n with O(n) sttes nd trnsitions. (2) Give DFA for L n with 2 n sttes. (3) Show tht ny DFA for L n hs t lest 2 n sttes. (4) Which of (1), (2) nd (3) still hold if L n is replced y L n, the set of words contining ll letters of Σ n? Let M n {0, 1} e the set of words of length 2n of the form (0 + 1) j 1 0(0 + 1) n 1 0(0 + 1) n j for some 0 j n. These re the words contining t lest one pir of 0s t distnce n. For exmple, , , M 3 nd , , M 3. (5) Give NFA for M n with O(n) sttes nd trnsitions. (6) Give DFA for M n with Ω(2 n ) sttes. (7) Show tht ny DFA for M n hs t lest 2 n sttes. Exercise 11 Recll tht nondeterministic utomton A ccepts word w if t lest one of the runs of A on w is ccepting. This is sometimes clled the existentil ccepting condition. Consider the vrint in which A ccepts w if ll runs of A on w re ccepting (in prticulr, if A hs no run on w then it ccepts w). This is clled the universl ccepting condition. Notice tht DFA ccepts the sme lnguge with oth the existentil nd the universl ccepting conditions. Intuitively, we cn visulize the utomton s executing ll runs in prllel. After reding word w, the utomton is simultneously in ll sttes reched y ll runs lelled y w. the utomton ccepts if ll those sttws re ccepting. Consider the fmily L n of lnguges over the lphet {0, 1} given y L n = {ww Σ 2n w Σ n }. (1) Give n utomton of size O(n) with universl ccepting condition tht recognizes L n.

43 2.5. A TOUR OF CONVERSIONS 43 (2) Prove tht every NFA (nd so in prticulr every DFA) recognizing L n hs t lest 2 n sttes. (3) Give n lgorithm tht trnsforms n utomton with universl ccepting condition into DFA recognizing the sme lnguge. This shows tht utomt with the universl ccepting condition recognize the regulr lnguges. Exercise 12 (1) Give regulr expression of size O(n) such tht the smllest DFA equivlent to it hs Ω(2 n ) sttes. (2) Give regulr expression of size O(n) without + such tht the smllest DFA equivlent to it hs Ω(2 n ) sttes. (3) Give regulr expression of size O(n) without + nd of strt-height 1 such tht the smllest DFA equivlent to it hs Ω(2 n ) sttes. (Pper y Shllit t STACS 2008). Exercise 13 Let K n e the complete directed grph with nodes {1,..., n} nd edges {(i, j) 1 i, j n}. A pth of K n is sequence of nodes, nd circuit of K n is pth tht egins nd ends t the sme node. Consider the fmily of DFAs A n = (Q n, Σ n, δ n, q 0n, F n ) given y Q n = {1,..., n, } nd Σ n = { i j 1 i, j, n}; δ n (, i j ) = for every 1 i, j n (tht is, is trp stte), nd q 0n = 1 nd F n = {1}. For exmple, here re K 3 nd A 3 : { if i j δ n (i, jk ) = k if i = j Words ccepted y A n encode circuits of K n. For exmple, nd , which re words ccepted y A 3 encode the circuits 121 nd 1321 of K 3. Clerly, A n recognizes the encodings of ll circuits of K n strting t node 1. A pth expression r over Σ n is regulr expression such tht every word of L(r) models pth of K n. The purpose of this exercise is to show tht every pth expression for L(A n ) nd so every

44 44 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS regulr expression, ecuse ny expression for L(A n ) is pth expression y definition must hve length Ω(2 n ). Let π e circuit of K n. A pth expression r covers π if L(r) contins word u w v such tht w encodes π. Further, r covers π if L(r) covers π k for every k 0. Let r e pth expression of length m strting t node i. Prove: () Either r covers π, or it does not cover π 2m. () If r covers π nd no proper suexpression of r does, then r = s for some expression s, nd every word of L(s) encodes circuit strting t node of π. For every 1 k n + 1, let [k] denote the permuttion of 1, 2,, n + 1 tht cycliclly shifts every index k positions to the right. Formlly, node i is renmed to i+k if i+k n+1, nd to i+k (n+1) otherwise. Let π[k] e the result of pplying the permuttion to π. So, for instnce, if n = 4 nd π = 24142, we get π[ = π[2] = π[3] = π[4] = π[5] = = π (c) Prove tht π[k] is circuit of K n+1 tht does not pss through node k. Define inductively the circuit g n of K n for every n 1 s follows: g 1 = 11 g n+1 = 1g n [ 2n g n [2] 2n g n [n + 2n for every n 1 In prticulr, we hve g 1 = 11 g 2 = 1 (22) 2 (11) 2 g 3 = 1 ( 2 (33) 2 (22) 2) 4 ( 3 (11) 2 (33) 2 3 ) 4 ( 1 (22) 2 (11) 2) 4 (d) Prove using prts ()-(c) tht every pth expression covering g n hs length t lest 2 n 1. Exercise 14 The existentil nd universl ccepting conditions cn e comined, yielding lternting utomt. The sttes of n lternting utomton re prtitioned into existentil nd universl sttes. An existentil stte q ccepts word w (i.e., w L(q)) if w = ε nd q F or w = w nd there exists trnsition (q,, q ) such tht q ccepts w. A universl stte q ccepts word w if w = ε nd q F or w = w nd for every trnsition (q,, q ) the stte q ccepts w. The lnguge recognized y n lternting utomton is the set of words ccepted y its initil stte. Give n lgorithm tht trnsforms n lternting utomton into DFA recognizing the sme lnguge. Exercise 15 Let L e n ritrry lnguge over 1-letter lphet. Prove tht L is regulr.

45 2.5. A TOUR OF CONVERSIONS 45 Exercise 16 In lgorithm NFAεtoNFA for the removl of ɛ-trnsitions, no trnsition tht hs een dded to the workset, processed nd removed from the workset is ever dded to the workset gin. However, trnsitions my e dded to the workset more thn once. Give NFA-ε A nd run of NFAεtoNFA(A) in which this hppens. Exercise 17 We sy tht u = 1 n is scttered suword of w, denoted y u w, if there re words w 0,, w n Σ such tht w = w 0 1 w 1 2 n w n. The upwrd closure of lnguge L is the lnguge L = {u Σ w L : w u}. The downwrd closure of L is the lnguge L := {u Σ w L : u w}. Give lgorithms tht tke NFA A s input nd return NFAs for L(A) nd L(A), respectively. Exercise 18 Algorithm NFAtoRE trnsforms finite utomton into regulr expression representing the sme lnguge y itertively eliminting sttes of the utomton. In this exercise we present n lgeric reformultion of the lgorithm. We represent NFA s system of lnguge equtions with s mny vriles s sttes, nd solve the system y eliminting vriles. A lnguge eqution over n lphet Σ nd set V of vriles is n eqution of the form r 1 = r 2, where r 1 nd r 2 re regulr expressions over Σ V. For instnce, X = X + is lnguge eqution. A solution of system of equtions is mpping tht ssigns to ech vrile X regulr expression over Σ, such tht the lnguges of the left nd right-hnd-sides of ech eqution re equl. For instnce, is solution of X = X + ecuse L( ) = L( + ), nd nd re two different solutions of X = X. (1) Arden s Lemm sttes tht given two lnguges A, B Σ with ε A, the smllest lnguge X Σ stisfying X = AX + B is the lnguge A B. Prove Arden s Lemm. (2) Consider the following system of equtions, where the vriles X, Y represent lnguges (regulr expressions) over the lphet Σ = {,, c, d, e, f }: X = X + Y + c Y = dx + ey + f. This system hs mny solutions. For exmple, X = Y = Σ is solution. But there is gin unique miniml solution, i.e., solution contined in every other solution. Find the smllest solution with the help of Arden s Lemm. Hint : In first step, consider X not s vrile, ut s constnt lnguge, nd solve the eqution for Y using Arden s Lemm. We cn ssocite to ny NFA A = (Q, Σ, δ, q I, F) system of liner equtions s follows. We tke s vriles the sttes of the utomton, which we cll here X, Y, Z,..., with X s initil stte. The system hs n eqution for ech stte X. If X F, then the eqution hs the form X = Y (X,,Y) δ

46 46 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS nd if X F then if X F. X = Y + ε (X,,Y) δ (3) Consider the DFA of Figure 2.14(). Let X, Y, Z, W e the sttes of the utomton, red from top to ottom nd from left to right. The ssocited system of liner equtions is X = Y + Z + ε Y = X + W Z = X + W W = Y + Z Clculte the solution of this liner system y itertively eliminting vriles. Strt with Y, then eliminte Z, nd finlly W. Compre with the elimintion procedure shown in Figure Exercise 19 (Inspired y R. Mjumdr) Consider deck of crds (with ritrry mny crds) in which lck nd red crds lternte, the top crd is lck, nd the ottom crd is red. The set of possile decks is then given y the regulr expression (BR). Cut the deck t ny point into two piles, nd then perform riffle (lso clled dovetil shuffle) to yield new deck. E.g., we cn cut deck with six crds (with 1 s top crd) into two piles 12 nd 3456, nd the riffle yields (we strt the riffle with the first pile). Give regulr expression over the lphet {B, R} descriing the possile configurtions of the decks fter the riffle. Hint: Let Rif (r 1, r 2 ) e the lnguge of ll decks otined y performing riffle on decks from r 1 nd r 2. We re looking for regulr expression for Rif ( (BR) B, R(BR) ) + Rif ( (BR), (BR) ). Use Exercise 18 to set up system of equtions with vriles Rif ( (BR) B, R(BR) ) nd Rif ( (BR), (BR) ), nd solve it. Exercise 20 Given n IN 0, let MSBF(n) e the set of most-significnt-it-first encodings of n, i.e., the words tht strt with n ritrry numer of leding zeros, followed y n written in inry. For exmple: MSBF(3) = 0 11 nd MSBF(9) = MSBF(0) = 0. Similrly, let LSBF(n) denote the set of lest-significnt-it-first encodings of n, i.e., the set contining for ech word w MSBF(n) its reverse. For exmple: LSBF(6) = L(0110 ) nd LSBF(0) = L(0 ).

47 2.5. A TOUR OF CONVERSIONS 47 () Construct nd compre DFAs recognizing the encodings of the even numers n IN 0 w.r.t. the unry encoding, where n is encoded y the word 1 n, the MSBF-encoding, nd the LSBFencoding. () Sme for the set of numers divisile y 3. (c) Give regulr expressions corresponding to the lnguges in (). Exercise 21 Consider the following DFA over the lphet [ ] 0, 0 [ ] 0, 1 [ ] 1, nd 0 [ ] 1 1. [ ] 0 0 [ ] 1 0 [ ] 0 0 [ ] 1 1 [ ] 1 1 [ ] 0 1 A word w encodes pir of nturl numers (X(w), Y(w)), where X(w) nd Y(w) re otined y reding the top nd ottom rows in MSBF encoding, respectively. For instnce, in the word [ ] [ ] [ ] [ ] [ ] [ ] the top nd ottom rows re nd , which in MSBF encoding correspond to 44 nd 19 Show tht the DFA recognizes the set of words w such tht X(w) = 3 Y(w), i.e., the solutions of the eqution x 3y = 0.

48 48 CHAPTER 2. AUTOMATA CLASSES AND CONVERSIONS

49 Chpter 3 Minimiztion nd Reduction In the previous chpter we showed through chin of conversions tht the two DFAs of Figure 3.1 recognize the sme lnguge. Oviously, the utomton on the left of the figure is etter s dt structure for this lnguge, since it hs smller size. A DFA (respectively, NFA) is miniml if Figure 3.1: Two DFAs for the sme lnguge no other DFA (respectively, NFA) recognizing the sme lnguge hs fewer sttes. We show tht every regulr lnguge hs unique miniml DFA up to isomorphism (i.e., up to renming of the sttes). nd present n efficient lgorithm tht minimizes given DFA, i.e., converts it into the unique miniml DFA. In prticulr, the lgorithm converts the DFA on the right of Figure 3.1 into the one on the left. From dt structure point of view, the existence of unique miniml DFA hs two importnt consequences. First, s mentioned ove, the miniml DFA is the one tht cn e stored with miniml mount of memory. Second, the uniqueness of the miniml DFA mkes it cnonicl representtion of regulr lnguge. As we shll see, cnonicity leds to fst equlity check: In order to decide if two regulr lnguges re equl, we cn construct their miniml DFAs, nd check 49

50 50 CHAPTER 3. MINIMIZATION AND REDUCTION if they re isomorphic. In the second prt of the chpter we show tht, unfortuntely, computing miniml NFA is PSPACE complete prolem, for which no efficient lgorithm is likely to exist. Moreover, the miniml NFA is not unique. However, we show tht generliztion of the minimiztion lgorithm for DFAs cn e used to t lest reduce the size of n NFA while preserving its lnguge. 3.1 Miniml DFAs We strt with simple ut very useful definition. Definition 3.1 Given lnguge L Σ nd w Σ, the w-residul of L is the lnguge L w = {u Σ wu L}. A lnguge L Σ is residul of L if L = L w for t lest one w Σ. A lnguge my e infinite ut hve finite numer of residuls. An exmple is the lnguge of the DFAs in Figure 3.1. Recll it is the lnguge of ll words over {, } with n even numer of s nd n even numer of s, which we denote in wht follows y EE. The lnguge hs four residuls, nmely EE, EO, OE, OO, where EO contins the words with n even numer of s nd n odd numer of s, etc. For exmple, we hve EE ε = EE, EE = EE = OE, nd EE = OO. There is close connection etween the sttes of DA (not necessrily finite) nd the residuls of its lnguge. In order to formulte it we introduce the following definition: Definition 3.2 Let A = (Q, Σ, δ, q 0, F) e DA nd let q Q. The lnguge recognized y q, denoted y L A (q), or just L(q) if there is no risk of confusion, is the lnguge recognized y A with q s initil stte, i.e., the lnguge recognized y the DA (Q, Σ, δ, q, F). Figure 3.2 shows the result of leling the sttes of the two DFAs of Figure 3.1 with the lnguges they recognize, which re residuls of EE. Lemm 3.3 Let L e lnguge nd let A = (Q, Σ, δ, q 0, F) e DA recognizing L. (1) Every residul of L is recognized y some stte of A. Formlly: for every w Σ there is q Q such tht L A (q) = L w. (2) Every stte of A recognizes residul of L. Formlly: for every q Q there is w Σ such tht L A (q) = L w. Proof: (1) Let w Σ, nd let q e the stte reched y the unique run of A on w. Then word wu Σ is recognized y A iff u is recognized y A with q s initil stte. So L A (q) = L w. (2) Since A is in norml form, q cn e reched from q 0 y t lest word w. So L A (q) = L w, nd we re done.

51 3.1. MINIMAL DFAS 51 EE OE OE EE EO OO EE EO OE OO EO Figure 3.2: Lnguges recognized y the sttes of the DFAs of Figure 3.1. The notion of residuls llows us to define the cnonicl deterministic utomton for lnguge L Σ. Definition 3.4 Let L Σ e lnguge. The cnonicl DA for L is the tuple C L = (Q L, Σ, δ L, q 0L, F L ), where: Q L is the set of residuls of L; i.e., Q L = {L w w Σ }; δ(k, ) = K for every K Q L nd Σ; q 0L = L; nd F L = {K Q L ε K}. Notice tht the numer of sttes of C L is equl to the numer of residuls of L, nd oth my e infinite. Exmple 3.5 The cnonicl DA for the lnguge EE is shown on the left of Figure 3.2. It hs four sttes, corresponding to the four residuls of EE. Since, for instnce, we hve EE = OE, we hve trnsition leled y leding from EE to OE. It is intuitively cler, nd esy to show, tht the cnonicl DA for lnguge L recognizes L: Proposition 3.6 For every lnguge L Σ, the cnonicl DA for L recognizes L. Proof: Let C L e the cnonicl DA for L. We prove L(C L ) = L. Let w Σ. We prove y induction on w tht w L iff w L(C L ).

52 52 CHAPTER 3. MINIMIZATION AND REDUCTION If w = 0 then w = ε, nd we hve ε L (w = ɛ) L F L (definition of F L ) q 0L F L (q 0L = L) ε L(C L ) (q 0L is the initil stte of C L ) If w > 0, then w = w for some Σ nd w Σ, nd we hve w L w L (definition of L ) w L(C L ) (induction hypothesis) w L(C L ) (δ L (L, ) = L ) We now prove tht C L is the unique miniml DFA recognizing regulr lnguge L (up to isomorphism). The informl rgument goes s follows. Since every DFA for L hs t lest one stte for ech residul nd C L hs exctly one stte for ech residul, C L is miniml, nd every other miniml DFA for L lso hs exctly one stte for ech residul. But the trnsitions, initil nd finl sttes of miniml DFA re completely determined y the residuls of the sttes: if stte q recognizes residul R, then the -trnsition leving q necessrily leds to the stte recognizing R ; q is finl iff ɛ R, nd q is initil iff R = L. So ll miniml DFAs re isomorphic. A more forml proof looks s follows: Theorem 3.7 If L is regulr, then C L is the unique miniml DFA up to isomorphism recognizing L. Proof: Let L e regulr lnguge, nd let A = (Q, Σ, δ, q 0, F) e n ritrry DFA recognizing L. By Lemm 3.3 the numer the numer of sttes of A is greter thn or equl to the numer of sttes of C L, nd so C L is miniml utomton for L. To prove uniqueness of the miniml utomton up to isomorphism, ssume A is miniml. By Lemm 3.3(2), L A is mpping tht ssigns to ech stte of A residul of L, nd so L A : Q Q L. We prove tht L A is n isomorphism. L A is ijective ecuse it is surjective (Lemm 3.3(2)), nd Q = Q L (A is miniml y ssumption). Moreover, if δ(q, ) = q, then L A (q ) = (L A (q)), nd so δ L (L A (q), ) = L A (q ). Also, L A mps the initil stte of A to the initil stte of C L : L A (q 0 ) = L = q 0L. Finlly, L A mps finl to finl nd non-finl to non-finl sttes: q F iff ε L A (q) iff L A (q) F L. The following simple corollry is often useful to estlish tht given DFA is miniml: Corollry 3.8 A DFA is miniml if nd only if L(q) L(q ) for every two distinct sttes q nd q.

53 3.2. MINIMIZING DFAS 53 Proof: ( ): By Theorem 3.7, the numer of sttes of miniml DFA is equl to the numer of residuls of its lnguge. Since every stte of recognizes some residul, ech stte must recognize different residul. ( ): If ll sttes of DFA A recognize different lnguges, then, since every stte recognizes some residul, the numer of sttes of A is less thn or equl to the numer of residuls. So A hs t most s mny sttes s C L(A), nd so it is miniml. 3.2 Minimizing DFAs We present n lgorithm tht converts given DFA into ( DFA isomorphic to) the unique miniml DFA recognizing the sme lnguge. The lgorithm first prtitions the sttes of the DFA into locks, where lock contins ll sttes recognizing the sme residul. We cll this prtition the lnguge prtition. Then, the lgorithm merges the sttes of ech lock into one single stte, n opertion usully clled quotienting with respect to the prtition. Intuitively, this yields DFA in which every stte recognizes different residul. These two steps re descried in Section nd Section For the rest of the section we fix DFA A = (Q, Σ, δ, q 0, F) recognizing regulr lnguge L Computing the lnguge prtition We need some sic notions on prtitions. A prtition of Q is finite set P = {B 1,..., B n } of nonempty susets of Q, clled locks, such tht Q = B 1... B n, nd B i B j = for every 1 i j n. The lock contining stte q is denoted y [q] P. A prtition P refines or is refinement of nother prtition P if every lock of P is contined in some lock of P. If P refines P nd P P, then P is corser thn P. The lnguge prtition, denoted y P l, puts two sttes in the sme lock if nd only if they recognize the sme lnguge (i.e, the sme residul). To compute P l we itertively refine n initil prtition P 0 while mintining the following Invrint: Sttes in different locks recognize different lnguges. P 0 consists of two locks contining the finl nd the non-finl sttes, respectively (or just one of the two if ll sttes re finl or ll sttes re nonfinl). Tht is, P 0 = {F, Q \ F} if F nd Q \ F re nonempty, P 0 = {F} if Q \ F is empty, nd P 0 = {Q \ F} if F is empty. Notice tht P 0 stisfies the invrint, ecuse every stte of F ccepts the empty word, ut no stte of Q \ F does. A prtition is refined y splitting lock into two locks. To find lock to split, we first oserve the following: Fct 3.9 If L(q 1 ) = L(q 2 ), then L(δ(q 1, )) = L(δ(q 2, )) for every Σ. Now, y contrposition, if L(δ(q 1, )) L(δ(q 2, )), then L(q 1 ) L(q 2 ), or, rephrsing in terms of locks: if δ(q 1, ) nd δ(q 2, ) elong to different locks, ut q 1 nd q 2 elong to the sme lock B, then B cn e split, ecuse q 1 nd q 2 cn e put in different locks while respecting the invrint.

54 54 CHAPTER 3. MINIMIZATION AND REDUCTION Definition 3.10 Let B, B e (not necessrily distinct) locks of prtition P, nd let Σ. The pir (, B ) splits B if there re q 1, q 2 B such tht δ(q 1, ) B nd δ(q 2, ) B. The result of the split is the prtition Ref P [B,, B ] = (P \ {B}) {B 0, B 1 }, where B 0 = {q B δ(q, ) B } nd B 1 = {q B δ(q, ) B }. A prtition is unstle if it contins locks B, B such tht (, B ) splits B for some Σ, nd stle otherwise. The prtition refinement lgorithm LnPr(A) itertively refines the initil prtition of A until it ecomes stle. The lgorithm termintes ecuse every itertion increses the numer of locks y one, nd prtition cn hve t most Q locks. LnPr(A) Input: DFA A = (Q, Σ, δ, q 0, F) Output: The lnguge prtition P l. 1 if F = or Q \ F = then return {Q} 2 else P {F, Q \ F} 3 while P is unstle do 4 pick B, B P nd Σ such tht (, B ) splits B 5 P Ref P [B,, B ] 6 return P Notice tht if ll sttes of DFA re nonfinl then every stte recognizes, nd if ll re finl then every stte recognizes Σ. In oth cses ll sttes recognize the sme lnguge, nd the lnguge prtition is {Q}. Exmple 3.11 Figure 3.3 shows run of LnPr on the DFA on the right of Figure 3.1. Sttes tht elong to the sme lock hve the sme color. The initil prtition, shown t the top, consists of the yellow nd the pink sttes. The yellow lock nd the letter split the pink lock into the green lock (pink sttes with n -trnsition to the yellow lock) nd the rest (pink sttes with n -trnsition to other locks), which sty pink. In the finl step, the green lock nd the letter split the pink lock into the mgent lock (pink sttes with trnsition into the green lock) nd the rest, which sty pink. We prove correctness of LnPr in two steps. First, we show tht it computes the corsest stle refinement of P 0, denoted y CSR; in other words, we show tht fter termintion the prtition P is corser thn every other stle refinement of P 0. Then we prove tht CSR is equl to P l. Lemm 3.12 LnPr(A) computes CSR.

55 3.2. MINIMIZING DFAS 55 Figure 3.3: Computing the lnguge prtition for the DFA on the left of Figure 3.1

56 56 CHAPTER 3. MINIMIZATION AND REDUCTION Proof: LnPr(A) clerly computes stle refinement of P 0. We prove tht fter termintion P is corser thn ny other stle refinement of P 0, or, equivlently, tht every stle refinement of P 0 refines P. Actully, we prove tht this holds not only fter termintion, ut t ny time. Let P e n ritrry stle refinement of P 0. Initilly P = P 0, nd so P refines P. Now, we show tht if P refines P, then P lso refines Ref P [B,, B ]. For this, let q 1, q 2 e two sttes elonging to the sme lock of P. We show tht they elong to the sme lock of Ref P [B,, B ]. Assume the contrry. Since the only difference etween P nd Ref P [B,, B ] is the splitting of B into B 0 nd B 1, exctly one of q 1 nd q 2, sy q 1, elongs to B 0, nd the other elongs to B 1. So there exists trnsition (q 2,, q 2 ) δ such tht q 2 B. Since P is stle nd q 1, q 2 elong to the sme lock of P, there is lso trnsition (q 1,, q 1 ) δ such tht q 1 B. But this contrdicts q 1 B 0. Theorem 3.13 CSR is equl to P l. Proof: The proof hs three prts: () P l refines P 0. Ovious. () P l is stle. By Fct 3.9, if two sttes q 1, q 2 elong to the sme lock of P l, then δ(q 1, ), δ(q 2, ) lso elong to the sme lock, for every. So no lock cn e split. (c) Every stle refinement P of P 0 refines P l. Let q 1, q 2 e sttes elonging to the sme lock B of P. We prove tht they elong to the sme lock of P l, i.e., tht L(q 1 ) = L(q 2 ). By symmetry, it suffices to prove tht, for every word w, if w L(q 1 ) then w L(q 2 ). We proceed y induction on the length of w. If w = ε then q 1 F, nd since P refines P 0, we hve q 2 F, nd so w L(q 2 ). If w = w, then there is (q 1,, q 1 ) δ such tht w L(q 1 ). Let B e the lock contining q 1. Since P is stle, B does not split B, nd so there is (q 2,, q 2 ) δ such tht q 2 B. By induction hypothesis, w L(q 1 ) iff w L(q 2 ). So w L(q 2 ), which implies w L(q 2) Quotienting It remins to define the quotient of A with respect to prtition. It is convenient to define it not only for DFAs, ut more generlly for NFAs. The sttes of the quotient re the locks of the prtition, nd there is trnsition (B,, B ) from lock B to lock B if A contins some trnsition (q,, q ) for sttes q nd Q elonging to B nd B, respectively. Formlly: Definition 3.14 The quotient of NFA A with respect to prtition P is the NFA A/P = (Q P, Σ, δ P, Q 0P, F P ) where Q P is the set of locks of P;

57 3.2. MINIMIZING DFAS 57 (B,, B ) δ P if (q,, q ) δ for some q B, q B ; Q 0P is the set of locks of P tht contin t lest one stte of Q 0 ; nd F P is the set of locks of P tht contin t lest one stte of F. Exmple 3.15 Figure 3.4 shows on the right the result of quotienting the DFA on the left with respect to its lnguge prtition. The quotient hs s mny sttes s colors, nd it hs trnsition etween two colors (sy, n -trnsition from pink to mgent) if the DFA on the left hs such trnsition. Figure 3.4: Quotient of DFA with respect to its lnguge prtition We show tht A/P l, the quotient of DFA A with respect to the lnguge prtition, is the miniml DFA for L. The min prt of the rgument is contined in the following lemm. Loosely speking, it sys tht ny refinement of the lnguge prtition, i.e., ny prtition in which sttes of the sme lock recognize the sme lnguge, is good for quotienting, ecuse the quotient recognizes the sme lnguge s the originl utomton. Moreover, if the prtition not only refines ut is equl to the lnguge prtition, then the quotient is DFA. Lemm 3.16 Let A e NFA, nd let P e prtition of the stte of A. If P refines P l, then L A (q) = L A/P (B) for every stte q of A, where B is the lock of P contining q; in prticulr L(A/P) = L(A). Moreover, if A is DFA nd P = P l, then A/P is DFA. Proof: Let P e ny refinement of P l. We prove tht for every w Σ we hve w L A (q) iff w L A/P (B). The proof is y induction on w. w = 0. Then w = ε nd we hve

58 58 CHAPTER 3. MINIMIZATION AND REDUCTION ε L A (q) iff q F iff B F (P refines P l, nd so lso P 0 ) iff B F P iff ε L A/P (B) w > 0. Then w = w for some Σ. So w L A (q) iff there is trnsition (q,, q ) δ such tht w L A (q ). Let B e the lock contining q. By the definition of A/P we hve (B,, B ) δ P, nd so: w L A (q) iff w L A (q ) (definition of q ) iff w L A/P (B ) (induction hypothesis) iff w L A/P (B) ( (B,, B ) δ P ) For the second prt, show tht (B,, B 1 ), (B,, B 2 ) δ Pl implies B 1 = B 2. By definition there exist (q,, q 1 ), (q,, q 2 ) δ for some q, q B, q 1 B 1, nd q 2 B 2. Since q, q elong to the sme lock of the lnguge prtition, we hve L A (q) = L A (q ). Since A is DFA, we get L A (q 1 ) = L A (q 2 ). Since P = P l, the sttes q 1 nd q 2 elong to the sme lock, nd so B 1 = B 2. Proposition 3.17 The quotient A/P l is the miniml DFA for L. Proof: By Lemm 3.16, A/P l is DFA, nd its sttes recognize residuls of L. Moreover, two sttes of A/P l recognize different residuls y definition of the lnguge prtition. So A/P l hs s mny sttes s residuls, nd we re done Hopcroft s lgorithm Algorithm LnPr leves the choice of of n dequte refinement triple [B,, B ] open. While ny exhustive sequence of refinements will need to the sme result, nd so does not ffect the correctness of the lgorithm, it ffects its runtime. Hopcroft s lgorithm is modifiction of LnPr which crefully selects the next triple. When properly implemented, Hopcroft s lgorithm runs in time O(mn log n) for DFA with n sttes over m-letter lphet. However, full nlysis is eyond the scope of this ook, nd so we limit ourselves to presenting some of the min ides. It is convenient to strt y descriing n intermedite lgorithm, not s efficient s the finl one. The intermedite lgorithm mintins worset of pirs (, B ), clled splitters. Initilly, the workset contins ll pirs (, B ) where Σ is n ritrry letter nd B is lock of the originl prtition (tht is, either B = F or B = Q \ F). At every step, the lgorithm chooses splitter from the workset, nd uses it to split every lock of the current prtition (if possile). Whenever lock B is split y (, B ) into two new locks B 0 nd B 1, the lgorithm dds to the workset ll pirs (, B 0 ) nd (, B 1 ) for every letter Σ. It is not difficult to see tht the intermedite lgorithm is correct. The only point requiring moment of thought is the fct tht ech splitter is used t most once. A priori the sme splitter

59 3.2. MINIMIZING DFAS 59 (, B ) could required t some point of the execution, nd then lter gin. To discrd this oserve tht, y the definition of split, if (, B ) splits lock B into B 0 nd B 1, then it does not split ny suset of B 0 or B 1. So, fter (, B ) is used to split ll locks of prtition, since ll future locks re strict susets of the current locks, (, B ) is not useful nymore. Hopcroft s lgorithm improves on the intermedite lgorithm y oserving tht when lock B is split into B 0 nd B 1, it is not lwys necessry to dd oth (, B 0 ) nd (, B 1 ) to the workset. The fundment for this is the following proposition: Proposition 3.18 Let A = (Q, Σ, δ, q 0, F), let P e prtition of Q, nd let B e lock of P. Suppose we refine B into B 0 nd B 1. Then, for every Σ, refining ll locks of P with respect to ny two of the splitters (, B), (, B 0 ), nd (, B 1 ) gives the sme result s refining them with respect to ll three of them. Proof: Let C e lock of P. Every refinement sequence with respect to two of the splitters (there re six possile cses) yields the sme prtition of C, nmely {C 0, C 1, C 2 }, where C 0 nd C 1 contin the sttes q Q such tht δ(q, ) B 0 nd δ(q, ) B 1, respectively, nd C 2 contins the sttes q Q such tht δ(q, ) B. Now, ssume tht (, B ) splits lock B into B 0 nd B 1. For every Σ, if (, B) is in the workset, then dding oth (, B 0 ) nd (, B 1 ) is redundnt, ecuse we only need two of the three. In this cse, Hopcroft s lgorithm chooses to replce (, B) in the workset y (, B 0 ) nd (, B 1 ) (tht is, to remove (, B) nd to dd (, B 0 ) nd (, B 1 )). If (, B) is not in the workset, then in principle we could hve two possile cses. If (, B) ws lredy removed from the workset nd used to refine, then we only need to dd one of (, B 0 ) nd (, B 1 ). Hopcroft s lgorithm dds the smller of the two (i.e., (, B 0 ) if B 0 B 1, nd (, B 1 ) otherwise). If (, B) hs not een dded to the workset yet, then it looks s if we would still hve to dd oth of (, B 0 ) nd (, B 1 ). However, more detiled nlysis shows tht this is not the cse, it suffices gin to dd only one of (, B 0 ) nd (, B 1 ), nd Hopcroft s lgorithm dds gin the smller of the two. These considertions led to the following pseudocode for Hopcroft s lgorithm, where (, min B 0, B 1 ) denotes the smller of (, B 0 ) nd (, B 1 ):

60 60 CHAPTER 3. MINIMIZATION AND REDUCTION Hopcroft(A) Input: DFA A = (Q, Σ, δ, q 0, F) Output: The lnguge prtition P l. 1 if F = or Q \ F = then return {Q} 2 else P {F, Q \ F} 3 W { (, min{f, Q \ F}) Σ} 4 while W do 5 pick (, B ) from W 6 for ll B P split y (, B ) do 7 replce B y B 0 nd B 1 in P 8 for ll Σ do 9 if (, B) W then replce (, B) y (, B 0 ) nd (, B 1 ) in W 10 else dd (, min{b 0, B 1 }) to W 11 return P We sketch n rgumentt showing tht the while loop is executed t most O(mn log n) times, where m = Σ nd n = Q. Fix stte q Q nd letter Σ. It is esy to see tht t every moment during the execution of Hopcroft the workset contins t most one splitter (, B) such tht q B (in prticulr, if (, B) is in the workset nd B is split t line 9, then q goes to either B 0 or to B 1 ). We cll this splitter (if present) the -q-splitter, nd define its size s the size of the lock B. So during the execution of the lgorithm there re lternting phses in which the workset contins one or zero -q-splitters, respectively. Let us cll them one-phses nd zero-phses, respectively. It is esy to see tht during one-phse the size of the -q-splitter (defined s the numer of sttes in the lock) cn only decrese (t line 9). Moreover, if t the end of one-phse the -q-splitter hs size k, then, ecuse of line??, t the eginning of the next one-phse it hs size t most k/2. So the numer of -q-splitters dded to the workset throughout the execution of the lgorithm is O(log n), nd therefore the totl numer of splitters dded to the workset is O(mn log n). So the while loop is executed O(mn log n) times. If the lgorithm is crefully implemented (which is non-trivil), then it lso runs in O(mn log n) time. 3.3 Reducing NFAs There is no cnonicl miniml NFA for given regulr lnguge. The simplest witness of this fct is the lnguge, which is recognized y the two non-isomorphic, miniml NFAs of Figure 3.5. Moreover, computing ny of the miniml NFAs equivlent to given NFA is computtionlly hrd. Recll tht the universlity prolem is PSPACE-complete: given NFA A over n lphet Σ, decide whether L(A) = Σ. Using this result, we cn esily prove tht deciding the existence of smll NFA for lnguge is PSPACE-complete. Theorem 3.19 The following prolem is PSPACE-complete: given NFA A nd numer k 1, decide if there exists n NFA equivlent to A hving t most k sttes.

61 3.3. REDUCING NFAS 61 Figure 3.5: Two miniml NFAs for. Proof: To prove memership in PSPACE, oserve first tht if A hs t most k sttes, then we cn nswer A. So ssume tht A hs more thn k sttes. We use NPSPACE = PSPACE = co-pspace. Since PSPACE = co-pspace, it suffices to give procedure to decide if no NFA with t most k sttes is equivlent to A. For this we construct ll NFAs with t most k sttes (over the sme lphet s A), reusing the sme spce for ech of them, nd check tht none of them is equivlent to A. Now, since NPSPACE=PSPACE, it suffices to exhiit nondeterministic lgorithm tht, given NFA B with t most k sttes, checks tht B is not equivlent to A (nd runs in polynomil spce). The lgorithm nondeterministiclly guesses word, one letter t time, while mintining the sets of sttes in oth A nd B reched from the initil sttes y the word guessed so fr. The lgorithm stops when it oserves tht the current word is ccepted y exctly one of A nd B. PSPACE-hrdness is esily proved y reduction from the universlity prolem. If n NFA is universl, then it is equivlent to n NFA with one stte, nd so, to decide if given NFA A is universl we cn proceed s follows: Check first if A ccepts ll words of length 1. If not, then A is not universl. Otherwise, check if some NFA with one stte is equivlent to A. If not, then A is not universl. Otherwise, if such NFA, sy B, exists, then, since A ccepts ll words of length 1, B is the NFA with one finl stte nd loop for ech lphet letter. So A is universl. However, we cn reuse prt of the theory for the DFA cse to otin n efficient lgorithm to possily reduce the size of given NFA The reduction lgorithm We fix for the rest of the section n NFA A = (Q, Σ, δ, Q 0, F) recognizing lnguge L. Recll tht Definition 3.14 nd the first prt of Lemm 3.16 were defined for NFA. So L(A) = L(A/P) holds for every refinement P of P l, nd so ny refinement of P l cn e used to reduce A. The lrgest reduction is otined for P = P l, ut P l is hrd to compute for NFA. On the other extreme, the prtition tht puts ech stte in seprte lock is lwys refinement of P l, ut it does not provide ny reduction. To find resonle trde-off we exmine gin Lemm 3.12, which proves tht LnPr(A) computes CSR for deterministic utomt. Its proof only uses the following property of stle prtitions: if q 1, q 2 elong to the sme lock of stle prtition nd there is trnsition (q 2,, q 2 ) δ such tht q 2 B for some lock B, then there is lso trnsition (q 1,, q 1 ) δ such tht q 1 B. We extend the definition of stility to NFAs so tht stle prtitions still stisfy this property: we just replce condition

62 62 CHAPTER 3. MINIMIZATION AND REDUCTION of Definition 3.10 y δ(q 1, ) B nd δ(q 2, ) B δ(q 1, ) B nd δ(q 2, ) B =. Definition 3.20 (Refinement nd stility for NFAs) Let B, B e (not necessrily distinct) locks of prtition P, nd let Σ. The pir (, B ) splits B if there re q 1, q 2 B such tht δ(q 1, ) B nd δ(q 2, ) B =. The result of the split is the prtition Ref NFA P [B,, B ] = (P\{B}) {B 0, B 1 }, where B 0 = {q B δ(q, ) B = } nd B 1 = {q B δ(q, ) B }. A prtition is unstle if it contins locks B, B such tht B splits B, nd stle otherwise. Using this definition we generlize LnPr(A) to NFAs in the ovious wy: llow NFAs s inputs, nd replce Ref P y Ref NFA P s new notion of refinement. Lemm 3.12 still holds: the lgorithm still computes CSR, ut with respect to the new notion of refinement. Notice tht in the specil cse of DFAs it reduces to LnPr(A), ecuse Ref P nd Ref NFA P coincide for DFAs. CSR(A) Input: NFA A = (Q, Σ, δ, Q 0, F) Output: The prtition CSR of A. 1 if F = or Q \ F = then P {Q} 2 else P {F, Q \ F} 3 while P is unstle do 4 pick B, B P nd Σ such tht (, B ) splits B 5 P Ref NFA P [B,, B ] 6 return P Notice tht line 1 is different from line 1 in lgorithm LnPr. If ll sttes of NFA re nonfinl then every stte recognizes, ut if ll re finl we cn no longer conclude tht every stte recognizes Σ, s ws the cse for DFAs. In fct, ll sttes might recognize different lnguges. In the cse of DFAs we hd Theorem 3.13, stting tht CSR is equl to P l. The theorem does not hold nymore for NFAs, s we will see lter. However, prt (c) of the proof, which showed tht CSR refines P l, still holds, with exctly the sme proof. So we get: Theorem 3.21 Let A = (Q, Σ, δ, Q 0, F) e NFA. The prtition CSR refines P l. Now, Lemm 3.16 nd Theorem 3.13 led to the finl result: Corollry 3.22 Let A = (Q, Σ, δ, Q 0, F) e NFA. Then L(A/CSR) = L(A).

63 3.3. REDUCING NFAS 63,,, ,,, {1,..., 14} {15} (, {15}) {1,..., 8, 11, 12, 13} {9, 10, 14} (, {9, 10, 14}) {1, 2, 6, 7, 11, 12} (, {3, 4, 5, 8, 13}) {3, 4, 5, 8, 13} (, {3, 4, 5, 8, 13}) {1, 6, 11} (, {4, 8}) {2, 7, 12} (, {4, 8}) {4, 8} {3, 5, 13} (, {4, 8}) {1} {6, 11} {7, 12} {2} {3} {5, 13} (, {6, 11}) {6} {11} Figure 3.6: An NFA nd run of CSR() on it.

64 64 CHAPTER 3. MINIMIZATION AND REDUCTION Exmple 3.23 Consider the NFA t the top of Figure 3.6. CSR is the prtition indicted y the colors. A possile run of CSR(A) is grphiclly represented t the ottom of the figure s tree. Initilly we hve the prtition with two locks shown t the top of the figure: the lock {1,..., 14} of non-finl sttes nd the lock {15} of finl sttes. The first refinement uses (, {15}) to split the lock of non-finl sttes, yielding the locks {1,..., 8, 11, 12, 13} (no -trnsition to {15}) nd {9, 10, 14} (n -trnsition to {15}). The leves of the tree re the locks of CSR. In this exmple we hve CSR P l. For instnce, sttes 3 nd 5 recognize the sme lnguge, nmely ( + ) ( + ), ut they elong to different locks of CSR. The quotient utomton is shown in Figure 3.7.,,,,, Figure 3.7: The quotient of the NFA of Figure 3.6. We finish the section with remrk. Remrk 3.24 If A is n NFA, then A/P l my not e miniml NFA for L. The NFA of Figure 3.8 is n exmple: ll sttes ccept different lnguges, nd so A/P l = A, ut the NFA is not miniml, since, for instnce, the stte t the ottom cn e removed without chnging the lnguge.

65 3.4. A CHARACTERIZATION OF THE REGULAR LANGUAGES 65, Figure 3.8: An NFA A such tht A/P l is not miniml. It is not difficult to show tht if two sttes q 1, q 2 elong to the sme lock of CSR, then they not only recognize the sme lnguge, ut lso stisfy the following fr stronger property: for every Σ nd for every q 1 δ(q 1, ), there exists q 2 δ(q 2, ) such tht L(q 1 ) = L(q 2 ). This cn e used to show tht two sttes elong to different locks of CSR. For instnce, consider sttes 2 nd 3 of the NFA on the left of Figure 3.9. They recognize the sme lnguge, ut stte 2 hs c-successor, nmely stte 4, tht recognizes {d}, while stte 3 hs no such successor. So sttes 2 nd 3 elong to different locks of CSR. A possile run of of the CSR lgorithm on this NFA is shown on the right of the figure. For this NFA, CSR hs s mny locks s sttes. {1,..., 6} {7} c c c 4 (d, {7}) d e 5 d, e 6 {1, 2, 3, 5} {4, 6} 7 (e, {7}) (e, {7}) {1, 2, 3} {5} {4} {6} (c, {5}) {1, 3} {2} (, {1, 3}) {1} {3} Figure 3.9: An NFA such tht CSR P l. 3.4 A Chrcteriztion of the Regulr Lnguges We present useful yproduct of the results of Section 3.1. Theorem 3.25 A lnguge L is regulr iff it hs finitely mny residuls.

66 66 CHAPTER 3. MINIMIZATION AND REDUCTION Proof: If L is not regulr, then no DFA recognizes it. Since, y Proposition 3.6, the cnonicl utomton C L recognizes L, then C L necessrily hs infinitely mny sttes, nd so L hs infinitely mny residuls. If L is regulr, then some DFA A recognizes it. By Lemm 3.3, the numer of sttes of A is greter thn or equl to the numer of residuls of L, nd so L hs finitely mny residuls. This theorem provides useful technique for proving tht given lnguge L Σ is not regulr: exhiit n infinite set of words W Σ with pirwise different residuls, i.e., W must stisfy L w L v for every two distinct words w, v W. Let us pply the technique to some typicl exmples. { n n n 0} is not regulr. Let W = { k k 0}. For every two distinct words i, j W (i.e., i j), we hve i L i ut i L j. {ww w Σ } is not regulr. Let W = Σ. For every two distinct words w, v W (i.e., w v), we hve w L w ut w L v. { n2 n 0}. Let W = { n2 n 0} (W = L in this cse). For every two distinct words i2, j2 W (i.e., i j), we hve tht 2i+i elongs to the i2 -residul of L, ecuse i2 +2i+1 = (i+1)2, ut not to the j2 -residul, ecuse j2 +2i+1 is only squre numer for i = j. Exercises Exercise 22 Consider the most-significnt-it-first encoding (MSBF encoding) of nturl numers over the lphet Σ = {0, 1}. In this exercise we ssume tht every numer hs infinitely mny encodings, ecuse ll the words of 0 w encode the sme numer s w. Construct the miniml DFAs ccepting the following lnguges, where Σ 4 denotes ll words of length 4. () {w MSBF 1 (w) mod 3 = 0} Σ 4. () {w MSBF 1 (w) is prime } Σ 4. Exercise 23 Consider the lnguge prtition lgorithm LnPr. Since every execution of its while loop increses the numer of locks y one, the loop cn e executed t most Q 1 times. Show tht this ound is tight, i.e. give fmily of DFAs for which the loops is executed Q 1 times. Hint: You cn tke one-letter lphet. Exercise 24 Descrie in words the lnguge of the following NFA over the lphet {, }, nd compute CSR, i.e., the corsest stle refinement of P 0.

67 3.4. A CHARACTERIZATION OF THE REGULAR LANGUAGES 67 q 1 q 2 q 0,, q f q 3 q 4 Exercise 25 Determine the residuls of the following lnguges over Σ = {, }: ( + ), (), nd { n n c n n 0}. Exercise 26 Consider the fmily of lnguges L k = {ww w Σ k }, where k 2. (1) Construct the miniml DFA for L 2. (2) How mny sttes hs the miniml DFA ccepting L k? Exercise 27 Given lnguge L Σ nd w Σ, we denote w L = {u Σ uw L}. A lnguge L Σ is n inverse residul of L if L = w L for some w Σ. (1) Determine the inverse residuls of the first two lnguges in Exercise 25. (2) Show tht lnguge is regulr iff it hs finitely mny inverse residuls. (3) Does lnguge lwys hve s mny residuls s inverse residuls? Exercise 28 Given regulr expression r over Σ, let E(r) = ε if ε L(r), nd E(r) = otherwise. Fore every Σ, define the regulr expression r inductively s follows: = ε = ; (r 1 + r 2 ) = r 1 + r 2 ; (r 1 r 2 ) = r 1 r 2 + E(r 1 )r 2 ; (r ) = r r. Prove L(r ) = ( L(r) ). Exercise 29 A DFA with negtive trnsitions (DFA-n) is DFA whose trnsitions re prtitioned into positive nd negtive trnsitions. A run of DFA-n is ccepting if:

68 68 CHAPTER 3. MINIMIZATION AND REDUCTION it ends in finl stte nd the numer of occurrences of negtive trnsitions is even, or it ends in non-finl stte nd the numer of occurrences of negtive trnsitions is odd. The intuition is tht tking negtive trnsition inverts the polrity of the cceptnce condition: fter tking the trnsition we ccept iff we would not ccept were the trnsition positive. Prove tht the lnguges recognized y DFAs with negtive trnsitions re regulr. Give DFA-n for regulr lnguge hving fewer sttes thn the miniml DFA for the lnguge. Show tht the miniml DFA-n for lnguge is not unique (even for lnguges whose miniml DFA-n s hve fewer sttes thn their miniml DFAs). Exercise 30 A residul of regulr lnguge L is composite if it is the union of other residuls of L. A residul of L is prime if it is not composite. Show tht every regulr lnguge L is recognized y n NFA whose numer of sttes is equl to the numer of prime residuls of L. Exercise 31 Prove or disprove: A suset of regulr lnguge is regulr. A superset of regulr lnguge is regulr. If L 1 nd L 1 L 2 re regulr, then L 2 is regulr. If L 2 nd L 1 L 2 re regulr, then L 1 is regulr. Exercise 32 (T. Henzinger) Which of these lnguges over the lphet {0, 1} re regulr? (1) The set of words contining the sme numer of 0 s nd 1 s. (2) The set of words contining the sme numer of occurrences of the strings 01 nd 10. (E.g., contins three occurrences of 01 nd two occurrences of 10.) (3) Sme for the pir of strings 00 nd 11, the pir 001 nd 110, nd the pir 001 nd 100. Exercise 33 A word w = 1... n is suword of v = 1... m, denoted y w v, if there re indices 1 i 1 < i 2... < i n m such tht i j = j for every j {1,... n}. Higmn s lemm sttes tht every infinite set of words over finite lphet contins two words w 1, w 2 such tht w 1 w 2. A lnguge L Σ is upwrd-closed, resp. downwrd-closed), if for every two words w, v Σ, if w L nd w v, then v L, resp. if w L nd w v, then v L. The upwrd-closure of lnguge L is the upwrd-closed lnguge otined y dding to L ll words v such tht w v for some v L.

69 3.4. A CHARACTERIZATION OF THE REGULAR LANGUAGES 69 (1) Prove using Higmn s lemm tht every upwrd-closed lnguge is regulr. Hint: Consider the miniml words of L, i.e., the words w L such tht no proper suword of w elongs to L. (2) Prove tht every downwrd-closed lnguge is regulr. (3) Give regulr expressions for the upwrd nd downwrd closures of { n n min n 0}. (4) Give lgorithms tht trnsform regulr expression r for lnguge into regulr expressions r nd r for its upwrd-closure nd its downwrd-closure. (5) Give lgorithms tht trnsform n NFA A recognizing lnguge into NFAs A nd A recognizing its upwrd-closure nd its downwrd-closure. Exercise 34 (Adull, Boujjni, nd Jonsson) An tomic expression over n lphet Σ is n expression of the form, ε, ( + ε) or ( n ), where, 1,..., n Σ. A product is conctention e 1 e 2... e n of tomic expressions. A simple regulr expression is sum p p n of products. (1) Prove tht the lnguge of simple regulr expression is downwrd-closed (see Exercise??). (2) Prove tht every downwrd-closed lnguge cn e represented y simple regulr expression. Hint: since every downwrd-closed lnguge is regulr, it is represented y regulr expression. Prove tht this expression is equivlent to simple regulr expression. Exercise 35 A word of lnguge L is miniml if it is not proper suword of nother word of L. We denote the set of miniml words of L y min(l). Give fmily of regulr lnguges {L n } n=1 such tht every L n is recognized y NFA with O(n) sttes, ut the smllest NFA recognizing min(l) hs O(2 n ) sttes. Exercise 36 Consider the lphet Σ = {up, down, left, right}. A word over Σ corresponds to line in grid consisting of conctented segments drwn in the direction specified y the letters. In the sme wy, lnguge corresponds to set of lines. For exmple, the set of ll stircses cn e specified s the set of lines given y the regulr lnguge (up right). It is regulr lnguge. () Specify the set of ll skylines s regulr lnguge (i.e., formlize the intuitive notion of skyline). From the lines elow, the one on the left is skyline, while the other two re not. () Show tht the set of ll rectngles is not regulr.

70 70 CHAPTER 3. MINIMIZATION AND REDUCTION Exercise 37 A NFA A = (Q, Σ, δ, Q 0, F) is em reverse-deterministic if (q 1,, q) δ nd (q 2,, q) trns implies q 1 = q 2, i.e., no stte hs two input trnsitions lelled y the sme letter. Further, A is trimmed if every stte ccepts t lest one word, i.e., if L A (q) for every q Q. Let A e reverse-deterministic, trimmed NFA with one single finl stte q f. Prove tht NFAtoDFA(A) is miniml DFA. Hint: Show tht ny two distinct sttes of NFAtoDFA(A) recognize different lnguges, nd pply Proposition??. Exercise 38 Let Rev(A) e the lgorithm of Exercise 7 tht, given NFA A s input, returns trimmed NFA A R such tht L(A R ) = ( L(A)) R, where L R denotes the reverse of L (see Exercise 7). Recll tht NFA is trimmed if every stte ccepts t lest one word (see Exercise 37). Prove tht for every NFA A the DFA is the unique miniml DFA recognizing L(A). NFAtoDFA ( Rev ( NFAtoDFA( Rev(A) ) ) )

71 Chpter 4 Opertions on Sets: Implementtions Recll the list of opertions on sets tht should e supported y our dt structures, where U is the universe of ojects, X, Y re susets of U, x is n element of U: Memer(x, X) : returns true if x X, flse otherwise. Complement(X) : returns U \ X. Intersection(X, Y) : returns X Y. Union(X, Y) : returns X Y. Empty(X) : returns true if X =, flse otherwise. Universl(X) : returns true if X = U, flse otherwise. Included(X, Y) : returns true if X Y, flse otherwise. Equl(X, Y) : returns true if X = Y, flse otherwise. We fix n lphet Σ, nd ssume tht there exists ijection etween U nd Σ, i.e., we ssume tht ech oject of the universe is encoded y word, nd ech word is the encoding of some oject. Under this ssumption, the opertions on sets nd elements ecome opertions on lnguges nd words. For instnce, the first two opertions ecome Memer(w, L) : returns true if w L, flse otherwise. Complement(L) : returns L. The ssumption tht ech word encodes some oject my seem too strong. Indeed, the lnguge E of encodings is usully only suset of Σ. However, once we hve implemented the opertions ove under this strong ssumption, we cn esily modify them so tht they work under much weker ssumption, tht lmost lwys holds: the ssumption tht the lnguge E of encodings is regulr. Assume, for instnce, tht E is regulr suset of Σ nd L is the lnguge of encodings of set X. Then, we implement Complement(X) so tht it returns, not L, ut Intersection(L, E). For ech opertion we present n implementtion tht, given utomt representtions of the 71

72 72 CHAPTER 4. OPERATIONS ON SETS: IMPLEMENTATIONS opernds, returns n utomton representing the result (or oolen, when tht is the return type). Sections 4.1 nd 4.2 consider the cses in which the representtion is DFA nd NFA, respectively. 4.1 Implementtion on DFAs In order to evlute the complexity of the opertions we must first mke explicit our ssumptions on the complexity of sic opertions on DFA A = (Q, Σ, δ, q 0, F). We ssume tht dictionry opertions (lookup, dd, remove) on Q nd δ cn e performed in constnt time using hshing. We ssume further tht, given stte q, we cn decide in constnt time if q = q 0, nd if q F, nd tht given stte q nd letter Σ, we cn find in constnt time the unique stte δ(q, ) Memership. To check memership for word w we just execute the run of the DFA on w. It is convenient for future use to hve n lgorithm Memer[A](w, q) tht tkes s prmeter DFA A, stte q of A, nd word w, nd checks if w is ccepted with q s initil stte. Memer(w, L) cn then e implemented y Mem[A](w, q 0 ), where A is the utomton representing L. Writing hed(w) = nd til(w) = w for Σ nd w Σ, the lgorithm looks s follows: MemDFA[A](w, q) Input: DFA A = (Q, Σ, δ, Q 0, F), stte q Q, word w Σ, Output: true if w L(q), flse otherwise 1 if w = ɛ then return q F 2 else return Memer[A]( δ(q, hed(w)), til(w) ) The complexity of the lgorithm is O( w ) Complement. Implementing the complement opertions on DFAs is esy. Recll tht DFA hs exctly one run for ech word, nd the run is ccepting iff it reches finl stte. Therefore, if we swp finl nd non-finl sttes, the run on word ecomes ccepting iff it ws non-ccepting, nd so the new DFA ccepts the word iff the new one did not ccept it. So we get the following liner-time lgorithm: CompDFA(A)?? Input: DFA A = (Q, Σ, δ, Q 0, F), Output: DFA B = (Q, Σ, δ, q 0, F ) with L(B) = L(A) 1 Q Q; δ δ; q 0 q 0; F = 2 for ll q Q do 3 if q F then dd q to F

73 4.1. IMPLEMENTATION ON DFAS 73 Oserve tht complementtion of DFAs preserves minimlity. By construction, ech stte of Comp(A) recognizes the complement of the lnguge recognized y the sme stte in A. Therefore, if the sttes of A recognize pirwise different lnguges, so do the sttes of Comp(A). Apply now Corollry 3.8, stting tht DFA is miniml iff their sttes recognize different lnguges Binry Boolen Opertions Insted of specific implementtions for union nd intersection, we give generic implementtion for ll inry oolen opertions. Given two DFAs A 1 nd A 2 nd inry oolen opertion like union, intersection, or difference, the implementtion returns DFA recognizing the result of pplying the opertion to L(A 1 ) nd L(A 2 ). The DFAs for different oolen opertions lwys hve the sme sttes nd trnsitions, they differ only in the set of finl sttes. We cll this DFA with yet unspecified set of finl sttes the piring of A 1 nd A 2, denoted y [A 1, A 2 ]. Formlly: Definition 4.1 Let A 1 = (Q 1, Σ, δ 1, q 01, F 1 ) nd A 2 = (Q 2, Σ, δ 2, q 02, F 2 ) e DFAs. The piring [A 1, A 2 ] of A 1 nd A 2 is the tuple (Q, Σ, δ, q 0 ) where: Q = { [q 1, q 2 ] q 1 Q 1, q 2 Q 2 }; δ = { ([q 1, q 2 ],, [q 1, q 2 ]) (q 1,, q 1 ) δ 1, (q 2,, q 2 ) δ 2}; q 0 = [q 01, q 02 ]. The run of [A 1, A 2 ] on word of Σ is defined s for DFAs. It follows immeditely from this definition tht the run of [A 1, A 2 ] over word w = n is lso piring of the runs of A 1 nd A 2 over w. Formlly, re the runs of A 1 nd A 2 on w if nd only if [ q01 q n q 01 q 11 q q (n 1)1 1 2 n q 02 q 12 q q (n 1)2 ] 1 [ q11 q 12 ] 2 [ q21 q 22 ]... [ q(n 1)1 q (n 1)2 is the run of [A 1, A 2 ] on w. DFAs for different oolen opertions re otined y dding n dequte set of finl sttes to [A 1, A 2 ]. Let L 1, L 2 e the lnguges For intersection, [A 1, A 2 ] must ccept w if nd only if A 1 ccepts w nd A 2 ccepts w. This is chieved y declring stte [q 1, q 2 ] finl if nd only if q 1 F 1 nd q 2 F 2. For union, we just replce nd y or. For difference, [A 1, A 2 ] must ccept w if nd only if A 1 ccepts w nd A 2 does not ccepts w, nd so we declre [q 1, q 2 ] finl if nd only if q 1 F 1 nd not q 2 F 2. ] n q n1 q n2 [ qn1 q n2 ]

74 74 CHAPTER 4. OPERATIONS ON SETS: IMPLEMENTATIONS Exmple 4.2 Figure 4.2 shows t the top two DFAs over the lphet Σ = {}. They recognize the words whose length is multiple of 2 nd multiple of three, respectively. We denote these lnguges y Mult 2 nd Mult 3. The Figure then shows the piring of the two DFAs (for clrity the sttes crry lels x, y insted of [x, y]), nd three DFAs recognizing Mult 2 Mult 3, Mult 2 Mult 3, nd Mult 2 \ Mult 3, respectively , 3 2, 4 1, 5 2, 3 1, 4 2, 5 1, 3 2, 4 1, 5 2, 3 1, 4 2, 5 1, 3 2, 4 1, 5 2, 3 1, 4 2, 5 1, 3 2, 4 1, 5 2, 3 1, 4 2, 5 Figure 4.1: Two DFAs, their piring, nd DFAs for the intersection, union, nd difference of their lnguges. Exmple 4.3 The tour of conversions of Chpter 2 strted with DFA for the lnguge of ll words over {, } contining n even numer of s nd n even numer of s. This lnguge is the intersection of the lnguge of ll words contining n even numer of s, nd the lnguge of ll words contining n even numer of s. Figure 4.2 shows DFAs for these two lnguges, nd the DFA for their intersection. We cn now formulte generic lgorithm tht, given two DFAs recognizing lnguges L 1, L 2 nd inry oolen opertion, returns DFA recognizing the result of pplying the oolen opertion to L 1, L 2. First we formlly define wht this mens. Given n lphet Σ nd inry

75 4.1. IMPLEMENTATION ON DFAS , 3 2, 3 1, 4 2, 4 Figure 4.2: Two DFAs nd DFA for their intersection. oolen opertor : {true, flse} {true, flse} {true, flse}, we lift to function : 2 Σ 2 Σ 2 Σ on lnguges s follows L 1 L 2 = {w Σ (w L 1 ) (w L 2 )} Tht is, in order to decide if w elongs to L 1 L 2, we first evlute (w L 1 ) nd (w L 2 ) to true of flse, nd then pply to the results. For instnce we hve L 1 L 2 = L 1 L 2. The generic lgorithm, prmeterized y, looks s follows: BinOp[ ](A 1, A 2 )?? Input: DFAs A 1 = (Q 1, Σ, δ 1, Q 01, F 1 ), A 2 = (Q 2, Σ, δ 2, Q 02, F 2 ) Output: DFA A = (Q, Σ, δ, Q 0, F) with L(A) = L(A 1 ) L(A 2 ) 1 Q, δ, F 2 q 0 [q 01, q 02 ] 3 W {q 0 } 4 while W do 5 pick [q 1, q 2 ] from W 6 dd [q 1, q 2 ] to Q 7 if (q 1 F 1 ) (q 2 F 2 ) then dd [q 1, q 2 ] to F 8 for ll Σ do 9 q 1 δ 1(q 1, ); q 2 δ 2(q 2, ) 10 if [q 1, q 2 ] Q then dd [q 1, q 2 ] to W 11 dd ([q 1, q 2 ],, [q 1, q 2 ]) to δ Populr choices of oolen lnguge opertions re summrized in the left column elow, while the right column shows the corresponding oolen opertion needed to instntite BinOp[ ].

76 76 CHAPTER 4. OPERATIONS ON SETS: IMPLEMENTATIONS Lnguge opertion 1 2 Union 1 2 Intersection 1 2 Set difference (L 1 \ L 2 ) 1 2 Symmetric difference (L 1 \ L 2 L 2 \ L 1 ) 1 2 The output of BinOp is DFA with O( Q 1 Q 2 ), sttes, regrdless of the oolen opertion eing implemented. To show tht the ound is rechle, let Σ = {}, nd for every n 1 let Mult n denote the lnguge of words whose length is multiple of n. As in Figure 4.2, the miniml DFA recognizing L n is cycle of n sttes, with the initil stte eing lso the only finl stte. For ny two reltively prime numers n 1 nd n 2, we hve Mult n 1 Mult n 2 = Mult (n 1 n 2 ). Therefore, ny DFA for Mult (n 1 n 2 ) hs t lest n 1 n 2 sttes. In fct, if we denote the miniml DFA for Mult k y A k, then BinOp[ ](A n, A m ) = A n m. Notice however, tht in generl minimlity is not preserved: the product of two miniml DFAs my not e miniml. In prticulr, given ny regulr lnguge L, the miniml DFA for L L hs one stte, ut the result of the product construction is DFA with the sme numer of sttes s the miniml DFA for L Emptiness. A DFA ccepts the empty lnguge if nd only if it hs no finl sttes (recll our norml form, where ll sttes must e rechle!). Empty(A) Input: DFA A = (Q, Σ, δ, Q 0, F) Output: true if L(A) =, flse otherwise 1 return F = The runtime depends on the implementtion. If we keep oolen indicting whether the DFA hs some finl stte, then the complexity of Empty() is O(1). If checking F = requires liner scn over Q, then the complexity is O( Q ) Universlity. A DFA ccepts Σ iff ll its sttes re finl, gin n lgorithm with complexity O(1) given norml form, nd O( Q ) otherwise. UnivDFA(A) Input: DFA A = (Q, Σ, δ, Q 0, F) Output: true if L(A) = Σ, flse otherwise 1 return F = Q

77 4.2. IMPLEMENTATION ON NFAS Inclusion. Given two regulr lnguges L 1, L 2, the following lemm chrcterizes when L 1 L 2 holds. Lemm 4.4 Let A 1 = (Q 1, Σ, δ 1, Q 01, F 1 ) nd A 2 = (Q 2, Σ, δ 2, Q 02, F 2 ) e DFAs. L(A 1 ) L(A 2 ) if nd only if every stte [q 1, q 2 ] of the piring [A 1, A 2 ] stisfying q 1 F 1 lso stisfies q 2 F 2. Proof: Let L 1 = L(A 1 ) nd L 2 = L(A 1 ). We hve L 1 L 2 iff L 1 \ L 2 iff t lest one stte [q 1, q 2 ] of the DFA for L 1 \ L 2 is finl iff q 1 F 1 nd q 2 F 2. The condition of the lemm cn e checked y slightly modifying BinOp. The resulting lgorithm checks inclusion on the fly: Equlity. InclDFA(A 1, A 2 ) Input: DFAs A 1 = (Q 1, Σ, δ 1, Q 01, F 1 ), A 2 = (Q 2, Σ, δ 2, Q 02, F 2 ) Output: true if L(A 1 ) L(A 2 ), flse otherwise 1 Q ; 2 W {[q 01, q 02 ]} 3 while W do 4 pick [q 1, q 2 ] from W 5 dd [q 1, q 2 ] to Q 6 if (q 1 F 1 ) nd (q 2 F 2 ) then return flse 7 for ll Σ do 8 q 1 δ 1(q 1, ); q 2 δ 2(q 2, ) 9 if [q 1, q 2 ] Q then dd [q 1, q 2 ] to W 10 return true For equlity, just oserve tht L(A 1 ) = L(A 2 ) holds if nd only if the symmetric difference of L(A 1 ) nd L(A 2 ) is empty. The lgorithm is otined y replcing Line 7 of IncDFA(A 1, A 2 ) y if ((q 1 F 1 ) nd q 2 F 2 )) or ((q 1 F 1 ) nd (q 2 F 2 )) then return flse. 4.2 Implementtion on NFAs For NFAs we mke the sme ssumptions on the complexity of sic opertions s for DFAs. For DFAs, however, we hd the ssumption tht, given stte q nd letter Σ, we cn find in constnt time the unique stte δ(q, ). This ssumption no longer mkes sense for NFA, since δ(q, ) is set.

78 78 CHAPTER 4. OPERATIONS ON SETS: IMPLEMENTATIONS Memership. Memership testing is slightly more involved for NFAs thn for DFAs. An NFA my hve mny runs on the sme word, nd exmining ll of them one fter the other in order to see if t lest one is ccepting is d ide: the numer of runs my e exponentil in the length of the word. The lgorithm elow does etter. For ech prefix of the word it computes the set of sttes in which the utomton my e fter hving red the prefix. MemNFA[A](w) Input: NFA A = (Q, Σ, δ, Q 0, F), word w Σ, Output: true if w L(A), flse otherwise 1 W Q 0 ; 2 while w ε do 3 U 4 for ll q W do 5 dd δ(q, hed(w)) to U 6 W U 7 w til(w) 8 return (W F ) Exmple 4.5 Consider the NFA of Figure 4.3, nd the word w =. The successive vlues of W, tht is, the sets of sttes A cn rech fter reding the prefixes of w, re shown on the right. Since the finl set contins finl sttes, the lgorithm returns true. 1, 2 3, 4 Prefix red W ɛ {1} {2} {2, 3} {1, 2, 3} {2, 3, 4} {2, 3, 4} {1, 2, 3, 4} Figure 4.3: An NFA A nd the run of Mem[A](). For the complexity, oserve first tht the while loop is executed w times. The for loop is executed t most Q times. Ech execution tkes t most time O( Q ), ecuse δ(q, hed(w)) contins t most Q sttes. So the overll runtime is O( w Q 2 ).

79 4.2. IMPLEMENTATION ON NFAS Complement. Recll tht n NFA A my hve multiple runs on word w, nd it ccepts w if t lest one is ccepting. In prticulr, n NFA cn ccept w ecuse of n ccepting run ρ 1, ut hve nother non-ccepting run ρ 2 on w. It follows tht the complementtion opertion for DFAs cnnot e extended to NFAs: fter exchnging finl nd non-finl sttes the run ρ 1 ecomes non-ccepting, ut ρ 2 ecomes ccepting. So the new NFA still ccepts w (t lest ρ 2 ccepts), nd so it does not recognize the complement of L(A). For this reson, complementtion for NFAs is crried out y converting to DFA, nd complementing the result. CompNFA(A) Input: NFA A, Output: DFA A with L(A) = L(A) 1 A CompDFA (NFAtoDFA(A)) Since mking the NFA deterministic my cuse n exponentil low-up in the numer of sttes, the numer of sttes of A my e O(2 Q ) Union nd intersection. On NFAs it is no longer possile to uniformly implement inry oolen opertions. The piring opertion cn e defined exctly s in Definition 4.1. The runs of piring [A 1, A 2 ] of NFAs on given word re defined s for NFAs. The difference with respect to the DFA cse is tht the piring my hve multiple runs or no run t ll on word. But we still hve tht 1 2 n q 01 q 11 q q (n 1)1 1 2 n q 02 q 12 q q (n 1)2 q n1 q n2 re runs of A 1 nd A 2 on w if nd only if [ q01 q 02 ] 1 [ q11 q 12 ] 2 [ q21 q 22 ]... [ q(n 1)1 q (n 1)2 ] n [ qn1 q n2 ] is run of [A 1, A 2 ] on w. Let us now discuss seprtely the cses of intersection, union, nd set difference. Intersection. Let [q 1, q 2 ] e finl stte of [A 1, A 2 ] if q 1 is finl stte of A 1 nd q 2 is finl stte of q 2. Then it is still the cse tht [A 1, A 2 ] hs n ccepting run on w if nd only if A 1 hs n ccepting run on w nd A 2 hs n ccepting run on w. So, with this choice of finl sttes, [A 1, A 2 ] recognizes L(A 1 ) L(A 2 ). So we get the following lgorithm:

80 80 CHAPTER 4. OPERATIONS ON SETS: IMPLEMENTATIONS IntersNFA(A 1, A 2 ) Input: NFA A 1 = (Q 1, Σ, δ 1, Q 01, F 1 ), A 2 = (Q 2, Σ, δ 2, Q 02, F 2 ) Output: NFA A 1 A 2 = (Q, Σ, δ, Q 0, F) with L(A 1 A 2 ) = L(A 1 ) L(A 2 ) 1 Q, δ, F ; Q 0 Q 01 Q 02 2 W Q 0 3 while W do 4 pick [q 1, q 2 ] from W 5 dd [q 1, q 2 ] to Q 6 if (q 1 F 1 ) nd (q 2 F 2 ) then dd [q 1, q 2 ] to F 7 for ll Σ do 8 for ll q 1 δ 1(q 1, ), q 2 δ 2(q 2, ) do 9 if [q 1, q 2 ] Q then dd [q 1, q 2 ] to W 10 dd ([q 1, q 2 ],, [q 1, q 2 ]) to δ Notice tht we overlod the symol, nd denote the output y A 1 A 2. The utomton A 1 A 2 is often clled the product of A 1 nd A 2. It is esy to see tht, s opertion on NFAs, is ssocitive nd commuttive in the following sense: L((A 1 A 2 ) A 3 ) = L(A 1 ) L(A 2 ) L(A 3 ) = L(A 1 (A 2 A 3 )) L(A 1 A 2 ) = L(A 1 ) L(A 2 ) = L(A 2 A 1 ) For the complexity, oserve tht in the worst cse the lgorithm must exmine ll pirs [t 1, t 2 ] of trnsitions of δ 1 δ 2, ut every pir is exmined t most once. So the runtime is O( δ 1 δ 2 ). Exmple 4.6 Consider the two NFAs of Figure 4.4 over the lphet {, }. The first one recognizes the words contining t lest two locks with two consecutive s ech, the second one those contining t lest one lock. The result of pplying IntersNFA() is the NFA of Figure 3.6 in pge 63. Oserve tht the NFA hs 15 sttes, i.e., ll pirs of sttes re rechle. Oserve tht in this exmple the intersection of the lnguges recognized y the two NFAs is equl to the lnguge of the first NFA. So there is n NFA with 5 sttes tht recognizes the intersection, which mens tht the output of IntersNFA() is fr from optiml in this cse. Even fter pplying the reduction lgorithm for NFAs we only otin the 10-stte utomton of Figure 3.7. Union. The rgumenttion for intersection still holds if we replce nd y or, nd so n lgorithm otined from IntersNFA() y sustituting or for nd correctly computes NFA for L(A 1 ) L(A 2 ). However, this is unnecessry. To otin such NFA, it suffices to put A 1 nd A 2 side y side : tke the union of its sttes, trnsitions, initil, nd finl sttes (where we ssume tht these sets re disjoint):

81 4.2. IMPLEMENTATION ON NFAS 81,,,,, Figure 4.4: Two NFAs UnionNFA(A 1, A 2 ) Input: NFA A 1 = (Q 1, Σ, δ 1, Q 01, F 1 ), A 2 = (Q 2, Σ, δ 2, Q 02, F 2 ) Output: NFA A 1 A 2 = (Q, Σ, δ, Q 0, F) with L(A 1 A 2 ) = L(A 1 ) L(A 2 ) 1 Q Q 1 Q 2 2 δ δ 1 δ 2 3 Q 0 Q 01 Q 02 4 F F 1 F 2 Set difference. The generliztion of the procedure for DFAs fils. Let [q 1, q 2 ] e finl stte of [A 1, A 2 ] if q 1 is finl stte of A 1 nd q 2 is not finl stte of q 2. Then [A 1, A 2 ] hs n ccepting run on w if nd only if A 1 hs n ccepting run on w nd A 2 hs non-ccepting run on w. But A 2 hs non-ccepting run on w is no longer equivlent to A 2 hs no ccepting run on w : this only holds in the DFA cse. An lgorithm producing n NFA A 1 \ A 2 recognizing L(A 1 ) \ L(A 2 ) cn e otined from the lgorithms for complement nd intersection through the equlity L(A 1 )\ L(A 2 ) = L(A 1 ) L(A 2 ) Emptiness nd Universlity. Emptiness for NFAs is decided using the sme lgorithm s for DFAs: just check if the NFA hs t lest one finl stte. Universlity requires new lgorithm. Since n NFA my hve multiple runs on word, n NFA my e universl even if some sttes re non-finl: for every word hving run tht leds to non-finl stte there my e nother run leding to finl stte. An exmple is the NFA of Figure 4.3, which, s we shll show in this section, is universl. A lnguge L is universl if nd only if L is empty, nd so universlity of n NFA A cn e checked y pplying the emptiness test to A. Since complementtion, however, involves worstcse exponentil lowup in the size of A, the lgorithm requires exponentil time nd spce.

82 82 CHAPTER 4. OPERATIONS ON SETS: IMPLEMENTATIONS We show tht the universlity prolem is PSPACE-complete. Tht is, the superpolynomil lowup cnnot e voided unless P = PS PACE,which is unlikely. Theorem 4.7 The universlity prolem for NFAs is PSPACE-complete Proof: We only sketch the proof. To prove tht the prolem is in PSPACE, we show tht it elongs to NPSPACE nd pply Svitch s theorem. The polynomil-spce nondeterministic lgorithm for universlity looks s follows. Given n NFA A = (Q, Σ, δ, Q 0, F), the lgorithm guesses run of B = NFAtoDFA(A) leding from {q 0 } to non-finl stte, i.e., to set of sttes of A contining no finl stte (if such run exists). The lgorithm only does not store the whole run, only the current stte, nd so it only needs liner spce in the size of A. We prove PSPACE-hrdness y reduction from the cceptnce prolem for linerly ounded utomt. A linerly ounded utomton is deterministic Turing mchine tht lwys hlts nd only uses the prt of the tpe contining the input. A configurtion of the Turing mchine on n input of length k is coded s word of length k. The run of the mchine on n input cn e encoded s word c 0 #c 1... #c n, where the c i s re the encodings of the configurtions. Let Σ e the lphet used to encode the run of the mchine. Given n input x, M ccepts if there exists word w of Σ stisfying the following properties: () w hs the form c 0 #c 1... #c n, where the c i s re configurtions; () c 0 is the initil configurtion; (c) c n is n ccepting configurtion; nd (d) for every 0 i n 1: c i+1 is the successor configurtion of c i ccording to the trnsition reltion of M. The reduction shows how to construct in polynomil time, given linerly ounded utomton M nd n input x, n NFA A(M, x) ccepting ll the words of Σ tht do not stisfy t lest one of the conditions ()-(d) ove. We then hve If M ccepts x, then there is word w(m, x) encoding the ccepting run of M on x, nd so L(A(M, x)) = Σ \ {w(m, x)}. If M rejects x, then no word encodes n ccepting run of M on x, nd so L(A(M, x)) = Σ. So M ccepts x if nd only if L(A(M, x)) = Σ, nd we re done. A Susumption Test. We show tht it is not necessry to completely construct A. First, the universlity check for DFA only exmines the sttes of the DFA, not the trnsitions. So insted of NFAtoDFA(A) we cn pply modified version tht only stores the sttes of A, ut not its trnsitions. Second, it is not necessry to store ll sttes.

83 4.2. IMPLEMENTATION ON NFAS 83 Definition 4.8 Let A e NFA, nd let B = NFAtoDFA(A). A stte Q of B is miniml if no other stte Q stisfies Q Q. Proposition 4.9 Let A e NFA, nd let B = NFAtoDFA(A). A is universl iff every miniml stte of B is finl. Proof: Since A nd B recognize the sme lnguge, A is universl iff B is universl. So A is universl iff every stte of B is finl. But stte of B is finl iff it contins some finl stte of A, nd so every stte of B is finl iff every miniml stte of B is finl. Exmple 4.10 Figure 4.5 shows NFA on the left, nd the equivlent DFA otined through the ppliction of NFAtoDFA() on the right. Since ll sttes of the DFA re finl, the NFA is universl. Only the sttes {1}, {2}, nd {3, 4} (shded in the picture), re miniml. 1, 2 3, , 4 2, 3 2, 3, 1 2, 3, 4 1, 3, 4 2, 3, 4, 1 Figure 4.5: An NFA, nd the result of converting it into DFA, with the miniml sttes shded. Proposition 4.9 shows tht it suffices to construct nd store the miniml sttes of B. Algorithm UnivNFA(A) elow constructs the sttes of B s in NFAtoDFA(A), ut introduces t line 8 susumption test: it checks if some stte Q δ(q, ) hs lredy een constructed. In this cse either δ(q, ) hs lredy een constructed (cse Q = δ(q, )) or is non-miniml (cse Q δ(q, )). In oth cses, the stte is not dded to the workset.

84 84 CHAPTER 4. OPERATIONS ON SETS: IMPLEMENTATIONS UnivNFA(A) Input: NFA A = (Q, Σ, δ, Q 0, F) Output: true if L(A) = Σ, flse otherwise 1 Q ; 2 W { {q 0 } } 3 while W do 4 pick Q from W 5 if Q F = then return flse 6 dd Q to Q 7 for ll Σ do 8 if W Q contins no Q δ(q, ) then dd δ(q, ) to W 9 return true The next proposition shows tht UnivNFA(A) constructs ll miniml sttes of B. If UnivNFA(A) would first generte ll sttes of A nd then would remove ll non-miniml sttes, the proof would e trivil. But the lgorithm removes non-miniml sttes whenever they pper, nd we must show tht this does not prevent the future genertion of other miniml sttes. Proposition 4.11 Let A = (Q, Σ, δ, Q 0, F) e NFA, nd let B = NFAtoDFA(A). After termintion of UnivNFA(A), the set Q contins ll miniml sttes of B. Proof: Let Q t e the vlue of Q fter termintion of UnivNFA(A). We show tht no pth of B leds from stte of Q t to miniml stte of B not in Q t. Since {q 0 } Q t nd ll sttes of B re rechle from {q 0 }, it follows tht every miniml stte of B elongs to Q t. 1 n Assume there is pth π = Q 1 Q 2... Q n 1 Q n of B such tht Q 1 Q t, Q n Q t, nd Q n is miniml. Assume further tht π is s short s possile. This implies Q 2 Q t (otherwise n Q 2... Q n 1 Q n is shorter pth stisfying the sme properties), nd so Q 2 is never dded to the workset. On the other hnd, since Q 1 Q t, the stte Q 1 is eventully dded to nd picked from the workset. When Q 1 is processed t line 7 the lgorithm considers Q 2 = δ(q 1, 1 ), ut does not dd it to the workset in line 8. So t tht moment either the workset or Q contin stte Q 2 Q 2. This stte is eventully dded to Q (if it is not lredy there), nd so Q 2 Q t. So B hs pth π = Q 2 2 Q 3... Q n n 1 Q n for some sttes Q 3,..., Q n. Since Q 2 Q 2 we hve Q 2 Q 2, Q 3 Q 3,..., Q n Q n (notice tht we my hve Q 3 = Q 3). By the minimlity of Q n, we get Q n = Q n, nd so π leds from Q 2, which elongs to Q t, to Q n, which is miniml nd not in to Q t. This contrdicts the ssumption tht π is s short s possile. Notice tht the complexity of the susumption test my e considerle, ecuse the new set δ(q, ) must e compred with every set in W Q. Good use of dt structures (hsh tles or rdix trees) is dvisle.

85 4.2. IMPLEMENTATION ON NFAS Inclusion nd Equlity. Recll Lemm 4.4: given two DFAs A 1, A 2, the inclusion L(A 1 ) L(A 2 ) holds if nd only if every stte [q 1, q 2 ] of [A 1, A 2 ] stisfying q 1 F 1 lso stisfies q 2 F 2. This lemm no longer holds for NFAs. To see why, let A e ny NFA hving two runs for some word w, one of them leding to finl stte q 1, the other to non-finl stte q 2. We hve L(A) L(A), ut the piring [A, A] hs run on w leding to [q 1, q 2 ]. To otin n lgorithm for checking inclusion, we oserve tht L 1 L 2 holds if nd only if L 1 L 2 =. This condition cn e checked using the constructions for intersection nd for the emptiness check. However, s in the cse of universlity, we cn pply susumption check. Definition 4.12 Let A 1, A 2 e NFAs, nd let B 2 = NFAtoDFA(A 2 ). A stte [q 1, Q 2 ] of [A 1, B 2 ] is miniml if no other stte [q 1, Q 2 ] stisfies q 1 = q 1 nd Q 2 Q. Proposition 4.13 Let A 1 = (Q 1, Σ, δ 1, Q 01, F 1 ), A 2 = (Q 2, Σ, δ 2, Q 02, F 2 ) e NFAs, nd let B 2 = NFAtoDFA(A 2 ). L(A 1 ) L(A 2 ) iff every miniml stte [q 1, Q 2 ] of [A 1, B 2 ] stisfying q 1 F 1 lso stisfies Q 2 F 2. Proof: Since A 2 nd B 2 recognize the sme lnguge, L(A 1 ) L(A 2 ) iff L(A 1 ) L(A 2 ) = iff L(A 1 ) L(B 2 ) = iff [A 1, B 2 ] hs stte [q 1, Q 2 ] such tht q 1 F 1 nd Q 2 F 2 =. But [A 1, B 2 ] hs some stte stisfying this condition iff it hs some miniml stte stisfying it. So we get the following lgorithm to check inclusion: InclNFA(A 1, A 2 ) Input: NFAs A 1 = (Q 1, Σ, δ 1, Q 01, F 1 ), Output: true if L(A 1 ) L(A 2 ), flse otherwise 1 Q ; 2 W { [q 01, {q 02 }] } 3 while W do 4 pick [q 1, Q 2 ] from W 5 if (q 1 F 1 ) nd (Q 2 F 2 = ) then return flse 6 dd [q 1, Q 2 ] to Q 7 for ll Σ, q 1 δ 1(q 1, ) do 8 Q 2 δ 2(Q 2, ) 9 if W Q contins no [q 1, Q 2 ] s.t. q 1 = q 1 nd Q 2 Q 2 then 10 dd [q 1, Q 2 ] to W 11 return true Notice tht in unfvorle cses the overhed of the susumption test my not e compensted y reduction in the numer of sttes. Without the test, the numer of pirs tht cn e dded to the workset is t most Q 1 2 Q2. For ech of them we hve to execute the for loop O( Q 1 ) times, ech of them tking O( Q 2 2 ) time. So the lgorithm runs in Q O( Q2 ) time nd spce.

86 86 CHAPTER 4. OPERATIONS ON SETS: IMPLEMENTATIONS As ws the cse for universlity, the inclusion prolem is PSPACE-complete, nd so the exponentil cnnot e voided unless P = PS PACE. Proposition 4.14 The inclusion prolem for NFAs is PSPACE-complete Proof: We first prove memership in PSPACE. Since PSPACE=co-PSPACE=NPSPACE, it suffices to give polynomil spce nondeterministic lgorithm tht decides non-inclusion. Given NFAs A 1 nd A 2, the lgorithm guesses word w L(A 1 ) \ L(A 2 ) letter y letter, mintining the sets Q 1, Q 2 of sttes tht A 1 nd A 2 cn reched y the word guessed so fr. When the guessing ends, the lgorithm checks tht Q 1 contins some finl stte of A 1, ut Q 2 does not. Hrdness follows from the fct tht A is universl iff Σ L(A), nd so the universlity prolem, which is PSPACE-complete, is suprolem of the inclusion prolem. There is however n importnt cse with polynomil complexity, nmely when A 2 is DFA. The numer the numer of pirs tht cn e dded to the workset is then t most Q 1 Q 2. The for loop is still executed O( Q 1 ] times, ut ech of them tkes O(1) time. So the lgorithm runs in O( Q 1 2 Q 2 ) time nd spce. Equlity. Equlity of two lnguges is decided y checking tht ech of them is included in the other. The equlity prolem is gin PSPACE-complete. The only point worth oserving is tht, unlike the inclusion cse, we do not get polynomil lgorithm when A 2 is DFA. Exercises Exercise 39 Consider the following lnguges over the lphet Σ = {, }: L 1 is the set of ll words where etween ny two occurrences of s there is t lest one. L 2 is the set of ll words where every non-empty mximl sequence of consecutive s hs odd length. L 3 is the set of ll words where occurs only t even positions. L 4 is the set of ll words where occurs only t odd positions. L 5 is the set of ll words of odd length. L 6 is the set of ll words with n even numer of s. Construct n NFA for the lnguge (L 1 \ L 2 ) (L 3 L 4 ) L 5 L 6 where L L denotes the symmetric difference of L nd L, while sticking to the following rules:

87 4.2. IMPLEMENTATION ON NFAS 87 Strt from NFAs for L 1,..., L 6. Any further utomton must e constructed from lredy existing utomt vi n lgorithm introduced in the chpter, e.g. Comp, BinOp, UnionNFA, NFAtoDFA, etc. Try to find n order on the construction steps such tht the intermedite utomt nd the finl result hve s few sttes s possile. Exercise 40 Prove or disprove: the miniml DFAs recognizing lnguge L nd its complement L hve the sme numer of sttes. Exercise 41 Give regulr expression for the words over {0, 1} tht do not contin 010 s suword. Exercise 42 Let A = (Q, Σ, δ, Q 0, F) e n NFA. Show tht with the universl ccepting condition of Exercise 11 the utomton A = (Q, Σ, δ, q 0, Q \ F) recognizes the complement of the lnguge recognized y A. Exercise 43 Recll the lternting utomt introduced in Exercise??. Let A = (Q 1, Q 2, Σ, δ, q 0, F) e n lternting utomton, where Q 1 nd Q 2 re the sets of existentil nd universl sttes, respectively, nd δ: (Q 1 Q 2 ) Σ P(Q 1 Q 2 ). Show tht the lternting utomton A = (Q 2, Q 1, Σ, δ, q 0, Q \ F) recognizes the complement of the lnguge recognized y A. I.e., show tht lternting utomt cn e complemented y exchnging existentil nd universl sttes, nd finl nd non-finl sttes. Give liner time lgorithms tht tke two lternting utomt recognizing lnguges L 1, L 2 nd deliver third lternting utomton recognizing L 1 L 2 nd L 1 L 2. Hint: The lgorithms re very similr to UnionDFA. Show tht the emptiness prolem for lternting utomt is PSPACE-complete. Hint: Using Exercise 46, prove tht the emptiness prolem for lternting utomt is PSPACE-complete. Exercise 44 Find fmily {A n } n=1 of NFAs with O(n) sttes such tht every NFA recognizing the complement of L(A n ) hs t lest 2 n sttes. Hint: See Exercise 11. Exercise 45 Consider gin the regulr expressions (1 + 10) nd 1 (101 ) of Exercise 2. Construct NFAs for the expressions nd use InclNFA to check if their lnguges re equl. Construct DFAs for the expressions nd use InclDFA to check if their lnguges re equl. Construct miniml DFAs for the expressions nd check whether they re isomorphic.

88 88 CHAPTER 4. OPERATIONS ON SETS: IMPLEMENTATIONS Exercise 46 Prove tht the following prolem is PSPACE-complete: Given: DFAs A 1,..., A n over the sme lphet Σ. Decide: Is n i=1 L(A i ) =? Hint: Reduction from the cceptnce prolem for deterministic, linerly ounded utomt. Prove tht if Σ is 1-letter lphet then the prolem is only NP-complete. Hint: reduction from 3-SAT. Prove tht if the DFAs re cyclic (ut the lphet ritrry) then the prolem is gin NP-complete. Exercise 47 Consider the vrint of IntersNFA in which line 7 is replced y if (q 1 F 1 ) nd (q 2 F 2 ) then dd [q 1, q 2 ] to F if (q 1 F 1 ) or (q 2 F 2 ) then dd [q 1, q 2 ] to F Let A 1 A 2 e the result of pplying this vrint to two NFAs A 1, A 2. We cll A 1 A 2 the or-product of A 1 nd A 2. An NFA A = (Q, Σ, δ, Q 0, F) is complete if δ(q, ) for every q Q nd every Σ. Prove: If A 1 nd A 2 re complete NFAs, then L(A 1 A 2 ) = L(A 1 ) L(L 2 ). Give NFAs A 1, A 2 such tht L(A 1 A 2 ) = L(A 1 ) L(L 2 ) ut neither A 1 nor A 2 re complete. Exercise 48 Given word w = n over n lphet Σ, we define the even prt of w s the word n/2. Given n NFA for lnguge L, construct n NFA recognizing the even prts of the words of L. Exercise 49 Given regulr lnguges L 1, L 2 over n lphet Σ, the left quotient of L 1 y L 2 is the lnguge L 2 L 1 := {v Σ u L 2 : uv L 1 } (Note tht L 2 L 1 is different from the set difference L 2 \ L 1.) 1. Given NFA A 1, A 2, construct n NFA A such tht L(A) = L(A 1 ) L(A 2 ). 2. Do the sme for the right quotient of L 1 y L 2, defined s L 1 L 2 := {u Σ v L 2 : uv L 1 }. 3. Determine the inclusion reltions etween the following lnguges: L 1, (L 1 L 2 )L 2, nd (L 1 L 2 ) L 2

89 4.2. IMPLEMENTATION ON NFAS 89 Exercise 50 Let L i = {w {} the length of w is divisile y i }. 1. Construct n NFA for L := L 4 L 6 with t most 11 sttes. 2. Construct the miniml DFA for L. Exercise 51 Modify lgorithm Empty so tht when the DFA or NFA is nonempty it returns witness, i.e., word ccepted y the utomton. Sme for shortest witness. Exercise 52 Check y mens of UnivNFA whether the following NFA is universl. q 2,,, q 4 q 5 strt q 3 q 1 Exercise 53 Let Σ e n lphet, nd define the shuffle opertor : Σ Σ 2 Σ s follows, where, Σ nd w, v Σ : For exmple we hve: w ε = {w} ε w = {w} w v = {} (w v) {} (w v) {w w u v} d = {d, d}, d = {d, d, d}, cd = {cd, cd, cd, cd, cd, cd}. Given DFAs recognizing lnguges L 1, L 2 Σ construct n NFA recognizing their shuffle L 1 L 2 := u v. u L 1,v L 2 Exercise 54 Let Σ 1, Σ 2 e two lphets. A homomorphism is mp h : Σ 1 Σ 2 such tht h(ε) = ε nd h(w 1 w 2 ) = h(w 1 )h(w 2 ) f oreveryw 1, w 2 Σ 1. Oserve tht if Σ 1 = { 1,..., n } then h is completely determined y the vlues h( 1 ),..., h( n ).

90 90 CHAPTER 4. OPERATIONS ON SETS: IMPLEMENTATIONS 1. Let h : Σ 1 Σ 2 e homomorphism nd let A e NFA over Σ 1. Descrie how to constuct NFA for the lnguge h(l(a)) := {h(w) w L(A)}. 2. Let h : Σ 1 Σ 2 e homomorphism nd let A e NFA over Σ 2. Descrie how to construct NFA for the lnguge h 1 (L(A)) := {w Σ 1 h(w) L(A)}. 3. Recll tht the lnguge {0 n 1 n n IN} is not regulr. Use the preceding results to show tht {(01 k 2) n 3 n k, n IN} is lso not regulr. Exercise 55 Given lphets Σ nd, sustitution is mp f : Σ 2 ssigning to ech letter Σ lnguge L.A susitution f cn e cnoniclly extended to mp 2 Σ 2 y defining f (ε) = ε, f (w) = f (w) f (), nd f (L) = w L f (w). Note tht homomorphism cn e seen s the specil cse of sustitution in which ll L s re singletons. Let Σ = {Nme, Tel, :, #}, let = {A,..., Z, 0, 1,..., 9, :, #}, nd let f e the sustitution f given y f (Nme) = (A + + Z) f (:) = {:} f (Tel) = 0049( )( ) ( )( ) 8 f (#) = {#} 1. Drw DFA recognizing L = Nme:Tel(#Telephone). 2. Sketch n NFA-reg recognizing f (L). 3. Give n lgorithm tht tkes s input n NFA A, sustitution f, nd for every Σ n NFA recognizing f (), nd returns n NFA recognizing f (L(A)). Exercise 56 Given two NFAs A 1 nd A 2, let B = NFAtoDFA(IntersNFA(A 1, A 2 )) nd C = IntersDFA(NFAtoDFA(A 1 ) Show tht B nd C re isomorphic, nd so in prticulr hve thesme numer of sttes. (A superficil nlysis gives tht for NFAs with n 1 nd n 2 sttes B nd C hve O(2 n 1 n 2 ) nd O(2 n 1+n 2 ) sttes, respectively, wrongly suggesting tht C might e more compct thn B.) Exercise 57 A DFA is synchronizing if there is word w nd stte q such tht fter reding w from ny stte the DFA is lwys in stte q. 1. Show tht the following DFA is synchronizing.

91 4.2. IMPLEMENTATION ON NFAS 91 strt 2. Give n lgorithm to decide if given DFA is synchronizing. 3. Give polynomil time lgorithm to decide if given DFA is synchronizing.

92 92 CHAPTER 4. OPERATIONS ON SETS: IMPLEMENTATIONS

93 Chpter 5 Applictions I: Pttern mtching As first exmple of prcticl ppliction of utomt, we consider the pttern mtching prolem. Given w, w Σ, we sy tht w is fctor of w if there re words w 1, w 2 Σ such tht w = w 1 w w 2. If w 1 nd w 1 w hve lengths k nd k, respectively, we sy tht w is the [k, k ]-fctor of w. The pttern mtching prolem is defined s follows: Given word t Σ + (clled the text), nd regulr expression p over Σ (clled the pttern), determine the smllest k 0 such tht some [k, k]-fctor of t elongs to L(p). We cll k the first occurrence of p in t. Exmple 5.1 Let p = ( ). Since, L(p), the [1, 3]-, [3, 5]-, nd [0, 5]-fctors of elong to L(p). So the first occurrence of p in is 3. Usully one is interested not only in finding the ending position k of the [k, k]-fctor, ut lso in the strting position k. Adpting the lgorithms to lso provide this informtion is left s n exercise. 5.1 The generl cse We present two different solutions to the pttern mtching prolem, using nondeterministic nd deterministic utomt, respectively. Solution 1. Clerly, some word of L(p) occurs in t if nd only if some prefix of t elongs to L(Σ p). So we construct n NFA A for the regulr expression Σ p using the rules of Figure 2.12 on pge 33, nd then removing the ε trnsitions y mens of NFAεtoNFA on pge 31. Let us cll the resulting lgorithm RegtoNFA. Once A is constructed, we simulte A on t s in MemNFA[A](q 0,t) on pge 72. Recll tht the simultion lgorithm reds the text letter y letter, mintining the set of sttes rechle from the initil stte y the prefix red so fr. So the simultion reches set of sttes S contining finl stte if nd only if the prefix red so fr elongs to L(Σ p). Here is the pseudocode for this lgorithm: 93

94 94 CHAPTER 5. APPLICATIONS I: PATTERN MATCHING PtternMtchingNFA(t, p) Input: text t = 1... n Σ +, pttern p Σ Output: the first occurrence of p in t, or if no such occurrence exists. 1 A RegtoNFA(Σ p) 2 S Q 0 3 for ll k = 0 to n 1 do 4 if S F then return k 5 S δ(s, k+1 ) 6 return For given fixed lphet Σ, rough estimte of the complexity of PtternMtchingNFA for word of length n nd pttern of length m cn e otined s follows. RegtoNFA is the conctention of RegtoNFAε nd NFAεtoNFA. For pttern of size m, RegtoNFAε tkes O(m) time, nd outputs NFAε with O(m) sttes nd O(m) trnsitions. So, when pplied to this utomton, NFAεtoNFA tkes O(m 2 ) time, nd outputs NFA with O(m) sttes nd O(m 2 ) trnsitions (see pge 32 for the complexity of NFAεtoNFA). The loop is executed t most n times, nd, for n utomton with O(m) sttes, ech line of the loop s ody tkes t most O(m 2 ) time. So the loop runs in O(nm 2 ) time. The overll runtime is thus O(nm 2 ). Solution 2. We proceed s in the previous cse, ut constructing DFA for Σ p insted of NFA: PtternMtchingDFA(t, p) Input: text t = 1... n Σ +, pttern p Output: the first occurrence of p in t, or if no such occurrence exists. 1 A NFAtoDFA(RegtoNFA(Σ p)) 2 q q 0 3 for ll k = 0 to n 1 do 4 if q F then return k 5 q δ(q, k+1 ) 6 return Notice tht there is trde-off: while the conversion to DFA cn tke (much) longer thn the conversion to NFA, the memership check for DFA is fster. The complexity of PtternMtchingDFA for word of length n nd pttern of length m cn e esily estimted: RegtoNFA(p) runs in O(m 2 ) time, ut it outputs NFA with only O(m) sttes, nd so the cll to NFAtoDFA (see Tle 16) tkes 2 O(m) time nd spce. Since the loop is executed t most n times, nd ech line of the ody tkes constnt time, the overll runtime is O(n) + 2 O(m).

95 5.2. THE WORD CASE The word cse We study the specil ut very common specil cse of the pttern-mtching prolem in which we wish to know if given word ppers in text. In this cse the pttern p is the word itself. For the rest of the section we consider n ritrry ut fixed text t = 1... n nd n ritrry ut fixed word pttern p = 1... m. Insted of tking fixed lphet, we ssume tht the lphet is implicitely defined y the text nd the pttern, nd so tht the lphet hs size O(n + m). It is esy to find fster lgorithm for this specil cse, without ny use of utomt theory: just move window of length m over the text t, one letter t time, nd check fter ech move whether the content of the window is p. The numer of moves is n m + 1, nd check requires O(m) letter comprisons, giving runtime of O(nm). In the rest of the section we present fster lgorithm with time complexity O(m + n). Notice tht in mny pplictions n is very lrge, nd so, even for reltively smll m the difference etween nm nd m + n cn e significnt. Moreover, the constnt hidden in the O-nottion is now independent of the size of the lphet. This is importnt for pplictions where the lphet is lrge (like chinese texts). Figure 5.1() shows the ovious NFA A p recognizing Σ p for the cse p = nno. In generl, A p = (Q, Σ, δ, {q 0 }, F), where Q = {0, 1,..., m}, q 0 = 0, F = {m}, nd δ = {(i, i+1, i + 1) i [0, m } {(0,, 0) Σ}. Clerly, A p cn rech stte k whenever the word red so fr ends with 0... k. We define the hit letter for ech stte of A p. Intuitively, it is the letter tht mkes A p progress towrds reding p. Definition 5.2 We sy tht Σ is hit for stte i of A p if δ(i, ) = {i + 1}; otherwise is miss for stte i. Figure 5.1() shows the DFA B p otined y pplying NFAtoDFA to A p. It hs s mny sttes s A p, nd there is nturl correspondence etween the sttes of A p nd B p : ech stte of A p is the lrgest element of exctly one stte of B p. For instnce, 3 is the lrgest element of {3, 1, 0}, nd 4 is the lrgest element of {4, 0}. Definition 5.3 The hed of stte S {0,..., m} of B p, denoted y h(s ), is the lrgest element of S. The til of S, denoted y t(s ) is the set t(s ) = S \ {h(s )}. The hit for stte S of B p is defined s the hit of the stte h(s ) in A p. If we lel stte with hed k y the string 1... k, s shown in Figure 5.1(c), then we see tht B p keeps trck of how close it is to finding nno. For instnce: if B p is in stte n nd reds n ( hit for this stte), then it mkes progress, nd moves to stte n; if B p is in stte nn nd reds n ( miss for this stte), then it is thrown ck to stte n. Not to ε, ecuse if the next two letters re n nd o, then B p should ccept!

96 96 CHAPTER 5. APPLICATIONS I: PATTERN MATCHING Σ n n o () other n n n () 0 n n 1, 0 2, 0 other other other 3, 1, 0 other o 4, 0 other n n n () ɛ n other n other n n other nn other o nno Figure 5.1: NFA A p nd DFA B p for p = nno B p hs nother property tht will e very importnt lter on: for ech stte S of B p (with the exception of S = {0}) the til of S is gin stte of B p. For instnce, the til of {3, 1, 0} is {1, 0}, which is lso stte of B p. We show tht this property nd the ones ove hold in generl, nd not only in the specil cse p = nno. Formlly, we prove the following invrint of NFAtoDFA (which is shown gin in Tle 5.2 for convenience) when pplied to word pttern p. Proposition 5.4 Let p = 1... m, nd let S k e the k-th set picked from the workset during the execution of NFAtoDFA(A p ). Then (1) h(s k ) = k (nd therefore k m), nd (2) either k = 0 nd t(s k ) =, or k > 0 nd t(s k ) Q.

97 5.2. THE WORD CASE 97 NFAtoDFA(A) Input: NFA A = (Q, Σ, δ, Q 0, F) Output: DFA B = (Q, Σ,, Q 0, F) with L(B) = L(A) 1 Q,, F ; Q 0 {q 0 } 2 W = {Q 0 } 3 while W do 4 pick S from W 5 dd S to Q 6 if S F then dd S to F 7 for ll Σ do 8 S δ(s, ) 9 if S Q then dd S to W 10 dd (S,, S ) to Tle 5.1: NFAtoDFA(A) Proof: We prove tht (1) nd (2) hold for ll 0 k m 1, nd moreover tht right efore the k-th itertion of the while loop the workset only contins S k (we cll this (3)). Clerly, (1), (2), nd (3) hold for k = 0. For k > 0, consider the k-th itertion of the while loop. By induction hypothesis, we hve h(s k ) = k y (1), t(s k ) = S l for some l < k y (2), nd y (3) we know tht efore the k-th itertion the workset only contins S k. So the lgorithm picks S k from the workset nd exmines the sets δ(s k, ) for every ction, where we use the nottion δ(s k, ) = q S k δ(q, ). We consider two cses: is hit for S k, nd is miss for S k. If is miss for S k, then y definition it is lso miss for its hed h(s k ) = k. So we hve δ(k, ) =, nd hence δ(s k, ) = δ(t(s k ), ) = δ(s l, ). So δ(s k, ) ws lredy explored y the lgorithm during the l-th itertion of the loop, nd δ(s k, ) is not dded to the workset t line 9. If is hit for S k, then δ(k, ) = {k + 1}. Since δ(s k, ) = δ(h(s k ), ) δ(t(s k ), ), we get δ(s k, ) = {k + 1} δ(s l, ). Since stte k + 1 hs not een explored efore, the set {k + 1} δ(s l, ) ecomes the (k + 1)-st stte dded to the workset, i.e., S k+1 = {k + 1} δ(s l, ). So we get h(s k+1 ) = k + 1, nd, since t(s k+1 ) = t(s l ), lso t(s k+1 ) Q; in other words, S k+1 stisfies (1) nd (2). Moreover, S k+1 is the only stte dded to the workset during the k-th itertion of the loop, nd so we lso hve (3). Finlly, consider the m-th itertion of the loop. Since no letter is hit for stte m, the lgorithm does not dd ny new stte to the workset, nd so t the end of the itertion the workset is empty nd the lgorithm termintes. By Proposition 5.4, B p hs t most m + 1 sttes for pttern of length m. So NFAtoDFA does not incur in ny exponentil lowup for word ptterns. Even more: ny DFA for Σ p must hve t

98 98 CHAPTER 5. APPLICATIONS I: PATTERN MATCHING lest m+1 sttes, ecuse for ny two distinct prefixes p 1, p 2 of p the residuls (Σ p) p 1 nd (Σ p) p 2 re lso distinct. So we get Corollry 5.5 B p is the miniml DFA recognizing Σ p. B p hs m + 1 sttes nd m Σ trnsitions. Trnsitions of B p leled y letters tht do not pper in p lwys led to stte 0, nd so they do not need to e explicitely stored. The remining O(m) trnsitions for ech stte cn e constructed nd stored using O(m 2 ) spce nd time, leding to O(n + m 2 ) lgorithm. To chieve O(n + m) time we introduce n even more compct dt structure: the lzy DFA for Σ p, tht, s we shll see, cn e constructed in O(m) spce nd time Lzy DFAs A DFA cn e seen s the control unit of mchine tht reds input from tpe divided into cells y mens of reding hed (see Figure 5.2). Initilly, the tpe contins the word to e red, nd the reding hed is positioned on the first cell of the tpe. At ech step, the mchine reds the content of the cell occupied y the reding hed, updtes the current stte ccording to the trnsition function, nd dvnces the hed one cell to the right. The mchine ccepts word if the stte reched fter reding it completely is finl. n n n o n... Figure 5.2: Tpe with reding hed. In lzy DFAs the mchine my dvnce the hed one cell to the right or keep it on the sme cell. Which of the two tkes plce is function of the current stte nd the current letter red y the hed. Formlly, lzy DFA only differs from DFA in the trnsition function, which hs the form δ: Q Σ Q {R, N}, where R stnds for move Right nd N stnds for No move. A trnsition of lzy DFA is fourtuple (q,, q, d), where d {R, N} is the move of the hed. Intuitively, trnsition (q,, q, N) mens tht stte q is lzy nd delegtes processing the letter to the stte q. A lzy DFA C p for Σ p. Recll tht every stte S k of B p hs hit letter nd ll other letters re misses. In prticulr, if is miss, then δ B (S k, ) = δ(t(s k ), ), nd so when it reds miss, B p moves from S k to the sme stte it would move to, if it were in stte t(s ). Using this, we construct lzy DFA C p with the sme sttes s B p nd trnsition function δ C (S k, ) given y: If is hit for S k, then C p ehves s B p : δ C (S k, ) = (S k+1, R).

99 5.2. THE WORD CASE 99 If is miss for S k, then S k delegtes to t(s k ): δ C (S k, ) = (t(s k ), N). However, if k = 0 then t(s k ) is not stte, nd C p moves s B p : δ C (S 0, ) = (S 0, R). Notice tht in the cse of miss C p lwys delegtes to the sme stte, independently of the letter eing red, nd so we cn summrize the trnsitions for ll misses into single trnsition δ C (S k, miss) = (t(s k ), N). Figure 5.3 shows the DFA nd the lzy DFA for p = nno. (We write just k insted of S k in the sttes.) Consider the ehviours of B p nd C p from stte S 3 if they red n. B p moves to S 1 (wht it would do if it were in stte S 1 ); insted, C p delegtes to S 1, which delegtes to S 0, which moves S 1 : tht is, the move of B p is simulted in C p y chin of delegtions, followed y move of the hed to the right (in the worst cse the chin of delegtions reches S 0, who cnnot delegte to nyody). The finl destintion is the sme in oth cses. other n n n () 0 n n 1, 0 2, 0 other other other 3, 1, 0 other o 4, 0 miss, R miss, N 0 n, R 1, R 2 n, R 3 o, R 4 miss, N miss, N miss, N Figure 5.3: DFA nd lzy DFA for p = nno Notice tht C p my require more steps thn B p to red the text. But the numer of steps is t most 2n. For every letter the utomton C p does numer of N-steps, followed y one R-step. Cll this step sequence mcrostep, nd let S ji e the stte reched fter the i-th mcrostep, with j 0 = 0. Since the i-th mcrostep leds from S ji 1 to S ji, nd N-steps never move forwrd long the spine, the numer of steps of the i-th mcrostep is ounded y j i 1 j i + 2. So the totl numer of steps is ounded y n ( j i 1 j i + 2) = j 0 j n + 2n 0 m + 2n = 2n. i=1

100 100 CHAPTER 5. APPLICATIONS I: PATTERN MATCHING Computing C p in O(m) time. Let Miss(i) e the hed of the trget stte of the miss trnsition for S i. For instnce, for p = nno we hve Miss(3) = 1 nd Miss(i) = 0 for i = 0, 1, 2, 4. Clerly, if we cn compute ll of Miss(0),..., Miss(m) together in O(m) time, then we cn construct C p in O(m) time. Consider the uxiliry function miss(s i ) which returns the trget stte of the miss trnsition, insted of its hed (i.e., miss(s i ) = S j iff Miss(i) = j). We otin some equtions for miss, which cn then e esily trnsformed into n lgorithm for Miss. By definition, for every i > 0 in the cse of miss the stte S i delegtes to t(s i ), i.e., miss(s i ) = t(s i ). Since t(s 1 ) = {0} = S 0, this lredy gives miss(s 1 ) = S 0. For i > 1 we hve S i = {i} t(s i ), nd so t(s i ) = t(δ B (S i 1, i )) = t(δ(i 1, i ) δ(t(s i 1, i )) = t({i} δ(t(s i 1 ), i )) = δ B (t(s i 1 ), i ). Hence we get Moreover, we hve δ B (S j, ) = miss(s i ) = δ B (miss(s i 1 ), i ) S j+1 if = j+1 (hit) S 0 if j+1 (miss) nd j = 0 δ B (t(s j ), ) if j+1 (miss) nd j 0 Putting these two lst equtions together, nd reclling tht miss(s 0 ) = S 0, we finlly otin { S miss(s i ) = 0 if i = 0 or i = 1 (5.1) δ B (miss(s i 1 ), i ) if i > 1 S j+1 if = j+1 (hit) δ B (S j, ) = S 0 if j+1 (miss) nd j = 0 (5.2) δ B (miss(s j ), ) if j+1 (miss) nd j 0 Equtions 5.1 nd 5.2 led to the lgorithms shown in Tle 5.2. CompMiss(p) computes Miss(i) for ech index i {0,..., m}. CompMiss(p) clls DeltB( j, ), shown on the right of the Tle, which returns the hed of the stte δ B (S j, ), i.e., DeltB( j, )= k iff δ B (S j, ) = S k. CompMiss(p) Input: pttern p = 1 m. Output: heds of trgets of miss trnsitions. 1 Miss(0) 0; Miss(1) 0 2 for i 2,..., m do 3 Miss(i) DeltB( Miss(i 1), i ) DeltB( j, ) Input: hed j {0,..., m}, letter. Output: hed of the stte δ B (S j, ). 1 while j+1 nd j 0 do j Miss( j) 2 if = j+1 then return j else return 0 Tle 5.2: Algorithm CopmpMiss(p) It remins to show tht ll clls to DeltB during the execution of Miss(p) tke together O(m) time. Let n i e the numer of itertions of the while loop t line 1 during the cll DeltB(Miss(i 1), i ).

101 5.2. THE WORD CASE 101 We prove m i=2 n i m. To this end, oserve tht n i Miss(i 1) (Miss(i) 1), ecuse initilly j is set to Miss(i 1), ech itertion of the loop decreses j y t lest 1 (line 2), nd the return vlue ssigned to Miss(i) is t most the finl vlue of j plus 1. So we get m n i i=2 m ( Miss(i 1) Miss(i) + 1) Miss(1) Miss(m) + m Miss(1) + m = m i=2 Exercises Exercise 58 Design n lgorithm tht solves the following prolem for n lphet Σ: Given text t Σ nd regulr expression p over Σ, find shortest prefix w 1 Σ of t such tht some prefix w 1 w 2 of w stisfies w 2 L(p). Discuss the complexity of your solution. Exercise 59 Construct DFAs nd lzy DFAs for the ptterns mmmmi nd rcdr. Exercise 60 We hve shown tht lzy DFAs for word pttern my need more thn n steps to red text of length n, ut not more thn 2n + m, where m is the length of the pttern. Find text t nd word pttern p such tht the run of B p on t tkes t most n steps nd the run of C p tkes t lest 2n 1 steps. Hint: simple pttern of the form k for some k 0 is sufficient. Exercise 61 Two-wy finite utomt re n extension of lzy utomt in which the reding hed my not only move right or sty put, ut lso move left. The tpe extends infinitely long to oth the left nd to the right of the inputwith ll cells empty. A word is ccepted if the control stte is finl stte t the moment the hed reches n empty cell to the right of the input for the first time. Give two-wy DFA with O(n) sttes for the lnguge (0 + 1) 1(0 + 1) n. Sketch lgorithms for memership nd emptiness of two-wy utomt. (Difficult!) Prove tht the lnguges recognized y two-wy DFA re regulr.

102 102 CHAPTER 5. APPLICATIONS I: PATTERN MATCHING

103 Chpter 6 Opertions on Reltions: Implementtions We show how to implement opertions on reltions over (possily infinite) universe U. Even though we will encode the elements of U s words, when implementing reltions it is convenient to think of U s n strct universe, nd not s the set Σ of words over some lphet Σ. The reson, s we shll see, is tht for some opertions we encode n element of X not y one word, ut y mny, ctully y infinitely mny. In the cse of opertions on sets this is not necessry, nd one cn sfely identify the oject nd its encoding s word. We re interested in numer of opertions. A first group contins the opertions we lredy studied for sets, ut lifted to reltions. For instnce,we consider the opertion Memership((x, y), R) tht returns true if (x, y) R, nd flse otherwise, or Complement(R), tht returns R = (X X)\R. Their implementtions will e very similr to those of the lnguge cse. A second group contins three fundmentl opertions proper to reltions. Given R, R 1, R 2 U U: Projection 1(R) : returns the set π 1 (R) = {x y (x, y) R}. Projection 2(R) : returns the set π 2 (R) = {y x (x, y) R}. Join(R 1, R 2 ) : returns R 1 R 2 = {(x, z) y (x, y) R 1 (y, z) R 2 } Finlly, given X U we re interested in two derived opertions: Post(X, R) : returns post R (X) = {y U x X : (x, y) R}. Pre(X, R) : returns pre R (X) = {y x X : (y, x) R}. Oserve tht Post(X, R) = Projection 2(Join(Id X, R)), nd Pre(X, R) = Projection 1(Join(R, Id x )), where Id X = {(x, x) x X}. 103

104 104 CHAPTER 6. OPERATIONS ON RELATIONS: IMPLEMENTATIONS 6.1 Encodings We encode elements of U s words over n lphet Σ. It is convenient to ssume tht Σ contins pdding letter #, nd tht n element x U is encoded not only y word s x Σ, ut y ll the words s x # n with n 0. Tht is, n element x hs shortest encoding s x, nd other encodings re otined y ppending to s x n ritrry numer of pdding letters. We ssume tht the shortest encodings of two distinct elements re lso distinct, nd tht for every x U the lst letter of s x is different from #. It follows tht the sets of encodings of two distinct elements re disjoint. The dvntge is tht for ny two elements x, y there is numer n (in fct infinitely mny) such tht oth x nd y hve encodings of length n. We sy tht (w x, w y ) encodes the pir (x, y) if w x encodes x, w y encodes y, nd w x, w y hve the sme length. Notice tht if (w x, w y ) encodes (x, y), then so does (w x # k, w y # k ) for every k 0. If s x, s y re the shortest encodings of x nd y, nd s x s y, then the shortest encoding of (x, y) is (s x # s y s y, s y ). Exmple 6.1 We encode the numer 6 not only y its smll end inry representtion 011, ut y ny word of L(0110 ). In this cse we hve Σ = {0, 1} with 0 s pdding letter. Notice, however, tht tking 0 s pdding letter requires to tke the empty word s the shortest encoding of the numer 0 (otherwise the lst letter of the encoding of 0 is the pdding letter). In the rest of this chpter, we will use this prticulr encoding of nturl numers without further notice. We cll it the lest significnt it first encoding nd write LSBF(6) to denote the lnguge L(0110 ) If we encode n element of U y more thn one word, then we hve to define when is n element ccepted or rejected y n utomton. Does it suffice tht the utomton ccepts(rejects) some encoding, or does it hve to ccept (reject) ll of them? Severl definitions re possile, leding to different implementtions of the opertions. We choose the following option: Definition 6.2 Assume n encoding of the universe U over Σ hs een fixed. Let A e n NFA. A ccepts x U if it ccepts ll encodings of x. A rejects x U if it ccepts no encoding of x. A recognizes set X U if L(A) = {w Σ w encodes some element of X}. A set is regulr (with respect to the fixed encoding) if it is recognized y some NFA. Notice tht if A recognizes X U then, s one would expect, A ccepts every x X nd rejects every x X. Oserve further tht with this definition NFA my neither ccept nor reject given x. An NFA is well-formed if it recognizes some set of ojects, nd ill-formed otherwise.

105 6.2. TRANSDUCERS AND REGULAR RELATIONS Trnsducers nd Regulr Reltions We ssume tht n encoding of the universe U over the lphet Σ hs een fixed. Definition 6.3 A trnsducer over Σ is n NFA over the lphet Σ Σ. Trnsducers re lso clled Mely mchines. According to this definition trnsducer ccepts sequences of pirs of letters, ut it is convenient to look t it s mchine ccepting pirs of words: Definition 6.4 Let T e trnsducer over Σ. Given words w 1 = n nd w 2 = n, we sy tht T ccepts the pir (w 1, w 2 ) if it ccepts the word ( 1, 1 )...( n, n ) (Σ Σ). In other words, we identify the sets (Σ i Σ i ) nd (Σ Σ) = (Σ Σ) i. i 0 We now define when trnsducer ccepts pir (x, y) X X, which llows us to define the reltion recognized y trnsducer. The definition is completely nlogous to Definition 6.2 Definition 6.5 Let T e trnsducer. T ccepts pir (x, y) U U if it ccepts ll encodings of (x, y). T rejects pir (x, y) U U if it ccepts no encoding of (x, y). T recognizes reltion R U U if L(T) = {(w x, w y ) (Σ Σ) (w x, w y ) encodes some pir of R}. A reltion is regulr if it is recognized y some trnsducer. It is importnt to emphsize tht not every trnsducer recognizes reltion, ecuse it my recognize only some, ut not ll, the encodings of pir (x, y). As for NFAs, we sy trnsducer if well-formed if it recognizes some reltion, nd ill-formed otherwise. Exmple 6.6 The Colltz function is the function f : N N defined s follows: { 3n + 1 if n is odd f (n) = n/2 if n is even Figure 6.1 shows trnsducer tht recognizes the reltion {(n, f (n)) n N} with LSBFencoding nd with Σ = {0, 1}. The elements of Σ Σ re drwn s column vectors with two components. The trnsducer ccepts for instnce the pir (7, 22) ecuse it ccepts the pirs ( k, k ), tht is, it ccepts [ ] [ ] [ ] [ ] [ ] ([ ]) k for every k 0, nd we hve LSBF(7) = L(1110 ) nd LSBF(22) = L( ). i 0

106 106 CHAPTER 6. OPERATIONS ON RELATIONS: IMPLEMENTATIONS [ [ 0 1 [ 0 1 [ 0 [ [ ] [ [ 1 [ 1 [ 1 [ 0 [ 1 [ 0 Figure 6.1: A trnsducer for Colltz s function. Why trnsducer? In Engineering, trnsducer is device tht converts signls in one form of energy into signls in different form. Two exmples of trnsducers re microphones nd loudspekers. We cn look t trnsducer T over n lphet Σ s device tht trnsforms n input word into n output word. If we choose Σ s the union of n input nd n output lphet, nd ensure tht in every trnsition q (,) q the letters nd re n input nd n output letter, respectively, then the trnsducer trnsforms word over the input lphet into word over the output lphet. (Oserve tht the sme word cn e trnsformed into different ones.) When looking t trnsducers from this point of view, it is customry to write pir (, ) Σ Σ s /, nd red it s the trnsducer reds n nd writes. In some exercises we use this nottion. However, in section 6.4 we extend the definition of trnsducer, nd consider trnsducers tht recognize reltions of ritrry rity. For such trnsducers, the metphore of converter is less ppeling: while in inry reltion it is nturl nd cnonicl to interpret the first nd second components of pir s input nd output, there is no such cnonicl interprettion for reltion of rity 3 or higher. In prticulr, there is no cnonicl extension of the / nottion. For this reson, while we keep the nme trnsducer for historicl resons, we use the nottion q ( 1,..., n ) q for trnsitions, or the column nottion, s in Exmple 6.6. Determinism A trnsducer is deterministic if it is DFA. In prticulr, stte of deterministic trnsducer over the lphet Σ Σ hs exctly Σ 2 outgoing trnsitions. The trnsducer of Figure 6.1 is deterministic in this sense, when n pproprite sink stte is dded. There is nother possiility to define determinism of trnsducers, which corresponds to the converter interprettion (, ) / descried in the previous prgrph. If the letter / is interpreted s the trnsducer receives the input nd produces the output, then it is nturl to

107 6.3. IMPLEMENTING OPERATIONS ON RELATIONS 107 cll trnsducer deterministic if for every stte q nd every letter there is exctly one trnsition of the form (q, /, q ). Oserve tht these two definitions of determinism re not equivlent. We do not give seprte implementtions of the opertions for deterministic nd nondeterministic trnsducers. The new opertions (projection nd join) cn only e resonly implemented on nondeterministic trnsducers, nd so the deterministic cse does not dd nything new to the discussion of Chpter Implementing Opertions on Reltions In Chpter 4 we mde two ssumptions on the encoding of ojects from the universe U s words: every word is the encoding of some oject, nd every oject is encoded y exctly one word. We hve relxed the second ssumption, nd llowed for multiple encodings (in fct, infinitely mny), of n oject. Fortuntely, s long s the first ssumption still holds, the implementtions of the oolen opertions remin correct, in the following sense: If the input utomt re well formed then the output utomton is lso well-formed. Consider for instnce the complementtion opertion on DFAs. Since every word encodes some oject, the set of ll words cn e prtitioned in equivlence clsses, ech of them contining ll the encodings of n oject. If the input utomton A is well-formed, then for every oject x from the universe, A either ccepts ll the words in n equivlence clss, or none of them. The complement utomton then stisfies the sme property, ut ccepting clss iff the originl utomton does not ccept it. Notice further tht memership of n oject x in set represented y well-formed utomton cn e checked y tking ny encoding w x of x, nd checking if the utomton ccepts w x Projection Given trnsducer T recognizing reltion R X X, we construct n utomton over Σ recognizing the set π 1 (R). The initil ide is very simple: loosely speking, we go through ll trnsitions, nd replce their lels (, ) y. This trnsformtion yields NFA, nd this NFA hs n ccepting run on word 1... n iff the trnsducer hs n ccepting run on some pir (w, w ). Formlly, this step is crried out in lines 1-4 of the following lgorithm (line 5 is explined elow): Proj 1(T) Input: trnsducer T = (Q, Σ Σ, δ, Q 0, F) Output: NFA A = (Q, Σ, δ, Q 0, F ) with L(A) = π 1 (L(T)) 1 Q Q; Q 0 Q 0; F F 2 δ ; 3 for ll (q, (, ), q ) δ do 4 dd (q,, q ) to δ 5 F PdClosure((Q, Σ, δ, Q 0, F ), #)

108 108 CHAPTER 6. OPERATIONS ON RELATIONS: IMPLEMENTATIONS However, this initil ide is not fully correct. Consider the reltion R = {(1, 4)} over N. A trnsducer T recognizing R recognizes the lnguge {(10 n+2, 0010 n ) n 0} nd so the NFA constructed fter lines 1-4 recognizes {10 n+2 n 0}. However, it does not recognize the numer 1, ecuse it does not ccept ll its encodings: the encodings 1 nd 10 re rejected. This prolem cn e esily repired. We introduce n uxiliry construction tht completes given NFA: the pdding closure of n NFA is nother NFA A tht ccepts word w if nd only if the first NFA ccepts w# n for some n 0 nd pdding symol #. Formlly, the pdding closure ugments the set of finl sttes nd return new such set. Here is the lgorithm constructing the pdding closure: PdClosure(A, #) Input: NFA A = (Σ Σ, Q, δ, q 0, F) Output: new set F of finl sttes 1 W F; F ; 2 while W do 3 pick q from W 4 dd q to F 5 for ll (q, #, q) δ do 6 if q F then dd q to W 7 return F Projection onto the second component is implemented nlogously. The complexity of Proj i() is clerly O( δ + Q ), since every trnsition is exmined t most twice, once in line 3, nd possily second time t line 5 of PdClosure. Oserve tht projection does not preserve determinism, ecuse two trnsitions leving stte nd leled y two different (pirs of) letters (, ) nd (, c), ecome fter projection two trnsitions leled with the sme letter : In prctice the projection of trnsducer is hrdly ever deterministic. Since, typiclly, sequence of opertions mnipulting trnsitions contins t lest one projection, deterministic trnsducers re reltively uninteresting. Exmple 6.7 Figure 6.2 shows the NFA otined y projecting the trnsducer for the Colltz function onto the first nd second components. Sttes 4 nd 5 of the NFA t the top (first component) re mde finl y PdClosure, ecuse they cn oth rech the finl stte 6 through chin of 0s (recll tht 0 is the pdding symol in this cse). The sme hppens to stte 3 for the NFA t the ottom (second component), which cn rech the finl stte 2 with 0. Recll tht the trnsducer recognizes the reltion R = {(n, f (n)) n N}, where f denotes the Colltz function. So we hve π 1 (R) = {n n N} = N nd π 2 (R) = { f (n) n N}, nd moment of thought shows tht π 2 (R) = N s well. So oth NFAs should e universl, nd the reder

109 6.3. IMPLEMENTING OPERATIONS ON RELATIONS Figure 6.2: Projections of the trnsducer for the Colltz function onto the first nd second components. cn esily check tht this is indeed the cse. Oserve tht oth projections re nondeterministic, lthough the trnsducer is deterministic Join, Post, nd Pre We give n implementtion of the Join opertion, nd then show how to modify it to otin implementtions of Pre nd Post. Given trnsducers T 1, T 2 recognizing reltions R 1 nd R 2, we construct trnsducer T 1 T 2 recognizing R 1 R 2. We first construct trnsducer T with the following property: T ccepts (w, w ) iff there is word w such tht T 1 ccepts (w, w ) nd T 2 ccepts (w, w ). The intuitive ide is to slightly modify the product construction. Recll tht the piring [A 1, A 2 ] of two NFA A 1, A 2 hs

110 110 CHAPTER 6. OPERATIONS ON RELATIONS: IMPLEMENTATIONS trnsition [q, r] [q, r ] if nd only if A 1 hs trnsition q r nd A 2 hs trnsition q (,) r. Similrly, T 1 T 2 hs trnsition [q, r] [q, r ] if there is letter c such tht T 1 hs trnsition r nd A 2 hs trnsition q r. Intuitively, T cn output on input if there is letter c such tht T 1 cn output c on input, nd T 2 cn output on input c. The trnsducer T hs run q (,c) (c,) [ q01 q 02 ] 1 1 [ q11 q 12 ] 2 2 [ q21 q 22 ]... [ q(n 1)1 q (n 1)2 ] n n [ qn1 q n2 ] iff T 1 nd T 2 hve runs q 01 q 02 1 c 1 q 11 c 1 1 q 12 2 c 2 q q (n 1)1 c 2 2 q q (n 1)2 n c n c n n q n1 q n2 Formlly, if T 1 = (Q 1, Σ Σ, δ 1, Q 01, F 1 ) nd T 2 = (Q 2, Σ Σ, δ 2, Q 02, F 2 ), then T = (Q, Σ Σ, δ, Q 0, F ) is the trnsducer generted y lines 1 9 of the lgorithm elow: Join(T 1, T 2 ) Input: trnsducers T 1 = (Q 1, Σ Σ, δ 1, Q 01, F 1 ), T 2 = (Q 2, Σ Σ, δ 2, Q 02, F 2 ) Output: trnsducer T 1 T 2 = (Q, Σ Σ, δ, Q 0, F) 1 Q, δ, F ; Q 0 Q 01 Q 02 2 W Q 0 3 while W do 4 pick [q 1, q 2 ] from W 5 dd [q 1, q 2 ] to Q 6 if q 1 F 1 nd q 2 F 2 then dd [q 1, q 2 ] to F 7 for ll (q 1, (, c), q 1 ) δ 1, (q 2, (c, ), q 2 ) δ 2 do 8 dd ([q 1, q 2 ], (, ), [q 1, q 2 ]) to δ 9 if [q 1, q 2 ] Q then dd [q 1, q 2 ] to W 10 F PdClosure((Q, Σ Σ, δ, Q 0, F ), (#, #)) However, T is not yet the trnsducer we re looking for. The prolem is similr to the one of the projection opertion. Consider the reltions on numers R 1 = {(2, 4)} nd R 2 = {(4, 2)}. Then T 1 nd T 2 recognize the lnguges {(010 n+1, 0010 n ) n 0} nd {(0010 n, 010 n+1 ) n 0} of word pirs. So T recognizes {(010 n+1, 010 n+1 ) n 0}. But then, ccording to our definition, T does not

111 6.3. IMPLEMENTING OPERATIONS ON RELATIONS 111 ccept the pir (2, 2) N N, ecuse it does not ccept ll its encodings: the encoding (01, 01) is missing. So we dd pdding closure gin t line 10, this time using [#, #] s pdding symol. The numer of sttes of Join(T 1, T 2 ) is O( Q 1 Q 2 ), s for the stndrd product construction. Exmple 6.8 Recll tht the trnsducer of Figure 6.1, shown gin t the top of Figure 6.3, recognizes the reltion {(n, f (n)) n N}, where f is the Colltz function. Let T e this trnsducer. The ottom prt of Figure 6.3 shows the trnsducer T T s computed y Join(T,T). For exmple, the trnsition leding from [2, 3] to [3, 2], leled y (0, 0), is the result of piring the trnsition from 2 to 3 leled y (0, 1), nd the one from 3 to 2 leled y (1, 0). Oserve tht T T is not deterministic, ecuse for instnce [1, is the source of two trnsitions leled y (0, 0), even though T is deterministic. This trnsducer recognizes the reltion {(n, f ( f (n))) n N}. A little clcultion gives f ( f ((n)) = n/4 if n 0 mod 4 3n/2 + 1 if n 2 mod 4 3n/2 + 1/2 if n 1 mod 4 or n 3 mod 4 The three components of the trnsducer rechle from the stte [1, correspond to these three cses. Post(X,R) nd Pre(X, R) Given n NFA A 1 = (Q 1, Σ, δ 1, Q 01, F 1 ) recognizing regulr set X U nd trnsducer T 2 = (Q 2, Σ Σ, δ 2, q 02, F 2 ) recognizing regulr reltion R U U, we construct n NFA B recognizing the set post R (U). It suffices to slightly modify the join opertion. The lgorithm Post(A 1,T 2 ) is the result of replcing lines 7-8 of Join() y 7 for ll (q 1, c, q 1 ) δ 1, (q 2, (c, ), q 2 ) δ 2 do 8 dd ([q 1, q 2 ],, [q 1, q 2 ]) to δ As for the join opertion, the resulting NFA hs to e postprocessed, closing it with respect to the pdding symol. In order to construct n NFA recognizing pre R (X), we replce lines 7-8 y 7 for ll (q 1, (, c), q 1 ) δ 1, (q 2, c, q 2 ) δ 2 do 8 dd δ to ([q 1, q 2 ],, [q 1, q 2 ]) Notice tht oth post nd pre re computed with the sme complexity s the piring construction, nmely, the product of the numer of sttes of trnsducer nd NFA. Exmple 6.9 We construct n NFA recognizing the imge under the Colltz function of ll multiples of 3, i.e., the set { f (3n) n N}. For this, we first need n utomton recognizing the set Y of ll lsf encodings of the multiples of 3. The following DFA does the jo:

112 112 CHAPTER 6. OPERATIONS ON RELATIONS: IMPLEMENTATIONS [ 0 [ 1 [ 0 [ [ 1 [ 0 1 [ 1 [ ] [ [ [ 1 1 [ 0 [ 1 [ 0 2, 3 3, 3 [ 0 [ [ 0 [ 1 1 [ 1 [ 0 [ 0 2, 2 3, 2 [ 0 [ 1 [ 0 2, 5 2, 6 [ 0 1, 1 [ 0 [ ] [ ] [ 1 [ 0 3, 4 [ 3, 5 1 [ 1 [ 1 [ 1 [ 1 [ 0 4, 2 5, 3 [ 1 [ 0 [ 1 [ 0 [ 0 6, 2 [ 0 [ 0 4, 3 5, 2 [ 1 [ 1 6, 3 [ 1 Figure 6.3: A trnsducer for f ( f (n)).

113 6.4. RELATIONS OF HIGHER ARITY For instnce, this DFA recognizes 0011 (encoding of 12) nd (encoding of 18), ut not 0101 (encoding of 10). We now compute post R (Y), where, s usul, R = {(n, f (n)) n N}. The result 1 is the NFA shown in Figure 6.4. For instnce, the trnsition [1, [1, 3] is generted y the trnsitions of the DFA nd 1 (0,1) 3 of the trnsducer for the Colltz function. Stte [2, 3] ecomes finl due to the closure with respect to the pdding symol 0. The NFA of Figure 6.4 is not difficult to interpret. The multiples of 3 re the union of the sets {6k k 0}, ll whose elements re even, nd the set {6k + 3 k 0}, ll whose elements re odd. Applying f to them yields the sets {3k k 0} nd {18k + 10 k 0}. The first of them is gin the set of ll multiples of 3, nd it is recognized y the upper prt of the NFA. (In fct, this upper prt is DFA, nd if we minimize it we otin exctly the DFA given ove.) The lower prt of the NFA recognizes the second set. The lower prt is miniml; it is esy to find for ech stte word recognized y it, ut not y the others. It is interesting to oserve tht n explicit computtion of the set { f (3k) k 0}) in which we pply f to ech multiple of 3 does not terminte, ecuse the set is infinite. In sense, our solution speeds up the computtion y n infinite fctor! 6.4 Reltions of Higher Arity The implementtions descried in the previous sections cn e esily extended to reltions of higher rity over the universe U. We riefly descrie the generliztion. Fix n encoding of the universe U over the lphet Σ with pdding symol #. A tuple (w 1,..., w k ) of words over Σ encodes the tuple (x 1,..., x k ) U k if w i encodes x i for every 1 i k, nd w 1,..., w k hve the sme length. A k-trnsducer over Σ is n NFA over the lphet Σ k. Acceptnce of k-trnsducer is defined s for norml trnsducers. Boolen opertions re defined s for NFAs. The projection opertion cn e generlized to projection over n ritrry suset of components. For this, given n index set I = {i 1,..., i n } {1,..., k}, let x I denote the projection of tuple x = (x 1,..., x k ) U k over I, defined s the tuple (x i1,..., x in ) U n. Given reltion R U, we define Projection I(R): returns the set π I (R) = { x I x R}. The opertion is implemented nlogously to the cse of inry reltion. Given k-trnsducer T recognizing R, the n-trnsducer recognizing Projection P(R) is computed s follows: Replce every trnsition (q, ( 1,..., k ), q ) of T y the trnsition (q, ( i1,..., in ), q ).

114 114 CHAPTER 6. OPERATIONS ON RELATIONS: IMPLEMENTATIONS [ 0 [ 1 [ [ [ 1 [ [ 1 [ ] [ [ [ 1 1 [ 0 [ 1 1, 3 0 2, 2 1 3, , 2 0 2, 3 1 3, 2 1, , , , , 4 0 2, 5 1 1, 6 1 3, , , 6 1 Figure 6.4: Computing f (n) for ll multiples of 3.

115 6.4. RELATIONS OF HIGHER ARITY 115 Compute the PAD-closure of the result: for every trnsition (q, (#,..., #), q ), if q is finl stte, then dd q to the set of finl sttes. The join opertion cn lso e generlized. Given two tuples x = (x 1,..., x n ) nd y = (y 1,..., y m ) of rities n nd m, respectively, we denote the tuple (x 1,..., x n, y 1,..., y m ) of dimension n + m y x y. Given reltions R 1 U k 1 nd R 2 U k 2 of rities k 1 nd k 2, respectively, nd index sets I 1 {1,..., k 1 }, I 2 {1,..., k 2 } of the sme crdinlity, we define Join I(R 1, R 2 ): returns R 1 I1,I 2 R 2 = { x K1 \I 1 x K2 \I 2 x R 1, y R 2 : x I1 = y I2 } The rity of Join I(R 1, R 2 ) is k 1 + k 2 I 1. The opertion is implemented nlogously to the cse of inry reltions. We proceed in two steps. The first step constructs trnsducer ccording to the following rule: If the trnsducer recognizing R 1 hs trnsition (q,, q ), the trnsducer recognizing R 2 hs trnsition (r,, r ), nd I1 = I 2, then dd to the trnsducer for Join I(R 1, R 2 ) trnsition ([q, r], K1 \I 1 K2 \I 2, [q, r ]). In the second step, we compute the PAD-closure of the result. The generliztion of the Pre nd Post opertions is nlogous. Exercises Exercise 62 Let U = N e the universe of nturl numers, nd consider the MSBF encoding. Give trnsducers for the sets of pirs (n, m) N 2 such tht () m = n + 1, () m = n/2, (c) n/4 m 4n. How do the constructions chnge for the LSBF encoding? Exercise 63 Let U e some universe of ojects, nd fix n encoding of U over Σ. Prove or disprove: if reltion R U U is regulr, then the lnguge is regulr. L R = {w x w y (w x, w y ) encodes pir (x, y) R} Exercise 64 Let A e n NFA over the lphet Σ. () Show how to construct trnsducer T over the lphet Σ Σ such tht (w, v) L(T) iff wv L(A) nd w = v. () Give n lgorithm tht ccepts n NFA A s input nd returns n NFA A/2 such tht L(A/2) = {w Σ v Σ : wv L(A) w = v }. Exercise 65 In phone dils letters re mpped into digits s follows:

116 116 CHAPTER 6. OPERATIONS ON RELATIONS: IMPLEMENTATIONS ABC 2 DEF 3 GHI 4 JKL 5 MNO 6 PQRS 7 TUV 8 WXYZ 9 This mp cn e used to ssign telephone numer to given word. For instnce, the numer for AUTOMATON is Consider the prolem of, given telephone numer (for simplicity, we ssume tht it contins neither 1 nor 0), finding the set of English words tht re mpped into it. For instnce, the set of words mpping to 233 contins t lest ADD, BED, nd BEE. Assume DFA N over the lphet {A,..., Z} recognizing the set of ll English words is given. Given numer n, show how to construct NFA recognizing ll the words tht re mpped to n. Exercise 66 As we hve seen, the ppliction of the Post, Pre opertions to trnsducers requires to compute the pdding closure in order to gurntee tht the resulting utomton ccepts either ll or none of the encodings of oject. The pdding closure hs een defined for encodings where pdding occurs on the right, i.e., if w encodes n oject, then so does w # k for every k 0. However, in some nturl encodings, like the most-significnt-it-first encoding of nturl numers, pdding occurs on the left. Give n lgorithm for clculting the pdding closure of trnsducer when pdding occurs on the left. Exercise 67 We hve defined trnsducers s NFAs whose trnsitions re leled y pirs of symols (, ) Σ Σ. With this definition trnsducers cn only ccept pirs of words ( 1... n, 1... n ) of the sme length. In mny pplictions this is limiting. An ε-trnsducer is NFA whose trnsitions re leled y elements of (Σ {ε}) (Σ {ε}). An ε-trnsducer ccepts pir (w, w ) of words if it hs run q 0 ( 1, 1 ) q 1 ( 2, 2 ) ( n, n ) q n with i, i Σ {ε} such tht w = 1... n nd w = 1... n. Note tht w n nd w n. The reltion ccepted y the ε-trnsducer T is denoted y L(T). The figure elow shows trnsducer over the lphet {, } tht, intuitively, duplictes the letters of word, e.g., on input it outputs. In the figure we use the nottion /. ε/ / / ε/

117 6.4. RELATIONS OF HIGHER ARITY 117 Give n lgorithm Post ε (A, T) tht, given NFA A nd n ε-trnsducer T, oth over the sme lphet Σ, returns NFA recognizing the lnguge post Tε (A) = {w w L(A) such tht (w, w) L(T)} Hint: View ε s n dditionl lphet letter. Exercise 68 Trnsducers cn e used to cpture the ehviour of simple progrms. The figure elow shows progrm P nd its control-flow digrm (the instruction end finishes the execution of the progrm). ool x, y init 0 x? write x while true do red y until y = x y if x = y then write y end x x 1 or y x + y if x y then write x end x = y 1 x? 2 write x 3 red y x x y 4 x = x y x = y 6 5 x y write y 8 7 x x 1 y x + y write x 9 x y 10 P communictes with the environment through the oolen vriles x nd y, oth with 0 s initil vlue. Let [i, x, y] denote the stte of P in which the next instruction to e executed is the one t line i, nd the vlues of x nd y re x nd y, respectively. The I/O-reltion of P is the set of pirs (w I, w O ) {0, 1} {0, 1} such tht there is n execution of P during which P reds w I nd writes w O. Let [i, x, y] denote the configurtion of P in which P is t node i of the control-flow digrm, nd the vlues of x nd y re x nd y, respectively. The initil configurtion of P is [1, 0,. By executing the first instruction P moves nondeterministiclly to one of the configurtions [2, 0, nd [2, 1, ; no input symol is red nd no output symol is written. Similrly, y executing its second instruction, the progrm P moves from [2, 1, to [3, 1, while reding nothing nd writing 1. () Give n ε-trnsducer recognizing the I/O-reltion of P.

118 118 CHAPTER 6. OPERATIONS ON RELATIONS: IMPLEMENTATIONS () Cn n overflow error occur? (Tht is, cn configurtion e reched in which the vlue of X or y is not 0 or 1?) (c) Cn node 10 of the control-flow grph e reched? (d) Wht re the possile vlues of x upon termintion, i.e. upon reching end? (e) Is there n execution during which P reds 101 nd writes 01? (f) Let I nd O e regulr sets of inputs nd outputs, respectively. Think of O s set of dngerous outputs tht we wnt to void. We wish to prove tht the inputs from I re sfe, i.e. tht when P is fed inputs from I, none of the dngerous outputs cn occur. Descrie n lgorithm tht decides, given I nd O, whether there re i I nd o O such tht (i, o) elongs to the I/O-reltion of P. Exercise 69 In Exercise 67 we hve shown how to compute pre- nd post-imges of reltions descried y ε-trnsducers. In this exercise we show tht, unfortuntely, nd unlike stndrd trnsducers, ε-trnsducers re not closed under intersection. () Construct ε-trnsducers T 1, T 2 recognizing the reltions R 1 = {( n m, c 2n ) n, m 0}, nd R 2 = {( n m, c 2m ) n, m 0}. () Show tht no ε-trnsducer recognizes R 1 R 2. Exercise 70 (Inspired y pper y Glwni l POPL 11.) Consider trnsducers whose trnsitions re leled y elements of (Σ {ε}) (Σ {ε}). Intuitively, t ech trnsition these trnsducers red one letter or no letter, n write string of ritrry length. These trnsducers cn e used to perform opertions on strings like, for instnce, cpitlizing ll the words in the string: if the trnsducer reds, sy, singing in the rin, it writes Singing In The Rin. Sketch ε-trnsducers for the following opertions, ech of which is informlly defined y mens of two exmples. In ech exmple, when the trnsducer reds the string on the left it writes the string on the right. Compny\Code\index.html Compny\Docs\Spec\specs.doc Interntionl Business Mchines Principles Of Progrmming Lnguges Compny\Code Compny\Docs\Spec IBM POPL Oege De Moor Oege De Moor Kthleen Fisher AT&T Ls Kthleen Fisher AT&T Ls Ern Yhv Yhv, E. Bill Gtes Gtes, B (00)

119 Chpter 7 Finite Universes In Chpter 3 we proved tht every regulr lnguge hs unique miniml DFA. A nturl question is whether the opertions on lnguges nd reltions descried in Chpters 4 nd 6 cn e implemented using miniml DFAs nd miniml deterministic trnsducers s dt structure. The implementtions of (the first prt of) Chpter 4 ccept nd return DFAs, ut do not preserve minimlity: even if the rguments re miniml DFAs, the result my e non-miniml (the only exception ws complementtion). So, in order to return the miniml DFA for the result n extr minimiztion opertion must e pplied. The sitution is worse for the projection nd join opertions of Chpter 6, ecuse they do not even preserve determincy: the result of projecting deterministic trnsducer or joining two of them my e nondeterministic. In order to return miniml DFA it is necessry to first determinize, t exponentil cost in the worst cse, nd then minimize. In this chpter we present implementtions tht directly yield the miniml DFA, with no need for n extr minimiztion step, for the cse in which the universe U of ojects is finite. When the universe is finite, ll ojects cn e encoded y words of the sme length, nd this common length is known priori. For instnce, if the universe consists of the numers in the rnge [ , its ojects cn e encoded y words over {0, 1} of length 32. Since ll encodings hve the sme length, pdding is not required to represent tuples of ojects, nd we cn ssume tht ech oject is encoded y exctly one word. As in Chpter??, we lso ssume tht ech word encodes some oject. Opertions on ojects correspond to opertions on lnguges, ut complementtion requires some cre. If X U is encoded s lnguge L Σ k for some numer k 0, then the complement set U \ X is not encoded y L (which contins words of ny length) ut y L Σ k. 7.1 Fixed-length Lnguges nd the Mster Automton Definition 7.1 A lnguge L Σ hs length n 0 if every word of L hs length n. If L hs length n for some n 0, then we sy tht L is fixed-length lnguge, or tht it hs fixed-length. Some remrks re in order: 119

120 120 CHAPTER 7. FINITE UNIVERSES According to this definition, the empty lnguge hs length n for every n 0 (the ssertion every word of L hs length n is vcuously true). This is useful, ecuse then the complement of lnguge of length n hs lso length n. There re exctly two lnguges of length 0: the empty lnguge, nd the lnguge {ε} contining only the empty word. Every fixed-length lnguge contins only finitely mny words, nd so it is regulr. The mster utomton over n lphet Σ is deterministic utomton with n infinite numer of sttes, ut no initil stte. As in the cse of cnonicl DAs, the sttes re lnguges. For the definition, recll the notion of residul with respect to letter: given lnguge L Σ nd Σ, its residul with respect to is the lnguge L = {w Σ w L}. Recll tht, in prticulr, we hve = {ɛ} =. A simple ut importnt oservtion is tht if L hs fixed-length, then so does L. Definition 7.2 The mster utomton over the lphet Σ is the tuple M = (Q M, Σ, δ M, F M ), where Q M is is the set of ll fixed-length lnguges over Σ; δ: Q M Σ Q M is given y δ(l, ) = L for every q Q M nd Σ; F M is the singleton set contining the lnguge {ɛ} s only element. Exmple 7.3 Figure 7.1 shows smll frgment of the mster utomton for the lphet Σ = {, }. Notice tht M is lmost cyclic. More precisely, the only cycles of M re the self-loops corresponding to δ M (, ) = for every Σ. The following proposition ws lredy proved in Chpter 3, ut with slightly different terminology. Proposition 7.4 Let L e fixed-length lnguge. The lnguge recognized from the stte L of the mster utomton is L. Proof: By induction on the length n of L. If n = 0, then L = {ɛ} or L =, nd the result is proved y direct inspection of the mster utomton. For n > 0 we oserve tht the successors of the initil stte L re the lnguges L for every Σ. Since, y induction hypothesis, the stte L recognizes the lnguge L, the stte L recognizes the lnguge L. By this proposition, we cn look t the mster utomton s structure contining DFAs recognizing ll the fixed-length lnguges. To mke this precise, ech fixed-length lnguge L determines DFA A L = (Q L, Σ, δ L, q 0L, F L ) s follows: Q L is the set of sttes of the mster utomton rechle from the stte L; q 0L is the stte L; δ L is the projection of δ M onto Q L ; nd F L = F M. It is esy to show tht A L is the miniml DFAs recognizing L:

121 7.2. A DATA STRUCTURE FOR FIXED-LENGTH LANGUAGES 121 {,,,,,, } {,,,, } {,, } {,,, } {, } {,, },, {} {, } {}, {ɛ},, Figure 7.1: A frgment of the mster utomton for the lphet {, } Proposition 7.5 For every fixed-lnguge L, the utomton A L is the miniml DFA recognizing L. Proof: By definition, distinct sttes of the mster utomton re distint lnguges. By Proposition 7.4, distinct sttes of A L recognize distinct lnguges. By Corollry 3.8 ( DFA is miniml if nd only if distinct sttes recognized different lnguges) A L is miniml. 7.2 A Dt Structure for Fixed-length Lnguges Proposition 7.5 llows us to define dt structure for representing finite sets of fixed-length lnguges, ll of them of the sme length. Loosely speking, the structure representing the lnguges L = {L 1,..., L m } is the frgment of the mster utomton contining the sttes recognizing L 1,..., L n nd their descendnts. It is DFA with multiple initil sttes, nd for this reson we cll it the multi-dfa for L. Formlly: Definition 7.6 Let L = {L 1,..., L n } e set of lnguges of the sme length over the sme lphet Σ. The multi-dfa A L is the tuple A L = (Q L, Σ, δ L, Q 0L, F L ), where Q L is the set of sttes of the mster utomton rechle from t lest one of the sttes L 1,..., L n ; Q 0L = {L 1,..., L n }; δ L is the projection of δ M onto Q L ; nd F L = F M.

122 122 CHAPTER 7. FINITE UNIVERSES L 1 L 2 L ,, , Figure 7.2: The multi-dfa for {L 1, L 2, L 3 } with L 1 = {, }, L 2 = {,, }, nd L 3 = {, }. Exmple 7.7 Figure 7.2 shows ( DFA isomorphic to) the multi-dfa for the set {L 1, L 2, L 3 }, where L 1 = {, }, L 2 = {,, }, nd L 3 = {, }. For clrity the stte for the empty lnguge hs een omitted, s well s the trnsitions leding to it. In order to mnipulte multi-dfas we represent them s tle of nodes. Assume Σ = { 1,..., m }. A node is pir q, s, where q is stte identifier nd s = (q 1,..., q m ) is the successor tuple of the node. Along the chpter we denote the stte identifiers of the stte for the lnguges nd {ɛ} y q nd q ɛ, respectively. The multi-dfa is represented y tle contining node for ech stte, with the exception of the nodes q nd q ɛ. The tle for the multi-dfa of Figure 7.2, where stte identifiers re numers, is Ident. -succ -succ The procedure mke(s). The lgorithms on multi-dfas use procedure mke(s) tht returns the stte of T hving s s successor tuple, if such stte exists; otherwise, it dds new node q, s to T, where q is fresh stte identifier (different from ll other stte identifiers in T) nd returns q. If s is the tuple ll whose components re q, then mke(s) returns q. The procedure ssumes tht ll the sttes of the tuple s (with the exception of q nd q ɛ ) pper in T. 1 For instnce, if T is the 1 Notice tht the procedure mkes use of the fct tht no two sttes of the tle hve the sme successor tuple.

123 7.3. OPERATIONS ON FIXED-LENGTH LANGUAGES 123 L 1 L 1 L 2 L 2 L 3 L 2 L ,, , Figure 7.3: The multi-dfa for {L 1, L 2, L 3, L 1 L 2, L 2 L 3 } tle ove, then mke(2, 2) returns 5, ut mke(3, 2) dds new row, sy 8, 3, 2, nd returns Opertions on fixed-length lnguges All opertions ssume tht the input fixed-length lnguge(s) is (re) given s multi-dfas represented s tle of nodes. Nodes re pirs of stte identifier nd successor tuple. The key to ll implementtions is the fct tht if L is lnguge of length n 1, then L is lnguge of length n 1. This llows to design recursive lgorithms tht directly compute the result when the inputs re lnguges of length 0, nd reduce the prolem of computing the result for lnguges of length n 1 to the sme prolem for lnguges of smller length. Fixed-length memership. in the size of the input. The opertion is implemented s for DFAs. The complexity is liner Fixed-length union nd intersection. Implementing oolen opertion on multi-dfas corresponds to possily extending the multi-dfa, nd returning the stte corresponding to the result of the opertion. This is est explined y mens of n exmple. Consider gin the multi-dfa of Figure 7.2. An opertion like Union(L 1, L 2 ) gets the initil sttes 5 nd 6 s input, nd returns the stte recognizing L 1 L 2 ; since L 1 L 2 = L 2, the opertion returns stte 6. However, if we tke Intersection(L 2, L 3 ), then the multi-dfa does not contin ny stte recognizing it. In this cse the opertion extends the multi-dfa for {L 1, L 2, L 3 } to the multi-dfa for {L 1, L 2, L 3, L 2 L 3 }, shown in Figure 7.3, nd returns stte 8. So Intersection(L 2, L 3 ) not only returns stte, ut lso hs side effect on the multi-dfa underlying the opertions.

124 124 CHAPTER 7. FINITE UNIVERSES Given two fixed-length lnguges L 1, L 2 of the sme length nd oolen opertion, we present n lgorithm tht returns the stte of the mster utomton recognizing L 1 L 2 (the lgorithm for L 1 L 2 is nlogous). The properties if L 1 = or L 2 =, then L 1 L 2 = ; if L 1 = {ɛ} nd L 2 = {ɛ}, then L 1 L 2 = {ɛ}; if L 1, L 2 {, {ɛ}}, then (L 1 L 2 ) = L1 L 2 for every Σ; led to the recursive lgorithm inter(q 1, q 2 ) shown in Tle Assume the sttes q 1, q 2 recognize the lnguges L 1, L 2. The lgorithm returns the stte identifier q L1 L 2. If q 1 = q, then L 1 =, which implies L 1 L 2 =. So the lgorithm returns the stte identifier q. If q 2 = q, the lgorithm lso returns q. If q 1 = q ɛ = q 2, the lgorithm returns q ɛ. This dels with ll the cses in which q 1, q 2 {q, q ɛ } (nd some more, which does no hrm). If q 1, q 2 {q, q ɛ }, then the lgorithm computes the stte identifiers r 1,..., r m recognizing the lnguges (L 1 L 2 ) 1,..., (L 1 L 2 ) m, nd returns mke(r 1,..., r n ) (creting new node if no node of T hs (r 1,..., r n ) s successor tuple). But how does the lgorithm compute the stte identifier of (L 1 L 2 ) i? By eqution (3) ove, we hve (L 1 L 2 ) i = L i 1 L i 2, nd so the lgorithm computes the stte identifier of L i 1 L i 2 y recursive cll inter(q i 1, q i 2 ). The only remining point is the rôle of the tle G. The lgorithm uses memoiztion to void recomputing the sme oject. The tle G is initilly empty. When inter(q 1, q 2 ) is computed for the first time, the result is memoized in G(q 1, q 2 ). In ny susequent cll the result is not recomputed, ut just red from G. For the complexity, let n 1, n 2 e the numer of sttes of T rechle from the stte q 1, q 2. It is esy to see tht every cll to inter receives s rguments sttes rechle from q 1 nd q 2, respectively. So inter is clled with t most n 1 n 2 possile rguments, nd the complexity is O(n 1 n 2 ). inter(q 1, q 2 ) Input: sttes q 1, q 2 recognizing lnguges of the sme length Output: stte recognizing L(q 1 ) L(q 2 ) 1 if G(q 1, q 2 ) is not empty then return G(q 1, q 2 ) 2 if q 1 = q or q 2 = q then return q 3 else if q 1 = q ε nd q 2 = q ε then return q ɛ 4 else / q 1, q 2 {q, q ε } / 5 for ll i = 1,..., m do r i inter(q i 6 G(q 1, q 2 ) mke(r 1,..., r m ) 7 return G(q 1, q 2 ) 1, q i 2 ) Tle 7.1: Algorithm inter Algorithm inter is generic: in order to otin n lgorithm for nother inry opertor it suffices to chnge lines 2 nd 3. If we re only interested in intersection, then we cn esily gin more

125 7.3. OPERATIONS ON FIXED-LENGTH LANGUAGES 125 efficient version. For instnce, we know tht inter(q 1, q 2 ) nd inter(q 2, q 1 ) return the sme stte, nd so we cn improve line 1 y checking not only if G(q 1, q 2 ) is nonempty, ut lso if G(q 2, q 1 ) is. Also, inter(q, q) lwys returns q, no need to compute nything either. Exmple 7.8 Consider the multi-dfa t the top of Figure 7.4, ut without the lue sttes. Stte 0, ccepting the empty lnguge, is gin not shown. The tree t the ottom of the figure grphiclly descries the run of inter(12, 13) (tht is, we compute the node for the intersection of the lnguges recognized from sttes 12 nd 13). A node q, q q of the tree stnds for recursive cll to inter with rguments q nd q tht returns q. For instnce, the node 2,4 2 indictes tht inter is clled with rguments 2 nd 4 nd the cll returns stte 2. Let us see why is this so. The cll inter(2, 4) produces two recursive clls, first inter(1, 1) (the -successors of 2 nd 4), nd then inter(0, 1). The first cll returns 1, nd the second 0. Therefore inter(2, 4) returns stte with 1 s -successor nd 0 s -successor. Since this stte lredy exists (it is stte 2), inter(2, 4) returns 2. On the other hnd, inter(9,10) cretes nd returns new stte: its two children clls return 5 nd 6, nd so new stte with stte 5 nd 6 s - nd -successors must e creted. Pink nodes correspond to clls tht hve lredy een computed, nd for which inter just looks up the result in G. Green nodes correspond to clls tht re computed y inter, ut not y the more efficient version. For instnce, the result of inter(4,4) t the ottom right cn e returned immeditely. Fixed-length complement. Recll tht if set X U is encoded y lnguge L of length n, then the set U \ X is encoded y the fixed-length complement Σ n \ L, which we denote y L n. Since the empty lnguge hs ll lengths, we hve e.g. 2 = Σ 2, ut 3 = Σ 3 nd 0 = Σ 0 = {ɛ}. Given the stte of the mster utomton recognizing L, we compute the stte recognizing L n with the help of these properties: If L hs length 0 nd L = then L 0 = {ɛ}. If L hs length 0 nd L = {ɛ}, then L 0 =. If L hs length n 1, then ( L n) = L (n 1). (Oserve tht w ( L ) iff w L iff w L iff w L.) We otin the lgorithm of Tle 7.9. If the mster utomton hs n sttes rechle from q, then the opertion hs complexity O(n). Exmple 7.9 Consider gin the multi-dfa t the top of Figure 7.5 without the lue sttes. The tree of recursive clls t the ottom of the figure grphiclly descries the run of comp(4, 12) (tht is, we compute the node for the complement of the lnguge recognized from stte 12, which hs length 4). For instnce, comp(1,2) genertes two recursive clls, first comp(0,1) (the -successor of 2), nd then comp(0,0). The clls returs 0 nd 1, respectively, nd so comp(1,2) returns 3. Oserve

126 126 CHAPTER 7. FINITE UNIVERSES , 5 6 7, , 12, , , , 7 5 0, 7 0 5, 7 5 7, 6 6 2, 4 2 3, 4 3 4, 2 2 4, 4 4 1, 1 1 0, 1 0 0, 1 0 1, 1 1 1, 1 1 1, 0 0 1, 1 1 1, 1 1 Figure 7.4: An execution of inter.

127 7.3. OPERATIONS ON FIXED-LENGTH LANGUAGES 127 comp(n, q) Input: length n, stte q of length n Output: stte recognizing L(q) n 1 if G(n, q) is not empty then return G(n, q) 2 if n = 0 nd q = q then return q ɛ 3 else if n = 0 nd q = q ɛ then return q 4 else / n 1 / 5 for ll i = 1,..., m do r i comp(n 1, q i ) 6 G(n, q) mke(r 1,..., r m ) 7 return G(n, q) Tle 7.2: Algorithm comp how the cll comp(2,0) returns 7, the stte ccepting {, } 2. Pink nodes correspond gin to clls for which comp just looks up the result in G. Green nodes correspond to clls whose result is directly computed y more efficient version of comp tht pplies the following rule: if comp(i, j) returns k, then comp(i, k) returns j. Fixed-length emptiness. A fixed-lnguge lnguge is empty if nd only if the node representing it hs q s stte identifier, nd so emptiness cn e checked in constnt time.. Fixed-length universlity. A lnguge L of length n is fixed-length universl if L = Σ n. The universlity of lnguge of length n recognized y stte q cn e checked in time O(n). It suffices to check for ll sttes rechle from q, with the exception of q, tht no trnsition leving them leds to q. More systemticlly, we use the properties if L =, then L is not universl; if L = {ɛ}, then L is universl; if L {ɛ}, then L is universl iff L is universl for every Σ; tht led to the lgorithm of Tle 7.3. Fixed-length inclusion. Given two lnguges L 1, L 2 Σ n, in order to check L 1 L 2 we compute L 1 L 2 nd check whether it is equl to L 1 using the equlity check shown next. The complexity is dominted y the complexity of computing the intersection.

128 128 CHAPTER 7. FINITE UNIVERSES , , , 4: : : : : 0 7 2: : 7 0 1: 2 3 1: 3 2 1: 0 4 1: 0 4 0: 1 0 0; 0 1 0; 0 1 0; 0 1 Figure 7.5: An execution of comp.

129 7.4. DETERMINIZATION AND MINIMIZATION 129 univ(q) Input: stte q Output: true if L(q) is fixed-length universl, flse otherwise 1 if G(q) is not empty then return G(q) 2 if q = q then return flse 3 else if q = q ɛ then return true 4 else / q q nd q q ɛ / 5 G(q) nd(univ(q 1 ),..., univ(q m )) 6 return G(q) Tle 7.3: Algorithm univ Fixed-length equlity. Since the miniml DFA recognizing lnguge is unique, two lnguges re equl if nd only if the nodes representing them hve the sme stte identifier, leding to constnt time lgorithm. This solution, however, ssumes tht the two input nodes come from the sme tle. If they come from two different tles T 1, T 2, then, since stte identifiers cn e ssigned in oth tles in different wys, it is necessry to check if the DFA rooted t the sttes q 1 nd q 2 re isomorphic. This is done y lgorithm eq2 of Tle 7.4, which ssumes tht q i elongs to tle T i, nd tht oth tles ssign stte identifiers q 1 nd q 2 to the empty lnguge. eq2(q 1, q 2 ) Input: sttes q 1, q 2 of different tles Output: true if L(q 1 ) = L(q 2 ), flse otherwise 1 if G(q 1, q 2 ) is not empty then return G(q 1, q 2 ) 2 if q 1 = q 1 nd q 2 = q 2 then G(q 1, q 2 ) true 3 else if q 1 = q 1 nd q 2 q 2 then G(q 1, q 2 ) flse 4 else if q 1 q 1 nd q 2 = q 2 then G(q 1, q 2 ) flse 5 else / q 1 q 1 nd q 2 q 2 / 6 G(q 1, q 2 ) nd(eq(q 1 1, q 1 2 ),..., eq(q m 1, q m 2 )) 7 return G(q 1, q 2 ) Tle 7.4: Algorithm eq2 7.4 Determiniztion nd Minimiztion Let L e fixed-length lnguge, nd let A = (Q, Σ, δ, Q 0, F) e NFA recognizing L. The lgorithm det&min(a) shown in Tle 7.5 returns the stte of the mster utomton recognizing L. In

130 130 CHAPTER 7. FINITE UNIVERSES other words, det&min(a) simultneously determinizes nd minimizes A. The lgorithm ctully solves more generl prolem. Given set S of sttes of A, ll recognizing lnguges of the sme length, the lnguge L(S ) = q S L(q) hs lso this common length. The hert of the lgorithm is procedure stte(s ) tht returns the stte recognizing L(S ). Since L = L({q 0 }), det&min(a) just clls stte({q 0 }). We mke the ssumption tht for every stte q of A there is pth leding from q to some finl stte. This ssumption cn e enforced y suitle preprocessing, ut usully it is not necessry; in pplictions, NFAs for fixed-length lnguges usully stisfy the property y construction. With this ssumption, L(S ) stisfies: if S = then L(S ) = ; if S F then L(S ) = {ɛ} (since the sttes of S recognize fixed-length lnguges, the sttes of F necessrily recognize {ɛ}; since ll the sttes of S recognize lnguges of the sme length nd S F, we hve L(S ) = {ɛ}); if S nd S F =, then L(S ) = n i L(S i ), where S i = δ(s, i ). i=1 These properties led to the recursive lgorithm of Tle 7.5. The procedure stte(s ) uses tle G of results, initilly empty. When stte(s ) is computed for the first time, the result is memoized in G(S ), nd ny susequent cll directly reds the result from G. The lgorithm hs exponentil complexity, ecuse, in the worst cse, it my cll stte(s) for every set S Q. To show tht n exponentil lowup is unvoidle, consider the fmily {L n } n 0 of lnguges, where L n = {ww w, w {0, 1} n nd w w }. While L n cn e recognized e n NFAs of size O(n 2 ), its miniml DFA hs O(2 n ) sttes: for every u, v Σ n if u v then L u n L v, ecuse v L u n ut v L v n. Exmple 7.10 Figure 7.6 shows NFA (upper left) nd the result of pplying det&min to it. The run of det&min is shown t the ottom of the figure, where, for the ske of redility, sets of sttes re written without the usul prenthesis (e.g. β, γ insted of {β, γ}. Oserve, for instnce, tht the lgorithm ssigns to {γ} the sme node s to {β, γ}, ecuse oth hve the sttes 2 nd 3 s -successor nd -successor, respectively. 7.5 Opertions on Fixed-length Reltions Fixed-length reltions cn e mnipulted very similrly to fixed-length lnguges. Boolen opertions re s for fixed-length lnguges. The projection, join, pre, nd post opertions cn e however implemented more efficiently s in Chpter 6. We strt with n oservtion on encodings. In Chpter 6 we ssumed tht if n element of X is encoded y w Σ, then it is lso encoded y w#, where # is the pdding letter. This ensures tht every pir (x, y) X X hs n encoding (w x, w y ) such tht w x nd w y hve the sme length.

131 7.5. OPERATIONS ON FIXED-LENGTH RELATIONS 131 α 5,, β γ, 4 δ ɛ ζ 2 3, η θ 1 α 5 β, γ 4 γ 4 ɛ 2 δ, ɛ, ζ 3 ɛ 2 ɛ, ζ 3 0 η 1 η 1 η, θ 1 η 1 η, θ 1 Figure 7.6: Run of det&min on n NFA for fixed-length lnguge

132 132 CHAPTER 7. FINITE UNIVERSES det&min(a) Input: NFA A = (Q, Σ, δ, Q 0, F) Output: mster stte recognizing L(A) 1 return stte(q 0 ) stte(s ) Input: set S Q recognizing lnguges of the sme length Output: stte recognizing L(S ) 1 if G(S ) is not empty then return G(S ) 2 else if S = then return q 3 else if S F then return q ɛ 4 else / S nd S F = / 5 for ll i = 1,..., m do S i δ(s, i ) 6 G(S ) mke(stte(s 1 ),..., stte(s m )); 7 return G(S ) Tle 7.5: Algorithm det&min. Since in the fixed-length cse ll shortest encodings hve the sme length, the pdding symol is no longer necessry. So in this section we ssume tht ech word or pir hs exctly one encoding. The sic definitions on fixed-length lnguges extend esily to fixed-length reltions. A word reltion R Σ Σ hs length n 0 if for ll pirs (w 1, w 2 ) R the words w 1 nd w 2 hve length n. If R hs length n for some n 0, then we sy tht R hs fixed-length Recll tht trnsducer T ccepts pir (w 1, w 2 ) Σ Σ if w 1 = 1... n, w 2 = 1... n, nd T ccepts the word ( 1, 1 )... ( n, n ) Σ Σ. A fixed-length trnsducer ccepts reltion R X X if it recognizes the word reltion {(w x, w y ) (x, y) R}. Given lnguge R Σ Σ nd, Σ, we define R [,] = {(w 1, w 2 ) Σ Σ (w 1, w 2 ) R}. Notice tht in prticulr, [,] =, nd tht if R hs fixed-length, then so does R [,]. The mster trnsducer over the lphet Σ is the tuple MT = (Q M, Σ Σ, δ M, F M ), where Q M is is the set of ll fixed-length reltions, F M = {(ε, ε)}, nd δ M : Q M (Σ Σ) Q M is given y δ M (R, [, ]) = R [,] for every q Q M nd, Σ. As in the lnguge cse, the miniml deterministic trnsducer recognizing fixed-length reltion R is the frgment of the mster trnsducer contining the sttes rechle from R. Like miniml DFA, miniml deterministic trnsducers re represented s tles of nodes. However, remrk is in order: since stte of deterministic trnsducer hs Σ 2 successors, one for ech letter of Σ Σ, row of the tle hs Σ 2 entries, too lrge when the tle is only sprsely filled. Sprse trnsducers over Σ Σ re etter encoded s NFAs over Σ y introducing uxiliry sttes: trnsition q [,] q of the trnsducer is simulted y two trnsitions q r q, where r is n uxiliry stte with exctly one input nd one output trnsition.

133 7.5. OPERATIONS ON FIXED-LENGTH RELATIONS 133 Fixed-length projection The implementtion of the projection opertion of Chpter 6 my yield nondterministic trnsducer, even if the initil trnsducer is deterministic. So we need different implementtion. We oserve tht projection cn e reduced to pre or post: the projection of inry reltion R onto its first component is equl to pre R (Σ ), nd the projection onto the second component to post R (Σ ). So we defer deling with projection until the implementtion of pre nd post hve een discussed. Fixed-length join. We give recursive definition of R 1 R 2. Let [, ] R = {(w 1, w 2 ) (w 1, w 2 ) R}. We hve the following properties: R = R = ; { [ε, ε] } { [ε, ε] } = { [ε, ε] }; (R [,c] R 1 R 2 = [, ] 1 R [c,] ) 2 ;,,c Σ which led to the lgorithm of Figure 7.7, where union is defined similrly to inter. The complexity is exponentil in the worst cse: if t(n) denotes the worst-cse complexity for two sttes of length n, then we hve t(n) = O(t(n 1) 2 ), ecuse union hs qudrtic worst-cse complexity. This exponentil lowup is unvoidle. We prove it lter for the projection opertion (see Exmple 7.11), which is specil cse of pre nd post, which in turn cn e seen s vrints of join. join(r 1, r 2 ) Input: sttes r 1, r 2 of trnsducer tle Output: stte recognizing L(r 1 ) L(r 2 ) 1 if G(r 1, r 2 ) is not empty then return G(r 1, r 2 ) 2 if r 1 = q or r 2 = q then return q 3 else if r 1 = q ɛ nd r 2 = q ɛ then return q ɛ 4 else / q r 1 q ɛ nd q r 2 q ɛ / 5 for ll ( i, j ) Σ Σ do 6 r i, j union ( join ( r [ i, 1 ] 7 G(r 1, r 2 ) = mke(r 1,1,...,..., r m,m ) 8 return G(r 1, r 2 ) 1, r [ 1, j ]) ( 2,..., join r [ i, m ] 1, r [ m, j ] 2 )) Figure 7.7: Algorithm join Fixed-length pre nd post. Recll tht in the fixed-length cse we do not need ny pdding symol. Then, given fixed-length lnguge L nd reltion R, pre R (L) dmits n inductive definition tht we now derive. We hve the following properties:

134 134 CHAPTER 7. FINITE UNIVERSES if R = or L =, then pre R (L) = ; if R = { [ɛ, ɛ] } nd L = {ɛ}, then pre R (L) = {ɛ}; if R {[ɛ, ɛ]} nd L {ɛ}, then pre R (L) = where R [,] = {w (Σ Σ) [, ]w R}. pre R [,](L ), The first two properties re ovious. For the lst one, oserve tht ll pirs of R hve length t lest one, nd so every word of pre R (L) lso hs length t lest one. Now, given n ritrry word w 1 ΣΣ, we hve w 1 pre R (L) w 2 L: [w 1, w 2 ] R Σ w 2 L : [w 1, w 2 ] R [,] Σ: w 1 pre R [,](L ) w 1 pre R [,](L ) nd so pre R (L) = Σ, Σ pre R [,](L ) These properties led to the recursive lgorithm of Tle 7.5,, Σ which ccepts s inputs stte of the trnsducer tle for reltion R nd stte of the utomton tle for lnguge L, nd returns the stte of the utomton tle recognizing pre R (L). The trnsducer tle is not chnged y the lgorithm. pre(r, q) Input: stte r of trnsducer tle, stte q of utomton tle Output: stte recognizing pre L(r) (L(q)) 1 if G(r, q) is not empty then return G(r, q) 2 if r = r or q = q then return q 3 else if r = r ɛ nd q = q ɛ then return q ɛ 4 else 5 for ll i Σ do 6 q i union ( pre ( ( )) r [ i, 1 ], q 1),..., pre r [ i, m ], q m 7 G(q, r) mke(q 1,..., q m) 8 return G(q, r) Tle 7.6: Algorithm pre. As promised, we cn now give n implementtion of the opertion tht projects reltion R onto its first component. It suffices to give dedicted lgorithm for pre R (Σ ), shown in Tle 7.5. Algorithm pro 1 hs exponentil worst-cse complexity. As in the cse of join, the reson is the qudrtic lowup introduced y union when the recursion depth increses y one. The next exmple shows tht projection is inherently exponentil.

135 7.6. DECISION DIAGRAMS 135 pro 1 (r) Input: stte r of trnsducer tle Output: stte recognizing proj 1 (L(r)) 1 if G(r) is not empty then return G(r) 2 if r = r then return q 3 else if r = r ɛ then return q ɛ 4 else 5 for ll i Σ do 6 q i union ( pro 1 ( r [ i, 1 ] ),..., pro 1 ( r [ i, m ] )) 7 G(r) mke(q 1,..., q m) 8 return G(r) Tle 7.7: Algorithm pro 1. Exmple 7.11 Consider the reltion R Σ 2n Σ 2n given y R = {(w 1 xw 2 yw 3, 0 w 1 10 w 2 10 w 3 ) x y, w 2 = n nd w 1 w 3 = n 2}. Tht is, R contins ll pirs of words of length 2n whose first word hs position i n such tht the letters t positions i nd i + n re distinct, nd whose second word contins only 0 s ut for two 1 s t the sme two positions. It is esy to see tht the miniml deterministic trnsducer for R hs O(n 2 ) sttes (intuitively, it memorizes the letter x ove the first 1, reds n 1 letters of the form (z, 0), nd then reds (y, 1), where y x). On the other hnd, we hve pro 1 (R) = {ww w, w Σ n nd w w }, whose miniml DFA, s shown when discussing det&min, hs O(2 n ) sttes. So ny lgorithm for projection hs Ω(2 n ) complexity. Slight modifictions of this exmple show tht join, pre, nd post re inherently exponentil s well. 7.6 Decision Digrms Binry Decision Digrms, BDDs for short, re very populr dt structure for the representtion nd mnipultion of oolen functions. In this section we show tht they cn e seen s miniml utomt of certin kind. Given oolen function f (x 1,..., x n ) : {0, 1} n {0, 1}, let L f denote the set of strings n {0, 1} n such tht f ( 1,... n ) = 1. The miniml DFA recognizing L f is very similr to the BDD representing f, ut not completely equl. We modify the constructions of the lst section to otin n exct mtch. Consider the following miniml DFA for lnguge of length four:

136 136 CHAPTER 7. FINITE UNIVERSES q 1 q 3 q 5 q 0 q 7 q 2 q 4 q 6 Its lnguge cn e descried s follows: fter reding n, ccept ny word of length three; fter reding, ccept ny word of length 2; fter reding, ccept ny two-letter word whose lst letter is. Following this description, the lnguge cn lso e more compctly descried y n utomton with regulr expressions s trnsitions: r 0 Σ 3 Σ 2 r 1 r 2 Σ r 3 We cll such n utomton decision digrm (DD). The intuition ehind this nme is tht, if we view sttes s points t which decision is mde, nmely which should e the next stte, then sttes q 1, q 3, q 4, q 6 do not correspond to ny rel decision; whtever the next letter, the next stte is the sme. As we shll see, the sttes of miniml DD will lwys correspond to rel decisions. Section shows tht the miniml DD for fixed-length lnguge is unique, nd cn e otined y repetedly pplying to the miniml DFA the following reduction rule: q 1 l 1 1 q r q 1 l 1 Σ r q n l 2 m q n l 2 Σ The converse direction lso works: the miniml DFA cn e recovered from the miniml DD y reversing the rule. This lredy llows us to use DDs s dt structure for fixed-length lnguges, ut only through conversion to miniml DFAs: to compute n opertion using miniml DDs, expnd them to miniml DFAs, conduct the opertion, nd convert the result ck. Section shows how to do etter y directly defining the opertions on miniml DDs, ypssing the miniml DFAs.

137 7.6. DECISION DIAGRAMS Decision Digrms nd Kernels A decision digrm (DD) is n utomton A (Q, Σ, δ, Q 0, F) whose trnsitions re lelled y regulr expressions of the form Σ n = ΣΣΣ... ΣΣ } {{ } n nd stisfies the following determincy condition: for every q Q nd Σ there is exctly one k N such tht δ(q, Σ k ). It is esy to see tht fixed-length lnguge is recognized y DD if nd only it is kernel: Definition 7.12 A fixed-lnguge L is kernel if it is empty or if there re, Σ such tht L L. The kernel of fixed-lnguge L, denoted y L, is the unique kernel stisfying L = Σ k L for some k 0. The mster decision digrm (we cll it just the mster ) hs the set of ll kernels s sttes, the kernel {ɛ} s unique finl stte, nd trnsition (K, Σ k, K ) for every kernel K nd Σ, where k is equl to the length of K minus the length of K. Exmple 7.13 Figure 7.8 shows frgment of the mster (compre with Figure 7.1). The lnguges {, }, {,,, }, nd {, } of Figure 7.1 re not kernels, nd so they re not sttes of the mster either. The DD A K for kernel K is the frgment of the mster contining the sttes rechle from K. It is esy to see tht A K recognizes K. A DD is miniml if no other DD for the sme lnguge hs fewer sttes. The following proposition shows tht miniml DDs hve very similr properties to miniml DFAs: Proposition 7.14 (1) A DD A tht recognizes kernel is miniml if nd only if (i) every stte of A recognizes kernel, nd (ii) distinct sttes of A recognize distinct kernels. (2) A K is the unique miniml DD recognizing K. (3) The result of exhustively pplying the reduction rule to the miniml DFA recognizing fixed-length lnguge L is the miniml DD recognizing L. Proof: (1 ): For (i), ssume A contins stte q such tht L(q) is not kernel. Since L(A) is kernel, q is neither initil nor finl. Let k e the smllest numer such tht A contins trnsition (q, Σ k, q ) for some letter nd some stte q. Then L(q) = Σ k L(q ), nd, since L(q) is not kernel, L(q) = L(q) for every Σ. So we hve L(q) = Σ Σ k L(q ) = Σ k+1 L(q ). Now we perform the following two opertions: first, we replce every trnsition (q, Σ l, q) of A y trnsition (q, Σ l+k+1, q ); then, we remove q nd ny other stte no longer rechle form the initil stte (recll tht q is neither initil nor finl). The resulting DD recognizes the sme lnguge s A, nd hs t lest one stte less. So A is not miniml.

138 138 CHAPTER 7. FINITE UNIVERSES {,,,,,, } {,,,, } Σ 2 Σ {,, } {,, } Σ Σ {} {} {ɛ},, Figure 7.8: A frgment of the mster decision digrm For (ii), oserve tht the quotienting opertion cn e defined for DDs s for DFAs, llowing us to merge sttes tht recognize the sme kernel without chning the lnguge. If two distinct sttes of A recognize the sme kernel then the quotient hs fewer sttes thn A, nd so A is not miniml. (1 ): We show tht two DDs A nd A tht stisfy (i) nd (ii) nd recognize the sme lnguge re isomorphic, which, together with (1 ), proves tht they re miniml. It suffices to prove tht if two sttes q, q of A nd A stisfy L(q) = L(q ), then for every Σ the (unique) trnsitions (q, Σ k, r) nd (q, Σ k, r ) stisfy k = k nd L(r) = L(r ). Let L(q) = K = L(q ). By (1 ), oth L(r) nd L(r ) re kernels. But then we necessrily hve L(r) = K = L(q ), ecuse the only solution to the eqution K = Σ l K, where l nd K re unknowns nd K must e kernel, is K = ( K ). (2) A K recognizes K nd it stisfies conditions () nd () of prt (1) y definition. So it is miniml DD. Uniqueness follows from the proof of (1 ). (3) Let B e DD otined y exhustively pplying the reduction rule to A. By (1), it suffices to prove tht B stisfies (i) nd (ii). For (ii) oserve tht, since every stte of A recognizes different lnguge, so does every stte of B (the reduction rule preserves the recognized lnguges). For (i), ssume tht some stte q does not recognize kernel. Without loss of generlity, we cn choose L(q) of miniml length, nd therefore the trget sttes of ll outgoing trnsitions of q recognize kernels. It follows tht ll of them necessrily recognize L(q). Since B contins t most one stte recognizing L(q), ll outgoing trnsitions of q hve the sme trget, nd so the reduction rule cn

139 7.6. DECISION DIAGRAMS 139 L 2 6 L 1 L 3 2 Σ 4 1 Figure 7.9: The multi-zdfa for {L 1, L 2, L 3 } with L 1 = {}, L 2 = {, }, nd L 3 = {, }. e pplied to q, contrdicting the hypothesis tht it hs een pplied exhustively Opertions on Kernels We use multi-dds to represent sets of fixed-length lnguges of the sme length. A set L = {L 1,..., L m } is represented y the sttes of the mster recognizing L 1,..., L m nd y the common length of L 1,..., L m. Oserve tht the sttes nd the length completely determine L. Exmple 7.15 Figure 7.9 shows the multi-dd for the set {L 1, L 2, L 3 } of Exmple 7.7. Recll tht L 1 = {, }, L 2 = {,, }, nd L 3 = {, }. The multi-dd is the result of pplying the reduction rule to the multi-utomton of Figure 7.2. We represent the set y the multi-dd nd the numer 2, the length of L 1, L 2, L 3. Oserve tht, while L 1, L 2 nd L 3 hve the sme length, L 2 hs different length thn L 1 nd L 3. Multi-DDs re represented s tle of kernodes. A kernode is triple q, l, s, where q is stte identifier, l is length, nd s = (q 1,..., q m ) is the successor tuple of the kernode. The tle for the multi-dd of Figure 7.9 is: Ident. Length -succ -succ This exmple explins the role of the new length field. If we only now tht the - nd -successors of, sy, stte 6 re sttes 2 nd 1, we cnnot infer which expressions lel the trnsitions from 6 to 2 nd from 6 to 1: they could e nd Σ, or Σ nd Σ 2, or Σ n nd Σ n+1 for ny n 0. However, once we know tht stte 6 ccepts lnguge of length 2, we cn deduce the correct lels: since sttes 2 nd 1 ccept lnguges of length 1 nd 0, respectively, the lels re nd Σ.

140 140 CHAPTER 7. FINITE UNIVERSES The procedure kmke(l, s). All lgorithms cll procedure kmke(l, s) with the following specifiction. Let K i e the kernel recognized y the i-th component of s. Then kmke(l, s) returns the kernode for L, where L is the unique lnguge of length l such tht L i = K i for every i Σ. If K i K j for some i, j, then kmke(l, s) ehves like mke(s): if the current tle lredy contins kernode q, l, s, then kmke(l, s) returns q; nd, if no such kernode exists, then kmke(l, s) cretes new kernode q, l, s with fresh identifier q, nd returns q. If K 1,..., K m re ll equl to some kernel K, then we hve L = m i=1 i Σ k K for some k, nd therefore L = Σ l+1 K = K. So kmke(l, t) returns the kernode for K. For instnce, if T is the tle ove, then kmke(3, (2, 2)) returns 3, while mke(2, 2) cretes new node hving 2 s -successor nd -successor. Algorithms. The lgorithms for opertions on kernels re modifictions of the lgorithms of the previous section. We show how to modify the lgorithms for intersection, complement, nd for simultneous determiniztion nd minimiztion. In the previous section, the stte of the mster utomton for lnguge L ws the lnguge L itself, nd ws otined y recursively computing the sttes for L 1,..., L m nd then pplying mke. Now, the stte of the mster DD for L is L, nd cn e otined y recursively computing sttes for L 1,..., L m nd pplying kmke. Fixed-length intersection. Given kernels K 1, K 2 of lnguges L 1, L 2, we compute the stte recognizing K 1 K 2 = L 1 L 2. 2 We hve the ovious def property if K 1 = or K 2 =, then K 1 K 2 =. Assume now K 1 K 2. If the lengths of K 1 nd K 2 re l 1, l 2, then since Σ k L = L holds for every k, L we hve Σ l 2 l 1 K 1 K 2 if l 1 < l 2 K 1 K 2 = K 1 Σ l 1 l 2 K 2 if l 1 > l 2 K 1 K 2 if l 1 = l 2 which llows us to otin the stte for K 1 K 2 y computing sttes for (Σ l 1 l 2 K 1 K 2 ), (K 1 Σ l 2 l 1 K 2 ) or (K 1 K 2 ) for every Σ, nd pplying kmke. These sttes cn e computed recursively y mens of: if l 1 < l 2 then (Σ l 2 l 1 K 1 K 2 ) = Σ l 2 l 1 1 K 1 K 2 = K 1 K 2 ; if l 1 > l 2 then (K 1 Σ l 1 l 2 K 2 ) = K 1 Σl 1 l 2 1 K 2 = K 1 K 2 ; if l 1 = l 2 then (K 1 K 2 ) = K 1 K 2 = K 1 K 2 ; which leds to the lgorithm of Tle is well defined ecuse L 1 = L 1 nd L 2 = L 2 implies L 1 L 2 = L 1 L 2.

141 7.6. DECISION DIAGRAMS 141 kinter(q 1, q 2 ) Input: sttes q 1, q 2 recognizing L 1, L 2 Output: stte recognizing L 1 L 2 1 if G(q 1, q 2 ) is not empty then return G(q 1, q 2 ) 2 if q 1 = q or q 2 = q then return q 3 if q 1 q nd q 2 q then 4 if l 1 < l 2 /* lengths of the kernodes for q 1, q 2 */ then 5 for ll i = 1,..., m do r i kinter(q 1, q i 2 ) 6 G(q 1, q 2 ) kmke(l 2, r 1,..., r m ) 7 else if l 1 l 2 then 8 for ll i = 1,..., m do r i kinter(q i 1, q 2) 9 G(q 1, q 2 ) kmke(l 1, r 1,..., r m ) 10 else /* l 1 = l 2 */ 11 for ll i = 1,..., m do r i kinter(q i 12 G(q 1, q 2 ) kmke(l 1, r 1,..., r m ) 13 return G(q 1, q 2 ) 1, q i 2 ) Tle 7.8: Algorithm kinter Exmple 7.16 Exmple 7.8 shows run of inter on the two lnguges represented y the multi- DFA t the top of Figure 7.4. The multi-dd for the sme lnguges is shown t the top of Figure 7.10, nd the rest of the figure descries the run of kinter on it. Recll tht pink nodes correspond to clls whose result hs lredy een memoized, nd need not e executed. The mening of the green nodes is explined elow. The lgorithm cn e improved y oserving tht two further properties hold: if K 1 = {ε} then L 1 L 2 = L 1, nd so K 1 K 2 = K 1, nd if K 2 = {ε} then L 1 L 2 = L 2, nd so K 1 K 2 = K 2. These properties imply tht kinter(q ε, q) = q = kinter(q, q ε ) for every stte q. So we cn improve kinter y explicitly checking if one of the rguments is q ε. The green nodes in Figure 7.10 correspond to clls whose result is immeditely returned with the help of this check. Oserve how this improvement hs sustntil effect, reducing the numer of clls from 19 to only 5. Fixed-length complement. Given the kernel K of fixed-lnguge L of length n, we wish to compute the mster stte recognizing L n, where n is the length of L. The suscript n is only necessry ecuse hs ll possile lengths, nd so n = Σ n Σ m = L m for n m. Now we hve n = {ɛ} for every n 0, nd so the suscript is not needed nymore. We define the opertor

142 142 CHAPTER 7. FINITE UNIVERSES Σ Σ 2 10 Σ Σ , , 1 8 9, , 1 5 0, 1 0 5, 1 5 1, 6 6 2, 1 2 3, 1 3 1, 2 2 1, 1 1 1, 1 1 0, 1 0 0, 1 0 1, 1 1 1, 1 1 0, 1 0 0, 0 0 0, 0 0 Figure 7.10: An execution of kinter.

143 7.6. DECISION DIAGRAMS 143 on kernels y K = L. 3 We otin the stte for K y recursively computing sttes for K y mens of the properties if K = then K = {ɛ}, nd if K = {ɛ}, then K = ; if K {ɛ} then K = K ; which led to the lgorithm of Tle 7.9. kcomp(q) Input: stte q recognizing kernel K Output: stte recognizing K 1 if G(q) is not empty then return G(q) 2 if q = q then return q ɛ 3 else if q = q ɛ then return q 4 else 5 for ll i = 1,..., m do r i kcomp(q i ) 6 G(q) kmke(r 1,..., r m ) 7 return G(q) Tle 7.9: Algorithm kcomp Determiniztion nd Minimiztion. The lgorithm kdet&min tht converts NFA recognizing fixed-lnguge L into the miniml DD recognizing L differs from det&min essentilly in one letter: it uses kmke insted of mke. It is shown in Tle Exmple 7.17 Figure 7.11 shows gin the NFA of Figure 7.6, nd the miniml DD for the kernel of its lnguge. The run of kdet&min(a) is shown t the ottom of the figure. For the difference with det&min(a), consider the cll kstte({δ, ɛ, ζ}). Since the two recursive clls kstte({η}) nd kstte({η, θ}) return oth stte 1 with length 1, kmke(1, 1) does not crete new stte, s mke(1, 1) would do it returns stte 1. The sme occurs t the top cll kstte({α}). Exercises Exercise 71 Prove tht the miniml DFA for lnguge of length 4 over two-letter tlphet hs t most 12 sttes, nd give lnguge for which the miniml DFA hs exctly 12 sttes. 3 The opertor is well defined ecuse L = L implies L = L.

144 144 CHAPTER 7. FINITE UNIVERSES α, β γ, 3 δ ɛ ζ 2 Σ η θ 1 α 3 β, γ 3 γ 3 ɛ 2 δ, ɛ, ζ 1 ɛ 2 ɛ, ζ 1 0 η 1 η 1 η, θ 1 η 1 η, θ 1 Figure 7.11:

145 7.6. DECISION DIAGRAMS 145 kdet&min(a) Input: NFA A = (Q, Σ, δ, Q 0, F) Output: stte of multi-dfa recognizing L(A) 1 return stte[a](q 0 ) kstte(s ) Input: set S of sttes of length l Output: stte recognizing L(R) 1 if G(S ) is not empty then return G(S ) 2 else if S = then return q 3 else if S F then return q ɛ 4 else / S nd S F = / 5 for ll i = 1,..., m do S i δ(s, i ) 6 G(S ) kmke(l, kstte(s 1 ),..., kstte(s m )); 7 return G(S ) Tle 7.10: The lgorithm kdet&min(a). Exercise 72 Give n efficient lgorithm tht receives s input the miniml DFA of fixed-length lnguge nd returns the numer of words it contins. Exercise 73 Let Σ = {0, 1}. Consider the oolen function f : Σ 6 Σ defined y f (x 1, x 2,..., x 6 ) = (x 1 x 2 ) (x 3 x 4 ) (x 5 x 6 ) () Construct the miniml DFA recognizing {x 1 x 6 Σ 6 f (x 1,..., x 6 ) = 1}. (For instnce, the DFA ccepts ecuse f (1, 1, 1, 0, 0, 0) = 1, ut not , ecuse f (1, 0, 1, 0, 1, 0) = 0.) () Show tht the miniml DFA recognizing {x 1 x 3 x 5 x 2 x 4 x 6 f (x 1,... x 6 ) = 1} hs t lest 15 sttes. (Notice the different order! Now the DFA ccepts neither , ecuse f (1, 0, 1, 0, 1, 0) = 0, nor , ecuse f (1, 0, 0, 1, 1, 0) = 0.) (c) More generlly, consider the function f (x 1,..., x 2n ) = (x 2k 1 x 2k ) 1 k n nd the lnguges {x 1 x 2... x 2n 1 x 2n f (x 1,..., x 2n ) = 1} nd {x 1 x 3... x 2n 1 x 2 x 4... x 2n f (x 1,..., x 2n ) = 1}. Show tht the size of the miniml DFA grows linerly in n for the first lnguge, nd exponentilly in n for the second lnguge.

146 146 CHAPTER 7. FINITE UNIVERSES Exercise 74 Given oolen formul over vriles x 1,..., x n, we define the lnguge of φ, denoted y L(φ), s follows: L(φ) = { 1 2 n the ssignment x 1 1,..., x n n stisfies φ} Give polynomil lgorithm tht tkes DFA A recognizing lnguge of length n s input, nd returns oolen formul φ such tht L(φ) = L(A). Give n exponentil lgorithm tht tkes oolen formul φ s input, nd returns DFA A recognizing L(φ). Exercise 75 Recll the definition of lnguge of oolen formul over vriles x 1,..., x n given in Exercise 74. Prove tht the following prolem is NP-hrd: Given: A oolen formul φ in conjunctive norml form, numer k 1. Decide: Does the miniml DFA for L(φ) hve t most 1 stte? Exercise 76 Given X {0, 1,..., 2 k 1}, where k 1, let A X e the miniml DFA recognizing the LSBF-encodings of length k of the elements of X. (1) Define X +1 y X +1 = {x+1 mod 2 k x X}. Give n lgorithm tht on input A X produces A X+1 s output. (2) Let A X = (Q, {0, 1}, δ, q 0, F). Which is the set of numers recognized y the utomton A = (Q, {0, 1}, δ, q 0, F), where δ (q, ) = δ(q, 1 )? Exercise 77 Recll the definition of DFAs with negtive trnsitions (DFA-nt s) introduced in Exercise 29, nd consider the lphet {0, 1}. Show tht if only trnsitions lelled y 1 cn e negtive, then every regulr lnguge over {0, 1} hs unique miniml DFA-nt. Exercise 78 Define em(l) = {[v 1, v 2 ] (Σ Σ) n v 2 L} nd define pre S (L), where S (Σ Σ) nd L Σ, s follows: pre S (L) = {w 1 Σ n [v 1, v 2 ] S : v 1 = w 1 nd v 2 L} Show tht ( pre S (L) ) = pre S [, ] (L ). Σ Exercise 79 Exhiit fmily of reltions R n {0, 1} n {0, 1} n such tht the miniml deterministic trnsducer for R n hs O(n) sttes, nd the miniml DFA for proj 1 (R n ) hs 2 O(n) sttes.

147 Chpter 8 Applictions II: Verifiction One of the min pplictions of utomt theory is the utomtic verifiction or flsifiction of correctness properties of hrdwre or softwre systems. Given system (like hrdwre circuit, progrm, or communiction protocol), nd property (like fter termintion the vlues of the vriles x nd y re equl or every sent messge is eventully received ), we wish to utomticlly determine whether the system stisfies the property or not. 8.1 The Automt-Theoretic Approch to Verifiction We consider discrete systems for which notion of configurtion cn e defined 1. The system is lwys t certin configurtion, with instntneous moves from one configurtion to the next determined y the system dynmics. If the semntics llows move from configurtion c to nother one c, then we sy tht c is legl successor of c. A configurtion my hve severl successors, in which cse the system is nondeterministic. There is distinguished set of initil configurtions. An execution is sequence of configurtions (finite or infinite) strting t some initil configurtion, nd in which every other configurtion is legl successor of its predecessor in the sequence. A full execution is either n infinite execution, or n execution whose lst configurtion hs no successors. In this chpter we re only interested in finite executions. The set of executions cn then e seen s lnguge E C, where the lphet C is the set of possile configurtions of the system. We cll C the potentil executions of the system. Exmple 8.1 As n exmple of system, consider the following progrm with two oolen vriles x, y: 1 We spek of the configurtions of system, nd not of its sttes, in order to void confusion with the sttes of utomt. 147

148 148 CHAPTER 8. APPLICATIONS II: VERIFICATION 1 while x = 1 do 2 if y = 1 then 3 x 0 4 y 1 x 5 end A configurtion of the progrm is triple [l, n x, n y ], where l {1, 2, 3, 4, 5} is the current vlue of the progrm counter, nd n x, n y {0, 1} re the current vlues of x nd y. So the set C of configurtions contins in this cse = 20 elements. The initil configurtions re [1, 0,, [1, 0,, [1, 1,, [1, 1,, i.e., ll configurtions in which control is t line 1. The sequence is full execution, while [1, 1, [2, 1, [3, 1, [4, 0, [1, 0, [5, 0, [1, 1, [2, 1, [4, 1, [1, 1, is lso n execution, ut not full one. In fct, ll the words of ( [1, 1, [2, 1, [4, 1, ) re executions, nd so the lnguge E of ll executions is infinite. Assume we wish to determine whether the system hs n execution stisfying some property of interest. If oth the lnguge E C of executions nd the lnguge P C of potentil executions tht stisfy the property re regulr, nd we cn construct utomt recognizing them, then we cn solve the prolem y checking whether the lnguge E P is empty, which cn e decided using the lgorithms of Chpter 4. This is the min insight ehind the utomt-theoretic pproch to verifiction. The requirement tht the lnguge E of executions is regulr is stisfied y ll systems with finitely mny rechle configurtions (i.e., finitely mny configurtions c such tht some execution leds from some initil configurtion to c). A system utomton recognizing the executions of the system cn e esily otined from the configurtion grph: the grph hving the rechle configurtions s nodes, nd rcs from ech configurtion to its successors. There re two possile constructions, oth very simple. In the first construction, the sttes re the rechle configurtions of the progrm plus new stte i, which is lso the initil stte. All sttes re finl. For every trnsition c c of the grph there is trnsition c c c in the system utomton. Moreover, there is trnsition c i c for every initil configurtion. It is esy to see tht this construction produces miniml deterministic utomton. Since the lel of trnsition is lso its trget stte, for ny two trnsitions c c c 1 nd c c c 2 we necesrily hve c 1 = c = c 2, nd so the utomton is deterministic. To show tht it is miniml, oserve tht ll words ccepted from stte c strt with c, nd so the lnguges ccepted y different sttes re lso different (in fct, they re even disjoint).

149 8.2. PROGRAMS AS NETWORKS OF AUTOMATA 149 In the second construction, the sttes re the rechle configurtions of the progrm plus new stte f. The initil sttes re ll the initil configurtions, nd ll sttes re finl. For every trnsition c c of the grph there is trnsition c c c in the system utomton. Moreover, there is trnsition c c f for every configurtion c hving no successor. Exmple 8.2 Figure 8.1 shows the configurtion grph of the progrm of Exmple 8.1, nd the system utomt produced y the two constructions ove. We wish to lgorithmiclly decide if the system hs full execution such tht initilly y = 1, finlly y = 0, nd y never increses. Let [l, x,, [l, x, stnd for the sets of configurtions where y = 0 nd y = 1, respectively, ut the vlues of l nd x re ritrry. Similrly, let [5, x, stnd for the set of configurtions with l = 5 nd y = 0, ut x ritrry. The set of potentil executions stisfying the property is given y the regulr expression [l, x, [l, x, [l, x, [5, x, which is recognized y the property utomton t the top of Figure 8.2. Its intersection with the system utomton in the middle of Figure 8.1 (we could lso use the one t the ottom) is shown t the ottom of Figure 8.2. A light pink stte of the piring leled y [l, x, y] is the result of piring the light pink stte of the property NFA nd the stte [l, x, y] of the system DFA. Since lels of the trnsitions of the piring re lwys equl to the trget stte, they re omitted for the ske of redility. Since no stte of the intersection hs drk pink color, the intersection is empty, nd so the progrm hs no execution stisfying the property. Exmple 8.3 We wish now to utomticlly determine whether the ssignment y 1 x in line 4 of the progrm of Exmple 8.1 is redundnt nd cn e sfely removed. This is the cse if the ssignment never chnges the vlue of y. The potentil executions of the progrm in which the ssignment chnges the vlue of y t some point correspond to the regulr expression [l, x, y] ( [4, x, [1, x, + [4, x, [1, x, ) [l, x, y]. A property utomton for this expression cn e esily constructed, nd its intersection with the system utomton is gin empty. So the property holds, nd the ssignment is indeed redundnt. 8.2 Progrms s Networks of Automt We cn lso model the progrm of Exmple 8.1 s network of communicting utomt. The key ide is to model the two vriles x nd y nd the control flow of the progrm s three independent processes. The processes for x nd y mintin their current vlue, nd control flow process mintins the current vlue of the progrm counter. The execution of, sy, the ssignment x 0 in line 3 of the progrm is modeled s the execution of joint ction etween the control flow process

150 150 CHAPTER 8. APPLICATIONS II: VERIFICATION 1, 0, 0 5, 0, 0 1, 1, 0 2, 1, 0 4, 1, 0 1, 0, 1 5, 0, 1 1, 1, 1 2, 1, 1 3, 1, 1 4, 0, 1 [5, 0, 1, 0, 0 5, 0, 0 i [1, 0, [1, 1, [2, 1, [4, 1, 1, 1, 0 2, 1, 0 [1, 1, [1, 0, [1, 1, 1, 0, 1 [5, 0, 5, 0, 1 [1, 0, 4, 1, 0 1, 1, 1 2, 1, 1 [2, 1, [3, 1, 3, 1, 1 [4, 0, 4, 0, 1 [1, 0, 1, 0, 0 5, 0, 0 [5, 0, [1, 1, 1, 1, 0 2, 1, 0 [2, 1, 4, 1, 0 i [4, 1, [1, 0, 1, 0, 1 5, 0, 1 [4, 0, [5, 0, 1, 1, 1 2, 1, 1 [1, 1, [2, 1, 3, 1, 1 4, 0, 1 [3, 1, Figure 8.1: Configurtion grph nd system utomt of the progrm of Exmple 8.1

151 8.2. PROGRAMS AS NETWORKS OF AUTOMATA 151 [l, x, [l, x, [l, x, [5, x, 1, 0, 1 5, 0, 1 1, 0, 1 5, 0, 1 i 1, 1, 1 2, 1, 1 3, 1, 1 4, 0, 1 1, 1, 1 2, 1, 1 3, 1, 1 4, 0, 1 Figure 8.2: Property utomton nd product utomton nd the process for vrile x: the control flow process updtes the current control position to 4, nd simultneously the process for x updtes the current vlue of x to 0. The processes for vriles nd control-flow re represented y finite utomt where ll sttes re finl. The three utomt for the progrm of Exmple 8.1 re shown in Figure 8.3. Since ll sttes re finl, we do not use the grphicl representtion with doule circle. The utomt for x nd y hve two sttes, one for ech for possile vlue.the control-flow utomton hs 5 sttes, one for ech control loction. The lphet of the utomt for x nd y correspond to the ssignments or oolen conditions of the progrm tht involve x or y, respectively. However, one single ssignment my produce severl lphet letters. For instnce, the ssignment y 1 x t line 4 produces two lphet letters, corresponding to two possile ctions: if the utomton for x is currently t stte 0 (tht is, if x currently hs vlue 0), then the utomton for y must move to stte 1, otherwise to stte 0. (The sme occurs with the ssignment x 1 x.) We denote these two lphet letters s x = 0 y 1 nd x = 1 y 0. Oserve lso tht the execution of y 1 x is modeled s joint ction of ll three utomt: intuitively, the ction x = 0 y 1 cn e jointly executed only if the utomton for x is currently t stte 0 nd the control-flow utomton is currently t stte 4. We now give forml definition of network of utomt. In the definition we do not require ll sttes to e finl, ecuse, s we shll see lter, more generl definition proves to e useful. Definition 8.4 A network of utomt is tuple A = A 1,..., A n of NFAs, where A i = (Q i, Σ i, δ i, Q 0i, F i ) nd the sets Q 1,..., Q n re pirwise disjoint. A letter of Σ = Σ 1 Σ n is clled n ction. A configurtion of A is tuple [q 1,..., q n ] of sttes, where q i Q i for every i {1,..., n}. A configurtion is initil if q i Q 0i for every i {1,..., n}, nd finl if q i F i for every i {1,..., n} Oserve tht ech NFA of network hs its own lphet Σ i. The lphets Σ 1,..., Σ n re not

152 152 CHAPTER 8. APPLICATIONS II: VERIFICATION x 0, x 1 x = 1 x = 1 y 0, y 1 x = 0 y 1, y = 1 x x = 0 y 1 x = 1 y y = 1 x = 1 2 y 1 x 0 1 x = 1 y 0, 4 x = 0 y 1 x 1 5 Figure 8.3: A network of three utomt modeling the progrm of Exmple 8.1. necessrily pirwise disjoint, in fct usully they re not. We define when is n ction enled t configurtion, nd wht hppens when it occurs. Definition 8.5 Let A = A 1,..., A n e network of utomt, where A i = (Q i, Σ i, δ i, Q 0i, F i ). Given n ction, we sy tht A i prticiptes in if Σ i. An ction is enled t configurtion [q 1,..., q n ] if for every i {1,..., n} such tht A i prticiptes in there is trnsition (q i,, q i ) δ i. If is enled, then it cn occur, nd its occurrence leds to the successor configurtion [q 1,..., q n ] defined y We denote this y [q 1,..., q n ] q i = q i q i [q 1,..., q n ]. if A i prticiptes in otherwise The notion of lnguge ccepted y network of utomt is defined in the stndrd wy: 0 1 Definition 8.6 A run of A on input n 1 is sequence c 0 c 1 c 2... c n such tht c i is configurtion for every 0 i n, the configurtion c 0 is initil, nd δ(q i, i ) = q i+1 for n 1

153 8.2. PROGRAMS AS NETWORKS OF AUTOMATA 153 every 0 i n 1. A run is ccepting if c n is finl configurtion. A ccepts w Σ if it hs n ccepting run on input w. The lnguge recognized y A, denoted y L(A) is the set of words of ccepted y A. Exmple 8.7 Let A x, A y, nd A P e the three utomt of Exmple 8.1 for the vriles x nd y nd the control, respectively. We hve Σ x = {x = 1, x 1, x 0, (x = 0 y 1), (x = 1 y 0) } Σ y = {y = 1, y 1, (x = 0 y 1), (x = 1 y 0) } Σ P = Σ x Σ y The utomt prticipting in, sy, the ction x = 0 re A P nd A x, nd ll three utomt prticipte in (x = 1 y 0). Oserve tht A P prticiptes in ll ctions. If we define A = A P, A x, A y, then the configurtions of A re the configurtions of the progrm of Exmple 8.1. The configurtion [3, 1, enles the ction x 0, nd we hve [3, 1, [4, 0,. One of the runs of A is [1, 1, x=1 [2, 1, y=1 x 0 x 0 [3, 1, [1, 0, nd so the word (x = 1) (y = 1) (x 0) (x 1) elongs to L(A). x 1 [5, 0, Prllel Composition The lnguge of network of utomt dmits useful chrcteriztion. Given lnguges L 1 Σ 1,..., L n Σ n, the prllel composition of L 1,..., L n is the lnguge L 1 L 2 L N (Σ 1 Σ n ) defined s follows: w L 1 L n iff proj Σi (w) L i for every 1 i n. Notice tht, strictly speking, prllel composition is n opertion tht depends not only on thelnguges L 1,..., L n, ut lso on their lphets. Tke for exmple L 1 = {} nd L 2 = {}. If we look t them s lnguges over the lphet {, }, then L 1 L 2 = ; if we look t L 1 s lnguge over {}, nd L 2 s lnguge over {, }, then L 1 L 2 = {}. So the correct nottion would e L 1 Σ1,Σ 2 L 2, ut we use lnguge, nd ssume tht when lnguge is defined we specify its lphet. Proposition 8.8 (1) Prllel composition is ssocitive, commuttive, nd idempotent. Tht is: (L 1 L 2 ) L 3 = L 1 (L 2 L 3 ) (ssocitivity); L 1 L 2 = L 2 L 1 (commuttivity), nd L L = L (idempotence). (2) If L 1, L 2 Σ, then L 1 L 2 = L 1 L 2. (3) (1) Let A = A 1,..., A n e network of utomt. Then L(A)) = L(A 1 ) L(A n ). Proof: See Exercise 82.

154 154 CHAPTER 8. APPLICATIONS II: VERIFICATION Comining (2) nd (3) we otin tht for two utomt over the sme lphet we hve L(A 1 A 2 ) = L(A 1 ) L(A 2 ). Intuitively, in this cse every step must e jointly executed y A 1 nd A 2, or, in other words, the mchines move in lockstep. At the other extreme, if the input lphets re pirwise disjoint, then, intuitively, the utomt do not communicte t ll, nd move independently of ech other Asynchonous Product Given network of utomt A = A 1,... A n, we cn compute NFA recognizing the sme lnguge. We cll it the synchronous product of A, nd denote ity A 1 A n. The NFA is computed y lgorithm AsyncProduct in Tle 8.1. The lgorithm follows esily from Definitions 8.5 nd 8.6. Strting t the initil configurtions, the lgorithm repetedly picks configurtion from the workset, stores it, constructs its successors, nd dds them (if not yet stored) to the workset. Line 10 is the most importnt one. Assume we re in the midlle of the execution of AsyncProduct(A 1, A 2 ), currently processing configurtion [q 1, q 2 ] nd n ction t line 8. Assume tht elongs to Σ 1 Σ 2, nd the -trnsitions leving q 1 nd q 2 re q 1 q 1, q 1 q 1 nd q 2 q 2, q 1 q 2. Then we otin Q 1 = {q 1, q 1 } nd Q 2 = {q 2, q 2 }, nd the loop t lines dds four trnsitions: [q 1, q 2 ] [q 1, q 2 ], [q 1, q 2 ] [q 1, q 2 ], [q 1, q 2 ] [q 1, q 2 ], [q 1, q 2 ] which correspond to the four possile joint -moves tht A 1 nd A 2 cn execute from [q 1, q 2 ]. Assume now tht only elongs to Σ 1, the -trnsitions leving q 1 re s efore, nd, since Σ 2, there re no -trnsitions leving q 2. Then Q 1 = {q 1, q 1 }, Q 2 = {q 2}, nd the loop dds trnsitions [q 1, q 2 ] [q 1, q 2], [q 1, q 2 ] [q 1, q 2], which correspond to A 1 mking move while A 2 stys put. Assume finlly tht elongs to Σ 1 Σ 2, the -trnsitions leving q 1 re s efore, nd there re no -trnsitions leving q 2 (which is possile even if Σ 2, ecuse A 2 is NFA). Then Q 1 = {q 1, q 1 }, Q 2 =, nd the loop dds no trnsitions. This corresponds to the fct tht, since -moves must e jointly executed y A 1 nd A 2, nd A 2 is not currently le to do ny -move, no joint -move cn hppen. [q 1, q 2 Exmple 8.9 The NFA AsyncProduct(A P, A x, A y ) is shown in Figure 8.4. Its sttes re the rechle configurtions of the progrm. Agin, since ll sttes re finl, we drw ll sttes s simple cycles. Finlly, oserve tht we hve defined the synchronous product of A 1 A n s n utomton over the lphet Σ = Σ 1 Σ n, ut the lgorithm cn e esily modified to return system utomton hving the set of configurtions s lphet (see Exercise 81).

155 8.2. PROGRAMS AS NETWORKS OF AUTOMATA 155 AsyncProduct(A 1,..., A n ) Input: network of utomt A = A 1,..., A n, where A 1 = (Q 1, Σ 1, δ 1, Q 01, Q 1 ),..., A n = (Q n, Σ n, δ n, Q 0n, Q n ) Output: NFA A 1 A n = (Q, Σ, δ, Q 0, F) recognizing L(A). 1 Q, δ, F 2 Q 0 Q 01 Q 0n 3 W Q 0 4 while W do 5 pick [q 1,..., q n ] from W 6 dd [q 1,..., q n ] to Q 7 dd [q 1,..., q n ] to F 8 for ll Σ 1... Σ n do 9 for ll i [1..n] do 10 if Σ i then Q i δ i(q i, ) else Q i = {q i} 11 for ll [q 1,..., q n] Q 1... Q n do 12 if [q 1,..., q n] Q then dd [q 1,..., q n] to W 13 dd ([q 1,..., q n ],, [q 1,..., q n]) to δ 14 return (Q, Σ, δ, Q 0, F) Tle 8.1: Asynchronous product of network of utomt Stte- nd event-sed properties. Properties of the sequence of configurtions visited y the progrm re often clled stte-sed properties (progrm configurtions re often clled progrm sttes, we use progrm configurtion ecuse of the possile confusion etween progrm sttes nd utomton sttes). If we we wish to check such properties, we construct system utomton s shown in Exercise 81. Properties of the sequence of instructions executed y the progrm cn e directly checked using the synchronous product. For instnce, consider the property: no terminting execution of the progrm contins n occurrence of the ction (x = 0 y 1). The property cn e reformulted s: no execution of progrm elongs to the regulr lnguge Σ P (x = 0 y 1) Σ P (x 1) for which we cn esily find property utomton A ϕ. We cn check the property y checking emptiness of n utomton for the intersection of AsyncProduct(A P, A x, A y ) nd A ϕ. This verifiction style is often clled event-sed verifiction (Occurrences of progrm ctions re often clled events; n execution my contin mny events corresponding to the sme ction).

156 156 CHAPTER 8. APPLICATIONS II: VERIFICATION x 1 1, 0, 0 5, 0, 0 x = 1 y 1 1, 1, 0 2, 1, 0 4, 1, 0 x = 1 y 0 x 1 1, 0, 1 5, 0, 1 x = 0 y 1 1, 1, 1 2, 1, 1 x = 1 y = 1 3, 1, 1 x 0 4, 0, 1 Figure 8.4: Asynchronous product of the utomt of Figure Concurrent Progrms Networks of utomt cn lso elegntly model concurrent progrms, tht is, progrms consisting of numer of sequentil progrms communicting in some wy. These sequentil progrms re often clled processes. A populr communiction mechnism etween processes re shred vriles, where process cn communicte with nother y writing vlue to vrile, which is then red y the other process. As n exmple, we consider the Lmport-Burns mutul exclusion lgorithm for two processes 2. It hs the following code. repet nc 0 : 0 1 t 0 : while 1 = 1 do skip c 0 : 0 0 forever repet nc 1 : 1 1 t 1 : if 0 = 1 then q 1 : 1 0 q 1 : while 0 = 1 do skip goto nc 1 c 1 : 1 0 forever In the lgorithm, process 0 nd process 1 communicte through two shred oolen vriles, 0 nd 1, which initilly hve the vlue 0. Process i reds nd writes vrile i nd reds vrile (1 i). The lgorithm should gurntee tht the processes 0 nd 1 never re simultneously t control points c 0 nd c 1 (their criticl sections), nd tht the two processes will not rech dedlock. Other properties the lgorithm should stisfy re discussed lter. Initilly, process 0 is in its noncriticl section (locl stte nc 0 ); it cn lso e trying to enter its criticl section (t 0 ), or e lredy in its criticl section (c 0 ). It cn move from nc 0 to t 0 t ny time y setting 0 to 1; it cn move 2 L. Lmport: The mutul exclusion prolem: prt II-sttements nd solutions. JACM 1986

157 8.3. CONCURRENT PROGRAMS 157 from t 0 to c 0 if the current vlue of 1 is 0; finlly, it cn move from c 0 to nc 0 t ny time y setting 0 to 0. Process 1 is it more complicted. While nc 1, t 1, nd c 1 ply the sme rôle s in process 0, the locl sttes q 1 nd q 1 model polite ehvior: Intuitively, if process 1 sees tht process 0 is either trying to enter or in the criticl section, it moves to n fter you locl stte q 1, nd then sets 1 to 0 to signl tht it is no longer trying to enter its criticl section (locl stte q 1 ). It cn then return to the non-criticl section if the vlue of 0 is 0. A configurtion of this progrm is tuple [n 0, n 1, l 0, l 1 ], where n 0, n 1 {0, 1}, l 0 {nc 0, t 0, c 0 }, nd l 1 {nc 1, t 1, q 1, q 1, c 1}. We define executions of the progrm y interleving. We ssume tht, if t the current configurtion oth processes cn do n ction, then the ctions will not hppen t the sme time, one of the two will tke plce efore the other. However, the ctions cn occur in ny order. So, loosely speking, if two processes cn execute two sequences of ctions independently of ech other (ecuse, sy, they involve disjoint sets of vriles), then the sequences of ctions of the two processes running in prllel re the interleving of the sequences of the processes. For exmple, t the initil configurtion [0, 0, nc 0, nc 1 ] oth processes cn set their vriles to 1. So we ssume tht there re two trnsitions [0, 0, nc 0, nc 1 ] [1, 0, t 0, nc 1 ] nd [0, 0, nc 0, nc 1 ] [0, 1, nc 0, t 1 ]. Since the other process cn still set its vrile, we lso hve trnsitions [1, 0, t 0, nc 1 ] [1, 1, t 0, t 1 ] nd [1, 0, t 0, nc 1 ] [1, 1, t 0, t 1 ] In order to model shred-vrile progrm s network of utomt we just model ech process nd ech vrile y n utomton. The network of utomt modelling the Lmport- Burns lgorithm is shown in Figure 8.5, nd its synchronous product in Figure Expressing nd Checking Properties We use the Lmport-Burns lgorithm to present some more exmples of properties nd how to check them utomticlly. The mutul exclusion property cn e esily formlized: it holds if the synchronous product does not contin ny configurtion of the form [v 0, v 1, c 0, c 1 ], where v 0, v 1 {0, 1}. The property cn e esily checked on-the-fly while constructing the synchronous product, nd quick inspection of Figure 8.6 shows tht it holds. Notice tht in this cse we do not need to construct the NFA for the executions of the progrm. This is lwys the cse if we only wish to check the rechility of configurtion or set of configurtions. Other properties of interest for the lgorithm re: Dedlock freedom. The lgorithm is dedlock-free if every configurtion of the synchronous product hs t lest one successor. Agin, the property cn e checked on the fly, nd it holds for Lmport s lgorithm. Bounded overtking. After process 0 signls its interest in ccessing the criticl section (y moving to stte t 0 ), process 1 cn enter the criticl section t most once efore process 0 enters the criticl section. This property cn e checked using the NFA E recognizing the executions of the network, otined s explined ove y renming the lels of the trnsitions of the synchronous

158 158 CHAPTER 8. APPLICATIONS II: VERIFICATION 0 0, 0 = 0 0 1, 0 = 1 1 0, 1 = 0 1 1, 1 = i i 0 1 = = 0 nc 0 t 0 c = 1 q q 1 0 = 0 0 = = 0 nc 1 t 1 c Figure 8.5: A network of four utomt modeling mutex lgorithm. product. Let NC i, T i, C i e the sets of configurtions in which process i is in its non-criticl section, is trying to ccess its criticl section, or is in its criticl section, respectively. Let Σ stnd for the set of ll configurtions. The regulr expression r = Σ T 0 (Σ \ C 0 ) C 1 (Σ \ C 0 ) NC 1 (Σ \ C 0 ) C 1 Σ represents ll the possile executions tht violte the property. 8.4 Coping with the Stte-Explosion Prolem The key prolem of this pproch is tht the numer of sttes of E cn e s high s the product of the numer of sttes of the the components A 1,..., A n, which cn esily exceed the ville memory. This is clled the stte-explosion prolem, nd the literture contins welth of proposls to del with it. We conclude the section with first esy step towrds plliting this prolem. A more in depth discussion cn e found in the next section.

159 8.4. COPING WITH THE STATE-EXPLOSION PROBLEM = 0 0 = 0 1 = 1 0, 0, nc 0, q , 0, nc 0, nc 1 1, 0, c 0, nc 1 1 = = 1 1, 0, t 0, nc 1 1, 1, c 0, t = 1 0 = 1 1, 1, t 0, t 1 1, 1, c 0, q , 0, c 0, q = , 1, nc 0, q 1 1 = 0 1 = 1 1, 1, t 0, q , 0, t 0, q 1 1 = , 1, nc 0, t , 1, t 0, c = 1 1 = 1 0, 1, nc 0, c 1 1 = 1 Figure 8.6: Asynchronous product of the network of Figure 8.5. The utomt-theoretic pproch constructs n NFA V recognizing the potentil executions of the system tht violte the property one is interested in, nd checks whether the utomton E V is empty, where E is n NFA recognizing the executions of the system. This is done y constructing the set of sttes of E V, while simultneously checking if ny of them is finl. The numer of sttes of E cn e very high. If we model E s network of utomt, the numer cn e s high s the product of the numer of sttes of ll the components of the network. So the pproch hs exponentil worst-cse complexity. The following result shows tht this cnnot e voided unless P=PSPACE. Theorem 8.10 The following prolem is PSPACE-complete. Given: A network of utomt A 1,..., A n over lphets Σ 1,..., Σ n, NFA V over Σ 1... Σ n. Decide: if L(A 1 A n V). Proof: We only give high-level sketch of the proof. To prove tht the prolem is in PSPACE, we show tht it elongs to NPSPACE nd pply Svitch s theorem. The polynomil-spce nonde-

160 160 CHAPTER 8. APPLICATIONS II: VERIFICATION terministic lgorithm just guesses n execution of the product, one configurtion t time, leding to finl configurtion. Notice tht storing configurtion requires liner spce. PSPACE-hrdness is proven y reduction from the cceptnce prolem for linerly ounded utomt. A linerly ounded utomton (LBA) is deterministic Turing mchine tht lwys hlts nd only uses the prt of the tpe contining the input. Given n LBA A, we construct in liner time network of utomt tht simultes A. The network hs one component modeling the control of A (notice tht the control is essentilly DFA), nd one component for ech tpe cell used y the input. The sttes of the control component re pirs (q, k), where q is control stte of A, nd k is hed position. The sttes of cell-component re the possile tpe symols. The trnsitions correspond to the possile moves of A ccording to its trnsition tle. Acceptnce of A corresponds to rechility of certin configurtions in the network, which cn e esily encoded s n emptiness prolem On-the-fly verifiction. Recll tht, given progrm with set E of executions, nd given regulr expression descriing the set of potentil executions V violting property, we hve the following four-step technique to check if the progrm stisfis the property, i.e., to check if E V = : (1) trnsform the regulr expression into n NFA A V (V for violtions ) using the lgorithm of Section 2.4.1; (2) model the progrm s network of utomt A 1,..., A n, nd construct the NFA A E = AsyncProd(A 1,..., A n ) recognizing E; (3) construct n NFA A E A V for E V using lgorithm intersnfa; (4) check emptiness of A E A V using lgorithm empty. Oserve tht A E my hve more sttes thn A E A V : if stte of A E is not rechle y ny word of V, then the stte does not pper in A E A V. The difference in size etween the NFAs cn e considerle, nd so it is etter to directly construct A E A V, ypssing the construction of A E. Further, it is inefficient to first construct A E A V nd then check for emptiness. It is etter to check for emptines while constructing A E A V, interrupting the lgorithm the moment it constructs finl stte. So, loosely speking, we look for n lgorithm tht constructs the intersection with A V nd checks for emptiness on the fly, while computing A E. This is esily chieved y mens of the oservtion ove: the intersection A 1 A 2 of two NFAs A 1, A 2 corresponds to the prticulr cse of the synchronous product in which Σ 1 Σ 2 (or vice vers): if Σ 1 Σ 2, then A 2 prticiptes in every ction, nd the NFAs A 1 A 2 nd A 1 A 2 coincide. More generlly, if Σ 1 Σ n Σ n+1, then L(A 1 A n+1 ) = L(A 1 A n )) L(A n+1 ). So, if the lphet of V is the union of the lphets of A 1,..., A n, we hve L(A) L(V) = L(A 1 A n A v ), nd we cn check emptiness y mens of the lgorithm CheckViol shown in Tle 8.2. Looking t A V s just nother component of the synchronous product, s in Algorithm Check- Viol, lso hs nother smll dvntge. Consider gin the lnguge V Σ P (x = 0 y 1) Σ P (x 1) Actully, we re only interested in the suset of ctions Σ = {(x = 0 y 1), (x 1)}. So we cn replce A V y n utomton A V over Σ recognizing only the sequence (x = 0 y 1) (x 1).

161 8.4. COPING WITH THE STATE-EXPLOSION PROBLEM 161 CheckViol(A 1,..., A n, V) Input: network A = A 1,... A n, where A i = (Q i, Σ i, δ i, Q 0i, Q i ); n NFA V = (Q V, Σ V, δ V, Q 0v, F v ). Output: true if L(A 1 A n V) is nonempty, flse otherwise. 1 Q ; Q 0 Q 01 Q 0n Q 0v 2 W Q 0 3 while W do 4 pick [q 1,..., q n, q] from W 5 dd [q 1,..., q n, q] to Q 6 for ll Σ 1... Σ n do 7 for ll i [1..n] do 8 if Σ i then Q i δ i(q i, ) else Q i = {q i} 9 Q δ V (q, ) 10 for ll [q 1,..., q n, q ] Q 1... Q n Q do 11 if n i=1 q i F i nd q F v then return true 12 if [q 1,..., q n, q ] Q then dd [q 1,..., q n, q ] to W 13 return flse Tle 8.2: Algorithm to check violtion of property. Tht is, this utomton prticiptes in ll occurrences of these ctions, ignoring the rest. Intuitively, we cn think of A V s n oserver of the network A 1 A n tht is only interested in oserving the ctions of Σ Compositionl Verifiction Consider the synchronous product A 1 A 2 of two NFAs over lphets Σ 1, Σ 2. Intuitively, A 2 does not oserve the ctions of Σ 1 \ Σ 2 : they re internl ctions of A 1. Therefore, A 1 cn e replced y ny other utomton A 1 stisfying L(A 1 ) = proj Σ 2 (L(A 1 )) without Σ 2 noticing, mening tht the sequences of ctions tht A 2 cn execute with A 1 nd A 1 s prtners re the sme, or, formlly, proj Σ2 (A 1 A 2 ) = proj Σ2 (A 1 A 2). In prticulr, we then hve L(A 1 A 2 ) if nd only if L(A 1 A 2), nd so checking emptiness of A 1 A 2 cn e replced y checking emptiness of A 1 A 2. It is esy to construct n utomton recognizing proj Σ2 (L(A 1 )): just replce ll trnsitions of A 1 leled with letters of Σ 1 \ Σ 2 y ε-trnsitions. This utomton hs the sme size s A 1, nd so susituting it for A 1 hs no immedite dvntge. However, fter removing the ɛ-trnsitions, nd reducing the resulting NFA, we my otin n utomton A 1 smller thn A 1.

162 162 CHAPTER 8. APPLICATIONS II: VERIFICATION This ide cn e extended to the prolem of checking emptiness of product A 1 A n with n ritrry numer of components. Exploting the ssocitivity of, we rewrite the product s A 1 (A 2 A n ), nd replce A 1 y hopefully smller utomton A 1 over the lphet Σ 2 Σ n. In second step we rewrite A 1 A 2 A 3 A n s (A 1 A 2) (A 3 A n ), nd, pplying gin the sme procedure, replce A 1 A 2 y new utomton A 2 over the lphet Σ 3 Σ n. The procedure continues until we re left with one single utomton A n over Σ n, whose emptiness cn e checked directly on-the-fly. To see this ide in ction, consider the network of utomt in The upper prt of Figure 8.9. It models 3-it counter consisting of n rry of three 1-it counters, where ech counter communictes with its neighours. We cll the components of the network A 0, A 1, A 2 insted of A 1, A 2, A 3 to etter reflect their mening: A i stnds for the i-th it. Ech NFA ut the lst one hs three sttes, two of which re mrked with 0 nd 1. The lphets re Σ 0 = {inc, inc 1, 0,..., 7} Σ 1 = {inc 1, inc 2, 0,..., 7} Σ 2 = {inc 2, 0,..., 7} Intuitively, the system intercts with its environment y mens of the visile ctions Vis = inc, 0, 1,..., 7. More precisely, inc models request of the environment to increse the counter y 1, nd i {0, 1,..., 7} models query of the environment sking if i is the current vlue of the counter. A configurtion of the form [ 2, 1, 0 ], where 2, 1, 0 {0, 1}, indictes tht the current vlue of the counter is (configurtions re represented s triples of sttes of A 2, A 1, A 0, in tht order). For exmple, here is run of the network, strting n ending t configurtion [0, 0,. [0, 0, inc [0, 0, inc [0, 0, ux 0 ] inc [0, 1, inc [0, 1, ux 0 ] inc [1, 0, inc [1, 0, ux 0 ] inc [1, 1, inc [1, 1, ux 0 ] inc 1 [0, 1, inc 1 [0, ux 1, inc 1 [1, 1, inc 1 [1, ux 1, inc 2 [1, 0, inc 2 [0, 0,... The ottom prt of Figure?? shows the synchronous product of the network. (All sttes re finl, ut we hve drwn them s simple insted of doule ellipses for simplicity. The product hs 18 sttes. Assume we wish to check some property whose violtions re given y the lnguge of n utomton A V over the lphet Vis of visile ctions. The specific shpe of A V is not relevnt for the discussion ove. Importnt is only tht, insted of checking emptiness of A 2 A 1 A 0 A V, we cn lso construct n utomton A 0 such tht L(A 0 ) = proj Vis(L(A 2 A 1 A 0 )), nd check emptiness

163 8.4. COPING WITH THE STATE-EXPLOSION PROBLEM 163 A 2 A 1 A 0 4, 5, 6, 7 2, 3, 6, 7 1, 3, 5, inc 1 inc inc 2 inc 2 inc 1 inc 0 0 inc 2 0 inc 1 0, 1, 2, 3 0, 1, 4, 5 0, 2, 4, 6 0 0, 0, 0 inc 2 1, ux, 0 inc inc 1 0, 0, 1 inc 2 1, ux, 1 inc inc 1, ux, ux inc 2 2 0, 1, 0 inc 1 0, 0, ux inc 3 0, 1, 1 inc 0, 1, ux inc 1 4 1, 0, 0 inc 2 0, ux, 0 inc 1 inc inc 5 1, 0, 1 inc 2 0, ux, 1 inc inc 0, ux, ux inc 2 6 1, 1, 0 inc 1 1, 0, ux 7 inc 1, 1, 1 inc 1, 1, ux Figure 8.7: A network modeling 3-it counter nd its synchronous product.

164 164 CHAPTER 8. APPLICATIONS II: VERIFICATION of A 0 A V. If we compute A 0 e first computing the synchronous product A 2 A 1 A 0, nd then removing invisile ctions nd reducing, then the mximum size of ll intermedite utomt involved is t lest 18. Let us now pply the procedure ove, strting with A 2. Since Σ 2 (Σ 1 Σ 0 ), A 2 cnnot execute ny moves hidden from A 1 nd A 0, nd so A 2 = A 2. In the next step we compute the product A 2 A 1 shown in Figure??. Now inc 2 is n internl ction of A 1 A 2, ecuse it elongs neither to the lphet of A 0 nor to the lphet of A V. The result of eliminting ε-trnsitions nd reducing is shown on the right hlf of the figure. 0, 1 0, , 0 inc 1 inc 1 2, 3 0, 1 2, 3 0, 1 inc 1 inc 2 inc 1 inc 1 4, 5 1, 0 inc 2 0, ux 4, 5 1, 0 inc 1 inc 1 6, 7 1, 1 inc 1 0, 1, ux 6, 7 1, 1 Figure 8.8: The synchronous product A 2 A 1, nd the reduced utomton A 1. Now we construct A 1 A 0, with inc 1 s new internl ction, not present in the lphet of A V, nd reduce the utomton gin. The results re shown in Figure??. The importnt fct is tht we hve never hd to construct n utomton with more thn 12 sttes, sving of 50% with respect to the mthod tht directly computes A 2 A 1 A 0. While sving six sttes is of course irrelevnt, in lrger exmples the svings cn e significnt Symolic Stte-spce Explortion Figure 8.10 shows gin the progrm of Exmple 8.1, nd its flowgrph. An edge of the flowgrph leding from node l to node l cn e ssocited step reltion S l,l contining ll pirs of configurtions ([l, x 0, y 0 ], [l, x 0, y 0 ]) such tht if t control point l the current vlues of the vriles re x 0, y 0, then the progrm cn tke step fter which the new control point is l, nd the new vlues re x 0, y 0. For instnce, for the edge leding from node 4 to node 1 we hve S 4,1 = { ( [4, x 0, y 0 ], [1, x 0, y 0 ] ) x 0 = x 0, y 0 = 1 x } 0 nd for the edge leding from 1 to 2

165 8.4. COPING WITH THE STATE-EXPLOSION PROBLEM , 0, 0 0 0, 0, 0 inc inc 1 0, 0, 1 inc 1 0, 0, 1 inc 2 0, 1, 0 inc 1 0, 0, ux 2 0, 1, 0 inc inc 3 0, 1, 1 inc 0, 1, ux 3 0, 1, 1 inc 1 inc 1 inc inc 4 1, 0, 0 4 1, 0, 0 inc inc 5 1, 0, 1 inc 5 1, 0, 1 inc 6 1, 1, 0 inc 1 1, 0, ux 6 1, 1, 0 7 inc 1, 1, 1 inc 1, 1, ux 7 inc 1, 1, 1 Figure 8.9: The synchronous product A 1 A 0, nd the reduced utomton A 0.

166 166 CHAPTER 8. APPLICATIONS II: VERIFICATION 1 1 while x = 1 do 2 if y = 1 then 3 x 0 4 y 1 x 5 end y = 1 x = 1 2 y 1 x 1 y 1 x x 0 Figure 8.10: Flowgrph of the progrm of Exmple 8.1 S 1,2 = { ( [1, x 0, y 0 ], [2, x 0, y 0 ] ) x 0 = 1 = x 0, y 0 = y } 0 It will e convenient to ssign reltion to every pir of nodes of the control grph, even to those not connected y ny edge. If no edge leds from to, then we define R, =. The complete progrm is then descried y the glol step reltion S =, C where C is the set of control points. Given set I of initil configurtions, the set of configurtions rechle from I cn e computed y the following lgorithm, which repetedly pplies the Post opertion: S, Rech(I, R) Input: set I of initil configurtions; reltion R Output: set of configurtions rechle form I 1 OldP ; P I 2 while P OldP do 3 OldP P 4 P Union(P, Post(P, S )) 5 return P The lgorithm cn e implemented using different dt structures. The verifiction community distinguishes etween explicit nd symolic dt structures. Explicit dt structures store seprtely ech of the configurtions of P, nd the pirs of configurtions of S ; typicl exmples re lists nd hsh tles. Their distinctive feture is tht the memory needed to store set is proportionl to the numer of its elements. Symolic dt structures, on the contrry, do not store set y storing ech of its elements; they store representtion of the set itself. A prominent exmple of symolic dt structure re finite utomt nd trnsducers: given n encoding of configurtions s words over

167 8.4. COPING WITH THE STATE-EXPLOSION PROBLEM 167 some lphet Σ, the set P nd the step reltion S re represented y n utomton nd trnsducer, respectively, recognizing the encodings of its elements. Their sizes cn e much smller thn the sizes of P or S. For instnce, if P is the set of ll possile configurtions then its encoding is usully Σ, which is encoded y very smll utomton. Symolic dt structures re only useful if ll the opertions required y the lgorithm cn e implemented without hving to switch to n explicit dt structure. This is the cse of utomt nd trnsducers: Union, Post, nd the equlity check in the condition of the while loop opertion re implemented y the lgorithms of Chpters 4 nd 6, or, if they re fixed-length, y the lgorithms of Chpter 7. Symolic dt structures re interesting when the set of rechle configurtions cn e very lrge, or even infinite. When the set is smll, the overhed of symolic dt structures usully offsets the dvntge of compct representtion. Despite this, nd in order to illustrte the method, we pply it to the five-line progrm of Figure The fixed-length trnsducer for the step reltion S is shown in Figure 8.11; configurtion [l, [ x 0, y 0 ] is encoded y the word lx 0 y 0 of length 3. 4 Consider for instnce the trnsition leled y. Using it the trnsducer cn recognize four pirs, [ 1 [ 1 2] [ 0 [ 1 5] [ 3 4] [ [ 0 1, 0 ] [ ] 0 0 [ 1, [ 0 [ [ 0 1, [ 2 3] [ 4 [ 1 [ [ 0 1, [ 2 4] [ [ 0 1, [ 1 [ 0 [ [ 0 1, Figure 8.11: Trnsducer for the progrm of Figure 8.10 which descrie the ction of the instruction y 1 x, nmely [ ] [ ] [ ] [ ]

168 168 CHAPTER 8. APPLICATIONS II: VERIFICATION Figure 8.12 shows miniml DFAs for the set I nd for the sets otined fter ech itertion of the while loop. Vrile orders. We hve defined configurtion of the progrm of Exmple 8.1 s triple [l, n x, n y ], nd we hve encoded it s the word l n x n y. We could hve lso encoded it s the word n x l n y, n y l n x, or s ny other permuttion, since in ll cses the informtion content is the sme. Of course, when encoding set of configurtions ll the configurtions must e encoded using the sme vrile order. While the informtion content is independent of the vrile order, the size of the utomton encoding set is not. An extreme cse is given y the following exmple. Exmple 8.11 Consider the set of tuples X = {[x 1, x 2,..., x 2k ] x 1,..., x 2k {0, 1}}, nd the suset Y X of tuples stisfying x 1 = x k, x 2 = x k+1,..., x k = x 2k. Consider two possile encodings of tuple [x 1, x 2,..., x 2k ]: y the word x 1 x 2... x 2k, nd y the word x 1 x k+1 x 2 x k+2... x k x 2k. In the first cse, the encoding of Y for k = 3 is the lnguge L 1 = {000000, , , , , , , } nd in the second the lnguge L 2 = {000000, , , , , , , } Figure 8.13 shows the miniml DFAs for the lnguges L 1 nd L 2. It is esy to see tht the miniml DFA for L 1 hs t lest 2 k sttes: since for every word w {0, 1} k the residul L1 w is equl to {w}, the lnguge L 1 hs different residul for ech word of length k, nd so the miniml DFA hs t lest 2 k sttes (the exct numer is 2 k k 2). On the other hnd, it is esy to see tht the miniml DFA for L 2 hs only 3k + 1 sttes. So good vrile order cn led to exponentilly more compct representtion. We cn lso pprecite the effect of the vrile order in Lmport s lgorithm. The set of rechle configurtions, sorted ccording to the stte of the first process nd then to the stte of the second process, is nc 0, nc 1, 0, 0 t 0, nc 1, 1, 0 c 0, nc 1, 1, 0 nc 0, t 1, 0, 1 t 0, t 1, 1, 1 c 0, t 1, 1, 1 nc 0, c 1, 0, 1 t 0, c 1, 1, 1 nc 0, q 1, 0, 1 t 0, q 1, 1, 1 c 0, q 1, 1, 1 nc 0, q 1, 0, 0 t 0, q 1, 1, 0 c 0, q 1, 1, 0 If we encode tuple s 0, s 1, v 0, v 1 y the word v 0 s 0 s 1 v 1, the set of rechle configurtions is recognized y the miniml DFA on the left of Figure However, if we encode y the word v 1 s 1 s 0 v 0 we get the miniml DFA on the right. The sme exmple cn e used to visulize how y

169 8.5. SAFETY AND LIVENESS PROPERTIES 169 dding configurtions to set the size of its miniml DFA cn decrese. If we dd the missing configurtion c 0, c 1, 1, 1 to the set of rechle configurtions (filling the hole in the list ove), two sttes of the DFAs of Figure 8.14 cn e merged, yielding the miniml DFAs of Figure Oserve lso tht the set of ll configurtions, rechle or not, contins 120 elements, ut is recognized y five-stte DFA. 8.5 Sfety nd Liveness Properties Aprt from the stte-explosion prolem, the utomt-theoretic pproch to utomtic verifiction s descried in this chpter hs second limittion: it ssumes tht the violtions of the property cn e witnessed y finite executions. In other words, if n execution violtes the property, then we cn detect the violtion is lredy violted fter finite time. Not ll properties stisfy this ssumption. A typicl exmple is the property if process requests ccess to the criticl section, it eventully enters the criticl section (without specifying how long it my tke). After finite time we cn only tell tht the process hs not entered the criticl section yet, ut we cnnot sy tht the property hs een violted: the process might still enter the criticl section in the future. A violtion of the property cn only e witnessed y n infinite execution, in which we oserve tht the process requests ccess, ut the ccess is never grnted. Properties which re violted y finite executions re clled sfety properties. Intuitively, they correspond to properties of the form nothing d ever hppens. Typicl exmples re the system never dedlocks, or, more generlly, the system never enters set of d sttes. Clerly, every interesting system must lso stisfy properties of the form something good eventully hppens, ecuse otherwise the system tht does nothing would lredy stisfy ll properties. Properties of this kind re clled liveness properties, nd cn only e witnessed y infinite executions. Fortuntely, the utomt-theoretic pproch cn e extended to liveness properties. This requires to develop theory of utomt on infinite words, which is the suject of the second prt of this ook. The ppliction of this theory to the verifiction of liveness properties is presented in Chpter 14. As n ppetizer, the exercises strt to discuss them. Exercises Exercise 80 Exhiit fmily {P n } n 1 of sequentil progrms (like the one of Exmple 8.1) stisfying the following conditions: P n hs O(n) vriles, ll of them oolen, O(n) lines, nd exctly one initil configurtion. P i hs t lest 2 i rechle configurtions. Exercise 81 Modify AsyncProduct so tht it produces system utomt s those shown in Figure 8.1 for the progrm of Exmple 8.1.

170 170 CHAPTER 8. APPLICATIONS II: VERIFICATION Exercise 82 Prove: (1) Prllel composition is ssocitive, commuttive, nd idempotent. Tht is: (L 1 L 2 ) L 3 = L 1 (L 2 L 3 ) (ssocitivity); L 1 L 2 = L 2 L 1 (commuttivity), nd L L = L (idempotence). (2) If L 1, L 2 Σ, then L 1 L 2 = L 1 L 2. (3) Let A = A 1,..., A n e network of utomt. Then L(A)) = L(A 1 ) L(A 2 ). Exercise 83 Let Σ = {request, nswer, working, idle}. (1) Build regulr expression nd n utomton recognizing ll words with the property P 1 : for every occurrence of request there is lter occurrence of nswer. (2) P 1 does not imply tht every occurrence of request hs its own nswer: for instnce, the sequence request request nswer stisfies P 1, ut oth requests must necessrily e mpped to the sme nswer. But, if words were infinite nd there were infinitely mny requests, would P 1 gurntee tht every request hs its own nswer? More precisely, let w = w 1 w 2 stisfying P 1 nd contining infinitely mny occurrences of request, nd define f : N N such tht w f (i) is the ith request in w. Is there lwys n injective function g : N N stisfying w g(i) = nswer nd f (i) < g(i) for ll i {1,..., k}? (3) Build n utomton recognizing ll words with the property P 2 : there is n occurrence of nswer efore which only working nd request occur. (4) Using utomt theoretic constructions, prove tht ll words ccepted y the utomton A elow stisfy P 1, nd give regulr expression for ll words ccepted y the utomton tht violte P 2. Σ q nswer 0 q 1 Exercise 84 This exercise focuses on modelling nd verifiction of mutul exclusion (mutex) lgorithms. Consider two processes running the following mutex lgorithm, where id is n identifier, locl vrile hving vlue 0 for one of the processes nd vlue 1 for the other. while true do loop-ritrrily-mny-times non-criticl-commnd enter(id) criticl-commnd leve(id)

171 8.5. SAFETY AND LIVENESS PROPERTIES 171 The procedures enter() nd leve() re specified elow. They use glol vrile turn, initilly set to 0. proc enter(i) while turn = 1 i do skip proc leve(i) turn 1 i Design n synchronous network of utomt cpturing this lgorithm. Furthermore, uild n utomton recognizing ll runs reching configurtion with oth gents in the criticl section. Using the intersection lgorithm, prove tht there re no such runs of this system, i.e. it is mutex lgorithm. Do ll infinite runs stisfy tht if process wnts to enter the criticl section then it eventully enters it? Consider now different definition of enter nd leve. Now the processes use two ooeln vriles flg[ nd flg[ initilly set to flse. proc enter(i) flg[i] true while flg[1 i] do skip proc leve(i) flg[i] flse Design n synchronous network of utomt cpturing this ehviour. Cn dedlock occur? Finlly, Peterson s lgorithm comines oth pproches. The processes use vriles turn, initilly set to 0, nd flg[, flg[, initilly set to flse. The procedures enter nd leve re defined s follows: proc enter(i) turn 1 i flg[i] true while flg[1 i] nd turn = 1 i do skip proc leve(i) flg[i] flse Cn dedlock occur? Wht kind of strvtion cn occur? Exercise 85 Consider circulr rilwy divided into 8 trcks: In the rilwy circulte three trins, modeled y three utomt T 1, T 2, nd T 3. Ech utomton T i hs sttes {q i,0,..., q i,7 }, lphet {enter[i, j] 0 j 7} (where enter[i, j] models tht trin i enters trck j), trnsition reltion {(q i, j, enter[i, j, q i, j 1 ) 0 j 7}, nd initil stte q i,2i, where denotes ddition modulo 8. In other words, initilly the trins occupy the trcks 2, 4, nd 6. Define utomt C 0,..., C 7 (the locl controllers) to mke sure tht two trins cn never e on the sme or djcent trcks (i.e., there must lwys e t lest one empty trck etween two trins). Ech controler C j cn only hve knowledge of the stte of the trcks j 1, j, nd j 1, there must e no dedlocks, nd every trin must eventully visit every trck. More formlly, the network of utomt A = C 0,..., C 7, T 1, T 2, T 3 must stisfy the following specifiction: For j = 0,..., 7: C j hs lphet {enter[i, j, enter[i, j], enter[i, j, 1 i 3}. (C j only knows the stte of trcks j 1, j, nd j 1.)

172 172 CHAPTER 8. APPLICATIONS II: VERIFICATION For i = 1, 2, 3: L(A) Σi = ( enter[i, 2i] enter[i, 2i... enter[i, 2i 7] ). (No dedlocks, nd every trin eventully visits every segment.) For every word w L(A): if w = w 1 enter[i, j] enter[i, j ] w 2 nd i i, then j j {0, 1, 7}. (No two trins on the sme or djcent trcks.)

173 8.5. SAFETY AND LIVENESS PROPERTIES , 1 0, , 1 0, , , , 1 0 0, Figure 8.12: Miniml DFAs for the rechle configurtions of the progrm of Figure 8.10

174 174 CHAPTER 8. APPLICATIONS II: VERIFICATION Figure 8.13: Miniml DFAs for the lnguges L 1 nd L 2

175 8.5. SAFETY AND LIVENESS PROPERTIES 175 nc 0 c 0 t nc 1, q 1 t 1, c 1, q 1 nc 1, q 1 t 1, c 1, q 1 t 1, q 1 t 1, q 1 nc 1, q 1 c 1 nc 1, q 1 t 0, c nc 0 nc 0 t Figure 8.14: Miniml DFAs for the rechle configurtions of Lmport s lgorithm. On the left configurtion s 0, s 1, v 0, v 1, q is encoded y the word s 0 s 1 v 0 v 1 q, on the right y v 1 s 1 s 0 v 0. nc 0 t 0, c t 1, c 1, q 1 nc 1, q nc 1, q 1 1 t 1, c 1, q 1 nc 1, q 1 t 1, c 1, q nc 0 t 0, c Figure 8.15: c 0, c 1, 1, 1. Miniml DFAs for the rechle configurtions of Lmport s lgorithm plus

176 176 CHAPTER 8. APPLICATIONS II: VERIFICATION

177 Chpter 9 Automt nd Logic A regulr expression cn e seen s set of instructions ( recipe ) for generting the words of lnguge. For instnce, the expression ( + ) cn e interpreted s write two s, repetedly write or n ritrry numer of times, nd then write. We sy tht regulr expressions re n opertionl description lnguge. Lnguges cn lso e descried in declrtive style, s the set of words tht stisfy property. For instnce, the words over {, } contining n even numer of s nd n even numer of s is declrtive description. A lnguge my hve simple declrtive description nd complicted opertionl description s regulr expression. For instnce, the regulr expression ( + + ( + )( + ) ( + )) is nturl opertionl description of the lnguge ove, nd it is rguly less intuitive thn the declrtive one. This ecomes even more cler if we consider the lnguge of the words over {,, c} contining n even numer of s, of s, nd of c s. In this chpter we present logicl formlism for the declrtive description of regulr lnguges. We use logicl formuls to descrie properties of words, nd logicl opertors to construct complex properties out of simpler ones. We then show how to utomticlly trnslte formul descriing property of words into n utomton recognizing the words stisfying the property. As consequence, we otin n lgorithm to convert declrtive into opertionl descriptions, nd vice vers. 9.1 First-Order Logic on Words In declrtive style, lnguge is defined y its memership predicte, i.e., the property tht words must stisfy in order to elong to it. Predicte logic is the stndrd lnguge to express memership predictes. Strting from some nturl, tomic predictes, more complex ones cn e constructed through oolen comintions nd quntifiction. We introduce tomic predictes Q (x), where is letter, nd x rnges over the positions of the word. The intended mening is the letter t 177

178 178 CHAPTER 9. AUTOMATA AND LOGIC position x is n. For instnce, the property ll letters re s is formlized y the formul x Q (x). In order to express reltions etween positions we dd to the syntx the predicte x < y, with intended mening position x is smller thn (i.e., lies to the left of) position y. For exmple, the property if the letter t position is n, then ll letters to the right of this position re lso s is formlized y the formul x y ((Q (x) x < y) Q (y)). Definition 9.1 Let V = {x, y, z,...} e n infinite set of vriles, nd let Σ = {,, c,...} e finite lphet. The set FO(Σ) of first-order formuls over Σ is the set of expressions generted y the grmmr: ϕ := Q (x) x < y ϕ (ϕ ϕ) x ϕ. As usul, vriles within the scope of n existentil quntifier re ounded, nd otherwise free. A formul without free vriles is sentence. Sentences of FO(Σ) re interpreted on words over Σ. For instnce, x Q (x) is true for the word, ut flse for word. Formuls with free vriles cnnot e interpreted on words lone: it does not mke sense to sk whether Q (x) holds for the word or not. A formul with free vriles is interpreted over pir (w, I), where I ssigns to ech free vrile (nd perhps to others) position in the word. For instnce, Q (x) is true for the pir (, x 1), ecuse the letter t position 1 of is, ut flse for (, x 2). Definition 9.2 An interprettion of formul ϕ of FO(Σ) is pir (w, I) where w Σ nd I is mpping tht ssigns to every free vrile x position I(x) {1,..., w } (the mpping my lso ssign positions to other vriles). Notice tht if ϕ is sentence then pir (w, E), where E is the empty mpping tht does not ssign ny position to ny vrile, is n interprettion of ϕ. Insted of (w, E) we write simply w. We now formlly define when n interprettion stisfies formul. Given word w nd numer k, let w[k] denote the letter of w t position k. Definition 9.3 The stisfction reltion (w, I) = ϕ etween formul ϕ of FO(Σ) nd n interprettion (w, I) of ϕ is defined y: (w, I) = Q (x) iff w[i(x)] = (w, I) = x < y iff I(x) < I(y) (w, I) = ϕ iff (w, I) = ϕ (w, I) = ϕ1 ϕ 2 iff (w, I) = ϕ 1 or (w, I) = ϕ 2 (w, I) = x ϕ iff w 1 nd some i {1,..., w } stisfies (w, I[i/x]) = ϕ where w[i] is the letter of w t position i, nd I[i/x] is the mpping tht ssigns i to x nd otherwise coincides with I. (Notice tht I my not ssign ny vlue to x.) If (w, I) = ϕ we sy tht (w, I) is model of ϕ. Two formuls re equivlent if they hve the sme models.

179 9.1. FIRST-ORDER LOGIC ON WORDS 179 It follows esily from this definition tht if two interprettions (w, I 1 ) nd (w, I 2 ) of ϕ differ only in the positions ssigned y I 1 nd I 2 to ounded vriles, then either oth interprettions re models of ϕ, or none of them is. In prticulr, whether n interprettion (w, I) of sentence is model or not depends only on w, not on I. We use some stndrd revitions: x ϕ := x ϕ ϕ 1 ϕ 2 := ( ϕ 1 ϕ 2 ) ϕ 1 ϕ 2 := ϕ 1 ϕ 2 Notice tht ccording to the definition of the stisfction reltion the empty word ɛ stisfies no formuls of the form x ϕ, nd ll formuls of the form x ϕ. While this cuses no prolems for our purposes, it is worth noticing tht in other contexts it my led to complictions. For instnce, the formuls x Q (x) nd y x Q (x) do not hold for exctly the sme words, ecuse the empty word stisfies the second, ut not the first. Further useful revitions re: x = y := (x < y y < x) first(x) := y y < x x is the first position lst(x) := y x < y x is the lst position y = x + 1 := x < y z(x < z z < y) y is the successor position of x y = x + 2 := z(z = x + 1 y = z + 1) y = x + (k + 1) := z(z = x + k y = z + 1) Exmple 9.4 Some exmples of properties expressile in the logic: The lst letter is nd efore it there re only s. x Q (x) x (lst(x) Q (x) lst(x) Q (x)) Every is immeditely followed y. x (Q (x) y (y = x + 1 Q (y))) Every is immeditely followed y, unless it is the lst letter. x (Q (x) y (y = x + 1 Q (y))) Between every nd every lter there is c. x y (Q (x) Q (y) x < y z (x < z z < y Q c (z)))

180 180 CHAPTER 9. AUTOMATA AND LOGIC Expressive power of FO(Σ) Once we hve defined which words stisfy sentence, we cn ssocite to sentence the set of words stisfying it. Definition 9.5 The lnguge L(ϕ) of sentence ϕ FO(Σ) is the set L(ϕ) = {w Σ w = φ}. We lso sy tht ϕ expresses L(ϕ). A lnguge L Σ is FO-definle if L = L(ϕ) for some formul ϕ of FO(Σ). The lnguges of the properties in the exmple re FO-definle y definition. To get n ide of the expressive power of FO(Σ), we prove theorem chrcterizing the FO-definle lnguges in the cse of 1-letter lphet Σ = {}. In this simple cse we only hve one predicte Q (x), which is lwys true in every interprettion. So every formul is equivlent to formul without ny occurrence of Q (x). For exmple, the formul y (Q (y) y < x) is equivlent to y y < x. We prove tht lnguge over one-letter lphet is FO-definle if nd only if it is finite or co-finite, where lnguge is co-finite if its complement is finite. So, for instnce, even simple lnguge like { n n is even } is not FO-definle. The pln of the proof is s follows. First, we define the quntifier-free frgment of FO({}), denoted y QF; then we show tht 1-letter lnguges re QF-definle iff they re finite or co-finite; finlly, we prove tht 1-letter lnguges re FO-definle iff they re QF-definle. For the definition of QF we need some more mcros whose intended mening should e esy to guess: x + k < y := z (z = x + k z < y) x < y + k := z (z = y + k x < z) k < lst := x (lst(x) x > k) In these mcros k is constnt, tht is, k < lst stnd for the infinite fmily of mcros 1 < lst, 2 < lst, 3 < lst.... Mcros like k > x or x + k > y re defined similrly. Definition 9.6 The logic QF (for quntifier-free) is the frgment of FO({}) with syntx where {<, >} nd k N. f := x k x y + k k lst f 1 f 2 f 1 f 2 Proposition 9.7 A lnguge over 1-letter lphet is QF-definle iff it is finite or co-finite. Proof: ( ): Let f e sentence of QF. Since QF does not hve quntifiers, f does not contin ny occurrence of vrile, nd so it is positive (i.e., negtion-free) oolen comintion of formuls of the form k < lst or k > lst. We proceed y induction on the structure of f. If f = k < lst, then L(ϕ) is co-finite, nd if f = k > lst, then L(ϕ) is finite. If f = f 1 f 2, then y induction hypothesis L( f 1 ) nd L( f 2 ) re finite or co-finite; if L( f 1 ) nd L( f 2 ) re finite, then so is L( f ), nd otherwise L( f ) is co-finite. The cse f = f 1 f 2 is similr.

181 9.2. MONADIC SECOND-ORDER LOGIC ON WORDS 181 ( ): A finite lnguge { k 1,..., k n } is expressed y the formul (lst > k 1 1 lst < k 1 + 1)... (lst > k 1 1 lst < k 1 + 1). To express co-finite lnguge, it suffices to show tht for every formul f of QF expressing lnguge L, there is nother formul f expressing the lnguge L. This is esily proved y induction on the structure of the formul. Theorem 9.8 Every formul ϕ of FO({}) is equivlent to formul f of QF. Proof: Sketch. By induction on the structure of ϕ. If ϕ(x, y) = x < y, then ϕ y < x + 0. If ϕ = ψ, the result follows from the induction hypothesis nd the fct tht negtions cn e removed using De Morgn s rules nd equivlences like (x < y + k) x y + k. If ϕ = ϕ 1 ϕ 2, the result follows directly from the induction hypothesis. Consider now the cse ϕ = x ψ. By induction hypothesis, ψ is equivlent to formul f of QF, nd we cn ssume tht f is in disjunctive norml form, sy f = D 1... D n. Then ϕ x D 1 x D 2... x D n, nd so it suffices to find formul f i of QF equivlent to x D i. The formul f i is conjunction of formuls contining ll conjuncts of D i with no occurrence of x, plus other conjuncts otined s follows. For every lower ound x < t 1 of D i, where t 1 = k 1 or t 1 = x 1 + k 1, nd every upper ound of the form x > t 2, where t 2 = k 1 or t 2 = x 1 + k 1 we dd to f i conjunct equivlent to t < t 1. For instnce, y + 7 < x nd x < z + 3 we dd y + 5 < z. It is esy to see tht f i x D i. Corollry 9.9 The lnguge Even = { 2n n 0} is not first-order expressile. These results show tht first-order logic cnnot express ll regulr lnguges, not even over 1-letter lphet. For this reson we now introduce mondic second-order logic. 9.2 Mondic Second-Order Logic on Words Mondic second-order logic extends first-order logic with vriles X, Y, Z,... rnging over sets of positions, nd with predictes x X, mening position x elongs to the set X. 1 It is llowed to quntify over oth kinds of vriles. Before giving forml definition, let us informlly see how this extension llows to descrie the lnguge Even. The formul sttes tht the lst position elongs to the set of even positions. A position elongs to this set iff it is the second position, or the second successor of nother position in the set. The following formul sttes tht X is the set of even positions: second(x) := y (first(y) x = y + 1) Even(X) := x (x X (second(x) y (x = y + 2 y X))) For the complete formul, we oserve tht the word hs even length if its lst position is even: EvenLength := X ( Even(X) x (lst(x) x X) ) 1 More generlly, second-order logic llows for vriles rnging over reltions of ritrry rity. The mondic frgment only llows rity 1, which corresponds to sets.

182 182 CHAPTER 9. AUTOMATA AND LOGIC We now define the forml syntx nd semntics of the logic. Definition 9.10 Let X 1 = {x, y, z,...} nd X 2 = {X, Y, Z,...} e two infinite sets of first-order nd second-order vriles. Let Σ = {,, c,...} e finite lphet. The set MSO(Σ) of mondic second-order formuls over Σ is the set of expressions generted y the grmmr: ϕ := Q (x) x < y x X ϕ ϕ ϕ x ϕ X ϕ An interprettion of formul ϕ is pir (w, I) where w Σ, nd I is mpping tht ssigns every free first-order vrile x position I(x) {1,..., w } nd every free second-order vrile X set of positions I(X) {1,..., w }. (The mpping my lso ssign positions to other vriles.) The stisfction reltion (w, I) = ϕ etween formul ϕ of MSO(Σ) nd n interprettion (w, I) of ϕ is defined s for FO(Σ), with the following dditions: (w, I) = x X iff I(x) I(X) (w, I) = X ϕ iff w > 0 nd some S {1,..., w } stisfies (w, I[S/X]) = ϕ where I[S/X] is the interprettion tht ssigns S to X nd otherwise coincides with I whether I is defined for X or not. If (w, I) = ϕ we sy tht (w, I) is model of ϕ. Two formuls re equivlent if they hve the sme models. The lnguge L(ϕ) of sentence ϕ MSO(Σ) is the set L(ϕ) = {w Σ w = φ}. A lnguge L Σ is MSO-definle if L = L(ϕ) for some formul ϕ MSO(Σ). Notice tht in this definition the set S my e empty. So, for instnce, y interprettion tht ssigns the empty set to X is model of the formul X x (x X). We use the stndrd revitions x X ϕ := x (x X ϕ) x X ϕ := x (x X ϕ) Expressive power of MSO(Σ) We show tht the lnguges expressile in mondic second-order logic re exctly the regulr lnguges. We strt with n exmple. Exmple 9.11 Let Σ = {,, c, d}. We construct formul of MSO(Σ) expressing the regulr lnguge c () d. The memership predicte of the lnguge cn e informlly formulted s follows: There is lock of consecutive positions X such tht: efore X there re only c s; fter X there re only d s; in X s nd s lternte; the first letter in X is n nd the lst letter is. The predicte is conjunction of predictes. We give formuls for ech of them.

183 9.2. MONADIC SECOND-ORDER LOGIC ON WORDS 183 X is lock of consecutive positions. Block(X) := x X y X (x < y ( z (x < z z < y) z X)) x lies efore/fter X. Before(x, X) := y X x < y After(x, X) := y X y < x Before X there re only c s. Before only c(x) := x Before(x, X) Q c (x) After X there re only d s. After only d(x) := x After(x, X) Q d (x) s nd s lternte in X. Alternte(X) := x X ( Q (x) y X (y = x + 1 Q (y) ) Q (x) y X (y = x + 1 Q (y) ) ) The first letter in X is n nd the lst is. First (X) := x X y (y < x y X) Q (x) Lst (X) := x X y (y > x y X) Q (x) Putting everything together, we get the formul X( Block(X) Before only c(x) After only d(x) Alternte(X) First (X) Lst (X) ) Notice tht the empty word is model of the formul. ecuse the empty set of positions stisfies ll the conjuncts. Let us now directly prove one direction of the result. Proposition 9.12 If L Σ is regulr, then L is expressile in MSO(Σ).

184 184 CHAPTER 9. AUTOMATA AND LOGIC Proof: Let A = (Q, Σ, δ, q 0, F) e DFA with Q = {q 0,..., q n } nd L(A) = L. We construct formul ϕ A such tht for every w ɛ, w = ϕ A iff w L(A). If ɛ L(A), then we cn extend the formul to ϕ A ϕ A, where ϕ A is only stisfied y the empty word (e.g. ϕ A = x x < x). We strt with some nottions. Let w = 1... m e word over Σ, nd let P q = { i {1,..., m} ˆδ(q 0, 0... i ) = q }. In words, i P q iff A is in stte q immeditely fter reding the letter i. Then A ccepts w iff m q F P q. Assume we were le to construct formul Visits(X 0,... X n ) with free vriles X 0,... X n such tht I(X i ) = P qi holds for every model (w, I) nd for every 0 i n. In words, Visits(X 0,... X n ) is only true when X i tkes the vlue P qi for every 0 i n. Then (w, I) would e model of ψ A := X 0... X n Visits(X 0,... X n ) x iff w hs lst letter, nd w L. So we could tke { ψa if q ϕ A := 0 F ψ A x x < x if q 0 F lst(x) x X i Let us now construct the formul Visits(X 0,... X n ). The sets P q re the unique sets stisfying the following properties: () 1 P δ(q0, 1 ), i.e., fter reding the letter t position 1 the DFA is in stte δ(q 0, 1 ); () every position i elongs to exctly one P q, i.e., the P q s uild prtition of the set positions; nd (c) if i P q nd δ(q, i+1 ) = q then i + 1 P q, i.e., the P q s respect the trnsition function δ. We express these properties through formuls. For every Σ, let q i = δ(q 0, ). The formul for () is: Init(X 0,..., X n ) = x first(x) (Q (x) x X i ) (in words: if the letter t position 1 is, then the position elongs to X i ). Formul for (): Σ q i F Prtition(X 0,..., X n ) = x n x X i i=0 n i, j = 0 i j (x X i x X j )

185 9.2. MONADIC SECOND-ORDER LOGIC ON WORDS 185 Formul for (c): Respect(X 0,..., X n ) = x y Altogether we get y = x + 1 Σ i, j {0,..., n} δ(q i, ) = q j (x X i Q (x) y X j ) Visits(X 0,... X n ) := Init(X 0,..., X n ) Prtition(X 0,..., X n ) Respect(X 0,..., X n ) It remins to prove tht MSO-definle lnguges re regulr. Given sentence ϕ MSO(Σ) show tht L(ϕ) is regulr y induction on the structure of ϕ. However, since the suformuls of sentence re not necessrily sentences, the lnguge defined y the suformuls of ϕ is not defined. We correct this. Recll tht the interprettions of formul re pirs (w, I) where I ssigns positions to the free first-order vriles nd sets of positions to the free second-order vriles. For exmple, if Σ = {, } nd if the free first-order nd second-order vriles of the formul re x, y nd X, Y, respectively, then two possile interprettions re, x 1 y 3 X {2, 3} Y {1, 2}, x 2 y 1 X Y {1} Given n interprettion (w, I), we cn encode ech ssignment x k or X {k 1,..., k l } s itstring of the sme length s w: the string for x k contins exctly 1 t position k, nd 0 s everywhere else; the string for X {k 1,..., k l } contins 1 s t positions k 1,..., k l, nd 0 s everywhere else. After fixing n order on the vriles, n interprettion (w, I) cn then e encoded s tuple (w, w 1,..., w n ), where n is the numer of vriles, w Σ, nd w 1,..., w n {0, 1}. Since ll of w, w 1,..., w n hve the sme length, we cn s in the cse of trnsducers look t (w, w 1,..., w n ) s word over the lphet Σ {0, 1} n. For the two interprettions ove we get the encodings x y X Y nd x y X Y corresponding to the words

186 186 CHAPTER 9. AUTOMATA AND LOGIC nd over Σ {0, 1} 4 Definition 9.13 Let ϕ e formul with n free vriles, nd let (w, I) e n interprettion of ϕ. We denote y enc(w, I) the word over the lphet Σ {0, 1} n descried ove. The lnguge of ϕ is L(ϕ) = {enc(w, I) (w, I) = ϕ}. Now tht we hve ssocited to every formul ϕ lnguge (whose lphet depends on the free vriles), we prove y induction on the structure of ϕ tht L(ϕ) is regulr. We do so y exhiiting utomt (ctully, trnsducers) ccepting L(ϕ). For simplicity we ssume Σ = {, }, nd denote y free(ϕ) the set of free vriles of ϕ. ϕ = Q (x). Then free(ϕ) = x, nd the interprettions of ϕ re encoded s words over Σ {0, 1}. The lnguge L(ϕ) is given y L(ϕ) = [ 1 1 ] nd is recognized y [ k k] k 0, i Σ nd i {0, 1} for every i {1,..., k}, nd i = 1 for exctly one index i {1,..., k} such tht i = [ [, [ [, [ ϕ = x < y. Then free(ϕ) = {x, y}, nd the interprettions of φ re encoded s words over Σ {0, 1} 2. The lnguge L(ϕ) is given y k 0, 1... k i Σ nd i, c i {0, 1} for every i {1,..., k}, L(ϕ) = 1... k i = 1 for exctly one index i {1,..., k}, c 1 c k c j = 1 for exctly one index j {1,..., k}, nd i < j nd is recognized y

187 9.2. MONADIC SECOND-ORDER LOGIC ON WORDS , ϕ = x X. Then free(ϕ) = {x, X}, nd interprettions re encoded s words over Σ {0, 1} 2. The lnguge L(ϕ) is given y 1 k 0,... k L(ϕ) = 1... k i Σ nd i, c i {0, 1} for every i {1,..., k}, c 1 c k i = 1 for exctly one index i {1,..., k}, nd for every i {1,..., k}, if i = 1 then c i = 1 nd is recognized y 0, 0, 0, , , 0, 0, ϕ = ψ. Then free(ϕ) = free(ψ), nd y induction hypothesis there exists n utomton A ψ s.t. L(A ψ ) = L(ψ). Oserve tht L(ϕ) is not in generl equl to L(ψ). To see why, consider for exmple the cse ψ = Q (x) nd ϕ = Q (x). The word [ ] [ ] [ ] elongs neither to L(ψ) nor L(ϕ), ecuse it is not the encoding of ny interprettion: the itstring for x contins more thn one 1. Wht holds is L(ϕ) = L(ψ) Enc(ψ), where Enc(ψ) is the lnguge of the encodings of ll the interprettions of ψ (whether they re models of ψ or not). We construct n utomton A enc ψ recognizing Enc(ψ), nd so we cn tke A ϕ = A ψ A enc ψ. Assume ψ hs k first-order vriles. Then word elongs to Enc(ψ) iff ech of its projections onto the 2nd, 3rd,..., (k + 1)-th component is itstring contining exctly one 1. As sttes of A enc ψ we tke ll the strings {0, 1} k. The intended mening of stte, sy stte 101 for the cse k = 3, is the utomton hs lredy red the 1 s in the itstrings of the first nd third vriles, ut not yet red the 1 in the second. The initil nd finl sttes re 0 k nd 1 k, respectively. The trnsitions re defined ccording to the intended mening of the sttes. For instnce, the utomton A enc x<y is

188 188 CHAPTER 9. AUTOMATA AND LOGIC 0, 0 1, 0 1, , 1 0 0, 0, , , , Oserve tht the numer of sttes of A enc ψ grows exponentilly in the numer of free vriles. This mkes the negtion opertion expensive, even when the utomton A φ is deterministic. ϕ = ϕ 1 ϕ 2. Then free(ϕ) = f ree(ϕ 1 ) free(ϕ 2 ), nd y induction hypothesis there re utomt A ϕi, A ϕ2 such tht L(A ϕ1 ) = L(ϕ 1 ) nd L(A ϕ2 ) = L(ϕ 2 ). If free(ϕ 1 ) = free(ϕ 2 ), then we cn tke A ϕ = A ϕ1 A ϕ2. But this need not e the cse. If free(ϕ 1 ) free(ϕ 2 ), then L(ϕ 1 ) nd L(ϕ 2 ) re lnguges over different lphets Σ 1, Σ 2, or over the sme lphet, ut with different intended mening, nd we cnnot just compute their intersection. For exmple, if ϕ 1 = Q (x) nd ϕ 2 = Q (y), then oth L(ϕ 1 ) nd L(ϕ 2 ) re lnguges over Σ {0, 1}, ut the second component indictes in the first cse the vlue of x, in the second the vlue of y. This prolem is solved y extending L(ϕ 1 ) nd L(A ϕ2 ) to lnguges L 1 nd L 2 over Σ {0, 1} 2. In our exmple, the lnguge L 1 contins the encodings of ll interprettions (w, {x n 1, y n 2 }) such tht the projection (w, {x n 1 }) elongs to L(Q (x)), while L 2 contins the interprettions such tht (w, {y n 2 }) elongs to L(Q (y)). Now, given the utomton A Q (x) recognizing L(Q (x)) [ [, [ [, [ we trnsform it into n utomton A 1 recognizing L 1

189 9.2. MONADIC SECOND-ORDER LOGIC ON WORDS 189 0, 0, 0, 0 0, 0, 0, , After constructing A 2 similrly, tke A ϕ = A 1 A 2. ϕ = x ψ. Then free(ϕ) = f ree(ψ) \ {x}, nd y induction hypothesis there is n utomton A ψ s.t. L(A ψ ) = L(ψ). Define A xψ s the result of the projection opertion, where we project onto ll vriles ut x. The opertion simply corresponds to removing in ech letter of ech trnsition of A σ the component for vrile x. For exmple, the utomton A x Q (x) is otined y removing the second components in the utomton for A Q (x) shown ove, yielding,, Oserve tht the utomton for x ψ cn e nondeterministic even if the one for ψ is deterministic, since the projection opertion my mp different letters into the sme one. ϕ = X ϕ. We proceed s in the previous cse. Size of A ϕ. The procedure for constructing A ϕ proceeds ottom-up on the syntx tree of ϕ. We first construct utomt for the tomic formuls in the leves of the tree, nd then proceed upwrds: given utomt for the children of node in the tree, we construct n utomton for the node itself. Whenever node is leled y negtion, the utomton for it cn e exponentilly igger thn the utomton for its only child. This yields n upper ound for the size of A ϕ equl to tower of exponentils, where the height of the tower is equl to the lrgest numer of negtions in ny pth from the root of the tree to one of its leves. It cn e shown tht this very lrge upper ound is essentilly tight: there re formuls for which the smllest utomton recognizing the sme lnguge s the formul reches the upper ound. This mens tht MSO-logic llows to descrie some regulr lnguges in n extremely succinct form. Exmple 9.14 Consider the lphet Σ = {, } nd the lnguge Σ, recognized y the NFA

190 190 CHAPTER 9. AUTOMATA AND LOGIC We derive this NFA y giving formul ϕ such tht L(ϕ) =, nd then using the procedure descried ove. We shll see tht the procedure is quite lorious. The formul sttes tht the lst letter is, nd ll other letters re s. We first ring ϕ into the equivlent form ϕ = x (lst(x) Q (x)) x ( lst(x) Q (x)) ψ = x (lst(x) Q (x)) x ( lst(x) Q (x)) We trnsform ψ into n NFA. First, we compute n utomton for lst(x) = y x < y. Recll tht the utomton for x < y is , [x < y] Applying the projection opertion, we get following utomton for y x < y [ [, [ [ ] [ [,, 0 [ [ ] [ [,, 1 [ y x < y] Recll tht computing the utomton for the negtion of formul requires more thn complementing the utomton. First,we need n utomton recognizing Enc( y x < y). [ [, [ [, [ [, Second, we determinize nd complement the utomton for y x < y: [ [, [ [, Σ {0, 1} Σ {0, 1}

191 9.2. MONADIC SECOND-ORDER LOGIC ON WORDS 191 And finlly, we compute the intersection of the lst two utomt, getting [ [, [ [, [ [, [ [, whose lst stte is useless nd cn e removed, yielding the following NFA for lst(x): [ [, [ [, [lst(x)] Next we compute n utomton for x (lst(x) Q (x)), the first conjunct of ψ. We strt with n NFA for Q (x) [ [ ] [ [,, 0 [ [Q (x)] The utomton for x (lst(x) Q (x)) is the result of intersecting this utomton with the NFA for lst(x) nd projecting onto the first component. We get, [ x (lst(x) Q (x))] Now we compute n utomton for x ( lst(x) Q (x)), the second conjunct of ψ. We first otin n utomton for Q (x) y intersecting the complement of the utomton for Q (x) nd the utomton for Enc(Q (x). The utomton for Q (x) is [ [ ] [ [,, 0 [ [Q (x)]

192 192 CHAPTER 9. AUTOMATA AND LOGIC nd fter determiniztion nd complementtion we get [ ] [ 0 [ ] [ 0 [ [ ] [ 1 1 ] [ Σ {0, 1} For the utomton recognizing Enc(Q (x)), notice tht Enc(Q (x)) = Enc( y x < y), ecuse oth formuls hve the sme free vriles, nd so the sme interprettions. But we hve lredy computed n utomton for Enc( y x < y), nmely [ [, [ [, [ [, The intersection of the lst two utomt yields three-stte utomton for Q (x), ut fter eliminting useless stte we get [ [ ] [ [,, 0 [ [ Q (x)] Notice tht this is the sme utomton we otined for Q (x), which is fine, ecuse over the lphet {, } the formuls Q (x) nd Q (x) re equivlent. To compute n utomton for lst(x) we just oserve tht lst(x) is equivlent to y x < y, for which we hve lredy compute n NFA, nmely [ [, [ [ ] [ [,, 0 [ [ ] [ [,, 1 [ lst(x)]

193 9.2. MONADIC SECOND-ORDER LOGIC ON WORDS 193 Intersecting the utomt for lst(x) nd Q (x), nd susequently projecting onto the first component, we get n utomton for x ( lst(x) Q (x)),,,, [ x ( lst(x) Q (x))] Determinizing, complementing, nd removing useless stte yields the following NFA for x ( lst(x) Q (x)): [ x ( lst(x) Q (x))] Summrizing, the utomt for the two conjuncts of ψ re, nd whose intersection yields 3-stte utomton, which fter removl of useless stte ecomes [ x (lst(x) Q (x)) x ( lst(x) Q (x))] ending the derivtion. Exercises Exercise 86 Descrie the lnguges descried y the following formuls of FO({, }) nd give corresponding regulr expression: () x first(x) () x first(x)

194 194 CHAPTER 9. AUTOMATA AND LOGIC (c) x y ( x < y Q (x) Q (y) ) x ( Q (x) y x < y Q (y) ) x y x < y Exercise 87 For every n 1, give FO-formul reviting y = x + 2 n of length O(n). (Notice tht the revition y = x + k of pge 9.1 hs length O(k), nd so it cnnot e directly used.) Use it to give nother FO-formul ϕ n, lso of of polynomil length in n, for the lnguge L n = {ww {, } w = 2 n }. Remrk: Since the miniml DFA for L n hs 2 2n sttes (Exercise 11), this shows tht the numer of sttes of miniml utomton equivlent to given FO-formul my e doule exponentil in the length of the formul. Exercise 88 The nesting depth d(ϕ) of formul ϕ of FO({}) is defined inductively s follows: d(q (x)) = d(x < y) = 0; d( ϕ) = d(ϕ), d(ϕ 1 ϕ 2 ) = mx{d(ϕ 1 ), d(ϕ 2 )}; nd d( x ϕ) = 1 + d(ϕ). Prove tht every formul ϕ of FO({}) of nesting depth n is equivlent to formul f of QF hving the sme free vriles s ϕ, nd such tht every constnt k ppering in f stisfies k 2 n. Hint: Modify suitly the proof of Theorem 9.8. Exercise 89 Give MSO-formul Odd crd(x) expressing tht the crdinlity of the set of positions X is odd. Hint: Follow the pttern of the formul Even(X). Exercise 90 Given formul ϕ of MSO(Σ) nd second order vrile X not occurring in ϕ, show how to construct formul ϕ X with X s free vrile expressing the projection of the word onto the positions of X stisfies ϕ. Formlly, ϕ X must stisfy the following property: for every interprettion I of ϕ X, we hve (w, I) = ϕ X iff (w IX, I) = ϕ, where w IX denotes the result of deleting from w the letters t ll positions tht do not elong to IX. Exercise 91 (1) Given two sentences ϕ 1 nd ϕ 2 of MSO(Σ), construct sentence Conc(ϕ 1, ϕ 2 ) stisfying L(Conc(ϕ 1, ϕ 2 )) = L(ϕ 1 ) L(ϕ 2 ). (2) Given sentence ϕ of MSO(Σ), construct sentence Str(ϕ) stisfying L(Str(ϕ)) = L(ϕ). (3) Give n lgorithm RegtoMSO tht ccepts regulr expression r s input nd directly constructs sentence ϕ of MSO such tht L(ϕ) = L(r), without first constructing n utomton for the formul. Hint: Use the solution to Exercise 90.

195 9.2. MONADIC SECOND-ORDER LOGIC ON WORDS 195 Exercise 92 Consider the logic Pure MSO(Σ) with syntx ϕ := X Q X < Y X Y ϕ ϕ ϕ X ϕ Notice tht formuls of Pure MSO(Σ) do not contin first-order vriles. The stisfction reltion of Pure MSO(Σ) is given y: (w, I) = X Q iff w[p] = for every p I(X) (w, I) = X < Y iff p < p for every p I(X), p I(Y) (w, I) = X Y iff p < p for every p I(X), p I(Y) with the rest s for MSO(Σ). Prove tht MSO(Σ) nd Pure MSO(Σ) hve the sme expressive power for sentences. Tht is, show tht for every sentence φ of MSO(Σ) there is n equivlent sentence ψ of Pure MSO(Σ), nd vice vers. Exercise 93 Recll the syntx of MSO(Σ): ϕ := Q (x) x < y x X ϕ ϕ ϕ x ϕ X ϕ We hve introduced y = x + 1 ( y is the successor position of x ) s n revition y = x + 1 := x < y z (x < z z < y) Consider now the vrint MSO (Σ) in which, insted of n revition, y = x + 1 is prt of the syntx nd replces x < y. In other words, the syntx of MSO (Σ) is ϕ := Q (x) y = x + 1 x X ϕ ϕ ϕ x ϕ X ϕ Prove tht MSO (Σ) hs the sme expressive power s MSO(Σ) y finding formul of MSO (Σ) with the sme mening s x < y. Exercise 94 Give defining MSO-formul for the following lnguges over {, }. (). () The set of words with n odd numer of occurrences of. (c) The set of words such tht every two s with no other in etween re seprted y lock of s of odd length.

196 196 CHAPTER 9. AUTOMATA AND LOGIC Exercise 95 Consider formul φ(x) of MSO(Σ) tht does not contin ny occurrence of the Q (x). Given ny two interprettions tht ssign to X the sme set of positions, we hve tht either oth interprettions stisfy φ(x), or none of them does. So we cn spek of the sets of nturl numers (positions) stisfying φ(x). In this sense, φ(x) expresses property of the finite sets of nturl numers, which prticulr set my stisfy or not. This oservtion cn e used to utomticlly prove some (very) simple properties of the nturl numers. Consider for instnce the following conjecture : every finite set of nturl numers hs miniml element 2. The conjecture holds iff the formul Hs min(x) := x X y X x y is stisfied y every interprettion in which X is nonempty. Construct n utomton for Hs min(x), nd check tht it recognizes ll nonempty sets. Exercise 96 The encoding of set is string, tht cn e seen s the encoding of numer. We cn use this oservtion to express ddition in mondic second-order logic. More precisely, find formul Sum(X, Y, Z) tht is true iff n X + n Y = n Z, where x, y, z re the numers encoded y the sets X, Y, Z, respectively, using the LSBF-encoding. For instnce, the words X Y Z nd X Y Z should stisfy the formul: the first encodes = 5, nd the second encodes = Of course, this lso holds for every infinite set, ut we cnnot prove it using MSO over finite words.

197 Chpter 10 Applictions III: Presurger Arithmetic Presurger rithmetic is logicl lnguge for expressing properties of numers y mens of ddition nd comprison. A typicl exmple of such property is x + 2y > 2z nd 2x 3z = 4y. The property is stisfied y some triples (n x, n y, n z ) of nturl numers, like (4, 2, 0) nd (8, 1, 4), ut not y others, like (6, 0, 4) or (2, 2, 4). Vlutions stisfying the property re clled solutions or models. We show how to construct for given formul ϕ of Presurger rithmetic n NFA A ϕ recognizing the solutions of ϕ. In Section 10.1 we introduce the syntx nd semntics of Presurger rithemetic. Section 10.2 constructs NFA recognizing ll solutions over the nturl numers, nd Section 10.3 NFA recognizing ll solutions over the integers Syntx nd Semntics Formuls of Presurger rithmetic re constructed out of n infinite set of vriles V = {x, y, z,...} nd the constnts 0 nd 1. The syntx of formuls is defined in three steps. First, the set of terms is inductively defined s follows: the symols 0 nd 1 re terms; every vrile is term; if t nd u re terms, then t + u is term. An tomic formul is n expression t u, where t nd u re terms. The set of Presurger formuls is inductively defined s follows: every tomic formul is formul; if ϕ 1, ϕ 2 re formuls, then so re ϕ 1, ϕ 1 ϕ 2, nd x ϕ

198 198 CHAPTER 10. APPLICATIONS III: PRESBURGER ARITHMETIC As usul, vriles within the scope of n existentil quntifier re ounded, nd otherwise free. Besides stndrd revitions like,,, we lso introduce: n := 1 } {{ } n times nx := } {{ } x + x x n times t t := t t t = t := t t t t t < t := t t (t = t ) t > t := t < t An interprettion is function I: V IN. An interprettion I is extended to terms in the nturl wy: I(0) = 0, I(1) = 1, nd I(t + u) = I(t) + I(u). The stisfction reltion I = ϕ for n interprettion I nd formul ϕ is inductively defined s follows, where I[n/x] denotes the interprettion tht ssigns the numer n to the vrile x, nd the sme numers s I to ll other vriles: I = t u iff I(t) I(u) I = ϕ 1 iff I = ϕ 1 I = ϕ 1 ϕ 2 iff I = ϕ 1 or I = ϕ 2 I = x ϕ iff there exists n 0 such tht I[n/x] = ϕ It is esy to see tht whether I stisfies ϕ or not depends only on the vlues I ssigns to the free vriles of ϕ (i.e., if two interprettions ssign the the sme vlues to the free vriles, then either oth stisfy the formul, or none does). The solutions of ϕ re the projection onto the free vriles of ϕ of the interprettions tht stisfy ϕ. if we fix totl order on the set V of vriles nd formul ϕ hs k free vriles, then its set of solutions cn e represented s suset of IN k, or s reltion of rity k over the universe IN. We cll this suset the solution spce of ϕ, nd denote it y Sol(ϕ). Exmple 10.1 The solution spce of the formul x 2 0 is the set {2, 3, 4,...}. The free vriles of the formul x (2x = y 2y = z) re y nd z. The solutions of the formul re the pirs {(2n, 4n) n 0}, where we ssume tht the first nd second components correspond to the vlues of y nd z, respectively. Automt encoding nturl numers. We use trnsducers to represent, compute nd mnipulte solution spces of formuls. As in Section 6.1 of Chpter 6, we encode nturl numers s strings over {0, 1} using the lest-significnt-it-first encoding LSBF. If formul hs free vriles x 1,..., x k, then its solutions re encoded s words over {0, 1} k. For instnce, the word x x x encodes the solution (3, 10, 0). The lnguge of formul φ is defined s L(ϕ) = LSBF(s) s Sol(ϕ)

199 10.2. AN NFA FOR THE SOLUTIONS OVER THE NATURALS. 199 where LSBF(s) denotes the set of ll encodings of the tuple s of nturl numers. In other words, L(ϕ) is the encoding of the reltion Sol(ϕ) An NFA for the Solutions over the Nturls. Given Presurger formul ϕ, we construct trnsducer A ϕ such tht L(A ϕ ) = L(ϕ). Recll tht Sol(ϕ) is reltion over IN whose rity is given y the numer of free vriles of φ. The lst section of Chpter 6 implements opertions reltions of ritrry rity. These opertions cn e used to compute the solution spce of the negtion of formul, the disjunction of two formuls, nd the existentil quntifiction of two formuls: The solution spce of the negtion of formul with k free vriles is the complement of its solution spce with respect to the universe U k. In generl, when computing the complement of reltion we hve to worry out ensuring tht the NFAs we otin only ccept words tht encode some tuple of elements (i.e., some clen-up mye necessry to ensure tht utomt do not ccept words encoding nothing). For Presurger rithmetic this is not necessry, ecuse in the LSBF encoding every word encodes some tuple of numers. The solution spce of disjunction ϕ 1 ϕ 2 where ϕ 1 nd ϕ 2 hve the sme free vriles is clerly the union of their solution spces, nd cn e computed s Union(Sol(ϕ 1 ), Sol(ϕ 2 )). If ϕ 1 nd ϕ 2 hve different sets V 1 nd V 2 of free vriles, then some preprocessing is necessry. Define Sol V1 V 2 (ϕ i ) s the set of vlutions of V 1 V 2 whose projection onto V 1 elongs to Sol(ϕ i ). Trnsducers recognizing Sol V1 V 2 (ϕ i ) for i = 1, 2 re esy to compute from trnsducers recognizing Sol(ϕ i ), nd the solution spce is Union(Sol V1 V 2 (ϕ 1 ), Sol V1 V 2 (ϕ 2 )). The solution spce of formul x ϕ, where x is free vrile of ϕ, is Projection I(Sol(ϕ)), where I contins the indices of ll vriles with the exception of the index of x. It only remins to construct utomt recognizing the solution spce of tomic formuls. Consider n expression of the form ϕ = 1 x n x n where 1,..., n, Z (not N!). Since we llow negtive integers s coefficients, for every tomic formul there is n equivlent expression in this form (i.e., n expression with the sme solution spce). For exmple, x y + 4 is equivlent to x + y 4. Letting = ( 1,..., n ), x = (x 1,..., x n ), nd denoting the sclr product of nd x y x we write ϕ = x. We construct DFA for Sol(ϕ). The sttes of the DFA re integers. We choose trnsitions nd finl sttes of the DFA so tht the following property holds: Stte q Z recognizes the encodings of the tuples c IN n such tht c q. (10.1) Given stte q Z nd letter ζ {0, 1} n, let us determine the trget stte q of the trnsition q ζ q of the DFA, where ζ {0, 1} n. A word w ({0, 1} n ) is ccepted from q iff the word ζw

200 200 CHAPTER 10. APPLICATIONS III: PRESBURGER ARITHMETIC is ccepted from q. Since we use the lsf encoding, the tuple of nturl numers encoded y ζw is 2c + ζ, where c IN n is the tuple encoded y w. So c IN n is ccepted from q iff 2c + c ζ is ccepted from q. Therefore, in order to stisfy property 10.1 we must choose q so tht c q iff (2c + c ζ ) q. A little rithmetic yields 1 q = (q ζ) 2 nd so we define the trnsition function of the DFA y δ(q, ζ) = 1 (q ζ). 2 For the finl sttes we oserve tht stte is finl iff it ccepts the empty word iff it ccepts the tuple (0,..., 0) IN n. So in order to stisfy property 10.1 we must mke stte q finl iff q 0. As initil stte we choose. This leds to the lgorithm AFtoDFA(ϕ) of Tle 10.1, where for clrity the stte corresponding to n integer k Z is denoted y s k. AFtoDFA(ϕ) Input: Atomic formul ϕ = x Output: DFA A ϕ = (Q, Σ, δ, q 0, F) such tht L(A ϕ ) = L(ϕ) 1 Q, δ, F ; q 0 s 2 W {s } 3 while W do 4 pick s k from W 5 dd s k to Q 6 if k 0 then dd s k to F 7 for ll ζ {0, 1} n do 1 8 j (k ζ) 2 9 if s j Q then dd s j to W 10 dd (s k, ζ, s j ) to δ Tle 10.1: Converting n tomic formul into DFA recognizing the lsf encoding of its solutions. Exmple 10.2 Consider the tomic formul 2x y 2. The DFA otined y pplying AFtoDFA to it is shown in Figure The initil stte is 2. Trnsitions leving stte 2 re given y 1 1 δ(2, ζ) = 2 (2 (2, 1) (ζ x, ζ y )) = 2 (2 2ζ x + ζ y ) nd so we hve 2 [0, 1, 2 [0, 1, 2 [1, 0 nd 2 [1, 0. Sttes 2, 1, nd 0 re finl. The DFA

201 10.2. AN NFA FOR THE SOLUTIONS OVER THE NATURALS. 201 [ 0 [ [ 0 0, 1 [ 1 [ [ 0 1, [ 1 2 [ [ 1 1, [ [ 0 1, [ [ 0 0, 0 [ 0 [ [ 1 1, 1 [ ] [ [ ] [ Figure 10.1: DFA for the formul 2x y 2. ccepts, for exmple, the word [ ] [ ] [ ] [ ] [ ] [ ] which encodes x = 12 nd y = 50 nd, indeed If we remove the lst letter then the word encodes x = 12 nd y = 18, nd is not ccepted, which indeed corresponds to Consider now the formul x + y 4. We rewrite it s x y 4, nd pply the lgorithm. The resulting DFA is shown in Figure The initil stte is 4. Trnsitions leving 4 re given y 1 1 δ( 4, ζ) = 2 ( 4 ( 1, 1) (ζ x, ζ y )) = 2 ( 4 + ζ x + ζ y ) nd so we hve 4 [0, 2, 4 [0, 2, 4 [1, 2 nd 4 [1, 1. Notice tht the DFA is not miniml, since sttes 0 nd 1 cn e merged. Prtil correctness of AFtoDFA is esily proved y showing tht for every q Z nd every word w ({0, 1} n ), the stte q ccepts w iff w encodes c IN n stisfying c q. The proof proceeds y induction of w. For w = 0 the result follows immeditely from the definition of the finl sttes, nd for w > 0 from the fct tht δ stisfies property 10.1 nd from the induction hypothesis. Detils re left to the reder. Termintion of AFtoDFA lso requires proof: in principle the lgorithm could keep generting new sttes forever. We show tht this is not the cse. Lemm 10.3 Let ϕ = x nd let s = k i=1 i. All sttes s j dded to the workset during the execution of AFtoDFA(ϕ) stisfy s j + s.

202 202 CHAPTER 10. APPLICATIONS III: PRESBURGER ARITHMETIC [ [ [ 0 0 1,, 2 [ 1 [ [ [ 0 0 1,, [ 1 [ [ [ 0 1 1,, 4 [ [ [ 0 0 1,, 0 [ 0 1 [ 1 1 [ 0 [ [ [ 0 1 1,, Figure 10.2: DFA for the formul x + y 4. Proof: The property holds for s, the first stte dded to the workset. We show tht, t ny point in time, if ll the sttes dded to the workset so fr stisfy the property, then so does the next one. Let s j e this next stte. Then there exists stte s k in the workset nd ζ {0, 1} n such tht j = 1 2 (k ζ). Since y ssumption s k stisfies the property we hve s k + s nd so s ζ + s ζ j 2 2 Now we mnipulte the right nd left ends of (10.2). A little rithmetic yields s + s ζ 2 2s 2 + 2s 2 s ζ + s 2 (10.2) which together with (10.2) leds to s j + s nd we re done. Exmple 10.4 We compute ll nturl solutions of the system of liner inequtions 2x y 2 x + y 4

203 10.2. AN NFA FOR THE SOLUTIONS OVER THE NATURALS. 203 such tht oth x nd y re multiples of 4. This corresponds to computing DFA for the Presurger formul z x = 4z w y = 4w 2x y 2 x + y 4 The miniml DFA for the first two conjuncts cn e computed using projections nd intersections, ut the result is lso esy to guess: it is the DFA of Figure 10.3 (where trp stte hs een omitted). [ ] [ [ ] [ ] [ ] [ 1 q 0 q 1 q 2 Figure 10.3: DFA for the formul z x = 4z w y = 4w. The solutions re then represented y the intersection of the DFAs shown in Figures 10.1, 10.2 (fter merging sttes 0 nd 1), nd The result is shown in Figure (Some sttes from which no finl stte cn e reched re omitted.) [ [ 0 0, 0, 0, q 2 [ 0 [ 0 [ [ 1 1, [ 1 2, 4, q 0 [ ] 1, 2, q 1 [ ] 0, 1, q [ 0 [ [ 1 1, 1, 0, q 2 2, 0, q 2 [ [ 0 0, [ [ 0 1, [ [ 1 1, Figure 10.4: Intersection of the DFAs of Figures 10.1, 10.2, nd Sttes from which no finl stte is rechle hve een omitted Equtions A slight modifiction of AFtoDFA directly constructs DFA for the solutions of x =, without hving to intersect DFAs for x nd x. The sttes of the DFA re trp stte q t

204 204 CHAPTER 10. APPLICATIONS III: PRESBURGER ARITHMETIC ccepting the empty lnguge, plus integers stisfying: Stte q Z recognizes the encodings of the tuples c IN n such tht c = q. (10.3) For the trp stte q t we tke δ(q t, ζ) = q t for every ζ {0, 1} n. For stte q Z nd letter ζ {0, 1} n we determine the trget stte q of trnsition q ζ q. Given tuple c IN n, property 10.3 requires c L(q ) iff c = q. As in the cse of inequtions, we hve c L(q ) iff 2c + c ζ L(q) iff (2c + ζ) = q (property 10.3 for q) iff c = 1 2 (q ζ) If q ζ is odd, then, since c is n integer, the eqution c = 1 2 (q ζ) hs no solution. So in order to stisfy property 10.3 we must choose q stisfying L(q ) =, nd so we tke q = q t. If q ζ is even then we must choose q stisfying c = q, nd so we tke q = 1 2 (q ζ). Therefore, the trnsition function of the DFA is given y: { δ(q, ζ) = 1 2 q t if q = q t or q ζ is odd (q ζ) if q ζ is even For the finl sttes, recll tht stte is finl iff it ccepts the tuple (0,..., 0). So q t is nonfinl nd, y property 10.3, q Z is finl iff (0..., 0) = q. So the only finl stte is q = 0. The resulting lgorithm is shown in Tle The lgorithm does not construct the trp stte. Exmple 10.5 Consider the formuls x + y 4 nd x + y = 4. The result of pplying AFtoDFA to x + y 4 is shown t the top of Figure Notice the similrities nd differences with the DFA for x + y 4 in Figure The ottom prt of the Figure shows the result of pplying EqtoDFA to x + y = 4. Oserve tht the trnsitions re suset of the trnsitions of the DFA for x + y 4. This exmple shows tht the DFA is not necessrily miniml, since stte 1 cn e deleted. Prtil correctness nd termintion of EqtoDFA re esily proved following similr steps to the cse of inequtions An NFA for the Solutions over the Integers. We construct n NFA recognizing the encodings of the integer solutions (positive or negtive) of formul. In order to del with negtive numers we use 2-complements. A 2-complement encoding of n integer x Z is ny word n, where n 1, stisfying n 1 x = i 2 i n 2 n (10.4) i=0

205 10.3. AN NFA FOR THE SOLUTIONS OVER THE INTEGERS. 205 EqtoDFA(ϕ) Input: Eqution ϕ = x = Output: DFA A = (Q, Σ, δ, q 0, F) such tht L(A) = L(ϕ) (without trp stte) 1 Q, δ, F ; q 0 s 2 W {s } 3 while W do 4 pick s k from W 5 dd s k to Q 6 if k = 0 then dd s k to F 7 for ll ζ {0, 1} n do 8 if (k ζ) is even then 9 j 1 (k ζ) 2 10 if s j Q then dd s j to W 11 dd (s k, ζ, s j ) to δ Tle 10.2: Converting n eqution into DFA recognizing the lsf encodings of its solutions. We cll n the sign it. For exmple, 110 encodes = 3, nd 111 encodes = 1. If the word hs length 1 then its only it is the sign it; in prticulr, the word 0 encodes the numer 0, nd the word 1 encodes the numer 1. The empty word encodes no numer. Oserve tht ll of 110, 1100, 11000,... encode 3, nd ll of 1, 11, 111,... encode 1. In generl, it is esy to see tht ll words of the regulr expression 0... n 1 n n encode the sme numer: for n = 0 this is ovious, nd for n = 1 oth 0... n 1 1 nd 0... n 1 11 m encode the sme numer ecuse 2 m+n + 2 m 1+n n+1 = 2 n. This property llows us to encode tuples of numers using pdding. Insted of pdding with 0, we pd with the sign it. Exmple 10.6 The triple (12, 3, 14) is encoded y ll the words of For exmple, the triples (x, y, z) nd (x, y z ) encoded y nd

206 206 CHAPTER 10. APPLICATIONS III: PRESBURGER ARITHMETIC [ 0 2 [ 0 [ [ [ 0 1 1,, 4 0 [ 0 [ [ [ 0 1 1,, [ [ [ 0 0 1,, 1 [ 1 [ [ [ [ ,,, 2 [ [ [ 0 1 1,, 1 [ [ [ 0 0 1,, [ 1 [ 0 2 [ 1 [ 0 [ 1 [ [ 0 1, [ [ 1 1 [ [ 0 1, Figure 10.5: DFAs for the formuls x + y 4 nd x + y = 4. re given y x = = 12 x = = 12 y = = 3 y = = 3 z = = 14 z = = 14 We construct NFA (no longer DFA!) recognizing the integer solutions of n tomic formul x. As usul we tke integers for the sttes, nd the NFA should stisfy: Stte q Z recognizes the encodings of the tuples c Z n such tht c q. (10.5) However, integer sttes re no longer enough, ecuse no stte q Z cn e finl: in the 2- complement encoding the empty word encodes no numer, nd so, since q cnnot ccept the empty word y property 10.5, q must e nonfinl. But we need t lest one finl stte, nd so we dd to the NFA unique finl stte q f without ny outgoing trnsitions, ccepting only the empty word. Given stte q Z nd letter ζ {0, 1} n, we determine the trgets q of the trnsitions q ζ q of the NFA, where ζ {0, 1} n. (As we will see, the NFA my hve one or two such trnsitions.)

207 10.3. AN NFA FOR THE SOLUTIONS OVER THE INTEGERS. 207 A word w ({0, 1} n ) is ccepted from some trget stte q iff ζw is ccepted from q. In the 2-complement encoding there re two cses: (1) If w ɛ, then ζw encodes the tuple of integers 2c + ζ, where c Z n is the tuple encoded y w. (This follows esily from the definition of 2-complements.) (2) If w = ɛ, then ζw encodes the tuple of integers ζ, ecuse in this cse ζ is the sign it. In cse (1), property 10.5 requires trget stte q such tht c q iff (2c + c ζ ) q. So we tke 1 q = (q ζ) 2 For cse (2) property 10.5 only requires trget stte q if ( ζ) q, nd if so then it requires q to e finl stte. So if q + ζ 0 then we dd q ζ q f to the set of trnsitions, nd in this cse the utomton hs two trnsitions leving stte q nd leled y ζ. Summrizing, we define the trnsition function of the NFA y δ(q, ζ) = { 1 2 (q ζ), q f } if q + ζ 0 { 1 2 (q ζ) } otherwise Oserve tht the NFA contins ll the sttes nd trnsitions of the DFA for the nturl solutions of x, plus possily other trnsitions. All integer sttes re now nonfinl, the only finl stte is q f. Exmple 10.7 Figure 10.6 shows t the top the NFA recognizing ll integer solutions of 2x y 2. It hs ll sttes nd trnsitions of the DFA for the nturl solutions, plus some more (compre with Figure 10.1). The finl stte q f nd the trnsitions leding to it re drwn in red. Consider for instnce stte 1. In order to determine the letters ζ {0, 1} 2 for which q f δ( 1, ζ), we compute q + ζ = 1 + 2ζ x ζ y for ech (ζ x, ζ y ) {0, 1} 2, nd compre the result to 0. We otin tht the letters leding to q f re (1, 0) nd (1, 1) Equtions If order to construct n NFA for the integer solutions of n eqution x = we cn proceed s for inequtions. The result is gin n NFA contining ll sttes nd trnsitions of the DFA for the nturl solutions computed in Section , plus possile some more. The utomton hs n dditionl finl stte q f, nd trnsition q ζ q f iff q + ζ = 0. Grphiclly, we cn lso otin the NFA y strting with the NFA for x, nd removing ll trnsitions q ζ q such tht q 1 ζ 2 (q ζ), nd ll trnsitions q q f such tht q + ζ 0.

208 208 CHAPTER 10. APPLICATIONS III: PRESBURGER ARITHMETIC Exmple 10.8 The NFA for the integer solutions of 2x y = 2 is shown in the middle of Figure Its trnsitions re suset of those of the NFA for 2x y 2. The NFA for the integer solutions of n eqution hs n interesting property. Since q + ζ = 0 holds iff 1 2 (q + ζ) = 1 ζ 2 (2q) = q, the NFA hs trnsition q q f iff it lso hs self-loop q ζ q. (For instnce, stte 1 of the DFA in the middle of Figure 10.6 hs red trnsition leled y (0, 1) nd self-loop leled y (0, 1).) Using this property it is not difficult to see tht the powerset construction does not cuse lowup in the numer of sttes: it only dds one extr stte for ech predecessor of the finl stte. Exmple 10.9 The DFA otined y pplying the powerset construction to the NFA for 2x y = 2 is shown t the ottom of Figure 10.6 (the trp stte hs een omitted). Ech of the three predecessors of q f gets duplicted. Moreover, the DFA otined y mens of the powerset construction is miniml. This cn e proved y showing tht ny two sttes recognize different lnguges. If exctly one of the sttes is finl, we re done. If oth sttes re nonfinl, sy, k nd k, then they recognize the solutions of x = k nd x = k, nd so their lnguges re not only distinct ut even disjoint. If oth sttes re finl, then they re the duplictes of two nonfinl sttes k nd k, nd their lnguges re those of k nd k, plus the empty word. So, gin, their lnguges re distinct Algorithms The lgorithms for the construction of the NFAs re shown in Tle Additions to the previous lgorithms re shown in lue. Exercises Exercise 97 Let k 1, k 2 N 0 e constnts. Find Presurger rithmetic formul, ϕ(x, y), with free vriles x nd y such tht I = ϕ(x, y) iff I(x) I(y) nd I(x) I(y) k 1 (mod k 2 ). Find corresponding utomton for the cse k 1 = 0 nd k 2 = 2. Exercise 98 Construct finite utomton for the Presurger formul y x = 3y using the lgorithms of the chpter. Exercise 99 AFtoDFA returns DFA recognizing ll solutions of given liner ineqution 1 x x k x k with 1, 2,..., k, Z encoded using the lsf encoding of IN k. We my lso use the most-significnt-it-first (msf) encoding, e.g., ([ ]) ([ ] [ ] [ ]) msf = L ( )

209 10.3. AN NFA FOR THE SOLUTIONS OVER THE INTEGERS. 209 IneqZtoNFA(ϕ) Input: Ineqution ϕ = x over Z Output: NFA A = (Q, Σ, δ, Q 0, F) such tht L(A) = L(ϕ) 1 Q, δ, F ; Q 0 {s } 2 W {s } 3 while W do 4 pick s k from W 5 dd s k to Q 6 for ll ζ {0, 1} n do 1 7 j (k ζ) 2 8 if s j Q then dd s j to W 9 dd (s k, ζ, s j ) to δ 10 j k + ζ 11 if j 0 then 12 dd q f to Q nd F 13 dd (s k, ζ, q f ) to δ EqZtoNFA(ϕ) Input: Eqution ϕ = x = over Z Output: NFA A = (Q, Σ, δ, Q 0, F) such tht L(A) = L(ϕ) 1 Q, δ, F ; Q 0 {s } 2 W {s } 3 while W do 4 pick s k from W 5 dd s k to Q 6 for ll ζ {0, 1} n do 7 if (k ζ) is even then 8 if (k + ζ) = 0 then dd k to F 9 j 1 (k ζ) 2 10 if s j Q then dd s j to W 11 dd (s k, ζ, s j ) to δ 12 j k + ζ 13 if j 0 then 14 dd q f to Q nd F 15 dd (s k, ζ, q f ) to δ Tle 10.3: Converting n inequlity into NFA ccepting the 2-complement encoding of the solution spce. 1. Construct finite utomton for the ineqution 2x y 2 w.r.t. msf encoding. 2. Adpt AFtoDFA to the msf encoding.

210 210 CHAPTER 10. APPLICATIONS III: PRESBURGER ARITHMETIC 2 [ [ ] [ 0 0 1, 1 [ [ 1 1, [ 0 [ [ 0 1, 1 0 [ [ 0 0, [ 0 [ [ [ 0 1 1,, [ [ [ [ ,,, [ [ 0 1, 1 [ [ 1 1, [ 1 0 [ [ 0 0, [ [ [ [ ,,, [ [ 1 1, ] [ [ 1 1, 2 [ 1 q f [ 0 [ 0 2 [ 0 [ 1 1 [ 0 [ 1 1 [ 1 q f [ 1 [ 1 [ 0 0 [ 0 [ 0 [ 0 [ 0 1 [ 1 2 [ 1 [ 0 1 [ 1 [ 1 [ 1 0 [ 0 [ 1 [ 0 [ 1 [ 0 Figure 10.6: NFAs for the solutions of 2x y 2 nd 2x y = 2 over Z, nd miniml DFA for the solutions of 2x y = 2.

211 Prt II Automt on Infinite Words 211

212

213 Chpter 11 Clsses of ω-automt nd Conversions In Section 11.1 we introduce ω-regulr expressions, textul nottion for defining lnguges of infinite words. The other sections introduce different clsses of utomt on infinite words, most of them with the sme expressive power s ω-regulr expressions, nd conversion lgorithms etween them ω-lnguges nd ω-regulr expressions Let Σ e n lphet. An infinite word, lso clled n ω-word, is n infinite sequence of letters of Σ. The conctention of finite word w 1 = 1... n nd n ω-word w 2 = is the ω-word w 1 w 2 = 1... n , sometimes lso denoted y w 1 w 2. We denote y Σ ω the set of ll ω-words over Σ. A set L Σ ω of ω-words is n infinitry lnguge or ω-lnguge over Σ. The conctention of lnguge L 1 nd n ω-lnguge L 2 is L 1 L 2 = {w 1 w 2 Σ ω w 1 L 1 nd w 2 L 2 }. The ω-itertion of lnguge L Σ is the ω-lnguge L ω = {w 1 w 2 w 3... w i L \ {ɛ}}. Oserve tht { } ω =, in contrst to the cse of finite words, where { } = {ɛ}. We extend regulr expressions to ω-regulr expressions, formlism to define ω-lnguges. Definition 11.1 The ω-regulr expressions s over n lphet Σ re defined y the following grmmr, where r RE(Σ) is regulr expression s ::= r ω rs 1 s 1 + s 2 Sometimes we write r s 1 insted of rs 1. The set of ll ω-regulr expressions over Σ is denoted y RE ω (Σ). The lnguge L ω s Σ of n ω-regulr expression s RE ω (Σ) is defined inductively s L ω r ω = (L(r)) ω ; L ω rs 1 = L(r) L ω s 1 ; nd L ω s 1 + s 2 = L ω s 1 L ω s

214 214 CHAPTER 11. CLASSES OF ω-automata AND CONVERSIONS A lnguge L is ω-regulr if there is n ω-regulr expression s such tht L = L ω s. Oserve tht the empty ω-lnguge is ω-regulr ecuse L ω ( ) ω =. As for regulr expressions, we often identify n ω-regulr expression s nd its ssocited ω- lnguge L ω (s). Exmple 11.2 ( + ) ω is the lnguge of ll ω-words over nd. ( + ) ω is the lnguge of ll ω-words over {, } contining only finitely mny s. ( + ) ω is the lnguge of ll words over {, } contining infinitely mny s nd infinitely mny s; n even shorter expression for the sme lnguge is (( + ) ) ω Büchi utomt Büchi utomt hve the sme syntx s NFAs, ut different definition of cceptnce. Suppose tht n NFA A = (Q, Σ, δ, Q 0, F) is given s input n infinite word w = over Σ. Intuitively, run of A on w never termintes, nd so we cnnot define cceptnce in terms of the stte reched t the end of the run. In fct, even the nme finl stte is no longer pproprite for Büchi utomt. So from now on we spek of ccepting sttes, lthough we still denote y F the set of ccepting sttes. We sy tht run of Büchi utomton is ccepting if some ccepting stte is visited long the run infinitely often. Since the set of ccepting sttes is finite, some ccepting stte is visited infinitely often is equivlent to the set of ccepting sttes is infinitely often. Definition 11.3 A nondeterministic Büchi utomton (NBA) is tuple A = (Q, Σ, δ, Q 0, F), where Q, Σ, δ, Q 0, nd F re defined s for NFAs. A run of A on n ω-word is n infinite 0 1 sequence ρ = p 0 p 1 p 2..., such tht p i Q for 0 i n, p 0 Q 0 nd p i+1 δ(p i, i ) for every 0 i. Let inf(ρ) e the set {q Q q = p i for infinitely mny i s}, i.e., the set of sttes tht occur in ρ infinitely often. A run ρ is ccepting if inf(ρ) F. An NBA ccepts n ω- word w Σ ω if it hs n ccepting run on w. The lnguge recognized y nd NBA A is the set L ω A = {w Σ ω w is ccepted y A}. We refer to the condition inf(ρ) F s the Büchi condition F. A Büchi utomton is deterministic if it is deterministic when seen s n utomton on finite words. One cn lso define NBAs with ɛ-trnsitions, ut we will not need them. 1 Exmple 11.4 Figure 11.1 shows two Büchi utomt. The utomton on the left is deterministic, nd recognizes ll ω-words over the lphet {, } tht contin infinitely mny occurrences of. So, for instnce, A ccepts ω, ω, () ω, or ( 100 ) ω, ut not ω or 100 ω. To prove tht this is indeed the lnguge we show tht every ω-word contining infinitely mny s is ccepted y A, nd tht every word ccepted y A contins infinitely mny s. For the first prt, oserve tht 1 Notice tht the definition of NBA-ɛ requires some cre, ecuse infinite runs contining only finitely mny non-ɛ trnsitions re never ccepting, even if they visit some ccepting stte infinitely often.

215 11.2. BÜCHI AUTOMATA 215 immeditely fter reding ny the utomton A lwys visits its (only) ccepting stte (ecuse ll trnsitions leled y led to it); therefore, when A reds n ω-word contining infinitely mny s it visits its ccepting stte infinitely often, nd so it ccepts. For the second prt, if w is ccepted y A, then there is run of A on w tht visits the ccepting stte infinitely often. Since ll trnsitions leding to the ccepting stte re leled y, the utomton must red infinitely mny s during the run, nd so w contins infinitely mny s., Figure 11.1: Two Büchi utomt The utomton on the right of the figure is not deterministic, nd recognizes ll ω-words over the lphet {, } tht contin finitely mny occurrences of. The proof is similr. Exmple 11.5 Figure 11.2 shows three further Büchi utomt over the lphet {,, c}., c, c c,, c, c,, c, c c c Figure 11.2: Three further Büchi utomt

216 216 CHAPTER 11. CLASSES OF ω-automata AND CONVERSIONS The top-left utomton recognizes the ω-words in which for every occurrence of there is lter occurrence of. So, for instnce, the utomton ccepts () ω, c ω, or (c) ω, ut not c ω or (c) ω. The top right utomton recognizes the ω-words tht contin finitely mny occurrences of, or infinitely mny occurrences of nd infinitely mny occurrences of. Finlly, the utomton t the ottom recognizes the ω-words in which etween every occurrence of nd the next occurrence of c there is t most one occurrence of ; more precisely, for every two numers i < j, if the letter t position i is n nd the first occurrence of c fter i is t position j, then there is t most one numer i < k < j such tht the letter t position k is From ω-regulr expressions to NBAs nd ck We present lgorithms for converting n ω-regulr expression into NBA, nd vice vers. This provides first snity check for NBAs s dt structure: they cn represent exctly the ω-regulr lnguges. From ω-regulr expressions to NBAs. We proceed y induction on the structure of the ω- regulr expression. Recll tht for every regulr expression r we cn construct n NFA-ɛ A r with unique initil stte, unique finl stte, no trnsition leding to the initil stte, nd no trnsition leving the finl stte. We cn then trnsform this utomton into NFA stisfying the sme property. An NBA for r ω is otined y dding to the NFA for r new trnsitions leding from the finl stte to the trgets of the trnsitions leving the initil stte, s shown t the top of Figure??. An NBA for r s is otined y merging sttes s shown in the middle of the figure. Finlly, n NBA for s 1 + s 2 is otined y merging the initil sttes of the NBAs for s 1 nd s 2 s shown t the ottom. Notice tht we lwys otin NBA with one initil stte nd no trnsitions leding into it. From NBAs to ω-regulr expressions. Let A = (Q, Σ, δ, Q 0, F) e NBA. For every two sttes q, q Q, let A q q e the NFA (not the NBA!) otined from A y chnging the set of initil sttes to {q} nd the set of finl sttes to {q }. Using lgorithm NFAtoRE we cn construct regulr expression s q q such tht L(A q q ) = L(s q q ). In fct, it is even etter to construct regulr expression rq q whose lnguge contins the word ccepted y L(A q q ) y mens of runs tht visit q exctly once (how to use NFAtoRE for this is left s little exercise). We use these expressions to compute n ω-regulr expression for L ω A. For every ccepting stte q F, let L q L ω A e the set of ω-words w such tht some run of A on w visits the stte q infinitely often. We hve L ω A = q F L q. Every word w L q cn e split into n infinite sequence w 1 w 2 w 3... of finite, nonempty words, where w 1 is the word red y A until it visits q for the first time, nd for every i > 1 w i is the word red y the utomton etween the i-th nd the (i + 1)-th visits to q. It follows w 1 L(rq q 0 ), nd w i L(r q q) for every i > 1. So we hve L q = L ω ( r q q0 ( r q q ) ω ), nd therefore

217 11.2. BÜCHI AUTOMATA n n... n NFA for r NBA for r ω NFA for r NBA for r s NBA for s NBA for s 1 NBA for s 2 NBA for s 1 + s 2 Figure 11.3: From ω-regulr expressions to Büchi utomt

218 218 CHAPTER 11. CLASSES OF ω-automata AND CONVERSIONS rq q ( ) q ω 0 rq q F is the ω-regulr expression we re looking for. Exmple 11.6 Consider the top right NBA of Figure 11.2, shown gin in Figure 11.4., c 1, c 0 2, c,, c Figure 11.4: An NBA ( ) We hve to compute r0 1 r 1 ω ( ) 1 + r 2 0 r 2 ω. 2 Using NFAtoRE nd simplifying we get r0 1 = ( + + c) ( + c) r0 2 = ( + + c) r1 1 = ( + c) r2 2 = + ( + c)( + + c) nd (fter some simplifictions) we otin the ω-regulr expression ( + + c) ( + c) ω + ( + + c) ( + ( + c)( + + c) ) ω Non-equivlence of NBA nd DBA Unfortuntely, DBAs do not recognize ll ω-regulr lnguges, nd so they do not hve the sme expressive power s NBAs. We show tht the lnguge of ω-words contining finitely mny s is not recognized y ny DBA. Intuitively, the NBA for this lnguge guesses the lst occurrence of, nd this guess cnnot e determinized using only finite numer of sttes.

219 11.3. GENERALIZED BÜCHI AUTOMATA 219 Proposition 11.7 The lnguge L = (+) ω, (i.e., the lnguge of ll ω-words in which occurs only finitely often) is not recognized y ny DBA. Proof: Assume tht L = L ω A, for some DBA A = ({, }, Q, q 0, δ, F), nd extend δ to ˆδ: Q {, } Q y ˆδ(q, ɛ) = q nd ˆδ(q, w) = δ(ˆδ(q, w), ). Consider the ω-word w 0 = ω. Since w 0 L, the utomton A hs n ccepting run on w 0, nd so ˆδ(q 0, u 0 ) F for some finite prefix u 0 of w 0. Consider now w 1 = u 0 ω. We hve w 1 L, nd so A hs n ccepting run on w 1, which implies ˆδ(q 0, u 0 u 1 ) F for some finite prefix u 0 u 1 of w 1. In similr fshion we continue constructing finite words u i such tht ˆδ(q 0, u 0 u 1... u i ) F. Since Q is finite, there re indices 0 i < j such tht ˆδ(q 0, u 0 u 1,... u i ) = ˆδ(q 0, u 0 u 1... u i... u j ). It follows tht A hs n ccepting run on u 0 u 1... u i ( u i+1... u j 1 u j ) ω. But this word hs infinitely mny occurrences of, so it does not elong to L. Note tht the L = (( + ) ) ω (the set of infinite words in which occurs infinitely often) is ccepted y the DBA on the left of Figure Generlized Büchi utomt Generlized Büchi utomt re n extension of Büchi utomt tht is convenient for some constructions (for instnce, intersection). A generlized Büchi utomton (NGA) differs from Büchi utomton in its ccepting condition. A NGA hs collection of sets of ccepting sttes F = {F 0,..., F m 1 }, nd run ρ is ccepting if for every set F i F some stte of F i is visited y ρ infinitely often. Formlly, ρ is ccepting if inf(ρ) F i for every i {0,..., m 1}. Ausing lnguge, we spek of the generlized Büchi condition F. Ordinry Büchi utomt correspond to the specil cse m = 1. A NGA with n sttes nd m sets of ccepting sttes cn e trnslted into n NBA with mn sttes. The construction is sed on the following oservtion: run ρ visits ech set of F infinitely if nd only if the following two conditions hold: (1) ρ eventully visits F 0 ; nd (2) for every i {0,..., m 1}, every visit of ρ to F i is eventully followed y lter visit to F i 1, where denotes ddition modulo m. (Between the visits to F i nd F i 1 there cn e ritrrily mny visits to other sets of F.) whose proof is ovious. This suggests to tke for the NBA m copies of the NGA, ut with modifiction: the NBA moves from the i-th to the i 1-th copy whenever it visits stte of F i (i.e., the trnsitions of the i-th copy tht leve stte of F i re redirected from the i-th copy to the (i 1)-th copy). This wy, visiting the ccepting sttes of the first copy infinitely often is equivlent to visiting the ccepting sttes of ech copy infinitely often.

220 220 CHAPTER 11. CLASSES OF ω-automata AND CONVERSIONS More formlly, the sttes of the NBA re pirs [q, i] where q is stte of the NGA nd i {0,..., m 1}. Intuitively, [q, i] is the i-th copy of q. If q F i then the successors of [q, i] re sttes of the i-th copy, nd otherwise sttes of the (i 1)-th copy. NGAtoNBA(A) Input: NGA A = (Q, Σ, Q 0, δ, F), where F = {F 1,..., F m } Output: NBA A = (Q, Σ, δ, Q 0, F ) 1 Q, δ, F ; Q 0 {[q 0, q 0 Q 0 } 2 W Q 0 3 while W do 4 pick [q, i] from W 5 dd [q, i] to Q 6 if q F 0 nd i = 0 then dd [q, i] to F 7 for ll Σ, q δ(q, ) do 8 if q F i then 9 if [q, i] Q then dd [q, i] to W 10 dd ([q, i],, [q, i]) to δ 11 else /* q F i */ 12 if [q, i Q then dd [q, i to W 13 dd ([q, i],, [q, i ) to δ 14 return (Q, Σ, δ, Q 0, F ) Exmple 11.8 Figure 11.5 shows NGA over the lphet {, } on the left, nd the NBA otined y pplying NGAtoNBA to it on the right. The NGA hs two sets of ccepting sttes, F 0 = {q} nd F 1 = {r}, nd so its ccepting runs re those tht visit oth q nd r infinitely often. It is esy to see tht the utomton recognizes the ω-words contining infinitely mny occurrences of nd infinitely mny occurrences of. The NBA on the right consists of two copies of the NGA: the 0-th copy (pink) nd the 1-st copy (lue). Trnsitions leving [q, re redirected to the lue copy, nd trnsitins leving [r, re redirected to the pink copy. The only ccepting stte is [q, Other clsses of ω-utomt We hve seen tht not every NBA is equivlent to DBA; i.e., there is determiniztion procedure for Büchi utomt. This rises the question whether there exists other clsses of utomt for which determiniztion procedure exists. As we shll see, the nswer is yes, ut the first determinizle clsses we find will hve other prolems, nd so this section cn e seen s quest for utomt clsses stisfying more nd more properties.

221 11.4. OTHER CLASSES OF ω-automata 221 q r F = { {q}, {r} } q, 0 q, 1 r, 0 r, 1 Figure 11.5: A NGA nd its corresponding NBA Co-Büchi Automt Like Büchi utomton, (nondeterministic) co-büchi utomton (NCA) hs set F of ccepting sttes. However, run ρ of NCA is ccepting if it only visits sttes of F finitely often. Formlly, ρ is ccepting if inf(ρ) F =. So run of NCA is ccepting iff it is not ccepting s run of NBA (this is the reson for the nme co-büchi ). We show tht co-büchi utomt cn e determinized. We fix n NCA A = (Q, Σ, δ, Q 0, F) with n sttes, nd use Figure 11.6 s running exmple. We construct DCA B such tht L ω B) = L ω A in q r Figure 11.6: Running exmple for the determiniztion procedure three steps: 1. We define mpping dg tht ssigns to ech w Σ ω directed cyclic grph dg(w). 2. We prove tht w is rejected y A iff dg(w) contins only finitely mny rekpoints. 3. We construct DCA B which ccepts w if nd only if dg(w) contins finitely mny rekpoints. Intuitively, dg(w) is the result of undling together ll the runs of A on the word w. Figure 11.7 shows the initil prts of dg( ω ) nd dg(() ω ). Formlly, let w = σ 1 σ 2... e word of Σ ω.

222 222 CHAPTER 11. CLASSES OF ω-automata AND CONVERSIONS q, 0 q, 1 q, 2 q, 3 q, 4 r, 1 r, 3 r, 4 q, 0 q, 1 q, 2 q, 3 q, 4 r, 1 r, 3 Figure 11.7: The (initil prts of) dg( ω ) nd dg(() ω ) The directed cyclic grph dg(w) hs nodes in Q IN nd edges lelled y letters of Σ, nd is inductively defined s follows: dg(w) contins node q, 0 for every initil stte q Q 0. If dg(w) contins node q, i nd q δ(σ i+1, q), then dg(w) lso contins node q, i + 1 nd n edge q, i σ i+1 q, i + 1. dg(w) contins no other nodes or edges. σ 1 σ 2 Clerly, q 0 q 1 q 2 is run of A if nd only if q 0, 0 σ 1 q 1, 1 σ 2 q 2, 2 is pth of dg(w). Moreover, A ccepts w if nd only if no pth of dg(w) visits ccepting sttes infinitely often. We prtition the nodes of dg(w) into levels: the i-th level contins ll the nodes of dg(w) of the form q, i. One could e tempted to think tht the ccepting condition some pth of dg(w) only visits ccepting sttes finitely often is equivlent to only finitely mny levels of dg(w) contin ccepting sttes, ut dg( ω ) shows tht this is not true: even though ll its pths visit ccepting sttes only finitely often, infinitely mny levels (in fct, ll ll levels i 3) contin ccepting sttes. For this reson we introduce the set of rekpoint levels of the grph dg(w), inductively defined s follows: The 0-th level of dg(w) is rekpoint. If level l is rekpoint, then the next level l > l such tht every pth etween nodes of l nd l visits n ccepting stte is lso rekpoint. We clim tht some pth of dg(w) only visits ccepting sttes finitely often is equivlent to the set of rekpoint levels of dg(w) is finite. If the rekpoint set is infinite, then clerly

223 11.4. OTHER CLASSES OF ω-automata 223 dg(w) contins t lest n infinite pth, nd moreover ll infinite pths visit ccepting sttes infinitely often. If the rekpoint set is finite, let i e the lrgest rekpoint. If dg(w) is finite, we re done. If dg(w) is infinite, then for every j > i there is pth π j from level i to level j tht does not visit ny ccepting stte. The pths {π j } j>i uild n cyclic grph of ounded degree, which therefore must contin n infinite pth π y Königs lemm. But oviously π never visits ny ccepting stte, nd we re done. Now, if we were le to tell tht level is rekpoint just y exmining it, we would e done: we would tke the set of ll possile levels s sttes of the DCA (i.e., the powerset of Q, s in the powerset construction for determiniztion of NFAs), the possile trnsitions etween levels s trnsitions, nd the rekpoints s ccepting sttes. The run of this utomton on w would e nothing ut n encoding of dg(w), nd it would e ccepting iff it contins only finitely mny rekpoints, s required y the co-büchi cceptnce condition. However, the level does not contin enough informtion for tht. The solution to this prolem is to dd informtion to the sttes: we tke for the sttes of the DCA pirs [P, O], where O P Q, with the following intended mening: P is the set of sttes of level, nd q O iff q is the endpoint of pth tht strts t the lst rekpoint nd hs not yet visited ny ccepting stte. We cll O the set of owing sttes (sttes tht owe visit to the ccepting sttes). To gurntee tht O indeed hs this intended mening, we define the DCA B = ( Q, Σ, δ, q 0, F) s follows: The initil stte is the pir [{q 0 }, ] if q 0 F, nd the pir [{q 0 }, {q 0 }] otherwise. The trnsition reltion is given y δ([p, O], ) = [P, O ], where P = δ(p, ), nd : if O, i.e., if the current set of owing sttes is nonempty, then O = δ(o, ) \ F; if O =, (i.e., if the current level is rekpoint, nd the utomton must strt serching for the next one) then O = δ(p, ) \ F, i.e., ll non-finl sttes of the next level ecome owing. The ccepting sttes re those t which rekpoint is reched, i.e. [P, O] F is ccepting iff O =. With this definition, run is ccepting iff it contins infinitely mny rekpoints. The lgorithm for the construction is:

224 224 CHAPTER 11. CLASSES OF ω-automata AND CONVERSIONS NCAtoDCA(A) Input: NCA A = (Q, Σ, δ, Q 0, F) Output: DCA B = ( Q, Σ, δ, q 0, F) with L ω A = B 1 Q, δ, F ; if q 0 F then q 0 [q 0, ] else q 0 [{q 0 }, {q 0 }] 2 W { q 0 } 3 while W do 4 pick [P, O] from W; dd [P, O] to Q 5 if P = then dd [P, O] to F 6 for ll Σ do 7 P = δ(p, ) 8 if O then O δ(o, ) \ F else O δ(p, ) \ F 9 dd ([P, O],, [P, O ]) to δ 10 if [P, O ] Q then dd [P, Q ] to W Figure 11.8 shows the result of pplying the lgorithm to our running exmple. The NCA is t the top, nd the DCA elow it on the left. On the right we show the DFA otined y pplying the powerset construction. Oserve tht it does not yield correct result: the resulting NCA ccepts for instnce the word ω, which is not ccepted y the originl NCA. For the complexity, oserve tht q r {q}, {q} {q, r}, {q} {q}, q {q, r},,, Figure 11.8: NCA of Figure 12.5 (top), DCA (lower left), nd DFA (lower right) the numer of sttes of the DCA is ounded y the numer of pirs [P, O] such tht O P Q. For

225 11.4. OTHER CLASSES OF ω-automata 225 every stte q Q there re three mutully exclusive possiilities: q O, q P \ O, nd q Q \ P. So if A hs n sttes then B hs t most 3 n sttes. Unfortuntely, co-büchi utomt do not recognize ll ω-regulr lnguges. In prticulr, we clim tht no NCA recognizes the lnguge L of ω-words over {, } contining infinitely mny s. To see why, ssume some NCA recognizes L. Then, since every NCA cn e determinized, some DCA A recognizes L. This utomton A, interpreted s DBA insted of DCA, recognizes the complement of L: indeed, word w is recognized y the DCA A iff the run of A on w visits ccepting sttes only finitely often iff w is not recognized y the DBA A. But the complement of L is ( + ) ω, which y Proposition 11.7 is not ccepted y ny DBA. We hve reched contrdiction, which proves the clim. So we now sk whether there is clss of ω-utomt tht (1) recognizes ll ω-regulr lnguges nd (2) hs determiniztion procedure Muller utomt A (nondeterministic) Muller utomton (NMA) hs collection {F 0,..., F m 1 } of sets of ccepting sttes. A run ρ is ccepting if the set of sttes ρ visits infinitely often is equl to one of the F i s. Formlly, ρ is ccepting if inf(ρ) = F i for some i {0,..., m 1}. We spek of the Muller condition {F 0,..., F m 1 }. NMAs hve the nice feture tht ny oolen comintion of predictes of the form stte q is visited infinitely often cn e formulted s Muller condition. It suffices to put in the collection ll sets of sttes for which the predicte holds. For instnce, the condition (q inf(ρ)) (q inf(ρ)) corresponds to the Muller condition contining ll sets of sttes F such tht q F nd q F. In prticulr, the Büchi nd generlized Büchi conditions re specil cses of the Muller condition (s well s the Rin nd Street conditions introduced in the next sections). The ovious disdvntge is tht the trnsltion of Büchi condition into Muller condition involves n exponentil low-up: Büchi utomton with sttes Q = {q 0,..., q n } nd Büchi condition {q n } is trnsformed into n NMA with the sme sttes nd trnsitions, ut with Muller condition {F Q q n F}, collection contining 2 n sets of sttes. Deterministic Muller utomt recognize ll ω-regulr lnguges. The proof of this result is complicted, nd we omit it here. Theorem 11.9 (Sfr) Any NBA with n sttes cn e effectively trnsformed into DMA of size n O(n). We cn esily give deterministic Muller utomton for the lnguge L = ( + ) ω, which, s shown in Proposition 11.7, is not recognized y ny DBA. The utomton is with Muller condition { {q 1 } }. The ccepting runs re the runs ρ such tht inf(ρ) = {q 1 }, i.e., the runs visiting stte 1 infinitely often nd stte q 0 finitely often. Those re the runs tht initilly move etween sttes q 0 nd q 1, ut eventully jump to q 1 nd never visit q 0 gin. It is esy to see tht these runs ccept exctly the words contining finitely mny occurrences of. We finlly show tht n NMA cn e trnslted into NBA, nd so tht Muller nd Büchi utomt hve the sme expressive power. First, it is cler tht putting two NBAs side y side

226 226 CHAPTER 11. CLASSES OF ω-automata AND CONVERSIONS q 0 q 1 (i.e., tking the unions of the sets of sttes, initil sttes, finl sttes, nd trnsitions) yields n NBA recognizing the union of the lnguges.. Figure 11.9: Union for NBAs We proceed s follows. Given Muller utomton A = (Q, Σ, Q 0, δ, {F 0,..., F m 1 }), it is esy to see tht L ω A = m 1 i=0 L ωa i, where A i = (Q, Σ, Q 0, δ, {F i }). So we proceed in three steps: first, we convert the NMA A i into NGA A i ; then we convert A i into NBA A i using NGAtoNBA() nd, finlly, we construct A = m 1 i=0 A i using the lgorithm for union of NFAs. For the first step, we oserve tht, since n ccepting run ρ of A i stisfies inf(ρ) = F i, from some point on the run only visits sttes of F i. In other words, ρ consists of n initil finite prt, sy ρ 0, tht my visit ll sttes, nd n infinite prt, sy ρ 1, tht only visits sttes of F i. The ide for the construction for A i is to tke two copies of A i. The first one is full copy, while the second one only contins copies of the sttes of F i. For every trnsition [q, [q, of the first copy such tht q F i there is nother trnsition [q, [q, leding to the twin rother [q,. Intuitively, A i simultes ρ y executing ρ 0 in the first copy, nd ρ 1 in the second. The condition tht ρ 1 must visit ech stte of F i infinitely often is enforced s follows: if F i = {q 1,..., q k }, then we tke for A i the generlized Büchi condition { {[q 1, },..., {q k, } }. Exmple Figure shows NMA A = (Q, Σ, δ, Q 0, F) where F = { {q}, {r} }. While A is syntcticlly identicl to the NGA of Figure 11.5, we now interpret F s Muller condition: run

227 11.4. OTHER CLASSES OF ω-automata 227 ρ is ccepting if inf(ρ) = {q} or inf(ρ) = {r}. In other words, n ccepting run ρ eventully moves to q nd stys there forever, or eventully moves to r nd stys there forever. It follows tht A ccepts the ω-words tht contin finitely mny s or finitely mny s. The top-right prt of the figure shows the two NGAs A 0, A 1 defined ove. Since in this prticulr cse F 0 nd F 1 only contin singleton sets, A 0 nd A 1 re in fct NBAs, i.e., we hve A 0 = A 0 nd A 1 = A 1. The ottom-right prt shows the finl NBA A = A 0 A 1. Formlly, the lgorithm crrying out the first step of the construction looks s follows: NMA1toNGA(A) Input: NMA A = (Q, Σ, Q 0, δ, {F}) Output: NGA A = (Q, Σ, Q 0, δ, F ) 1 Q, δ, F 2 Q 0 {[q 0, q 0 Q 0 } 3 W Q 0 4 while W do 5 pick [q, i] from W; dd [q, i] to Q 6 if q F nd i = 1 then dd {[q, } to F 7 for ll Σ, q δ(q, ) do 8 if i = 0 then 9 dd ([q,,, [q, ) to δ 10 if [q, Q then dd [q, to W 11 if q F then 12 dd ([q,,, [q, ) to δ 13 if [q, Q then dd [q, to W 14 else /* i = 1 */ 15 if q F then 16 dd ([q,,, [q, ) to δ 17 if [q, Q then dd [q, to W 18 return (Q, Σ, q 0, δ, F ) Complexity. Assume Q contins n sttes nd F contins m ccepting sets. Ech of the NGAs A 0,..., A m 1 hs t most 2n sttes, nd n cceptnce condition contining t most m cceptnce sets. So ech of the NBAs A 0,..., A m 1 hs t most 2n2 sttes, nd the finl NBA hs t most 2n 2 m + 1 sttes. Oserve in prticulr tht while the conversion from NBA to NMA involves possily exponentil low-up, the conversion NMA to NBA does not. It cn e shown tht the exponentil low-up in the conversion from NBA to NMA cnnot e voided, which leds to the next step in our quest: is there clss of ω-utomt tht (1) recognizes ll ω-regulr lnguges, (2) hs determiniztion procedure, nd (3) hs polynomil conversion lgorithms to nd from NBA.

228 228 CHAPTER 11. CLASSES OF ω-automata AND CONVERSIONS Rin utomt The cceptnce condition of nondeterministic Rin utomton (NRA) is set of pirs F = { F 0, G 0,..., F m, G m }, where the F i s nd G i s re sets of sttes. A run ρ is ccepting if there is pir F i, G i such tht ρ visits some stte of F i infinitely often nd ll sttes of G i finitely often. Formlly, ρ is ccepting if there is i {1,..., m} such tht inf(ρ) F i nd inf(ρ) G i =. A Büchi utomton cn e esily trnsformed into Rin utomton nd vice vers, without ny exponentil low-up. NBA NRA. A Büchi condition {q 1,..., q k } corresponds to the Rin condition {({q 1 }, ),..., ({q n }, )}. NRA NBA. Given Rin utomton A = (Q, Σ, Q 0, δ, { F 0, G 0,..., F m 1, G m 1 }), it is esy to see tht, s for Muller utomt, we hve L ω A = m 1 i=0 L ωa i, where A i = (Q, Σ, Q 0, δ, { F i, G i }). In this cse we directly trnslte ech A i into n NBA. Since n ccepting run ρ of A i stisfies inf(ρ) G i =, from some point on the run only visits sttes of Q i \ G i. So ρ consists of n initil finite prt, sy ρ 0, tht my visit ll sttes, nd n infinite prt, sy ρ 1, tht only visits sttes of Q \ G i. Agin, we tke two copies of A i. Intuitively, A i simultes ρ y executing ρ 0 in the first copy, nd ρ 1 in the second. The condition tht ρ 1 must visit some stte of F i infinitely often is enforced y tking F i s Büchi condition. Exmple Figure cn e reused to illustrte the conversion of Rin into Büchi utomton. Consider the utomton on the left, ut this time with Rin ccepting condition { F 0, G 0, F 1, G 1 }, where F 0 = {q} = G 1, nd G 0 = {r} = F 1. Then the utomton ccepts the ω-words tht contin finitely mny s or finitely mny s. The Büchi utomt A 0, A 1 re s shown t the top-right prt, ut now insted of NGAs they re NBAs with ccepting sttes [q, nd [r,, respectively. The finl NBA is exctly the sme one. For the complexity, oserve tht ech of the A i hs t most 2n sttes, nd so the finl Büchi utomton hs t most 2nm + 1 sttes. The proof tht NRAs re s expressive s DRAs is complicted. Since NRAs nd NBAs re eqully expressive y the conversions ove, it suffices to show tht DRAs re s expressive s NBAs. We only present the result, without proof. Theorem (Sfr) Any NBA with n sttes cn e effectively trnsformed into DRA of size n O(n) nd O(n) ccepting pirs. The ccepting condition of Rin utomt is not closed under negtion. Recll the condition: there is i {1,..., m} such tht inf(ρ) F i nd inf(ρ) G i = The negtion is of the form for every i {1,..., m}: inf(ρ) F i = or inf(ρ) G i This is clled the Streett condition. We finish the chpter with short discussion of Street utomt.

229 11.4. OTHER CLASSES OF ω-automata 229 Streett utomt The finl clss of ω-utomt we consider re Streett utomt. In Streett utomton, the cceptnce condition is gin set of pirs { F 1, G 1,..., F m, G m }, where F i, G i re sets of sttes. A run ρ is ccepting if for every pir F i, G i, if ρ visits some stte of F i infinitely often, then it lso visits some stte of G i infinitely often. Formlly, run ρ is ccepting if for every i {1,..., m} inf(ρ) F i implies inf(ρ) G i (equivlently, if inf(ρ) F i = or inf(ρ) G i for every i {1,..., m}). The reder hs proly oserved tht this is the dul of Rin s condition: run stisfies the Streett condition { F 1, G 1,..., F m, G m } if nd only if it does not stisfy the Rin condition { F 1, G 1,..., F m, G m }. A Büchi utomton cn e esily trnsformed into Streett utomton nd vice vers. However, the conversion from Streett to Büchi is exponentil. NBA NSA. A Büchi condition {q 1,..., q k } corresponds to the Streett condition { Q, {q 1,..., q k } }. NSA NBA. We cn trnsform into n NBA y following the pth NSA NMA NBA. This yields for NSA with n sttes n NBA with 2n 2 2 n sttes. It cn e shown tht the exponentil low-up is unvoidle; in other words, Streett utomt cn e exponentilly more succinct thn Büchi utomt. Exmple Let Σ = {0, 1, 2}. For n 1, we represent n infinite sequence x 1, x 2,... of vectors of dimension n with components in Σ y the ω-word x 1 x 2... over Σ n. Let L n e the lnguge in which, for ech component i {1,..., n}, x j (i) = 1 for infinitely mny j s if nd only if x k (i) = 2 for infinitely mny k s. It is esy to see tht L n cn e ccepted y NSA with 3n sttes nd 2n ccepting pirs, ut cnnot e ccepted y ny NBA with less thn 2 n sttes. Exercises Exercise 100 Recll tht finite set of finite words is lwys regulr lnguge (see Exercise 9). Prove or disprove: finite set of ω-words is lwys n ω-regulr lnguge. Exercise 101 Construct Büchi utomt nd ω-regulr expressions, s smll s possile, recognizing the following lnguges over the lphet {,, c}. (1) {w {,, c} ω {, } inf (w)} (2) {w {,, c} ω {, } = inf (w)} (3) {w {,, c} ω {, } inf (w)} (4) {w {,, c} ω {,, c} = inf (w)} (5) {w {,, c} ω if inf (w) then {, c} in f (w)}

230 230 CHAPTER 11. CLASSES OF ω-automata AND CONVERSIONS Exercise 102 The limit of lnguge L Σ, denoted y lim(l), is the ω-lnguge defined s follows: w lim(l) iff infinitely mny prefixes of w re words of L. For exmple, the limit of () is {() ω }. (1) Determine the limit of the following regulr lnguges over {, }: (i) (+) ; (ii) (+) ; (iii) the set of words contining n even numer of s; (iv). (2) Prove: An ω-lnguge is recognizle y deterministic B uchi utomton iff it is the limit of regulr lnguge. (3) Exhiit non-regulr lnguge whose limit is ω-regulr. (4) Exhiit non-regulr lnguge whose limit is not ω-regulr. Exercise 103 Let L 1 = () ω, nd let L 2 e the lnguge of ll words contining infinitely mny s nd infinitely mny s (oth lnguges over the lphet {, }). (1) Show tht no DBA with t most two sttes recognizes L 1 or L 2. (2) Exhiit two different DBAs with three sttes recognizing L 1. (3) Exhiit six different DBAs with three sttes recognizing L 2. Exercise 104 Find ω-regulr expressions (the shorter the etter) for the following lnguges: (1) {w {, } ω k is even for ech suword k of w} (2) {w {, } ω w hs no occurrence of } Exercise 105 We dpt the reduction lgorithm for NFAs of Chpter 3 to NBAs nd NGAs. Recll tht given n NFA A = (Q, Σ, δ, Q 0, F), the lgorithm computes the reltion CSR, the corsest stle refinement of the equivlence reltion {F, Q \ F}, nd returns the quotient A/CSR. (1) Let A e n NBA, nd let CSR, the corsest stle refinement of the equivlence reltion {F, Q \ F}. Show tht L ω (A) = L ω (A/CSR). (2) Modify the reduction lgorithm so tht (1) lso holds for generlized B uchi utomt. Exercise 106 The prity cceptnce condition for ω-utomt is defined s follows. Every stte q of the utomton is ssigned nturl numer n q. A run ρ is ccepting if the numer mx{n s s inf (ρ)} is even. Find prity utomton ccepting the lnguge L = {w {, } ω occurrences of }. w hs exctly two Show tht ech lnguge ccepted y prity utomton is lso ccepted y Rin utomton nd vice vers.

231 11.4. OTHER CLASSES OF ω-automata 231 A 0 A 1 q, 0 r, 0 q, 0 r, 0 q, 1 r, 1 A q r F = {F 0, F 1 } F 0 = { [q, } q, 0 F 1 r, 0 = { [r, } F 0 = {q} F 1 = {r} q, 1 q, 0 r, 0 r, 1 A Figure 11.10: A Muller utomton nd its conversion into NBA

232 232 CHAPTER 11. CLASSES OF ω-automata AND CONVERSIONS

233 Chpter 12 Boolen opertions: Implementtions The list of opertions of Chpter 4 cn e split into two prts, with the the oolen opertions union, intersection, nd complement in the first prt, nd the emptiness, inclusion, nd equlity tests in the second. This chpter dels with the oolen opertions, while the tests re discussed in the next one. Oserver tht we now leve the memership test out. Oserve tht test for ritrry ω-words does not mke sense, ecuse no description formlism cn represent ritrry ω-words. For ω- words of the form w 1 (w 2 ) ω, where w 1, w 2 re finite words, memership in n ω-regulr lnguge L cn e implemented y checking if the intersection of L nd {w 1 (w 2 ) ω } is empty. We provide implementtions for ω-lnguges represented y NBAs nd NGAs. We do not discuss implementtions on DBAs, ecuse they cnnot represent ll ω-regulr lnguges. In Section 12.1 we show tht union nd intersection cn e esily implemented using constructions lredy presented in Chpter 2. The rest of the chpter is devoted to the complement opertion, which is more involved Union nd intersection As lredy oserved in Chpter 2, the lgorithm for union of regulr lnguges represented s NFAs lso works for NBAs nd for NGAs. One might e tempted to think tht, similrly, the intersection lgorithm for NFAs lso works for NBAs. However, this is not the cse. Consider the two Büchi utomt A 1 nd A 2 of Figure The Büchi utomton A 1 A 2 otined y pplying lgorithm IntersNFA(A 1, A 2 ) in pge q 0 q 1 r 0 r 1 Figure 12.1: Two Büchi utomt ccepting the lnguge ω 233

234 234 CHAPTER 12. BOOLEAN OPERATIONS: IMPLEMENTATIONS 80 (more precisely, y interpreting the output of the lgorithm s Büchi utomton) is shown in Figure It hs no ccepting sttes, nd so L ω (A 1 ) = L ω (A 2 ) = { ω }, ut L ω (A 1 A 2 ) =. q 0, r 0 q 1, r 1 Figure 12.2: The utomton A 1 A 2 Wht hppened? A run ρ of A 1 A 2 on n ω-word w is the result of piring runs ρ 1 nd ρ 2 of A 1 nd A 2 on w. Since the ccepting set of A 1 A 2 is the crtesin product of the ccepting sets of A 1 nd A 2, ρ is ccepting if ρ 1 nd ρ 2 simultneously visit ccepting sttes infinitely often. This condition is too strong, nd s result L ω (A 1 A 2 ) cn e strict suset of L ω (A 1 ) L ω (A 2 ). This prolem is solved y mens of the oservtion we lredy mde when deling with NGAs: the run ρ visits sttes of F 1 nd F 2 infinitely often if nd only if the following two conditions hold: (1) ρ eventully visits F 1 ; nd (2) every visit of ρ to F 1 is eventully followed y visit to F 2 (with possily further visits to F 1 in-etween), nd every visit to F 2 is eventully followed y visit to F 1 (with possily further visits to F 1 in-etween). We proceed s in the trnsltion NGA NBA. Intuitively, we tke two copies of the piring [A 1, A 2 ], nd plce them one of top of the other. The first nd second copies of stte [q 1, q 2 ] re clled [q 1, q 2, nd [q 1, q 2, 2], respectively. The trnsitions leving the sttes [q 1, q 2, such tht q 1 F 1 re redirected to the corresponding sttes of the second copy, i.e., every trnsition of the form [q 1, q 2, [q 1, q 2, is replced y [q 1, q 2, [q 1, q 2, 2]. Similrly, the trnsitions leving the sttes [q 1, q 2, 2] such tht q 2 F 2 re redirected to the first copy. We choose [q 01, q 02,, s initil stte, nd declre the sttes [q 1, q 2, such tht q 1 F 1 s ccepting. Exmple 12.1 Figure 12.3 shows the result of the construction for the NBAs A 1 nd A 2 of Figure 12.1, fter removing the sttes tht re not rechle form the initil stte. Since q 0 is not n q 0, r 0, 1 q 1, r 1, 1 q 1, r 1, 2 Figure 12.3: The NBA A 1 ω A 2 for the utomt A 1 nd A 2 of Figure 12.1 ccepting stte of A 1, the trnsition [q 0, r 0, [q 1, r 1, is not redirected. However, since q 1 is

235 12.1. UNION AND INTERSECTION 235 n ccepting stte, trnsitions leving [q 1, r 1, must jump to the second copy, nd so we replce [q 1, r 1, [q 0, r 0, y [q 1, r 1, [q 0, r 0, 2]. Finlly, since r 0 is n ccepting stte of A 2, trnsitions leving [q 0, r 0, 2] must return to the first copy, nd so we replce [q 0, r 0, 2] [q 1, r 1, 2] y [q 0, r 0, 2] [q 1, r 1,. The only ccepting stte is [q 1, r 1,, nd the lnguge ccepted y the NBA is ω. To see tht the construction works, oserve first tht run ρ of this new NBA still corresponds to the piring of two runs ρ 1 nd ρ 2 of A 1 nd A 2, respectively. Since ll trnsitions leving the ccepting sttes jump to the second copy, ρ is ccepting iff it visits oth copies infinitely often, which is the cse iff ρ 1 nd ρ 2 visit sttes of F 1 nd F 2, infinitely often, respectively. Algorithm IntersNBA(), shown elow, returns n NBA A 1 ω A 2. As usul, the lgorithm only constructs sttes rechle from the initil stte. IntersNBA(A 1, A 2 ) Input: NBAs A 1 = (Q 1, Σ, δ 1, Q 01, F 1 ), A 2 = (Q 2, Σ, δ 2, Q 02, F 2 ) Output: NBA A 1 ω A 2 = (Q, Σ, δ, Q 0, F) with L ω (A 1 ω A 2 ) = L ω (A 1 ) L ω (A 2 ) 1 Q, δ, F 2 q 0 [q 01, q 02, 3 W { [q 01, q 02, } 4 while W do 5 pick [q 1, q 2, i] from W 6 dd [q 1, q 2, i] to Q 7 if q 1 F 1 nd i = 1 then dd [q 1, q 2, to F 8 for ll Σ do 9 for ll q 1 δ 1(q 1, ), q 2 δ(q 2, ) do 10 if i = 1 nd q 1 F 1 then 11 dd ([q 1, q 2,,, [q 1, q 2, ) to δ 12 if [q 1, q 2, Q then dd [q 1, q 2, to W 13 if i = 1 nd q 1 F 1 then 14 dd ([q 1, q 2,,, [q 1, q 2, 2]) to δ 15 if [q 1, q 2, 2] Q then dd [q 1, q 2, 2] to W 16 if i = 2 nd q 2 F 2 then 17 dd ([q 1, q 2, 2],, [q 1, q 2, 2]) to δ 18 if [q 1, q 2, 2] Q then dd [q 1, q 2, 2] to W 19 if i = 2 nd q 2 F 2 then 20 dd ([q 1, q 2, 2],, [q 1, q 2, ) to δ 21 if [q 1, q 2, Q then dd [q 1, q 2, to W 22 return (Q, Σ, δ, Q 0, F) There is n importnt cse in which the construction for NFAs cn lso e pplied to NBAs, nmely when ll the sttes of one of the two NBAs, sy A 1 re ccepting. In this cse, the condition

236 236 CHAPTER 12. BOOLEAN OPERATIONS: IMPLEMENTATIONS tht two runs ρ 1 nd ρ 2 on n ω-word w simultneously visit ccepting sttes infinitely often is equivlent to the weker condition tht does not require simultneity: ny visit of ρ 2 to n ccepting stte is simultneous visit of ρ 1 nd ρ 2 to ccepting sttes. It is lso importnt to oserve difference with the intersection for NFAs. In the finite word cse, given NFAs A 1,..., A k with n 1,..., n k sttes, we cn compute n NFA for L(A 1 )... L(A n ) with t most k i=1 n i sttes y repetedly pplying the intersection opertion, nd this construction is optiml (i.e., there is fmily of instnces of ritrry size such tht the smllest NFA for the intersection of the lnguges hs the sme size). In the NBA cse, however, the repeted ppliction of IntersNBA() is not optiml. Since IntersNBA() introduces n dditionl fctor of 2 in the numer of sttes, for L ω (A 1 )... L ω (A k ) it yields n NBA with 2 k 1 n 1... n k sttes. We otin etter construction proceeding s in the trnsltion NGA NBA: we produce k copies of A 1... A k, nd move from the i-th copy to the (i + 1)-th copy when we hit n ccepting stte of A i. This construction yields n NBA with k n 1... n k sttes Complement So fr we hve een le to dpt the constructions for NFAs to NBAs. The sitution is considerly more involved for complement The prolems of complement Recll tht for NFAs complement utomton is constructed y first converting the NFA into DFA, nd then exchnging the finl nd non-finl sttes of the DFA. For NBAs this pproch reks down completely: () The suset construction does not preserve ω-lnguges; i.e, NBA nd the result of pplying the suset construction to it do not necessrily ccept the sme ω-lnguge. The NBA on the left of Figure 12.4 ccepts the empty lnguge. However, the result of pplying the suset construction to it, shown on the right, ccepts ω. Notice tht oth utomt ccept the sme finite words. q 0 q 1 {q 0 } {q 0, q 1 } Figure 12.4: The suset construction does not preserve ω-lnguges () The suset construction cnnot e replced y nother determiniztion procedure, ecuse no such procedure exists: As we hve seen in Proposition 11.7, some lnguges re ccepted y NBAs, ut not y DBAs.

237 12.2. COMPLEMENT 237 (c) The utomton otined y exchnging ccepting nd non-ccepting sttes in given DBA does not necessrily recognize the complement of the lnguge. In Figure 12.1, A 2 is otined y exchnging finl nd non-finl sttes in A 1. However, oth A 1 nd A 2 ccept the lnguge ω. Oserve tht s utomt for finite words they ccept the words over the letter of even nd odd length, respectively. Despite these discourging oservtions, NBAs turn out to e closed under complement. For the rest of the chpter we fix n NBA A = (Q, Σ, δ, Q 0, F) with n sttes, nd use Figure 12.5 s running exmple. Further, we revite infinitely often to i.o.. We wish to uild n utomton q r Figure 12.5: Running exmple for the complementtion procedure A stisfying: no pth of dg(w) visits ccepting sttes of A i.o. if nd only if some run of w in A visits ccepting sttes of A i.o. We give summry of the procedure. First, we define the notion of rnking. For the moment it suffices to sy tht rnking of w is the result of decorting the nodes of dg(w) with numers. This cn e done in different wys, nd so, while word w hs one single dg dg(w), it my hve mny rnkings. The essentil property of rnkings will e: no pth of dg(w) visits ccepting sttes of A i.o. if nd only if for some rnking R(w) every pth of dg(w) visits nodes of odd rnk i.o. In the second step we profit from the determiniztion construction for co-büchi utomt. Recll tht the construction mps dg(w) to run ρ of new utomtonsuch tht: every pth of dg(w) visits ccepting sttes of A i.o. if nd only if ρ visits ccepting sttes of the new utomton i.o. We pply the sme construction to mp every rnking R(w) to run ρ of new utomton B such tht every pth of dg(w) visits nodes of odd rnk i.o. (in R(w)) if nd only if the run ρ visits sttes of B i.o.

238 238 CHAPTER 12. BOOLEAN OPERATIONS: IMPLEMENTATIONS This immeditely implies L ω (B) = L ω (A). However, the utomton B my in principle hve n infinite numer of sttes! In the finl step, we show tht finite suutomton A of B lredy recognizes the sme lnguge s B, nd we re done Rnkings nd rnking levels Recll tht, given w l ω, the directly cyclic grph dg(w) is the result of undling together the runs of A on w. A rnking of dg(w) is mpping R(w) tht ssocites to ech node of dg(w) nturl numer, clled rnk, stisfying the following two properties: () the rnk of node is greter thn or equl to the rnk of its children, nd () the rnk of n ccepting node is even q 0, 0 q 0, 1 q 0, 2 q 0, 3 q 0, q 1, 1 q 1, 3 q 1, q 0, 0 q 0, 1 q 0, 2 q 0, 3 q 0, q 1, 1 q 1, 3 Figure 12.6: Rnkings for dg( ω ) nd dg(() ω ) The rnks of the nodes in n infinite pth form non-incresing sequence, nd so there is node such tht ll its (infinitely mny) successors hve the sme rnk; we cll this numer the stle rnk of the pth. Figure 12.6 shows rnkings for dg( ω ) nd dg(() ω ). Both hve one single infinite pth with stle rnk 1 nd 0, respectively. We now prove the fundmentl property of rnk ings: Proposition 12.2 No pth of dg(w) visits ccepting nodes of A i.o. if nd only if for some rnking R(w) every infinite pth of dg(w) visits nodes of odd rnk i.o. Proof: If ll infinite pths of rnking R hve odd stle rnk, then ech of them contins only finitely mny nodes with even rnk. Since ccepting nodes hve even rnks, no pth visits ccepting nodes i.o.

239 12.2. COMPLEMENT 239 For the other direction, ssume tht no pth of dg(w) visits ccepting nodes of A i.o. Give ech ccepting node q, l the rnk 2k, where k is the mximl numer of ccepting nodes in the pths strting t q, l, nd give non-ccepting nodes rnk 2k + 1, where 2k is the mximl rnk of its descendnts with even rnk. In the rnking so otined every infinite pth visits nodes of even rnk only finitely often, nd therefore it visits nodes of odd rnk i.o. Recll tht the i-th level of dg(w) is defined s the set of nodes of dg(w) of the form q, i. Let R e the set of ll rnking levels. Any rnking r of dg(w) cn e decomposed into n infinite sequence lr 1, lr 2,... of level rnkings y defining lr i (q) = r( q, i ) if q, i is node of dg(w), nd lr i (q) = otherwise. For exmple, if we represent level rnking lr of our running exmple y the column vector [ ] lr(q0 ), lr(q 1 ) then the rnkings of Figure 12.6 correspond to the sequences [ 2 ] [ ] [ 1 ] [ 1 ] ω [ 1 2 ] [ 1 0 ] ([ 0 0 ] [ 0 0 For two level rnkings lr nd lr nd letter Σ, we write lr lr if for every q Q: lr (q ) = iff no q Q stisfies q q, nd lr(q) lr (q ) for every q Q stisfying q q A (possily infinite) complement utomton We construct n NBA B n infinite numer of sttes (nd mny initil sttes) whose runs on n ω-word w re the rnkings of dg(w). The utomton ccepts rnking R iff every infinite pth of R visits nodes of odd rnk i.o. We strt with n utomton without ny ccepting condition: The sttes re ll the possile rnking levels. The initil sttes re the levels rl n defined y: rl n (q 0 ) = n, nd lr n (q) = for every q q 0. The trnsitions re the triples (rl,, rl ), where rl nd rl re rnking levels, Σ, nd rl rl holds. The runs of this utomton on w clerly correspond to the rnkings of dg(w). Now we pply the sme construction we used for determiniztion of co-büchi utomt. We decorte the rnking ]) ω

240 240 CHAPTER 12. BOOLEAN OPERATIONS: IMPLEMENTATIONS levels with set of owing sttes, nmely those tht owe visit to stte of odd rnk, nd tke s ccepting sttes the rekpoints i.e., the levels with n empty set of owing sttes. We get the Büchi utomton B: The sttes re ll pirs [rl, O], where rl is rnking level nd O is suset of the sttes q for which rl(q) N. The initil sttes re ll pirs of the form [lr, {q 0 }] where lr(q 0 ) is n even numer nd lr 0 (q) = for every q q 0, nd of the form [lr, ], where lr(q 0 ) is n odd numer nd lr 0 (q) = for every q q 0. The trnsitions re the triples [rl, O]δ[rl, O ] if lr lr nd O nd O = {q δ(o, ) lr (q ) is even }, or O = nd O = {q Q lr (q ) is even }. The ccepting sttes (rekpoints) re the pirs [rl, ]. B ccepts rnking iff it contins infinitely mny rekpoints. As we sw in the construction for co-büchi utomt, this is the cse iff every infinite pth of dg(w) visits nodes of odd rnk i.o., nd so iff A does not ccept w. The remining prolems with this utomton re tht its numer of sttes is infinite, nd tht it hs mny initil sttes. Both cn e solved y proving the following ssertion: there exists numer k such tht for every word w, if dg(w) dmits n odd rnking, then it dmits n odd rnking whose initil node q 0, 0 hs rnk k. (Notice tht, since rnks cnnot increse long pths, every node hs rnk t most k.) If we re le to prove this, then we cn eliminte ll sttes corresponding to rnking levels in which some node is mpped to numer lrger thn k: they re redundnt. Moreover, the initil stte is now fixed: it is the level rnking tht mps q 0 to k nd ll other sttes to. Proposition 12.3 Let n e the numer of sttes of A. For every word w Σ ω, if w is rejected y A then dg(w) hs rnking such tht () every infinite pth of dg(w) visits nodes of odd rnk i.o., nd () the initil node q 0, 0 hs rnk 2n. Proof: In the proof we cll rnking stisfying () n odd rnking. Assume w is rejected y A. We construct n odd rnking in which q 0, 0 hs rnk t most 2n. Then we cn just chnge the rnk of the initil node to 2n, since the chnge preserves the properties of rnking. In the sequel, given two dgs D, D, we denote y D D the fct tht D cn e otined from D through deletion of some nodes nd their djcent edges. Assume tht A rejects w. We descrie n odd rnking for dg(w). We sy tht node q, l is red in (possily finite) dg D dg(w) iff only finitely mny nodes of D re rechle from

241 12.2. COMPLEMENT 241 q, l. The node q, l is yellow in D iff ll the nodes rechle from q, l (including itself) re not ccepting. In prticulr, yellow nodes re not ccepting. Oserve lso tht the children of red node re red, nd the children of yellow node re red or yellow. We inductively define n infinite sequence D 0 D 1 D 2... of dgs s follows: D 0 = dg(w); D 2i+1 = D 2i \ { q, l q, l is red in D 2i }; D 2i+2 = D 2i+1 \ { q, l q, l is yellow in D 2i+1 }. Figure 12.7 shows D 0, D 1, nd D 2 for dg( ω ). D 3 is the empty dg. q 0, 0 q 0, 1 q 0, 2 q 0, 3 q 0, 4 D 0 q 1, 1 q 1, 3 q 1, 4 q 0, 0 q 0, 2 q 0, 3 q 0, 4 D 1 q 1, 1 q 0, 0 D 2 q 1, 1 Figure 12.7: The dgs D 0, D 1, D 2 for dg( ω ) Consider the function f tht ssigns to ech node of dg(w) nturl numer s follows: { 2i if q, l is red in D2i f ( q, l ) = 2i + 1 if q, l is yellow in D 2i+1 We prove tht f is n odd rnking. The proof is divided into three prts: (1) f ssigns ll nodes numer in the rnge [0... 2n]. (2) If q, l is child of q, l, then f ( q, l ) f ( q, l ). (3) If q, l is n ccepting node, then f ( q, l ) is even.

242 242 CHAPTER 12. BOOLEAN OPERATIONS: IMPLEMENTATIONS Prt (1). We show tht for every i 0 there exists numer l i such tht for ll l l i, the dg D 2i contins t most n i nodes of the form q, l. This implies tht D 2n is finite, nd so tht D 2n+1 is empty, which in turn implies tht f ssigns ll nodes numer in the rnge [0... 2n]. The proof is y n induction on i. The cse where i = 0 follows from the definition of G 0 : indeed, in dg(w) ll levels l 0 hve t most n nodes of the form q, l. Assume now tht the hypothesis holds for i; we prove it for i + 1. Consider the dg D 2i. If D 2i is finite, then D 2i+1 is empty; D 2i+2 is empty s well, nd we re done. So ssume tht D 2i is infinite. We clim tht D 2i+1 contins some yellow node. Assume, y wy of contrdiction, tht no node in D 2i+1 is yellow. Since D 2i is infinite, D 2i+1 is lso infinite. Moreover, since D 2i+1 is otined y removing ll red nodes from D 2i, every node of D 2i+1 hs t lest one child. Let q 0, l 0 e n ritrry node of D 2i+1. Since, y the ssumption, it is not yellow, there exists n ccepting node q 0, l 0 rechle from q 0, l 0. Let q 1, l 1 e child of q 0, l 0. By the ssumption, q 1, l 1 is lso not yellow, nd so there exists n ccepting node q 1, l 1 rechle from q 1, l 1. We cn thus construct n infinite sequence of nodes q j, l j, q j, l j such tht for ll i the node q j, l j is ccepting, rechle from q j, l j, nd q j+1, l j+1 is child of q j, l j. Such sequence, however, corresponds to pth in dg(w) visiting infinitely mny ccepting nodes, which contrdicts the ssumption tht A rejects w, nd the clim is proved. So, let q, l e yellow node in D 2i+1. We clim tht we cn tke l i+1 = l, tht is, we clim tht for every j l the dg D 2i+2 contins t most n (i + 1) nodes of the form q, j. Since q, l is in D 2i+1, it is not red in D 2i. Thus, infinitely mny nodes of D 2i re rechle from q, l. By König s Lemm, D 2i contins n infinite pth q, l, q 1, l + 1, q 2, l + 2,.... For ll k 1, infinitely mny nodes of D 2i re rechle from q k, l + k, nd so q k, l + k is not red in D 2i. Therefore, the pth q, l, q 1, l + 1, q 2, l + 2,... exists lso in D 2i+1. Recll tht q, l is yellow. Hence, eing rechle from q, l, ll the nodes q k, l + k in the pth re yellow s well. Therefore, they re not in D 2i+2. It follows tht for ll j l the numer of nodes of the form q, j in D 2i+2 is strictly smller thn their numer in D 2i, which, y the induction hypothesis, is n i. So there re t most n (i + 1) nodes of the form q, j in D 2i+2, nd the clim is proved. Prt(2). Follows from the fct tht the children of red node in D 2i re red, nd the children of yellow node in D 2i+1 re yellow. Therefore, if node hs rnk i, ll its successors hve rnk t most i or lower. Prt(3). Nodes tht get n odd rnk re yellow t D 2i+1 for some i, nd so not ccepting. Exmple 12.4 We construct the complements A 1 nd A 2 of the two possile NBAs over the lphet {} hving one stte nd one trnsition: B 1 = ({q}, {}, δ, {q}, {q}) nd B 2 = ({q}, {}, δ, {q}, ), where δ(q, ) = {q}. The only difference etween B 1 nd B 2 is tht the stte q is ccepting in B 1, ut not in B 2. We hve L ω (A 1 ) = ω nd L ω (A 2 ) =. We egin with B 1. A stte of B 1 is pir lr, O, where lr is the rnk of node q (since there is only one stte, we cn identify lr nd lr(q)). The initil stte is 2, {q}, ecuse q hs even rnk

243 12.2. COMPLEMENT 243 nd so it owes visit to node of odd rnk. Let us compute the successors of 2, {q} under the letter. Let lr, O e successor. Since δ(q, ) = {q}, we hve lr, nd since q is ccepting, we hve lr 1. So either lr = 0 or lr = 2. In oth cses the visit to node of odd rnk is still owed, which implies O = {q}. So the successors of 2, {q} re 2, {q} nd 0, {q}. Consider now the successors lr, O of 0, {q}. We hve lr nd lr 1 s efore, ut now, since rnks cnnot increse long pth, we lso hve lr 2. So lr = 0, nd, since the visit to the node of odd rnk is still owed, the only successor of 0, {q} is 0, {q}. Since the set of owing sttes is never empty, B 1 hs no ccepting sttes, nd so it recognizes the empty lnguge. B 1 is shown on the left of Figure Let us now construct B 2. The difference with B 2 is tht, since q is no longer 2, {q} 0, {q} 2, {q} 0, {q} 1, Figure 12.8: The NBAs B 1 nd B 2 ccepting, it cn lso hve odd rnk 1. So [2, {q}] hs three successors: [2, {q}], [1, ], nd [0, {q}]. The successors of [1, ] re [1, ] nd [0, {q}], nd the only successor of [0, {q}] is [0, {q}]. The only ccepting stte is [1, ], nd B 2 recognizes ω. The pseudocode for the complementtion lgorithm is shown elow. In the code, R denotes the set of ll level rnkings, nd lr 0 denotes the level rnking given y lr(q 0 ) = 2 Q nd lr(q) = for every q q 0. Recll lso tht lr lr holds if for every q Q: lr (q ) = iff no q Q stisfies q q, nd lr(q) lr (q ) for every q Q stisfying q q.

244 244 CHAPTER 12. BOOLEAN OPERATIONS: IMPLEMENTATIONS CompNBA(A) Input: NBA A = (Q, Σ, δ, Q 0, F) Output: NBA A = (Q, Σ, δ, q 0, F) with L ω (A) = L ω (A) 1 Q, δ, F 2 q 0 [lr 0, {q 0 }] 3 W { [lr 0, {q 0 }] } 4 while W do 5 pick [lr, P] from W; dd [lr, P] to Q 6 if P = then dd [lr, P] to F 7 for ll Σ, lr R such tht lr lr do 8 if P then P {q δ(p, ) lr (q) is even } 9 else P {q Q lr (q) is even } 10 dd ([lr, P],, [lr, P ]) to δ 11 if [lr, P ] Q then dd [lr, P ] to W 12 return (Q, Σ, δ, q 0, F) Complexity. Let n e the numer of sttes of A. Since level rnking is mpping lr : Q { } [0, 2n], there re t most (2n + 2) n level rnkings. So A hs t most (2n + 2) n 2 n n O(n) sttes. Since n O(n) = 2 O(n log n), we hve introduced n extr log n fctor in the exponent with respect to the suset construction for utomt on finite words. The next section shows tht this fctor is unvoidle The size of A We exhiit fmily {L n } n 1 of infinitry lnguges such tht L n is ccepted y n utomton with n + 2 sttes nd ny Büchi utomton ccepting the complement of L n hs t lest n! 2Θ(n log n) sttes. Let Σ n = {1,..., n, #}. We ssocite to word w Σ ω n the following directed grph G(w): the nodes of G(w) re 1,..., n nd there is n edge from i to j if w contins infinitely mny occurrences of the word i j. Define L n s the lnguge of infinite words w A ω for which G(w) hs cycle nd define L n s the complement of L n. We first show tht for ll n 1, L n is recognized y Büchi utomton with n + 2 sttes. Let A n e the utomton shown in Figure We show tht A n ccepts L n. (1) If w L n, then A n ccepts w. Choose cycle i1 i2... ik i1 of G(w). We construct n ccepting run of A n y picking q i1 s initil stte nd itertively pplying the following rule: If the current stte is q i j, sty there until the next occurrence of the word i j i j +1 in w, then use i j to move to r, nd use i j +1 to move to q i j +1. By the definition of G(w), r is visited infinitely often, nd so w is ccepted. (2) If A n ccepts w, then w L n.

245 12.2. COMPLEMENT 245 1, 2,..., n, # q 1 1 1, 2,..., n, # q r # h. 2 n n 1, 2,..., n, # 1, 2,..., n, # q n Figure 12.9: The utomton A n Let ρ e run of A n ccepting w, nd let Q ρ = inf (ρ) {q,..., q n }. Since ρ is ccepting, it cnnot sty in ny of the q i forever, nd so for ech q i Q ρ there is q j Q ρ such tht the sequence q i rq j ppers infinitely often in ρ. Therefore, for every q i Q ρ there is q j Q ρ such tht i j ppers infinitely often in w, or, in other words, such tht ( i, j ) G(w). Since Q ρ is finite, G(w) contins cycle, nd so w L n. Proposition 12.5 For ll n 1, every NBA recognizing L n, hs t lest n! sttes. Proof: We need some preliminries. Given permuttion τ = τ(1),..., τ(n) of 1,..., n, we identify τ nd the word τ(1)... τ(n). We mke two oservtions: () (τ#) ω L n for every permuttion τ. The edges of G((τ#) ω ) re τ(1), τ( 2 ), τ( 2 ), τ( 3 ),..., τ( n 1 ), τ( n ), nd so G((τ#) ω ) is cyclic. () If word w contins infinitely mny occurrences of two different permuttions τ nd τ of 1... n, then w L n. Since τ nd τ re different, there re i nd j in {1,..., n} such tht i precedes j in τ nd j precedes i in τ. Since w contins infinitely mny occurrences of τ, G(w) hs pth from i to j. Since it contins infinitely mny occurrences of τ, G(w) hs pth from j to i. So G(w) contins cycle, nd so w L n. Now, let A e Büchi utomton recognizing L n, nd let τ, τ e two ritrry permuttions of (1,..., n). By (), there exist runs ρ nd ρ of A ccepting (τ#) ω nd (τ #) ω, respectively. We prove tht the intersection of inf(ρ) nd inf(ρ ) is empty. This implies tht A contins t lest s mny finl sttes s permuttions of (1,..., n), which proves the Proposition. We proceed y contrdiction. Assume q inf(ρ) inf(ρ ). We uild n ccepting run ρ y comining ρ nd ρ s follows:

246 246 CHAPTER 12. BOOLEAN OPERATIONS: IMPLEMENTATIONS (0) Strting from the initil stte of ρ, go to q following the run ρ. (1) Strting from q, follow ρ until hving gone through finl stte, nd hving red t lest once the word τ ; then go ck to q (lwys following ρ ). (2) Strting from q, follow ρ until hving gone through finl stte, nd hving red t lest once the word τ; then go ck to q (lwys following ρ). (3) Go to (1). The word ccepted y ρ contins infinitely mny occurrences of oth τ nd τ. By (), this word elongs to L n, contrdicting the ssumption tht A recognizes L n. Exercises Exercise 107 Find lgorithms (not necessrily eficient) for the following decision prolems: (1) Given finite words u, v, x, y Σ, decide whether the ω-words u v ω nd x y ω re equl. (2) Given Büchi utomton A nd finite words u, v, decide whether A ccepts the ω-word u v ω. Exercise 108 Show tht for every DBA A with n sttes there is n NBA B with 2n sttes such tht L ω (B) = L ω (A). Exercise 109 A B uchi utomton A = (Q, Σ, δ, Q 0, F) is wek if no strongly connected component (SCC) of sttes contins of A oth ccepting nd non-ccepting sttes, tht is, every SCC C Q stisfies either C F or C Q \ F. () Prove tht B uchi utomton A is wek iff for every run ρ either inf (ρ) F or inf (ρ) Q \ F. () Prove tht the lgorithms for union, intersection, nd complementtion of DFAs re lso correct for wek DBAs. More precisely, show tht the lgorithms return wek DBAs recognizing the union, intersection, nd complement, respectively, of the lnguges of the input utomt. Exercise 110 Give lgorithms tht directly complement deterministic Muller nd prity utomt, without going through Büchi utomt. Exercise 111 Let A = (Q, Σ, q 0, δ, { F 0, G 0,..., F m 1, G m 1 }) e deterministic. Which is the reltion etween the lnguges recognized y A s deterministic Rin utomton nd s deterministic Streett utomton?

247 12.2. COMPLEMENT 247 Exercise 112 Consider Büchi utomt with universl ccepting condition (UBA): n ω-word w is ccepted if every run of the utomton on w is ccepting, i.e., if every run of the utomton on w visits finl sttes infinitely often. Recll tht utomt on finite words with existentil nd universl ccepting conditions recognize the sme lnguges. Prove tht is no longer the cse for utomt on ω-words y showing tht for every UBA there is DBA utomton tht recognizes the sme lnguge. (This implies tht the ω-lnguges recognized y UBAs re proper suset of the ω-regulr lnguges.) Hint: On input w, the DBA checks tht every pth of dg(w) visits some finl stte infinitely often. The sttes of the DBA re pirs (Q, O) of sets of the UBA where O Q is set of owing sttes (see elow). Loosely speking, the trnsition reltion is defined to stisfy the following property: fter reding prefix w of w, the DBA is t the stte (Q, O) given y: Q is the set of sttes reched y the runs of the UBA on w. O is the suset of sttes of Q tht owe visit to finl stte of the UBA. (See the construction for the complement of Büchi utomton.)

248 248 CHAPTER 12. BOOLEAN OPERATIONS: IMPLEMENTATIONS

249 Chpter 13 Emptiness check: Implementtions We present efficient lgorithms for the emptiness check. We fix n NBA A = (Q, Σ, δ, Q 0, F). Since trnsition lels re irrelevnt for checking emptiness, in this Chpter we redefine δ from suset of Q Σ Q into suset of Q Q s follows: δ := {(q, q ) Q Q (q,, q ) δ for some Σ} Since in mny pplictions we hve to del with very lrge Büchi utomt, we re interested in on-the-fly lgorithms tht do not require to know the Büchi utomton in dvnce, ut check for emptiness while constructing it. More precisely, we ssume the existence of n orcle tht, provided with stte q returns the set δ(q). We need few grph-theoreticl notions. If (q, r) δ, then r is successor of nd q is predecessor of r. A pth is sequence q 0, q 1,..., q n of sttes such tht q i+1 is successor of q i for every i {0,..., n 1}; we sy tht the pth leds from q 0 to q n. Notice tht pth my consist of only one stte; in this cse, the pth is empty, nd leds from stte to itself. A cycle is pth tht leds from stte to itself. We write q r to denote tht there is pth from q to r. Clerly, A is nonempty if it hs n ccepting lsso, i.e., pth q 0 q 1... q n 1 q n such tht q n = q i for some i {0,..., n 1}, nd t lest one of {q i, q i+1,..., q n 1 } is ccepting. The lsso consists of pth q 0... q i, followed y nonempty cycle q i q i+1... q n 1 q i. We re interested in emptiness checks tht on input A report EMPTY or NONEMPTY, nd in the ltter cse return n ccepting lsso, s witness of nonemptiness Algorithms sed on depth-first serch We present two emptiness lgorithms tht explore A using depth-first serch (DFS). We strt with rief description of depth-first serch nd some of its properties. A depth-first serch (DFS) of A strts t the initil stte q 0. If the current stte q still hs unexplored outgoing trnsitions, then one of them is selected. If the trnsition leds to not yet discovered stte r, then r ecomes the current stte. If ll of q s outgoing trnsitions hve een 249

250 250 CHAPTER 13. EMPTINESS CHECK: IMPLEMENTATIONS explored, then the serch cktrcks to the stte from which q ws discovered, i.e., this stte ecomes the current stte. The process continues until q 0 ecomes the current stte gin nd ll its outgoing trnsitions hve een explored. Here is pseudocode implementtion (ignore the lgorithm DFS Tree for the moment). DFS(A) Input: NBA A = (Q, Σ, δ, Q 0, F) 1 S 2 dfs(q 0 ) 3 proc dfs(q) 4 dd q to S 5 for ll r δ(q) do 6 if r S then dfs(r) 7 return DFS Tree(A) Input: NBA A = (Q, Σ, δ, Q 0, F) Output: Time-stmped tree (S, T, d, f ) 1 S 2 T ; t 0 3 dfs(q 0 ) 4 proc dfs(q) 5 t t + 1; d[q] t 6 dd q to S 7 for ll r δ(q) do 8 if r S then 9 dd (q, r) to T; dfs(r) 10 t t + 1; f [q] t 11 return Oserve tht DFS is nondeterministic, ecuse we do not fix the order in which the sttes of δ(q) re exmined y the for-loop. Since, y hypothesis, every stte of n utomton is rechle from the initil stte, we lwys hve S = Q fter termintion. Moreover, fter termintion every stte q q 0 hs distinguished input trnsition, nmely the one tht, when explored y the serch, led to the discovery of q. It is well-known tht the grph with sttes s nodes nd these trnsitions s edges is tree with root q 0, clled DFS-tree. If some pth of the DFS-tree leds from q to r, then we sy tht q is n scendnt of r, nd r is descendnt of q (in the tree). It is esy to modify DFS so tht it returns DFS-tree, together with timestmps for the sttes. The lgorithm, which we cll DFS Tree is shown ove. While timestmps re not necessry for conducting serch itself, mny lgorithms sed on depth-first serch use them for other purposes 1. Ech stte q is ssigned two timestmps. The first one, d[q], records when q is first discovered, nd the second, f [q], records when the serch finishes exmining the outgoing trnsitions of q. Since we re only interested in the reltive order in which sttes re discovered nd finished, we cn ssume tht the timestmps re integers rnging etween 1 nd 2 Q. Figure 13.1 shows n exmple. 1 In the rest of the chpter, nd in order to present the lgorithms is more compct form, we omit the instructions for computing the timestmps, nd just ssume they re there.

251 13.1. ALGORITHMS BASED ON DEPTH-FIRST SEARCH 251 In our nlyses we lso ssume tht t every time point stte is white, grey, or lck. A stte q is white during the intervl [ 0, d[q] ], grey during the intervl ( d[q], f [q] ], nd lck during the intervl ( f [q], 2 Q ]. So, loosely speking, q is white, if it hs not een yet discovered, grey if it hs lredy een discovered ut still hs unexplored outgoing edges, or lck if ll its outgoing edges hve een explored. It is esy to see tht t ll times the grey sttes form pth (the grey pth) strting t q 0 nd ending t the stte eing currently explored, i.e., t the stte q such tht dfs(q) is eing currently executed; moreover, this pth is lwys prt of the DFS-tree. q 0 q 1 q 2 q 3 q 4 q 6 [1,12] [2,1 [4,9] [5,8] [6,7] q 0 q 1 q 2 q 3 q 4 q 6 [3,1 Figure 13.1: An NBA (the lels of the trnsitions hve een omitted), nd possile run of DFS Tree on it. The numeric intervls re the discovery nd finishing times of the sttes, shown in the formt [d[q], f [q]]. We recll two importnt properties of depth-first serch. Both follow esily from the fct tht procedure cll suspends the execution of the cller, which is only resumed fter the execution of the cllee termintes. Theorem 13.1 (Prenthesis Theorem) In DFS-tree, for ny two sttes q nd r, exctly one of the following four conditions holds, where I(q) denotes the intervl ( d[q], f [q] ], nd I(q) I(r) denotes tht f [q] < d[r] holds. I(q) I(r) nd q is descendnt of r, or I(r) I(q) nd r is descendnt of q, or I(q) I(r), nd neither q is descendnt of r, nor r is descendnt of q, or I(r) I(q), nd neither q is descendnt of r, nor r is descendnt of q.

252 252 CHAPTER 13. EMPTINESS CHECK: IMPLEMENTATIONS Theorem 13.2 (White-pth Theorem) In DFS-tree, r is descendnt of q (nd so I(r) I(q)) if nd only if t time d[q] stte r cn e reched from q in A long pth of white sttes The nested-dfs lgorithm To determine if A is empty we cn serch for the ccepting sttes of A, nd check if t lest one of them elongs to cycle. A nïve implementtion proceeds in two phses, serching for ccepting sttes in the first, nd for cycles in the second. The runtime is qudrtic: since n utomton with n sttes nd m trnsitions hs O(n) ccepting sttes, nd since serching for cycle contining given stte tkes O(n + m) time, we otin O(n 2 + nm) ound. The nested-dfs lgorithm runs in time O(n+m) y using the first phse not only to discover the rechle ccepting sttes, ut lso to sort them. The serches of the second phse re conducted ccording to the order determined y the sorting. As we shll see, conducting the serch in this order voids repeted visits to the sme stte. The first phse is crried out y DFS, nd the ccepting sttes re sorted y incresing finishing (not discovery!) time. This is known s the postorder induced y the DFS. Assume tht in the second phse we hve lredy performed serch strting from the stte q tht hs filed, i.e., no cycle of A contins q. Suppose we proceed with serch from nother stte r (which implies f [q] < f [r]), nd this serch discovers some stte s tht hd lredy een discovered y the serch strting t q. We clim tht it is not necessry to explore the successors of s gin. More precisely, we clim tht s r, nd so it is useless to explore the successors of s, ecuse the explortion cnnot return ny cycle contining r. The proof of the clim is sed on the following lemm: Lemm 13.3 If q r nd f [q] < f [r] in some DFS-tree, then some cycle of A contins q. Proof: Let π e pth leding from q to r, nd let s e the first node of π tht is discovered y the DFS. By definition we hve d[s] d[q]. We prove tht s q, q s nd s q hold, which implies tht some cycle of A contins q. q s. If s = q, then t time d[q] the pth π is white, nd so I(r) I(q), contrdicting f [q] < f [r]. q s. Ovious, ecuse s elongs to π. s q. By the definition of s, nd since s q, we hve d[s] d[q]. So either I(q) I(s) or I(s) I(q). We clim tht I(s) I(q) is not possile. Since t time d[s] the supth of π leding from s to r is white, we hve I(r) I(s). But I(r) I(s) nd I(s) I(q) contrdict f [q] < f [r], which proves the clim. Since I(s) I(q) is not possile, we hve I(q) I(s), nd hence q is descendnt of s, which implies s q.

253 13.1. ALGORITHMS BASED ON DEPTH-FIRST SEARCH 253 Exmple 13.4 The NBA of Figure 13.1 contins pth from q 1 to q 0, nd the DFS-tree displyed stisfied f [q 1 ] = 11 < 12 = f [q 0 ]. As gurnteed y lemm 13.3, some cycle contins q 1, nmely the cycle q 1 q 6 q 0. To prove the clim, we ssume tht s r holds nd derive contrdiction. Since s ws lredy discovered y the serch strting t q, we hve q s, nd so q r. Since f [q] < f [r], y Lemm 13.3 some cycle of A contins q, contrdicting the ssumption tht the serch from q filed. Hence, during the second phse we only need to explore trnsition t most once, nmely when its source stte is discovered for the first time. This gurntees the correctness of the following lgorithm: Perform DFS on A from q 0, nd output the ccepting sttes of A in postorder 2. q 1,..., q k e the output of the serch, i.e., f [q 1 ] <... < f [q k ]. Let For i = 1 to k, perform DFS from the stte q i, with the following chnges: If the serch visits stte q tht ws lredy discovered y ny of the serches strting t q 1,..., q i 1, then the serch cktrcks. If the serch visits q i, it stops nd returns NONEMPTY. If none of the serches from q 1,..., q k returns NONEMPTY, return EMPTY. Exmple 13.5 We pply the lgorithm to the exmple of Figure Assume tht the first DFS runs s in Figure The serch outputs the ccepting sttes in postorder, i.e., in the order q 2, q 1, q 0. Figure 13.2 shows the trnsitions explored during the serches of the second phse. The serch from q 2 explores the trnsitions lelled y 2.1, 2.2, 2.3. The serch from q 1 explores the trnsitions 1.1,..., 1.5. Notice tht the serch cktrcks fter exploring 1.1, ecuse the stte q 2 ws lredy visited y the previous serch. This serch is successful, ecuse trnsition 1.5 reches stte q 1, nd so cycle contining q 1 hs een found. The running time of the lgorithm cn e esily determined. The first DFS requires O( Q + δ ) time. During the serches of the second phse ech trnsition is explored t most once, nd so they cn e executed together in O( Q + δ ) time. Nesting the two serches Recll tht we re looking for lgorithms tht return n ccepting lsso when A is nonempty. The lgorithm we hve descried is not good for this purpose. Define the DFS-pth of stte s the unique pth of the DFS-tree leding from the initil stte to it. When the second phse nswers NONEMPTY, the DFS-pth of the stte eing currently explored, sy q, is n ccepting cycle, ut 2 Notice tht this does not require to pply ny sorting lgorithm, it suffices to output n ccepting stte immeditely fter lckening it.

254 254 CHAPTER 13. EMPTINESS CHECK: IMPLEMENTATIONS q q q q q q 6 Figure 13.2: The trnsitions explored during the serch strting t q i re lelled y the index i. The serch strting t q 1 stops with NONEMPTY. usully not n ccepting lsso. For n ccepting lsso we cn prefix this pth with the DFS-pth of q otined during the first phse. However, since the first phse cnnot foresee the future, it does not know which ccepting stte, if ny, will e identified y the second phse s elonging to n ccepting lsso. So either the first serch must store the DFS-pths of ll the ccepting sttes it discovers, or third phse is necessry, in which new DFS-pth is recomputed. This prolem cn e solved y nesting the first nd the second phses: Whenever the first DFS lckens n ccepting stte q, we immeditely lunch second DFS to check if q is rechle from itself. We otin the nested-dfs lgorithm, due to Courcouetis, Vrdi, Wolper, nd Ynnkkis: Perform DFS from q 0. Whenever the serch lckens n ccepting stte q, lunch new DFS from q. If this second DFS visits q gin (i.e., if it explores some trnsition leding to q), stop with NONEMPTY. Otherwise, when the second DFS termintes, continue with the first DFS. If the first DFS termintes, output EMPTY. A pseudocode implementtion is shown elow; for clrity, the progrm on the left does not include the instructions for returning n ccepting lsso. A vrile seed is used to store the stte from which the second DFS is lunched. The instruction report X produces the output X nd stops the execution. The set S is usully implemented y mens of hsh-tle. Notice tht it is not necessry to store sttes [q, nd [q, 2] seprtely. Insted, when stte q is discovered, either during the first or the second serches, then it is stored t the hsh ddress, nd two extr its re used to store which of the following three possiilities hold: only [q, hs en discovered so fr, only [q, 2], or oth. So, if stte is encoded y itstring of length c, then the lgorithm needs c + 2 its of memory per stte.

255 13.1. ALGORITHMS BASED ON DEPTH-FIRST SEARCH 255 NestedDFS(A) Input: NBA A = (Q, Σ, δ, Q 0, F) Output: EMP if L ω (A) = NEMP otherwise 1 S 2 dfs1(q 0 ) 3 report EMP 4 proc dfs1(q) 5 dd [q, to S 6 for ll r δ(q) do 7 if [r, S then dfs1(r) 8 if q F then { seed q; dfs2(q) } 9 return 10 proc dfs2(q) 11 dd [q, 2] to S 12 for ll r δ(q) do 13 if r = seed then report NEMP 14 if [r, 2] S then dfs2(r) 15 return NestedDFSwithWitness(A) Input: NBA A = (Q, Σ, δ, Q 0, F) Output: EMP if L ω (A) = NEMP otherwise 1 S ; succ flse 2 dfs1(q 0 ) 3 report EMP 4 proc dfs1(q) 5 dd [q, to S 6 for ll r δ(q) do 7 if [r, S then dfs1(r) 8 if succ = true then return [q, 9 if q F then 10 seed q; dfs2(q) 11 if succ = true then return [q, 12 return 13 proc dfs2(q) 14 dd [q, 2] to S 15 for ll r δ(q) do 16 if [r, 2] S then dfs2(r) 17 if succ = true then return [q, 2] 18 if r = seed then 19 report NEMP; succ true 20 return The lgorithm on the right shows how to modify NestedDFS so tht it returns n ccepting lsso. It uses glol oolen vrile succ (for success), initilly set to flse. If t line 11 the lgorithm finds tht r = seed holds, it sets success to true. This cuses procedure clls in dfs1(q) nd dfs2(q) to e replced y return[q, nd return[q, 2], respectively. The lsso is produced in reverse order, i.e., with the initil stte t the end. A smll improvement We show tht dfs2(q) cn lredy return NONEMPTY if it discovers stte tht elongs to the DFSpth of q in dfs1. Let q k e n ccepting stte. Asssume tht dfs1(q 0 ) discovers q k, nd tht the

256 256 CHAPTER 13. EMPTINESS CHECK: IMPLEMENTATIONS DFS-pth of q k in dfs1 is q 0 q 1... q k 1 q k. Assume further tht dfs2(q k ) discovers q i for some 0 i k 1, nd tht the DFS-pth of q i in dfs2 is q k q k+1... q k+l q i. Then the pth q 0 q 1... q k 1 q k... q k+l q i is lsso, nd, since q k is ccepting, it is n ccepting lsso. So stopping with NONEMPTY is correct. Implementing this modifiction requires to keep trck during dfs1 of the sttes tht elong to the DFS-pth of the stte eing currently explored. Notice, however, tht we do not need informtion out their order. So we cn use set P to store the sttes of the pth, nd implement P s e.g. hsh tle. We do not need the vrile seed nymore, ecuse the cse r = seed is susumed y the more generl r P. ImprovedNestedDFS(A) Input: NBA A = (Q, Σ, δ, Q 0, F) Output: EMP if L ω (A) =, NEMP otherwise 1 S ; P 2 dfs1(q 0 ) 3 report EMP 4 proc dfs1(q) 5 dd [q, to S ; dd q to P 6 for ll r δ(q) do 7 if [r, S then dfs1(r) 8 if q F then dfs2(q) 9 remove q from P 10 return 11 proc dfs2(q) 12 dd [q, 2] to S 13 for ll r δ(q) do 14 if r P then report NEMP 15 if [r, 2] S then dfs2(r) 16 return Evlution The strong point of the the nested-dfs lgorithm re its very modest spce requirements. Aprt from the spce needed to store the stck of clls to the recursive dfs procedure, the lgorithm just needs two extr its for ech stte of A. In mny prcticl pplictions, A cn esily hve millions or tens of millions of sttes, nd ech stte my require mny ytes of storge. In these cses, the two extr its per stte re negligile. The lgorithm, however, lso hs two importnt wek points: It cnnot e extended to NGAs, nd it is not optiml, in forml sense defined elow. We discuss these two points seprtely.

257 13.1. ALGORITHMS BASED ON DEPTH-FIRST SEARCH 257 The nested-dfs lgorithm works y identifying the ccepting sttes first, nd then checking if they elong to some cycle. This principle no longer works for the cceptnce condition of NGAs, where we look for cycles contining t lest one stte of ech fmily of ccepting sttes. No etter procedure thn trnslting the NGA into n NBA hs een descried so fr. For NGAs hving lrge numer of ccepting fmilies, the trnsltion my involve sustntil penlty in performnce. A serch-sed lgorithm for emptiness checking explores the utomton A strting from the initil stte. At ech point t in time, the lgorithm hs explored suset of the sttes nd the trnsitions of the lgorithm, which form su-nba A t = (Q t, Σ, δ t, q 0, F t ) of A (i.e., Q t Q, δ t δ, nd F t F)). Clerly, serch-sed lgorithm cn hve only reported NONEMPTY t time t if A t contins n ccepting lsso. A serch-sed lgorithm is optiml if the converse holds, i.e., if it reports NONEMPTY t the erliest time t such tht A t contins n ccepting lsso. It is esy to see tht NestedDFS is not optiml. Consider the utomton on top of Figure Initilly, q 1 q 0 q 2 q n 1 q n q 1 q n+1 q 0 q 2 q n 1 q n Figure 13.3: Two d exmples for NestedDFS the lgorithm chooses etween the trnsitions (q 0, q 1 ) nd (q 0, q 2 ). Assume it chooses (q 0, q 1 ) (the lgorithm does not know tht there is long til ehind q 2 ). The lgorithm explores (q 0, q 1 ) nd then (q 1, q 0 ) t some time t. The utomton A t lredy contins n ccepting lsso, ut, since q 0 hs not een lckened yet, dfs1 continues its execution with (q 0, q 2 ), nd explores ll trnsitions efore dfs2 is clled for the first time, nd reports NONEMPTY. So the time elpsed etween the first moment t which the lgoritm hs enough informtion to report NONEMPTY, nd the moment t which the report occurs, cn e ritrrily lrge. The utomton t the ottom of Figure 13.5 shows nother prolem of NestedDFS relted to non-optimlity. If it selects (q 0, q 1 ) s first trnsition, then, since q n precedes q 0 in postorder, dfs2(q n ) is executed efore dfs2(q 0 ), nd it succeeds, reporting the lsso q 0 q 2... q n q n+1 q n, insted of the much shorter lsso q 0 q 1 q 0.

258 258 CHAPTER 13. EMPTINESS CHECK: IMPLEMENTATIONS In the next section we descrie n optiml lgorithm tht cn e esily extended to NGAs. The price to py is higher memory consumption. As we shll see, the new lgorithm needs to ssign numer to ech stte, nd store it (prt from mintining other dt structures) The two-stck lgorithm Recll tht the nested-dfs lgorithm serches for ccepting sttes of A, nd then checks if they elong to some cycle. The two-stck lgorithm proceeds the other wy round: It serches for sttes tht elong to some cycle of A y mens of single DFS, nd checks whether they re ccepting. A first oservtion is tht y the time the DFS lckens stte, it lredy hs explored enough to decide whether it elongs to cycle: Lemm 13.6 Let A t e the su-nba of A contining the sttes nd trnsitions explored y the DFS up to (nd including) time t. If stte q elongs to some cycle of A, then it lredy elongs to some cycle of A f [q]. Proof: Let π e cycle contining q, nd consider the snpshot of the DFS t time f [q]. Let r e the lst stte of π fter q such tht ll sttes in the supth from q to r re lck. We hve f [r] f [q]. If r = q, then π is cycle of A f [q], nd we re done. If r q, let s e the successor of r in π (see Figure 13.4). We hve f [r] < f [q] < f [s]. Moreover, since ll successors of r hve q π π r s Figure 13.4: Illustrtion of the proof of Lemm 13.6 necessrily een discovered t time f [r], we hve d[s] < f [r] < f [q] < f [s]. By the Prenthesis theorem, s is DFS-scendnt of q. Let π e the cycle otined y conctenting the DFS-pth from s to q, the prefix of π from q to r, nd the trnsition (r, s). By the Prenthesis Theorem, ll the trnsitions in this pth hve een explored t time f [q], nd so the cycle elongs to A f [q] This lemm suggests to mintin during the DFS set C of cndidtes, contining the sttes for which it is not yet known whether they elong to some cycle or not. A stte is dded to C when it is discovered. While the stte is grey, the lgorithm tries to find cycle contining it. If it succeeds, then the stte is removed from C. If not, then the stte is removed from C when it is lckened. At ny time t, the cndidtes re the currently grey sttes tht do not elong to ny cycle of A t.

259 13.1. ALGORITHMS BASED ON DEPTH-FIRST SEARCH 259 Assume tht t time t the set C indeed contins the current set of cndidtes, nd tht the DFS explores new trnsition (q, r). We need to updte C. If r hs not een discovered yet (i.e., if it does not elong to A t ), the ddition of r nd (q, r) to A t does not crete ny new cycle, nd the updte just dds r to C. If r elongs to A t ut no pth of A t leds from r to q, gin no new cycle is creted, nd the set C does not chnge. But if r elongs to A t, nd r q then the ddition of (q, r) does crete new cycles. Let us ssume we cn sk n orcle whether r q holds, nd the orcle nswers yes. Then we hve lredy lernt tht oth q nd r elong to some cycle of A, nd so oth of them must e removed from C. However, we my hve to remove other sttes s well. Consider the DFS of Figure 13.5: fter dding (q 4, q 1 ) to the set of explored trnsitions t time 5, ll of q 1, q 2, q 3, q 4 elong to cycle. The fct tht these re the sttes discovered y the DFS etween the [5,6] q 4 [4,7] q 3 q 0 q 1 q 2 [1,1 [2,9] [3,8] Figure 13.5: A DFS on n utomton discoveries of q 1 nd q 4 suggests to implement C using stck: By pushing sttes into C when they re discovered, nd removing when they re lckened or erlier (if some cycle contins them), the updte to C fter exploring trnsition (q, r) cn e performed very esily: it suffices to pop from C until r is hit (in Figure 13.5 we pop q 4, q 3, q 2, nd q 1 ). Oserve lso tht removing q from C when it is lckened does not require to inspect the complete stck; since every stte is removed t the ltest when it is lckened, if q hs not een removed yet, then it is necessrily t the top of C. (This is the cse of stte q 0 in Figure 13.5). So it suffices to inspect the top of the stck: if q is t the top, we pop it; otherwise q is not in the stck, nd we do nothing. This leds to our first ttempt t n lgorithm, shown on the top left corner of Figure When stte q is discovered it is pushed into C (line 5), nd its successors explored (lines 6-12). When exploring successor r, if r hs not een discovered yet then dfs(r) is clled (line 7). Otherwise the orcle is consulted (line 8), nd if r q holds t the time (i.e., in the prt of A explored so fr), then sttes re popped from C until r is hit (lines 9-12). Then the lgorithm checks if q hs lredy een removed y inspecting the top of the stck (line 13), nd removes q if tht is the cse. The NBA elow FirstAttempt shows, however, tht the lgorithm needs to e ptched. After exploring (q 4, q 1 ) the sttes q 4, q 3, q 2, q 1 re popped from C, in tht order, nd C contins only q 0. Now the DFS cktrcks to stte q 3, nd explores (q 3, q 5 ), pushing q 5 into the stck. Then the DFS explores (q 5, q 1 ), nd pops from C until it hits q 1. But this leds to n incorrect result, ecuse, since q 1 no longer elongs to C, the lgorithm pops ll sttes from C, nd when it pops q 0 it reports

260 260 CHAPTER 13. EMPTINESS CHECK: IMPLEMENTATIONS FirstAttempt(A) Input: NBA A = (Q, Σ, δ, Q 0, F) Output: EMP if L ω (A) = NEMP otherwise 1 S, C ; 2 dfs(q 0 ) 3 report EMP 4 proc dfs(q) 5 dd q to S ; push(q, C) 6 for ll r δ(q) do 7 if r S then dfs(r) 8 else if r q then 9 repet 10 s pop(c) 11 if s F then report NEMP 12 until s = r 13 if top(c) = q then pop(c) SecondAttempt(A) Input: NBA A = (Q, Σ, δ, Q 0, F) Output: EMP if L ω (A) = NEMP otherwise 1 S, C ; 2 dfs1(q 0 ) 3 report EMP 4 proc dfs(q) 5 dd q to S ; push(q, C) 6 for ll r δ(q) do 7 if r S then dfs(r) 8 else if r q then 9 repet 10 s pop(c) 11 if s F then report NEMP 12 until s = r 13 push(r, C) 14 if top(c) = q then pop(c) [5,6] q 4 [4,7] q 3 [7,8] q 5 [1,12] [2,1 [3,1 q 4 q 3 q 0 q 1 q 2 [5,6] [4,9] q 5 [8,9] q 0 q 1 q 2 [1,12] [2,1 [3,1 Figure 13.6: Two incorrect ttempts t n emptiness checking lgorithm

261 13.1. ALGORITHMS BASED ON DEPTH-FIRST SEARCH 261 NONEMPTY. This prolem cn e solved s follows: If the DFS explores (q, r) nd r q holds, then it pops from C until r is popped, nd pushes r ck into the stck. This second ttempt is shown t the top right of the figure. However, the NBA elow SecondAttempt shows tht it is gin incorrect. After exploring (q 4, q 1 ) (with stck content q 4 q 3 q 2 q 1 q 0 ), the sttes q 4, q 3, q 2, q 1 re popped from C, in tht order, nd q 1 is pushed gin. C contins now q 1 q 0. The DFS explores (q 3, q 5 ) next, pushing q 5, followed y (q 5, q 4 ). Since q 4 q 5 holds, the lgorithm pops from C until q 4 is found. But, gin, since q 4 does not elong to C, the result is incorrect. A ptch for this prolem is to chnge the condition of the repet loop: If the DFS explores (q, r) nd r q holds, we cnnot e sure tht r is still in the stck. So we pop until either r or some stte discovered efore r is hit, nd then we push this stte ck gin. In the exmple, fter exploring (q 5, q 4 ) with stck content q 5 q 1 q 0, the lgorithm pops q 5 nd q 1, nd then pushes q 1 ck gin. This new ptch leds to the OneStck lgorithm: OneStck(A) Input: NBA A = (Q, Σ, δ, Q 0, F) Output: EMP if L ω (A) =, NEMP otherwise 1 S, C ; 2 dfs(q 0 ) 3 report EMP 4 dfs(q) 5 dd q to S ; push(q, C) 6 for ll r δ(q) do 7 if r S then dfs(r) 8 else if r q then 9 repet 10 s pop(c); if s F then report NEMP 11 until d[s] d[r] 12 push(s, C) 13 if top(c) = q then pop(c) Exmple 13.7 Figure 13.7 shows run of OneStck on the NBA shown t the top. The NBA hs no ccepting sttes, nd so it is empty. However, during the run we will see how the lgorithm nswers NONEMPTY (resp. EMPTY) when f (res. h) is the only ccepting stte. The discovery nd finishing times re shown t the top. Oserve tht the NBA hs three sccs: {,, e, f, g}, {h}, nd {c, d, i, j}, with roots, h, nd i, respectively. Below the NBA t the top, the figure shows different snpshots of the run of OneStck. At ech snpshot, the current grey pth is shown in red/pink. The dotted sttes nd trnsitions hve not een discovered yet, while drk red sttes hve lredy een lckened. The current content of stck C is shown on the right (ignore stck L for the moment).

262 262 CHAPTER 13. EMPTINESS CHECK: IMPLEMENTATIONS The first snpshot is tken immeditely efore stte j is lckened. The lgorithm hs just explored the trnsition ( j, i), hs popped the sttes c, d, j, i from C, nd hs pushed stte i ck. The sttes c, d, j, i hve een identified s elonging to some cycle. The second snpshot is tken immeditely fter stte i is lckened. Stte i hs een popped from C t line 13. Oserve tht fter this the lgorithm cktrcks to dfs(h), nd, since stte h is t the top of the stck, it pops h from C t line 13. So h is never popped y the repet loop, nd so even if h is ccepting the lgorithm does not report NONEMPTY. The third snpshot is tken immeditely efore stte f is lckened. The lgorithm hs just explored the trnsition ( f, ), hs popped the sttes f, g,, from C, nd hs pushed stte ck. The sttes f, g,, hve een identified s elonging to some cycle. If stte f is ccepting, t this point the lgorithm reports EMPTY nd stops. The fourth snpshot is tken immeditely fter stte e is discovered. The stte hs een pushed into C. The finl snpshot is tken immeditely efore is lckened nd the run termintes. Stte is going to e removed from C t line 13, nd the run termintes. Oserve tht when the lgorithm explores trnsition (, c) it clls the orcle, which nswers c, nd no sttes re popped from C. The lgorithm looks now plusile, ut we still must prove it correct. We hve two proof oligtions: If A is nonempty, then OneStck reports NONEMPTY. This is equivlent to: every stte tht elongs to some cycle is eventully popped during the repet loop. If OneStck reports NONEMPTY, then A is nonempty. This is equivlent to: every stte popped during the repet loop elongs to some cycle. These properties re shown in Propositions 13.8 nd elow. Proposition 13.8 If q elongs to cycle, then q is eventully popped y the repet loop. Proof: Let π e cycle contining q, let q e the lst successor of q long π such tht t time d[q] there is white pth from q to q, nd let r e the successor of q in π. Since r is grey or lck t time d[q], we hve d[r] d[q] d[q ]. By the White-pth Theorem, q is descendnt of q, nd so the trnsition (q, r) is explored efore q is lckened. So when (q, r) is explored, q hs not een popped t line 13. Since r q, either q hs lredy een popped y t some former execution of the repet loop, or it is popped now, ecuse d[r] d[q ].

263 13.1. ALGORITHMS BASED ON DEPTH-FIRST SEARCH 263 [1,2 [2,19] [6,1 [7,1 [16,17] e f g h i j [14,15] [3,18] [4,13] [5,12] [8,9] c d unexplored grey pth lck f e g t = 9 ɛ h c i d j g h i g h i j d c C V f e t = 12 + ɛ g h c i d j g g h h C V f e t = 15 ɛ g h c i d j g f C V f e g t = 16 h c i d j g e g f e C V f e t = 20 ɛ g h c i d j g f e C V Figure 13.7: A run of OneStck

264 264 CHAPTER 13. EMPTINESS CHECK: IMPLEMENTATIONS Actully, the proof of Proposition 13.8 proves not only tht q is eventully popped y the repet loop, ut lso tht for every cycle π contining q, the repet loop pops q immeditely fter ll trnsitions of π hve een explored, or erlier. But this is precisely the optimlity property, which leds to: Corollry 13.9 OneStck is optiml. The property tht every stte popped during the repet loop elongs to some cycle hs more involved proof. A strongly connected component (scc) of A is mximl set of sttes S Q such tht q r for every q, r S. 3 The first stte of rechle scc tht is discovered y the DFS is clled the root of the scc (with respect to this DFS). In other words, if q is the root of n scc, then d[q] d[r] for every stte r of the scc. The following lemm sttes n importnt invrint of the lgorithm. If root elongs to C t line 9, efore the repet loop is executed, then it still elongs to C fter the loop finishes nd the lst popped stte is pushed ck. So, loosely speking, the repet loop cnnot remove root from the stck; more precisely, if the loop removes root, then the push instruction t line 12 reintroduces it gin. Lemm Let ρ e root. If ρ elongs to C efore n execution of the repet loop t lines 9-11, then ll sttes s popped during the execution of the loop stisfy d[ρ] d[s], nd ρ still elongs to C fter the repet loop hs terminted nd line 12 hs een executed. Proof: Let t e the time right efore n execution of the repet loop strts t line 9, nd ssume ρ elongs to C t time t. Since sttes re removed from C when they re lckened or erlier, ρ is still grey t time t. Since r δ(q) (line 6) nd r q (line 8), oth q nd r elong to the sme scc. Let ρ e the root of this scc. Since ρ is lso grey t time t, nd grey sttes lwys re pth of the DFS-tree, either ρ is DFS-scendnt of ρ, or ρ ρ nd ρ is DFS-scendnt of ρ. In the ltter cse we hve ρ ρ q, contrdicting tht ρ is the root of q s scc. So ρ is DFS-scendnt of ρ, nd so in prticulr we hve d[ρ] d[ρ ] d[r]. Since sttes re dded to C when they re discovered, the sttes of C re lwys ordered y decresing discovery time, strting from the top, nd so every stte s popped efore ρ stisfies d[ρ] d[s]. If ρ is popped, then, since d[ρ] d[r], the execution of the repet loop termintes, nd ρ is pushed gin t line 12. Proposition Any stte popped during the repet loop (t line 10) elongs to some cycle. Proof: Consider the time t right efore stte s is out to e popped t line 10 while the for-loop (lines 6-12) is exploring trnsition (q, r). (Notice tht the ody of the for-loop my hve lredy een executed for other trnsitions (q, r )). Since the lgorithm hs reched line 10, we hve r q (line 8), nd so oth q nd r elong to the sme scc of A. Let ρ e the root of this scc. We show tht s elongs to cycle. 3 Notice tht pth consisting of just stte q nd no trnsitions is pth leding from q to q.

265 13.1. ALGORITHMS BASED ON DEPTH-FIRST SEARCH 265 (1) s is DFS-scendnt of q. Since s elongs to C t time t, oth s nd q re grey t time t, nd so they elong to the current pth of grey sttes. Moreover, since dfs(q) is eing currently executed, q is the lst stte in the pth. So s is DFS-scendnt of q. (2) ρ is DFS-scendnt of s. Since ρ is root of the scc of q, t time d[ρ] there is white pth from ρ to q. By the Whitepth nd Prenthesis Theorem, ρ is DFS-scendnt of q. Together with (1), this implies tht either ρ is DFS-scendnt of s or s is DFS-scendnt of ρ. By Lemm we hve d[ρ] d[s], nd so y the Prenthesis Theorem ρ is DFS-scendnt of s. By (1), (2), nd r q, we hve ρ s q r ρ, nd so s elongs to cycle. Implementing the orcle Recll tht OneStck clls n orcle to decide t time t if r q holds. At first sight the orcle seems difficult to implement. We show tht this is not the cse. Assume tht OneStck clls the orcle t line 8 t some time t. We look for condition tht holds t time t if nd only if r q, nd is esy to check. Lemm Assume tht OneStck(A) is currently exploring trnsition (q, r), nd the stte r hs lredy een discovered. Let R e the scc of A stisfying r R. Then r q iff some stte of R is not lck. Proof: Assume r q. Then r nd q elong to R, nd since q is not lck ecuse (q, r) is eing explored, R is not lck. Assume every stte of R is lck. Not ll sttes of R re white ecuse r hs lredy een discovered, nd so t lest one stte s R is grey. Since grey sttes form pth ending t the stte whose output trnsitions re eing currently explored, the grey pth contins s nd ends t q. So s q, nd, since s nd r elong to R, we hve r q. By Lemm 13.12, checking if r q holds mounts to checking if ll sttes of R re lck or not. This cn e done s follows: we mintin set V of ctive sttes, where stte is ctive if its scc hs not yet een completely explored, i.e., if some stte of the scc is not lck. Then, checking r q reduces to checking whether r is ctive. The set V cn e mintined y dding stte to it whenever it is discovered, nd removing ll the sttes of scc right fter the lst of them is lckened. The next lemm shows tht the lst of them is lwys the root: Lemm Let ρ e root, nd let q e stte such tht ρ q ρ. Then I(q) I(r). (In words: The root is the first stte of scc to e gryed, nd the lst to e lckened) Proof: By the definition of root, t time d[ρ] there is white pth from ρ to q. By the White-pth nd the Prenthesis Theorems, I(q) I(r).

266 266 CHAPTER 13. EMPTINESS CHECK: IMPLEMENTATIONS By this lemm, in order to mintin V it suffices to remove ll the sttes of n scc whenever its root is lckened. So whenever the DFS lckens stte q, we hve to perform two tsks: (1) check if q is root, nd (2) if q is root, remove ll the sttes of q s scc. Checking if q is root is surprisingly simple: Lemm When OneStck executes line 13, q is root if nd only if top(c) = q. Proof: Assume q is root. By Lemm 13.10, q still elongs to C fter the for loop t lines 6-12 is executed, nd so top(c) = q t line 13. Assume now tht q is not root. Then there is pth from q to the root of q s scc. Let r e the first stte in the pth stisfying d[r] < d[q], nd let q e the predecessor of r in the pth. By the White-pth theorem, q is descendnt of q, nd so when trnsition (q, r) is explored, q is not yet lck. When OneStck explores (q, r), it pops ll sttes s from C stisfying d[s] > d[r], nd none of these sttes is pushed ck t line 12. In prticulr, either OneStck hs lredy removed q from C, or it removes it now. Since q hs not een lckened yet, when OneStck executes line 14 for dfs(q), the stte q does not elong to C nd in prticulr q top(c) Tsks (2) cn e performed very elegntly y implementing V s second stck, nd mintining it s follows: when stte is discovered (greyed), it is pushed into the stck (so sttes re lwys ordered in V y incresing discovery time); nd when root is lckened, ll sttes of V ove it (including the root itself) re popped. Exmple Figure 13.7 shows the content of L t different times when the policy ove is followed. Right efore stte j is lckened, L contins ll sttes in the grey pth. When the root i is lckened, ll sttes ove it (including i itself), re popped; these re the sttes c, d, j, i, which form scc. Stte h is lso root, nd it is lso popped. Then, sttes f nd e re discovered, nd pushed into V. Finlly, when the root is lckened, sttes c, f, g,, re popped, which correspond to the third nd lst scc. We show tht the sttes popped when ρ is lckened re exctly those tht elong to ρ s scc. Lemm The sttes popped from V right fter lckening root ρ re exctly those elonging to ρ s scc. Proof: Let q e stte of ρ s scc. Since ρ is root, we hve d[ρ] d[q], nd so q lies ove ρ in L. So q is popped when ρ is lckened, unless it hs een popped efore. We show tht this cnnot e the cse. Assume q is popped efore ρ is lckened, i.e., when some other root ρ ρ is lckened t time f [ρ ]. We collect some fcts: () d[ρ] d[q] d[q] f [ρ] y Lemm 13.13; () d[ρ ] d[q] < f [ρ ], ecuse q is in the stck t time f [ρ ] (implying d[q] < f [ρ ]), nd it is

267 13.1. ALGORITHMS BASED ON DEPTH-FIRST SEARCH 267 ove ρ in the stck (implying d[ρ ] d[q]); (c) f [ρ ] < f [ρ], ecuse q hs not een popped yet t time f [ρ ], nd so ρ cnnot hve een lckened yet. From ()-(c) nd the Prenthesis Theorem we get I(q) I(ρ ) I(ρ), nd so in prticulr ρ ρ q. But then, since q ρ, we get ρ ρ ρ, contrdicting tht ρ nd ρ re different roots, nd so elong to different sccs. This finlly leds to the two-stck lgorithm TwoStck(A) Input: NBA A = (Q, Σ, δ, Q 0, F) Output: EMP if L ω (A) =, NEMP otherwise 1 S, C, V ; 2 dfs(q 0 ) 3 report EMP 4 proc dfs(q) 5 dd q to S ; push(q, C); push(q, V) 6 for ll r δ(q) do 7 if r S then dfs(r) 8 else if r V then 9 repet 10 s pop(c); if s F then report NEMP 11 until d[s] d[r] 12 push(s, C) 13 if top(c) = q then 14 pop(c) 15 repet s pop(v) until s = q The chnges with respect to OneStck re shown in lue. The orcle r q is replced y r V (line 8). When the lgorithm lckens root (line 13), it pops from V ll elements ove q, nd q itself (line 15). Oserve tht V cnnot e implemented only s stck, ecuse t line 8 we hve to check if stte elongs to the stck or not. The solution is to implement V oth s stck nd use n dditionl it in the hsh tle for S to store whether the stte elongs to V or not, which is possile ecuse V S holds t ll times. The check t line 8 is performed y checking the vlue of the it. Extension to GBAs We show tht TwoStck cn e esily trnsformed into n emptiness check for generlized Büchi utomt tht does not require to construct n equivlent NBA. Recll tht NGA hs in generl severl sets {F 0,..., F k 1 } of ccepting sttes, nd tht run ρ is ccepting if inf ρ F i for every i {0,..., k 1}. So we hve the following chrcteriztion of nonemptiness, where K = {0,..., k 1}:

268 268 CHAPTER 13. EMPTINESS CHECK: IMPLEMENTATIONS Fct Let A e NGA with ccepting condition {F 0,..., F k 1 }. A is nonempty iff some scc S of A stisfies S F i for every i K. Since every time the repet loop t line 15 is executed, it pops from L one scc, we cn esily check this condition y modifying line 15 ccordingly. However, the resulting lgorithm would not e optiml, ecuse the condition is not checked until the scc hs een completely explored. To solve this prolem, we hve closer look t Proposition The proof shows tht stte s popped t line 10 elongs to the ssc of stte r. So, in prticulr, ll the sttes popped during n execution of the repet loop t lines 9-11 elong to the sme scc. So we collect the set I of indices of the sets of ccepting sttes they elong to, nd keep checking whether I = K. If so, then we report NONEMPTY. Otherwise, we ttch the set I to the stte s tht is pushed ck into the C t line 12. This yields lgorithm TwoStckNGA, where F(q) denotes the set of ll indices i K such tht q F i : TwoStckNGA(A) Input: NGA A = (Q, Σ, δ, q 0, {F 0,..., F k 1 }) Output: EMP if L ω (A) =, NEMP otherwise 1 S, C, V ; 2 dfs(q 0 ) 3 report EMP 4 proc dfs(q) 5 dd [q, F(q)] to S ; push([q, F(q)], C); push(q, V) 6 for ll r δ(q) do 7 if r S then dfs(r) 8 else if r V then 9 I 10 repet 11 [s, J] pop(c); 12 I I J; if I = K then report NEMP 13 until d[s] d[r] 14 push([s, I], C) 15 if top(c) = (q, I)for some I then 16 pop(c) 17 repet s pop(v) until s = q For the correctness of the lgorithm, oserve tht, t every time t, the sttes of teh suutomton A t cn e prtitioned into strongly connected components, nd ech of these components hs root. The key invrint for the correctness proof is the following:

269 13.1. ALGORITHMS BASED ON DEPTH-FIRST SEARCH 269 Lemm At every time t, the stck C contins pir [q, I] iff q is root of A t, nd I is the suset of indices i K such tht some stte of F i elongs to q s scc. Proof: Initilly the invrint holds ecuse oth A t nd C re empty. We show tht whenever new trnsition (q, r) is explored, TwoStckNGA crries out the necessry chnges to keep the invrint. Let t e the time immeditely fter (q, r) is explored. If r is new stte, then it hs no successors in A t, nd so it uilds n scc of A t y itself, with root r. Moreover, ll roots efore the explortion of (q, r) re lsoroots of A t. So new pir [r, F(r)] must e dded to C, nd tht is wht dfs(r) does. If r L, then r ρ, the ddition of (q, r) hs not chnged the sccs of the utomton explored so fr, nd nothing must e done. If r L, then the ddition of (q, r) cretes new cycles, nd some sttes stop eing roots. More precisely, let NR e the set of sttes of A t tht elong to cycle contining oth q nd r, nd let ρ e the stte of NR with miniml discovery time. Then the lgorithm must remove from C ll sttes of NR with the exception of ρ (nd no others). We show tht this is exctly wht the execution of lines 9-14 chieves. By Proposition 13.8 nd Corollry 13.9, ll the sttes of NR \ {ρ} hve lredy een removed t some former execution of the loop, or re removed now t lines 9-14, ecuse they hve discovery time smller thn or equl to d[r]. It remins to show tht ll sttes popped t line 11 elong to NR (tht ρ is not removed follows then from the fct tht the stte with the lowest discovery time is pushed gin t line 14, nd tht stte is ρ). For, this, we hve closer look t the proof of Proposition The proposition shows not only tht the sttes popped y the repet loop elong to some cycle, ut lso tht they ll elong to cycles tht contining q nd r (see the lst line of the proof), nd we re done. We cn now esily prove: Proposition TwoStckNGA(A) reports NONEMPTY iff A is nonempty. Moreover, TwoStck- NGA is optiml. Proof: If TwoStckNGA(A) reports NONEMPTY, then the repet loop t lines pops some pir [q, K]. By Lemm 13.18, q elongs to cycle of A contining some stte of F i for every i K. If A is nonempty, then some scc S of A stisfies S F i for every i K. So there is n erliest time t such tht A t contins n scc S t S stisfying the sme property. By Lemm 13.18, TwoStckNGA(A) reports NONEMPTY t time t or erlier, nd so it is optiml. Evlution Recll tht the two wek points of the nested-dfs lgorithm were tht it cnnot e directly extended to NGAs, nd it is not optiml. Both re strong points of the two-stck lgorithm. The strong point of the the nested-dfs lgorithm were its very modest spce requirements: just two extr its for ech stte of A. Let us exmine the spce needed y the two-stck lgorithm. It is conveniet to compute it for empty utomt, ecuse in this cse oth the nested-dfs nd the two-stck lgorithms must visit ll sttes.

270 270 CHAPTER 13. EMPTINESS CHECK: IMPLEMENTATIONS Becuse of the check d[s] d[r], the lgorithm needs to store the discovery time of ech stte. This is done y extending the hsh tle S. If stte q cn e stored using c its, then log n its re needed to store d[q]; however, in prctice d[q] is stored using word of memory, ecuse if the numer sttes of A exceeds 2 w, where w is the numer of its of word, then A cnnot e stored in min memory nywy. So the hsh tle S requires c + w + 1 its per stte (the extr it eing the one used to check memership in L t line 8). The stcks C nd L do not store the sttes themselves, ut the memory ddresses t which they re stored. Ignoring hshing collisions, this requires 2w dditionl its per stte. For generlized Büchi utomt, we must lso dd the k its needed to store the suset of K in the second component of the elements of C. So the two-stck lgorithm uses totl of c + 3w + 1 its per stte (c + 3w + k + 1 in the version for NGA), compred to the c + 2 its required y the nested-dfs lgorithm. In most cses w << c, nd so the influence of the dditionl memory requirements on the performnce is smll Algorithms sed on redth-first serch In this section we descrie lgorithms sed on redth-first serch (BFS). No liner BFS-sed emptiness check is known, nd so this section my look t first sight superfluous. However, BFSsed lgorithms cn e suitly descried using opertions nd checks on sets, which llows us to implement them using utomt s dt structures. In mny cses, the gin otined y the use of the dt structure more thn compenstes for the qudrtic worse-cse ehviour, mking the lgorithms competitive. Bredth-first serch (BFS) mintins the set of sttes tht hve een discovered ut not yet explored, often clled the frontier or oundry. A BFS from set Q 0 of sttes (in this section we consider serches from n ritrry set of sttes of A) initilizes oth the set of discovered sttes nd its frontier to Q 0, nd then proceeds in rounds. In forwrd serch, round explores the outgoing trnsitions of the sttes in the current frontier; the new sttes found during the round re dded to the set of discovered sttes, nd they ecome the next frontier. A ckwrd BFS proceeds similrly, ut explores the incoming insted of the outgoing trnsitions. The pseudocode implementtions of oth BFS vrints shown elow use two vriles S nd B to store the set of discovered sttes nd the oundry, respectively. We ssume the existence of orcles tht, given the current oundry B, return either δ(b) = q B δ(q) or δ 1 (B) = q B δ 1 (q).

271 13.2. ALGORITHMS BASED ON BREADTH-FIRST SEARCH 271 ForwrdBFS[A](Q 0 ) Input: NBA A = (Q, Σ, δ, Q 0, F), Q 0 Q 1 S, B Q 0 ; 2 repet 3 B δ(b) \ S 4 S S B 5 until B = BckwrdBFS[A](Q 0 ) Input: NBA A = (Q, Σ, δ, Q 0, F), Q 0 Q 1 S, B Q 0 ; 2 repet 3 B δ 1 (B) \ S 4 S S B 5 until B = Both BFS vrints compute the successors or predecessors of stte exctly once, i.e., if in the course of the lgorithm the orcle is clled twice with rguments B i nd B j, respectively, then B i B j =. To prove this in the forwrd cse (the ckwrd cse is nlogous), oserve tht B S is n invrint of the repet loop, nd tht the vlue of S never decreses. Now, let B 1, S 1, B 2, S 2,... e the sequence of vlues of the vriles B nd S right efore the i-th execution of line 3. We hve B i S i y the invrint, S i S j for every j i, nd nd B j+1 S j = y line 3. So B j B i = for every j > i. As dt structures for the sets S nd B we cn use hsh tle nd queue, respectively. But we cn lso tke the set Q of sttes of A s finite universe, nd use utomt for fixed-length lnguges to represent oth S nd B. Moreover, we cn represent δ Q Q y finite trnsducer T δ, nd reduce the computtion of δ(b) nd δ 1 (B) in line 3 to computing Post(B, δ) nd Pre(B, δ), respectively Emerson-Lei s lgorithm A stte q of A is live if some infinite pth strting t q visits ccepting sttes infinitely often. Clerly, A is nonempty if nd only if its initil stte is live. We descrie n lgorithm due to Emerson nd Lei for computing the set of live sttes. For every n 0, the n-live sttes of A re inductively defined s follows: every stte is 0-live; stte q is (n + 1)-live if some pth contining t lest one trnsition leds from q to n ccepting n-live stte. Loosely speking, stte is n-live if strting t it it is possile to visit ccepting sttes n-times. Let L[n] denote the set of n-live sttes of A. We hve: Lemm () L[n] L[n + for every n 0. () The sequence L[ L[ L[2]... reches fixpoint L[i] (i.e., there is lest index i 0 such tht L[i + = L[i]), nd L[i] is the set of live sttes.

272 272 CHAPTER 13. EMPTINESS CHECK: IMPLEMENTATIONS Proof: We prove () y induction on n. The cse n = 0 is trivil. Assume n > 0, nd let q L[n +. There is pth contining t lest one trnsition tht leds from q to n ccepting stte r L[n]. By induction hypothesis, r L[n, nd so q L[n]. To prove (), first notice tht, since Q is finite, the fixpoint L[i] exists. Let L e the set of live sttes. Clerly, L L[i] for every i 0. Moreover, since L[i] = L[i +, every stte of L[i] hs proper descendnt tht is ccepting nd elongs to L[i]. So L[i] L. Emerson-Lei s lgorithm computes the fixpoint L[i] of the sequence L[ L[ L[2].... To compute L[n + given L[n] we oserve tht stte is n + 1-live if some nonempty pth leds from it to n n-live ccepting stte, nd so L[n + = BckwrdBFS( Pre(L[n] F) ) The pseudocode for the lgorithm is shown elow on the left-hnd-side; the vrile L is used to store the elements of the sequence L[, L[, L[2],.... EmersonLei(A) Input: NBA A = (Q, Σ, δ, Q 0, F) Output: EMP if L ω (A) =, NEMP otherwise 1 L Q 2 repet 3 OldL L 4 L Pre(OldL F) 5 L BckwrdBFS(L) 6 until L = OldL 7 if q 0 L then report NEMP 8 else report NEMP EmersonLei2(A) Input: NBA A = (Q, Σ, δ, Q 0, F) Output: EMP if L ω (A) =, NEMP otherwise 1 L Q 2 repet 3 OldL L 4 L Pre(OldL F) \ OldL 5 L BckwrdBFS(L) OldL 6 until L = OldL 7 if q 0 L then report NEMP 8 else report NEMP The repet loop is executed t most Q + 1-times, ecuse ech itertion ut the lst one removes t lest one stte from L. Since ech itertion tkes O( Q + δ ) time, the lgorithm runs in O( Q ( Q + δ )) time. The lgorithm my compute the predecessors of stte twice. For instnce, if q F nd there is trnsition (q, q), then fter line 4 is executed the stte still elongs to L. The version on the right voids this prolem. Emerson-Lei s lgorithm cn e esily generlized to NGAs (we give only the generliztion of the first version):

273 13.2. ALGORITHMS BASED ON BREADTH-FIRST SEARCH 273 GenEmersonLei(A) Input: NGA A = (Q, Σ, δ, q 0, {F 0,..., F m 1 }) Output: EMP if L ω (A) =, NEMP otherwise 1 L Q 2 repet 3 OldL L 4 for i=0 to m 1 5 L Pre(OldL F i ) 6 L BckwrdBFS(L) 7 until L = OldL 8 if q 0 L then report NEMP 9 else report NEMP Proposition GenEmersonLei(A) reports NEMP iff A is nonempty. Proof: For every k 0, redefine the n-live sttes of A s follows: every stte is 0-live, nd q is (n + 1)-live if some pth hving t lest one trnsition leds from q to n-live stte of F (n mod m). Let L[n] denote the set of n-live sttes. Proceeding s in Lemm 13.20, we cn esily show tht L[(n + 1) m] L[n m] holds for every n 0. We clim tht the sequence L[ L[m] L[2 m]... reches fixpoint L[i m] (i.e., there is lest index i 0 such tht L[(i + 1) m] = L[i m]), nd L[i m] is the set of live sttes. Since Q is finite, the fixpoint L[i m] exists. Let q e live stte. There is pth strting t q tht visits F j infinitely often for every j {0,..., m 1}. In this pth, every occurrence of stte of F j is lwys followed y some lter occurrence of stte of F ( j+1) mod m, for every i {0,..., m 1}. So q L[i m]. We now show tht every stte of L[i m] is live. For every stte q L[(i + 1) m] there is pth π = π m 1 π m 2 π 0 such tht for every j {0,..., m 1} the segment π j contins t lest one trnsition nd leds to stte of L[i m + j] F j. In prticulr, π visits sttes of F 0,..., F m 1, nd, since L[(i + 1) m] = L[i m], it leds from stte of L[(i + 1) m] to nother stte of L[(i + 1) m]. So every stte of L[(i + 1) m] = L[i m] is live, which proves the clim. Since GenEmersonLei(A) computes the sequence L[ L[m] L[2 m]..., fter termintion L contins the set of live sttes A Modified Emerson-Lei s lgorithm There exist mny vrints of Emerson-Lei s lgorithm tht hve the sme worst-cse complexity, ut try to improve the efficiency, t lest in some cses, y mens of heuristics. We present here one of these vrints, which we cll the Modified Emerson-Lei s lgorithm (MEL). Given set S Q of sttes, let inf (S ) denote the sttes q S such tht some infinite pth strting t q contins only sttes of S. Insted of computing Pre(OldL F) t ech itertion step, MEL computes Pre(inf (OldL) F i ).

274 274 CHAPTER 13. EMPTINESS CHECK: IMPLEMENTATIONS MEL(A) Input: NGA A = (Q, Σ, δ, q 0, {F 0,..., F k 1 }) Output: EMP if L ω (A) =, NEMP otherwise 1 L Q; 2 repet 3 OldL L 4 L inf (OldL) 5 L Pre(L F) 6 L BckwrdBFS(L) 7 until L = OldL 8 if q 0 L then report NEMP 9 else report NEMP 10 function inf(s ) 11 repet 12 OldS S 13 S S Pre(S ) 14 until S = OldS 15 return S In the following we show tht MEL is correct, nd then compre it with Emerson-Lei s lgorithm. As we shll see, while MEL introduces the overhed of repetedly computing inf - opertions, it still mkes sense in mny cses ecuse it reduces the numer of executions of the repet loop. To prove correctness we clim tht fter termintion L contins the set of live sttes. Recll tht the set of live sttes is the fixpoint L[i] of the sequence L[ L[ L[2].... By the definition of liveness we hve inf (L[i]) = L[i]. Define now L [ = Q, nd L [n + = inf (pre + (L [i] α)). Clerly, MEL computes the sequence L [ L [ L [2].... Since L[n] L [n] L[i] for every n > 0, we hve tht L[i] is lso the fixpoint of the sequence L [ L [ L [2]..., nd so MEL computes L[i]. Since inf (S ) cn e computed in time O( Q + δ ) for ny set S, MEL runs in O( Q ( Q + δ )) time. Interestingly, we hve lredy met Emerson-Lei s lgorithm in Chpter??. In the proof of Proposition 12.3 we defined sequence D 0 D 1 D 2... of infinite cyclic grphs. In the terminology of this chpter, D 2i+1 ws otined from D 2i y removing ll nodes hving only finitely mny descendnts, nd D 2i+2 ws otined from D 2i+1 y removing ll nodes hving only non-ccepting descendnts. This corresponds to D 2i+1 := inf(d 2i ) nd D 2i+2 := pre + (D 2i+1 α). So, in fct, we cn look t this procedure s the computtion of the live sttes of D 0 using MEL.

275 13.2. ALGORITHMS BASED ON BREADTH-FIRST SEARCH Compring the lgorithms We give two fmilies of exmples showing tht MEL my outperform Emerson-lei s lgorithm, ut not lwys. A good cse for MEL. Consider the utomton of Figure The i-th itertion of Emnerson- Lei s lgorithm removes q n i+1 The numer of clls to BckwrdBFS is (n + 1), lthough simple modifiction llowing the lgorithm to stop if L = spres the (n + 1)-th opertion. On the other hnd, the first inf-opertion of MEL lredy sets the vrile L to the empty set of sttes, nd so, with the sme simple modifiction, the lgorithm stops fter on itertion. q 0 q 1 q n 1 q n Figure 13.8: An exmple in which the MEL-lgorithm outperforms the Emerson-Lei lgorithm A good cse for Emerson-Lei s lgorithm. Consider the utomton of Figure The i-th itertion, of Emerson-Lei s lgorithm removes q (n i+1),1 nd q (n i+1),2, nd so the lgorithm clls BckwrdBFS (n + 1) times The i-th itertion of MEL-lgorithm removes no stte s result of the inf -opertion, nd sttes q (n i+1),1 nd q (n i+1),2 s result of the cll to BckwrdBFS. So in this cse the inf -opertions re ll redundnt. q 0,1 q 0,2 q 1,1 q 1,2 q n,1 q n,2 Figure 13.9: An exmple in which the EL-lgorithm outperforms the MEL-lgorithm Exercises Exercise 113 Which lssos of the following NBA cn e found y run of NestedDFS? (Recll tht NestedDFS is nonderterministic lgorithm, nd so different runs on the sme input my return different lssos.)

276 276 CHAPTER 13. EMPTINESS CHECK: IMPLEMENTATIONS strt Exercise 114 A Büchi utomton is wek if no strongly connected component contins oth ccepting nd non-ccepting sttes. Show tht the following lgorithm correctly decides emptiness of wek Büchi utomt. Hint. Consider the root of scc contining only ccepting sttes. SingleDFS(A) Input: wek NBA A = (Q, Σ, δ, Q 0, F) Output: EMP if L ω (A) =, NEMP otherwise 1 S 2 dfs(q 0 ) 3 report EMP 4 proc dfs(q) 5 dd q to S 6 for ll r δ(q) do 7 if r S then dfs(r) 8 if r F then report NEMP 9 return Exercise 115 Consider Muller utomt whose ccepting condition contins one single set of sttes F, i.e., run ρ is ccepting if inf (ρ) = F. Trnsform TwoStck into liner lgorithm for checking emptiness of these utomt. Hint: Consider the version of TwoStck for NGAs. Exercise 116 (1) Given R, S Q, define pre + (R, S ) s the set of scendnts q of R such tht there is pth from q to R tht contins only sttes of S. Give n lgorithm to compute pre + (R, S ). (2) Consider the following modifiction of Emerson-Lei s lgorithm:

277 13.2. ALGORITHMS BASED ON BREADTH-FIRST SEARCH 277 MEL2(A) Input: NBA A = (Q, Σ, δ, Q 0, F) Output: EMP if L ω (A) =, NEMP otherwise 1 L Q 2 repet 3 OldL L 4 L pre + (L F, L) 5 until L = OldL 6 if q 0 L then report NEMP 7 else report NEMP Is MEL2 correct? Wht is the difference etween the sequences of sets computed y MEL nd MEL2?

278 278 CHAPTER 13. EMPTINESS CHECK: IMPLEMENTATIONS

279 Chpter 14 Applictions I: Verifiction nd Temporl Logic Recll tht, intuitively, liveness properties re those stting tht the system will eventully do something good. More formlly, they re properties tht re only violted y infinite executions of the systems, i.e., y exmining only finite prefix of n infinite execution it is not possile to determine whether the infinite execution violtes the property or not. In this chpter we pply the theory of Büchi utomt to the prolem of utomticlly verifying liveness properties Automt-Bsed Verifiction of Liveness Properties In Chpter 8 we introduced some sic concepts out systems: configurtion, possile execution, nd execution. We extend these notions to the infinite cse. An ω-execution of system is n infinite sequence c 0 c 1 c 2... of configurtions where c 0 is some initil configurtion, nd for every i 1 the configurtion c i is legl successor ccording to the semntics of the system of the configurtion c i 1. Notice tht ccording to this definition, if configurtion hs no legl successors then it does not elong to ny ω-execution. Usully this is undesirle, nd it is more convenient to ssume tht such configurtion c hs exctly one legl successor, nmely c itself. In this wy, every rechle configurtion of the system elongs to some ω-execution. The terminting executions re then the ω-executions of the form c 0... c n 1 c ω n for some terminting configurtion c n. The set of terminting configurtions cn usully e identified syntcticlly. For instnce, in progrm the terminting configurtions re usully those in which control is t some prticulr progrm line. In Chpter 8 we showed how to construct system NFA recognizing ll the executions of given system. The sme construction cn e used to define system NBA recognizing ll the ω-executions. Exmple 14.1 Consider the little progrm of Chpter

280 280 CHAPTER 14. APPLICATIONS I: VERIFICATION AND TEMPORAL LOGIC [5, 0, 1, 0, 0 5, 0, 0 [5, 0, i [1, 0, [1, 1, [2, 1, [4, 1, 1, 1, 0 2, 1, 0 [1, 1, [1, 0, [1, 1, 1, 0, 1 [5, 0, 5, 0, 1 [5, 0, [1, 0, 4, 1, 0 1, 1, 1 2, 1, 1 [2, 1, [3, 1, 3, 1, 1 [4, 0, 4, 0, 1 Figure 14.1: System NBA for the progrm 1 while x = 1 do 2 if y = 1 then 3 x 0 4 y 1 x 5 end Its system NFA is the utomton of 14.1, ut without the red self-loops t sttes [5, 0, nd [5, 0,. The system NBA is the result of dding the self-loops Checking Liveness Properties In Chpter 8 we used Lmport s lgorithm to present exmples of sfety properties, nd how they cn e utomticlly checked. We do the sme now for liveness properties. Figure 14.2 shows gin the network of utomt modelling the lgorithm nd its synchronous product, from which we cn esily gin its system NBA. Oserve tht in this cse every configurtion hs t lest successor, nd so no self-loops need to e dded. For i {0, 1}, let NC i, T i, C i e the sets of configurtions in which process i is in the non-criticl section, is trying to ccess the criticl section, nd is in the criticl section, respectively, nd let Σ stnd for the set of ll configurtions. The finite witing property for process i sttes tht if process i tries to ccess its criticl section, it eventully will. The possile ω-executions tht violte the property for process i re represented y the ω-regulr expression v i = Σ T i (Σ \ C i ) ω. We cn check this property using the sme technique s in Chpter 8. We construct the system NBA ωe recognizing the ω-executions of the lgorithm (the NBA hs just two sttes), nd trnsform the regulr expression v i into n NBA V i using the lgorithm of Chpter 11. We then

281 14.1. AUTOMATA-BASED VERIFICATION OF LIVENESS PROPERTIES , 0 = 0 0 1, 0 = 1 1 0, 1 = 0 1 1, 1 = i i 0 1 = = 0 nc 0 t 0 c = 1 q q 1 0 = 0 0 = = 0 nc 1 t 1 c = 0 0 = 0 1 = 1 0, 0, nc 0, q , 0, nc 0, nc 1 1, 0, c 0, nc 1 1 = = 1 1, 0, t 0, nc 1 1, 1, c 0, t = 1 0 = 1 1, 1, t 0, t 1 1, 1, c 0, q , 0, c 0, q = , 1, nc 0, q 1 1 = 0 1 = , 1, t 0, q 1 1, 0, t 0, q 1 1 = , 1, nc 0, t , 1, t 0, c = 1 1 = 1 0, 1, nc 0, c 1 1 = 1 Figure 14.2: Lmport s lgorithm nd its synchronous product.

282 282 CHAPTER 14. APPLICATIONS I: VERIFICATION AND TEMPORAL LOGIC construct n NBA for ωe V i using intersnba(), nd check its emptiness using one of the lgorithms of Chpter 13. Oserve tht, since ll sttes of ωe re ccepting, we do not need to use the specil lgorithm for intersection of NBAs, nd so we cn pply the construction for NFAs. The result of the check for process 0 yields tht the property fils ecuse for instnce of the ω-execution [0, 0, nc 0, nc 1 ] [1, 0, t 0, nc 1 ] [1, 1, t 0, t 1 ] ω In this execution oth processes request ccess to the criticl section, ut from then on process 1 never mkes ny further step. Only process 0 continues, ut ll it does is continuously check tht the current vlue of 1 is 1. Intuitively, this corresponds to process 1 reking down fter requesting ccess. But we do not expect the finite witing property to hold if processes my rek down while witing. So, in fct, our definition of the finite witing property is wrong. We cn repir the definition y reformulting the property s follows: in ny ω-execution in which oth processes execute infinitely mny steps, if process 0 tries to ccess its criticl section, then it eventully will. The condition tht oth processes must move infinitely often is clled firness ssumption. The simplest wy to solve this prolem is to enrich the lphet of the system NBA. Insted of leling trnsition only with the nme of the trget configurtion, we lso lel it with the numer of the process responsile for the move leding to tht configurtion. For instnce, the trnsition [1,0,t 0,nc 1 ] [0, 0, nc 0, nc 1 ] [1, 0, t 0, nc 1 ] ecomes ([1,0,t 0,nc 1 ],0) [0, 0, nc 0, nc 1 ] [1, 0, t 0, nc 1 ] to reflect the fct tht [1, 0, t 0, nc 1 ] is reched y move of process 0. So the new lphet of the NBA is Σ {0, 1}. If we denote M 0 = Σ {0} nd M 1 = Σ {1} for the moves of process 0 nd process 1, respectively, then the regulr expression inf = ( (M 0 + M 1 ) M 0 M 1 ) ω represents ll ω-executions in which oth processes move infinitely often, nd L(v i ) L(inf ) (where v i is suitly rewritten to ccount for the lrger lphet) is the set of violtions of the reformulted finite witing property. To check if some ω-execution is violtion, we cn construct NBAs for v i nd inf, nd compute their intersection. For process 0 the check yields tht the properly indeed holds. For process 1 the property still fils ecuse of, for instnce, the sequence ( [0, 0, nc 0, nc 1 ] [0, 1, nc 0, t 1 ] [1, 1, t 0, t 1 ] [1, 1, t 0, q 1 ] [1, 0, t 0, q 1 ] [1, 0, c 0, q 1 ] [0, 0, nc 0, q 1 ] )ω in which process 1 repetedly tries to ccess its criticl section, ut lwys lets process 0 ccess first.

283 14.2. LINEAR TEMPORAL LOGIC Liner Temporl Logic In Chpter 8 nd in the previous section we hve formlized properties of systems using regulr, or ω-regulr expressions, NFAs, or NBAs. This ecomes rther difficult for ll ut the esiest properties. For instnce, the NBA or the ω-regulr expression for the modified finite witing property re lredy quite involved, nd it is difficult to e convinced tht they correspond to the intended property. In this section we introduce new lnguge for specifying sfety nd liveness properties, clled Liner Temporl Logic (LTL). LTL is close to nturl lnguge, ut still hs forml semntics. Formuls of LTL re constructed from set AP of tomic propositions. Intuitively, tomic propositions re strct nmes for sic properties of configurtions, whose mening is fixed only fter concrete system is considered. Formlly, given system with set C of configurtions, the mening of the tomic propositions is fixed y vlution function V: AP 2 C tht ssigns to ech strct nme the set of configurtions t which it holds. Atomic propositions re comined y mens of the usul Boolen opertors nd the temporl opertors X ( next ) nd U ( until ). Intuitively, s first pproximtion Xϕ mens ϕ holds t the next configurtion (the configurtion reched fter one step of the progrm), nd ϕ U ψ mens ϕ holds until configurtion is reched stisfying ψ. Formlly, the syntx of LTL is defined s follows: Definition 14.2 Let AP e finite set of tomic propositions. The set of LTL formuls over AP, denoted y LTL(AP), is the set of expressions generted y the grmmr ϕ := true p ϕ 1 ϕ 1 ϕ 2 Xϕ 1 ϕ 1 U ϕ 2. Formuls re interpreted on sequences σ = σ 0 σ 1 σ 2..., where σ i AP for every i 0. We cll these sequences computtions. The set of ll computtions over AP is denoted y C(AP). The executle computtions of system re the computtions σ for which there exists n ω-execution c 0 c 1 c 2... such tht for every i 0 the set of tomic propositions stisfied y c i is exctly σ i. We now formlly define when computtionm stisfies formul. Definition 14.3 Given computtion σ C(AP), letσ j denote the suffix σ j σ j+1 σ j+2... of σ. The stisfction reltion σ = ϕ (red σ stisfies ϕ ) is inductively defined s follows: σ = true. σ = p iff p σ(0). σ = ϕ iff σ = ϕ. σ = ϕ 1 ϕ 2 iff σ = ϕ 1 nd σ = ϕ 2. σ = Xϕ iff σ 1 = ϕ.

284 284 CHAPTER 14. APPLICATIONS I: VERIFICATION AND TEMPORAL LOGIC σ = ϕ 1 U ϕ 2 iff there exists k 0 such tht σ k = ϕ 2 nd σ i = ϕ 1 for every 0 i < k. We use the following revitions: flse,, nd, interpreted in the usul wy. Fϕ = true U ϕ ( eventully ϕ ). According to the semntics ove, σ = Fϕ iff there exists k 0 such tht σ k = ϕ,. Gϕ = F ϕ ( lwys ϕ or glolly ϕ ). According to the semntics ove, σ = Gϕ iff σ k = ϕ for every k 0. The set of computtions tht stisfy formul ϕ is denoted y L(ϕ). A system stisfies ϕ if ll its executle computtions stisfy ϕ. Exmple 14.4 Consider the little progrm t the eginning of the chpter. We write some formuls expressing properties of the possile ω-executions of the progrm. Oserve tht the system NBA of Figure 14.1hs exctly four ω-executions: e 1 = [1, 0, [5, 0, ω e 2 = ( [1, 1, [2, 1, [4, 1, ) ω e 3 = [1, 0, [5, 0, ω e 4 = [1, 1, [2, 1, [3, 1, [4, 0, [1, 0, [5, 0, ω Let C e the set of configurtions of the progrm. We choose AP = {t 1, t 2,..., t 5, x=0, x=1, y=0, y=1} nd define the vlution function V: AP 2 C s follows: V(t i) = {[l, x, y] C l = i} for every i {1,..., 5}. V(x=0) = {[l, x, y] C x = 0}, nd similrly for x = 1, y = 0, y = 1. Under this vlution, t i expresses tht the progrm is t line i, nd x=j expresses tht the current vlue of x is j. The executle computtions corresponding to the four ω-executions ove re σ 1 = {t 1, x=0, y=0} {t 5, x=0, y=0} ω σ 2 = ( {t 1, x=1, y=0} {t 2, x=1, y=0} {t 4, x=1, y=0} ) ω σ 3 = {t 1, x=0, y=1} {t 5, x=0, y=1} ω σ 4 = {t 1, x=1, y=1} {t 2, x=1, y=1} {t 3, x=1, y=1} {t 4, x=0, y=1} {t 1, x=0, y=1} {t 5, x=0, y=1} ω We give some exmples of properties:

285 14.2. LINEAR TEMPORAL LOGIC 285 ϕ 0 = x=1 Xy=0 XXt 4. In nturl lnguge: the vlue of x in the first configurtion of the execution is 1, the vlue of y in the second configurtion is 0, nd in the third configurtion the progrm is t loction 4. We hve σ 2 = ϕ 0, nd σ 1, σ 3, σ 4 = ϕ 0. ϕ 1 = Fx=0. In nturl lnguge: x eventully gets the vlue 0. We hve σ 1, σ 2, σ 4 = ϕ 1, ut σ 3 = ϕ 1. ϕ 2 = x=0 U t 5. In nturl lnguge: x stys equl to 0 until the execution reches loction 5. Notice however tht the nturl lnguge description is miguous: Do executions tht never rech loction 5 stisfy the property? Do executions tht set x to 1 immeditely efore reching loction 5 stisfy the property? The forml definition removes the miguities: the nswer to the first question is no, to the second yes. We hve σ 1, σ 3 = ϕ 2 nd σ 2, σ 4 = ϕ 2. ϕ 3 = y=1 F(y=0 t 5) (F(y=0 Xy=1)). In nturl lnguge: the first configurtion stisfies y = 1, the execution termintes in configurtion with y = 0, nd y never decreses during the execution. This is one of the properties we nlyzed in Chpter 8, nd it is not stisfied y ny ω-execution. Exmple 14.5 We express severl properties of the Lmport-Bruns lgorithm (see Chpter 8) using LTL formuls. As system NBA we use the one in which trnsitions re leled with the nme of the trget configurtion, nd with the numer of the process responsile for the move leding to tht configurtion. We tke AP = {NC 0, T 0, C 0, NC 1, T 1, C 1, M 0, M 1 }, with the ovious vlution. The mutul exclusion property is expressed y the formul The lgorithm stisfies the formul. G( C 0 C 1 ) The property tht process i cnnot ccess the criticl section without hving requested it first is expressed y ( T i U C i ) Both processes stisfy this property. The nïve finite witing property for process i is expressed y G(T i FC i ) The modified version in which oth processes must execute infinitely mny moves is expressed (GFM 0 GFM 1 ) G(T i FC i )

286 286 CHAPTER 14. APPLICATIONS I: VERIFICATION AND TEMPORAL LOGIC Oserve how firness ssumptions cn e very elegntly expressed in LTL. The ssumption itself is expressed s formul ψ, nd the property tht ω-executions stisfying the firness ssumption lso stisfy ϕ is expressed y ψ ϕ. None of the processes stisfies the nïve version of the finite witing property. Process 0 stisfies the modified version, ut process 1 does not. The ounded overtking property for process 0 is expressed y G( T 0 ( C 1 U (C 1 U ( C 1 U C 0 )))) The formul sttes tht whenever T 0 holds, the computtion continues with (possily empty!) intervl t which we see C 1 holds, followed y (possily empty!) intervl t which C 1 holds, followed y point t which C 0 holds. The property holds. Exmple 14.6 Formlly speking, it is not correct to sy Xϕ mens tht the next configurtion stisfies ϕ or ϕ U ψ mens tht some future configurtion stisfies ψ, nd until then ll configurtions stisfy ϕ. The reson is tht formuls do not hold t configurtions, ut t computtions. Correct is: the suffix of the computtion strting t the next configurtion (which is lso computtion) stisfies ϕ, nd some suffix of the computtion stisfies ψ, nd until then ll suffixes stisfy ϕ. To illustrte this point, let AP = {p, q}, nd consider the formul ϕ = GFp U q. Then the computtion τ = {q} {p} ω stisfies ϕ. Indeed, the suffix {q} {p} ω stisfies q, nd ll lrger suffixes, tht is, {q} {p} ω nd τ itself, stisfy Fp From LTL formuls to generlized Büchi utomt We present n lgorithm tht, given formul ϕ LTL(AP) returns NGA A ϕ over the lphet 2 AP recognizing L(ϕ), nd then derive fully utomtic procedure tht, given system nd n LTL formul, decides whether the executle computtions of the system stisfy the formul Stisfction sequences nd Hintikk sequences We define the stisfction sequence nd the Hintikk sequence of computtion σ nd formul ϕ. We first need to introduce the notions of closure of formul, nd tom of the closure. Definition 14.7 Given formul ϕ, the negtion of ϕ is the formul ψ if ϕ = ψ, nd the formul ϕ otherwise. The closure cl(ϕ) of formul ϕ is the set contining ll suformuls of ϕ nd their negtions. A nonempty set α cl(ϕ) is n tom of cl(ϕ) if it stisfies the following properties:

287 14.3. FROM LTL FORMULAS TO GENERALIZED BÜCHI AUTOMATA 287 (0) If true cl(ϕ), then true α. (1) For every ϕ 1 ϕ 2 cl(ϕ): ϕ 1 ϕ 2 α if nd only if ϕ 1 α nd ϕ 2 α. (2) For every ϕ 1 cl(ϕ): ϕ 1 α if nd only if ϕ 1 α. The set of ll toms of cl(ϕ) is denoted y t(φ). Oserve tht if α is the set of ll formuls of cl(ϕ) stisfied y computtion σ, then α is necessrily n tom. Indeed, every computtion stisfies true; if computtion stisfies the conjunction of two formuls, then it stisfies ech of the conjuncts; finlly, if computtion stisfies formul, then it does not stisfy its negtion, nd vice vers. Notice s well tht, ecuse of (2), if cl(ϕ) contins k formuls, then every tom of cl(ϕ) contins exctly k/2 formuls. Exmple 14.8 The closure of the formul p (p U q) is { p, p, q, q, p U q, (p U q), p (p U q), ( p (p U q) ) }. We clim tht the only two toms contining p (p U q) re { p, q, p U q, p (p U q) } nd { p, q, p U q, p (p U q) }. Let us see why. By (2), n tom lwys contins either suformul or its negtion, ut not oth. So in principle there re 16 possiilities for toms, since we hve to choose exctly one of p nd p, q nd q, p U q nd (p U q), nd p (p U q) nd ( p (p U q) ). Since we look for toms contining p (p U q), we re left with 8 possiilities. But, y (1), every tom α contining p (p U q) must contin oth p nd p U q. So the only freedom left is the possiility to choose q or q. None of these choices violtes ny of the conditions, nd so exctly two toms contin p (p U q). Definition 14.9 The stisfction sequence for computtion σ nd formul ϕ is the infinite sequence of toms sts(σ, ϕ) = sts(σ, ϕ, 0) sts(σ, ϕ, 1) sts(σ, ϕ, 2)... where sts(σ, ϕ, i) is the tom contining the formuls of cl(ϕ) stisfied y σ i. Intuitively, the stisfction sequence of computtion σ is otined y completing σ: while σ only indictes which tomic propositions hold t ech point in time, the stisfction sequence lso indictes which tom holds t ech moment. Exmple Let ϕ = p U q, nd consider the computtions σ 1 = {p} ω, nd σ 2 = ({p} {q}) ω. We hve sts(σ 1, ϕ) = { p, q, (p U q) } ω sts(σ 2, ϕ) = ( {p, q, p U q } { p, q, p U q } ) ω

288 288 CHAPTER 14. APPLICATIONS I: VERIFICATION AND TEMPORAL LOGIC Oserve tht σ stisfies ϕ if nd only if nd only if ϕ sts(σ, ϕ, 0), i.e., if nd only if ϕ elongs to the first tom of σ. Stisfction sequences hve semntic definition: in order to know which tom holds t point one must know the semntics of LTL. Hintikk sequences provide syntctic chrcteriztion of stisfction sequences. The definition of Hintikk sequence does not involve the semntics of LTL, i.e., someone who ignores the semntics cn still determine whether given sequence is Hintikk sequence or not. We prove tht sequence is stisfction sequence if nd only if it is Hintikk sequence. Definition A pre-hintikk sequence for ϕ is n infinite sequence α 0 α 1 α 2... of toms stisfying the following conditions for every i 0: (l1) For every Xϕ cl(ϕ): Xϕ α i if nd only if ϕ α i+1. (l2) For every ϕ 1 U ϕ 2 cl(ϕ): ϕ 1 U ϕ 2 α i if nd only if ϕ 2 α i or ϕ 1 α i nd ϕ 1 U ϕ 2 α i+1. A pre-hintikk sequence is Hintikk sequence if it lso stisfies (g) For every ϕ 1 U ϕ 2 α i, there exists j i such tht ϕ 2 α j. A pre-hintikk or Hintikk sequence α mtches computtion σ if σ i α i for every i 0. Oserve tht conditions (l1) nd (l2) re locl: in order to determine if α stisfies them we only need to inspect every pir α i, α i+1 of consecutive toms. On the contrry, condition (g) is glol, since the distnce etween the indices i nd j cn e ritrrily lrge. Exmple Let ϕ = (p q) U (r s). Let α 1 = { p, q, r, s, ϕ }. The sequence α ω 1 is not Hintikk sequence for ϕ, ecuse α 1 is not n tom; indeed, y (1) every tom contining r nd s must contin r s. Let α 2 = { p, r, ϕ } ω. The sequence α ω 2 is not Hintikk sequence for ϕ, ecuse α 2 is not n tom; indeed, y (2) every tom mut contins either q or q, nd either s or s. Let α 3 = { p, q, r, s, r s, ϕ} ω. The sequence α ω 3 is not Hintikk sequence for ϕ, ecuse α 3 is not n tom; indeed, y (2) every tom must contin either (p q) or (p q). Let α 4 = { p, q, (p q) r, s, r s, ϕ}. The set α 4 is n tom, ut the sequence α ω 4 is not Hintikk sequence for ϕ, ecuse it violtes condition (l2): since α 4 contins (r s), it must lso contin ϕ. Let α 5 = { p, q, (p q), r, s, (r s), ϕ } ω. The set α 5 is n tom, nd the sequence α ω is pre-hintikk sequence. However, it is not Hintikk sequence ecuse it violtes 5 condition (g): since α 5 contins ϕ, some tom in the sequence must contin (r s), which is not the cse.

289 14.3. FROM LTL FORMULAS TO GENERALIZED BÜCHI AUTOMATA 289 Let α 6 = { p, q, (p q), r, s, (r s), ϕ }. The sequence ( α 5 α 6 ) ω is Hinktikk sequence for ϕ. It follows immeditely from the definition of Hintikk sequence tht if α = α 0 α 1 α 2... is stisfction sequence, then every pir α i, α i+1 stisfies (l1) nd (l2), nd the sequence α itself stisfies (g). So every stisfction sequence is Hintikk sequence. The following theorem shows tht the converse lso holds: every Hintikk sequence is stisfction sequence. Theorem Let σ e computtion nd let ϕ e formul. The unique Hintikk sequence for ϕ mtching σ is the stisfction sequence sts(σ, ϕ). Proof: As oserved ove, it follows immeditely from the definitions tht sts(σ, ϕ) is Hintikk sequence for ϕ mtching σ. To show tht no other Hintikk sequence mtches sts(σ, ϕ), let α = α 0 α 1 α 2... e Hintikk sequence for ϕ mtching σ, nd let ψ e n ritrry formul of cl(ϕ). We prove tht for every i 0: ψ α i if nd only if ψ sts(σ, ϕ, i). The proof is y induction on the structure of ψ. ψ = true. Then true sts(σ, ϕ, i) nd, since α i is n tom, true α i. ψ = p for n tomic proposition p. Since α mtches σ, we hve p α i if nd only if p σ i. By the definition of stisfction sequence, p σ i if nd only if p sts(σ, ϕ, i). So p α i if nd only if p sts(σ, ϕ, i). ψ = ϕ 1 ϕ 2. We hve ϕ 1 ϕ 2 α i ϕ 1 α i nd ϕ 2 α i (condition (1)) ϕ 1 sts(σ, ϕ, i) nd ϕ 2 sts(σ, ϕ, i) (induction hypothesis) ϕ 1 ϕ 2 sts(σ, ϕ, i) (definition of sts(σ, ϕ)) ψ = ϕ 1 or ψ = Xϕ 1. The proofs re very similr to the lst one. ψ = ϕ 1 U ϕ 2. We prove: () If ϕ 1 U ϕ 2 α i, then ϕ 1 U ϕ 2 sts(σ, ϕ, i). By condition (l2) of the definition of Hintikk sequence, we hve to consider two cses: ϕ 2 α i. By induction hypothesis, ϕ 2 sts(σ, ϕ), nd so ϕ 1 U ϕ 2 sts(σ, ϕ, i). ϕ 1 α i nd ϕ 1 U ϕ 2 α i+1. By condition (g), there is t lest one index j i such tht ϕ 2 α j. Let j m e the smllest of these indices. We prove the result y induction on j m i. If i = j m, then ϕ 2 α j, nd we proceed s in the cse ϕ 2 α i. If i < j m, then since ϕ 1 α i, we hve ϕ 1 sts(σ, ϕ, i) (induction on ψ). Since ϕ 1 U ϕ 2 α i+1,

290 290 CHAPTER 14. APPLICATIONS I: VERIFICATION AND TEMPORAL LOGIC we hve either ϕ 2 α i+1 or ϕ 1 α i+1. In the first cse we hve ϕ 2 sts(σ, ϕ, i + 1), nd so ϕ 1 U ϕ 2 sts(σ, ϕ, i). In the second cse, y induction hypothesis (induction on j m i), we hve ϕ 1 U ϕ 2 sts(σ, ϕ, i + 1), nd so ϕ 1 U ϕ 2 sts(σ, ϕ, i). () If ϕ 1 U ϕ 2 sts(σ, ϕ, i), then ϕ 1 U ϕ 2 α i. We consider gin two cses. ϕ 2 sts(σ, ϕ, i). By induction hypothesis, ϕ 2 α i, nd so ϕ 1 U ϕ 2 α i. ϕ 1 sts(σ, ϕ, i) nd ϕ 1 U ϕ 2 sts(σ, ϕ, i + 1). By the definition of stisfction sequence, there is t lest one index j i such tht ϕ 2 sts(σ, ϕ, j). Proceed now s in cse () Constructing the NGA for n LTL formul Given formul ϕ, we construct generlized Büchi utomton A ϕ recognizing L(ϕ). By the definition of stisfction sequence, computtion σ stisfies ϕ if nd only if ϕ sts(σ, ϕ, 0). Moreover, y Theorem sts(σ, ϕ) is the (unique) Hintikk sequence for ϕ mtching σ. So A ϕ must recognize the computtions σ stisfying: the first tom of the unique Hintikk sequence for ϕ mtching σ contins ϕ. To chieve this, we pply the following strtegy: () Define the sttes nd trnsitions of the utomton so tht the runs of A ϕ re ll the sequences σ 0 σ 1 σ 2 α 0 α 1 α 2... such tht σ = σ 0 σ 1... is computtion, nd α = α 0 α 1... is pre-hintikk sequence of ϕ mtching σ. () Define the sets of ccepting sttes of the utomton (recll tht A ϕ is NGA) so tht run is ccepting if nd only its corresponding pre-hintikk sequence is lso Hintikk sequence. Condition () determines the lphet, sttes, trnsitions, nd initil stte of A ϕ : - The lphet of A ϕ is 2 AP. - The sttes of A ϕ (prt from the distinguished initil stte q 0 ) re toms of ϕ. - The output trnsitions of stte α (where α is n tom) re the triples α σ β such tht σ mtches α, nd the pir α, β stisfies conditions (l1) nd (l2) (where α nd β ply the roles of α i resp. α i+1 ).

291 14.3. FROM LTL FORMULAS TO GENERALIZED BÜCHI AUTOMATA 291 The sets of ccepting sttes of A ϕ re determined y condition (). By the definition of Hin- σ 0 σ 1 σ 2 tikk sequence, we must gurntee tht in every run α 0 α 1 α 2..., if ny α i contins suformul ϕ 1 U ϕ 2, then there is j i such tht ϕ 2 α j. By condition (l2), this mounts to gurnteeing tht every run contins infinitely mny indices i such tht ϕ 2 α i, or infinitely mny indices j such tht (ϕ 1 U ϕ 2 ) α j. So we choose the sets of ccepting sttes s follows: - The ccepting condition contins set F ϕ1 U ϕ 2 of ccepting sttes for ech suformul ϕ 1 U ϕ 2 of ϕ. An tom elongs to F ϕ1 U ϕ 2 if it does not contin ϕ 1 U ϕ 2 or if it contins ϕ 2. The pseudocode for the trnsltion lgorithm is shown elow. LTLtoNGA(ϕ) Input: LTL-Formul ϕ over AP Output: NGA A ϕ = (Q, 2 AP, Q 0, δ, F) with L(A ϕ ) = L(ϕ) 1 Q 0 {α t(φ) ϕ α}; Q ;δ 2 W Q 0 3 while W do 4 pick α from W 5 dd α to Q 6 for ll ϕ 1 U ϕ 2 cl(ϕ) do 7 if ϕ 1 U ϕ 2 α or ϕ 2 α then dd α to F ϕ1 U ϕ 2 8 for ll β t(φ) do 9 if α, β stisfies (l1) nd (l2) then 10 dd (α, α AP, β) to δ 11 if β Q then dd β to W 12 F 13 for ll ϕ 1 U ϕ 2 cl(ϕ) do F F {F ϕ1 U ϕ 2 } 14 return (Q, 2 AP, Q 0, δ, F) Exmple We construct the utomton A ϕ for the formul ϕ = p U q. The closure cl(ϕ) hs eight toms, corresponding to ll the possile wys of choosing etween p nd p, q nd q, nd p U q nd (p U q). However, we cn esily see tht the toms {p, q, (p U q)}, { p, q, (p U q)}, nd { p, q, p U q} hve no output trnsitions, ecuse those trnsitions would violte condition (l2). So these sttes cn e removed, nd we re left with the five toms shown in Figure The three toms on the left contin p U q, nd so they ecome the initil sttes. Figure 14.3 uses some conventions to simplify the grphicl representtion. Oserve tht every trnsition of A ϕ leving n tom α is leled y α AP. For instnce, ll trnsitions leving the stte { p, q, p U q} re leled with {q}, nd ll trnsitions leving { p, q, (p U q)} re leled with. Therefore, since the lel of trnsition cn e deduced from its source stte, we omit them in the figure. Moreover, since ϕ only hs one suformul of the form ϕ 1 U ϕ 2, the NGA is in fct NBA, nd we cn represent the ccepting sttes s for NBAs. The ccepting sttes of F p U q re the toms tht do not

292 292 CHAPTER 14. APPLICATIONS I: VERIFICATION AND TEMPORAL LOGIC p, q, p U q p, q, p U q p, q, (p U q) p, q, (p U q) p, q, p U q Figure 14.3: NGA (NBA) for the formul p U q. contin p U q the two toms on the right nd the toms contining q the leftmost tom nd the tom t the top. Consider for exmple the toms α = { p, q, (p U q)} nd β = {p, q, p U q}. A ϕ contins trnsition α {p} β ecuse {p} mtches β, nd α, β stisfy conditions (l1) nd (l2). Condition (l1) holds vcuously, ecuse ϕ contins no suformuls of the form X ψ, while condition (l2) holds ecuse p U q α nd q β nd p α. On the other hnd, there is no trnsition from β to α ecuse it would violte condition (l2): p U q β, ut neither q β nor p U q α. NGAs otined from LTL formuls y mens of LTLtoNGA hve very prticulr structure: As oserved ove, ll trnsitions leving stte crry the sme lel. Every computtion ccepted y the NGA hs one single ccepting run. σ 0 sigm 1 By the definiiton of the NGA, if α 0 α 1 is n ccepting run, then α 0 α 1 α 2... is the stisfction sequence of σ 0 σ 1 σ Since the stisfction sequence of given computtion is y definition unique, there cn e only n ccepting run. The sets of computtions recognized y ny two distinct sttes of the NGA re disjoint. Let σ e computtion, nd let sts(σ, ϕ) = sts(σ, ϕ, 0) sts(σ, ϕ, 1)... e its stisfction sequence. Then σ is only ccepted from the stte sts(σ, ϕ, 0) Size of the NGA Let n e the length of the formul ϕ. It is esy to see tht the set cl(ϕ) hs size O(n). Therefore, the NGA A ϕ hs t most O(2 n ) sttes. Since ϕ contins t most n suformuls of the form ϕ 1 U ϕ 2, A ϕ hs t most n sets of finl sttes.

293 14.4. AUTOMATIC VERIFICATION OF LTL FORMULAS 293 We now prove mtching lower ound on the numer of sttes. We exhiit fmily of formuls {ϕ n } n 1 such tht ϕ n hs length O(n), nd every NGA recognizing L ω ϕ n hs t lest 2 n sttes. Consider the fmily of ω-lnguges {D n } n 1 over the lphet {0, 1, #} given y D n = {ww# ω w {0, 1} n }. We first show tht every NGA recognizing D n hs t lest 2 n sttes. Assume A = (Q, {0, 1, #}, δ, q 0, {F 1,..., F k }) recognizes D n nd Q < 2 n. Then for every word w {0, 1} n there is stte q w such tht A ccepts w# ω from q w. By the pigeonhole principle, q w1 = q w2 for two distinct words w 1, w 2 {0, 1} n. But then A ccepts w 1 w 2 # ω, which does not elong to D n, contrdicting the hypothesis. It now suffices to construct fmily of formuls {ϕ n } n 1 of size O(n) such tht L ω ϕ n = D n. For this, we tke AP = {0, 1, #} with the ovious vlution, nd construct the following formuls: ϕ n1 = G ( (0 1 #) (0 1) (0 #) (1 #) ). This formul expresses tht t every position exctly one proposition of AP holds. 2n 1 ϕ n2 = # X i # X2n G #. i=1 This formul expresses tht # does not hold t ny of the first 2n positions, nd it holds t ll lter positions. ϕ n3 = G( (0 X n (0 #)) (1 X n (1 #)) ). This formul expresses tht if the tomic proposition holding t position is 0 or 1, then n positions lter the tomic proposition holding is the sme one, or #. Clerly, ϕ n = ϕ n1 ϕ n2 ϕ n3 is the formul we re looking for. Oserve tht ϕ n contins O(n) chrcters Automtic Verifiction of LTL Formuls We cn now sketch the procedure for the utomtic verifiction of properties expressed y LTL formuls. The input to the procedure is system NBA A s otined either directly from the system, or y computing the synchronous product of network of utomt; formul ϕ of LTL over set of tomic propositions AP; nd vlution ν: AP 2 C, where C is the set of configurtions of A s, descriing for ech tomic proposition the set of configurtions t which the proposition holds. The procedure follows these steps: (1) Compute NGA A v for the negtion of the formul ϕ. A v recognizes ll the computtions tht violte ϕ.

294 294 CHAPTER 14. APPLICATIONS I: VERIFICATION AND TEMPORAL LOGIC (2) Compute NGA A v A s recognizing the executle computtions of the system tht violte the formul. (3) Check emptiness of A v A s. Step (1) cn e crried out y pplying LTLtoNGA, nd Step (3) y, sy, the two-stck lgorithm. For Step (2), oserve first tht the lphets of A v nd A s re different: the lphet of A v is 2 AP, while the lphet of A s is the set C of configurtions. By pplying the vlution ν we trnsform A v into n utomton with C s lphet. Since ll the sttes of system NBAs re ccepting, the utomton A v A s cn e computed y internfa. It is importnt to oserve tht the three steps cn e crried out simultneously. The sttes of A v A s re pirs [α, c], where α is n tom of ϕ, nd c is configurtion. The following lgorithm tkes pir [α, c] s input nd returns its successors in the NGA A v A s. The lgorithm first computes the successors of c in A s. Then, for ech successor c it computes first the set P of tomic propositions stisfying c ccording to the vlution, nd then the set of toms β such tht () β mtches P nd () the pir α, β stisfies conditions (l1) nd (l2). The successors of [α, c] re the pirs [β, c ]. Succ([α, c]) 1 S 2 for ll c δ s (c) do 3 P 4 for ll p AP do 5 if c ν(p) then dd p to P 6 for ll β t(φ) mtching P do 7 if α, β stisfies (l1) nd (l2) then dd c to S 8 return S This lgorithm cn e inserted in the lgorithm for the emptiness check. For instnce, if we use TwoStck, then we just replce line 6 y cll to Succ: 6 for ll r δ(q) do 6 for ll [β, c ] Succ([α, c]) do Exercises Exercise 117 (Sntos Lortory). The wek until opertor W hs the following semntics: σ = φ 1 W φ 2 iff there exists k 0 such tht σ k = φ 2 nd σ i = φ 1 for ll 0 i < k, or σ k = φ 2 for every k 0.

295 14.4. AUTOMATIC VERIFICATION OF LTL FORMULAS 295 Prove: p W q Gp (p U q) F p (p U q) p U (q Gp). Exercise 118 Let AP = {p, q} nd let Σ = 2 AP. Give LTL formuls defining the following lnguges: {p, q} Σ ω Σ {q} ω Σ ({p} + {p, q}) Σ {q} Σ ω {p} {q} ω Exercise 119 Let AP = {p, q}. Give NBAs ccepting the lnguges defined y the LTL formuls XG p, (GFp) (Fq), nd p XFp. Exercise 120 Two formuls φ, ψ of LTL re equivlent, denoted y φ ψ, if they re stisfied y the sme computtions. Which of the following equivlences hold? Xϕ Xψ Xϕ ψ Xϕ Xψ Xϕ ψ Fϕ Fψ Fϕ ψ Fϕ Fψ Fϕ ψ Gϕ Gψ Gϕ ψ Gϕ Gψ Gϕ ψ FXϕ XFϕ GXϕ XGϕ (ψ 1 U ϕ) (ψ 2 U ϕ) (ψ 1 ψ 2 ) U ϕ (ψ 1 U ϕ) (ψ 2 U ϕ) (ψ 1 ψ 2 ) U ϕ (ϕ U ψ 1 ) (ϕ U ψ 2 ) ϕ U (ψ 1 ψ 2 ) (ϕ U ψ 1 ) (ϕ U ψ 2 ) ϕ U (ψ 1 ψ 2 ) ψ U Fϕ Fϕ ψ U Gϕ Gϕ ψ U GFϕ GFϕ ψ U FGϕ GFϕ Exercise 121 Prove FGp VFGp nd GFp VGFp for every sequence V {F, G} of the temporl opertors F nd G. Exercise 122 (Sntos Lortory). Let AP = {p, q, r}. Give formuls tht hold for the computtions stisfying the following properties. If in dout out wht the property relly mens, choose n interprettion, nd explicitely indicte your choice. Here re two solved exmples: p is flse efore q: F(q) ( p U q). p ecomes true efore q: q W (p q). Now it is your turn: p is true etween q nd r. p precedes q efore r. p precedes q fter r. fter p nd q eventully r.

296 296 CHAPTER 14. APPLICATIONS I: VERIFICATION AND TEMPORAL LOGIC Exercise 123 (Schwoon). Which of the following formuls of LTL re tutologies? (A formul is tutology if ll computtions stisfy it.) If the formul is not tutology, give computtion tht does not stisfy it. Gp Fp G(p q) (Gp Gq) F(p q) (Fp Fq) Fp F Fp (Gp Fq) (p U ( p q)) (FGp GFq) G(p U ( p q)) G(p Xp) (p Gp). Exercise 124 In this exercise we show tht the reduction lgorithm of Exercise 105 does not reduce the B uchi utomt generted from LTL formuls, nd show tht little modifiction to LTLtoNGA cn llevite this prolem. Let ϕ e formul of LTL(AP), nd let A ϕ = LTLtoNGA(ϕ). (1) Prove tht the reduction lgorithm of Exercise 105 does not reduce A, tht is, show tht A = A/CSR. (2) Let B ϕ e the result of modifying A ϕ s follows: Add new stte q 0 nd mke it the unique initil stte. For every initil stte q of A ϕ, dd trnsition q 0 q AP q to B ϕ (recll tht q is n tom of cl(ϕ), nd so q AP is well defined). Replce every trnsition q 1 q 1 AP q 2 of A ϕ y q 1 Prove tht L ω (B ϕ ) = L ω (A ϕ ). q 2 AP q 2. (3) Construct the utomton B ϕ for the utomton of Figure (4) Apply the reduction lgorithm of Exercise 105 to B ϕ.

297 Chpter 15 Applictions II: Mondic Second-Order Logic nd Liner Arithmetic In Chpter 9 we showed tht the lnguges expressile in mondic second-order logic on finite words were exctly the regulr lnguges, nd derived n lgorithm tht, given formul, constructs n NFA ccepting exctly the set of interprettions of the formul. We show tht this result cn e esily extended to the cse of infinite words: in Section 15.1 we show tht the lnguges expressile in mondic second-order logic on ω-words re exctly the ω-regulr lnguges. In Chpter 10 we introduced Presurger Arithmetic, logicl lnguge for expressing properties of the integers, nd showed how to construct for given formul ϕ of Presurger Arithmetic n NFA A ϕ recognizing the solutions of ϕ. In Section 15.2 we extend this result to Liner Arithmetic, lnguge for descriing properties of rel numers with exctly the sme syntx s Presurger rithmetic Mondic Second-Order Logic on ω-words Mondic second-oder logic on ω-words hs the sme syntx s nd very similr semntics to its counterprt on finite words. Definition 15.1 Let X 1 = {x, y, z,...} nd X 2 = {X, Y, Z,...} e two infinite sets of first-order nd second-order vriles. Let Σ = {,, c,...} e finite lphet. The set MSO(Σ) of mondic second-order formuls over Σ is the set of expressions generted y the grmmr: ϕ := Q (x) x < y x X ϕ ϕ ϕ x ϕ X ϕ An interprettion of formul ϕ is pir (w, I) where w Σ ω, nd I is mpping tht ssigns every free first-order vrile x position I(x) N nd every free second-order vrile X set of positions I(X) N. (The mpping my lso ssign positions to other vriles.) The stisfction reltion (w, I) = ϕ etween formul ϕ of MSO(Σ) nd n interprettion (w, I) of ϕ is defined s follows: 297

298 298CHAPTER 15. APPLICATIONS II: MONADIC SECOND-ORDER LOGIC AND LINEAR ARITHMETIC (w, I) = Q (x) iff w[i(x)] = (w, I) = x < y iff I(x) < I(y) (w, I) = ϕ iff (w, I) = ϕ (w, I) = ϕ1 ϕ 2 iff (w, I) = ϕ 1 or (w, I) = ϕ 2 (w, I) = x ϕ iff w 1 nd some i N stisfies (w, I[i/x]) = ϕ (w, I) = x X iff I(x) I(X) (w, I) = X ϕ iff some S N stisfies (w, I[S/X]) = ϕ where w[i] is the letter of w t position i, I[i/x] is the interprettion tht ssigns i to x nd otherwise coincides with I, nd I[S/X] is the interprettion tht ssigns S to X nd otherwise coincides with I whether I is defined for i, X or not. If (w, I) = ϕ we sy tht (w, I) is model of ϕ. Two formuls re equivlent if they hve the sme models. The lnguge L(ϕ) of sentence ϕ MSO(Σ) is the set L(ϕ) = {w Σ ω w = φ}. An ω-lnguge L Σ ω is MSO-definle if L = L(ϕ) for some formul ϕ MSO(Σ) Expressive power of MSO(Σ) on ω-words We show tht the ω-lnguges expressile in mondic second-order logic re exctly the ω-regulr lnguges. The proof is very similr to its counterprtfor lnguges of finite words (Proposition 9.12), even it simpler. Proposition 15.2 If L Σ ω is regulr, then L is expressile in MSO(Σ). Proof: Let A = (Q, Σ, δ, Q 0, F) e NBA with Q = {q 0,..., q n } nd L(A) = L. We construct formul ϕ A such tht for every w Σ ω, w = ϕ A iff w L(A). We strt with some nottions. Let w = e n ω-word over Σ, nd let P q = { i N q ˆδ(q 0, 0... i ) }. In words, i P q iff A cn e in stte q immeditely fter reding the letter i. Then A ccepts w iff m q F P q. We construct formul Visits(X 0,... X n ) with free vriles X 0,... X n exctly s in Proposition The formul hs the property tht I(X i ) = P qi holds for every model (w, I) nd for every 0 i n. In words, Visits(X 0,... X n ) is only true when X i tkes the vlue P qi for every 0 i n. So we cn tke ϕ A := X 0... X n Visits(X 0,... X n ) x y y X i q i F We proceed to prove tht MSO-definle ω-lnguges re regulr. Given sentence ϕ MS O(Σ), we encode n interprettion (w, I) s n ω-word. We proceed s for finite words. Consider for instnce formul with first-order vriles x, y nd second-order vriles X, Y. Consider the interprettion

299 15.2. LINEAR ARITHMETIC 299 We encode it s () ω, x 2 y 6 X set of prime numers Y set of even numers corresponding to the ω-word x y X Y over Σ {0, 1} 4 Definition 15.3 Let ϕ e formul with n free vriles, nd let (w, I) e n interprettion of ϕ. We denote y enc(w, I) the word over the lphet Σ {0, 1} ω descried ove. The ω-lnguge of ϕ is L ω (ϕ) = {enc(w, I) (w, I) = ϕ}. We cn now prove y induction on the structure of ϕ tht L ω (ϕ) is ω-regulr. The proof is strightforwrd modifiction of the proof for the cse of finite words. The cse of negtion requires to replce the complementtion opertion for NFAs y the complementtion opertion for NBAs Liner Arithmetic Liner Arithmetic is lnguge for descriing properties of rel numers. It hs the sme syntx s Presurger rithmetic (see Chpter 10), ut formuls re interpreted over the rels, insted of over the nturls or the integers. Given formul ϕ or rel Presurger Arithmetic, we show how to construct NBA A ϕ recognizing the solutions of ϕ. Section discusses how to encode rel numers s strings, nd Section 15.3 constructs the NBA Encoding Rel Numers We encode rel numers s infinite strings in two steps. First we encode rels s pirs of numers, nd then these pirs s strings.

300 300CHAPTER 15. APPLICATIONS II: MONADIC SECOND-ORDER LOGIC AND LINEAR ARITHMETIC We encode rel numer x R s pir (x I, x F ), where x I Z, x F [0, nd x = x I + x F. We cll x I nd x F the integer nd frctionl prts of x. So, for instnce, (1, 1/3) encodes 4/3, nd ( 1, 2/3) encodes 1/3 (not 5/3). Every integer is encoded y two different pirs, e.g., 2 is encoded y (1, 1) nd (2, 0). We re not othered y this. (In the stndrd deciml representtion of rel numers integers lso hve two representtions, for exmple 2 is represented y oth 2 nd 1.9.) We encode pirs (x I, x F ) s infinite strings w I w F. The string w i is 2-complement encoding of x I (see Chpter 10). However, unlike Chpter 10, we use the msf insted of the lsf encoding (this is not essentil, it leds to more elegnt construction.) So w I is ny word w I = n n {0, 1} stisfying The string w F is ny ω-word {0, 1} ω stisfying n 1 x I = i 2 i 0 2 n (15.1) i=0 x F = i 2 i (15.2) i=1 The only word for which we hve x F = 1 is 1 ω. So, in prticulr, the encodings of the integer 1 re ω nd ω. Eqution 15.2 lso hs two solutions for frctions of the form 2 k. For instnce, the encodings of 1/2 re ω nd ω. Other frctions hve unique encoding: 0 0 (01) ω is the unique encoding of 1/3. Exmple 15.4 The encodings of 3 re ω nd ω. The encodings of 3.3 re (01) ω. The encodings of 3.75 re ω nd ω. Tuples of rels re encoded using pdding to mke the -symols fll on the sme column. For instnce, the encodings of the triple ( 6.75, 12.3, 3) re Constructing n NBA for the Rel Solutions Given rel Presurger formul ϕ, we construct NBA A ϕ ccepting the encodings of the solutions of ϕ. If ϕ is negtion, disjunction, or existentil quntifiction, we proceed s in Chpter 10, replcing the opertions on NFAs nd trnsducres y opertions on NBAs. Consider now n tomic formul ϕ = x. The NBA A ϕ must ccept the encodings of ll the tuples c R n stisfying c. We decompose the prolem into two suprolems for ω

301 15.3. CONSTRUCTING AN NBA FOR THE REAL SOLUTIONS 301 integer nd frctionl prts. Given c R n, let c I nd c F e the integer nd frctionl prt of c for some encoding of c. For instnce, if c = (2.3, 2.75, 1), then we cn hve c I = (2, 3, 1) nd c F = (0.3, 0.25, 0), corresponding to the encoding, sy, [010 (01) ω, ω, 01 0 ω ] or c I = (2, 3, 0) nd c F = (0.3, 0.25, 1), corresponding to, sy, [010 (01) ω, ω, 0 1 ω ]. Let α + (α ) e the sum of the positive (negtive) components of ; for instnce, if = (1, 2, 0, 3, 1) then α + = 4 nd α = 3. Since c F [0, n, we hve nd therefore, if c is solution of ϕ, then α c F α + (15.3) c I + c F (15.4) Putting together , we get tht c I + c F is solution of ϕ iff: c I α + ; or c I α (15.5) c I β for some integer β [ α + + 1, α ] nd c F β. If we denote β + = α nd β = α, then we cn decompose the solution spce of ϕ s follows: Sol(ϕ) = {c I + c F c I < β + } { c I + c F β + β β c I = β nd ; c F β } Exmple 15.5 We use ϕ = 2x y 0 s running exmple. We hve So x, y R is solution of ϕ iff: 2x I y I 2; or 2x I y I = 1 nd 2x F y F 1; or 2x I y I = 0 nd 2x F y F 0. α + = 2 α = 0 β + = 1 β = 0.

302 302CHAPTER 15. APPLICATIONS II: MONADIC SECOND-ORDER LOGIC AND LINEAR ARITHMETIC The solutions of c I < β + nd c I = β cn e computed using lgorithms IneqZNFA nd EqZNFA of Section Recll tht oth lgorithms use the lsf encoding, ut it is esy to trnsform their output into NFAs for the msf encoding: since the lgorithms deliver NFAs with exctly one finl stte, it suffices to reverse the trnsitions of the NFA, nd exchnge the initil nd finl sttes: the new utomton recognizes word w iff the old one recognizes its reverse w 1, nd so it recognizes exctly the msf-encodings. Exmple 15.6 Figure 15.1 shows NFAs for the solutions of 2x I y I 2 in lsf (left) nd msf encoding (right). The NFA on the right is otined y reversing the trnsition, nd exchnging the initil nd finl stte. Figure 15.2 shows NFAs for the solutions of 2x I y I = 1, lso in lsf nd msf encoding. [ 0 [ 0 [ [ 0 1, [ 1 [ [ 1 1, [ [ 0 0, [ 0 [ 1 [ [ 1 1, [ [ 0 0, [ [ [ 0 1 1,, q f q f [ 1 [ [ 1 1, [ [ [ 0 1 1,, [ [ 1 1, 2 [ ] [ ] [ 1 0 0, [ 0 [ [ 0 0, [ [ 0 1, Figure 15.1: NFAs for the solutions of 2x y 2 over Z with lsf (left) nd msf (right) encodings A NBA for the Solutions of x F β We construct DBA recognizing the solutions of x F β. The lgorithm is similr to AFtoNFA in Section The sttes of the DBA re integers. We choose trnsitions nd finl sttes so tht the following property holds: Stte q Z recognizes the encodings of the tuples c F [0, n such tht c F q. (15.6) However, recll tht α c F α + for every c F [0, n, nd therefore:

303 15.3. CONSTRUCTING AN NBA FOR THE REAL SOLUTIONS 303 [ 1 [ 1 1 [ 1 [ 1 1 [ 0 [ 1 q f [ [ ] q f [ 0 [ Figure 15.2: NFAs for the solutions of 2x y = 1 over Z with lsf (left) nd msf (right) encodings. ll sttes q α + ccept ll tuples of rels in [0, n, nd cn e merged with the stte α + ; ll sttes q < α ccept no tuples in [0, n, nd cn e merged with the stte α 1. Clling these two merged sttes ll nd none, the possile sttes of the DBA (not ll of them my e rechle from the initil stte) re ll, none, nd {q Z α q α + 1}. All these sttes ut none re finl, nd the initil stte is β. Let us now define the set of trnsitions. Given stte q nd letter ζ {0, 1} n, let us determine the trget stte q of the unique trnsition q ζ q. Clerly, if q = ll, then q = ll, nd if q = none, then q = none. If q Z, we compute the vlue v tht q must hve in order to stisfy property 15.6, nd then: If v [α, α +, we set q = v; if v < α, we set q = none, nd if q > α + 1, we set q = ll. To compute v, recll tht word w ({0, 1} n ) is ccepted from q iff the word ζw is ccepted from q. So the tuple c R n encoded y w nd the tuple c R n of rel numers encoded y ζw re linked y the eqution c = 1 2 ζ c (15.7) Since c is ccepted from q iff c is ccepted y q, to fulfil property 15.6 we must choose v so tht ( 1 2 ζ c ) q holds iff c v holds. We get v = q ζ, nd so we define the trnsition function of the DBA s follows: δ(q, ζ) = none if q = none or q ζ < α q ζ if α q ζ α + 1 ll if q = ll or α + 1 < q ζ Exmple 15.7 Figure 15.3 shows the DBA for the solutions of 2x F y F 1 (the stte none hs een omitted). Since α + = 2 nd α = 1, the possile sttes of the DBA re ll, none, nd

304 304CHAPTER 15. APPLICATIONS II: MONADIC SECOND-ORDER LOGIC AND LINEAR ARITHMETIC 1, 0, 1. The initil stte is 1. Let us determine the trget stte of the trnsitions leving stte 1. We instntite the definition of δ(q, ζ) with q = 1, α + = 2 nd α = 1, nd get which leds to δ(1, ζ) = none if 2ζ x + ζ y < 2 1 2ζ x + ζ y if 2 2ζ x + ζ y 0 ll if 0 < 2ζ x + ζ y δ(1, ζ) = 1 if ζ x = 0 nd ζ y = 0 ll if ζ x = 0 nd ζ y = 1 1 if ζ x = 1 nd ζ y = 0 0 if ζ x = 1 nd ζ y = 1 Recll tht, y property 15.6, stte q Z ccepts the encodings of the pirs (x F, y F ) [0, n such tht 2x F y F q. This llows us to immeditely derive the DBAs for 2x F y F 0 or 2x F y F 1: it is the DBA of Figure 15.3, with 0 or 1 s initil stte, respectively. [ ] [ ] [ 0 [ ,..., 0 [ ] [ ] 1 1 [ 1 [ 1 ll 0 ] [ 0 1 [ 0 [ 0 0 Figure 15.3: DBA for the solutions of 2x y 1 over [0, [0,. Exmple 15.8 Consider gin ϕ = 2x y 0. Recll tht (x, y) R 2 is solution of ϕ iff: (i) 2x I y I 2; or (ii) 2x I y I = 1 nd 2x F y F 1; or (iii) 2x I y I = 0 nd 2x F y F 0. Figure 15.4 shows t the top DBA for the x, y stisfying (i). It is esily otined from the NFA for the solutions of 2x I y I 2 shown on the right of Figure 15.1.

305 15.3. CONSTRUCTING AN NBA FOR THE REAL SOLUTIONS 305 The DBA t the ottom of Figure 15.4 recognizes the x, y R stisfying (ii) or (iii). To construct it, we conctente the DFA on the right of Figure 15.2, nd the DBA of Figure The DFA recognizes the integer solutions of 2x I y I = 1, which is dequte for (ii), ut chnging the finl stte to 0 we get DFA for the integer solutions of 2x I y I = 0, dequte for (iii). Simrly with the DBA, nd so it suffices to link stte 1 of the DFA to stte 1 of the DBA, nd stte 0 of the DFA to stte 0 of the DBA. [ ] [ [ 0 0 1, 1 [ 1 [ [ 1 1, [ [ 1 1, 2 [ ] [ ] [ 1 0 0, [ 0 [ [ 0 1, [ [ [ 0 1 1,, 0 [ [ 0 0, [ 1 [ 0 [ 1 [ 0 1 [ 1 [ 0 1 [ ] [ ] [ 0 [ 1 [ 1 [ [ 0 1,..., 0 [ 0 0 [ 0 1 [ 0 Figure 15.4: DBA for the rel solutions of 2x y 0 stisfying (i) (top) nd (ii) or (iii) (ottom).

Homework 3 Solutions

Homework 3 Solutions CS 341: Foundtions of Computer Science II Prof. Mrvin Nkym Homework 3 Solutions 1. Give NFAs with the specified numer of sttes recognizing ech of the following lnguges. In ll cses, the lphet is Σ = {,1}.

More information

Regular Sets and Expressions

Regular Sets and Expressions Regulr Sets nd Expressions Finite utomt re importnt in science, mthemtics, nd engineering. Engineers like them ecuse they re super models for circuits (And, since the dvent of VLSI systems sometimes finite

More information

One Minute To Learn Programming: Finite Automata

One Minute To Learn Programming: Finite Automata Gret Theoreticl Ides In Computer Science Steven Rudich CS 15-251 Spring 2005 Lecture 9 Fe 8 2005 Crnegie Mellon University One Minute To Lern Progrmming: Finite Automt Let me tech you progrmming lnguge

More information

EQUATIONS OF LINES AND PLANES

EQUATIONS OF LINES AND PLANES EQUATIONS OF LINES AND PLANES MATH 195, SECTION 59 (VIPUL NAIK) Corresponding mteril in the ook: Section 12.5. Wht students should definitely get: Prmetric eqution of line given in point-direction nd twopoint

More information

Reasoning to Solve Equations and Inequalities

Reasoning to Solve Equations and Inequalities Lesson4 Resoning to Solve Equtions nd Inequlities In erlier work in this unit, you modeled situtions with severl vriles nd equtions. For exmple, suppose you were given usiness plns for concert showing

More information

Section 5-4 Trigonometric Functions

Section 5-4 Trigonometric Functions 5- Trigonometric Functions Section 5- Trigonometric Functions Definition of the Trigonometric Functions Clcultor Evlution of Trigonometric Functions Definition of the Trigonometric Functions Alternte Form

More information

PROF. BOYAN KOSTADINOV NEW YORK CITY COLLEGE OF TECHNOLOGY, CUNY

PROF. BOYAN KOSTADINOV NEW YORK CITY COLLEGE OF TECHNOLOGY, CUNY MAT 0630 INTERNET RESOURCES, REVIEW OF CONCEPTS AND COMMON MISTAKES PROF. BOYAN KOSTADINOV NEW YORK CITY COLLEGE OF TECHNOLOGY, CUNY Contents 1. ACT Compss Prctice Tests 1 2. Common Mistkes 2 3. Distributive

More information

Math 135 Circles and Completing the Square Examples

Math 135 Circles and Completing the Square Examples Mth 135 Circles nd Completing the Squre Exmples A perfect squre is number such tht = b 2 for some rel number b. Some exmples of perfect squres re 4 = 2 2, 16 = 4 2, 169 = 13 2. We wish to hve method for

More information

0.1 Basic Set Theory and Interval Notation

0.1 Basic Set Theory and Interval Notation 0.1 Bsic Set Theory nd Intervl Nottion 3 0.1 Bsic Set Theory nd Intervl Nottion 0.1.1 Some Bsic Set Theory Notions Like ll good Mth ooks, we egin with definition. Definition 0.1. A set is well-defined

More information

Regular Languages and Finite Automata

Regular Languages and Finite Automata N Lecture Notes on Regulr Lnguges nd Finite Automt for Prt IA of the Computer Science Tripos Mrcelo Fiore Cmbridge University Computer Lbortory First Edition 1998. Revised 1999, 2000, 2001, 2002, 2003,

More information

FORMAL LANGUAGES, AUTOMATA AND THEORY OF COMPUTATION EXERCISES ON REGULAR LANGUAGES

FORMAL LANGUAGES, AUTOMATA AND THEORY OF COMPUTATION EXERCISES ON REGULAR LANGUAGES FORMAL LANGUAGES, AUTOMATA AND THEORY OF COMPUTATION EXERCISES ON REGULAR LANGUAGES Introduction This compendium contins exercises out regulr lnguges for the course Forml Lnguges, Automt nd Theory of Computtion

More information

CS99S Laboratory 2 Preparation Copyright W. J. Dally 2001 October 1, 2001

CS99S Laboratory 2 Preparation Copyright W. J. Dally 2001 October 1, 2001 CS99S Lortory 2 Preprtion Copyright W. J. Dlly 2 Octoer, 2 Ojectives:. Understnd the principle of sttic CMOS gte circuits 2. Build simple logic gtes from MOS trnsistors 3. Evlute these gtes to oserve logic

More information

A Visual and Interactive Input abb Automata. Theory Course with JFLAP 4.0

A Visual and Interactive Input abb Automata. Theory Course with JFLAP 4.0 Strt Puse Step Noninverted Tree A Visul nd Interctive Input Automt String ccepted! 5 nodes generted. Theory Course with JFLAP 4.0 q0 even 's, even 's q2 even 's, odd 's q1 odd 's, even 's q3 odd 's, odd

More information

Polynomial Functions. Polynomial functions in one variable can be written in expanded form as ( )

Polynomial Functions. Polynomial functions in one variable can be written in expanded form as ( ) Polynomil Functions Polynomil functions in one vrible cn be written in expnded form s n n 1 n 2 2 f x = x + x + x + + x + x+ n n 1 n 2 2 1 0 Exmples of polynomils in expnded form re nd 3 8 7 4 = 5 4 +

More information

Babylonian Method of Computing the Square Root: Justifications Based on Fuzzy Techniques and on Computational Complexity

Babylonian Method of Computing the Square Root: Justifications Based on Fuzzy Techniques and on Computational Complexity Bbylonin Method of Computing the Squre Root: Justifictions Bsed on Fuzzy Techniques nd on Computtionl Complexity Olg Koshelev Deprtment of Mthemtics Eduction University of Texs t El Pso 500 W. University

More information

Pentominoes. Pentominoes. Bruce Baguley Cascade Math Systems, LLC. The pentominoes are a simple-looking set of objects through which some powerful

Pentominoes. Pentominoes. Bruce Baguley Cascade Math Systems, LLC. The pentominoes are a simple-looking set of objects through which some powerful Pentominoes Bruce Bguley Cscde Mth Systems, LLC Astrct. Pentominoes nd their reltives the polyominoes, polycues, nd polyhypercues will e used to explore nd pply vrious importnt mthemticl concepts. In this

More information

Bayesian Updating with Continuous Priors Class 13, 18.05, Spring 2014 Jeremy Orloff and Jonathan Bloom

Bayesian Updating with Continuous Priors Class 13, 18.05, Spring 2014 Jeremy Orloff and Jonathan Bloom Byesin Updting with Continuous Priors Clss 3, 8.05, Spring 04 Jeremy Orloff nd Jonthn Bloom Lerning Gols. Understnd prmeterized fmily of distriutions s representing continuous rnge of hypotheses for the

More information

flex Regular Expressions and Lexical Scanning Regular Expressions and flex Examples on Alphabet A = {a,b} (Standard) Regular Expressions on Alphabet A

flex Regular Expressions and Lexical Scanning Regular Expressions and flex Examples on Alphabet A = {a,b} (Standard) Regular Expressions on Alphabet A flex Regulr Expressions nd Lexicl Scnning Using flex to Build Scnner flex genertes lexicl scnners: progrms tht discover tokens. Tokens re the smllest meningful units of progrm (or other string). flex is

More information

Example 27.1 Draw a Venn diagram to show the relationship between counting numbers, whole numbers, integers, and rational numbers.

Example 27.1 Draw a Venn diagram to show the relationship between counting numbers, whole numbers, integers, and rational numbers. 2 Rtionl Numbers Integers such s 5 were importnt when solving the eqution x+5 = 0. In similr wy, frctions re importnt for solving equtions like 2x = 1. Wht bout equtions like 2x + 1 = 0? Equtions of this

More information

Operations with Polynomials

Operations with Polynomials 38 Chpter P Prerequisites P.4 Opertions with Polynomils Wht you should lern: Write polynomils in stndrd form nd identify the leding coefficients nd degrees of polynomils Add nd subtrct polynomils Multiply

More information

LINEAR TRANSFORMATIONS AND THEIR REPRESENTING MATRICES

LINEAR TRANSFORMATIONS AND THEIR REPRESENTING MATRICES LINEAR TRANSFORMATIONS AND THEIR REPRESENTING MATRICES DAVID WEBB CONTENTS Liner trnsformtions 2 The representing mtrix of liner trnsformtion 3 3 An ppliction: reflections in the plne 6 4 The lgebr of

More information

Or more simply put, when adding or subtracting quantities, their uncertainties add.

Or more simply put, when adding or subtracting quantities, their uncertainties add. Propgtion of Uncertint through Mthemticl Opertions Since the untit of interest in n eperiment is rrel otined mesuring tht untit directl, we must understnd how error propgtes when mthemticl opertions re

More information

Solution to Problem Set 1

Solution to Problem Set 1 CSE 5: Introduction to the Theory o Computtion, Winter A. Hevi nd J. Mo Solution to Prolem Set Jnury, Solution to Prolem Set.4 ). L = {w w egin with nd end with }. q q q q, d). L = {w w h length t let

More information

Binary Representation of Numbers Autar Kaw

Binary Representation of Numbers Autar Kaw Binry Representtion of Numbers Autr Kw After reding this chpter, you should be ble to: 1. convert bse- rel number to its binry representtion,. convert binry number to n equivlent bse- number. In everydy

More information

9 CONTINUOUS DISTRIBUTIONS

9 CONTINUOUS DISTRIBUTIONS 9 CONTINUOUS DISTIBUTIONS A rndom vrible whose vlue my fll nywhere in rnge of vlues is continuous rndom vrible nd will be ssocited with some continuous distribution. Continuous distributions re to discrete

More information

A.7.1 Trigonometric interpretation of dot product... 324. A.7.2 Geometric interpretation of dot product... 324

A.7.1 Trigonometric interpretation of dot product... 324. A.7.2 Geometric interpretation of dot product... 324 A P P E N D I X A Vectors CONTENTS A.1 Scling vector................................................ 321 A.2 Unit or Direction vectors...................................... 321 A.3 Vector ddition.................................................

More information

Appendix D: Completing the Square and the Quadratic Formula. In Appendix A, two special cases of expanding brackets were considered:

Appendix D: Completing the Square and the Quadratic Formula. In Appendix A, two special cases of expanding brackets were considered: Appendi D: Completing the Squre nd the Qudrtic Formul Fctoring qudrtic epressions such s: + 6 + 8 ws one of the topics introduced in Appendi C. Fctoring qudrtic epressions is useful skill tht cn help you

More information

Use Geometry Expressions to create a more complex locus of points. Find evidence for equivalence using Geometry Expressions.

Use Geometry Expressions to create a more complex locus of points. Find evidence for equivalence using Geometry Expressions. Lerning Objectives Loci nd Conics Lesson 3: The Ellipse Level: Preclculus Time required: 120 minutes In this lesson, students will generlize their knowledge of the circle to the ellipse. The prmetric nd

More information

2 DIODE CLIPPING and CLAMPING CIRCUITS

2 DIODE CLIPPING and CLAMPING CIRCUITS 2 DIODE CLIPPING nd CLAMPING CIRCUITS 2.1 Ojectives Understnding the operting principle of diode clipping circuit Understnding the operting principle of clmping circuit Understnding the wveform chnge of

More information

1.2 The Integers and Rational Numbers

1.2 The Integers and Rational Numbers .2. THE INTEGERS AND RATIONAL NUMBERS.2 The Integers n Rtionl Numers The elements of the set of integers: consist of three types of numers: Z {..., 5, 4, 3, 2,, 0,, 2, 3, 4, 5,...} I. The (positive) nturl

More information

Algebra Review. How well do you remember your algebra?

Algebra Review. How well do you remember your algebra? Algebr Review How well do you remember your lgebr? 1 The Order of Opertions Wht do we men when we write + 4? If we multiply we get 6 nd dding 4 gives 10. But, if we dd + 4 = 7 first, then multiply by then

More information

P.3 Polynomials and Factoring. P.3 an 1. Polynomial STUDY TIP. Example 1 Writing Polynomials in Standard Form. What you should learn

P.3 Polynomials and Factoring. P.3 an 1. Polynomial STUDY TIP. Example 1 Writing Polynomials in Standard Form. What you should learn 33337_0P03.qp 2/27/06 24 9:3 AM Chpter P Pge 24 Prerequisites P.3 Polynomils nd Fctoring Wht you should lern Polynomils An lgeric epression is collection of vriles nd rel numers. The most common type of

More information

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall 2011 - Final Exam

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall 2011 - Final Exam 1./1.1 Introduction to Computers nd Engineering Problem Solving Fll 211 - Finl Exm Nme: MIT Emil: TA: Section: You hve 3 hours to complete this exm. In ll questions, you should ssume tht ll necessry pckges

More information

Graphs on Logarithmic and Semilogarithmic Paper

Graphs on Logarithmic and Semilogarithmic Paper 0CH_PHClter_TMSETE_ 3//00 :3 PM Pge Grphs on Logrithmic nd Semilogrithmic Pper OBJECTIVES When ou hve completed this chpter, ou should be ble to: Mke grphs on logrithmic nd semilogrithmic pper. Grph empiricl

More information

Factoring Polynomials

Factoring Polynomials Fctoring Polynomils Some definitions (not necessrily ll for secondry school mthemtics): A polynomil is the sum of one or more terms, in which ech term consists of product of constnt nd one or more vribles

More information

5 a LAN 6 a gateway 7 a modem

5 a LAN 6 a gateway 7 a modem STARTER With the help of this digrm, try to descrie the function of these components of typicl network system: 1 file server 2 ridge 3 router 4 ckone 5 LAN 6 gtewy 7 modem Another Novell LAN Router Internet

More information

6.2 Volumes of Revolution: The Disk Method

6.2 Volumes of Revolution: The Disk Method mth ppliction: volumes of revolution, prt ii Volumes of Revolution: The Disk Method One of the simplest pplictions of integrtion (Theorem ) nd the ccumultion process is to determine so-clled volumes of

More information

Physics 43 Homework Set 9 Chapter 40 Key

Physics 43 Homework Set 9 Chapter 40 Key Physics 43 Homework Set 9 Chpter 4 Key. The wve function for n electron tht is confined to x nm is. Find the normliztion constnt. b. Wht is the probbility of finding the electron in. nm-wide region t x

More information

2005-06 Second Term MAT2060B 1. Supplementary Notes 3 Interchange of Differentiation and Integration

2005-06 Second Term MAT2060B 1. Supplementary Notes 3 Interchange of Differentiation and Integration Source: http://www.mth.cuhk.edu.hk/~mt26/mt26b/notes/notes3.pdf 25-6 Second Term MAT26B 1 Supplementry Notes 3 Interchnge of Differentition nd Integrtion The theme of this course is bout vrious limiting

More information

Unit 6: Exponents and Radicals

Unit 6: Exponents and Radicals Eponents nd Rdicls -: The Rel Numer Sstem Unit : Eponents nd Rdicls Pure Mth 0 Notes Nturl Numers (N): - counting numers. {,,,,, } Whole Numers (W): - counting numers with 0. {0,,,,,, } Integers (I): -

More information

4.11 Inner Product Spaces

4.11 Inner Product Spaces 314 CHAPTER 4 Vector Spces 9. A mtrix of the form 0 0 b c 0 d 0 0 e 0 f g 0 h 0 cnnot be invertible. 10. A mtrix of the form bc d e f ghi such tht e bd = 0 cnnot be invertible. 4.11 Inner Product Spces

More information

Example A rectangular box without lid is to be made from a square cardboard of sides 18 cm by cutting equal squares from each corner and then folding

Example A rectangular box without lid is to be made from a square cardboard of sides 18 cm by cutting equal squares from each corner and then folding 1 Exmple A rectngulr box without lid is to be mde from squre crdbord of sides 18 cm by cutting equl squres from ech corner nd then folding up the sides. 1 Exmple A rectngulr box without lid is to be mde

More information

Lec 2: Gates and Logic

Lec 2: Gates and Logic Lec 2: Gtes nd Logic Kvit Bl CS 34, Fll 28 Computer Science Cornell University Announcements Clss newsgroup creted Posted on we-pge Use it for prtner finding First ssignment is to find prtners Due this

More information

Object Semantics. 6.170 Lecture 2

Object Semantics. 6.170 Lecture 2 Object Semntics 6.170 Lecture 2 The objectives of this lecture re to: to help you become fmilir with the bsic runtime mechnism common to ll object-oriented lnguges (but with prticulr focus on Jv): vribles,

More information

. At first sight a! b seems an unwieldy formula but use of the following mnemonic will possibly help. a 1 a 2 a 3 a 1 a 2

. At first sight a! b seems an unwieldy formula but use of the following mnemonic will possibly help. a 1 a 2 a 3 a 1 a 2 7 CHAPTER THREE. Cross Product Given two vectors = (,, nd = (,, in R, the cross product of nd written! is defined to e: " = (!,!,! Note! clled cross is VECTOR (unlike which is sclr. Exmple (,, " (4,5,6

More information

Basic Analysis of Autarky and Free Trade Models

Basic Analysis of Autarky and Free Trade Models Bsic Anlysis of Autrky nd Free Trde Models AUTARKY Autrky condition in prticulr commodity mrket refers to sitution in which country does not engge in ny trde in tht commodity with other countries. Consequently

More information

Mathematics. Vectors. hsn.uk.net. Higher. Contents. Vectors 128 HSN23100

Mathematics. Vectors. hsn.uk.net. Higher. Contents. Vectors 128 HSN23100 hsn.uk.net Higher Mthemtics UNIT 3 OUTCOME 1 Vectors Contents Vectors 18 1 Vectors nd Sclrs 18 Components 18 3 Mgnitude 130 4 Equl Vectors 131 5 Addition nd Subtrction of Vectors 13 6 Multipliction by

More information

Integration by Substitution

Integration by Substitution Integrtion by Substitution Dr. Philippe B. Lvl Kennesw Stte University August, 8 Abstrct This hndout contins mteril on very importnt integrtion method clled integrtion by substitution. Substitution is

More information

Automated Grading of DFA Constructions

Automated Grading of DFA Constructions Automted Grding of DFA Constructions Rjeev Alur nd Loris D Antoni Sumit Gulwni Dileep Kini nd Mhesh Viswnthn Deprtment of Computer Science Microsoft Reserch Deprtment of Computer Science University of

More information

and thus, they are similar. If k = 3 then the Jordan form of both matrices is

and thus, they are similar. If k = 3 then the Jordan form of both matrices is Homework ssignment 11 Section 7. pp. 249-25 Exercise 1. Let N 1 nd N 2 be nilpotent mtrices over the field F. Prove tht N 1 nd N 2 re similr if nd only if they hve the sme miniml polynomil. Solution: If

More information

Unambiguous Recognizable Two-dimensional Languages

Unambiguous Recognizable Two-dimensional Languages Unmbiguous Recognizble Two-dimensionl Lnguges Mrcell Anselmo, Dor Gimmrresi, Mri Mdoni, Antonio Restivo (Univ. of Slerno, Univ. Rom Tor Vergt, Univ. of Ctni, Univ. of Plermo) W2DL, My 26 REC fmily I REC

More information

9.3. The Scalar Product. Introduction. Prerequisites. Learning Outcomes

9.3. The Scalar Product. Introduction. Prerequisites. Learning Outcomes The Sclr Product 9.3 Introduction There re two kinds of multipliction involving vectors. The first is known s the sclr product or dot product. This is so-clled becuse when the sclr product of two vectors

More information

FAULT TREES AND RELIABILITY BLOCK DIAGRAMS. Harry G. Kwatny. Department of Mechanical Engineering & Mechanics Drexel University

FAULT TREES AND RELIABILITY BLOCK DIAGRAMS. Harry G. Kwatny. Department of Mechanical Engineering & Mechanics Drexel University SYSTEM FAULT AND Hrry G. Kwtny Deprtment of Mechnicl Engineering & Mechnics Drexel University OUTLINE SYSTEM RBD Definition RBDs nd Fult Trees System Structure Structure Functions Pths nd Cutsets Reliility

More information

All pay auctions with certain and uncertain prizes a comment

All pay auctions with certain and uncertain prizes a comment CENTER FOR RESEARC IN ECONOMICS AND MANAGEMENT CREAM Publiction No. 1-2015 All py uctions with certin nd uncertin prizes comment Christin Riis All py uctions with certin nd uncertin prizes comment Christin

More information

Protocol Analysis. 17-654/17-764 Analysis of Software Artifacts Kevin Bierhoff

Protocol Analysis. 17-654/17-764 Analysis of Software Artifacts Kevin Bierhoff Protocol Anlysis 17-654/17-764 Anlysis of Softwre Artifcts Kevin Bierhoff Tke-Awys Protocols define temporl ordering of events Cn often be cptured with stte mchines Protocol nlysis needs to py ttention

More information

Words Symbols Diagram. abcde. a + b + c + d + e

Words Symbols Diagram. abcde. a + b + c + d + e Logi Gtes nd Properties We will e using logil opertions to uild mhines tht n do rithmeti lultions. It s useful to think of these opertions s si omponents tht n e hooked together into omplex networks. To

More information

Concept Formation Using Graph Grammars

Concept Formation Using Graph Grammars Concept Formtion Using Grph Grmmrs Istvn Jonyer, Lwrence B. Holder nd Dine J. Cook Deprtment of Computer Science nd Engineering University of Texs t Arlington Box 19015 (416 Ytes St.), Arlington, TX 76019-0015

More information

RTL Power Optimization with Gate-level Accuracy

RTL Power Optimization with Gate-level Accuracy RTL Power Optimiztion with Gte-level Accurcy Qi Wng Cdence Design Systems, Inc Sumit Roy Clypto Design Systems, Inc 555 River Oks Prkwy, Sn Jose 95125 2903 Bunker Hill Lne, Suite 208, SntClr 95054 [email protected]

More information

Vectors 2. 1. Recap of vectors

Vectors 2. 1. Recap of vectors Vectors 2. Recp of vectors Vectors re directed line segments - they cn be represented in component form or by direction nd mgnitude. We cn use trigonometry nd Pythgors theorem to switch between the forms

More information

On the expressive power of temporal logic

On the expressive power of temporal logic On the expressive power of temporl logic Joëlle Cohen, Dominique Perrin nd Jen-Eric Pin LITP, Pris, FRANCE Astrct We study the expressive power of liner propositionl temporl logic interpreted on finite

More information

Learning Workflow Petri Nets

Learning Workflow Petri Nets Lerning Workflow Petri Nets Jvier Esprz, Mrtin Leucker, nd Mximilin Schlund Technische Universität München, Boltzmnnstr. 3, 85748 Grching, Germny {esprz,leucker,schlund}@in.tum.de Abstrct. Workflow mining

More information

MATH 150 HOMEWORK 4 SOLUTIONS

MATH 150 HOMEWORK 4 SOLUTIONS MATH 150 HOMEWORK 4 SOLUTIONS Section 1.8 Show tht the product of two of the numbers 65 1000 8 2001 + 3 177, 79 1212 9 2399 + 2 2001, nd 24 4493 5 8192 + 7 1777 is nonnegtive. Is your proof constructive

More information

On decidability of LTL model checking for process rewrite systems

On decidability of LTL model checking for process rewrite systems Act Informtic (2009) 46:1 28 DOI 10.1007/s00236-008-0082-3 ORIGINAL ARTICLE On decidbility of LTL model checking for process rewrite systems Lur Bozzelli Mojmír Křetínský Vojtěch Řehák Jn Strejček Received:

More information

A formal model for databases in DNA

A formal model for databases in DNA A forml model for dtses in DNA Joris J.M. Gillis nd Jn Vn den Bussche Hsselt University nd trnsntionl University of Limurg Astrct Our gol is to etter understnd, t theoreticl level, the dtse spects of DNA

More information

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 16 th May 2008. Time: 14:00 16:00

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 16 th May 2008. Time: 14:00 16:00 COMP20212 Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE Digitl Design Techniques Dte: Fridy 16 th My 2008 Time: 14:00 16:00 Plese nswer ny THREE Questions from the FOUR questions provided

More information

Rotating DC Motors Part II

Rotating DC Motors Part II Rotting Motors rt II II.1 Motor Equivlent Circuit The next step in our consiertion of motors is to evelop n equivlent circuit which cn be use to better unerstn motor opertion. The rmtures in rel motors

More information

Lectures 8 and 9 1 Rectangular waveguides

Lectures 8 and 9 1 Rectangular waveguides 1 Lectures 8 nd 9 1 Rectngulr wveguides y b x z Consider rectngulr wveguide with 0 < x b. There re two types of wves in hollow wveguide with only one conductor; Trnsverse electric wves

More information

Basic Research in Computer Science BRICS RS-02-13 Brodal et al.: Solving the String Statistics Problem in Time O(n log n)

Basic Research in Computer Science BRICS RS-02-13 Brodal et al.: Solving the String Statistics Problem in Time O(n log n) BRICS Bsic Reserch in Computer Science BRICS RS-02-13 Brodl et l.: Solving the String Sttistics Prolem in Time O(n log n) Solving the String Sttistics Prolem in Time O(n log n) Gerth Stølting Brodl Rune

More information

Integration. 148 Chapter 7 Integration

Integration. 148 Chapter 7 Integration 48 Chpter 7 Integrtion 7 Integrtion t ech, by supposing tht during ech tenth of second the object is going t constnt speed Since the object initilly hs speed, we gin suppose it mintins this speed, but

More information

Experiment 6: Friction

Experiment 6: Friction Experiment 6: Friction In previous lbs we studied Newton s lws in n idel setting, tht is, one where friction nd ir resistnce were ignored. However, from our everydy experience with motion, we know tht

More information

Java CUP. Java CUP Specifications. User Code Additions You may define Java code to be included within the generated parser:

Java CUP. Java CUP Specifications. User Code Additions You may define Java code to be included within the generated parser: Jv CUP Jv CUP is prser-genertion tool, similr to Ycc. CUP uilds Jv prser for LALR(1) grmmrs from production rules nd ssocited Jv code frgments. When prticulr production is recognized, its ssocited code

More information

Hillsborough Township Public Schools Mathematics Department Computer Programming 1

Hillsborough Township Public Schools Mathematics Department Computer Programming 1 Essentil Unit 1 Introduction to Progrmming Pcing: 15 dys Common Unit Test Wht re the ethicl implictions for ming in tody s world? There re ethicl responsibilities to consider when writing computer s. Citizenship,

More information

5.2. LINE INTEGRALS 265. Let us quickly review the kind of integrals we have studied so far before we introduce a new one.

5.2. LINE INTEGRALS 265. Let us quickly review the kind of integrals we have studied so far before we introduce a new one. 5.2. LINE INTEGRALS 265 5.2 Line Integrls 5.2.1 Introduction Let us quickly review the kind of integrls we hve studied so fr before we introduce new one. 1. Definite integrl. Given continuous rel-vlued

More information

Learning Outcomes. Computer Systems - Architecture Lecture 4 - Boolean Logic. What is Logic? Boolean Logic 10/28/2010

Learning Outcomes. Computer Systems - Architecture Lecture 4 - Boolean Logic. What is Logic? Boolean Logic 10/28/2010 /28/2 Lerning Outcomes At the end of this lecture you should: Computer Systems - Architecture Lecture 4 - Boolen Logic Eddie Edwrds [email protected] http://www.doc.ic.c.uk/~eedwrds/compsys (Hevily sed

More information

Decision Rule Extraction from Trained Neural Networks Using Rough Sets

Decision Rule Extraction from Trained Neural Networks Using Rough Sets Decision Rule Extrction from Trined Neurl Networks Using Rough Sets Alin Lzr nd Ishwr K. Sethi Vision nd Neurl Networks Lbortory Deprtment of Computer Science Wyne Stte University Detroit, MI 48 ABSTRACT

More information

An Undergraduate Curriculum Evaluation with the Analytic Hierarchy Process

An Undergraduate Curriculum Evaluation with the Analytic Hierarchy Process An Undergrdute Curriculum Evlution with the Anlytic Hierrchy Process Les Frir Jessic O. Mtson Jck E. Mtson Deprtment of Industril Engineering P.O. Box 870288 University of Albm Tuscloos, AL. 35487 Abstrct

More information

Vector differentiation. Chapters 6, 7

Vector differentiation. Chapters 6, 7 Chpter 2 Vectors Courtesy NASA/JPL-Cltech Summry (see exmples in Hw 1, 2, 3) Circ 1900 A.D., J. Willird Gis invented useful comintion of mgnitude nd direction clled vectors nd their higher-dimensionl counterprts

More information

Section 5.2, Commands for Configuring ISDN Protocols. Section 5.3, Configuring ISDN Signaling. Section 5.4, Configuring ISDN LAPD and Call Control

Section 5.2, Commands for Configuring ISDN Protocols. Section 5.3, Configuring ISDN Signaling. Section 5.4, Configuring ISDN LAPD and Call Control Chpter 5 Configurtion of ISDN Protocols This chpter provides instructions for configuring the ISDN protocols in the SP201 for signling conversion. Use the sections tht reflect the softwre you re configuring.

More information

Small Businesses Decisions to Offer Health Insurance to Employees

Small Businesses Decisions to Offer Health Insurance to Employees Smll Businesses Decisions to Offer Helth Insurnce to Employees Ctherine McLughlin nd Adm Swinurn, June 2014 Employer-sponsored helth insurnce (ESI) is the dominnt source of coverge for nonelderly dults

More information

MODULE 3. 0, y = 0 for all y

MODULE 3. 0, y = 0 for all y Topics: Inner products MOULE 3 The inner product of two vectors: The inner product of two vectors x, y V, denoted by x, y is (in generl) complex vlued function which hs the following four properties: i)

More information

Solutions for Selected Exercises from Introduction to Compiler Design

Solutions for Selected Exercises from Introduction to Compiler Design Solutions for Selected Exercises from Introduction to Compiler Design Torben Æ. Mogensen Lst updte: My 30, 2011 1 Introduction This document provides solutions for selected exercises from Introduction

More information

FUNCTIONS AND EQUATIONS. xεs. The simplest way to represent a set is by listing its members. We use the notation

FUNCTIONS AND EQUATIONS. xεs. The simplest way to represent a set is by listing its members. We use the notation FUNCTIONS AND EQUATIONS. SETS AND SUBSETS.. Definition of set. A set is ny collection of objects which re clled its elements. If x is n element of the set S, we sy tht x belongs to S nd write If y does

More information

19. The Fermat-Euler Prime Number Theorem

19. The Fermat-Euler Prime Number Theorem 19. The Fermt-Euler Prime Number Theorem Every prime number of the form 4n 1 cn be written s sum of two squres in only one wy (side from the order of the summnds). This fmous theorem ws discovered bout

More information

COMPONENTS: COMBINED LOADING

COMPONENTS: COMBINED LOADING LECTURE COMPONENTS: COMBINED LOADING Third Edition A. J. Clrk School of Engineering Deprtment of Civil nd Environmentl Engineering 24 Chpter 8.4 by Dr. Ibrhim A. Asskkf SPRING 2003 ENES 220 Mechnics of

More information

RIGHT TRIANGLES AND THE PYTHAGOREAN TRIPLETS

RIGHT TRIANGLES AND THE PYTHAGOREAN TRIPLETS RIGHT TRIANGLES AND THE PYTHAGOREAN TRIPLETS Known for over 500 yers is the fct tht the sum of the squres of the legs of right tringle equls the squre of the hypotenuse. Tht is +b c. A simple proof is

More information

AntiSpyware Enterprise Module 8.5

AntiSpyware Enterprise Module 8.5 AntiSpywre Enterprise Module 8.5 Product Guide Aout the AntiSpywre Enterprise Module The McAfee AntiSpywre Enterprise Module 8.5 is n dd-on to the VirusScn Enterprise 8.5i product tht extends its ility

More information

DlNBVRGH + Sickness Absence Monitoring Report. Executive of the Council. Purpose of report

DlNBVRGH + Sickness Absence Monitoring Report. Executive of the Council. Purpose of report DlNBVRGH + + THE CITY OF EDINBURGH COUNCIL Sickness Absence Monitoring Report Executive of the Council 8fh My 4 I.I...3 Purpose of report This report quntifies the mount of working time lost s result of

More information

Vendor Rating for Service Desk Selection

Vendor Rating for Service Desk Selection Vendor Presented By DATE Using the scores of 0, 1, 2, or 3, plese rte the vendor's presenttion on how well they demonstrted the functionl requirements in the res below. Also consider how efficient nd functionl

More information

Chapter. Contents: A Constructing decimal numbers

Chapter. Contents: A Constructing decimal numbers Chpter 9 Deimls Contents: A Construting deiml numers B Representing deiml numers C Deiml urreny D Using numer line E Ordering deimls F Rounding deiml numers G Converting deimls to frtions H Converting

More information

Lecture 3 Gaussian Probability Distribution

Lecture 3 Gaussian Probability Distribution Lecture 3 Gussin Probbility Distribution Introduction l Gussin probbility distribution is perhps the most used distribution in ll of science. u lso clled bell shped curve or norml distribution l Unlike

More information

Review guide for the final exam in Math 233

Review guide for the final exam in Math 233 Review guide for the finl exm in Mth 33 1 Bsic mteril. This review includes the reminder of the mteril for mth 33. The finl exm will be cumultive exm with mny of the problems coming from the mteril covered

More information

Virtual Machine. Part II: Program Control. Building a Modern Computer From First Principles. www.nand2tetris.org

Virtual Machine. Part II: Program Control. Building a Modern Computer From First Principles. www.nand2tetris.org Virtul Mchine Prt II: Progrm Control Building Modern Computer From First Principles www.nnd2tetris.org Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine,

More information