Top Banner
Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017
79

Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

Jun 30, 2018

Download

Documents

vuongthuan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

Introduction to the Theory of ComputationSlides for CIS262

Jean Gallier

September 27, 2017

Page 2: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2

Page 3: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

Chapter 1

Part I: Basics of Formal LanguageTheory

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: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

4 CHAPTER 1. PART I: 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 difficult 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: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

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 different computationsand to ignore failing computations as long as there is somesuccessful computation on input w.

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

Page 6: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

6 CHAPTER 1. PART I: BASICS OF FORMAL LANGUAGE THEORY

Sometimes, a nondeterministic version of an automatonturns out to be equivalent to the deterministic version(although, with different 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: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

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: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

8 CHAPTER 1. PART I: 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: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

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: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

10 CHAPTER 1. PART I: 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 ofstrings. In turn, a string is a finite sequence of lettersfrom some alphabet. These concepts are defined rigor-ously as follows.

Definition 1.1. An alphabet Σ is any finite set.

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

Page 11: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

1.2. ALPHABETS, STRINGS, LANGUAGES 11

Examples :

Σ = {a}Σ = {a, b, c}Σ = {0, 1}Σ = {α, β, γ, δ, ϵ,λ,ϕ,ψ,ω, µ, ν, ρ,σ, η, ξ, ζ}

A string is a finite sequence of symbols. Technically,it is convenient to define strings as functions. For anyinteger n ≥ 1, let

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

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

Definition 1.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 stringu : [0]→ Σ of length 0 is called the empty string, or nullstring , and is denoted as ϵ.

Page 12: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

12 CHAPTER 1. PART I: 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 definedsuch that u(1) = a, u(2) = b, and u(3) = a, we write

u = aba.

Other examples of strings are

work, fun, gabuzomeuh

Strings of length 1 are functions u : [1]→ Σ simply pick-ing some element u(1) = ai in Σ. Thus, we will identifyevery symbol ai ∈ Σ with the corresponding string oflength 1.

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

Page 13: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

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.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. Observe that

|uv| = |u| + |v|.

For example, if u = ga, and v = buzo, then

uv = gabuzo

Page 14: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

14 CHAPTER 1. PART I: BASICS OF FORMAL LANGUAGE THEORY

It is immediately verified that

u(vw) = (uv)w.

Thus, concatenation is a binary operation on Σ∗ which isassociative and has ϵ as an identity.

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

Given a string u ∈ Σ∗ and n ≥ 0, we define un recursivelyas follows:

u0 = ϵ

un+1 = unu (n ≥ 0).

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

unu = uun, for all n ≥ 0.

Page 15: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

1.2. ALPHABETS, STRINGS, LANGUAGES 15

For the induction step, we have

un+1u = (unu)u by definition of un+1

= (uun)u by the induction hypothesis

= u(unu) by associativity

= uun+1 by definition of un+1.

Definition 1.4. Given an alphabet Σ, given any twostrings u, v ∈ Σ∗ we define the following notions as fol-lows:

u is a prefix of v iff there is some y ∈ Σ∗ such that

v = uy.

u is a suffix of v iff there is some x ∈ Σ∗ such that

v = xu.

u is a substring of v iff there are some x, y ∈ Σ∗ suchthat

v = xuy.

We say that u is a proper prefix (suffix, substring) ofv iff u is a prefix (suffix, substring) of v and u ̸= v.

Page 16: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

16 CHAPTER 1. PART I: BASICS OF FORMAL LANGUAGE THEORY

For example, ga is a prefix of gabuzo,

zo is a suffix of gabuzo and

buz is a substring of gabuzo.

Recall that a partial ordering ≤ on a set S is a binaryrelation ≤ ⊆ S × S which is reflexive, transitive, andantisymmetric.

The concepts of prefix, suffix, 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: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

1.2. ALPHABETS, STRINGS, LANGUAGES 17

Definition 1.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

⎧⎨

(1) if v = uy, for some y ∈ Σ∗, or(2) if u = xaiy, v = xajz, ai < aj,with ai, aj ∈ Σ, and for some x, y, z ∈ Σ∗.

Note that cases (1) and (2) are mutually exclusive. Incase (1) u is a prefix of v. In case (2) v ̸≼ u and u ̸= v.

For example

ab ≼ b, gallhager ≼ gallier.

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 anytwo strings u, v ∈ Σ∗, either u ≼ v, or v ≼ u.

Page 18: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

18 CHAPTER 1. PART I: BASICS OF FORMAL LANGUAGE THEORY

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

ϵR = ϵ,

(ua)R = auR,

where a ∈ Σ and u ∈ Σ∗.

For examplereillag = gallierR.

It can be shown that

(uv)R = vRuR.

Thus,(u1 . . . un)

R = uRn . . . uR1 ,

and when ui ∈ Σ, we have

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

Page 19: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

1.2. ALPHABETS, STRINGS, LANGUAGES 19

We can now define languages.

Definition 1.6. Given an alphabet Σ, a language overΣ (or simply a language) is any subset L of Σ∗.

If Σ ̸= ∅, there are uncountably many languages.

A Quick Review of Finite, Infinite,Countable, and Uncountable Sets

For details and proofs, see Discrete Mathematics, byGallier.

Let N = {0, 1, 2, . . .} be the set of natural numbers.

Recall that a set X is finite if there is some naturalnumber n ∈ N and a bijection between X and the set[n] = {1, 2, . . . , n}. (When n = 0, X = ∅, the emptyset.)

The number n is uniquely determined. It is called thecardinality (or size) of X and is denoted by |X|.

Page 20: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

20 CHAPTER 1. PART I: BASICS OF FORMAL LANGUAGE THEORY

A set is infinite iff it is not finite.

Recall that any injection or surjection of a finite set toitself is in fact a bijection.

The above fails for infinite sets.

The pigeonhole principle asserts that there is no bijec-tion between a finite set X and any proper subset Yof X .

Consequence: If we think of X as a set of n pigeonsand if there are only m < n boxes (corresponding to theelements of Y ), then at least two of the pigeons mustshare the same box.

As a consequence of the pigeonhole principle, a set X isinfinite iff it is in bijection with a proper subset of itself.

For example, we have a bijection n ,→ 2n between N andthe set 2N of even natural numbers, a proper subset ofN, so N is infinite.

Page 21: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

1.2. ALPHABETS, STRINGS, LANGUAGES 21

A set X is countable (or denumerable) if there is aninjection from X into N.

If X is not the empty set, then X is countable iff there isa surjection from N onto X .

It can be shown that a set X is countable if either it isfinite or if it is in bijection with N.

We will see later that N × N is countable. As a conse-quence, the set Q of rational numbers is countable.

A set is uncountable if it is not countable.

For example, R (the set of real numbers) is uncountable.

Similarly

(0, 1) = {x ∈ R | 0 < x < 1}

is uncountable. However, there is a bijection between(0, 1) and R (find one!)

Page 22: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

22 CHAPTER 1. PART I: BASICS OF FORMAL LANGUAGE THEORY

The set 2N of all subsets of N is uncountable.

If Σ ̸= ∅, then the set Σ∗ of all strings over Σ is infiniteand countable.

Suppose |Σ| = k with Σ = {a1, . . . , ak}.

If k = 1 write a = a1, and then

{a}∗ = {ϵ, a, aa, aaa, . . . , an, . . .}.

We have the bijection n ,→ an from N to {a}∗.

Page 23: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

1.2. ALPHABETS, STRINGS, LANGUAGES 23

If k ≥ 2, then we can think of the string

u = ai1 · · · ainas a representation of the integer ν(u) in base k shiftedby (kn − 1)/(k − 1),

ν(u) = i1kn−1 + i2k

n−2 + · · · + in−1k + in

=kn − 1

k − 1+ (i1 − 1)kn−1 + · · · + (in−1 − 1)k + in − 1.

(with ν(ϵ) = 0).

We leave it as an exercise to show that ν : Σ∗ → N is abijection.

In fact, ν correspond to the enumeration of Σ∗ where uprecedes v if |u| < |v|, and u precedes v in the lexico-graphic ordering if |u| = |v|.

For example, if k = 2 and if we write Σ = {a, b}, thenthe enumeration begins with

ϵ, a, b, aa, ab, ba, bb.

Page 24: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

24 CHAPTER 1. PART I: BASICS OF FORMAL LANGUAGE THEORY

On the other hand, if Σ ̸= ∅, the set 2Σ∗ of all subsets ofΣ∗ (all languages) is uncountable.

Indeed, we can show that there is no surjection from N

onto 2Σ∗.

First, we show that there is no surjection from Σ∗ onto2Σ∗.

But, if Σ ̸= ∅, then Σ∗ is infinite and countable, thus inbijection with N, so there is no surjection from N onto2Σ∗either.

We use a diagonalization argument. This is an instanceof Cantor’s Theorem.

Page 25: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

1.2. ALPHABETS, STRINGS, LANGUAGES 25

Assume there is a surjection h : Σ∗ → 2Σ∗, and consider

the set

D = {u ∈ Σ∗ | u /∈ h(u)}.

By definition, for any u we have u ∈ D iff u /∈ h(u), soD can’t be in the range of h, contradicting the fact thath is surjective.

Therefore, if Σ ̸= ∅, then 2Σ∗is uncountable.

We will try to single out countable “tractable” families oflanguages.

We will begin with the family of regular languages , andthen proceed to the context-free languages .

We now turn to operations on languages.

Page 26: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

26 CHAPTER 1. PART I: BASICS OF FORMAL LANGUAGE THEORY

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 difference L1 − L2 of L1 and L2 is the language

L1 − L2 = {w ∈ Σ∗ | w ∈ L1 and w /∈ L2}.

The difference is also called the relative complement .

Page 27: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

1.3. OPERATIONS ON LANGUAGES 27

A special case of the difference 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.7. 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 (n ≥ 0).

Page 28: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

28 CHAPTER 1. PART I: BASICS OF FORMAL LANGUAGE THEORY

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 29: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

1.3. OPERATIONS ON LANGUAGES 29

Definition 1.8. Given an alphabet Σ, for any languageL over Σ, the Kleene ∗-closure L∗ of L is the language

L∗ =⋃

n≥0Ln.

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

L+ =⋃

n≥1Ln.

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 30: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

30 CHAPTER 1. PART I: BASICS OF FORMAL LANGUAGE THEORY

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 31: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

1.3. OPERATIONS ON LANGUAGES 31

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 functionh : Σ∗ → ∆∗ such that

h(uv) = h(u)h(v) for all u, v ∈ Σ∗.

Page 32: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

32 CHAPTER 1. PART I: BASICS OF FORMAL LANGUAGE THEORY

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 33: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

1.3. OPERATIONS ON LANGUAGES 33

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 34: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

34 CHAPTER 1. PART I: BASICS OF FORMAL LANGUAGE THEORY

Page 35: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

Chapter 2

DFA’s, NFA’s, Regular Languages

The family of regular languages is the simplest, yet inter-esting family of languages.

We give six definitions of the regular languages.

1. Using deterministic finite automata (DFAs).

2. Using nondeterministic finite automata (NFAs).

3. Using a closure definition involving, union, concate-nation, and Kleene ∗.

4. Using regular expressions .

5. Using right-invariant equivalence relations of finiteindex (the Myhill-Nerode characterization).

6. Using right-linear context-free grammars .

35

Page 36: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

36 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

We prove the equivalence of these definitions, often byproviding an algorithm for converting one formulationinto another.

We find that the introduction of NFA’s is motivated bythe conversion of regular expressions into DFA’s.

To finish this conversion, we also show that every NFA canbe converted into a DFA (using the subset construction).

So, although NFA’s often allow for more concise descrip-tions, they do not have more expressive power than DFA’s.

NFA’s operate according to the paradigm: guess a suc-cessful path, and check it in polynomial time.

This is the essence of an important class of hard problemsknown as NP , which will be investigated later.

We will also discuss methods for proving that certain lan-guages are not regular (Myhill-Nerode, pumping lemma).

We present algorithms to convert a DFA to an equivalentone with a minimal number of states.

Page 37: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.1. DETERMINISTIC FINITE AUTOMATA (DFA’S) 37

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.

Page 38: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

38 CHAPTER 2. DFA’S, NFA’S, 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}.

Page 39: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.1. DETERMINISTIC FINITE AUTOMATA (DFA’S) 39

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.

Here is a graph representation of the DFA specified bythe transition function shown above:

0 1 2

3

a

b

b

aa

b

a, b

Figure 2.1: DFA for {ab}+

Page 40: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

40 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

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}.

Page 41: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.1. DETERMINISTIC FINITE AUTOMATA (DFA’S) 41

Transition table (function) δ2:

a b0 1 21 2 02 2 2

State 2 is a trap state or dead state.

Here is a graph representation of the DFA specified bythe transition function shown above:

0 1

2

b

a

b

a

a, b

Figure 2.2: DFA for {ab}∗

Page 42: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

42 CHAPTER 2. DFA’S, NFA’S, 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

Page 43: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.1. DETERMINISTIC FINITE AUTOMATA (DFA’S) 43

Here is a graph representation of the DFA specified bythe transition function shown above:

0 1 2 3a b

a

b

b a

b

a

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

Is this a minimal DFA?

Page 44: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

44 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

Definition 2.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.

For any state p ∈ Q and any input a ∈ Σ, the stateq = δ(p, a) is uniquely determined.

Thus, it is possible to define the state reached from agiven state p ∈ Q on input w ∈ Σ∗, following the pathspecified by w.

Page 45: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.1. DETERMINISTIC FINITE AUTOMATA (DFA’S) 45

Technically, this is done by defining the extended transi-tion function δ∗ : Q× Σ∗ → Q.

Definition 2.2. Given a DFA D = (Q,Σ, δ, q0, F ), theextended transition function δ∗ : Q×Σ∗ → Q is definedas follows:

δ∗(p, ϵ) = p,

δ∗(p, ua) = δ(δ∗(p, u), a),

where a ∈ Σ and u ∈ Σ∗.

It is immediate that δ∗(p, a) = δ(p, a) for a ∈ Σ.

The meaning of δ∗(p, w) is that it is the state reachedfrom state p following the path from p specified by w.

Page 46: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

46 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

We can show (by induction on the length of v) that

δ∗(p, uv) = δ∗(δ∗(p, u), v) for all p ∈ Q and all u, v ∈ Σ∗

For the induction step, for u ∈ Σ∗, and all v = ya withy ∈ Σ∗ and a ∈ Σ,

δ∗(p, uya) = δ(δ∗(p, uy), a) by definition of δ∗

= δ(δ∗(δ∗(p, u), y), a) by induction

= δ∗(δ∗(p, u), ya) by definition of δ∗.

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

Definition 2.3. Given a DFA D = (Q,Σ, δ, q0, F ), thelanguage L(D) accepted (or recognized) by D is thelanguage

L(D) = {w ∈ Σ∗ | δ∗(q0, w) ∈ F}.

Thus, a string w ∈ Σ∗ is accepted iff the path from q0 oninput w ends in a final state.

Page 47: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.1. DETERMINISTIC FINITE AUTOMATA (DFA’S) 47

The definition of a DFA does not prevent the possibilitythat a DFA may have states that are not reachable fromthe start state q0, which means that there is no path fromq0 to such states.

For example, in the DFA D1 defined by the transitiontable below and the set of final states F = {1, 2, 3}, thestates in the set {0, 1} are reachable from the start state0, but the states in the set {2, 3, 4} are not (even thoughthere are transitions from 2, 3, 4 to 0, but they go in thewrong direction).

a b0 1 01 0 12 3 03 4 04 2 0

Since there is no path from the start state 0 to any of thestates in {2, 3, 4}, the states 2, 3, 4 are useless as far asacceptance of strings, so they should be deleted as wellas the transitions from them.

Page 48: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

48 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

Given a DFA D = (Q,Σ, δ, q0, F ), the above suggestsdefining the set Qr of reachable (or accessible) states as

Qr = {p ∈ Q | (∃u ∈ Σ∗)(p = δ∗(q0, u))}.

The set Qr consists of those states p ∈ Q such that thereis some path from q0 to p (along some string u).

Computing the set Qr is a reachability problem in adirected graph. There are various algorithms to solvethis problem, including breadth-first search or depth-firstsearch.

Once the set Qr has been computed, we can clean up theDFA D by deleting all redundant states in Q − Qr andall transitions from these states.

More precisely, we form the DFADr = (Qr,Σ, δr, q0, Qr ∩ F ), where δr : Qr × Σ→ Qr isthe restriction of δ : Q× Σ→ Q to Qr.

Page 49: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.1. DETERMINISTIC FINITE AUTOMATA (DFA’S) 49

If D1 is the DFA of the previous example, then the DFA(D1)r is obtained by deleting the states 2, 3, 4:

a b0 1 01 0 1

It can be shown that L(Dr) = L(D) (see the homeworkproblems).

A DFA D such that Q = Qr is said to be trim (or re-duced).

Observe that the DFA Dr is trim. A minimal DFA mustbe trim.

Computing Qr gives us a method to test whether a DFAD accepts a nonempty language. Indeed

L(D) ̸= ∅ iff Qr ∩ F ̸= ∅

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

Page 50: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

50 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

Regular Languages, Version 1

Definition 2.4. A language L is a regular language ifit is accepted by some DFA.

Note that a regular language may be accepted by manydifferent DFAs. Later on, we will investigate how to findminimal DFA’s.

For a given regular language L, a minimal DFA for Lis a DFA with the smallest number of states among allDFA’s accepting L. A minimal DFA for L must existsince every nonempty subset of natural numbers has asmallest 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 ∗.

Page 51: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.1. DETERMINISTIC FINITE AUTOMATA (DFA’S) 51

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.

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), which we do a little later.

Page 52: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

52 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

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 53: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.2. THE “CROSS-PRODUCT” CONSTRUCTION 53

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 a DFA simulating D1 and D2

in parallel. This can be done by using states which arepairs (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 ∈ Σ.

Page 54: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

54 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

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 ∈ Σ∗.

Now, we have w ∈ L(D1) ∩ L(D2)

iff w ∈ L(D1) and w ∈ L(D2),

iff δ∗1(q0,1, w) ∈ F1 and δ∗2(q0,2, w) ∈ F2,

iff (δ∗1(q0,1, w), δ∗2(q0,2, w)) ∈ F1 × F2,

iff δ∗((q0,1, q0,2), w) ∈ F1 × F2,

iff w ∈ L(D).

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

Page 55: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.2. THE “CROSS-PRODUCT” CONSTRUCTION 55

We can now modify D very easily to acceptL(D1) ∪ L(D2).

We change the set of final states so that it becomes(F1 ×Q2) ∪ (Q1 × F2).

Indeed, w ∈ L(D1) ∪ L(D2)

iff w ∈ L(D1) or w ∈ L(D2),

iff δ∗1(q0,1, w) ∈ F1 or δ∗2(q0,2, w) ∈ F2,

iff (δ∗1(q0,1, w), δ∗2(q0,2, w)) ∈ (F1 ×Q2) ∪ (Q1 × F2),

iff δ∗((q0,1, q0,2), w) ∈ (F1 ×Q2) ∪ (Q1 × F2),

iff w ∈ L(D).

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

Page 56: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

56 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

We can also modify D very easily to acceptL(D1)− L(D2).

We change the set of final states so that it becomesF1 × (Q2 − F2).

Indeed, w ∈ L(D1)− L(D2)

iff w ∈ L(D1) and w /∈ L(D2),

iff δ∗1(q0,1, w) ∈ F1 and δ∗2(q0,2, w) /∈ F2,

iff (δ∗1(q0,1, w), δ∗2(q0,2, w)) ∈ F1 × (Q2 − F2),

iff δ∗((q0,1, q0,2), w) ∈ F1 × (Q2 − F2),

iff 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 57: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.3. NONDETETERMINISTIC FINITE AUTOMATA (NFA’S) 57

2.3 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.

Page 58: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

58 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

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 59: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.3. NONDETETERMINISTIC FINITE AUTOMATA (NFA’S) 59

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 every DFA accepting Ln has at least2n states.

However, there is an NFA with 2n + 1 states acceptingLn.

We define NFA’s as follows.

Page 60: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

60 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

Definition 2.5. A nondeterministic finite automaton(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 .

Page 61: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.3. NONDETETERMINISTIC FINITE AUTOMATA (NFA’S) 61

However, given an NFAN , unlike the situation for DFA’s,given a state p ∈ Q and some input w ∈ Σ∗, in generalthere is no unique path from p on input w, but insteada tree of computation paths .

For example, given the NFA shown below,

0 1 2 3a b b

a, b

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

from state 0 on input w = ababb we obtain the followingtree of computation paths:

0

0

0

3

2

1

0

0

2

1

0a a

bb

a

b

b

a

b

b

Figure 2.6: A tree of computation paths on input ababb

Page 62: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

62 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

Observe that there are three kinds of computation paths:

1. A path on input w ending in a rejecting state (forexample, the lefmost path).

2. A path on some proper prefix of w, along which thecomputation gets stuck (for example, the rightmostpath).

3. A path on input w ending in an accepting state (suchas the path ending in state 3).

The acceptance criterion for NFA is very lenient : a stringw is accepted iff the tree of computation paths containssome accepting path (of type (3)).

Thus, all failed paths of type (1) and (2) are ignored.Furthermore, there is no charge for failed paths.

Page 63: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.3. NONDETETERMINISTIC FINITE AUTOMATA (NFA’S) 63

A string w is rejected iff all computation paths are failedpaths of type (1) or (2).

The “philosophy” of nondeterminism is that an NFA“guesses” an accepting path and then checks it in poly-nomial time by following this path. We are only chargedfor one accepting path (even if there are several acceptingpaths).

A way to capture this acceptance policy if to extend thetransition function δ : Q× (Σ∪ {ϵ})→ 2Q to a function

δ∗ : Q× Σ∗ → 2Q.

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

Page 64: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

64 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

2.4 ϵ-Closure

Definition 2.6. 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 ϵ (an ϵ-path).

This means that either q = p, or that all the edges on thepath from p 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 65: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.4. ϵ-CLOSURE 65

Since ϵ-cloi(p) ⊆ ϵ-cloi+1(p), ϵ-cloi(p) ⊆ Q, for all i ≥ 0,and Q is finite, it can be shown that there is a smallest i,say i0, such that

ϵ-cloi0(p) = ϵ-cloi0+1(p).

It suffices to show that there is some i ≥ 0 such thatϵ-cloi(p) = ϵ-cloi+1(p), because then there is a smallestsuch i (since every nonempty subset of N has a smallestelement).

Assume by contradiction that

ϵ-cloi(p) ⊂ ϵ-cloi+1(p) for all i ≥ 0.

Then, I claim that |ϵ-cloi(p)| ≥ i + 1 for all i ≥ 0.

This is true for i = 0 since ϵ-clo0(p) = {p}.

Page 66: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

66 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

Since ϵ-cloi(p) ⊂ ϵ-cloi+1(p), there is some q ∈ ϵ-cloi+1(p)that does not belong to ϵ-cloi(p), and since by induction|ϵ-cloi(p)| ≥ i + 1, we get

|ϵ-cloi+1(p)| ≥ |ϵ-cloi(p)| + 1 ≥ i + 1 + 1 = i + 2,

establishing the induction hypothesis.

If n = |Q|, then |ϵ-clon(p)| ≥ n + 1, a contradiction.

Therefore, there is indeed some i ≥ 0 such thatϵ-cloi(p) = ϵ-cloi+1(p), and for the least such i = i0, wehave i0 ≤ n− 1.

Page 67: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.4. ϵ-CLOSURE 67

It can also be shown that

ϵ-closure(p) = ϵ-cloi0(p),

by proving that

1. ϵ-cloi(p) ⊆ ϵ-closure(p), for all i ≥ 0.

2. ϵ-closure(p)i ⊆ ϵ-cloi0(p), for all i ≥ 0.

where ϵ-closure(p)i is the set of states reachable from pby an ϵ-path of length ≤ i.

Page 68: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

68 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

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 efficient 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 69: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.4. ϵ-CLOSURE 69

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 do

q = 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 70: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

70 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

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

ϵ-closure(S) =⋃

s∈S

ϵ-closure(s),

with

ϵ-closure(∅) = ∅.

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 71: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.5. CONVERTING AN NFA INTO A DFA 71

2.5 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.7. 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)

).

In the second equation, if δ∗(p, u) = ∅ then

δ∗(p, ua) = ∅.

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

L(N) = {w ∈ Σ∗ | δ∗(q0, w) ∩ F ̸= ∅}.

Page 72: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

72 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

Observe that the definition of L(N) conforms to the le-nient acceptance policy: a stringw is accepted iff δ∗(q0, w)contains some final state.

We can also extend δ∗ : Q× Σ∗ → 2Q to a function

δ̂ : 2Q × Σ∗ → 2Q

defined as follows: for every subset S of Q, for everyw ∈ Σ∗,

δ̂(S,w) =⋃

s∈Sδ∗(s, w),

withδ̂(∅, w) = ∅.

Let Q be the subset of 2Q consisting of those subsets Sof Q that are ϵ-closed, i.e., such that

S = ϵ-closure(S).

Page 73: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.5. CONVERTING AN NFA INTO A DFA 73

If we consider the restriction

∆ : Q× Σ→ Q

of δ̂ : 2Q × Σ∗ → 2Q to Q and Σ, we observe that ∆ isthe transition function of a DFA.

Indeed, this is the transition function of a DFA acceptingL(N). It is easy to show that ∆ is defined directly asfollows (on subsets S in Q):

∆(S, a) = ϵ-closure

(⋃

s∈Sδ(s, a)

),

with∆(∅, a) = ∅.

Then, the DFA D is defined as follows:

D = (Q,Σ,∆, ϵ-closure({q0}),F),

where F = {S ∈ Q | S ∩ F ̸= ∅}.

Page 74: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

74 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

It is not difficult to show that L(D) = L(N), that is, Dis a DFA accepting L(N). For this, we show that

∆∗(S,w) = δ̂(S,w).

Thus, we have converted the NFA N into a DFA D (andgotten 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.

This construction is due to Rabin and Scott.

Although theoretically fine, the method may constructuseless sets S that are not reachable from the start stateϵ-closure({q0}). A more economical construction is givennext.

Page 75: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.5. CONVERTING AN NFA INTO A DFA 75

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 index of the targetstate of the transition from K[i] = S on input a, withS ∈ K, and a ∈ Σ.

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] := index(T )

endfor

endwhile;

F := {S ∈ K | S ∩ F ̸= ∅}

Page 76: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

76 CHAPTER 2. DFA’S, NFA’S, 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.7: NFA for {a, b}∗{abb}

Page 77: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.5. CONVERTING AN NFA INTO A DFA 77

The pointer ⇒ corresponds to marked and the pointer→ to total.

Initial transition table ∆.

⇒ index states a b→ A {0}

Just after entering the while loop

index states a b⇒→ A {0}

After the first round through the while loop.

index states a b⇒ A {0} B A→ B {0, 1}

Page 78: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

78 CHAPTER 2. DFA’S, NFA’S, REGULAR LANGUAGES

After just reentering the while loop.

index states a bA {0} B A

⇒→ B {0, 1}After the second round through the while loop.

index states a bA {0} B A

⇒ B {0, 1} B C→ C {0, 2}

After the third round through the while loop.

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

⇒ C {0, 2} B D→ D {0, 3}

Page 79: Introduction to the Theory of Computation Slides for CIS262cis262/notes/cis262sl1.pdf · Introduction to the Theory of Computation Slides for CIS262 Jean Gallier September 27, 2017

2.5. CONVERTING AN NFA INTO A DFA 79

After the fourth round through the while loop.

index 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.8: DFA for {a, b}∗{abb}