CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511...

60
CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of Computation Jean Gallier May 27, 2010

Transcript of CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511...

Page 1: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

CIS511Introduction to the Theory of Computation

Formal Languages and AutomataModels of Computation

Jean Gallier

May 27, 2010

Page 2: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2

Page 3: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

Chapter 1

Basics of Formal Language Theory

1.1 Generalities, Motivations, Problems

In this part of the course we want to understand

• What is a language?

• How do we define a language?

• How do we manipulate languages, combine them?

• What is the complexity of a language?

Roughly, there are two dual views of languages:

(A) The recognition point view.

(B) The generation point of view.

3

Page 4: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

4 CHAPTER 1. BASICS OF FORMAL LANGUAGE THEORY

No matter how we view a language, we are typically con-sidering two things:

(1) The syntax , i.e., what are the “legal” strings in thatlanguage (what are the “grammar rules”?).

(2) The semantics of strings in the language, i.e., whatis the meaning (or interpretation) of a string.

The semantics is usually a lot more interesting than thesyntax but unfortunately much more di!cult to deal with!

Therefore, sorry, we will only be dealing with syntax!

In (A), we typically assume some kind of “black box”,M , (an automaton) that takes a string, w, as input andreturns two possible answers:

Yes, the string w is accepted , which means that w be-longs to the language, L, that we are trying to define.

No, the string w is rejected , which means that w doesnot belong to the language, L.

Page 5: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

1.1. GENERALITIES, MOTIVATIONS, PROBLEMS 5

Usually, the black boxM gives a definite answer for everyinput after a finite number of steps, but not always.

For example, a Turing machine may go on computingforever and not give any answer for certain strings not inthe language. This is an example of undecidability .

The black box may compute deterministically or non-deterministically , which means roughly that on input w,the machine M is allowed to try di"erent computationsand to ignore failing computations as long as there is somesuccessful computation on input w.

This a"ects greatly the complexity of recognition, i.e,.how many steps it takes to process w.

Page 6: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

6 CHAPTER 1. BASICS OF FORMAL LANGUAGE THEORY

Sometimes, a nondeterministic version of an automatonturns out to be equivalent to the deterministic version(although, with di"erent complexity).

This tends to happen for very restrictive models—wherenondeterminism does not help, or for very powerfulmodels—where again, nondeterminism does not help, butbecause the deterministic model is already very powerful!

We will investigate automata of increasing power of recog-nition:

(1) Deterministic and nondeterministic finite automata(DFA’s and NFA’s, their power is the same).

(2) Pushdown automata (PDA’s) and determinstic push-down automata (DPDA’s), here PDA > DPDA.

(3) Deterministic and nondeterministic Turing machines(their power is the same).

(4) If time permits, we will also consider some restrictedtype of Turing machine known as LBA (linear boundedautomaton).

Page 7: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

1.1. GENERALITIES, MOTIVATIONS, PROBLEMS 7

In (B), we are interested in formalisms that specify alanguage in terms of rules that allow the generation of“legal” strings. The most common formalism is that of aformal grammar .

Remember:

• An automaton recognizes (or accepts) a language,

• a grammar generates a language.

• grammar is spelled with an “a” (not with an “e”).

• The plural of automaton is automata(not automatons).

For “good” classes of grammars, it is possible to build anautomaton, MG, from the grammar, G, in the class, sothatMG recognizes the language, L(G), generated by thegrammar G.

Page 8: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

8 CHAPTER 1. BASICS OF FORMAL LANGUAGE THEORY

However, grammars are nondeterministic in nature. Thus,even if we try to avoid nondeterministic automata, weusually can’t escape having to deal with them.

We will investigate the following types of grammars (theso-calledChomsky hierarchy) and the corresponding fam-ilies of languages:

(1) Regular grammars (type 3-languages).

(2) Context-free grammars (type 2-languages).

(3) The recursively enumerable languages or r.e. sets(type 0-languages).

(4) If time permit, context-sensitive languages(type 1-languages).

Miracle: The grammars of type (1), (2), (3), (4) corre-spond exactly to the automata of the corresponding type!

Page 9: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

1.1. GENERALITIES, MOTIVATIONS, PROBLEMS 9

Furthermore, there are algorithms for converting gram-mars to the corresponding automata (and backward), al-though some of these algorithms are not practical.

Building an automaton from a grammar is an importantpractical problem in language processing. A lot is knownfor the regular and the context-free grammars, but thereis still room for improvements and innovations!

There are other ways of defining families of languages, forexample

Inductive closures .

In this style of definition, a collection of basic (atomic)languages is specified, some operations to combine lan-guages are also specified, and the family of languages isdefined as the smallest one containing the given atomiclanguages and closed under the operations.

Page 10: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

10 CHAPTER 1. BASICS OF FORMAL LANGUAGE THEORY

Investigating closure properties (for example, union, in-tersection) is a way to assess how “robust” (or complex)a family of languages is.

Well, it is now time to be precise!

1.2 Alphabets, Strings, Languages

Our view of languages is that a language is a set of strings.In turn, a string is a finite sequence of letters from somealphabet. These concepts are defined rigorously as fol-lows.

Definition 1.2.1 An alphabet # is any finite set.

We often write # = {a1, . . . , ak}. The ai are called thesymbols of the alphabet.

Page 11: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

1.2. ALPHABETS, STRINGS, LANGUAGES 11

Examples :

# = {a}

# = {a, b, c}

# = {0, 1}

A string is a finite sequence of symbols. Technically, it isconvenient to define strings as functions. For any integern ! 1, let

[n] = {1, 2, . . . , n},

and for n = 0, let[0] = ".

Definition 1.2.2 Given an alphabet #, a string over# (or simply a string) of length n is any function

u: [n] # #.

The integer n is the length of the string u, and it isdenoted as |u|. When n = 0, the special string u: [0] # #of length 0 is called the empty string, or null string , andis denoted as !.

Page 12: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

12 CHAPTER 1. BASICS OF FORMAL LANGUAGE THEORY

Given a string u: [n] # # of length n ! 1, u(i) is thei-th letter in the string u. For simplicity of notation, wedenote the string u as

u = u1u2 . . . un,

with each ui $ #.

For example, if # = {a, b} and u: [3] # # is defined suchthat u(1) = a, u(2) = b, and u(3) = a, we write

u = aba.

Strings of length 1 are functions u: [1] # # simply pickingsome element u(1) = ai in #. Thus, we will identify everysymbol ai $ # with the corresponding string of length 1.

The set of all strings over an alphabet #, including theempty string, is denoted as #%.

Page 13: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

1.2. ALPHABETS, STRINGS, LANGUAGES 13

Observe that when # = ", then

"% = {!}.

When # &= ", the set #% is countably infinite. Later on,we will see ways of ordering and enumerating strings.

Strings can be juxtaposed, or concatenated.

Definition 1.2.3 Given an alphabet #, given any twostrings u: [m] # # and v: [n] # #, the concatenationu · v (also written uv) of u and v is the stringuv: [m + n] # #, defined such that

uv(i) =

!u(i) if 1 ' i ' m,v(i(m) if m + 1 ' i ' m + n.

In particular, u! = !u = u.

It is immediately verified that

u(vw) = (uv)w.

Thus, concatenation is a binary operation on #% which isassociative and has ! as an identity.

Page 14: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

14 CHAPTER 1. BASICS OF FORMAL LANGUAGE THEORY

Note that generally, uv &= vu, for example for u = a andv = b.

Given a string u $ #% and n ! 0, we define un as follows:

un =

!! if n = 0,un(1u if n ! 1.

Clearly, u1 = u, and it is an easy exercise to show that

unu = uun,

for all n ! 0.

Page 15: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

1.2. ALPHABETS, STRINGS, LANGUAGES 15

Definition 1.2.4 Given an alphabet #, given any twostrings u, v $ #% we define the following notions as fol-lows:

u is a prefix of v i" there is some y $ #% such that

v = uy.

u is a su!x of v i" there is some x $ #% such that

v = xu.

u is a substring of v i" there are some x, y $ #% suchthat

v = xuy.

We say that u is a proper prefix (su!x, substring) ofv i" u is a prefix (su!x, substring) of v and u &= v.

Page 16: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

16 CHAPTER 1. BASICS OF FORMAL LANGUAGE THEORY

Recall that a partial ordering ' on a set S is a binaryrelation ' ) S * S which is reflexive, transitive, andantisymmetric.

The concepts of prefix, su!x, and substring, define binaryrelations on #% in the obvious way. It can be shown thatthese relations are partial orderings.

Another important ordering on strings is the lexicographic(or dictionary) ordering.

Page 17: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

1.2. ALPHABETS, STRINGS, LANGUAGES 17

Definition 1.2.5 Given an alphabet # = {a1, . . . , ak}assumed totally ordered such that a1 < a2 < · · · < ak,given any two strings u, v $ #%, we define the lexico-graphic ordering + as follows:

u + v

"#

$

if v = uy, for some y $ #%, orif u = xaiy, v = xajz,and ai < aj, for some x, y, z $ #%.

It is fairly tedious to prove that the lexicographic orderingis in fact a partial ordering. In fact, it is a total ordering ,which means that for any two strings u, v $ #%, eitheru + v, or v + u.

The reversal wR of a string w is defined inductively asfollows:

!R = !,

(ua)R = auR,

where a $ # and u $ #%.

Page 18: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

18 CHAPTER 1. BASICS OF FORMAL LANGUAGE THEORY

It can be shown that

(uv)R = vRuR.

Thus,(u1 . . . un)

R = uRn . . . uR1 ,

and when ui $ #, we have

(u1 . . . un)R = un . . . u1.

We can now define languages.

Definition 1.2.6 Given an alphabet#, a language over# (or simply a language) is any subset L of #%.

If # &= ", there are uncountably many languages. Wewill try to single out countable “tractable” families oflanguages. We will begin with the family of regular lan-guages , and then proceed to the context-free languages .

We now turn to operations on languages.

Page 19: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

1.3. OPERATIONS ON LANGUAGES 19

1.3 Operations on Languages

A way of building more complex languages from simplerones is to combine them using various operations. First,we review the set-theoretic operations of union, intersec-tion, and complementation.

Given some alphabet #, for any two languages L1, L2 over#, the union L1 , L2 of L1 and L2 is the language

L1 , L2 = {w $ #% | w $ L1 or w $ L2}.

The intersection L1 - L2 of L1 and L2 is the language

L1 - L2 = {w $ #% | w $ L1 and w $ L2}.

The di"erence L1 ( L2 of L1 and L2 is the language

L1 ( L2 = {w $ #% | w $ L1 and w /$ L2}.

The di"erence is also called the relative complement .

Page 20: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

20 CHAPTER 1. BASICS OF FORMAL LANGUAGE THEORY

A special case of the di"erence is obtained when L1 = #%,in which case we define the complement L of a languageL as

L = {w $ #% | w /$ L}.

The above operations do not use the structure of strings.The following operations use concatenation.

Definition 1.3.1 Given an alphabet #, for any two lan-guages L1, L2 over #, the concatenation L1L2 of L1 andL2 is the language

L1L2 = {w $ #% | .u $ L1, .v $ L2, w = uv}.

For any language L, we define Ln as follows:

L0 = {!},Ln+1 = LnL.

Page 21: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

1.3. OPERATIONS ON LANGUAGES 21

The following properties are easily verified:

L" = ","L = ",

L{!} = L,

{!}L = L,

(L1 , {!})L2 = L1L2 , L2,

L1(L2 , {!}) = L1L2 , L1,

LnL = LLn.

In general, L1L2 &= L2L1.

So far, the operations that we have introduced, exceptcomplementation (since L = #%(L is infinite if L is finiteand # is nonempty), preserve the finiteness of languages.This is not the case for the next two operations.

Page 22: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

22 CHAPTER 1. BASICS OF FORMAL LANGUAGE THEORY

Definition 1.3.2 Given an alphabet #, for any lan-guage L over #, the Kleene %-closure L% of L is thelanguage

L% =%

n!0

Ln.

The Kleene +-closure L+ of L is the language

L+ =%

n!1

Ln.

Thus, L% is the infinite union

L% = L0 , L1 , L2 , . . . , Ln , . . . ,

and L+ is the infinite union

L+ = L1 , L2 , . . . , Ln , . . . .

Since L1 = L, both L% and L+ contain L.

Page 23: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

1.3. OPERATIONS ON LANGUAGES 23

In fact,

L+ = {w $ #%, .n ! 1,

.u1 $ L · · ·.un $ L, w = u1 · · ·un},

and since L0 = {!},

L% = {!} , {w $ #%, .n ! 1,

.u1 $ L · · ·.un $ L, w = u1 · · ·un}.

Thus, the language L% always contains !, and we have

L% = L+ , {!}.

Page 24: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

24 CHAPTER 1. BASICS OF FORMAL LANGUAGE THEORY

However, if ! /$ L, then ! /$ L+. The following is easilyshown:

"% = {!},L+ = L%L,

L%% = L%,

L%L% = L%.

The Kleene closures have many other interesting proper-ties.

Homomorphisms are also very useful.

Given two alphabets #,$, a homomorphismh:#% # $% between #% and $% is a function h:#% # $%

such that

h(uv) = h(u)h(v)

for all u, v $ #%.

Page 25: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

1.3. OPERATIONS ON LANGUAGES 25

Letting u = v = !, we get

h(!) = h(!)h(!),

which implies that (why?)

h(!) = !.

If # = {a1, . . . , ak}, it is easily seen that h is completelydetermined by h(a1), . . . , h(ak) (why?)

Example: # = {a, b, c}, $ = {0, 1}, and

h(a) = 01, h(b) = 011, h(c) = 0111.

For example

h(abbc) = 010110110111.

Page 26: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

26 CHAPTER 1. BASICS OF FORMAL LANGUAGE THEORY

Given any language L1 ) #%, we define the image h(L1)of L1 as

h(L1) = {h(u) $ $% | u $ L1}.

Given any language L2 ) $%, we define theinverse image h(1(L2) of L2 as

h(1(L2) = {u $ #% | h(u) $ L2}.

We now turn to the first formalism for defining languages,Deterministic Finite Automata (DFA’s)

Page 27: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

Chapter 2

Regular Languages

2.1 Deterministic Finite Automata (DFA’s)

First we define what DFA’s are, and then we explain howthey are used to accept or reject strings. Roughly speak-ing, a DFA is a finite transition graph whose edges arelabeled with letters from an alphabet #.

The graph also satisfies certain properties that make itdeterministic. Basically, this means that given any stringw, starting from any node, there is a unique path in thegraph “parsing” the string w.

27

Page 28: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

28 CHAPTER 2. REGULAR LANGUAGES

Example 1. A DFA for the language

L1 = {ab}+ = {ab}%{ab},

i.e.,L1 = {ab, abab, ababab, . . . , (ab)n, . . .}.

Input alphabet: # = {a, b}.

State set Q1 = {0, 1, 2, 3}.

Start state: 0.

Set of accepting states: F1 = {2}.

Transition table (function) "1:

a b0 1 31 3 22 1 33 3 3

Note that state 3 is a trap state or dead state.

Page 29: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.1. DETERMINISTIC FINITE AUTOMATA (DFA’S) 29

Example 2. A DFA for the language

L2 = {ab}% = L1 , {!}

i.e.,

L2 = {!, ab, abab, ababab, . . . , (ab)n, . . .}.

Input alphabet: # = {a, b}.

State set Q2 = {0, 1, 2}.

Start state: 0.

Set of accepting states: F2 = {0}.

Transition table (function) "2:

a b0 1 21 2 02 2 2

State 2 is a trap state or dead state.

Page 30: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

30 CHAPTER 2. REGULAR LANGUAGES

Example 3. A DFA for the language

L3 = {a, b}%{abb}.

Note that L3 consists of all strings of a’s and b’s endingin abb.

Input alphabet: # = {a, b}.

State set Q3 = {0, 1, 2, 3}.

Start state: 0.

Set of accepting states: F3 = {3}.

Transition table (function) "3:

a b0 1 01 1 22 1 33 1 0

Is this a minimal DFA?

Page 31: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.1. DETERMINISTIC FINITE AUTOMATA (DFA’S) 31

0 1 2

3

a

b

b

aa

b

a, b

Figure 2.1: DFA for {ab}+

0 1

2

b

a

b

a

a, b

Figure 2.2: DFA for {ab}!

0 1 2 3a b

a

b

b a

b

a

Figure 2.3: DFA for {a, b}!{abb}

Page 32: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

32 CHAPTER 2. REGULAR LANGUAGES

Definition 2.1.1 A deterministic finite automaton (orDFA) is a quintuple D = (Q,#, ", q0, F ), where

• # is a finite input alphabet

• Q is a finite set of states ;

• F is a subset of Q of final (or accepting) states ;

• q0 $ Q is the start state (or initial state);

• " is the transition function , a function

":Q* # # Q.

Page 33: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.1. DETERMINISTIC FINITE AUTOMATA (DFA’S) 33

For any state p $ Q and any input a $ #, the stateq = "(p, a) is uniquely determined. Thus, it is possible todefine the state reached from a given state p $ Q on inputw $ #%, following the path specified by w. Technically,this is done by defining the extended transition function"%:Q* #% # Q.

Definition 2.1.2 Given a DFA D = (Q,#, ", q0, F ),the extended transition function "%:Q * #% # Q isdefined as follows:

"%(p, !) = p,

"%(p, ua) = "("%(p, u), a),

where a $ # and u $ #%.

It is immediate that "%(p, a) = "(p, a) for a $ #. Themeaning of "%(p, w) is that it is the state reached fromstate p following the path from p specified by w.

Page 34: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

34 CHAPTER 2. REGULAR LANGUAGES

It is also easy to show that

"%(p, uv) = "%("%(p, u), v).

We can now define how a DFA accepts or rejects a string.

Definition 2.1.3 Given a DFA D = (Q,#, ", q0, F ),the language L(D) accepted (or recognized) by D isthe language

L(D) = {w $ #% | "%(q0, w) $ F}.

Thus, a string w $ #% is accepted i" the path from q0 oninput w ends in a final state.

We now come to the first of several equivalent definitionsof the regular languages.

Page 35: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.1. DETERMINISTIC FINITE AUTOMATA (DFA’S) 35

Regular Languages, Version 1

A language L is a regular language if it is accepted bysome DFA.

Note that a regular language may be accepted by manydi"erent DFAs. Later on, we will investigate how to findminimal DFA’s. (For a given regular language, L, a min-imal DFA for L is a DFA with the smallest number ofstates among all DFA’s accepting L. A minimal DFAfor L must exist since every nonempty subset of naturalnumbers has a smallest element.)

In order to understand how complex the regular languagesare, we will investigate the closure properties of the reg-ular languages under union, intersection, complementa-tion, concatenation, and Kleene %.

It turns out that the family of regular languages is closedunder all these operations. For union, intersection, andcomplementation, we can use the cross-product construc-tion which preserves determinism.

Page 36: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

36 CHAPTER 2. REGULAR LANGUAGES

However, for concatenation and Kleene %, there does notappear to be any method involving DFA’s only. The wayto do it is to introduce nondeterministic finite automata(NFA’s).

2.2 The “Cross-product” Construction

Let # = {a1, . . . , am} be an alphabet.

Given any two DFA’s D1 = (Q1,#, "1, q0,1, F1) andD2 = (Q2,#, "2, q0,2, F2), there is a very useful construc-tion for showing that the union, the intersection, or therelative complement of regular languages, is a regular lan-guage.

Given any two languages L1, L2 over #, recall that

L1 , L2 = {w $ #% | w $ L1 or w $ L2},L1 - L2 = {w $ #% | w $ L1 and w $ L2},L1 ( L2 = {w $ #% | w $ L1 and w /$ L2}.

Page 37: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.2. THE “CROSS-PRODUCT” CONSTRUCTION 37

Let us first explain how to constuct a DFA accepting theintersection L1 -L2. Let D1 and D2 be DFA’s such thatL1 = L(D1) and L2 = L(D2). The idea is to construct aDFA simulating D1 and D2 in parallel. This can be doneby using states which are pairs (p1, p2) $ Q1*Q2. Thus,we define the DFA D as follows:

D = (Q1 *Q2,#, ", (q0,1, q0,2), F1 * F2),

where the transition function ": (Q1*Q2)*# # Q1*Q2

is defined as follows:

"((p1, p2), a) = ("1(p1, a), "2(p2, a)),

for all p1 $ Q1, p2 $ Q2, and a $ #.

Clearly, D is a DFA, since D1 and D2 are. Also, by thedefinition of ", we have

"%((p1, p2), w) = ("%1(p1, w), "%2(p2, w)),

for all p1 $ Q1, p2 $ Q2, and w $ #%.

Page 38: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

38 CHAPTER 2. REGULAR LANGUAGES

Now, we have w $ L(D1) - L(D2)

i" w $ L(D1) and w $ L(D2),

i" "%1(q0,1, w) $ F1 and "%2(q0,2, w) $ F2,

i" ("%1(q0,1, w), "%2(q0,2, w)) $ F1 * F2,

i" "%((q0,1, q0,2), w) $ F1 * F2,

i" w $ L(D).

Thus, L(D) = L(D1) - L(D2).

We can now modify D very easily to acceptL(D1),L(D2). We change the set of final states so thatit becomes (F1 *Q2) , (Q1 * F2).Indeed, w $ L(D1) , L(D2)

i" w $ L(D1) or w $ L(D2),

i" "%1(q0,1, w) $ F1 or "%2(q0,2, w) $ F2,

i" ("%1(q0,1, w), "%2(q0,2, w)) $ (F1 *Q2) , (Q1 * F2),

i" "%((q0,1, q0,2), w) $ (F1 *Q2) , (Q1 * F2),

i" w $ L(D).

Thus, L(D) = L(D1) , L(D2).

Page 39: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.2. THE “CROSS-PRODUCT” CONSTRUCTION 39

We can also modify D very easily to acceptL(D1)(L(D2). We change the set of final states so thatit becomes F1* (Q2(F2). Indeed, w $ L(D1)(L(D2)

i" w $ L(D1) and w /$ L(D2),

i" "%1(q0,1, w) $ F1 and "%2(q0,2, w) /$ F2,

i" ("%1(q0,1, w), "%2(q0,2, w)) $ F1 * (Q2 ( F2),

i" "%((q0,1, q0,2), w) $ F1 * (Q2 ( F2),

i" w $ L(D).

Thus, L(D) = L(D1)( L(D2).

In all cases, if D1 has n1 states and D2 has n2 states, theDFA D has n1n2 states.

Page 40: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

40 CHAPTER 2. REGULAR LANGUAGES

2.3 Morphisms, F -Maps, B-Maps and Homomorphismsof DFA’s

A map between DFA’s is a certain kind of graph ho-momorphism. The following Definition is adapted fromEilenberg.

Definition 2.3.1 Given any two DFA’sD1 = (Q1,#, "1, q0,1, F1) and D2 = (Q2,#, "2, q0,2, F2),a morphism h:D1 # D2 of DFA’s is a functionh:Q1 # Q2 satisfying the following conditions:

(1)

h("1(p, a)) = "2(h(p), a),

for all p $ Q1 and all a $ #;

(2) h(q0,1) = q0,2.

An F -map of DFA’s , for short, a map, is a morphismof DFA’s h:D1 # D2 that satisfies the condition

(3a) h(F1) ) F2.

A B-map of DFA’s is a morphism of DFA’sh:D1 # D2 that satisfies the condition

(3b) h(1(F2) ) F1.

Page 41: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.3. MORPHISMS, F -MAPS, B-MAPS AND HOMOMORPHISMS OF DFA’S 41

A proper homomorphism of DFA’s , for short, a homo-morphism , is an F -map of DFA’s that is also a B-mapof DFA’s.

Now, for any function f :X # Y and any two subsetsA ) X and B ) Y , recall that

f(A) = {f(a) $ Y | a $ A}f(1(B) = {x $ X | f(x) $ B}

andf(A) ) B i" A ) f(1(B).

Thus, (3a) & (3b) is equivalent to the condition

(3c) h(1(F2) = F1.

Note that the condition for being a proper homomor-phism of DFA’s is not equivalent to

h(F1) = F2.

Condition (3c) forces h(F1) = F2 - h(Q1), and further-more, for every p $ Q1, whenever h(p) $ F2, thenp $ F1.

The reader should check that if f :D1 # D2 andg:D2 # D3 are morphisms (resp. F -maps, resp.B-maps), then g / f :D1 # D3 is also a morphism (resp.an F -map, resp. a B-map).

Page 42: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

42 CHAPTER 2. REGULAR LANGUAGES

Remark: In previous versions of these notes, an F -mapwas called simply amap and aB-map was called an F(1-map. Over the years, the old terminology proved to beconfusing. We hope the new one is less confusing!

Note that an F -map or a B-map is a special case of theconcept of simulation of automata. A proper homomor-phism is a special case of a bisimulation . Bisimulationsplay an important role in real-time systems and in con-currency theory.

The main motivation behind these definitions is that whenthere is an F -map h:D1 # D2, somehow, D2 simulatesD1, and it turns out that L(D1) ) L(D2).

When there is a B-map h:D1 # D2, somehow, D1 sim-ulates D2, and it turns out that L(D2) ) L(D1).

When there is a proper homomorphism h:D1 # D2,somehow, D1 bisimulates D2, and it turns out thatL(D2) = L(D1).

Page 43: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.3. MORPHISMS, F -MAPS, B-MAPS AND HOMOMORPHISMS OF DFA’S 43

A DFA morphism, f :D1 # D2, is an isomorphism i"there is a DFA morphism, g:D2 # D1, so that

g / f = idD1 and f / g = idD2.

Similarly, an F -map (respectively, a B-map),f :D1 # D2, is an isomorphism i" there is an F -map(respectively, a B-map), g:D2 # D1, so that

g / f = idD1 and f / g = idD2.

The map g is unique and it is denoted f(1. The readershould prove that if a DFA F -map is an isomorphism,then it is also a proper homomorphism and if a DFAB-map is an isomorphism, then it is also a proper homo-morphism.

If h:D1 # D2 is a morphism of DFA’s, it is easily shownby induction on the length of w that

h("%1(p, w)) = "%2(h(p), w),

for all p $ Q1 and all w $ #%.

As a consequence, we have the following Lemma:

Page 44: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

44 CHAPTER 2. REGULAR LANGUAGES

Lemma 2.3.2 If h:D1 # D2 is an F -map of DFA’s,then L(D1) ) L(D2). If h:D1 # D2 is a B-map ofDFA’s, then L(D2) ) L(D1). Finally, if h:D1 # D2

is a proper homomorphism of DFA’s, thenL(D1) = L(D2).

A DFA is accessible, or trim, if every state is reachablefrom the start state.

A morphism (resp. F -map, B-map) h:D1 # D2 is sur-jective if h(Q1) = Q2.

It can be shown that if D1 is trim, then there is at mostone morphism h:D1 # D2 (resp. F -map, B-map). IfD2 is also trim and we have a morphism h:D1 # D2,then h is surjective.

It can also be shown that a minimal DFA DL for L ischaracterized by the property that there is unique surjec-tive proper homomorphism h:D # DL from any trimDFA D accepting L to DL.

Page 45: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.3. MORPHISMS, F -MAPS, B-MAPS AND HOMOMORPHISMS OF DFA’S 45

Another useful notion is the notion of a congruence on aDFA.

Definition 2.3.3 Given any DFAD = (Q,#, ", q0, F ), a congruence 0 on D is an equiva-lence relation 0 on Q satisfying the following conditions:For all p, q $ Q and all a $ #,

(1) If p 0 q, then "(p, a) 0 "(q, a).

(2) If p 0 q and p $ F , then q $ F .

It can be shown that a proper homomorphism of DFA’sh:D1 # D2 induces a congruence 0h on D1 defined asfollows:

p 0h q i" h(p) = h(q).

Given a congruence 0 on a DFA D, we can define thequotient DFA D/ 0, and there is a surjective properhomomorphism #:D # D/ 0.

We will come back to this point when we study minimalDFA’s.

Page 46: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

46 CHAPTER 2. REGULAR LANGUAGES

2.4 Nondeteterministic Finite Automata (NFA’s)

NFA’s are obtained from DFA’s by allowing multiple tran-sitions from a given state on a given input. This can bedone by defining "(p, a) as a subset of Q rather than asingle state. It will also be convenient to allow transitionson input !.

We let 2Q denote the set of all subsets of Q, including theempty set. The set 2Q is the power set of Q. We defineNFA’s as follows.

Page 47: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.4. NONDETETERMINISTIC FINITE AUTOMATA (NFA’S) 47

Example 4. A NFA for the language

L3 = {a, b}%{abb}.

Input alphabet: # = {a, b}.

State set Q4 = {0, 1, 2, 3}.

Start state: 0.

Set of accepting states: F4 = {3}.

Transition table "4:

a b0 {0, 1} {0}1 " {2}2 " {3}3 " "

0 1 2 3a b b

a, b

Figure 2.4: NFA for {a, b}!{abb}

Page 48: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

48 CHAPTER 2. REGULAR LANGUAGES

Example 5. Let # = {a1, . . . , an}, let

Lin = {w $ #% | w contains an odd number of ai’s},

and letLn = L1

n , L2n , · · · , Ln

n.

The language Ln consists of those strings in #% that con-tain an odd number of some letter ai $ #.

Equivalently #% (Ln consists of those strings in #% withan even number of every letter ai $ #.

It can be shown that that every DFA accepting Ln hasat least 2n states.

However, there is an NFA with 2n + 1 states acceptingLn (and even with 2n states!).

Page 49: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.4. NONDETETERMINISTIC FINITE AUTOMATA (NFA’S) 49

Definition 2.4.1 A nondeterministic finite automa-ton (or NFA) is a quintuple N = (Q,#, ", q0, F ), where

• # is a finite input alphabet

• Q is a finite set of states ;

• F is a subset of Q of final (or accepting) states ;

• q0 $ Q is the start state (or initial state);

• " is the transition function , a function

":Q* (# , {!}) # 2Q.

For any state p $ Q and any input a $ # , {!}, theset of states "(p, a) is uniquely determined. We writeq $ "(p, a).

Given an NFA N = (Q,#, ", q0, F ), we would like todefine the language accepted by N , and for this, we needto extend the transition function ":Q* (# , {!}) # 2Q

to a function"%:Q* #% # 2Q.

Page 50: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

50 CHAPTER 2. REGULAR LANGUAGES

The presence of !-transitions (i.e., when q $ "(p, !))causes technical problems, and to overcome these prob-lems, we introduce the notion of !-closure.

2.5 !-Closure

Definition 2.5.1 Given an NFA N = (Q,#, ", q0, F )(with !-transitions) for every state p $ Q, the !-closureof p is set !-closure(p) consisting of all states q such thatthere is a path from p to q whose spelling is !. This meansthat either q = p, or that all the edges on the path fromp to q have the label !.

We can compute !-closure(p) using a sequence of approx-imations as follows. Define the sequence of sets of states(!-cloi(p))i!0 as follows:

!-clo0(p) = {p},!-cloi+1(p) = !-cloi(p) ,

{q $ Q | .s $ !-cloi(p), q $ "(s, !)}.

Page 51: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.5. !-CLOSURE 51

Since !-cloi(p) ) !-cloi+1(p), !-cloi(p) ) Q, for all i ! 0,and Q is finite, there is a smallest i, say i0, such that

!-cloi0(p) = !-cloi0+1(p),

and it is immediately verified that

!-closure(p) = !-cloi0(p).

WhenN has no !-transitions, i.e., when "(p, !) = " for allp $ Q (which means that " can be viewed as a function":Q* # # 2Q), we have

!-closure(p) = {p}.

It should be noted that there are more e!cient ways ofcomputing !-closure(p), for example, using a stack (basi-cally, a kind of depth-first search).

We present such an algorithm below. It is assumed thatthe types NFA and stack are defined. If n is the numberof states of an NFA N , we let

eclotype = array[1..n] of boolean

Page 52: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

52 CHAPTER 2. REGULAR LANGUAGES

function eclosure[N :NFA, p: integer]: eclotype;beginvar eclo: eclotype, q, s: integer, st: stack;for each q $ setstates(N) doeclo[q] := false;

endforeclo[p] := true; st := empty;trans := deltatable(N);st := push(st, p);while st &= emptystack doq = pop(st);for each s $ trans(q, !) doif eclo[s] = false theneclo[s] := true; st := push(st, s)

endifendfor

endwhile;eclosure := eclo

end

This algorithm can be easily adapted to compute the setof states reachable from a given state p (in a DFA or anNFA).

Page 53: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.5. !-CLOSURE 53

Given a subset S of Q, we define !-closure(S) as

!-closure(S) =%

p$S

!-closure(p).

When N has no !-transitions, we have

!-closure(S) = S.

We are now ready to define the extension "%:Q*#% # 2Q

of the transition function ":Q* (# , {!}) # 2Q.

Page 54: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

54 CHAPTER 2. REGULAR LANGUAGES

2.6 Converting an NFA into a DFA

The intuition behind the definition of the extended transi-tion function is that "%(p, w) is the set of all states reach-able from p by a path whose spelling is w.

Definition 2.6.1 Given an NFA N = (Q,#, ", q0, F )(with !-transitions), the extended transition function"%:Q * #% # 2Q is defined as follows: for every p $ Q,every u $ #%, and every a $ #,

"%(p, !) = !-closure({p}),

"%(p, ua) = !-closure(%

s$"%(p,u)

"(s, a)).

The language L(N) accepted by an NFA N is the set

L(N) = {w $ #% | "%(q0, w) - F &= "}.

Page 55: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.6. CONVERTING AN NFA INTO A DFA 55

We can also extend "%:Q* #% # 2Q to a function

&": 2Q * #% # 2Q

defined as follows: for every subset S of Q, for everyw $ #%,

&"(S,w) =%

p$S

"%(p, w).

Let Q be the subset of 2Q consisting of those subsets Sof Q that are !-closed, i.e., such that S = !-closure(S).If we consider the restriction

$:Q* # # Q

of &": 2Q*#% # 2Q to Q and #, we observe that $ is thetransition function of a DFA. Indeed, this is the transitionfunction of a DFA accepting L(N). It is easy to show that$ is defined directly as follows (on subsets S in Q):

$(S, a) = !-closure(%

s$S

"(s, a)).

Page 56: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

56 CHAPTER 2. REGULAR LANGUAGES

Then, the DFA D is defined as follows:

D = (Q,#,$, !-closure({q0}),F),

where F = {S $ Q | S - F &= "}.

It is not di!cult to show that L(D) = L(N), that is, Dis a DFA accepting L(N). Thus, we have converted theNFA N into a DFA D (and gotten rid of !-transitions).

Since DFA’s are special NFA’s, the subset constructionshows that DFA’s and NFA’s accept the same family oflanguages, the regular languages, version 1 (althoughnot with the same complexity).

The states of the DFA D equivalent to N are !-closedsubsets of Q. For this reason, the above construction isoften called the “subset construction”. It is due to Rabinand Scott. Although theoretically fine, the method mayconstruct useless sets S that are not reachable from thestart state !-closure({q0}). A more economical construc-tion is given next.

Page 57: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.6. CONVERTING AN NFA INTO A DFA 57

An Algorithm to convert an NFA into a DFA:The “subset construction”

Given an input NFA N = (Q,#, ", q0, F ), a DFA D =(K,#,$, S0,F) is constructed. It is assumed that K isa linear array of sets of states S ) Q, and $ is a 2-dimensional array, where $[i, a] is the target state of thetransition from K[i] = S on input a, with S $ K, anda $ #.

S0 := !-closure({q0}); total := 1; K[1] := S0;

marked := 0;

while marked < total do;

marked := marked + 1; S := K[marked];

for each a $ # do

U :='

s$S "(s, a); T := !-closure(U );

if T /$ K then

total := total + 1; K[total] := T

endif;

$[marked, a] := T

endfor

endwhile;

F := {S $ K | S - F &= "}

Page 58: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

58 CHAPTER 2. REGULAR LANGUAGES

Let us illustrate the subset construction on the NFA ofExample 4.

A NFA for the language

L3 = {a, b}%{abb}.

Transition table "4:

a b0 {0, 1} {0}1 " {2}2 " {3}3 " "

Set of accepting states: F4 = {3}.

0 1 2 3a b b

a, b

Figure 2.5: NFA for {a, b}!{abb}

Page 59: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

2.6. CONVERTING AN NFA INTO A DFA 59

The pointer 1 corresponds to marked and the pointer# to total.

Initial transition table $.

1 names states a b# A {0}

Just after entering the while loop

names states a b1# A {0}

After the first round through the while loop.

names states a b1 A {0} B A# B {0, 1}

After just reentering the while loop.

names states a bA {0} B A

1# B {0, 1}

After the second round through the while loop.

names states a bA {0} B A

1 B {0, 1} B C# C {0, 2}

Page 60: CIS511 Introduction to the Theory of Computation Formal ...jean/gbooks/cis51108sl1.pdf · CIS511 Introduction to the Theory of Computation Formal Languages and Automata Models of

60 CHAPTER 2. REGULAR LANGUAGES

After the third round through the while loop.

names states a bA {0} B AB {0, 1} B C

! C {0, 2} B D" D {0, 3}

After the fourth round through the while loop.

names states a bA {0} B AB {0, 1} B CC {0, 2} B D

!" D {0, 3} B A

This is the DFA of Figure 2.3, except that in that exampleA,B,C,D are renamed 0, 1, 2, 3

0 1 2 3a b

a

b

b a

b

a

Figure 2.6: DFA for {a, b}!{abb}