Top Banner
9/10/2019 Sofya Raskhodnikova Intro to Theory of Computation LECTURE 4 Last time: Equivalence of NFAs and DFAs Closure properties of regular languages Today: Finish closure properties Equivalence of NFAs, DFAs and regular expressions Sofya Raskhodnikova; based on slides by Nick Hopper L4.1
23

CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

Aug 10, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

9/10/2019

Sofya Raskhodnikova

Intro to Theory of Computation

LECTURE 4Last time:

• Equivalence of NFAs and DFAs

• Closure properties of regular

languages

Today:

• Finish closure properties

Equivalence of NFAs, DFAs

and regular expressions

Sofya Raskhodnikova; based on slides by Nick Hopper L4.1

Page 2: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

Operations on languages

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

Union: A B = { w | w A or w B }

Intersection: A B = { w | w A and w B }

Complement: A = { w | w A }

Reverse: AR = { w1 …wk | wk …w1 A }

Concatenation: A B = { vw | v A and w B }

Star: A* = { w1 …wk | k ≥ 0 and each wi A }

L3.2

Page 3: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

Closure properties of the class of

regular languages

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

THEOREM. The class of regular languages

is closed under all 6 operations.

If A and B are regular, applying any of these

operation yields a regular language.

L3.3

Page 4: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

Star operation

9/10/2019

Sofya Raskhodnikova; based on slides by Nick Hopper

Star: A* = { w1 …wk | k ≥ 0 and each wi A }

Theorem. If A is regular, A* is also regular.

L(M)=Aε

ε

ε

L3.4

Page 5: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

The class of regular languages is

closed under

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

Union: A B = { w | w A or w B }

Intersection: A B = { w | w A and w B }

Complement: A = { w | w A }

Reverse: AR = { w1 …wk | wk …w1 A }

Regular operations

Union: A B = { w | w A or w B }

Concatenation: A B = { vw | v A and w B }

Star: A* = { w1 …wk | k ≥ 0 and each wi A }

Other operations

L3.5

Page 6: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

Regular expressions

9/12/2019 Sofya Raskhodnikova; based on slides by Nick Hopper L4.6

• In a regular expression, we can use

– Constants: ε , ∅ , a set Σ, members of Σ.

– Regular operations: *, , ∪

• Examples:

– 0*1*

– (0∪1)*

– 0*1*(ε ∪ 0)

• L(R) = the language regular expression R describes

= the set of all strings over the alphabet Σ={0,1}

= { w | w has a run of 0s followed by a run of 1s}

Page 7: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

* ∪

Precedence

(𝑹𝟏(𝑹𝟐∗ )) ∪ (𝑹𝟑𝑹𝟒)

EXAMPLE

𝑹𝟏𝑹𝟐∗ ∪ 𝑹𝟑𝑹𝟒 =

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper L4.7

Page 8: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

{ w | w has exactly one character 1, any # of 0s}

0*10*

1)

2)

3)

Regular expressions: examples

{ w | w has length ≥ 3 and its 3rd symbol is 0 }

(0 ∪ 1)(0 ∪ 1) 0 (0 ∪ 1)*

{ w | every odd position of w is a 1 }

(1(0 ∪ 1))* (ε ∪ 1)

9/12/2019 Sofya Raskhodnikova; based on slides by Nick Hopper L4.8

Page 9: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

Regular expressions to NFAs

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

Theorem. Every regular expression has an equivalent NFA.

Proof: Induction on the length of regular expression R.

Base case: length 1

Inductive step: follows from closure of the class of

regular languages under the regular operations.

L4.9

R =

R = ε

R =

Page 10: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

Exercise

What should the induction hypothesis be?

A. Suppose some regular expression of length 𝑘 can be

converted an NFA, for some 𝑘 ∈ ℕ.

B. Suppose each regular expression of length 𝑘 can be

converted an NFA, for some 𝑘 ∈ ℕ.

C. Suppose each regular expression of length at most k

can be converted an NFA, for some 𝑘 ∈ ℕ.

D. None of the above.

Page 11: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

Regular expression to NFA

L4.119/12/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

Transform (1(0 ∪ 1))* to an NFA

1ε 1,0

ε

Page 12: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

NFAs to regular expressions

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

Theorem. Every NFA has an equivalent regular expression.

Proof idea:

Transform NFA to a regular expression by removing states

and relabeling the arrows with regular expressions.

L4.12

0

1

001*0

Page 13: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

Exercise

What is the regular expression that generates all

strings that take this machine from 𝒒𝒔 to 𝒒𝒂?

A. 𝑎𝑏∗𝑐 ∪ 𝑑

B. 𝑎𝑏∗𝑐 ∩ 𝑑

C. 𝑎𝑏𝑐 ∪ 𝑑

D. 𝑎𝑏∗𝑐𝑑 ∗

E. None of the above.

q

𝒃

𝒄𝒂qs qa

𝒅

Page 14: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

Generalized NFAs

• Each transition is labeled with a regular

expression

• Unique and distinct start and accept states

• No transitions to the start state

• No transitions from the accept state

L4.149/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

Page 15: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

R(qs,q) = a*b

R(qa,q) = Ø

R(q,qs) = Ø

A GNFA accepts w if ∃q0,q1,…,qk, w1,…,wk:

wi is generated by R(qi-1,qi)

w = w1w2…wk

q0=qs, qk=qa

q

a ∪ b

aa*bqs qa

Generalized NFAs

Page 16: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

NFA to GNFA

L4.169/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

NFAε

ε

ε

ε

Add a new start state with no incoming arrows.

Make a unique accept state with no outgoing arrows.

Page 17: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

GNF to regular expression

L4.179/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

NFAε

ε

ε

While machine has more than 2 states:

Pick an internal state, rip it out and relabel

the arrows with regular expressions to

account for the missing state.

Page 18: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

q1

b

a

εq2

a,b

εa*b(a*b)(ab)*q0 q3

R(q0,q3) = (a*b)(ab)*

Page 19: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

q3

q2

b

a

b

q1

b

a

a

ε

ε

ε

bb

9/10/2019 Sofya Raskhodnikova; based on

slides by Nick Hopper

Page 20: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

q2

b

a

b

q1

a

a

ε

ε

ε

bb

a ba

b

9/10/2019 Sofya Raskhodnikova; based on

slides by Nick Hopper

Page 21: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

bb (a ba)b*a

a ba

q2

b

q1

a

ε

ε

bb

bb (a ba)b*

= R(q1,q1)

9/10/2019 Sofya Raskhodnikova; based on

slides by Nick Hopper

Page 22: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

bb (a ba)b*a

q1

ε

b (a ba)b*

= R(q1,q1)

9/10/2019 Sofya Raskhodnikova; based on

slides by Nick Hopper

= R(q1,qa)qs

qa

Page 23: CS332 Elements of Theory of Computation · Intro to Theory of Computation LECTURE 4 Last time: •Equivalence of NFAs and DFAs •Closure properties of regular languages Today: •Finish

DFA NFA

Regular

Language

Regular

Expression

definition

Conversion procedures

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper 4.27