A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A...

105
DFA Formal Definition (reminder) A deterministic finite automaton (DFA) is a 5-tuple (Q, Σ, δ, q 0 ,F ), where Q is a finite set called the states, Σ is a finite set called the alphabet, δ : Q × Σ Q is the transition function, q 0 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.1

Transcript of A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A...

Page 1: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 2: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Back to M1

q1

0

q2 3q

11 0

0,1

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

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

the transition function δ is

0 1q1 q1 q2

q2 q3 q2

q3 q2 q2

q1 is the start state, and F= {q2}.

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

Page 3: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Back to M1

q1

0

q2 3q

11 0

0,1

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

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

the transition function δ is

0 1q1 q1 q2

q2 q3 q2

q3 q2 q2

q1 is the start state, and F= {q2}.

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

Page 4: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Back to M1

q1

0

q2 3q

11 0

0,1

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

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

the transition function δ is

0 1q1 q1 q2

q2 q3 q2

q3 q2 q2

q1 is the start state, and F= {q2}.

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

Page 5: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Another Example

q1

q2

a b

a

a a

a

b

b

b

b

r1

s

r2

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

Page 6: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

And Yet Another Example

q1

q2q0

1 2

2,RESET

1,RESET

2

0

00

1

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

Page 7: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

A Formal Model of Computation

Let M= (Q,Σ, δ, q0, F ) be a DFA, and

let w= w1w2 · · ·wn be a string over Σ.

We say that M accepts w if there is a sequence of statesr0, . . . , rn (ri ∈ Q) such that

r0 = q0

δ(ri, wi+1) = ri+1, 0 ≤ i < n

rn ∈ F

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

Page 8: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

A Formal Model of Computation

Let M= (Q,Σ, δ, q0, F ) be a DFA, and

let w= w1w2 · · ·wn be a string over Σ.

We say that M accepts w if there is a sequence of statesr0, . . . , rn (ri ∈ Q) such that

r0 = q0

δ(ri, wi+1) = ri+1, 0 ≤ i < n

rn ∈ F

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

Page 9: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

A Formal Model of Computation

Let M= (Q,Σ, δ, q0, F ) be a DFA, and

let w= w1w2 · · ·wn be a string over Σ.

We say that M accepts w if there is a sequence of statesr0, . . . , rn (ri ∈ Q) such that

r0 = q0

δ(ri, wi+1) = ri+1, 0 ≤ i < n

rn ∈ F

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

Page 10: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

The Regular Operations

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

Page 11: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

The Regular Operations – Examples

Let A= {good, bad} and B = {boy, girl}.

UnionA ∪B = {good, bad, boy, girl}

Concatenation

A ◦B = {goodboy, goodgirl, badboy, badgirl}

Star

A∗ = {ε, good, bad, goodgood, goodbad, badbad, badgood, . . .}

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

Page 12: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Claim: Closure Under Union

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.

Attempted Proof Idea:

first simulate M1, and

if M1 doesn’t accept, then simulate M2.

What’s wrong with this?

Fix: Simulate both machines simultaneously.

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

Page 13: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Claim: Closure Under Union

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.

Attempted Proof Idea:

first simulate M1, and

if M1 doesn’t accept, then simulate M2.

What’s wrong with this?

Fix: Simulate both machines simultaneously.

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

Page 14: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Claim: Closure Under Union

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.

Attempted Proof Idea:

first simulate M1, and

if M1 doesn’t accept, then simulate M2.

What’s wrong with this?

Fix: Simulate both machines simultaneously.

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

Page 15: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Claim: Closure Under Union

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.

Attempted Proof Idea:

first simulate M1, and

if M1 doesn’t accept, then simulate M2.

What’s wrong with this?

Fix: Simulate both machines simultaneously.

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

Page 16: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Claim: Closure Under Union

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.

Attempted Proof Idea:

first simulate M1, and

if M1 doesn’t accept, then simulate M2.

What’s wrong with this?

Fix: Simulate both machines simultaneously.

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

Page 17: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Closure Under Union: Correct Proof

Suppose M1 = (Q1,Σ, δ1, q1, F1) accepts L1,

and M2 = (Q2,Σ, δ2, q2, F2) accepts L2.

Define M as follows (M will accept L1 ∪ L2):

Q = Q1 ×Q2.

Σ is the same.

For each (r1, r2) ∈ Q and a ∈ Σ,δ((r1, r2), a) = (δ1(r1, a), δ2(r2, a))

q0 = (q1, q2)

F = {(r1, r2)|r1 ∈ F1 or r2 ∈ F2}. ♣

(hey, why not choose F = F1 × F2?)

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

Page 18: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Closure Under Union: Correct Proof

Suppose M1 = (Q1,Σ, δ1, q1, F1) accepts L1,

and M2 = (Q2,Σ, δ2, q2, F2) accepts L2.

Define M as follows (M will accept L1 ∪ L2):

Q = Q1 ×Q2.

Σ is the same.

For each (r1, r2) ∈ Q and a ∈ Σ,δ((r1, r2), a) = (δ1(r1, a), δ2(r2, a))

q0 = (q1, q2)

F = {(r1, r2)|r1 ∈ F1 or r2 ∈ F2}. ♣

(hey, why not choose F = F1 × F2?)

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

Page 19: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Closure Under Union: Correct Proof

Suppose M1 = (Q1,Σ, δ1, q1, F1) accepts L1,

and M2 = (Q2,Σ, δ2, q2, F2) accepts L2.

Define M as follows (M will accept L1 ∪ L2):

Q = Q1 ×Q2.

Σ is the same.

For each (r1, r2) ∈ Q and a ∈ Σ,δ((r1, r2), a) = (δ1(r1, a), δ2(r2, a))

q0 = (q1, q2)

F = {(r1, r2)|r1 ∈ F1 or r2 ∈ F2}. ♣

(hey, why not choose F = F1 × F2?)

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

Page 20: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Closure Under Union: Correct Proof

Suppose M1 = (Q1,Σ, δ1, q1, F1) accepts L1,

and M2 = (Q2,Σ, δ2, q2, F2) accepts L2.

Define M as follows (M will accept L1 ∪ L2):

Q = Q1 ×Q2.

Σ is the same.

For each (r1, r2) ∈ Q and a ∈ Σ,δ((r1, r2), a) = (δ1(r1, a), δ2(r2, a))

q0 = (q1, q2)

F = {(r1, r2)|r1 ∈ F1 or r2 ∈ F2}. ♣

(hey, why not choose F = F1 × F2?)

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

Page 21: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Closure Under Union: Correct Proof

Suppose M1 = (Q1,Σ, δ1, q1, F1) accepts L1,

and M2 = (Q2,Σ, δ2, q2, F2) accepts L2.

Define M as follows (M will accept L1 ∪ L2):

Q = Q1 ×Q2.

Σ is the same.

For each (r1, r2) ∈ Q and a ∈ Σ,δ((r1, r2), a) = (δ1(r1, a), δ2(r2, a))

q0 = (q1, q2)

F = {(r1, r2)|r1 ∈ F1 or r2 ∈ F2}. ♣

(hey, why not choose F = F1 × F2?)

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

Page 22: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Closure Under Union: Correct Proof

Suppose M1 = (Q1,Σ, δ1, q1, F1) accepts L1,

and M2 = (Q2,Σ, δ2, q2, F2) accepts L2.

Define M as follows (M will accept L1 ∪ L2):

Q = Q1 ×Q2.

Σ is the same.

For each (r1, r2) ∈ Q and a ∈ Σ,δ((r1, r2), a) = (δ1(r1, a), δ2(r2, a))

q0 = (q1, q2)

F = {(r1, r2)|r1 ∈ F1 or r2 ∈ F2}. ♣

(hey, why not choose F = F1 × F2?)

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

Page 23: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 24: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 25: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 26: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Non-Deterministic Finite Automata

q4q1 q

2 q30

0,1 0,1

10,ε

an NFA may have more than one transition labeled witha certain symbol,

an NFA may have no transitions labeled with a certainsymbol, and

transitions may be labeled with ε, the empty string.

Comment: Every DFA is also a non-deterministic finiteautomata (NFA).

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

Page 27: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Non-Deterministic Finite Automata

q4q1 q

2 q30

0,1 0,1

10,ε

an NFA may have more than one transition labeled witha certain symbol,

an NFA may have no transitions labeled with a certainsymbol, and

transitions may be labeled with ε, the empty string.

Comment: Every DFA is also a non-deterministic finiteautomata (NFA).

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

Page 28: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Non-Deterministic Finite Automata

q4q1 q

2 q30

0,1 0,1

10,ε

an NFA may have more than one transition labeled witha certain symbol,

an NFA may have no transitions labeled with a certainsymbol, and

transitions may be labeled with ε, the empty string.

Comment: Every DFA is also a non-deterministic finiteautomata (NFA).

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

Page 29: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Non-Deterministic Finite Automata

q4q1 q

2 q30

0,1 0,1

10,ε

an NFA may have more than one transition labeled witha certain symbol,

an NFA may have no transitions labeled with a certainsymbol, and

transitions may be labeled with ε, the empty string.

Comment: Every DFA is also a non-deterministic finiteautomata (NFA).

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

Page 30: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Non-Deterministic Computation

q4q1 q

2 q30

0,1 0,1

10,ε

What happens when more than one transition is possible?

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

Page 31: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Non-Deterministic Computation

q4q1 q

2 q30

0,1 0,1

10,ε

What happens when more than one transition is possible?

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

Page 32: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Non-Deterministic Computation

q4q1 q

2 q30

0,1 0,1

10,ε

What happens when more than one transition is possible?

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

Page 33: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 34: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 35: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 36: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Why Non-Determinism?

Theorem: Deterministic and non-deterministic finiteautomata accept exactly the same set of languages.

Q.: So why do we need them?

A.: NFAs are usually easier to design than equivalent DFAs.

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

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

Page 37: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Why Non-Determinism?

Theorem: Deterministic and non-deterministic finiteautomata accept exactly the same set of languages.

Q.: So why do we need them?

A.: NFAs are usually easier to design than equivalent DFAs.

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

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

Page 38: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Why Non-Determinism?

Theorem: Deterministic and non-deterministic finiteautomata accept exactly the same set of languages.

Q.: So why do we need them?

A.: NFAs are usually easier to design than equivalent DFAs.

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

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

Page 39: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Why Non-Determinism?

Theorem: Deterministic and non-deterministic finiteautomata accept exactly the same set of languages.

Q.: So why do we need them?

A.: NFAs are usually easier to design than equivalent DFAs.

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

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

Page 40: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

A Deterministic Automaton

1

q000

q111q001

q010

q011

q100

q101

q110

0

0 0

0

000

1 1

1

1 1 1

(there are a few errors, e.g. q101 should be an accept state,

but overall it is OK.)Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.16

Page 41: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

A Non-Deterministic Automaton

q4q1 q

2 q31

0,1

0,10,1

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

checks that it’s a 1.

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

Page 42: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

NFA – Formal Definition

Transition 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.18

Page 43: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

NFA – Formal Definition

Transition 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.18

Page 44: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 45: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 46: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 47: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Formal Model of Computation

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

w be a string over Σε that has the form y1y2 · · · ym whereyi ∈ Σε.

u be the string over Σ obtained from w by omitting alloccurances of ε.

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

r0 = q0

δ(ri, yi+1) ∈ ri+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.20

Page 48: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Formal Model of Computation

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

w be a string over Σε that has the form y1y2 · · · ym whereyi ∈ Σε.

u be the string over Σ obtained from w by omitting alloccurances of ε.

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

r0 = q0

δ(ri, yi+1) ∈ ri+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.20

Page 49: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Formal Model of Computation

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

w be a string over Σε that has the form y1y2 · · · ym whereyi ∈ Σε.

u be the string over Σ obtained from w by omitting alloccurances of ε.

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

r0 = q0

δ(ri, yi+1) ∈ ri+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.20

Page 50: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Equivalence of NFA’s and DFA’s

Given an an NFA, N then we construct a DFA, M , thataccepts 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 NFA has k

states, the DFA has 2k states.

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

Page 51: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Equivalence of NFA’s and DFA’s

Given an an NFA, N then we construct a DFA, M , thataccepts 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 NFA has k

states, the DFA has 2k states.

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

Page 52: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Equivalence of NFA’s and DFA’s

Given an an NFA, N then we construct a DFA, M , thataccepts 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 NFA has k

states, the DFA has 2k states.

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

Page 53: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Equivalence of NFA’s and DFA’s

Given an an NFA, N then we construct a DFA, M , thataccepts 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 NFA has k

states, the DFA has 2k states.

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

Page 54: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 55: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 56: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 57: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 58: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Dealing with ε-Transitions

For any state R of M , define E(R) to be the collection ofstates 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.23

Page 59: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Dealing with ε-Transitions

For any state R of M , define E(R) to be the collection ofstates 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.23

Page 60: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Dealing with ε-Transitions

For any state R of M , define E(R) to be the collection ofstates 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.23

Page 61: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Languages, Revisited

By definition, a language is regular if it is accepted by someDFA.

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

This is an alternative way of characterizing regularlanguages.

We will now use the equivalence to show that regular lan-

guages are closed under the regular operations (union, con-

catenation, star).

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

Page 62: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Languages, Revisited

By definition, a language is regular if it is accepted by someDFA.

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

This is an alternative way of characterizing regularlanguages.

We will now use the equivalence to show that regular lan-

guages are closed under the regular operations (union, con-

catenation, star).

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

Page 63: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Languages, Revisited

By definition, a language is regular if it is accepted by someDFA.

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

This is an alternative way of characterizing regularlanguages.

We will now use the equivalence to show that regular lan-

guages are closed under the regular operations (union, con-

catenation, star).

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

Page 64: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Languages, Revisited

By definition, a language is regular if it is accepted by someDFA.

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

This is an alternative way of characterizing regularlanguages.

We will now use the equivalence to show that regular lan-

guages are closed under the regular operations (union, con-

catenation, star).

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

Page 65: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Languages Closed Under Union

N1

N2

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

Page 66: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Languages Closed Under Union

N1

N2

ε

ε

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

Page 67: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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 6= εSlides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.27

Page 68: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Languages Closed UnderConcatenation

N2

N1

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

Page 69: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular LanguagesClosed Under Concatenation

N2N1ε

ε

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

Page 70: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular LanguagesClosed Under Concatenation

Suppose

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 /∈ F

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

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

δ2(q, a) q ∈ Q2

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

Page 71: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular LanguagesClosed Under Concatenation

Suppose

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 /∈ F

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

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

δ2(q, a) q ∈ Q2

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

Page 72: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular LanguagesClosed Under Concatenation

Suppose

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 /∈ F

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

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

δ2(q, a) q ∈ Q2

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

Page 73: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 74: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 75: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 76: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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 6= ε

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

{q1} q = q0 and a = ε

∅ q = q0 and a 6= ε

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

Page 77: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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 6= ε

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

{q1} q = q0 and a = ε

∅ q = q0 and a 6= ε

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

Page 78: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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 6= ε

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

{q1} q = q0 and a = ε

∅ q = q0 and a 6= ε

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

Page 79: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Summary

Regular languages are closed under

unionconcatenationstar

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

Page 80: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Summary

Regular languages are closed underunion

concatenationstar

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

Page 81: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Summary

Regular languages are closed underunionconcatenation

star

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

Page 82: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 83: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Summary

Regular languages are closed underunionconcatenationstar

Non-deterministic finite automata

are 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.33

Page 84: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Summary

Regular languages are closed underunionconcatenationstar

Non-deterministic finite automataare equivalent to deterministic finite automata

but much easier to use in some proofs andconstructions.

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

Page 85: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 86: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Expressions

A notation for building up languages by describing them asexpressions, 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, so 0∗10∗ isshorthand for the set of all strings over Σ = {0, 1} havingexactly a single 1.

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

Remark: Regular expressions are often used in text editors

or shell scripts.

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

Page 87: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Expressions

A notation for building up languages by describing them asexpressions, 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, so 0∗10∗ isshorthand for the set of all strings over Σ = {0, 1} havingexactly a single 1.

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

Remark: Regular expressions are often used in text editors

or shell scripts.

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

Page 88: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Expressions

A notation for building up languages by describing them asexpressions, 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, so 0∗10∗ isshorthand for the set of all strings over Σ = {0, 1} havingexactly a single 1.

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

Remark: Regular expressions are often used in text editors

or shell scripts.

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

Page 89: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Expressions

A notation for building up languages by describing them asexpressions, 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, so 0∗10∗ isshorthand for the set of all strings over Σ = {0, 1} havingexactly a single 1.

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

Remark: Regular expressions are often used in text editors

or shell scripts.

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

Page 90: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Expressions

A notation for building up languages by describing them asexpressions, 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, so 0∗10∗ isshorthand for the set of all strings over Σ = {0, 1} havingexactly a single 1.

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

Remark: Regular expressions are often used in text editors

or shell scripts.

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

Page 91: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Expressions

A notation for building up languages by describing them asexpressions, 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, so 0∗10∗ isshorthand for the set of all strings over Σ = {0, 1} havingexactly a single 1.

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

Remark: Regular expressions are often used in text editors

or shell scripts.

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

Page 92: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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 order

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

Page 93: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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 order

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

Page 94: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 95: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 96: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 97: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 98: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 99: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

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

Page 100: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Expressions – Formal Definition

Let L(R) be the language denoted by regular expression 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.37

Page 101: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Expressions – Formal Definition

Let L(R) be the language denoted by regular expression 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.37

Page 102: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Regular Expressions – Formal Definition

Let L(R) be the language denoted by regular expression 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.37

Page 103: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Remarkable Fact

Thm.: A language, L, is described by a regularexpression, 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.38

Page 104: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Remarkable Fact

Thm.: A language, L, is described by a regularexpression, 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.38

Page 105: A deterministic finite automaton (DFA) is a 5-tuplebchor/CM/Compute2-Printer.pdf · A deterministic finite automaton (DFA) is a 5-tuple (Q; ; ;q0;F), where Q is a finite set called

Remarkable Fact

Thm.: A language, L, is described by a regularexpression, 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.38