Sunteți pe pagina 1din 114

Limbaje Formale si Automate

˘

Instructor Andrei P AUN

Semestrul II 2009-2010

Departmentul de Infomrmatica Universitatea Bucuresti Bucuresti, Romania

1

Structura cursului:

1. Cunostinte de baza multimi, multiseturi, secvente, functii, relatii, metode si tehnici de demonstratie, alfabet, cuvinte, limbaje

2. Automate finite

automate finite nedeterministe (NFA), automate finite deterministe (DFA), transformarea de la NFA la DFA, proprietati pentru limbajele acceptate de automate,

lema de pompare

3. Expresii regulate

echivalenta dintre expresiile regulate si au- tomatele finite (in definirea limbajelor)

4. Gramatici independente de context (CFG)

simplificare si forme normale, algoritmi de parsare, lema de pompare si alte proprietati

2

5.

Automate pushdown (PDA)

nondeterministic pushdown automata (NPDA), deterministic pushdown automata (DPDA), PDA vs. CFGs

6. Masini Turing (TM)

diverse tipuri de TM, TM universale, decidabilitate si nedecidabilitate, complexitate de timp si spatiu

3

Motivare pentru curs

Compiler

Programming

Database

AI

Graphics

VLSI

Theory of Computing

(1) Putere de calcul

(2) Complexitate

(3) Software design

(4) Circuit design

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

 

4

I. Cunostinte de baza

1. Multimi 1) O multime este o colectie de elemente luate dintr-un univers.

2) O multime este finita daca contine un nu- mar finit de elemente, si este infinita in caz contrar.

3) Cardinalitatea unei multimi este numarul sau de elemente si se noteaza cu #𝐴 sau 𝐴(pen-

tru multimea 𝐴).

5

– Cum specificam o multime:

(1) Listam toate elementele sale:

{} (or ),

𝐴 = {2,

3,

5,

7},

𝐵 = {3,

2,

2,

7,

5,

7}

(2) Precizam o proprietate: {𝑥 𝑃 (𝑥)}

𝐶 = {𝑥 𝑥 este numar prim}, 𝐷 = {𝑥 𝑥 este un cuvant in romana si 𝑥 este si palindrom}, 𝐸 = {𝑥 𝑥 este un intreg divizibil cu 3 si mai mic decat 10 }

(3) Definite recursiv:

Forma generala:

i) Anumite elemente initiale sau de baza sunt in 𝐴 ii) Daca 𝑎, 𝑏 𝐴, atunci 𝑎 𝑏 𝐴 iii) Nici un alt element nu apartine lui 𝐴

6

Exemplu Definirea lui A:

i) 2 𝐴 ii) Daca 𝑎,

iii) Nici un alt element nu apartine lui 𝐴

𝑏 𝐴, atunci 𝑎 + 𝑏 𝐴

(4) Inchiderea la operatii

Spunem ca o multime 𝐴 este inchisa la o op- eratie binara daca pentru toate 𝑎, 𝑏 𝐴,

𝑎 𝑏 𝐴

Spunem ca

𝐵 este o inchidere a lui 𝐴 la operatia daca nu exista 𝐵 astfel incat 𝐴 𝐵 𝐵 si 𝐵 este inchisa la .

Forma generala (pentru multimi definite prin

Fie 𝐵 𝐴 si 𝐵

este inchisa la .

inchidere) 𝐴 este cea mai mica multime care contine an- umite elemente initiale (de baza) si care este inchisa la operatia .

7

Exemplu

𝐵 este cea mai mica multime care il contine pe 𝜏 si este inchisa la “+”.

— Operatii cu multimi

Daca se dau 𝐴 si 𝐵 doua multimi:

cu multimi Daca se dau 𝐴 si 𝐵 doua multimi: 𝐴 ∪ 𝐵 = { 𝑥

𝐴 𝐵 = {𝑥 𝑥 este in 𝐴 sau 𝑥 este in 𝐵}

{ 𝑥 ∣ 𝑥 este in 𝐴 sau 𝑥 este in 𝐵 } 𝐴 ∩ 𝐵

𝐴 𝐵 = {𝑥 𝑥 este in 𝐴 si 𝑥 este si in 𝐵}

𝑥 ∣ 𝑥 este in 𝐴 si 𝑥 este si in 𝐵 } 𝐴 − 𝐵

𝐴 𝐵 = {𝑥 𝑥 este in 𝐴 si 𝑥 nu este in 𝐵}

𝐴 si 𝑥 este si in 𝐵 } 𝐴 − 𝐵 = { 𝑥 ∣ 𝑥

8

𝑛

𝑖=1

𝑛}

𝑛

𝑖=1

𝐴 𝑖 = {𝑥 𝑥

este

𝐴 𝑖 = {𝑥 𝑥

este

in 𝐴 𝑖 , pentru un 𝑖,

in 𝐴

𝑖 ,

1 𝑖 𝑛}

1 𝑖

— Proprietati ale operatiilor cu multimi

Distributivitate

𝐴 (𝐵 𝐶) = (𝐴 𝐵) (𝐴 𝐶)

𝐴 (𝐵 𝐶) = (𝐴 𝐵) (𝐴 𝐶)

Indempotenta

𝐴 𝐴 = 𝐴;

𝐴 𝐴 = 𝐴

Involutie ¬(¬𝐴) = 𝐴

Comutativitate

𝐴 𝐵 = 𝐵 𝐴;

Asociativitate

𝐴 𝐵 = 𝐵 𝐴

𝐴 (𝐵 𝐶) = (𝐴 𝐵) 𝐶

𝐴 (𝐵 𝐶) = (𝐴 𝐵) 𝐶

9

Regulile lui De Morgan

¬(𝐴 𝐵) = ¬𝐴 ∩ ¬𝐵 ¬(𝐴 𝐵) = ¬𝐴 ∪ ¬𝐵

Multimea submultimilor Multimea tuturor submultimilor unei mul- timi 𝐴 (“power set” of 𝐴) se noteaza cu 2 𝐴 .

Exemplu:

𝐴 = {2, 3, 5} 2 𝐴 = {∅, {2}, {3}, {5}, {2, 3}, {2, 5}, {3, 5}, {2, 3, 5}}

— Cateva multimi speciale:

: multimea vida; 𝒵: multimea intregilor; 𝒩 : multimea intregilor pozitivi; 𝒩 0 : multimea intregilor care nu sunt negativi; : multimea numerelor reale. .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

10

2. Multiseturi Un multiset este o colectie de elemente dintr-un univers oarecare, colectie in care repetitiile nu sunt ignorate.

3. Secvente O secventa de elemente dintr-un univers oare- care este o lista de elemente care sunt ordonate (fiecare element are o pozitie).

11

4. Functii

Definitie Daca se dau doua multimi 𝐴 si 𝐵, o functie (partiala) de la 𝐴 la 𝐵 asociaza cu fiecare 𝑎 din 𝐴 (cel mult) un element 𝑏 din 𝐵.

Notatii:

functie 𝑓 : 𝐴 𝐵 asocierea 𝑓 (𝑎) = 𝑏 nedefinita 𝑓 (𝑎) = 𝑢𝑛𝑑𝑒𝑓

Functii speciale:

functia identitate:

𝑓 : 𝐴 𝐴 si

𝑓 (𝑎) = 𝑎 pentru toti 𝑎 𝐴

Functia caracteristica a lui 𝐴 pentru 𝐵:

𝐵 𝐴

𝑓 : 𝐴 → {𝑡𝑟𝑢𝑒, 𝑓 𝑎𝑙𝑠𝑒}

𝑓 (𝑎) = 𝑡𝑟𝑢𝑒, 𝑖𝑓

𝑓 (𝑎) = 𝑓 𝑎𝑙𝑠𝑒,

𝑎 𝐵 𝑎 𝐴 𝐵

𝑖𝑓

12

Cateva proprietati ale functiilor 𝑓 : 𝐴 𝐵

totala:

𝑓 (𝑎) este definita pentru toate elementele 𝑎 din 𝐴 surjectiva:

pentru toate elementele 𝑏 din 𝐵 exista un 𝑎 din 𝐴 astfel incat 𝑓 (𝑎) = 𝑏 injectiva:

𝑎,

𝑎 𝐴,

bijectiva:

𝑎 = 𝑎

implica faptul ca 𝑓 (𝑎) = 𝑓 (𝑎 )

in acelasi timp si surjectiva si injectiva

Notatia big-O

𝑓 (𝑛) = 𝑂(𝑔(𝑛)) daca exista 𝑐 > 0 si intregul poz-

itiv 𝑑 astfel incat pentru toate 𝑛 𝑑

𝑓 (𝑛) 𝑐𝑔(𝑛)

𝑓 (𝑛) = Ω(𝑔(𝑛)) daca

𝑐𝑓 (𝑛) 𝑔(𝑛)

𝑓 (𝑛) = Θ(𝑔(𝑛)) daca

𝑓 (𝑛) = 𝑂(𝑔(𝑛)) si 𝑓 (𝑛) = Ω(𝑔(𝑛))

13

functie (partiala) 𝑚,𝑛

𝑓 : 𝐴 1 × 𝐴 2 ×

× 𝐴 𝑚 𝐵 1 ×

𝑓(𝑎 1 , 𝑎 2 , sau 𝑓 (𝑎 1 , 𝑎 2 ,

, 𝑎 𝑚 ) = (𝑏 1 ,

, 𝑏 𝑛 )

, 𝑎 𝑚 ) = 𝑢𝑛𝑑𝑒𝑓

14

× 𝐵 𝑛

5. Relatii

O relatie 𝑛-ara 𝑅, 𝑛 1, pentru multimile

𝐴 1 ,

, 𝐴 𝑛 este orice submultime 𝑅 a produsu-

lui cartezian 𝐴 1 ×

× 𝐴 𝑛 .

Exemplu

lui cartezian 𝐴 1 × × 𝐴 𝑛 . Exemplu 𝑃 { 𝑝 1 , 𝑝

𝑃 {𝑝 1 , 𝑝 2 }, 𝐶 = {𝑐 1 , 𝑐 2 , 𝑐 3 , 𝑐 4 }

𝑇 𝑃 × 𝐶

=

𝑇 = {(𝑝 1 , 𝑐 1 ), (𝑝 1 , 𝑐 2 ), (𝑝 1 , 𝑐 3 ), (𝑝 2 , 𝑐 1 ), (𝑝 2 , 𝑐 4 )}

15

Compunerea relatiilor

Fie 𝑅 𝐴×𝐵 si 𝑆 𝐵×𝐶 doua relatii. Atunci compunerea lui 𝑅 cu 𝑆, notata prin 𝑅 𝑆, este relatia 𝑅 𝑆 𝐴 × 𝐶, definita prin:

𝑅 𝑆 = {(𝑎, 𝑐) (𝑎, 𝑏) este in 𝑅 si (𝑏, 𝑐) este in 𝑆 pentru un 𝑏 din 𝐵}

Compunerea unei relatii 𝑅 : 𝐴 × 𝐴 cu ea insasi

𝑅 0

𝑅 1 : 𝑅

𝑅 𝑖 ,

𝑅 + = 𝑖=1 𝑅 𝑖 inchiderea tranzitiva a lui 𝑅 𝑅 = 𝑖=0 𝑅 𝑖 inchiderea reflexiva si tranzitiva a lui 𝑅

{(𝑎, 𝑎) 𝑎 este in 𝐴}

:

𝑅 𝑅 𝑖1

:

𝑖1

16

Proprietatile relatiei 𝑅 𝐴 × 𝐴

reflexiva: 𝑎𝑅𝑎 pentru toti 𝑎 din 𝐴

∙ reflexiva: 𝑎𝑅𝑎 pentru toti 𝑎 din 𝐴 ∙ simetrica: 𝑎𝑅𝑏 implica 𝑏𝑅𝑎 ∙

simetrica: 𝑎𝑅𝑏 implica 𝑏𝑅𝑎

𝐴 ∙ simetrica: 𝑎𝑅𝑏 implica 𝑏𝑅𝑎 ∙ antisimetrica: 𝑎𝑅𝑏, 𝑏𝑅𝑎 implica

antisimetrica: 𝑎𝑅𝑏, 𝑏𝑅𝑎 implica 𝑎 = 𝑏

simetrica: 𝑎𝑅𝑏 implica 𝑏𝑅𝑎 ∙ antisimetrica: 𝑎𝑅𝑏, 𝑏𝑅𝑎 implica 𝑎 = 𝑏 17

17

tranzitiva: 𝑎𝑅𝑏, 𝑏𝑅𝑐 implica 𝑎𝑅𝑐

𝑎𝑅𝑏, 𝑏𝑅𝑐 implica 𝑎𝑅𝑐 △ O relatie binara 𝑅 peste 𝐴 este o relatie de

O relatie binara 𝑅 peste 𝐴 este o relatie de echivalenta daca este

reflexiva,

simetrica, si

tranzitiva.

O relatie de echivalenta 𝑅 peste 𝐴 defineste submultimi disjuncte ale lui 𝐴, numite clase de echivalenta.

[𝑎] 𝑅 = {𝑏 𝑏 este in 𝐴 si 𝑎𝑅𝑏}

18

Fie 𝑅 o relatie binara de echivalenta peste 𝐴 si 𝐵 𝐴. Spunem ca 𝐵 este 𝑅-inchisa daca pentru toti 𝑎 din 𝐵, 𝑎𝑅𝑏 implica faptul ca 𝑏 este in 𝐵.

Exemplu 𝐴 : multimea intregilor;

𝐵 = {9, 4, 3, 2};

𝑅 :“este patratul lui” (“is the square

of”) Atunci 𝐵 este 𝑅-inchisa.

𝑅 este o relatie n-ara peste 𝐴.

𝑅-inchisa daca pentru toate 𝑏 1 ,

avem ca daca (𝑏 1 , 𝑏 𝑛 este in 𝐵.

𝐵 𝐴 este , 𝑏 𝑛1 din 𝐵,

, 𝑏 𝑛1 , 𝑏 𝑛 ) este in 𝑅 atunci

Exemplu 𝐴 : multimea tuturor intregilor;

𝑅 = {(𝑎, 𝑏, 𝑐) 𝑎 × 𝑏 = 𝑐};

𝐵 = {𝑖𝑎 𝑖 1} pentru un intreg 𝑎

Atunci 𝐵 este 𝑅-inchisa.

19

Lema Fie 𝑅 o relatie de echivalenta peste 𝐴. Atunci pentru toti 𝑎, 𝑏 din 𝐴 avem sau [𝑎] 𝑅 = [𝑏] 𝑅 sau [𝑎] 𝑅 [𝑏] 𝑅 =

O relatie binara 𝑅 peste 𝐴 este o ordine par- tiala daca este

reflexiva,

tranzitiva, si

antisimetrica.

Inchidere

𝑅 + este inchiderea tranzitiva a lui 𝑅

– O reprezentare in digraf pentru o inchidere tranzitiva:

– 𝑅 + este inchiderea tranzitiva a lui 𝑅 – O reprezentare in digraf pentru o

20

6. Cardinalitate

Daca se dau doua multimi 𝐴 si 𝐵, spunem ca ele au cardinalitate egala daca exista o

bijectie 𝑓 : 𝐴 𝐵.

Exemplu #𝒵 = #𝒩 0

𝑓(𝑖) = 2𝑖 1,

𝑓 (𝑖) = 2𝑖,

𝑖 0

𝑖 < 0

O multime este numarabila sau enumerabila daca este ori finita ori infinita, caz in care are aceeasi cardinalitate cu 𝒩 .

21

7. Metode de demonstratie

— demonstratie directa

— demonstratie prin contradictie

— demonstratie prin inductie

Demonstratie directa Enumeram toate cazurile pentru a arata ca proprietatea este adevarate.

Exemplu Orice numar par dintre 4 si 2 32 este

suma a doua numere prime.

Demonstratie prin contradictie

Stabilim validitatea propozitiei presupunand ca nu este adevarata si inferam o contradictie.

Exemplu Exista un numar infinit de numere

impare.

22

Demonstratia prin inductie

– baza

– ipoteza inductiva

– pasul inductiv

– concluzie

Two forms:

(1) — Prove it holds for a basis value 𝑏,

— Hypothesize it holds for value 𝑛,

— Prove it holds for value 𝑛 + 1.

(2) — Prove it holds for basis values,

— Hypothesize it holds for all values less than 𝑛 and greater than basis value,

— Prove it holds for value 𝑛.

23

Example (proof by induction)

(i) no two lines are parallel;

(ii) no three lines have a common intersec- tion point.

Prove that the number of regions in an arrangement of 𝑛 lines is

1 + 𝑛(𝑛 + 1)/2

24

8. Proof Techniques

— pigeonhole principle

— counting

— diagonalisation

The Pigeonhole Principle

Let 𝑛 1 be an integer, let there be 𝑛 pigeonholes, and there be 𝑛 + 1 items of mail

to be placed in the pigeonholes. Then, how- ever the the items are placed, at least one pigeonhole will contain at least two items.

The Pigeonhole Principle(Extended version)

Let 𝑞 1 , ger 𝑁 (𝑞 1 ,

ever 𝑛 items are placed into 𝑡 pigeonholes,

there exists 𝑖,

𝑖-th

, 𝑞 𝑡 1. There exists a positive inte-

, 𝑞 𝑡 ), then how-

, 𝑞 𝑡 ).

If 𝑛 𝑁 (𝑞 1 ,

1

𝑖

𝑡

such that the

pigeonhole contains at least 𝑞 1 + 1 items.

25

Example Let 𝐺 = (𝑉, 𝐸) be a digraph with

#𝑉 = 𝑛 1. Then 𝐺 has a cycle if and only if 𝐺 has a path of length at least 𝑛.

with # 𝑉 = 𝑛 ≥ 1 . Then 𝐺 has a cycle if and only

26

9. Alphabets, Words, and Languages

An alphabet is a finite, nonempty set of

elements. The elements of the alphabet are called symbols or letters.

A word over an alphabet Σ is a finite se- quence of symbols from Σ.

A language is a set of words.

Examples:

(1) English alphabet, words and language.

(2) Alphabet: {𝑎, 𝑏}

words: 𝜆, 𝑎,

language: {𝜆, 𝑎𝑏,

𝑏,

𝑎𝑏, 𝑏𝑎,

. 𝑎𝑎𝑏𝑏, 𝑎𝑎𝑎𝑏𝑏𝑏, {𝑎 𝑖 𝑏 𝑖 𝑖 0}

.

.

.

.

.

i.e.

.

.

.

.}

(3) Alphabet : Fortran reserved words and identifiers words : a Fortran program Language : the set of all Fortran programs.

27

Empty word is a word over every alphabet.

𝜆

The length of a word is the number of sym-

bols in the given word.

Example:

𝜆= 0

𝑎𝑏𝑏𝑐= 4

The 𝑎-length of a word 𝑥 is the number of

times 𝑎 occurs in 𝑥.

Example:

011010 = 2

𝑐𝑏𝑐𝑏𝑐𝑎 = 0

28

The catenation of two words 𝑥 and 𝑦, de-

noted by 𝑥𝑦, is the word obtained by append- ing the word 𝑦 to 𝑥.

Example 𝑥 = 𝑎𝑏𝑐

𝑦

= 𝑑𝑒𝑓𝑔

𝑥𝑦

= 𝑎𝑏𝑐𝑑𝑒𝑓𝑔

𝑦𝑥 = 𝑑𝑒𝑓𝑔𝑎𝑏𝑐

𝑥𝜆 = 𝜆𝑥 = 𝑥

𝜆𝜆 = 𝜆

Catenation is associative:

(𝑥𝑦)𝑧 = 𝑥(𝑦𝑧)

△ ∣𝑥𝑦= 𝑥+ 𝑦

𝑥 0 = 𝜆 (Power of a word)

𝑥 𝑖 = 𝑥𝑥 𝑖1 ,

𝑖 1

Example (𝑎𝑏𝑐) 0 = 𝜆

(𝑎𝑏𝑐) 3 = 𝑎𝑏𝑐𝑎𝑏𝑐𝑎𝑏𝑐

29

Prefix: 𝑥 is a prefix of 𝑦 if 𝑧 such that

𝑥𝑧 = 𝑦

Example 𝑛𝑜𝑡 is a prefix of 𝑛𝑜𝑡𝑖𝑐𝑒

𝜆 is a prefix of any word

Any word is a prefix of itself

Suffix: 𝑥 is a suffix of 𝑦 if 𝑧 such that

𝑧𝑥 = 𝑦

Example 𝑎𝑙𝑙𝑜𝑤 is a suffix of 𝑠𝑤𝑎𝑙𝑙𝑜𝑤

𝜆 is a suffix of any word

01011 is a suffix of 01011

Subword: 𝑥 is a subword of 𝑦 if 𝑤, 𝑧 s.t.

𝑤𝑥𝑧 = 𝑦

Example The subwords of 𝑏𝑒𝑎𝑡 are:

𝜆,

𝑏,

𝑒,

𝑎,

𝑡,

𝑏𝑒,

𝑒𝑎, 𝑎𝑡,

𝑏𝑒𝑎,

𝑒𝑎𝑡, 𝑏𝑒𝑎𝑡

Proper prefix (suffix, subword):

𝑥 is a proper prefix (suffix, subword) of 𝑦 if 𝑥 is a prefix (suffix, subword) of 𝑦 and 𝑥 = 𝜆 and 𝑥 = 𝑦.

30

Reversal:

(i) 𝑥 𝑅 = 𝑥, if 𝑥 = 𝜆; (ii) 𝑥 𝑅 = 𝑦 𝑅 𝑎, if 𝑥 = 𝑎𝑦 for 𝑎 Σ, 𝑦 is a word

Lemma If 𝑥 = 𝑎 1

𝑥 𝑅 = 𝑎 𝑛

Proof: By induction on 𝑥. Basis: 𝑥= 0. 𝜆 𝑅 = 𝜆 by (i). I.H.: Assume Lemma holds for all 𝑥 with 𝑥= 𝑛.

I.S.: Consider a word 𝑥 with 𝑥= 𝑛 + 1.

𝑎 𝑛 for some 𝑛 0, then

𝑎

1 .

Then 𝑥 = 𝑎 1

𝑎 𝑛+1 = 𝑎 1 𝑢, where

𝑢 = 𝑎 2

𝑎 𝑛+1 .

Then

But 𝑢= 𝑛, by I.H., 𝑢 𝑅 = 𝑎 𝑛+1 so we get

𝑥 𝑅 = 𝑢 𝑅 𝑎 1 .

𝑥 𝑅 = 𝑎 𝑛+1

𝑎 1 . 𝑞𝑒𝑑

𝑎 2 ,

Universal Language over Σ: Σ

Σ = {0,

1}, Σ = {𝜆,

0,

31

1, 00,

01,

10, 11, 000,

.}

Mappings (Morphism and Substitution)

Definition Let Σ and Δ be two alphabets. Then a mapping : Σ Δ is said to be a morphism if

(i) (𝜆) = 𝜆; (ii) (𝑥𝑦) = (𝑥)(𝑦), for all 𝑥, 𝑦 in Σ .

Note: (ii) implies that we need only define the images of letters to define the images of words.

Example:

Σ = {0,

(0) = 𝑏𝑎𝑎𝑐, ℎ(1) = 𝜆. Then (1001) = (1)(0)(0)(1) = 𝜆𝑏𝑎𝑎𝑐𝑏𝑎𝑎𝑐𝜆 = 𝑏𝑎𝑎𝑐𝑏𝑎𝑎𝑐

1},

Δ = {𝑎,

𝑏,

𝑑},

𝑐,

A morphism is said to be 𝜆-free if, for all

𝑥 = 𝜆,

(𝑥) = 𝜆.

32

Definition Let Σ and Δ be two alphabets. A mapping 𝜎 : Σ 2 Δ is said to be a substitution if i) 𝜎(𝜆) = {𝜆};

ii) 𝜎(𝑥𝑦) = 𝜎(𝑥)𝜎(𝑦), for all 𝑥,

Example:

𝑦 Σ .

Σ 1 = {𝑎,

𝑏},

Δ 1 = {𝑎,

𝑏,

𝑐},

a substitution 𝜎 1 :

 

𝜎 1 (𝑎) = {𝑎𝑏, 𝑎𝑐, 𝑏}; 𝜎 1 (𝑏) = {𝑏, 𝑏𝑎}.

Then 𝜎 1 (𝑏𝑎) =

 

Example:

Σ 2 = {0,

1,

2},

Δ 2 = {𝑎,

𝑏},

𝜎 2 (0) = {𝑎 𝑖 𝑖 1} 𝜎 2 (1) = {𝑏 𝑖 𝑖 1}

𝜎 2 (2) = {𝜆}

Then 𝜎 2 (1020) =

33

Given an alphabet Σ,

lows:

Σ is defined as fol-

(i) 𝜆 Σ ; (ii) if 𝑥 Σ , then 𝑎𝑥 Σ for all 𝑎 Σ.

A language 𝐿 over an alphabet Σ is a subset

of Σ , i.e.,

𝐿 Σ .

Example:

𝜙 is a language over every alphabet . {𝜆} is a language over every alphabet.

{𝑎 𝑖 𝑏 𝑖 𝑖 0} is a language over {𝑎,

{𝑎 𝑖 𝑏 𝑖 𝑐 𝑖 𝑖 0} is a language over {𝑎,

𝑏}

𝑏,

34

𝑐}

Catenation of Languages

𝐿 1 𝐿 2 = {𝑥 1 𝑥 2 𝑥 1 in 𝐿 1 , usually written as 𝐿 1 𝐿 2 .

Examples

𝐿 1 = {𝑎,

𝑎𝑏};

𝐿 2 = {𝑏𝑐,

𝑐}

𝐿 1 𝐿 2 = {𝑎𝑏𝑐, 𝑎𝑐, 𝑎𝑏𝑏𝑐}

𝑥 2 in 𝐿 2 },

(Note that 𝐿 1 ×𝐿 2 = {(𝑎, 𝑏𝑐), (𝑎, 𝑐), (𝑎𝑏, 𝑏𝑐), (𝑎𝑏, 𝑐)})

𝜙𝐿 1 =

{𝜆}𝐿 1 =

{𝜆}𝜙 =

𝐿 𝐴 = {𝑎,

𝜆};

𝐿 𝐵 = {𝑎𝑏,

𝑏}

𝐿 𝐴 𝐿 𝐵 =

35

Powers of Languages:

𝐿 0 = {𝜆}

𝐿 𝑛+1 = 𝐿 𝑛 𝐿,

𝑛 1

Plus 𝐿 + =

𝑖=1 𝐿 𝑖

Star

𝐿 = 𝑖=0 𝐿 𝑖

𝐿 0 𝐿 1 𝐿 2

Examples

𝐿 = {𝑎𝑏, 𝑏}. Then 𝐿 0 = {𝜆}

𝐿 1 = 𝐿 = {𝑎𝑏,

𝐿 2 = {𝑎𝑏𝑎𝑏, 𝑏𝑎𝑏,

𝑏}

𝑎𝑏𝑏, 𝑏𝑏}

𝜙 0 = {𝜆} ∕= 𝜙 {𝜆} 0 = {𝜆}

𝐿 = {𝜆,

𝑎𝑏,

(Note 𝑎𝑏𝑎𝑏𝑎𝑏𝑎𝑏 𝐿 4 )

𝑏,

𝑎𝑏𝑎𝑏, 𝑏𝑎𝑏,

𝑎𝑏𝑏,

𝑏𝑏,

.

.

.

.

36

.

.}

Reversal of Languages

𝐿 𝑅 = {𝑥 𝑅 𝑥 is in 𝐿}

Examples

𝐿 𝐹 = {𝑎𝑎𝑏, 𝑎𝑏𝑏, 𝑎𝑏𝑏𝑏};

𝐿

𝑅

𝐹

= {𝑏𝑎𝑎, 𝑏𝑏𝑎, 𝑏𝑏𝑏𝑎}

𝐿 𝐼 = {𝑎 𝑚 𝑏 𝑛 𝑚 > 𝑛 > 0}

𝐿

𝑅

𝐼

= {𝑏 𝑛 𝑎 𝑚 𝑚 > 𝑛 > 0}

— properties of reversal

(𝐴 𝐵) 𝑅

=

𝐴 𝑅 𝐵 𝑅

(𝐴 𝐵) 𝑅

=

𝐴 𝑅 𝐵 𝑅

(𝐴𝐵) 𝑅

=

𝐵 𝑅 𝐴 𝑅

(𝐴 + ) 𝑅

=

(𝐴 𝑅 ) +

(𝐴 ) 𝑅

=

(𝐴 𝑅 )

Complementation of Languages

Given a language 𝐿 over Σ,

𝐿 = Σ 𝐿

Σ = 𝜙

𝐿 = {𝑎 𝑛 𝑏 𝑛 𝑛 0} over Σ = {𝑎,
𝐿 =

Σ ? {𝜆}

37

(𝐴) 𝑅 = 𝐴 𝑅 𝑏}

Alternative definition of 𝐿 + and 𝐿

𝐿 + = {𝑤 in Σ 𝑤 = 𝑤 1 𝑤 2

𝑤 1 , 𝑤 2 ,

for and 𝑛 1}

𝑤 𝑛 ,

, 𝑤 𝑛 𝐿

𝐿 = {𝑤 in Σ 𝑤 = 𝑤 1 𝑤 2

𝑤 𝑛 ,

𝑤 1 , 𝑤 2 ,

, 𝑤 𝑛 𝐿

for and 𝑛 0}

Given a word 𝑥 in Σ , and a language 𝐿 Σ

to test if 𝑥 is in 𝐿 we use 𝐿 = 𝑖=0 𝐿 𝑖 and test

if 𝑥 is in 𝐿 0 ,

𝐿 1 ,

.

.

.

.

38

Property Summaries

— properties of catenation (of languages)

Associativity — 𝐿 1 (𝐿 2 𝐿 3 ) = (𝐿 1 𝐿 2 )𝐿 3 Identity — 𝐿{𝜆} = {𝜆}𝐿 = 𝐿

Zero

𝐿𝜙 = 𝜙𝐿 = 𝜙

— Distributivity — 𝐿 1 (𝐿 2 𝐿 3 ) = 𝐿 1 𝐿 2 𝐿 1 𝐿 3

w.r. (𝐿 1 𝐿 2 )𝐿 3 = 𝐿 1 𝐿 3 𝐿 2 𝐿 3

Distributivity does not hold with respect to

— properties of Plus and Star

𝐿 𝐿 (𝐿 + ) + 𝜙 {𝜆} 𝐿 + 𝐿 𝐴 𝐵

+

+

+

=

=

=

=

=

=

𝐿 + ∪ {𝜆} 𝐿𝐿

𝐿 + , 𝜙, {𝜆}, 𝐿𝐿 + ,

( But 𝐿 + = 𝐿 − {𝜆}?)

(𝐿 ) = 𝐿 𝜙 = {𝜆}, {𝜆} = {𝜆}, 𝐿 𝐿 = 𝐿𝐿

𝜙 0 = {𝜆} 𝜙 2 = 𝜙

𝐴 + 𝐵 + and 𝐴 𝐵

39

Chapter 2.

FINITE AUTOMATA

The

lock has 1-bit sequential input. Initially the lock is closed. If the lock is closed it will open when the last three input signals are “1”, “0”, “1”, and then remains open.

Example Design a “sequential lock”.

— state (transition) diagram

remains open. Example Design a “sequential lock”. — state (transition) diagram — state (or transition) table

— state (or transition) table

remains open. Example Design a “sequential lock”. — state (transition) diagram — state (or transition) table

40

state set :

{0,

1,

2,

3}

input alphabet :

{0,

1}

Transition function : 𝛿(0, 0) = 0, 𝛿(0, 1) = 1,

Start state :

Final state set :

0

{3}

.

.

.

Deterministic Finite Automata (DFA)

𝑀 = (𝑄,

Σ,

𝛿,

𝑠,

𝐹 ) where

𝑄 is a finite nonempty set of states

Σ is the input alphabet

𝛿 : 𝑄 × Σ 𝑄 transition function

s start state

𝐹 𝑄 final state set

41

A computer is a finite state system (i.e. FA) which has millions of states.

There are many examples of FINITE STATE SYSTEMS. A finite automaton is an ABSTRACTION of them.

View a DFA as a machine

are many examples of FINITE STATE SYSTEMS . A finite automaton is an ABSTRACTION of them.

42

Specifying 𝛿

1)

Specifying 𝛿 1) State diagram (Transition diagram) Start state Final state 2) 43

State diagram (Transition diagram)

Specifying 𝛿 1) State diagram (Transition diagram) Start state Final state 2) 43

Start state

Specifying 𝛿 1) State diagram (Transition diagram) Start state Final state 2) 43

Final state

2)

Specifying 𝛿 1) State diagram (Transition diagram) Start state Final state 2) 43

43

Configurations

a word in 𝑄Σ

𝑝𝑥

where 𝑝 is the present state, and 𝑥 is the remaining input

Example:

0𝑎𝑎

1𝑎

. (start configuration)

.

.

.

.

.

2

(final configuration)

Moves of a DFA 0𝑎𝑎 1𝑎 1𝑎 2

𝑝𝑥 𝑞𝑦 if 𝑥 = 𝑎𝑦 and 𝛿(𝑝, 𝑎) = 𝑞

Configuration sequence

0𝑎𝑎 1𝑎 2

+

and

is a binary relation over 𝑄Σ . + : transitive closure of . : reflexive transitive closure of .

44

if 𝑝𝑥 𝑝 𝑖 1 𝑥 𝑖 1

0𝑎𝑎 +

0𝑎𝑎

0𝑎𝑎

0𝑎𝑎

2

𝑘

𝑝𝑥

𝑝 𝑖 2 𝑥 𝑖 2

2

2

0𝑎𝑎

2

𝑞𝑦

.

.

.

𝑘 steps

𝑞𝑦

Accepting Configuration Sequence

0𝑎𝑎

1𝑎

2

can also be viewed as a function

: 𝑄Σ 𝑄Σ ,

since the next configuration is determined uniquely for a given configuration.

The DFA stops when:

(i) we have no more input, or (ii) the next configuration is undefined.

45

A word 𝑥 is said to be accepted by a DFA 𝑀

if 𝑠𝑥

𝑓,

𝑓 𝐹.

The language of a DFA 𝑀 , 𝐿(𝑀 ), is defined

as:

𝐿(𝑀 ) = {𝑥 𝑠𝑥

𝑓,

for some 𝑓 𝐹 }

Examples

( 𝑀 ) = { 𝑥 ∣ 𝑠𝑥 ⊢ ∗ 𝑓, for some 𝑓 ∈ 𝐹

𝐿(𝑀) =

( 𝑀 ) = { 𝑥 ∣ 𝑠𝑥 ⊢ ∗ 𝑓, for some 𝑓 ∈ 𝐹

𝐿(𝑁) =

46

DFA membership problem

DFA MEMBERSHIP INSTANCE: A DFA, 𝑀 = (𝑄, Σ, 𝛿, 𝑠, 𝐹 ) and a word 𝑥 Σ . QUESTION: Is 𝑥 in 𝐿(𝑀 ) ?

Run the DFA 𝑀 with input 𝑥. In at most 𝑥steps it accepts, rejects or aborts.

Examples

∣ 𝑥 ∣ steps it accepts, rejects or aborts. Examples Checking for words that contain 𝑎𝑏𝑎

Checking for words that contain 𝑎𝑏𝑎 as subword.

Check: 𝑎𝑏𝑎𝑏𝑏𝑎 𝑎𝑏𝑏𝑎𝑎𝑏𝑏𝑏𝑎𝑏

47

Let 𝐿 denote any letter of English alphabet and 𝐷 any decimal digit; the form of PASCAL IDENTIFIERS can be specified by

digit; the form of PASCAL IDENTIFIERS can be specified by Recognizing comments that may go over

Recognizing comments that may go over several lines.

/* */

by Recognizing comments that may go over several lines. /* */ Δ : symbols other than

Δ: symbols other than and /

48

A DFA which has a total 𝛿 is said to be complete; if 𝛿 is nontotal it is

incomplete.

Theorem. Every incomplete DFA 𝑀 can be ”completed” by adding one new state (”sink”) to give DFA 𝑀 ’ such that 𝐿(𝑀 ) = 𝐿(𝑀).

Example:

such that 𝐿 ( 𝑀 ′ ) = 𝐿 ( 𝑀 ) . Example: 𝐿 (

𝐿(𝑀 ) is the set of all words that do not contain two consecutive 𝑎’s.

49

Two DFA 𝑀 1 and 𝑀 2 are equivalent

if 𝐿(𝑀 1 ) = 𝐿(𝑀 2 ).

The collection of languages accepted

by DFA’s is denoted by

𝐷𝐹 𝐴 .

It is called the family of DFA languages

and it is defined as:

𝐷𝐹 𝐴 = {𝐿 𝐿 = 𝐿(𝑀 ) for some DFA 𝑀 }

𝐾 = {𝑎 𝑖 𝑏 𝑖 𝑖 1} is not accepted by any DFA.

50

Proof: Use contradiction and Pigeonhole principle. Assume 𝐾 = 𝐿(𝑀 ), for some DFA

𝑀 = (𝑄, {𝑎, 𝑏}, 𝛿, 𝑠, 𝐹 ).

Let 𝑛 = #𝑄. Consider the accepting configuration sequence for 𝑎 𝑛 𝑏 𝑛 ,

𝑠 0 𝑎 𝑛 𝑏 𝑛 𝑠 1 𝑎 𝑛1 𝑏 𝑛

𝑠 𝑛 𝑏 𝑛

𝑠 2𝑛

where 𝑠 0 = 𝑠 and 𝑠 2𝑛 𝐹 .

appear during the reading of 𝑎 𝑛 , but there are only 𝑛 distinct states in 𝑄. By Pigeonhole principle at least one state must appear at least twice during the reading of 𝑎’s.

Assume 𝑠 𝑖 = 𝑠 𝑗 , 0 𝑖 < 𝑗 𝑛. Then

𝑠 0 𝑎 𝑛(𝑗𝑖) 𝑏 𝑛

Now 𝑛 + 1 states

𝑠 𝑖 𝑎