208-2

Upload: arpita-mishra

Post on 04-Jun-2018

287 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 208-2

    1/25

    10 Chopter I ImrnooucrroN To a1rn THnonv orr CloururArrglr

    fhe starting staternents Pt,Pz,,., Pp a,re called the ba sis of the indrrc-tion. lhe step connecting P' with Pra"l is ca,lled he inductive step. Theincluctive step is gerrerally made ea,sier y thc inductive assumption tltatPt,Pz,..,,P' a,re nre. In a, brmir,l ndrrctive argtrmcr rt, wtl sltow all tlrree

    parts explicitly.

    A bina,ry trt:c is a tree irr which no parerrl can have more than two children,Prove that tr, hinrr,ry tree of heigtrt n hirs irt rnt)st 2t' Ieirves.

    Proof: If we denote the naxirnum number of Ieaves of a binary tree ofheight n , by I (rz), then we want to show tha,t

    I ( n ) 2 " .

    Basis: Clearly I (0) : 1 : 2" since a tree of height 0 can have no nodesother than the rool, that is, it has at r nost one leaf.

    Induc t ive Assumpt ion : I ( i ) < 2t , f r rr , 0 , I , . . . ,n .

    Inductive Step: 'lb get a binary tree of height n * 1 from one of height rz,'we caJr create, at nost, two leaves n place of each previous one. Therefore

    l ( n + 1 ) : 2 t ( n ) .

    Now, r.rsing he inductive a,ssumption, we ge t

    I ( n + 1 ) 1 2 x 2 n 2 n i r .

    'Ihus, our cla,im is also true for n * I. Since rz can be any nrrmher, thestatement must be tnre fbr a,ll n. .

    I

    Herc wc irrtroduce the symbol r that is used n this book to denote theend of a proof.

    Show that

    n , ( n I )c - \ - ; -* n - / _ " -i.=ll

    First we note that

    5 ' , . + r . 5 L + n + t .

    ( 1 4 )

  • 8/13/2019 208-2

    2/25

    1.1 MnrHnua.rrcel PnnlrurnAn.rEs ANDNo'rwlror'r 1 1

    We then make the inductive assurrrption hat (1.4) holds f'or 5',; if this isso, therr

    S'.,*t =

    Thus (1.4) holds or 5'2-.1 nd(1.a) s obviously rue for n :irrductiorr trr all n.

    n , ( n * l )

    - * t z * 1( n + 2 ) ( r z + 1 )2

    we havc jrrstified thel, we have a basis arrrl

    indrrctive step. Sincehitvc:provcd (1.4) hy

    T

    Irr this la,st example we have been a Iittle less formal in identifying thebirsis, rrrhxltive assumption, and inductive step, but they are l,]rere and are

    tlsscrrtiir,l. To keep orrr suhseqrrent discussions rorn becoming too fonnal, wewill gcnerally prefer the style of the second exanple. However, f you havedilficulty irr f'rrlklwingor r:rlrrstnrr:ting ,proof, go back to the more explicitforrn of Exarrrule 1.5.

    Inductive reasoning can be clillicult to grasp. It helps to rroticc tht:r:lose (x)nnection between incluction and recursion in proglanrning. Fbr

    ffi li'*i:;JTJJil:T,1#"bl'-.,l' ,lil#,/'"Iil,l*,lJj;?iT,'flT'fit e r r r r s f f ( r r ) , . f (n - 1) , . . . , . / (1 ) . This cor responds o the induc t ive s tep .The scc:rlrxl lir,rt is thc "r:sr:a1lc"fiorn the re:crtrsion, which is accomplished

    by defining / (1) ,./ (2) , ....f (A) rxrrrrcc:rrrsively. his urrresponds o the basisof inducliott. As in itrduction, tecursiorr allows us to rlraw rxrnr :hrsions horrtall instances of the problem, given only a few starting valrrcls rrxl using tlrrlrecursive nature of the uroblem.

    Proof by contracliction is a,rrother powerful technique that often workswhetr everytlfttg else fails, Suppose we want to provu that some statementP is true. We then assurrle, or the rnornerrt, thrrt Jt is falsc: lrrd sr:rl whcrc:that assumption leads us. If we arrive at a conclusion that we know isincorrect, we can lay the blame on the starting assunption and concludethat P must be true. The following is a classic and elegant example.

    '[ttf,fttgl$ |.,y A rntiona,l number is a, number tha,t ca,n be expressed as the ration of two

    irrtr:gurs n, tnxl m, scl hat n, tr,nd rn, have no conrmon factor. A rea,l numbert,hat is rrot ratiorral is said to bt: irra.tional. Show that r/2 is irra,tionrr,l.

    As in all proofs by corrtrarlit:tiorr, wt: iLrJ$llfire he r:onfirilry of what wtlwant to show. Here we assunre that r,4 is a ratiorrirl rlrrntrcr so tlnt it carrbe written as

    , 5 - nTN

    ( 1 5 )

  • 8/13/2019 208-2

    3/25

    t 2 Chopter I IurnonucTroN To THE THEoRy or ColapurertoN

    where n and m are integers without & common factor. Rearranging (1.5),rve have

    2 m 2 : n 2 .

    Therefore rz2 must be even. This implies that n is even, so that we canwr i te n :2h or

    and

    Therefore m is even. Buthave no common factors.not a rational number.

    Zmz :41r l ,

    m 2 : 2 k 2 .

    this contradicts our assumption that n and rnThus, rn and n in (1.5) carnot exist and 1E is

    I

    1 .

    ,

    This example exhibits the essence of a proof by contradiction. By mak-ing a certain assumption we are led to a contradiction of the assumption orsome known fact. If all steps in our argument are logically $oundr we mustconclude that our initial assumption wa*s alse.

    Use nduction on the size of S to show hat if S is a finite set hen l2s : 2lsl

    Show hat if ,9r and 5z are finite sets with lSrl : n and l,9rl = tn, then

    l S rU S z l 1 n l m .

    If Sr and 92 re inite sets, how hat lSr x Szl lSlllsrl.Consider the relation between two sets defined by 51 = Sz if and only iflSrl = l5':ll.Show hat this is an equivalence elation.

    Prove DeMorgan's aws, Equations 1.2) and (1.3). ff i

    Occasionally, we need o use he union and intersection symbols n a manneranalogous o the summation sign E. We define

    U s p : s , u s i u s s . . .

    with an anatosous ,l,i :;;he intersection f several ets.With this notatiorr, the general DeMorgan's laws are written as

    3 .

    4 .

    5 .

    6 .

    U s " : [ l EP E P F E P

  • 8/13/2019 208-2

    4/25

    1.1 MarxnltATIcAL Pn-ultl,trwanrEs ANDNotnuon 13

    f l s o : U E 'peP peP

    Prove these dentities when P is a finite set. ffi7. Show that

    ,5ru 9e- l;E.

    E. Show hat ,9r : Sz if and only if

    ( s r n B r ) u ( F r n z ) : s .

    9. Show that

    Sr U Sz - (Sr nFz) :52. ffi

    10. Show hat

    Sr x (Ss UFB) (5r x Sz) U (Sr x Ss).

    11. Show hat if Sr e Se, hen Fr c 5t .

    12, Give conditions on ^9r and ,92 necessaxy nd sufficient o ensure hat

    ,9, : (Sr U Sz) - Sz. ffi

    1 3 . S h o w t h a t i f ( n ) : O ( s ( " ) ) a n d s ( n ) = O ( f ( r r. ) ) , t h e nf ( " ) : 6 ( s ( " ) ) .

    14. Show hat 2* = O (3") b:ut 2" t' O (3').

    15. Show that the following order-of-magnitude esults hold.

    ( * ) t ' + 5 l o g n : O ( " ' )

    ( b ) 3 * : O ( n ! )

    (c) n! : O (rz") {W

    16. Prove ha t i f f (n) : O(s(" ) ) and s(n) :O(h( rz ) ) , then (n) =O(n(n) ) .17. Show hat if f (n): O (rz2)and g(n) = O (nB), he"

    f (")+ sfu): o ( '*)

    and

    f (n) u): o ("u)

    18. In Exercise 17, s it true that S.(n) f (n) : O (n)?

  • 8/13/2019 208-2

    5/25

    t4 Chopter I INrRoDllci'I'IoN To rHE THEoRY op Cotupurauoru

    19. Assurnc that / (n) :2rr,2 * n and.q(") : O(rr ') . Wtrat is wrorrg with thefollowing argurrretrt'l

    f (n,) r) (n2)+ o (n,)

    so that

    / (") - g (n,) rt (n2) + o (n) - o (n.2)

    'fherefore,

    f (,,) e Ut) rjJ1r1

    20. Draw a picture ofthe graph with vert iccs {u1,u?ru3} and edges {(u1,u1),(ur, uz) , (u2,T. ,3) ,(rr, r r ), (ug,ur )I. Flnumerate al l cycles with ba.se 'ur.

    21. Let G : (V,E) be any graph. Prove the fbllowing claim: If there is any walk

    betweerr u E V and ur' E V, therr there must bc a path of length no largcrtharr ll/l - 1 between these two vertices,

    22. Considcr graphs in whir:h there is at most one edge between any two verticert.Show that uncler this ct-rnclition a graph with n vertices has at tnost rz2 edges.

    23. Show thatn

    E- ,2| t :

    ,tJ

    rr. n, l) (2n,+ 1)

    24. Show thatt L -

    1 1 * r - 1 .-._r r. rr

    25. Prove that for all n, ) 4 the irrequality 2' { n! holds.

    26, Show that r" is not a rational ntrmber.

    27. Show that 2 - y4 is irrational. ff i

    28, Prove or disprove thc following staternertts.

    (a) The srrrn of a rational and an irrational ntrtttber rnust bc irrational.

    (b) 'Ihe srrrn of two positive irrational ntrrnbers trust bc irrational.

    (c) The product of a ration a,l and a n irratiorral nurnber must be irra-tional.

    29. Show that every positive integer can be expressetl as the product of primetrur,rbe.s. ff i

    *30, Prove that t,he sct of all prirne mtrnbers is infinitc.

    31. A prime pair cxirrsists of two primes tha,t differ lty two. Thcre are marryprime pairs, for exatnplc, 11 and 1i3, 17 arrd 19, etc' Prirne triplets are tlrreernrrnbers n,n *2,nf 4 that are all pritre. Show that the only prirne triplet'a r e ( 1 , 3 , 5 ) a n d ( 3 , 5 , 7 ) .

  • 8/13/2019 208-2

    6/25

    1.? THnnn BASrc CoNCEPTS l 5

    Three BosicConceptsTlrrcc frrnda,mental deas arer he rnajor therrres of this book: languages,grammars, and automata. In the course

    of our study we will explorenrarry results about thesc rxlncepts and about their relationship to eachother. First, we must understir.rrrl he meaning of thr: terms.

    LonguogesWe a,re all familiar with the notion of naturtr,l languages, such as Englishirnd Ftench. Still, rnost of rrs would probably lind it difficult to say cxrrr:tlywhnt the word ttlanguagd' means. Dictiornrics define the term inforurallyas ir uystem suitable for thc cxpression of certairr idea,s, a,cts, or concelrts,inchtditrg a set of syrnbols and ru les for their rner,nipulation. While this gives

    us arr intuitive idea of wlnt rr lrr,ngJuage s, it is rrot srrflicient as a definitiorrfor tlrt: study of formal languagcs. We need a precise rlefinition for the term.

    We start with a finite, nonerrrpty sot X of symbols, callexl the alpha-bet. Fl'orn thc individual symbols wc construct strings, which are finite$equences of syrrrbols from the alphabet. For example, if the a\rhabetX : {o, b}, t}ren nbab and aaabbba are stringu on X. With few exceptions,we will use ower cirse etters e,,b,cr.,. or cltlments of E and lt, 't),ta,... ferstring names. We will write, fbr example,

    ut = abaaa

    to ittdicate that the string narrred z,' has the specific value abaaa.The concatenation of two strirrgs ru and u is the strirrg obtained by

    appending the symbols of u to the right end of tu, that is, if

    u ] : Q , I A ? ' . . A r t

    and

    u : b f t 2 . . . h , , , ,

    tlterr he concatenation f rl.' rnd ?, denoted by trru, s

    wu : a taz , . , t l , . , rb1h2. - b*,

    The reverse of a string is obtained by writing the symbols in reverse order;if ur is a string as showrr rrhove, then its reverso rufi is

    1 i l 8 : e , n - , , a z a t ,

    The length of a string tr', tlcnoted by lrul, s the rrurrrber f sJ'mbols nthe strirrg. We will frequently need o refer to the empty string, which

  • 8/13/2019 208-2

    7/25

    16 Chopter I InrnorrtrcrrloN To rHu Tsnonv ort Co\,tPu'rartoN

    is a string with no symbols at all. It will tre denoted by A. Tlte followingsirnple relations

    l A l0 ,

    . \ tu uA: ur.

    hold for all trr.Any string of r:onsccutive hara,cter$ rr sonte o is sa,id o btl a substring

    of tr.'. If

    u l : ' l ) 1 . 1 , 1

    then the substrings u itrrtl z are said to bel a prefix and a suffix of 'u,

    rr:spectively. For exi rrrrple, f tu : abbab, hetr {'\,o,ab, abb,a,ltln,,abbab} sthe set of all prefixcs uf 'u, while bab, ab, b are sorne of its suffixc:s.

    Simple properties of strings, sut:h as their length' are v()ry itrtuitive andprobably nced little elaboratiqn. For example, if tt, ir,rrd r are strings, tht:rrthe length of their concatrrnatiotr s the sum of thc: irrdividual lengths, thatis.

    l z u ll u l + l u l . ( l . 6 )

    But although this relationship is obvious, t is useful to be able to mitkeit precisc: and prove it. The tt:chniques for doing $() are iilrportant in rnorecomnlicated situation.

    aIIy lr

    rlefltrition of the length of a string.fashion by

    and o. Trr prove this, we first rreed aWe ma,kt: such a definition in a recursive

    l n l 1 'l u r a , ll r l l + 1 ,

    for all a X and tr.r any string on X. This definition is a forrrral statement ofour intrritive understanding of the length of a strirrg: the length of a singlesymhol is one, and the length of any string is int:reased by one if wc addanother syurbol to it. With this formal definitittrr, we are ready to prove(1.6) by induction.

    By definition, (1.6) holds for all u o f tr,nv errgth and all u of ltlrrgth l, sowe have a basis. As an irrcluctive a'ssumptiorr, we take that (1.6) holds fortr,llz of any Iength and all u of Iength I,2,...,r1. Now take any r.' of lengthrt f I and write it it^s : trra. Then,

    l u l l u r l+ ,lzul lurual lztrl+ 1.

  • 8/13/2019 208-2

    8/25

    But by the inductivc hypothesis (which

    l zu l lu lso that

    l uu l l r r, l+ru l+Thereforc, (1.6) holds for all ,u,ir.rrda,llthe ittdrtr:tive step and the argrrrrx)rrt.

    1.2 THREE BesrcrCor-rcnprs 17

    is applicable since uris of length rz),

    * l r u l '

    1 : l z l + l T . ' 1 .Trof Iength up to z { l, completirrg

    I

    If tu is ir. string, tlren u"' starrds f'or the string obtained by repeating tun times. As a sptx:iir.l cane, we define

    uo : .1 ,

    for all tr.,.If }J is arr r,lphahet, hen we use X* to denote the set of strirrgs obtained

    by concatcrrir,ting zero or rrlore syrnbt.rls rom X, The set IJ* always contains,\. Io excludc the empty string, we dcfine

    X + : E * - { I }

    Whilc X is finite by assunrption, X* a,nd E+ are always irrlinite since thereis no lirnit on the length of the strirrgs n these sets.

    A latrgutr.ge s defined very gerrerirlly a,s a sulrset of }j*. A string in aIanguage .L will bc ca,lled a sentence of tr. This definition is quite hroad;

    any set of strings orr iln ellpha,bet E can be corrsidered a language. Latcr wewill study methods bv which specific anguages cir.n e defined and describr:d;this will enable us to givrl some structure to tlis rir,ther broad concept. Forthe moment, though, wc will just look at a few sper:ific examples.

    Let E : {a,,b}. Then

    X* : { t r,u , ,b ,&&,ab ,ba ,bb ,aaa ,aa ,h , . . . . l

    {u, a,a,, abl;

    X. Because L has a firrite mrmber of sentences, wc t:all itTlrt: sot

    7 : { u r r t t " : n > 0 }

    is also a languagc on X. The strings aaltb ard aaaabbbb are in the larrgrra,geL, but the strirrg a,bb s not in .L. 'fhis larrgrrrr,ges infinite. Most irrtcrestinggla,nguages are irrfinite.

    I

    The sct

    is a Ianguagc onrr finite language.

  • 8/13/2019 208-2

    9/25

    18 Chopter I Imrn.onucrroN To 'r'sp TnnoRv oH Col,Ipurauolt

    Since lir,rrguages re sets, the uttiott, iuterset:ticlrt, atrd differentlt: of twola,nguir.ges re imrnedintcly clefined. The mlrrrplerneut of a langrrirge s clefinedwith rt:spect to X*; thtlt is, the complerltlrrt of -L is

    T : E * - L .

    The reverse of a, ir.rrguage s the set tlf all string reverstlls, that is,

    7 H : { t n R : w e L } .

    The conr:ir,tcrratiou of two ltr,nguages L1 and L2 is the set of all strings ob*taintrrl by cotrcatenating ir.ny eletnent of L1 with atry eletnent of tr2; specifi.-car,lIy,

    L1L2: l ry : : r Lt , ' ! E LzI .

    We definc Ln as L conca,tener,tctl iLh itself rz timers, with the specitr,l :astls

    rn tr]^ anrl

    L I : L

    for every la,nguage L.Firrally, we define thtl star-closure of rr larrguage as

    L * : L o r - t r u L z ' . '

    and the positive closure as

    L + : L 1 D L 2 . . . .

    I f

    1 , : { a , , b " : n } 0 } ,

    then

    [ r2 : l , ,u ,n l l ' ' a r l lb t 'Ln t 0,m > 0].

    Nott: tlnt rz ancl rn in tht: ir bove are utrrelattld; the string aabbaaa,hhb s itt

    L 2 .The reversr: tlf -L is easilv clescribtNl rt set trotations as

    7 R : { I t n u , r : n > 0 } ,

    brrt it is considerably hardt:r to describe L or L* this way. A few trics willqrric:kly convince you of tlic Iinitation of set notation for the specificationof courplicated languages.

    I

  • 8/13/2019 208-2

    10/25

    1.2 'l 'Hnnn Basrc C.jol(;r;prs 19

    Grommqrs'Ib study languages rnir.thetnatically, wcl reed a mechanisrrr o clescribe hern.Everytlirv la,nguage s irnprecise ancl atnbigrrr)llsr o inforrnal descriptions rrIlnglish artl often inadequatt:. The

    set nolatiorr rrsed n Exarnplr:s 1.9 andl.I0 is rnore suitable, but lirnitecl. As we proceed wc will lea,rn about scvt:ralIarrgrta,ge-definiliotr er:hanistns hat irre useful in diffclrt:nt circurnslancc:s.Hert: we irrtroduce a (:orrrrn()n nd powerfirl one, the nol,iorr of rr, rammar.

    A grammar fcrr the English language tells us whetlrcr rr,pa,rticular scrr-tcrrt:e s well-forntetl or not. A typicrrl nrle of English grirrrrrna,rs "a senlerrt:clt:trn consist, -rf r noun phrase followcd hy a predicat(1." More concisely wewrite tltis as

    (s erften ce) - \r ,oun h,r as e) (ytrul,icatel,

    with the obvious interpretation, This is of course not criorrgh o deal witiractual $()ntences, We rmrst rrtiw provicle definitions for the newly introduceclcorrstrrrcts \no,un,4lu'asr:) rrrd (predicate). If we do so by

    (noun,4lu'ase) + (arti,cle) (ruru,n) ,(pred,i,cate.) -+ (uerb) ,

    tlrrd f we associatcl lxl tr,ctualwords "irttelnd "tlte" wilh (nr.rfrlr:le),boy" and"rlog" with \nourt), irrr(l

    "rlrns" and ttwalks" with (uerb), then tlNr gra,mrnartells us that the sentences t&boy runs" and "thr: rlog wa,lks" are proprlrlyIlrrmed. If we werrc o give a, complete grirmma,r'1

    hen irr thcory, every properselrt(lrrr:e ould be explilirrtxl this way,'Ihis cxa,mple llustlatcs the definition of a genera,l concepL n tcrms

    of sitttpltl ones. We start with the top level corrr:ept, here (.serz[erlr:r:),r,ndsuccessivcly educe l to the irrtxhrcible building trlor:ks of the languagr :. Thegetreralization of these ideas lt:ads lrs to forrnal HrirrnmA,rs.

    A gratnrnirr' J is definecl as a rlrirdrrrp le

    (; : (v,'1" ,P)

    where V is ir firrite set of ol-rjects ir,lk:d ariablcs,7 is a finile set of obirrcts callecl crrninal symbols,S e V is a specirrl syrnbril ca,llecl he start ver,ria,ble,.f is ir. inite set o[ productions.

  • 8/13/2019 208-2

    11/25

    20 Chopter I lNrRottrcrloN To THFI IIEoRYor Cotr,tpureuon

    It will be assumed without furtlter mcrrtion that the sttts V and 7 ale IIoII-r:rnpty and di sioint.

    The production rult:s are the heart of a grammar; thev specify how thegrammar transfortns one strirrg into another, irnd through this they definea language associated with the grammar. In orrr discussion we will assumethat all prorluction rules are of the form

    f r + , A t

    where z is an elernetrt f (VU?)+ ancl gr s in (yUT).. The productiorrsare applied in the following matrtter: given a string u of the forrn

    1U ultu,

    we say the production tr -f g is applicable to this stritrg, and we may use itto replace r with y, tltereby olrtaining a new string

    E :'u!|L,.

    This is written iur

    Wc say that ur derives z or that a is derived from ur' Successive strings aredcrived by applying the productions of the grammar in a.rbitrary order, Aproduction can ber used whenever it is applicable, and it ctr,n be applied a'soften a s desired. If

    w r + w z + " ' + u n l

    we sa,y ha,t tu1 derives rur, and write

    ut1 1'ut.,,.

    The 'r. ndicates that an unspecified number of stcps (including zero) can betrlktlrr to derive tu, fiorrr tu1, Ihus

    w l w

    is always thc case.By applying the productiorr rules in a diff'crcrrt order, a given grirrrrtnar

    ca.n rorrnally generate marry strings. The set of all such termina,I stritrgs isthe language defined or generated by thc grallrlnar'

  • 8/13/2019 208-2

    12/25

    1.2 THnnn BasIc Cor{cnprs 2l

    Let G : (V,7, S,P) be a grammar. Therr the set

    L ( G ) : { u te T. : s 5 - }

    is the languagc generated by G.

    If tr L (G), then the sequence

    5 + u r 1 + w z + ' . . + u ) n + w

    is a derivation of the sentence .rir. The strings S,ut1,rLt2,.,,,trl",, which

    contain variables as well as terminals, are called sentential forms of thederivation.

    Exomplo l. | | Consicler he grammar

    G : ( { S } , { o , b } , 5 , P ) , , , 1with P giverr by " -

    . i 'S - aSb,

    ^9 .\.;")

    +"--

    \ar --i-":' ''l' '--:t' :'Then

    S + a 1 h + a a S b b + a a b b ,

    so we can write

    S 3 aubb.

    Tlro string aabb is a serntence n the language generated by G, while aasbbis a sentential fortr.

    A grammar G cornpletely defines L(G), but it may rrot tleeasy o

    getavery explicit description of the language frorn ther grammar. Here, however,

    thtl answer is fairly r:lear. It is not lmrd to conjecture that

    L ( G ) : { a " b " n } 0 } ,

    tr,nd t is easy to prove it. If we rrotice that the rule S --+aSb is rec:rrrsive, aproof by induction readily suggests itself. We first show that all serrtr:ntialft)rms must havc the f'orm

    uti : qtS6t (f '7)

  • 8/13/2019 208-2

    13/25

    Chopter I IFIrRotucrIoN To IHE THEoRY oF CoMPUTATIoN

    srrppose hat (1.7) holcls or all serrtential orms u; of length 2i + 1 or less.T()get another senterrtial orm (whidr is t]ot a sentt:ilce), we can orrly applythe production S - aSb. This gt:ts us

    ,ri bi + oi+Ig6i*t,so hrr.t every sentential orn of length 2i + 3 is also pf the form (1.7). since(1.?) s obviously rue fo|i : l, it holds by indrrr:tion or all i. Fiilally, togct a sentence, we rnust apply the production 1.9

    -+ l and we see hat

    S 4 a" Sbn + u,"Li"'

    reprcsents ll possible erivations. Tltus, G can derive only strings of theftlrrn a"'bt'.

    we also havc to show that nll strings of this t'trrm can be derived. This

    is easy; we sirnply apply s --+asb a"s many tirrres as needed, followed byS - 4 .

    I

    F\t ti",.' * "

    u - '

    L : { { _ ! " : I : r r. 0 } .

    'I'heiclea, br:hincl the previous example can tre extended to this case. AII

    we neerl to do is genera,te an extra b. This can be done with a production

    S - Ab, with other productions chosen so that A carr derive the language

    irr the previorrs tlxample. Retr*sorring n this fa^shiort, we get the gramrnar

    G : ({5, , ,1} {n, b} , S, P), wit}r productions

    g __+ b,

    A - aAh,

    A - 4 .

    Derive af'rrwspecific sentenrxrs o convince yourself that this *.rtul

    I

    I-he above tlxirrnples are fairly easy ones, so rigorous arguments may

    sccnr superfluous. But often it is not so easy to find a grarnmar fbr g}an-grrage described irr an informal wirY or to give an irrtuitive characterizationof the langlagtr defitred by a grarrrrnar. To show that a given language i

    incleecl generirtecl by a certain gralllmar G, we must be able to show (a) that

    every ?l/ E tr can be derived frgtn S using G, arrd (b) that evcry string so

    derived is irr .L.

  • 8/13/2019 208-2

    14/25

    1.2 TnRr:r: ]eslc Cor'rclnpr.s 23

    .$x$ntp le . t 3 T t r, keE :{a ,b} , anc l l e t , , (w) r r r r l n6 (u r )enotc thenumberof ,$anc lb ' s

    irt the string u, resptr

  • 8/13/2019 208-2

    15/25

    Chopter I IlnnooucrloN To rHr: TIInonv or" Cotr'tpureuoN

    can bc derived with G. Take any u G L of length 2n l 2' If ru : al'l b, then

    zu1 s in L, a,nd -rl:2n,. Thercfore, by assumption'

    5 4 u 1 '

    S + o5b arulh: w

    is 1>ossible, nd ru can be derived with G. Obviously, similar a,rgumetlts can

    he made if ru : b'rtrcr,,If tu is not of this form, that is, if it sta,rts and ends with the same

    symbol, then the counting irrgurnent tells us that it must have the form

    ru : tt)Llitztwith urr arrcl u2 both in -L and of Ierlgth less than or equal to

    2n. Hence again wc:see that

    5 + 5 5 5 t u r S 1 w 1 u 2 : 1 x

    is possible.Since the inductive assrrtnption is clearly satisfied f'or z : 1, we have a

    basis, an

  • 8/13/2019 208-2

    16/25

    1,2 THnnn Baslc Cor'rceprs 25

    Automoto

    An arrtomaton is an allstrac:t rnodel of a digiLal corrrlnrter. As such, everyautomilton includes sottte esscrrtial f'ea,tures, t, has a rntx:hanism for readingittllrrt.

    It will be assurned hat the input is a strirrg()vcr

    a, given alphabet,writttln on an input filc, whir:h the a,utomaton carr rca,d brrt not change.The input fik: is divided into cells, ear:h of which can hold o rre symbol. Theinput mechanisrrr can rerrd the inpui file frorn lerft to riglrt, one symbol ata time. The input rnechanism can a,lso delect thr: r:rrd of the input string(by scrrsirrg a,n end-of-file condition). The automar,on can Jrroriuce ourpurof some form. It may havt: ir tcrnporary storage devit:rr, cnnsisting of anuttlirnited number of cells, errr:h r:a,pa,ble f holding a single symbol froman alphabet (not necessarily he $a,nle one as the irrput a,Iphabet). Thearrtornatcln an read and charrgcl he contents of the storrrgc cells. Finally, theautomaton has a, control unit, which ca,n be in any orre of a finite numberof intcrnal states, and which can t:hirrrge tate in some definerl manner.Figure 1.3 shows a schematic represerrtation of a general autornatiorr.

    An automaton is assumed to operate in a discrete time frarne. Atany givcn tirne, the control unit is in some internal state, and the inputmecharri$m is scanning a particrrlirr symbol on the input file. Tlxl internalstate of the control unit at thc rxrxt time step is deterrnined trv the next-state or transition function. Ttris transition function gives he next statein terrn$ of the current state, the c:rrrrent nput symbol, and the irrfo rmationcurrently in the ternporary storagc. ,During the transition frorn one timeinterval to the next, output tr ay bc Jlrodrrced or the inforrnation in the

    temporary storage r;hir.nged. he term configuration will be used to referto a pa,rticular state of the control unit, input file, and tcrnllora,ry storage.rhe trarrsition of the a,utomaton rorn orrrl :onfiguration to the next vdll becallecl a rrrove.

    This gerreral rrxrdel covers all the autorrrata we will discuss n this book.A flnite-statc r:orrtrol will be cornmorr o all specific cases, but differencc:s

    Figure 1.3 Input file

  • 8/13/2019 208-2

    17/25

    Chopter I INrtootrcrloN To 'l'HuTnnonv ort Coupu't'Artoru

    will arise frorn the way in which thc output cau be prorluced and the naturcof the temporary storage. Thc rrature of the temporary storage ha"s ht

    stronger trffcct otr particula,r tyllt:s of automata''Frtr s1$sequeut discussions, it will be necessir,tY o distinguish betwecn

    deterministic autornata ir.rrdnondeterrninistic automata. A rlelttlrnirtislic automa'ttln is orre u which eat:h rnover s LlniquelY dett:rrnirred bythe current cxrrtfi.guratiou. f wtl krxlw tlte interna,l stattl, the itrput' atrctire rrlrrtt:rrtsof the ternporrr,ry storage, we can prtxlit:t the future behaviorof the a,ltornirton exactly, In rr, ulirtlcteruriuistic a,ttttlrnittott, his is notso. At elch lloint, a noncleterministic automaton tnav hirvclseveral possi

    ble moves, so we call only preditlt a set of possifule actiqrrs. The relationbetweel dcterrninistic and norrrk:ttlrrninistic automattr, of various types willplay a significant role itt our strrilv'

    Arr irrrtorr atott whose orrtllrt tespollse is limittxl to a sinple"yes" Q

    ,,rrtl" iS called an accepter, I'resetrted with eln irrptrt string, an iltltxlptteither a,ccepts lrc strirrg or rej ects t. A more getreral automa,ton, capable oprodrrcing strings of syrnbols trs otrtput, is called a transducor. Althoughwe will givc: sotne simple exa,rrrllltls f transducers in thC rtext section, ourprimtr.ry rrterest n this book is irr trt:ceplers.

    1' use induct ion orrT, to show that f i t" l :vl lul f t l r a l l s t r ings u and al l n '

    2. The reverse of a string, introduced informally above, carr be defined moreprecisclv by the ret:ursive rolcs

    (tLta)tr o"'n

    for all a E, tu E-, tlse this to prove that

    (n)R : 'xR'aR

    for all u,, r e E+. ff i

    Prove that (r*)n : u for al l ' t r. r X* .

    Let L: {ab,aa,baa}. Whittr r-rfhe following strings are in -L*: abaabaaabaa'aaattbaaaa, baaaao,ha'o,a,nb, aaaaabaa? ffi

    consider the languages n Examples 1.12 arxl 1.13. For which is it true tha

    L : L - ?

    Are there larrguages or which U :T-'t

    Prove that

    oo

    3 .

    4 .

    5 .

    X 6 .

    for all la,ngrrages Lr and -Le,

    ( L t L t ) R : t f t l

  • 8/13/2019 208-2

    18/25

    1.2 'I 'HREEBnsrc Clorucnrrs 27

    L Show that (Z-)" : .L. for all lalguagcs.

    9. Prove or disprove the following claitrs.

    (a) (1,r sl Lr)^ : t'f I rf ttrr all larrgrtages 1,1 and L2.

    (b) (rE). : Q:)tt for al l languages r.

    1O. Find grammars for X : {a, b} that generate t}re srets f

    (a) all strings with exactly orre s, ,

    (b) all strirrgs with at least one o,

    (c) all strings with no rlorc than three a's.

    (d) all strings with at least three n,'s. ff i

    In eat:h case, give convincing argurnents that the grarrrnar yorr give doesirrrleed generate the indicatcd language.

    11. Give a simple description of the language gerreraterl by the grammar withprodrrctions

    S + n A ,

    A - b S ,

    S - A . f f i

    12. What larrguage does the grammar with thcsc produc[ions generate'l

    S + Au,,

    A + B ,

    B - A a . f f i

    13. For each of the followirrg languages, ind a gramrnar that generates t.

    ( a ) 11 : {a^hno n } O ,nz > n} m

    (b) ,/,? {onb,n ; rz } 0}

    ( c ) t g : { a n + z b n : n } 1 }

    (d) I,4 : {a,"6"-:t ' t, > 3} ff i

    ( e ) L tL z

    (t) L, r) L,

    (e) i- i(h) ,l( i ) L t - L4

    * t+. Firxt grarrrnrarrtor the following anguagcs n E : {a}.(o) , | , : {ur l ru l rnoc l3 0]

    ( l r ) Z , { ' r r. ' : lz . ' lnxxl ;3>0} ff i

    (c) l , : {tu : ltulmod 3 f iul mod 2}

    (d) Z: { ' r i r l , r r l rno t l l J l r r r l od2}

  • 8/13/2019 208-2

    19/25

    28 Chopter I lrurRonucrloN To rnp THnoRv on CoMrutlttou

    15, Find a grammar that generates the language

    t :{wwR:

    t r. , { a , b}+ } .

    Give a completejustification for your answer.

    16. Using the notation of Example 1.13, find gr&mmars for the languages below.Ass u rne : {o ,b} .

    ( a ) I = {w: n* ( . ) : to ( u r ) + t} f f i

    ( b ) I , : {w: n* ( u , ) > na ( ru )}

    rt (c) I - {w : no 1,ro) Znr (tr)}

    (d ) I : {u {a ,b}* : l r l , " *) -nu (u ) l = 1}

    Repeat Exercises 16(a) and 16(d) with 5 : {a, b, c}

    Complete the arguments in Example 1.14, showing that .L (G1) does in factgenerate the given language.

    19, Are the two grammars with respective productions

    S * aSb ablA,

    and

    S * aAblab,A * aAbltr,

    equiw,lent? Assurne hat ,9 s the start symbol in both cases.

    20. Show hat the grammar G : ({S} , {a, b} , S, P), with productions

    ,9 ,9,9lSSSIaSb bSal \,

    is equivalent o thc grammar n Hxample 1.13.* 21. So ar, we have given examples of only relatively sirnple grammars; every pro-

    duction had a single variable on the left side. As we will see, such grammarsare very important, but Definition 1.1 allows more general orms.Consider he grammar C : ({A, B,C, D, E, S} , {a},S,P), with productions

    Derive tlrree dilferentabout I (G).

    S - ABaC,

    Ba - aaB,BC - DCIE.aI) + Da,AD - AB,aE + Ea,A E + 4 .

    sentences n .L(G), Ftom these, make a iortjecture

    L 7 .

    1 8 .

  • 8/13/2019 208-2

    20/25

    1.3 Solre AppLrcm'ror.ls 29

    gffi Some pplicot ions.Although rve stress the abstract and mathematical nature of fbrmal lan-guages and automata, it turns out that these concepts have widespreadapplications in computer $(:ience and are, in fact, a common theme thatconnects many specialty area^s. n this sectiorr, we present some simple ex-amples to give the reader $ome tuir$urancc hat what we study here is notjust a collection of abstractions, but is sontething that helps us understandmany important, real probletns.

    Formal langrragcs and gramrnars are used widely in connection withprogrammirrg larrguages. In most of our programming, we work with amore or less intuiiive understanding of the language in which we write.Occasionally though, when using an unfhmiliar fcature, we Inay need torefer to precise descriptions srrch a^s he syntax diagratrts foutrd in most

    programming texts. If we write a compiler, or if we wish to reason aboutthe correctness of a programr a prccise description of the language is neededat almost every step. Arrrorrg the ways in which programmiug languages canbe defined precisely, grammars arc perhaps the rnost widely used.

    The grammars that describe a typical language like Pascal or C are veryextensive. For arr example, let us take a smaller language that is part ofthis larger one.

    The set of all legal dentifiers n Pa,scal s a lattguage. nformally, t is the set

    of all strings starting with a letter ir,nd ollowcd by art arbitrary number ofletters or digits, The gra,mmar elow makes lfs inforntal definition precise.

    (id) -* (lt:ttu) (r'est),(resf) -+ (t,etter) rest| lklisit) (rest)l ,

    (Ietter) -+ a, bl ' ' l.z( a tu t t t )0111" ' l e

    In this grammar) the variables trrrr (id), (letter), (dig'it), and (resf), anda ,b , . . . ,2 ,0 ,L , . . . , I the te rmina ls . A der iva t ion f ihe iden t i f ie r a0 is

    (id) + \letter) (rest)* n (r'est)+ a (di11it) 'rest)=+ z0 re,rt).+ 40.

    *As explained in the Prefar:e, an astcriskterial.

    following a heading indicates o ptional ma-

  • 8/13/2019 208-2

    21/25

    30 Chopter I Ilrrn,ooucrroN To rsn TspoRy oF' CjoMptrm'rroN

    The definition of programuritrg Iatrguages lrough grrrrrrrrrirrs s t:t)Inrnorratrd very useful. But therc irrtl irlttlrrrativcs thrr,t rlre often convenient. Forexample, we can describe a lartguage by art at:cclltc:r, trrkirrg cvcry stringthat is accepted as part of the language. To talk irtrout this irr ir. prur:istrway, we will need to give a rrrore fornal deflnition of arr autornirtorr. Wc willdo this shortly; for the rnomenl, Iel us proceed n a rrrorc rrtuitivc way.

    An automa,ton ca,n be represented by a graph in which the vertices givctlte itrterttal stirtc:s rrrrl hc edges ransitions. The labels on the edges showwhat happens (in terrns of i rrput irrrrl outprrt) druing the tra,nsition. Forexample, Figure 1.4 represents a transitiorr frorn St.r,te to State ?, whichis taken when thc irrput syrrrhol s a,. With this intuitive picture in rnind,let us look at another wrry of rlcst:ritlirrg Perirctr,l dentifiers.

    I

    Figure 1.5 is an automaton that accepts all le :gal Pir,sctrl dentifiers. Someintcrpretation is necessa,r'v. We a,sslrrrle hat initially the automerton is inState 1; we irrrlir:atrr his by drawing an arrow (not originating in any vertex)to this state. As alwnys, the string to be exarnined s read Ieft to right,, orrecharacter at eac:h stttp. When the first symbol is a letter, the automatorrgoes nto State 2, aftr:r whir:h the rest of the string is immaterial. State 2therefore rcprcstlrrts he "yes" sta,te of the accepter. Conversely, f the firstsymbol is a digit, the autorna torr will go into Sta,te 3, the "no" state, andremain there. In our solu tiorr, we assurnc ha,t no inprrt other than lettersor digits is possiblc.

    I

    Figurc 1.4

    Figure 1.5

    Lcttcr or digit

  • 8/13/2019 208-2

    22/25

    1.3 Soun Appl,IcartoNs 31

    Cornllilcrs anrl other translators that convert a progril,ilr frorn ttttc latt-guage to another make extensive use of the idetr,s orrr:htxl orr itr these exam-ples. Programming la,nguages a,n be clefintrdprtx:iscly tlrrough gralnmars,as in Exarnple 1.15, and both grirnrmiilrr rrrd rutotnata play a futrdanrentalrole in the decision procerJsert y whir:h a specific piece of code s a,ccepted ssatisfying the conditions of ir progritrtrtttittg atrguage. The a,bove exa,mplegives a first hint of how this is done; subsequeut exa,mples will cxparrd ortthis obscrvatiorr.

    Arxlthc:r irnportant application area is cligital desigrr, where transducerr:oncepts arc prt:valent. Although this is a subject thtr,t wc will not treatcxttlrrsivc:ly lrrlre, we will give a simple exa,mple. Irr prirrciple, any digitaltxrrrrputrlr arr be viewed as a,n automaton, brrt srrch t vicw is not necessarilya1ryro1lriate. Suppose we consider the interner,l cgisters and main memoryof a corrrputer as tire autorna,ton's :ontrol unit. Then tlte automa'ton ha,s a,

    total of 2"' internal states, where n is tht: total rnrmber of bits in the registersancl memory. Even for smir.ll rz, this is such a large number tha't the resultis impossible to work wittr. Brrt i f wtl look at a rnuch stnaller unit, thenarrtomiltil thcxlry becotttes a useful design tool.

    A binary a,clder s an intcgral part of atry getreral purpose computcr. Suchan adder ta,kels wo llit strings represeuting numbers, and ltrtlduces their$llm a$ orrtput. Ftlr sirnplicily, let us assume hat wt+ r,rtl ltlaling only withpositivc irrttlgc:rs rrtd tlnt we use a, representation in whiclt

    f r : Q O Q I " ' Q n

    stands for the integer'11,

    , , - \ - \ - , , , , r"s \ " ' /? o * ' - '

    This is ihe usual binary repre$orltirtitlr t tt teverse.A serial a,dder procx:sscls wo such nunbers z : &oet " a,,, and y :

    bobr . .b,, bit by trit, starting at the left end. Each hit addition creates a

    digit fbr the srrm as wtlll as a carry cligit for the netxt highcr position. Abinilry adclitiort table (F gr.rre 1.[i) srrmmarizcs ]rc lrrocess.

    A block diagram of the kinrl wo sirw when we first studied computelrsis given in Figure 1.7. It tells rrs thilt an rrdder is a box that accepts twcrbits arrd produces their sum hit and ir possiblc carry. It describes what anadder does, but explains little nborrt its irttertral workings. An au tomrr,torr(now a transducer) can milke this rnuch more explicit,

    The input to the trir,rrsrhrt:t:r re Lhe bit pairs (ai, bi), the output will bethe sum bit di, Again, w,, (]I)r()scnt he autouratou by a, graph ntlw lrrbelitrg

  • 8/13/2019 208-2

    23/25

    32 Chopter I InrnonucTroN To rHn THoonv or Corr,rpurnuon

    0 I

    00

    No carry1

    No carry

    INo carry

    0Carry

    Figure 1.6

    Figure 1.7 di +

    bi- Serial adder

    Sum bit d,

    tlre edges (ot,ht.) lrl;. The carry from one step to the rrext is rernerntruredby the automatorr via tw(l internal sta,tes abeled "c&rry" and "no carry."Initially, the tratnducr:r will ber n state "no carry." It will remain in thisstate until a bit pair (1, 1) is encountered; this will generate a ca,rry tha,ttakes the automa ton into the "carrytt statc. Thc pre]$ence f a ca,rry s thentaken into account when the next bit pair is read. A corrrplete picture of aserial adder is given in Figure 1.8. Follow this tlrrough with a fcrw examplesto convirrce yourself that it works correctly,

    As this examplc irrdir:ates, the automaton serves as a bridge betwecrrthe very high-level, firrrctional description of a circuit and its logical irnple-mentation through transistors, gates, arrd flip-flops. The automaton clearlyshows the decision logic, yet it is forrrral cnorrgh to lend itself to precise

    (0,1)/1Figure 1.8

    Carry

    (0,0)/1

  • 8/13/2019 208-2

    24/25

    1 ..)

    3 .

    4 .

    6 .

    1..3 Sour: Apptlcauotqs 33

    mathematica,l manipulatiorr. Frrr this reasotr, digital design methods rt:lyheavily en crlrrr:opts f'rorrr autotnata theory. The interested retadcr shouldlook irt a typical text otr this topic, for example Kova,hi 1978.

    I

    Give a grarrrnar for thc sct integer numbers in C. {ffi

    Design an acceptcr for integers in C.

    Give a grammar that generates all reals in C.

    Suppose that a certain prograrnrning language permits only identifiers thatbegin with a lctter, contain at lea^ st rre but lto Inore than three digits, and

    can have any numberof letters. Cive a gramlnar and an accepter for srrch a

    sct of identifiers.

    5. Give a gralrllrlar for the var declaration in Pa"scal

    In the rornan number systern, tlttttbers a,re reprcscnted bv strings orr thealplrabet {M, D,C,I', X,I{ 1}. Design an accepter that accepts such utringsonly if they are properly ftrrrned roman numbers. I'br simplicity, replace the"subtraction" conventiort in which t,he numbcr nine i.i represented trv IX withan atldition equivalent that uses V1111 instead.

    Wc assumed that an autornatott works in a framework of discrete tirrre steps,but this aspect has little influence on our subsequent discussion. In digitaldesign, however, the tirnc clcnrent a*ssume$ onsitlerable significance,

    In orrler to synchronize signals arriving from different parts of the com-

    Jlrter, delay circuitry is needed. A unit-delay trarrsducer is onc that simplyreproduces the input (viewed a*sa contimtal streatn of symbols) one time unitlater, Specifically, f the transdur:er reads as input a svmbol a at time f , it willrcproduce that syrnbol as output at time t + 1. At time f : 0, the tratrsduceroutputs nothirrg. We indicatc this by saying that the transducer translatesinput arar ' . ' into outprr t Aara. : . . .

    Draw a, graph showirrg how such a unit-dclay tra'nsdtrcer miglrt be de-signed for E : {4, b} m

    L An n-unit delay transducer is one that reproduces the input rz time rrnitsIater ; that is , thc input ate ,z" ' is t rarrs lated nto , \ 'arar "' , meaning againthat thc transducer protlut:es to output for thc first n, time rrlots.

    (a) Construct a two-urrit delay transducer on D : {4, b} .

    (b) Show that an n-rrnit delay transducer must have at lea"st lDl"'states.

    I, The two's t:ornpletnen t of a binary string, represerrtir tg a positive integer'is formetl by first complemcnting each bit, then adding one to the lowest-order bit. Design a transducer for trarrslating bit strings into their two'scotnplement, assuming that the binary number is rcpresented as in Exanrple1.17, with lower-order l-ritsat the left of the string.

  • 8/13/2019 208-2

    25/25

    34 Chopter I InrnouucTro N To rHn Tnnonv or CoNapureuoN

    1.0. Design a transducer to convert a binary string into octal. For exarnple, thebit string 001101110 shoulrl produce the outprit 156. m

    1I ' Let araz- . - be an input bi t s t r ing. Design a t ransducer that computes theparity of everv substring of three bits. Specifically, the transdur:er shouldprodrrce orrtput

    T I : ' I r z : l l j

    r ; : ( a i - 2 * o , ; - r * a l ) m o d 2 , i , 3 , 4 , . . .

    .Fbr examplc, the input 110111 shoukl protluce 000001. W

    1 2. Design a transducer tb at accept s bit strings arazr,,it and computes the hinaryvalue of each set of three consecutive bits modulo five, More specifically, thetratrsducer should produce rnr I n7z n13, .., where

    mI : n1 ' 2 01

    m4 : (4a i *Z a t - r I a t - t ) r nod 5 , i : 3 , 4 , . .

    lS. Digital computers normally represent all inforrnatiorr by bit strings, usingsome type of encoding, For example, character information can be encodedusing the well-known ASCII systcm,

    I,br this exercisc, consider the two alphabets {.a,h,c,d} and {0, 1}, rc-spectively, arrd arr errt:oding from the first to thc sccond, defined by a + fifl,b + 01, c * 10, tl - 11. Construct a transducer for decoding strings on {0, 1}into the original rnessage. For exarnple, the inJrrrt 01t1011 hould Elenerate soutput bad.

    14. Let t antl 3r be two positive binary numbers. Design a transducer whose

    output is 'nzur(r, 'g) .