+ All Categories
Home > Documents > Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2...

Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2...

Date post: 24-Aug-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
159
Computational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1
Transcript
Page 1: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Computational Models - Lecture 2

Non-Deterministic Finite Automata (NFA)

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 2: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Computational Models - Lecture 2

Non-Deterministic Finite Automata (NFA)

Closure of Regular Languages Under⋃

, ◦, ∗

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 3: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Computational Models - Lecture 2

Non-Deterministic Finite Automata (NFA)

Closure of Regular Languages Under⋃

, ◦, ∗

Regular expressions

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 4: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Computational Models - Lecture 2

Non-Deterministic Finite Automata (NFA)

Closure of Regular Languages Under⋃

, ◦, ∗

Regular expressions

Equivalence with finite automata

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 5: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Computational Models - Lecture 2

Non-Deterministic Finite Automata (NFA)

Closure of Regular Languages Under⋃

, ◦, ∗

Regular expressions

Equivalence with finite automata

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 6: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Computational Models - Lecture 2

Non-Deterministic Finite Automata (NFA)

Closure of Regular Languages Under⋃

, ◦, ∗

Regular expressions

Equivalence with finite automata

Sipser’s book, 1.1-1.3

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1

Page 7: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

DFA Formal Definition (reminder)

A deterministic finite automaton (DFA) is a 5-tuple(Q,Σ, δ, q0, F ), where

Q is a finite set called the states,

Σ is a finite set called the alphabet,

δ : Q × Σ → Q is the transition function,

q0 ∈ Q is the start state, and

F ⊆ Q is the set of accept states.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.2

Page 8: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Languages and DFA (reminder)

Definition: Let L ( L ⊆ Σ∗ ) be the set of strings thatM accepts. L(M), the language of a DFA M , isdefined as L(M) = L.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.3

Page 9: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Languages and DFA (reminder)

Definition: Let L ( L ⊆ Σ∗ ) be the set of strings thatM accepts. L(M), the language of a DFA M , isdefined as L(M) = L.

Note that

M may accept many strings, but

M accepts only one language.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.3

Page 10: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Languages and DFA (reminder)

Definition: Let L ( L ⊆ Σ∗ ) be the set of strings thatM accepts. L(M), the language of a DFA M , isdefined as L(M) = L.

Note that

M may accept many strings, but

M accepts only one language.

A language is called regular if some deterministic finite

automaton accepts it.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.3

Page 11: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The Regular Operations (reminder)

Let A and B be languages.

The union operation:

A∪B = {x|x ∈ A or x ∈ B}The concatenation operation:

A◦B = {xy|x ∈ A and y ∈ B}The star operation:

A∗ = {x1x2 . . . xk|k ≥ 0 and each xi ∈ A}

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.4

Page 12: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Claim: Closure Under Union (reminder)

If A1 and A2 are regular languages, so is A1 ∪ A2.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.5

Page 13: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Claim: Closure Under Union (reminder)

If A1 and A2 are regular languages, so is A1 ∪ A2.

Approach to Proof:

some M1 accepts A1

some M2 accepts A2

construct M that accepts A1 ∪ A2.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.5

Page 14: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

What About Concatenation?

Thm: If L1, L2 are regular languages, so is L1 ◦ L2.

Example: L1= {good, bad} and L2 = {boy, girl}.

L1 ◦ L2 = {goodboy, goodgirl, badboy, badgirl}

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.6

Page 15: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

What About Concatenation?

Thm: If L1, L2 are regular languages, so is L1 ◦ L2.

Example: L1= {good, bad} and L2 = {boy, girl}.

L1 ◦ L2 = {goodboy, goodgirl, badboy, badgirl}This is much harder to prove.

Idea: Simulate M1 for a while, then switch to M2.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.6

Page 16: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

What About Concatenation?

Thm: If L1, L2 are regular languages, so is L1 ◦ L2.

Example: L1= {good, bad} and L2 = {boy, girl}.

L1 ◦ L2 = {goodboy, goodgirl, badboy, badgirl}This is much harder to prove.

Idea: Simulate M1 for a while, then switch to M2.

Problem: But when do you switch?

This leads us into non-determinism.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.6

Page 17: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Non-Deterministic Finite Automata

q4q1 q

2 q30

0,1 0,1

10,ε

an NFA may have more than one transitionlabeled with the same symbol,

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.7

Page 18: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Non-Deterministic Finite Automata

q4q1 q

2 q30

0,1 0,1

10,ε

an NFA may have more than one transitionlabeled with the same symbol,

an NFA may have no transitions labeled with acertain symbol, and

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.7

Page 19: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Non-Deterministic Finite Automata

q4q1 q

2 q30

0,1 0,1

10,ε

an NFA may have more than one transitionlabeled with the same symbol,

an NFA may have no transitions labeled with acertain symbol, and

an NFA may have transitions labeled with ε, theempty string.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.7

Page 20: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Non-Deterministic Finite Automata

q4q1 q

2 q30

0,1 0,1

10,ε

an NFA may have more than one transitionlabeled with the same symbol,

an NFA may have no transitions labeled with acertain symbol, and

an NFA may have transitions labeled with ε, theempty string.

Comment: Every DFA is also a non-deterministicfinite automata (NFA).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.7

Page 21: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Non-Deterministic Computation

q4q1 q

2 q30

0,1 0,1

10,ε

What happens when more than one transition ispossible?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.8

Page 22: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Non-Deterministic Computation

q4q1 q

2 q30

0,1 0,1

10,ε

What happens when more than one transition ispossible?

the machine “splits” into multiple copies

each branch follows one possibility

together, branches follow all possibilities.

If the input doesn’t appear, that branch “dies”.

Automaton accepts if some branch accepts.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.8

Page 23: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Non-Deterministic Computation

q4q1 q

2 q30

0,1 0,1

10,ε

What happens when more than one transition ispossible?

the machine “splits” into multiple copies

each branch follows one possibility

together, branches follow all possibilities.

If the input doesn’t appear, that branch “dies”.

Automaton accepts if some branch accepts.

What does an ε transition do?Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.8

Page 24: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Non-Deterministic Computation

q4q1 q

2 q30

0,1 0,1

10,ε

What happens on string 1001?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.9

Page 25: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The String 1001

q4q1 q

2 q30

0,1 0,1

10,ε

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.10

Page 26: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The String 1001

q4q1 q

2 q30

0,1 0,1

10,ε

q4

q1

q3

q1

q2q1

q2 q3q1 q3

q1 q4

1

0

0

1

symbol

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.10

Page 27: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Why Non-Determinism?

Theorem (to be proved soon): Deterministic andnon-deterministic finite automata accept exactly thesame set of languages.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.11

Page 28: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Why Non-Determinism?

Theorem (to be proved soon): Deterministic andnon-deterministic finite automata accept exactly thesame set of languages.

Q.: So why do we need them?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.11

Page 29: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Why Non-Determinism?

Theorem (to be proved soon): Deterministic andnon-deterministic finite automata accept exactly thesame set of languages.

Q.: So why do we need them?

A.: NFAs are usually easier to design than equivalentDFAs.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.11

Page 30: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Why Non-Determinism?

Theorem (to be proved soon): Deterministic andnon-deterministic finite automata accept exactly thesame set of languages.

Q.: So why do we need them?

A.: NFAs are usually easier to design than equivalentDFAs.

Example: Design a finite automaton that accepts allstrings with a 1 in their third-to-the-last position?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.11

Page 31: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Solving with DFA

1

q000

q111q001

q010

q011

q100

q101

q110

0

0 0

0

000

1 1

1

1 1 1

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.12

Page 32: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Solving with DFA

1

q000

q111q001

q010

q011

q100

q101

q110

0

0 0

0

000

1 1

1

1 1 1

(oops, there are two errors:

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.12

Page 33: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Solving with DFA

1

q000

q111q001

q010

q011

q100

q101

q110

0

0 0

0

000

1 1

1

1 1 1

(oops, there are two errors: q101 should be an acceptstate, and there should be a “0" labeled arrow fromq011 to q110, but overall it is OK.)

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.12

Page 34: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Solving with NFA

q4q1 q

2 q31

0,1

0,10,1

“Guesses” which symbol is third from the last,and

checks that indeed it is a 1.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.13

Page 35: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

NFA – Formal DefinitionTransition function δ is going to be different.

P(Q) is the powerset of Q.

Σε = Σ ∪ {ε}.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.14

Page 36: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

NFA – Formal DefinitionTransition function δ is going to be different.

P(Q) is the powerset of Q.

Σε = Σ ∪ {ε}.

A non-deterministic finite automaton is a 5-tuple(Q,Σ, δ, q0, F ), where

Q is a finite set called the states,

Σ is a finite set called the alphabet,

δ : Q × Σε → P(Q) is the transition function,

q0 ∈ Q is the start state, and

F ⊆ Q is the set of accept states.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.14

Page 37: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Example

q4q1 q

2 q30

0,1 0,1

10,ε

N1 = (Q,Σ, δ, q1, F ) where

Q = {q1, q2, q3, q4}, Σ = {0, 1},

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.15

Page 38: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Example

q4q1 q

2 q30

0,1 0,1

10,ε

N1 = (Q,Σ, δ, q1, F ) where

Q = {q1, q2, q3, q4}, Σ = {0, 1},

δ is

0 1 ε

q1 {q1, q2} {q1} ∅q2 {q3} ∅ {q3}q3 ∅ {q4} ∅q4 {q4} {q4} ∅

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.15

Page 39: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Example

q4q1 q

2 q30

0,1 0,1

10,ε

N1 = (Q,Σ, δ, q1, F ) where

Q = {q1, q2, q3, q4}, Σ = {0, 1},

δ is

0 1 ε

q1 {q1, q2} {q1} ∅q2 {q3} ∅ {q3}q3 ∅ {q4} ∅q4 {q4} {q4} ∅

q1 is the start state, and F = {q4}.Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.15

Page 40: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Formal Model of Computation

Let M = (Q,Σ, δ, q0, F ) be an NFA, and

w be a string over Σε that has the formy1y2 · · · ym where yi ∈ Σε.

u be the string over Σ obtained from w byomitting all occurances of ε.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.16

Page 41: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Formal Model of Computation

Let M = (Q,Σ, δ, q0, F ) be an NFA, and

w be a string over Σε that has the formy1y2 · · · ym where yi ∈ Σε.

u be the string over Σ obtained from w byomitting all occurances of ε.

Suppose there is a sequence of states (in Q),r0, . . . , rn, such that

r0 = q0

ri+1∈δ(ri, yi+1), 0 ≤ i < n

rn ∈ F

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.16

Page 42: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Formal Model of Computation

Let M = (Q,Σ, δ, q0, F ) be an NFA, and

w be a string over Σε that has the formy1y2 · · · ym where yi ∈ Σε.

u be the string over Σ obtained from w byomitting all occurances of ε.

Suppose there is a sequence of states (in Q),r0, . . . , rn, such that

r0 = q0

ri+1∈δ(ri, yi+1), 0 ≤ i < n

rn ∈ F

Then we say that M accepts u.Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.16

Page 43: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Equivalence of NFA’s and DFA’s

Given an an NFA, N , we construct a DFA, M ,that accepts the same language.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.17

Page 44: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Equivalence of NFA’s and DFA’s

Given an an NFA, N , we construct a DFA, M ,that accepts the same language.

To begin with, we make things easier by ignoringε transitions.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.17

Page 45: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Equivalence of NFA’s and DFA’s

Given an an NFA, N , we construct a DFA, M ,that accepts the same language.

To begin with, we make things easier by ignoringε transitions.

Make DFA simulate all possible NFA states.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.17

Page 46: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Equivalence of NFA’s and DFA’s

Given an an NFA, N , we construct a DFA, M ,that accepts the same language.

To begin with, we make things easier by ignoringε transitions.

Make DFA simulate all possible NFA states.

As consequence of the construction, if the NFAhas k states, the DFA has 2k states.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.17

Page 47: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Equivalence of NFA’s and DFA’s

Let N = (Q,Σ, δ, q0, F ) be the NFA accepting A.

Construct a DFA M = (Q′,Σ, δ′, q′0, F′).

Q′ = P(Q).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18

Page 48: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Equivalence of NFA’s and DFA’s

Let N = (Q,Σ, δ, q0, F ) be the NFA accepting A.

Construct a DFA M = (Q′,Σ, δ′, q′0, F′).

Q′ = P(Q).

For R ∈ Q′ and a ∈ Σ, let

δ′(R, a) = {q ∈ Q|q ∈ δ(r, a) for some r ∈ R}

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18

Page 49: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Equivalence of NFA’s and DFA’s

Let N = (Q,Σ, δ, q0, F ) be the NFA accepting A.

Construct a DFA M = (Q′,Σ, δ′, q′0, F′).

Q′ = P(Q).

For R ∈ Q′ and a ∈ Σ, let

δ′(R, a) = {q ∈ Q|q ∈ δ(r, a) for some r ∈ R}q′0 = {q0}

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18

Page 50: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Equivalence of NFA’s and DFA’s

Let N = (Q,Σ, δ, q0, F ) be the NFA accepting A.

Construct a DFA M = (Q′,Σ, δ′, q′0, F′).

Q′ = P(Q).

For R ∈ Q′ and a ∈ Σ, let

δ′(R, a) = {q ∈ Q|q ∈ δ(r, a) for some r ∈ R}q′0 = {q0}F ′ = {R ∈ Q′|R contains an accept state of N}

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18

Page 51: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Dealing with ε-Transitions

For any state R of M , define E(R) to be the collectionof states reachable from R by ε transitions only.

E(R) = {q ∈ Q|q can be reached from some r ∈ Rby 0 or more ε transitions}

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.19

Page 52: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Dealing with ε-Transitions

For any state R of M , define E(R) to be the collectionof states reachable from R by ε transitions only.

E(R) = {q ∈ Q|q can be reached from some r ∈ Rby 0 or more ε transitions}

Define transition function:

δ′(R, a) = {q ∈ Q| there is some r ∈ Rsuch that q ∈ E(δ(r, a))}

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.19

Page 53: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Dealing with ε-Transitions

For any state R of M , define E(R) to be the collectionof states reachable from R by ε transitions only.

E(R) = {q ∈ Q|q can be reached from some r ∈ Rby 0 or more ε transitions}

Define transition function:

δ′(R, a) = {q ∈ Q| there is some r ∈ Rsuch that q ∈ E(δ(r, a))}

Change start state to

q′0 = E({q0}) ♣

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.19

Page 54: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages, Revisited

By definition, a language is regular if it is accepted bysome DFA.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.20

Page 55: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages, Revisited

By definition, a language is regular if it is accepted bysome DFA.

Corollary: A language is regular if and only if it isaccepted by some NFA.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.20

Page 56: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages, Revisited

By definition, a language is regular if it is accepted bysome DFA.

Corollary: A language is regular if and only if it isaccepted by some NFA.

This is an alternative way of characterizing regularlanguages.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.20

Page 57: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages, Revisited

By definition, a language is regular if it is accepted bysome DFA.

Corollary: A language is regular if and only if it isaccepted by some NFA.

This is an alternative way of characterizing regularlanguages.

We will now use the equivalence to show that regu-

lar languages are closed under the regular operations

(union, concatenation, star).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.20

Page 58: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Closure Under Union (alternative proof)

N1

N2

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.21

Page 59: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages Closed Under Union

N1

N2

ε

ε

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.22

Page 60: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages Closed Under Union

Suppose

N1 = (Q1,Σ, δ1, q1, F1) accept L1, and

N2 = (Q2,Σ, δ2, q2, F2) accept L2.

Define N = (Q,Σ, δ, q0, F ):

Q = q0 ∪ Q1 ∪ Q2

Σ is the same, q0 is the start state

F = F1 ∪ F2

δ′(q, a) =

δ1(q, a) q ∈ Q1

δ2(q, a) q ∈ Q2

{q1, q2} q = q0 and a = ε

∅ q = q0 and a �= εSlides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.23

Page 61: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages Closed UnderConcatenation

N2

N1

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.24

Page 62: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular LanguagesClosed Under Concatenation

N2N1ε

ε

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.25

Page 63: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular LanguagesClosed Under ConcatenationSuppose

N1 = (Q1,Σ, δ1, q1, F1) accept L1, and

N2 = (Q2,Σ, δ2, q2, F2) accept L2.

Define N = (Q,Σ, δ, q1, F2):

Q = Q1 ∪ Q2

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.26

Page 64: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular LanguagesClosed Under ConcatenationSuppose

N1 = (Q1,Σ, δ1, q1, F1) accept L1, and

N2 = (Q2,Σ, δ2, q2, F2) accept L2.

Define N = (Q,Σ, δ, q1, F2):

Q = Q1 ∪ Q2

q1 is the start state of N

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.26

Page 65: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular LanguagesClosed Under ConcatenationSuppose

N1 = (Q1,Σ, δ1, q1, F1) accept L1, and

N2 = (Q2,Σ, δ2, q2, F2) accept L2.

Define N = (Q,Σ, δ, q1, F2):

Q = Q1 ∪ Q2

q1 is the start state of N

F2 is the set of accept states of N

δ′(q, a) =

δ1(q, a) q ∈ Q1 and q /∈ F1

δ1(q, a) q ∈ Q1 and a �= ε

δ1(q, a) ∪ {q2} q ∈ F1 and a = ε

δ2(q, a) q ∈ Q2Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.26

Page 66: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages Closed Under Star

N1

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.27

Page 67: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages Closed Under Star

N1

N1

ε

ε

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.27

Page 68: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages Closed Under Star

N1

N1

ε

ε

Oops - bad construction. How do we fix it?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.27

Page 69: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages Closed Under Star

N1 accepts R1. Wanna build NFA for R = (R1)∗

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.28

Page 70: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages Closed Under Star

N1 accepts R1. Wanna build NFA for R = (R1)∗

������������

������������������������ ��������

���������

��

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.28

Page 71: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages Closed Under Star

N1 accepts R1. Wanna build NFA for R = (R1)∗

������������

������������������������ ��������

���������

��

�� ������������� q0 �����������

������������������������ ��������

���������

��ε

���� ♣

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.28

Page 72: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages Closed Under Star

N1 accepts R1. Wanna build NFA for R = (R1)∗

������������

������������������������ ��������

���������

��

�� ������������� q0 �����������

������������������������ ��������

���������

��ε

���� ♣

Ahaa - a better construction!

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.28

Page 73: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages Closed Under Star

Suppose N1 = (Q1,Σ, δ1, q1, F1) accepts L1.Define N = (Q,Σ, δ, q0, F ):

Q = {q0} ∪ Q1

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.29

Page 74: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages Closed Under Star

Suppose N1 = (Q1,Σ, δ1, q1, F1) accepts L1.Define N = (Q,Σ, δ, q0, F ):

Q = {q0} ∪ Q1

q0 is the new start state.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.29

Page 75: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Languages Closed Under Star

Suppose N1 = (Q1,Σ, δ1, q1, F1) accepts L1.Define N = (Q,Σ, δ, q0, F ):

Q = {q0} ∪ Q1

q0 is the new start state.

F = {q0 ∪ F1}

δ′(q, a) =

δ1(q, a) q ∈ Q1 and q /∈ F1

δ1(q, a) q ∈ F1 and a �= ε

δ1(q, ε) ∪ {q1} q ∈ F1 and a = ε

{q1} q = q0 and a = ε

∅ q = q0 and a �= ε

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.29

Page 76: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Summary

Regular languages are closed under

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.30

Page 77: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Summary

Regular languages are closed underunion

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.30

Page 78: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Summary

Regular languages are closed underunionconcatenation

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.30

Page 79: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Summary

Regular languages are closed underunionconcatenationstar

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.30

Page 80: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Summary

Regular languages are closed underunionconcatenationstar

Non-deterministic finite automata

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.30

Page 81: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Summary

Regular languages are closed underunionconcatenationstar

Non-deterministic finite automataare equivalent to deterministic finite automata

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.30

Page 82: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Summary

Regular languages are closed underunionconcatenationstar

Non-deterministic finite automataare equivalent to deterministic finite automatabut much easier to use in some proofs andconstructions.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.30

Page 83: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expressions

A notation for building up languages by describingthem as expressions, e.g. (0 ∪ 1)0∗.

0 and 1 are shorthand for {0} and {1}

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.31

Page 84: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expressions

A notation for building up languages by describingthem as expressions, e.g. (0 ∪ 1)0∗.

0 and 1 are shorthand for {0} and {1}so (0 ∪ 1) = {0, 1}.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.31

Page 85: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expressions

A notation for building up languages by describingthem as expressions, e.g. (0 ∪ 1)0∗.

0 and 1 are shorthand for {0} and {1}so (0 ∪ 1) = {0, 1}.

0∗ is shorthand for {0}∗.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.31

Page 86: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expressions

A notation for building up languages by describingthem as expressions, e.g. (0 ∪ 1)0∗.

0 and 1 are shorthand for {0} and {1}so (0 ∪ 1) = {0, 1}.

0∗ is shorthand for {0}∗.concatenation, like multiplication, is implicit, so0∗10∗ is shorthand for the set of all strings overΣ = {0, 1} having exactly a single 1.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.31

Page 87: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expressions

A notation for building up languages by describingthem as expressions, e.g. (0 ∪ 1)0∗.

0 and 1 are shorthand for {0} and {1}so (0 ∪ 1) = {0, 1}.

0∗ is shorthand for {0}∗.concatenation, like multiplication, is implicit, so0∗10∗ is shorthand for the set of all strings overΣ = {0, 1} having exactly a single 1.

Q.: What does (0 ∪ 1)0∗ stand for?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.31

Page 88: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expressions

A notation for building up languages by describingthem as expressions, e.g. (0 ∪ 1)0∗.

0 and 1 are shorthand for {0} and {1}so (0 ∪ 1) = {0, 1}.

0∗ is shorthand for {0}∗.concatenation, like multiplication, is implicit, so0∗10∗ is shorthand for the set of all strings overΣ = {0, 1} having exactly a single 1.

Q.: What does (0 ∪ 1)0∗ stand for?Remark: Regular expressions are often used in texteditors or shell scripts.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.31

Page 89: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

More Examples

Let Σ be an alphabet.

The regular expression Σ is the language ofone-symbol strings.

Σ∗ is all strings.

Σ∗1 all strings ending in 1.

0Σ∗ ∪ Σ∗1 strings starting with 0 or ending in 1.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.32

Page 90: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

More Examples

Let Σ be an alphabet.

The regular expression Σ is the language ofone-symbol strings.

Σ∗ is all strings.

Σ∗1 all strings ending in 1.

0Σ∗ ∪ Σ∗1 strings starting with 0 or ending in 1.

Just like in arithmetic, operations have precedence:

star first

concatenation next

union last

parentheses used to change usual orderSlides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.32

Page 91: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expressions – Formal Definition

Syntax: R is a regular expression if R is of form

a for some a ∈ Σ

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.33

Page 92: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expressions – Formal Definition

Syntax: R is a regular expression if R is of form

a for some a ∈ Σ

ε

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.33

Page 93: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expressions – Formal Definition

Syntax: R is a regular expression if R is of form

a for some a ∈ Σ

ε

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.33

Page 94: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expressions – Formal Definition

Syntax: R is a regular expression if R is of form

a for some a ∈ Σ

ε

∅(R1 ∪ R2) for regular expressions R1 and R2

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.33

Page 95: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expressions – Formal Definition

Syntax: R is a regular expression if R is of form

a for some a ∈ Σ

ε

∅(R1 ∪ R2) for regular expressions R1 and R2

(R1 ◦ R2) for regular expressions R1 and R2

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.33

Page 96: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expressions – Formal Definition

Syntax: R is a regular expression if R is of form

a for some a ∈ Σ

ε

∅(R1 ∪ R2) for regular expressions R1 and R2

(R1 ◦ R2) for regular expressions R1 and R2

(R∗1) for regular expression R1

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.33

Page 97: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expressions – Formal Definition

Let L(R) be the language denoted by regularexpression R.

R L(R)

a {a}ε {ε}(R1 ∪ R2) L(R1) ∪ L(R2)

(R1 ◦ R2) L(R1) ◦ L(R2)

(R1)∗ L(R1)

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.34

Page 98: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expressions – Formal Definition

Let L(R) be the language denoted by regularexpression R.

R L(R)

a {a}ε {ε}(R1 ∪ R2) L(R1) ∪ L(R2)

(R1 ◦ R2) L(R1) ◦ L(R2)

(R1)∗ L(R1)

Q.: What’s the difference between ∅ and ε?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.34

Page 99: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expressions – Formal Definition

Let L(R) be the language denoted by regularexpression R.

R L(R)

a {a}ε {ε}(R1 ∪ R2) L(R1) ∪ L(R2)

(R1 ◦ R2) L(R1) ◦ L(R2)

(R1)∗ L(R1)

Q.: What’s the difference between ∅ and ε?

Q.: Isn’t this definition circular?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.34

Page 100: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Remarkable FactThm.: A language, L, is described by a regular

expression, R, if and only if L is regular.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.35

Page 101: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Remarkable FactThm.: A language, L, is described by a regular

expression, R, if and only if L is regular.

=⇒ construct an NFA accepting R.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.35

Page 102: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Remarkable FactThm.: A language, L, is described by a regular

expression, R, if and only if L is regular.

=⇒ construct an NFA accepting R.

⇐= Given a regular language, L, construct an

equivalent regular expression.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.35

Page 103: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Given R, Build NFA Accepting It (=⇒)

1. R = a , for some a ∈ Σa

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.36

Page 104: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Given R, Build NFA Accepting It (=⇒)

1. R = a , for some a ∈ Σa

2. R = ε

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.36

Page 105: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Given R, Build NFA Accepting It (=⇒)

1. R = a , for some a ∈ Σa

2. R = ε

3. R = ∅

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.36

Page 106: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Given R, Build NFA Accepting It (=⇒)

N1

N2

ε

εN2N1

ε

ε

R = (R1 ∪ R2) R = (R1 ◦ R2)

R = (R1)∗ �� ������������� q0 ε

������������

������������������������ ��������

���������

��ε

���� ♣

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.37

Page 107: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Approximately (?) Correct Examplea

a

b

b

ab ε

ab

ab ε

a

ε

ε

ab U a

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.38

Page 108: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expression from an NFA (⇐=)

We now define generalized non-deterministic finiteautomata (GNFA).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.39

Page 109: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expression from an NFA (⇐=)

We now define generalized non-deterministic finiteautomata (GNFA).An NFA:

Each transition labeled with a symbol or ε,

reads zero or one symbols,

takes matching transition, if any.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.39

Page 110: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expression from an NFA (⇐=)

We now define generalized non-deterministic finiteautomata (GNFA).An NFA:

Each transition labeled with a symbol or ε,

reads zero or one symbols,

takes matching transition, if any.

A GNFA:

Each transition labeled with a regular expression,

reads zero or more symbols,

takes transition whose regular expression matchesstring, if any.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.39

Page 111: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Regular Expression from an NFA (⇐=)

We now define generalized non-deterministic finiteautomata (GNFA).An NFA:

Each transition labeled with a symbol or ε,

reads zero or one symbols,

takes matching transition, if any.

A GNFA:

Each transition labeled with a regular expression,

reads zero or more symbols,

takes transition whose regular expression matchesstring, if any.

GNFAs are natural generalization of NFAs.Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.39

Page 112: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

GNFA Special Form

Start state has outgoing arrows to every otherstate, but no incoming arrows.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.40

Page 113: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

GNFA Special Form

Start state has outgoing arrows to every otherstate, but no incoming arrows.

Unique accept state has incoming arrows fromevery other state, but no outgoing arrows

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.40

Page 114: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

GNFA Special Form

Start state has outgoing arrows to every otherstate, but no incoming arrows.

Unique accept state has incoming arrows fromevery other state, but no outgoing arrows

Except for start and accept states, arrows goesfrom every state to every other state, includingitself.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.40

Page 115: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

GNFA Special Form

Start state has outgoing arrows to every otherstate, but no incoming arrows.

Unique accept state has incoming arrows fromevery other state, but no outgoing arrows

Except for start and accept states, arrows goesfrom every state to every other state, includingitself.

Easy to transform any GNFA into special form.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.40

Page 116: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

GNFA Special Form

Start state has outgoing arrows to every otherstate, but no incoming arrows.

Unique accept state has incoming arrows fromevery other state, but no outgoing arrows

Except for start and accept states, arrows goesfrom every state to every other state, includingitself.

Easy to transform any GNFA into special form.

Really? How? . . .

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.40

Page 117: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Converting DFA to Regular Expression(⇐=)

Strategy – sequence of equivalent transformations

given a k-state DFA

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.41

Page 118: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Converting DFA to Regular Expression(⇐=)

Strategy – sequence of equivalent transformations

given a k-state DFA

transform into (k + 2)-state GNFA

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.41

Page 119: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Converting DFA to Regular Expression(⇐=)

Strategy – sequence of equivalent transformations

given a k-state DFA

transform into (k + 2)-state GNFA

while GNFA has more than 2 states, transform itinto equivalent GNFA with one fewer state

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.41

Page 120: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Converting DFA to Regular Expression(⇐=)

Strategy – sequence of equivalent transformations

given a k-state DFA

transform into (k + 2)-state GNFA

while GNFA has more than 2 states, transform itinto equivalent GNFA with one fewer state

eventually reach 2-state GNFA (states are juststart and accept).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.41

Page 121: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Converting DFA to Regular Expression(⇐=)

Strategy – sequence of equivalent transformations

given a k-state DFA

transform into (k + 2)-state GNFA

while GNFA has more than 2 states, transform itinto equivalent GNFA with one fewer state

eventually reach 2-state GNFA (states are juststart and accept).

label on single transition is the desired regularexpression. ♠

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.41

Page 122: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Converting Strategy (⇐=)

3-stateDFA

5-stateGNFA

4-stateGNFA

3-stateGNFA

2-stateGNFA

regularexpression

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.42

Page 123: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Removing One State

We remove one state qr, and then repair the machineby altering regular expression of other transitions.

qi jq

qr

R1 3R

4R

qi jq

2R

2R*R1 3R U 4R

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.43

Page 124: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Formal Treatment – GNDA Definitionqs is start state.

qa is accept state.

R is collection of regular expressions over Σ.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.44

Page 125: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Formal Treatment – GNDA Definitionqs is start state.

qa is accept state.

R is collection of regular expressions over Σ.

The transition function is

δ : (Q − {qa}) × (Q − {qs}) → R

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.44

Page 126: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Formal Treatment – GNDA Definitionqs is start state.

qa is accept state.

R is collection of regular expressions over Σ.

The transition function is

δ : (Q − {qa}) × (Q − {qs}) → RIf δ(qi, qj) = R, then arrow from qi to qj has label R.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.44

Page 127: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Formal Treatment – GNDA Definitionqs is start state.

qa is accept state.

R is collection of regular expressions over Σ.

The transition function is

δ : (Q − {qa}) × (Q − {qs}) → RIf δ(qi, qj) = R, then arrow from qi to qj has label R.

Arrows connect every state to every other state except:

no arrow from qa

no arrow to qs

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.44

Page 128: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Formal DefinitionA generalized deterministic finite automaton (GDFA)is (Q,Σ, δ, qs, qa), where

Q is a finite set of states,

Σ is the alphabet,

δ : (Q − {qa}) × (Q − {qs}) → R is thetransition function.

qs ∈ Q is the start state, and

qa ∈ Q is the unique accept state.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.45

Page 129: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

A Formal Model of GNFA Computation

A GNFA accepts a string w ∈ Σ∗ if there exists aparsing of w, w = w1w2 · · ·wk, where each wi ∈ Σ∗,and there exists a sequence of states q0, . . . , qk suchthat

q0 = qs, the start state,

qk = qa, the accept state, and

for each i, wi ∈ L(Ri), where Ri = δ(qi−1, qi).

(namely wi is an element of the languagedescribed by the regular expression Ri.)

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.46

Page 130: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The CONVERT AlgorithmGiven GDFA G, convert it to equivalent GNFA G′.

let k be the number of states of G.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.47

Page 131: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The CONVERT AlgorithmGiven GDFA G, convert it to equivalent GNFA G′.

let k be the number of states of G.

If k = 2, return the regular expression labelingthe only arrow.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.47

Page 132: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The CONVERT AlgorithmGiven GDFA G, convert it to equivalent GNFA G′.

let k be the number of states of G.

If k = 2, return the regular expression labelingthe only arrow.

If k > 2, select any qr distinct from qs and qa.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.47

Page 133: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The CONVERT AlgorithmGiven GDFA G, convert it to equivalent GNFA G′.

let k be the number of states of G.

If k = 2, return the regular expression labelingthe only arrow.

If k > 2, select any qr distinct from qs and qa.

Let Q′ = Q − {qr} .

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.47

Page 134: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The CONVERT AlgorithmGiven GDFA G, convert it to equivalent GNFA G′.

let k be the number of states of G.

If k = 2, return the regular expression labelingthe only arrow.

If k > 2, select any qr distinct from qs and qa.

Let Q′ = Q − {qr} .

For any qi ∈ Q′ − {qa} and qj ∈ Q′ − {qs}, let

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.47

Page 135: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The CONVERT AlgorithmGiven GDFA G, convert it to equivalent GNFA G′.

let k be the number of states of G.

If k = 2, return the regular expression labelingthe only arrow.

If k > 2, select any qr distinct from qs and qa.

Let Q′ = Q − {qr} .

For any qi ∈ Q′ − {qa} and qj ∈ Q′ − {qs}, letR1 = δ(qi, qr), R2 = δ(qr, qr),

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.47

Page 136: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The CONVERT AlgorithmGiven GDFA G, convert it to equivalent GNFA G′.

let k be the number of states of G.

If k = 2, return the regular expression labelingthe only arrow.

If k > 2, select any qr distinct from qs and qa.

Let Q′ = Q − {qr} .

For any qi ∈ Q′ − {qa} and qj ∈ Q′ − {qs}, letR1 = δ(qi, qr), R2 = δ(qr, qr),

R3 = δ(qr, qj), and R4 = δ(qi, qj).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.47

Page 137: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The CONVERT AlgorithmGiven GDFA G, convert it to equivalent GNFA G′.

let k be the number of states of G.

If k = 2, return the regular expression labelingthe only arrow.

If k > 2, select any qr distinct from qs and qa.

Let Q′ = Q − {qr} .

For any qi ∈ Q′ − {qa} and qj ∈ Q′ − {qs}, letR1 = δ(qi, qr), R2 = δ(qr, qr),

R3 = δ(qr, qj), and R4 = δ(qi, qj).

Define δ′(qi, qj) = (R1)(R2)∗(R3) ∪ (R4).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.47

Page 138: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The CONVERT AlgorithmGiven GDFA G, convert it to equivalent GNFA G′.

let k be the number of states of G.

If k = 2, return the regular expression labelingthe only arrow.

If k > 2, select any qr distinct from qs and qa.

Let Q′ = Q − {qr} .

For any qi ∈ Q′ − {qa} and qj ∈ Q′ − {qs}, letR1 = δ(qi, qr), R2 = δ(qr, qr),

R3 = δ(qr, qj), and R4 = δ(qi, qj).

Define δ′(qi, qj) = (R1)(R2)∗(R3) ∪ (R4).

Denote the resulting k − 1 states GNFA by G′.Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.47

Page 139: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The CONVERT ProcedureWe define the recursive procedure CONVERT(·):Given GDFA G.

Let k be the number of states of G.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.48

Page 140: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The CONVERT ProcedureWe define the recursive procedure CONVERT(·):Given GDFA G.

Let k be the number of states of G.

If k = 2, return the regular expression labelingthe only arrow of G.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.48

Page 141: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The CONVERT ProcedureWe define the recursive procedure CONVERT(·):Given GDFA G.

Let k be the number of states of G.

If k = 2, return the regular expression labelingthe only arrow of G.

If k > 2, let G′ be the k − 1 states GNFAproduced by the algorithm.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.48

Page 142: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

The CONVERT ProcedureWe define the recursive procedure CONVERT(·):Given GDFA G.

Let k be the number of states of G.

If k = 2, return the regular expression labelingthe only arrow of G.

If k > 2, let G′ be the k − 1 states GNFAproduced by the algorithm.

Return CONVERT(G′).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.48

Page 143: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Correctness Proof of Construction

Theorem: G and CONVERT(G) accept the samelanguage.

Proof: By induction on number of states of G

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.49

Page 144: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Correctness Proof of Construction

Theorem: G and CONVERT(G) accept the samelanguage.

Proof: By induction on number of states of G

Basis: When there are only 2 states, there is a singlelabel, which characterizes the strings accepted by G.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.49

Page 145: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Correctness Proof of Construction

Theorem: G and CONVERT(G) accept the samelanguage.

Proof: By induction on number of states of G

Basis: When there are only 2 states, there is a singlelabel, which characterizes the strings accepted by G.

Induction Step: Assume claim for k − 1 states, provefor k.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.49

Page 146: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Correctness Proof of Construction

Theorem: G and CONVERT(G) accept the samelanguage.

Proof: By induction on number of states of G

Basis: When there are only 2 states, there is a singlelabel, which characterizes the strings accepted by G.

Induction Step: Assume claim for k − 1 states, provefor k.

Let G′ be the k − 1 states GNFA produced from G bythe algorithm.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.49

Page 147: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

G and G′ accept the same language

By the induction hypothesis, G′ and CONVERT(G′)accept the same language.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.50

Page 148: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

G and G′ accept the same language

By the induction hypothesis, G′ and CONVERT(G′)accept the same language.

On input G, the procedure returns CONVERT(G′).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.50

Page 149: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

G and G′ accept the same language

By the induction hypothesis, G′ and CONVERT(G′)accept the same language.

On input G, the procedure returns CONVERT(G′).

So to complete the proof, it suffices to show that Gand G′ accept the same language.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.50

Page 150: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

G and G′ accept the same language

By the induction hypothesis, G′ and CONVERT(G′)accept the same language.

On input G, the procedure returns CONVERT(G′).

So to complete the proof, it suffices to show that Gand G′ accept the same language.

Three steps:

1. If G accepts w, then so does G′.2. If G′ accepts w, then so does G.

3. Therefore G and G′ are equivalent.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.50

Page 151: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Step One

Claim: If G accepts w, then so does G′:If G accepts w, then there exists a “path of states”qs, q1, q2, . . . , qa traversed by G on w, leading tothe accept state qa.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.51

Page 152: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Step One

Claim: If G accepts w, then so does G′:If G accepts w, then there exists a “path of states”qs, q1, q2, . . . , qa traversed by G on w, leading tothe accept state qa.

If qr does not appear on path, then G′ accepts wbecause the the new regular expression on eachedge of G′ contains the old regular expression inthe “union part”.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.51

Page 153: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Step One

Claim: If G accepts w, then so does G′:If G accepts w, then there exists a “path of states”qs, q1, q2, . . . , qa traversed by G on w, leading tothe accept state qa.

If qr does not appear on path, then G′ accepts wbecause the the new regular expression on eachedge of G′ contains the old regular expression inthe “union part”.

If qr does appear, consider the regular expressioncorresponding to . . . qi, qr, . . . , qr, qj . . . .The new regular expression (Ri,r)(Rr,r)

∗(Rr,j)linking qi and qj encompasses any such string.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.51

Page 154: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Step One

Claim: If G accepts w, then so does G′:If G accepts w, then there exists a “path of states”qs, q1, q2, . . . , qa traversed by G on w, leading tothe accept state qa.

If qr does not appear on path, then G′ accepts wbecause the the new regular expression on eachedge of G′ contains the old regular expression inthe “union part”.

If qr does appear, consider the regular expressioncorresponding to . . . qi, qr, . . . , qr, qj . . . .The new regular expression (Ri,r)(Rr,r)

∗(Rr,j)linking qi and qj encompasses any such string.

Either way, the claim holds.Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.51

Page 155: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Steps Two and Three

Claim: If G′ accepts w, then so does G.

Proof: Each transition from qi to qj in G′ correspondsto a transition in G, either directly or through qr. Thusif G′ accepts w, then so does G.

This completes the proof of the claim thatL(G) = L(G′).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.52

Page 156: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Steps Two and Three

Claim: If G′ accepts w, then so does G.

Proof: Each transition from qi to qj in G′ correspondsto a transition in G, either directly or through qr. Thusif G′ accepts w, then so does G.

This completes the proof of the claim thatL(G) = L(G′).Combined with the induction hypothesis, thisshows that G and the regular expressionCONVERT(G) accept the same language.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.52

Page 157: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Steps Two and Three

Claim: If G′ accepts w, then so does G.

Proof: Each transition from qi to qj in G′ correspondsto a transition in G, either directly or through qr. Thusif G′ accepts w, then so does G.

This completes the proof of the claim thatL(G) = L(G′).Combined with the induction hypothesis, thisshows that G and the regular expressionCONVERT(G) accept the same language.

This, in turn, proves our remarkable claim:A language, L, is described by a regularexpression, R, if and only if L is regular. ♣

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.52

Page 158: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Conversion Example

We now constructed a simple, 2 state DFA thataccepts the language over {0, 1} of all stringswith an even number of 1s.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.53

Page 159: Computational Models - Lecture 2bchor/CM05/Compute2.pdfComputational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under, , ∗ Regular expressions

Conversion Example

We now constructed a simple, 2 state DFA thataccepts the language over {0, 1} of all stringswith an even number of 1s.

We followed the conversion through GNFAs totranslate this DFA (on the blackboard) into theregular expression (0 ∪ 10∗1)∗.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.53


Recommended