Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a...

31

Transcript of Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a...

Page 1: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition
Page 2: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Finite Automata

Basic idea: a FA is a “machine” that changes states

while processing symbols, one at a time.

• Finite set of states: Q = {q0, q1, q3, ..., qk}

• Transition function: d: QS Q

• Initial state: q0 Q

• Final states: F Q

• Finite automaton is M=(Q, S, d, q0, F)

Ex: an FA that accepts all odd-length strings of zeros:

q0 q1

0

0

M=({q0,q1}, {0}, {((q0,0),q1), ((q1,0),q0)}, q0, {q1})

q0

qi qj

q1

qk

Page 3: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Finite AutomataFA operation: consume a string wS* one symbol at a time

while changing states

Acceptance: end up in a final state

Rejection: anything else (including hang-up / crash)

Ex: FA that accepts all strings of form abababab…= (ab)*

q1

a

b

M=({q0,q1}, {a,b}, {((q0,a),q1), ((q1,b),q0)}, q0, {q0})

But M “crashes” on input string “abba”!Solution: add dead-end state to fully specify M

M’=({q0,q1,q2}, {a,b}, {((q0,a),q1), ((q1,b),q0), ((q0,b),q2), ((q1,b),q2). ((q2,a),q2), ((q2,b),q2) }, q0, {q0})

q0

q2

b a

a,b

M

M’

Page 4: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Finite AutomataTransition function d extends from symbols to strings:

d:QS*Q d(q0,wx) = d(d(q0,w),x)

where d(qi,e) = qi

Language of M is L(M)={wS*| d(q0,w) F}

Definition: A language is regular iff it is accepted by some FA.

L is regular iff there is a FA M where L(M)=L

Equivalently: A language is regular iff it can be described by a regular expression (proof next time)

Page 5: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Give a Regular Expression for the following languages over Σ={a,b}

1. L={w| w does not contain the letter ‘a’}

• 𝑏∗

2. L={w| |w| is odd}

• 𝑎 + 𝑏 𝑎 + 𝑏 𝑎 + 𝑏∗

3. L={w| w has an odd number of a’s followed by an even number of b’s}

• 𝑎 𝑎𝑎 ∗ 𝑏𝑏 ∗

Page 6: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Give a FA for the following languages over Σ={a,b}

1. L={w| w does not contain the letter ‘a’}

2. L={w| |w| is odd}

3. L={w| w has an odd number of a’s then an even number of b’s}

E a’s

a

a

fail

b

O a’s

a,b

O b’sb

E b’sb

a

b

a

even odda,b

a,b

( even, odd , Σ, { even, a , odd , even, b , odd ,

( odd, a , even), ( odd, b , even)}, even, {odd})

b‘s

b

a‘sa

a,b( b′s, a′s , Σ, { b′s, a , a′s , b′s, b , b′s ,

( a′s, a , a′s), ( a′s, b , a′s)}, b′s, {b′s})

Page 7: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Closure properties of Regular Languages

• An operation “preserves regularity” if:

• Given all input languages are regular

• The output language must be regular

• The regular languages are closed under:

• Union:L1 + 𝐿2 or L1 ∪ 𝐿2• Concatenation: 𝐿1 ⋅ 𝐿2 or 𝐿1𝐿2• Kleene closure: 𝐿 ∗

• Reversal:

• 𝐿𝑅 = 𝑤 𝑤𝑅 ∈ 𝐿}

• Complement: 𝐿 or 𝐿𝑐

• Intersection: 𝐿1 ∩ 𝐿2• Subtraction: 𝐿1 − 𝐿2• Among many other things (see problem sets)

Page 8: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Problem: design a DFA that accepts all strings over {a,b} where any a’s precede any b’s.

Idea: skip over any contiguous a’s, then skip over any b’s, and then accept iff the end is reached.

q0

a

q1b

b

q2a

a,b

L = a*b*

Q: What is the complement of L?

Page 9: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Problem: what is the complement of L = a*b* ?

Idea: write a regular expression and then simplify.

L’= (a+b)*b+(a+b)*a+(a+b)*= (a+b)*b(a+b)*a(a+b)*= (a+b)*b+a(a+b)*= (a+b)*ba(a+b)*= a*b+a(a+b)*

q0

a

q1b

b

q2a

a,b

Page 10: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Theorem: Complement Preservers Regularity.

Proof:

Given a FSA for a language 𝐿 (call it 𝑀), construct

a FSA for the language 𝐿 (call it 𝑀′).

From the machine 𝑀 to build 𝑀′ convert every final state to a non-final state, every non-final state to a final state.

L={w| |w| is odd}→ L ={w| |w| is even}

even odda,b

a,b

𝑀 = ( even, odd , Σ, { even, a , odd , even, b , odd ,

( odd, a , even), ( odd, b , even)}, even, {odd})

even odda,b

a,b

𝑀′ = ( even, odd , Σ, { even, a , odd , even, b , odd ,

( odd, a , even), ( odd, b , even)}, even, {even})

𝑀 = 𝑄, Σ, 𝛿, 𝑞0, 𝐹 → 𝑀′ = (𝑄, Σ, 𝛿, 𝑞0, 𝐹)

Page 11: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Theorem: Union Preservers Regularity.

Proof: “Cross Product Construction”

Simulate both machines “in parallel”

Let each state in the new machine be a pair of states. Transitions occur as if each machine ran individually. Accept if ended in a final state for either machine.

To union regular languages 𝐿1 and 𝐿2:

(FSA 𝑀1 = 𝑄1, Σ, 𝛿1, 𝑞1, 𝐹1 , 𝑀2 = 𝑄2, Σ, 𝛿2, 𝑞2, 𝐹2 respectively) 𝑀1∪2 = 𝑄1 × 𝑄2, Σ, 𝛿1∪2, 𝑞1, 𝑞2 , 𝐹1∪2

Where:

𝛿1∪2 𝑞𝑖 , 𝑞𝑗 , 𝜎 = 𝛿1 𝑞𝑖 , 𝜎 , 𝛿2 𝑞𝑗 , 𝜎 ,

𝐹1∪2 = {(𝑞𝑖 , 𝑞𝑗)|𝑞𝑖 ∈ 𝐹1 ∨ 𝑞𝑗 ∈ 𝐹2}

even odda,b

a,bb‘s

b

a‘sa

a,b

E b

b

E a

a

a,b

O aa

b a,b

O b

{w| w does not contain the letter ‘a’} {w| |w| is odd}{w| |w| is odd or contains no ‘a’s}

Page 12: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Theorem: Intersection Preservers Regularity.

Proof:

𝐿1 ∩ 𝐿2 = 𝐿1 ∪ 𝐿2

To intersect regular languages 𝐿1 and 𝐿2:

(FSA 𝑀1 = 𝑄1, Σ, 𝛿1, 𝑞1, 𝐹1 , 𝑀2 = 𝑄2, Σ, 𝛿2, 𝑞2, 𝐹2 respectively) 𝑀1∪2 = 𝑄1 × 𝑄2, Σ, 𝛿1∪2, 𝑞1, 𝑞2 , 𝐹1∪2

Where:

𝛿1∪2 𝑞𝑖 , 𝑞𝑗 , 𝜎 = 𝛿1 𝑞𝑖 , 𝜎 , 𝛿2 𝑞𝑗 , 𝜎 ,

𝐹1∪2 = {(𝑞𝑖 , 𝑞𝑗)|𝑞𝑖 ∈ 𝐹1 ∧ 𝑞𝑗 ∈ 𝐹2}

Cross Product Construction!

Page 13: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Theorem: Subtraction Preservers Regularity.

Proof:𝐿1 − 𝐿2 = 𝐿1 ∩ 𝐿2

To subtract regular language 𝐿2 from 𝐿1:

(FSA 𝑀1 = 𝑄1, Σ, 𝛿1, 𝑞1, 𝐹1 , 𝑀2 = 𝑄2, Σ, 𝛿2, 𝑞2, 𝐹2 respectively) 𝑀1∪2 = 𝑄1 × 𝑄2, Σ, 𝛿1∪2, 𝑞1, 𝑞2 , 𝐹1∪2

Where:

𝛿1∪2 𝑞𝑖 , 𝑞𝑗 , 𝜎 = 𝛿1 𝑞𝑖 , 𝜎 , 𝛿2 𝑞𝑗 , 𝜎 ,

𝐹1∪2 = {(𝑞𝑖 , 𝑞𝑗)|𝑞𝑖 ∈ 𝐹1 ∧ 𝑞𝑗 ∉ 𝐹2}

Cross Product Construction!

Page 14: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Problem:

?

Page 15: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Finite AutomataNon-determinism: generalizes determinism, where

many “next moves” are allowed at each step:

Old d:QS Q

New d:2QS 2Q

Computation becomes a “tree”.

Acceptance: $ a path from root (start state) to some leaf (a final state)

Ex: non-deterministically accept all strings where the 7th symbol before the end is a “b”:

a,b Input: a b a b b a a a

b a,b a,b a,ba,ba,ba,b

Accept!

q2q0 q7q3 q4 q5 q6q1

Page 16: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Finite AutomataTheorem: Non-determinism in FAs doesn’t increase power.Proof: by simulation:

• Construct all super-states,one per each state subset.

• New super-transition functionjumps among super-states,simulating old transition function

• Initial super state are those containing old initial state.

• Final super states are those containing old final states.

• Resulting DFA accepts the samelanguage as original NFA, but can have exponentially more states.

Page 17: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Finite AutomataNote: Powerset construction generalizes the cross-product construction. More general constructions are possible.

EC: Let HALF(L)={v | $v,w S* ' |v|=|w| and vw eL}Show that HALF preserves regularity.

A two way FA can move its head backwards

on the input: d:QS Q{left,right}

EC: Show that two-way FA are not more powerful than ordinary one-way FA.

e-transitions:

Theorem: e-transitions don’t increase FA recognition power.Proof: Simulate e-transitions FA without using e-transitions.i.e., consider e-transitions to be a form of non-determinism.

qi qje qi qj

eOne super-state!

Page 18: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

The movie “Next” (2007)Based on the science fictionstory “The Golden Man” by Philip Dick

Premise: a man with the super power of non-determinism!

At any given moment his reality branches into multiple directions, and he can choose the branch that he prefers!

Transition function!

Page 19: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Top-10 Reasons to Study Non-determinism

1. Helps us understand the ubiquitousconcept of parallelism / concurrency;

2. Illuminates the structure of problems;

3. Can help save time & effort by solving

intractable problems more efficiently;

4. Enables vast, deep, and general studies of

“completeness” theories;

5. Helps explain why verifying proofs & solutions

seems to be easier than constructing them;

Page 20: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Why Study Non-determinism?

6. Gave rise to new and novel mathematical

approaches, proofs, and analyses;

7. Robustly decouples / abstracts complexity from

underlying computational models;

8. Gives disciplined techniques for identifying

“hardest” problems / languages;

9. Forged new unifications between

computer science, math & logic;

10. Non-determinism is interesting

fun, and cool!

Page 21: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Regular ExpressionsRegular expressions are defined recursively as follows:

{e}trivial language

{x} " xSsingleton language

Øempty set

q0

Inductively, if R and S are regular expressions, then so are:

q0 q0 q1x

(R+S)union

RSconcatenation

R*

Kleene closure

Examples: aa(a+b)*bb (a+b)*b(a+b)*a(a+b)*

Theorem: Any regular expression is accepted by some FA.

M2

M1

e

e M2e

M1 M

Compositions!

e

e

e

Page 22: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Regular ExpressionsA FA for a regular expressions can be built by composition:

Ex: all strings over S={a,b} where $ a “b” preceding an “a”(a+b)*b(a+b)*a(a+b)*

= (a+b)*ba(a+b)*

b a

b

a

e

ee

e

e

b

a

e

ee

Why?

e

e

e

e

e

ee b a

b

a

e

ee

e

e

b

a

e

eee

e

e

e

e

ee

b a

b

a

e

ee

e

e

b

a

e

eee

e

e

e

e

ee b a

b

a

e

ee

e

e

b

a

e

eee

e

e

e

e

ee

Remove previous start/final states

Page 23: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

FA MinimizationIdea: “Equivalent” states can be merged:

b aa,b a,b

b a

b

a

e

ee

e

e

b

a

e

eee

e

e

e

e

ee

b a

b

a

e

ee

e b

a

e

ee

e

e

e

e

ee

e

b aa,be e a,beee

e

b aa,b

ea,b

ee

Page 24: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

FA MinimizationTheorem [Hopcroft 1971]: the number N of states in a FA

can be minimized within time O(N log N).

Based on earlier work [Huffman 1954] & [Moore 1956].

Conjecture: Minimizing the number of states in a nondeterministic FA can not be done in polynomial time.

Theorem: Minimizing the number of states in a pushdown automaton (or TM) is undecidable.

Project idea: implement a finite automaton minimization tool.

Try to design it to run reasonably efficiently.

Consider also including:

• A regular-expression-to-FA transformer,

• A non-deterministic-to-deterministic FA converter.

Page 25: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

M

FAs and Regular ExpressionsTheorem: Any FA accepts a language denoted by some RE.

Proof: Use “generalized finite automata” where a transition can be a regular expression (not just a symbol), and:

Only 1 super start state and 1 (separate) super final state.

Each state has transitions to all other states (including itself),except the super start state, with no incoming transitions, and the super final state, which has no outgoing transitions.

Original FA M

M

e

e

ee

e e

e

e

ØØ

ØØ

Ø

Ø

Ø

ØØ e

Generalized FA (GFA) M’

M’

Page 26: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

FAs and Regular ExpressionsNow reduce the size of the GFA by one state at each step.A transformation step is as follows:

qi qj

q’R

S

T

P qi qjP

RS*T

qi qjP + RS*T

Such a transformation step is always possible, until the GFAhas only two states, the super-start and super-final states:

Label of last remaining transition isthe regular expression corresponding to the language of the original FA!

M’P

Corollary: FAs and REs denote the same class of languages.

Page 27: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Regular Expressions Identities• R+S = S+R

• R(ST) = (RS)T

• R(S+T) = RS+RT

• (R+S)T = RT+ST

• Ø* = e* = e

• R+Ø = Ø+R = R

• Re= eR = R

• (R*)* = R*

• (e + R)* = R*

• (R*S*)* = (R+S)*

R+e ≠ RRØ ≠ R

Page 28: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Decidable Finite Automata ProblemsDef: A problem is decidable if $an algorithm which can determine (in finite time) the correct answer for any instance.

Given a finite automata M1 and M2:

Q1: Is L(M1) = Ø ?Hint: graph reachability

Q2: Is L(M2) infinite ?Hint: cycle detection

Q3: Is L(M1) = L(M2) ?Hint: consider L1-L2 and L2-L1

M’

$?

M’

$?S*-{e}

Ø Ø

Page 29: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition
Page 30: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

Give a FA and a regular expression for the following language over the Sesame Street Alphabet:

L = {w | w satisfies “i before e except after c”}

This means we want any string where an i does not immediately succeed an e, unless we see a c, in which case an e may not succeed an i.

Accepted words: believe, fierce, receipt

Rejected words: seize, their, science

Page 31: Finite Automata - Computer Sciencenjb2b/theory/Theory_lecture7_web.pdf · Theorem: Any FA accepts a language denoted by some RE. Proof: Use generalized finite automata where a transition

L = {w | w satisfies “i before e except after c”}

Σ − {𝑐, 𝑒}

safe

fail

i

e

e

c

Σ

cci

c

c c

ei

e

( Σ − 𝑐, 𝑒 ∗ ⋅ 𝑒+ Σ − 𝑐, 𝑖, 𝑒 + 𝑐+ Σ − 𝑐, 𝑖 + 𝑐+𝑖 Σ − 𝑒, 𝑐 + 휀 )∗ ⋅ 𝑒+ + 𝑐+ + 𝑐+𝑖 + 휀