Introduction to CS Theory

Post on 15-Jan-2016

34 views 0 download

description

Introduction to CS Theory. Lecture 5 – Nondeterministic Finite Automata Piotr Faliszewski pf@cs.rit.edu. Finite automata Formal definition of a formal automata δ * function, acceptance by a finite automaton Closure properties Union Intersection Completement - PowerPoint PPT Presentation

Transcript of Introduction to CS Theory

Introduction to CS Theory

Lecture 5 – Nondeterministic Finite AutomataPiotr Faliszewskipf@cs.rit.edu

Previous Class

Finite automata Formal definition of a

formal automata δ* function, acceptance

by a finite automaton Closure properties

Union Intersection Completement the “cartesian

product” construction A nonregular language

Quiz Statistics

Min: 3 / 20

Med: 14 / 20

Max: 26 / 20

Not bad, but lightly graded!

Next quiz: Wednesday! Regular languages Drawing diagrams for

finite automatons accepting particular languages

Reading and writing regular expressions

Etc.

Yet Another Regular Language

Finite automata diagrams are somewhat constrained A transition for each

state and each alphabet symbol

For each state q and symbol a at most one transition from q labeled a

Consequence In each transition we

have to make sure we make the right move

This can be challenging…

Example L = (11+110)*0

Can we somehow defer our decisions?

Nondeterministic Finite Automata

Nondeterministic finite automata (NFA) We allow states to not

have transitions for all alphabet symbols

We allow states to have multiple transitions labeled with the same symbol

Examples L’ = (11 + 110)*0 L’’ is a language of

strings that contain aaba as a substring

What does it mean for an NFA to accept a string? An NFA accepts a string

x if starting from the initial state we can choose the transitions for consecutive symbols of x so that we end up in an accepting state.

NFA – Formal Definition

Def. NFA M is a quintuple M = (Q, Σ, q0, A, δ)

where:Q – set of statesΣ – input alphabetq0 – initial state (q0 Q)

A – set of accepting states (A Q)

δ – transition function

δ: Q Σ 2Q

δ(q,a) = set of states that M can go to from state q upon seeing symbol a

δ*(q,x) = set of states that M can reach if it starts from state q and sees string x

Define δ*(q,x) formally.

Def. We say that an NFA M = (Q, Σ, q0, A, δ) accepts a string x Σ* iff δ*(q0, x) A ≠ .

L(M) = set of strings accepted by M

Using δ*

Example Compute δ*(q0,x) for:

x = ε x = 0 x = 00 x = 001 x = 0011

1q1 q2q0

0,10

0,1

Are NFAs More Powerful than FAs?

NFAs are no less powerful than FAs Let M1 = (Q1, Σ1, q1, A1, δ1) be an FA

There is an NFA M2 = (Q2, Σ2, q2, A2, δ2) such that L(M1) = L(M2)

Exercise Given an FA M1 above, provide the appropriate

NFA M2

This is almost immediate!

Are there algorithms for languages of NFA? Can we simulate an NFA on an FA? Can we simulate an NFA in polynomial time?

NFAs vs FAs

What is the difference between NFAs and FAs? An NFA can “be in several states” at the same time after

reading some string x An FA is always in one state

However… Each NFA only has a finite number of states Thus, an FA can keep track of those subsets!

Formal Proof (The Subset Construction)

Let M1 = (Q1, Σ1, q1, A1, δ1) be an NFA

We build an FA M2 = (Q2, Σ2, q2, A2, δ2) such that

Q2 = 2Q1

Σ2 = Σ1

q2 = {q1} A2 = {q Q1 | q A1 ≠ } For each q Q1 and each a Σ1,

δ2(q,a) = Up q δ1(p,a)

Now… we need to prove L(M1) = L(M2)

We will use structural induction But what statement to prove exactly? We can try to show that for each string x we have that

x L1 if and only if x L2 – but we would get stuck! We show: For each string x, it holds that

δ1*(q1,x) = δ2

*(q2,x)This implies what we need

a

a

aa

b

b

q1

q2

q3

NFAs

Are NFAs missing something?

Consider L’ = 01*

L’’ = 0*1

We can get NFAs for L’ and L’’ Can we get an NFA for L’L’’ = 01*0*1? An NFA for L’ L’’

NFA’s With ε-Transitions

NFA We allow multiple

transitions with the same label

We allow missing transitions…

NFA-ε We additionally allow

transitions labeled with ε We can follow an ε-

transition at any time, without consuming another symbol of input

Consider the NFA-ε below. Does it accept aba abab aaabbb

Exercise: Give NFA-ε’s for the following languages a*b*c*

0*(01)*0*

Can we get NFAs for them as well?

a

a ab

b

ε εa,b

NFA-ε – Formal Definition

Def. NFA-ε M is a quintuble M = (Q, Σ, q0, A, δ)where:Q – set of statesΣ – input alphabetq0 – initial state (q0 Q)A – set of accepting states (A

Q)δ – transition function

δ: Q (Σ {ε}) 2Q

We want:

δ*(q,x) = set of states that M can reach if it starts from state q and sees string x

But we need to handle ε-transitions first.

S – set of statesε(S) – the ε closure of S (i.e., all

states reachable from S via ε-transitions).

Define ε(S) properly.

Now define δ*(q,x) properly.

Def. We say that a finite automaton M = (Q, Σ, q0, A, δ) accepts a string x Σ* iff δ*(q0, x) A.L(M) = set of strings accepted by M

NFAs versus NFA-ε’s

NFA-ε’s are no weaker than NFAs Almost immediate! NFA “is” an NFA-ε with

no ε-transitions

Can we use NFA-ε’s instead of NFAs for free? How can we simulate

the ε-transitions.

Yes! Show how!

Conclusion: NFAs, NFA-ε, and FAs are all equvialent!

Example: Conversions

Conversion to an NFA Start state? Transitions?

Convert To NFA Then to FA

B DA

0 10

ε

0

C

ε

Kleene’s Theorem

Theorem A language L is regular if and only if there is finite

automaton M such that L = L(M)

Proof Two parts

For each regular expression r there is an FA M such that L(M) = L(r) (L(r) – the set of strings described by the regular expression)

For each FA M, L(M) is regular

Proof: Mostly on the board

Kleene’s Theorem: Part 1

Theorem For each regular expression r there is an FA M such that

L(M) = L(r) (L(r) – the set of strings described by the regular expression)

Proof Given: Regular expression r, r is either

ε, , aΣ, or r1r2 r1+r2

(r1)*

where r1 and r2 are two (smaller) regular expressions (structural induction)

In each case we can convert!

Example Convert r = (00+1)*(10)*

Kleene’s Theorem: Part 2

Theorem For each FA M, L(M) is regular

Proof Wow, this is nontrivial! Need some insight! We will focus on the construction

Correctness: Implicit and natural.

M = (Q, Σ, q0, δ, A) be an FA

L(p,q) = {x | δ*(p,x) = q }L(p,q,k) = {x | δ*(p,x) = q without ever going

throughstate with a number >

k}L(p,q,||Q||) = L(p,q)

How to use these definitions in an inductive proof? (Board!)