This is page 9 Priter: Opaque this Lecture 7 Two-Way Fiite Automata Two-way fiite automata are similar to the machies we have bee studyig, except that they ca read the iput strig i either directio. We thik of them as havig a read head, which ca move left or right over the iput strig. Like ordiary fiite automata, they have a fiite set Q of states ad ca be either determiistic (2DFA) or odetermiistic (2NFA). Although these automata appear much more powerful tha oe-way fiite automata, i reality they are equivalet i the sese that they oly accept regular sets. We will prove this result usig the Myhill Nerode theorem. We thik of the symbols of the iput strig as occupyig cells of a fiite tape, oe symbol per cell. The iput strig is eclosed i left ad right edmarkers ad, which are ot elemets of the iput alphabet Σ. The read head may ot move outside of the edmarkers. Q a a 2 a 3 a 4 a 5 a 6 a 7 a Iformally, the machie starts i its start state s with its read head poitig to the left edmarker. At ay poit i time, the machie is i some state q with its read head scaig some tape cell cotaiig a iput symbol a i or oe of the edmarkers. Based o its curret state ad the symbol occupyig
20 Lecture 7 Example 7. the tape cell it is curretly scaig, it moves its read head either left or right oe cell ad eters a ew state. It accepts by eterig a special accept state t ad rejects by eterig a special reject state r. The machie s actio o a particular state ad symbol is determied by a trasitio fuctio δ that is part of the specificatio of the machie. Here is a iformal descriptio of a 2DFA acceptig the set A = {x {a, b} #a(x) is a multiple of 3 ad #b(x) is eve}. The machie starts i its start state scaig the left edmarker. It scas left to right over the iput, coutig the umber of a s mod 3 ad igorig the b s. Whe it reaches the right edmarker, if the umber of a s it has see is ot a multiple of 3, it eters its reject state, thereby rejectig the iput the iput strig x is ot i the set A, sice the first coditio is ot satisfied. Otherwise it scas right to left over the iput, coutig the umber of b s mod 2 ad igorig the a s. Whe it reaches the left edmarker agai, if the umber of b s it has see is odd, it eters its reject state; otherwise, it eters its accept state. Ulike ordiary fiite automata, a 2DFA eeds oly a sigle accept state ad a sigle reject state. We ca thik of it as haltig immediately whe it eters oe of these two states, although formally it keeps ruig but remais i the accept or reject state. The machie eed ot read the etire iput before acceptig or rejectig. Ideed, it eed ot ever accept or reject at all, but may loop ifiitely without ever eterig its accept or reject state. Formal Defiitio of 2DFA Formally, a 2DFA is a octuple M = (Q, Σ,,, δ, s, t, r), where Q is a fiite set (the states), Σ is a fiite set (the iput alphabet), is the left edmarker, Σ, is the right edmarker, Σ, δ : Q (Σ {, }) (Q {L, R}) is the trasitio fuctio (L, R stad for left ad right, respectively), s Q is the start state, t Q is the accept state, ad
Two-Way Fiite Automata 2 r Q is the reject state, r t, such that for all states q, δ(q, ) = (u, R) for some u Q, δ(q, ) = (v, L) for some v Q, (7.) ad for all symbols b Σ { }, δ(t, b) = (t, R), δ(t, ) = (t, L), δ(r, b) = (r, R), δ(r, ) = (r, L). (7.2) Ituitively, the fuctio δ takes a state ad a symbol as argumets ad returs a ew state ad a directio to move the head. If δ(p, b) = (q, d), the wheever the machie is i state p ad scaig a tape cell cotaiig symbol b, it moves its head oe cell i the directio d ad eters state q. The restrictios (7.) prevet the machie from ever movig outside the iput area. The restrictios (7.2) say that oce the machie eters its accept or reject state, it stays i that state ad moves its head all the way to the right of the tape. The octuple is ot a legal 2DFA if its trasitio fuctio δ does ot satisfy these coditios. Example 7.2 Here is a formal descriptio of the 2DFA described iformally i Example 7. above. Q = {q 0, q, q 2, p 0, p, t, r}, Σ = {a, b}. The start, accept, ad reject states are q 0, t, ad r, respectively. The trasitio fuctio δ is give by the followig table: a b q 0 (q 0, R) (q, R) (q 0, R) (p 0, L) q (q 2, R) (q, R) (r, L) q 2 (q 0, R) (q 2, R) (r, L) p 0 (t, R) (p 0, L) (p, L) p (r, R) (p, L) (p 0, L) t (t, R) (t, R) (t, R) (t, L) r (r, R) (r, R) (r, R) (r, L) The etries marked will ever occur i ay computatio, so it does t matter what we put here. The machie is i states q 0, q, or q 2 o the first pass over the iput from left to right; it is i state q i if the umber of a s it has see so far is i mod 3. The machie is i state p 0 or p o the secod pass over the iput from right to left, the idex idicatig the parity of the umber of b s it has see so far.
22 Lecture 7 Cofiguratios ad Acceptace Fix a iput x Σ, say x = a a 2 a. Let a 0 = ad a + =. The a 0 a a 2 a a + = x. A cofiguratio of the machie o iput x is a pair (q, i) such that q Q ad 0 i +. Iformally, the pair (q, i) gives a curret state ad curret positio of the read head. The start cofiguratio is (s, 0), meaig that the machie is i its start state s ad scaig the left edmarker. A biary relatio x, the ext cofiguratio relatio, is ied o cofiguratios as follows: δ(p, a i ) = (q, L) (p, i) x (q, i ), δ(p, a i ) = (q, R) (p, i) x (q, i + ). The relatio x describes oe step of the machie o iput x. We ie the relatios x iductively, 0: 0 (p, i) x (p, i); ad if (p, i) x (q, j) ad (q, j) x (u, k), the (p, i) + x (u, k). The relatio x x is just the -fold compositio of x. The relatios are fuctios; that is, for ay cofiguratio (p, i), there is exactly oe cofiguratio (q, j) such that (p, i) x (p, i) x (q, j) 0 (p, i) x (q, j). Now ie (q, j). Note that the iitios of these relatios deped o the iput x. The machie is said to accept the iput x if (s, 0) x (t, i) for some i. I other words, the machie eters its accept state at some poit. The machie is said to reject the iput x if (s, 0) x (r, i) for some i. I other words, the machie eters its reject state at some poit. It caot both accept ad reject iput x by our assumptio that t r ad by properties (7.2). The machie is said to halt o iput x if it either accepts x or rejects x. Note that this is a purely mathematical iitio the machie does t really grid to a halt! It is possible that the machie either accepts or rejects x, i which case it is said to loop o x. The set L(M) is ied to be the set of strigs accepted by M.
Two-Way Fiite Automata 23 Example 7.3 The 2DFA described i Example 7.2 goes through the followig sequece of cofiguratios o iput aababbb, leadig to acceptace: (q 0, 0), (q 0, ), (q, 2), (q 2, 3), (q 2, 4), (q 0, 5), (q 0, 6), (q 0, 7), (q 0, 8), (p 0, 7), (p, 6), (p 0, 5), (p, 4), (p, 3), (p 0, 2), (p 0, ), (p 0, 0), (t, ). It goes through the followig sequece of cofiguratios o iput aababa, leadig to rejectio: (q 0, 0), (q 0, ), (q, 2), (q 2, 3), (q 2, 4), (q 0, 5), (q 0, 6), (q, 7), (r, 6). It goes through the followig sequece of cofiguratios o iput aababb, leadig to rejectio: (q 0, 0), (q 0, ), (q, 2), (q 2, 3), (q 2, 4), (q 0, 5), (q 0, 6), (q 0, 7), (p 0, 6), (p, 5), (p 0, 4), (p 0, 3), (p, 2), (p, ), (p, 0), (r, ).
This is page 24 Priter: Opaque this Lecture 8 2DFAs ad Regular Sets I this lecture we show that 2DFAs are o more powerful tha ordiary DFAs. Here is the idea. Cosider a log iput strig broke up i a arbitrary place ito two substrigs xz. How much iformatio about x ca the machie carry across the boudary from x ito z? Sice the machie is two-way, it ca cross the boudary betwee x ad z several times. Each time it crosses the boudary movig from right to left, that is, from z ito x, it does so i some state q. Whe it crosses the boudary agai movig from left to right (if ever), it comes out of x i some state, say p. Now if it ever goes ito x i the future i state q agai, it will emerge agai i state p, because its future actio is completely determied by its curret cofiguratio (state ad head positio). Moreover, the state p depeds oly o q ad x. We will write T x (q) = p to deote this relatioship. We ca keep track of all such iformatio by meas of a fiite table T x : (Q { }) (Q { }), where Q is the set of states of the 2DFA M, ad ad are two other objects ot i Q whose purpose is described below. O iput xz, the machie M starts i its start state scaig the left edmarker. As it computes, it moves its read head. The head may evetually cross the boudary movig left to right from x ito z. The first time it does so (if ever), it is i some state, which we will call T x ( ) (this is the purpose of ). The machie may ever emerge from x; i this case we
2DFAs ad Regular Sets 25 write T x ( ) = (this is the purpose of ). The state T x ( ) gives some iformatio about x, but oly a fiite amout of iformatio, sice there are oly fiitely may possibilities for T x ( ). Note also that T x ( ) depeds oly o x ad ot o z: if the iput were xw istead of xz, the first time the machie passed the boudary from x ito w, it would also be i state T x ( ), because its actio up to that poit is determied oly by x; it has t see aythig to the right of the boudary yet. If T x ( ) =, M must be i a ifiite loop iside x ad will ever accept or reject, by our assumptio about movig all the way to the right edmarker wheever it accepts or rejects. Suppose that the machie does emerge from x ito z. It may wader aroud i z for a while, the later may move back ito x from right to left i state q. If this happes, the it will either evetually emerge from x agai i some state p, i which case we ie T x (q) = p; or ever emerge, i which case we ie T x (q) =. Agai, ote that T x (q) depeds oly o x ad q ad ot o z. If the machie etered x from the right o iput xw i state q, the it would emerge agai i state T x (q) (or ever emerge, if T x (q) = ), because M is determiistic, ad its behavior while iside x is completely determied by x ad the state it etered x i. If we write dow T x (q) for every state q alog with T x ( ), this gives all the iformatio about x oe could ever hope to carry across the boudary from x to z. Oe ca imagie a aget sittig to the right of the boudary betwee x ad z, tryig to obtai iformatio about x. All it is allowed to do is observe the state T x ( ) the first time the machie emerges from x (if ever) ad later sed probes ito x i various states q to see what state T x (q) the machie comes out i (if at all). If y is aother strig such that T y = T x, the x ad y will be idistiguishable from the aget s poit of view. Now ote that there are oly fiitely may possible tables T : (Q { }) (Q { }), amely (k + ) k+, where k is the size of Q. Thus there is oly a fiite amout of iformatio about x that ca be passed across the boudary to the right of x, ad it is all ecoded i the table T x. Note also that if T x = T y ad M accepts xz, the M accepts yz. This is because the sequece of states the machie is i as it passes the boudary betwee x ad z (or betwee y ad z) i either directio is completely determied by the table T x = T y ad z. To accept xz, the machie must at some poit be scaig the right edmarker i its accept state t. Sice
26 Lecture 8 the sequece of states alog the boudary is the same ad the actio whe the machie is scaig z is the same, this also must happe o iput yz. Now we ca use the Myhill Nerode theorem to show that L(M) is regular. We have just argued that T x = T y z (M accepts xz M accepts yz) z (xz L(M) yz L(M)) x L(M) y, where L(M) is the relatio first ied i Eq. (6.) of Lecture 6. Thus if two strigs have the same table, the they are equivalet uder L(M). Sice there are oly fiitely may tables, the relatio L(M) has oly fiitely may equivalece classes, at most oe for each table; therefore, L(M) is of fiite idex. By the Myhill Nerode theorem, L(M) is a regular set. Costructig a DFA The argumet above may be a bit usatisfyig, sice it does ot explicitly costruct a DFA equivalet to a give 2DFA M. We ca easily do so, however. Ituitively, we ca build a DFA whose states correspod to the tables. Formally, ie x y T x = T y. That is, call two strigs i Σ equivalet if they have the same table. There are oly fiitely may equivalece classes, at most oe for each table; thus is of fiite idex. We ca also show the followig: (i) The table T xa is uiquely determied by T x ad a; that is, if T x = T y, the T xa = T ya. This says that is a right cogruece. (ii) Whether or ot x is accepted by M is completely determied by T x ; that is, if T x = T y, the either both x ad y are accepted by M or either is. This says that refies L(M). These observatios together say that is a Myhill Nerode relatio for L(M). Usig the costructio M described i Lecture 5, we ca obtai a DFA for L(M) explicitly. To show (i), we show how to costruct T xa from T x ad a. If p 0, p,..., p k, q 0, q,..., q k Q such that δ(p i, a) = (q i, L) ad T x (q i ) = p i+, 0 i k, ad δ(p k, a) = (q k, R), the T xa (p 0 ) = q k.
2DFAs ad Regular Sets 27 If p 0, p,..., p k, q 0, q,..., q k Q such that δ(p i, a) = (q i, L) ad T x (q i ) = p i+, 0 i k, ad p k = p i, i < k, the T xa (p 0 ) =. If p 0, p,..., p k, q 0, q,..., q k Q such that δ(p i, a) = (q i, L), 0 i k, T x (q i ) = p i+, 0 i k, ad T x (q k ) =, the T xa (p 0 ) =. If T x ( ) =, the T xa ( ) =. If T x ( ) = p, the T xa ( ) = T xa (p). For (ii), suppose T x = T y ad cosider the sequece of states M is i as it crosses the boudary i either directio betwee the iput strig ad the right edmarker. This sequece is the same o iput x as it is o iput y, sice it is completely determied by the table. Both strigs x ad y are accepted iff this sequece cotais the accept state t. We have show that the relatio is a Myhill Nerode relatio for L(M), where M is a arbitrary 2DFA. The costructio M of Lecture 5 gives a DFA equivalet to M. Recall that i that costructio, the states of the DFA correspod i a oe-to-oe fashio with the -classes; ad here, each -class [x] correspods to a table T x : (Q { }) (Q { }). If we wated to, we could build a DFA M directly from the tables: Q s δ (T x, a) F = {T : (Q { }) (Q { })}, = T ɛ, = T xa, = {T x x L(M)}. The trasitio fuctio δ is well ied because of property (i), ad T x F iff x L(M) by property (ii). As usual, oe ca prove by iductio o y that the δ (T x, y) = T xy ; x L(M ) δ (s, x) F δ (T ɛ, x) F T x F Thus L(M ) = L(M). x L(M). Aother proof, due to Vardi [3], is give i Miscellaeous Exercise 6.
28 Lecture 8 Historical Notes Two-way fiite automata were first studied by Rabi ad Scott [96] ad Shepherdso [05]. Vardi [3] gave a shorter proof of equivalece to DFAs (Miscellaeous Exercise 6).