FSA Lecture 1 Finite State Machines. Creating a Automaton Given a language L over an alphabet ,...

Post on 21-Dec-2015

218 views 0 download

Transcript of FSA Lecture 1 Finite State Machines. Creating a Automaton Given a language L over an alphabet ,...

FSA Lecture 1

Finite State Machines

Creating a Automaton

Given a language L over an alphabet , design a deterministic finite automaton (DFA) M such that L(M) = L.

Example 1

L1 = { w | w is a string over {0, 1} that contains an even number of 0s and an odd number of 1s }

Method:Define nodes to represent when a) both an even number of 0s and 1s have been seen in the input b) both an odd number of 0s and 1s have been seen in the input c) an even number of 0s and an odd number of 1s have been seen in the input d) an even number of 1s and an odd number of 0s have been seen in the input

qoe qoo

qee

1

01

0 00

1

1

qeo

Example 1

Example 2

L2 = { w | w is a string over {0, 1} that does not contain an even number of 0s and an odd number of 1s } = L1

qeo

1

01

0 00

1

1

Example 2

qee

qoeqoo

Example 3

L3 = { w | w is a string over {0, 1} such that |w| 3}

= {, 0, 1, 00, 01, 10, 11, 000, 001, 010, 011, 100, 101, 110, 111}

Example 3

q0 q1q2

0, 1 0, 1q3

0, 1

0, 1

q4

0, 1

Example 4

L4 = { w | w is a string over {0, 1} such that w contains the substring 11} = { w | w = x11y, where x and y are strings over {0, 1}}

Example 4

q0 q1

0

1

0

1q2

0, 1

0 1q0 q0 q1q1 q0 q2q2 q2 q2

Machine M accepts string w

If there exists a sequence of states r0, r1, …, rn in Q such that 1) r0 = q0 2) (ri , wi+1) = ri+1, for i=0,…,n-1 3) rn in F

Note: w = w1w2…wn

Regular Languages

Machine M recognizes language A if A = {w| M accepts w}

A language is called regular if some finite automaton recognizes it.

Regular Operations

Let A and B be languages. Union

A B = { x | x in A or x in B}

ConcatenationA B = {xy | x in A and y in B}

StarA* = {x1x2…xk | k 0 and each xj in A}

Note: is always a member of A*.

Regular languages are closed under union

Let A1 and A2 be regular languages. We want to show A1A2 is a regular language. Since A1 and A2 are regular languages there exists a finite automaton M1 and there exists a finite automaton M2 such that M1 recognizes A1 and M2 recognizes A2.

Assume M1 = (Q1, , 1, q1, F1) and M2 = (Q2, , 2, q2, F2) It suffices to create a finite automaton M that recognizes A1A2.

Continue …

Let a be a symbol in and states r1 in Q1 and r2 in Q2. Define M = (Q, , , q0, F) where

Q = Q1 x Q2 states

((r1, r2), a) = (1(r1, a), 2(r2, a)) transition function

q0 = (q1, q2) start state

F = (F1 x Q2) (Q1 x F2) final states

Regular languages are closed under concatenation

Let A1 and A2 be regular languages. We want to show

A1 A2 is a regular language. Since A1 and A2 are regular languages there exists a finite automaton M1 and there exists a finite automaton M2 such that M1 recognizes A1 and M2 recognizes A2.

Assume M1 = (Q1, , 1, q1, F1) and M2 = (Q2, , 2, q2, F2) It suffices to create a finite automaton M that recognizes

A1 A2. There is a problem since M doesn’t know where to subdivide the input string into the part accepted by M1 and the remaining part that will be accepted by M2. We will return to this later.

Non-Deterministic Automaton

NFAs generalize DFAs. In a DFA, each state has exactly one transition for each

symbol in the alphabet. In an NFA, at any state there may be zero or more

transitions for a symbol in the alphabet. In a DFA, a label on a transition arrow is a symbol in the

alphabet. In an NFA, a label on a transition arrow is a symbol in

the alphabet or .

Example

q1 q2 q3

0, 1

1 0, 1

0, 1

q4

Non-Deterministic Automaton

NFAs generalize DFAs. In a DFA, each state has exactly one transition for each

symbol in the alphabet. In an NFA, at any state there may be zero or more

transitions for a symbol in the alphabet. In a DFA, a label on a transition arrow is a symbol in the

alphabet. In an NFA, a label on a transition arrow is a symbol in

the alphabet or .

Example

q1 q2 q3

0, 1

1 0, 1

0, 1

q4

q1

q1

q1

q2

q1

q3

q2 q4q3

q3q1

q4q4q3q2q1

q3q1 q4 q4

0

1 11

00

11

1 1

0 0 0 0

1 1 1 1 1

Input:010110

Non-Deterministic Finite Automaton

N = (Q, , , q0, F)

Q is a finite set of states is a finite alphabet : Q x ( {}) (Q) F Q is a set of accept states

(Q) is the powerset of Q =

{X| X Q}

Machine N accepts string w

If there exists a sequence of states r0, r1, …, rn in Q such that 1) r0 = q0 2) ri+1 in (ri , wi+1) for i=0,…,n-1 3) rn in F

Note: w = w1w2…wn (ri , wi+1) is a set of states

Are NFAs more powerful than DFAs?

Every deterministic finite automaton has an equivalent non-deterministic finite automaton. (see next slide)

Every non-deterministic finite automaton has an equivalent deterministic finite automaton.

Non-deterministic?

q0 q1

0

1

0

1q2

0, 1

0 1q0 q0 q1q1 q0 q2q2 q2 q2

0 1{q0} {q0} {q1}{q1} {q0} {q2}{q2} {q2} {q2}

Non-deterministic interpretationDeterministic interpretation

Deterministic Equivalent?

1

2 3

b

a, b

a

a

DFA from NFA Construction

Assume no edges.Let N = (Q, , , q0, F)be an NFA that recognizes language A. We construct a DFA called M = (Q’, , ’, q0’, F’)

1) Q’ = (Q) 2) For R in Q’ and a in let ’(R,a) = {q in Q| q in (r,a) for some r in R}

= (r,a)

r in R

Q’ = {{}, {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}}

’({1,2},b) = (1,b) (2,b) = {2} {3} = {2,3}

Continued …

3) q0’ = { q0}

4) F’ = {R in Q’| R contains an accept state of N}

Assume edges, then we need these modifications.Let R be a state of M. Define E(R) = {q| q can be reached from R traveling along 0 or more edges}

Modify ’(R,a) = {q in Q| q in E((r,a)) for some r in R}

= E((r,a)) transition function r in R

’({1,2},b) = E((1,b)) E((2,b)) = E({2}) E({3}) = {2,3} ’({3},a) = E((3,a)) = E({1}) = {1,3}

q0’ = E({q0}) start state

Deterministic Equivalent?

1

2 3

b

a, b

a

a

a b{ } { } { }{1} { } {2}{2} {2,3} {3}{3} {1,3} { }

{1,2} {2,3} {2,3}{1,3} {1,3} {2}{2,3} {1,2,3} {3}

{1,2,3} {1,2,3} {2,3}

Deterministic Equivalent

Start state q0’= E({1}) = {1,3}Final states F’ = {{1}, {1,2}, {1,3}, {1,2,3}}

Final Solutiona

{1,3}

{2}

b

{3}a

{ }b

b

{2,3}a

b

a, b

a

b

a

{1,2,3}

b

{1,2}

a, b {1}

a

Regular languages are closed under union

Let A1 and A2 be regular languages. We want to show A1A2 is a regular language. Since A1 and A2 are regular languages there exists an NFA N1 and there exists an NFA N2 such that N1 recognizes A1 and N2 recognizes A2.

Assume N1 = (Q1, , 1, q1, F1) and N2 = (Q2, , 2, q2, F2) It suffices to create a NFA N that recognizes A1A2.

Construction of NFA

N1

N2

q0

N = (Q, , , q0, F)Q = {q0} Q1 Q2

F = F1 F2

1 (q,a) q in Q1 (q,a) = 2 (q,a) q in Q2 {q1, q2} q = q0 and a= { } q = q0 and a

Nq1

q2

Regular languages are closed under concatenation

Let A1 and A2 be regular languages. We want to show

A1 A2 is a regular language. Since A1 and A2 are regular languages there exists an NFA N1 and there exists an NFA N2 such that N1 recognizes A1 and N2 recognizes A2.

Assume N1 = (Q1, , 1, q1, F1) and N2 = (Q2, , 2, q2, F2)

It suffices to create a NFA N that recognizes A1 A2.

Construction of NFA

N1

N2

N = (Q, , , q1, F2)Q = Q1 Q2

F = F2

1 (q,a) q in Q1 and q not in F1 (q,a) = 2 (q,a) q in Q2 1 (q,a) {q2} q in F1 and a= 1 (q,a) q in F1 and a

q1

q2

q1q2

N

Regular languages are closed under the star operation

Let A be a regular language. We want to show A* is a regular language. Since A is regular language there exists an NFA N1 such that N1 recognizes A.

Assume N1 = (Q1, , 1, q1, F1) It suffices to create a NFA N that recognizes A*.

Regular languages are closed under union

Let A1 and A2 be regular languages. We want to show A1A2 is a regular language. Since A1 and A2 are regular languages there exists an NFA N1 and there exists an NFA N2 such that N1 recognizes A1 and N2 recognizes A2.

Assume N1 = (Q1, , 1, q1, F1) and N2 = (Q2, , 2, q2, F2) It suffices to create a NFA N that recognizes A1A2.

Construction of NFA

N1

N2

q0

N = (Q, , , q0, F)Q = {q0} Q1 Q2

F = F1 F2

1 (q,a) q in Q1 (q,a) = 2 (q,a) q in Q2 {q1, q2} q = q0 and a= { } q = q0 and a

Nq1

q2

Regular languages are closed under concatenation

Let A1 and A2 be regular languages. We want to show

A1 A2 is a regular language. Since A1 and A2 are regular languages there exists an NFA N1 and there exists an NFA N2 such that N1 recognizes A1 and N2 recognizes A2.

Assume N1 = (Q1, , 1, q1, F1) and N2 = (Q2, , 2, q2, F2)

It suffices to create a NFA N that recognizes A1 A2.

Construction of NFA

N1

N2

N = (Q, , , q1, F2)Q = Q1 Q2

F = F2

1 (q,a) q in Q1 and q not in F1 (q,a) = 2 (q,a) q in Q2 1 (q,a) {q2} q in F1 and a= 1 (q,a) q in F1 and a

q1

q2

q1q2

N

Regular languages are closed under the star operation

Let A be a regular language. We want to show A* is a regular language. Since A is regular language there exists an NFA N1 such that N1 recognizes A.

Assume N1 = (Q1, , 1, q1, F1) It suffices to create a NFA N that recognizes A*.

Construct NFA

N1

N

N = (Q, , , q0, F)Q = {q0} Q1 F = F1 {q0}

1 (q,a) q in Q1 and q not in F1

(q,a) = 1 (q,a) q in F1 and a 1 (q,a) {q1} q in F1 and a= {q1} q = q0 and a= { } q= q0 and a

q0

q1

q1

Regular Expressions

R is a regular expression if1) x for some x in (note: regular expression x represents language {x})

2) (empty string) (note: regular expression represents language {}) 3) (empty set)4) (R1 R2) where R1 and R2 are regular expressions5) (R1 R2) where R1 and R2 are regular expressions6) (R1*) where R1 is a regular expression

If R is a regular expression then L(R) is the language of R.

Examples

0*0 {w| w contains at least one zero} * = {} 11 00 = {11, 00} 0 *1 = {w| w begins with a 0 and ends in a 1} (01)* = {, 01, 0101, 010101, 01010101, …} 1*0 = {w| w contains any number of 1s followed by exactly one 0}

Using Regular Expressions

Beginning or End?

Regular Expressions vs. Regular Languages

A language is regular if and only if some regular expression describes it.

Part a) If a regular expression describes a language then it is regular.

Part b) If a language is regular then a regular expression describes it.

x

NFA that recognizes {x}

x

NFA that recognizes {}

NFA that recognizes

R1 R2, R1 R2, or R1*

Construct a machine the same way we did to show regular languages are closed under , , or *.

NFA to recognize (0 11)*

0 1

11

0

11

NFA to recognize (0 11)*

0

11

0

11

Part b) If a language is regular then a regular expression describes it.

Properties of GNFA1) The start state has transition arrows going to every other state but no arrows coming in from any other state.2) There is one accept state, and it has arrows coming in from every other state but no arrows going to any other state. The accept state is not the same as the final state.3) Except for the start and accept states, one arrow goes from every state to every other state and also from each state to itself.4) The labels on each edge is a regular expression.

Example GNFA

start accept

ab*

b

ab ba

b*

ab

aa

(aa)*a*

Generalize Non-deterministic Finite Automaton

GNFA is a 5-tuple (Q, , , qstart, qaccept)

: (Q – {qaccept}) x (Q – {qstart}) (all regular expressions over )

(qs, qt) R

qsqt

R

Example

1a

b

a, b

2

b

a b

2

1a

qstart

qaccept

1a

qstart

qaccept

b(a b)*

qstart

qaccept

a*b(a b)*

Example 1.36 (b to c)

new(s,2) = old(s,2) old(s,1) old(1,1)* old(1,2) = * a = a

new(s,3) = old(s,3) old(s,1) old(1,1)* old(1,3) = * b = b

new(2,2) = old(2,2) old(2,1) old(1,1)* old(1,2) = b a * a = b aa

new(3,3) = old(3,3) old(3,1) old(1,1)* old(1,3) = b * b = bb

Example 1.36 (b to c)

new(2,3) = old(2,3) old(2,1) old(1,1)* old(1,3) = a * b = ab

new(3,2) = old(3,2) old(3,1) old(1,1)* old(1,2) = a b * a = a ba

Example 1.36 (c to d)

new(s,a) = old(s,a) old(s,2) old(2,2)* old(2,a) = a (aa b)* = a(aa b)*

new(s,3) = old(s,3) old(s,2) old(2,2)* old(2,3) = b a (aa b)* ab = b a(aa b)* ab

new(3,a) = old(3,a) old(3,2) old(2,2)* old(2,a) = (ba a) (aa b)* = (ba a) (aa b)*

new(3,3) = old(3,3) old(3,2) old(2,2)* old(2,3) = bb (ba a) (aa b)* ab

Example 1.36 (d to e)

new(s,a) = old(s,a) old(s,3) old(3,3)* old(3,a) = a(aa b)* (b a(aa b)* ab)(bb (ba a) (aa b)* ab)* ((ba a) (aa b)* )

Part b) If a language is regular then a regular expression describes it.

Properties of GNFA1) The start state has transition arrows going to every other state but no arrows coming in from any other state.2) There is one accept state, and it has arrows coming in from every other state but no arrows going to any other state. The accept state is not the same as the final state.3) Except for the start and accept states, one arrow goes from every state to every other state and also from each state to itself.4) The labels on each edge is a regular expression.

Example GNFA

start accept

ab*

b

ab ba

b*

ab

aa

(aa)*a*

Generalize Non-deterministic Finite Automaton

GNFA is a 5-tuple (Q, , , qstart, qaccept)

: (Q – {qaccept}) x (Q – {qstart}) (all regular expressions over )

(qs, qt) R

qsqt

R

Example

1a

b

a, b

2

b

a b

2

1a

qstart

qaccept

1a

qstart

qaccept

b(a b)*

qstart

qaccept

a*b(a b)*

new(1,qaccept) = old(1, qaccept) old(1,2) old(2,2)* old(2, qaccept) = b (a b)* = b(a b)*

new(qstart, qaccept) = old(qstart, qaccept) old(qstart,1) old(1,1)* old(1, qaccept) = a* b(a b)* = a*b(a b)*

Example

Remove vertex 2:new(1,qaccept) = old(1, qaccept) old(1,2) old(2,2)* old(2, qaccept) = b (a b)* = b(a b)*

Remove vertx 1:|new(qstart, qaccept) = old(qstart, qaccept) old(qstart,1) old(1,1)* old(1, qaccept) = a* b(a b)* = a*b(a b)*

Example 1.36

s

b

1

3

2

a

a

ab

b

1

3

2

a

a

ab

b a

b

s

3

2

a

abb a

baa

aab

bb

s

3

a

(ba a) (aa b)* b a(aa b)* ab

a(aa b)*

bb (ba a) (aa b)*ab

Example 1.36 (b to c)

new(s,2) = old(s,2) old(s,1) old(1,1)* old(1,2) = * a = a

new(s,3) = old(s,3) old(s,1) old(1,1)* old(1,3) = * b = b

new(2,2) = old(2,2) old(2,1) old(1,1)* old(1,2) = b a * a = b aa

new(3,3) = old(3,3) old(3,1) old(1,1)* old(1,3) = b * b = bb

Remove vertex 1

Example 1.36 (b to c)

new(2,3) = old(2,3) old(2,1) old(1,1)* old(1,3) = a * b = ab

new(3,2) = old(3,2) old(3,1) old(1,1)* old(1,2) = a b * a = a ba

Remove vertex 1

Example 1.36 (c to d)

new(s,a) = old(s,a) old(s,2) old(2,2)* old(2,a) = a (aa b)* = a(aa b)*

new(s,3) = old(s,3) old(s,2) old(2,2)* old(2,3) = b a (aa b)* ab = b a(aa b)* ab

new(3,a) = old(3,a) old(3,2) old(2,2)* old(2,a) = (ba a) (aa b)* = (ba a) (aa b)*

new(3,3) = old(3,3) old(3,2) old(2,2)* old(2,3) = bb (ba a) (aa b)* ab

Remove vertex 2

Example 1.36 (d to e)

new(s,a) = old(s,a) old(s,3) old(3,3)* old(3,a) = a(aa b)* (b a(aa b)* ab)(bb (ba a) (aa b)* ab)* ((ba a) (aa b)* )

Remove vertex 3

s a

a(aa b)* (b a(aa b)* ab)(bb (ba a) (aa b)* ab)* ((ba a) (aa b)* )

Pumping Lemma

Purpose: Used to prove a language is not regular. What does it say?

All strings in a regular language can be “pumped” if they are at least as long as the pumping length p. Suppose xyz represents a string in the language whose length is at least as long as p. There is a section of the string (say y) that can be repeated, i.e., xykz, where k>=0 is also a member of the language.

Pumping Lemma

If A is a regular language, then there is a number p (the pumping length) where, if s is any string in A of length at least p, then s can be divided into three pieces, s = xyz satisfying the following conditions 1) for each k0, the string xykz is in A. 2) |y| > 0 3) |xy| p.

(Note form of this theorem is: RS)

Sketch Proof

Since A is a regular language there exists a DFA M = (Q,,,q1,F) with p states that recognizes A.

Either A has strings of length at least p or it doesn’t. Case 1: Suppose no string in A has length at least p Then the Pumping Lemma (RS) is vacuously true since the antecedent R is False.

Case 2: See next page.

Sketch Proof

Let s be a string in A of length n, where n is at least p. Starting in q1, M processes the string s by visiting n+1 states (namely, r1= q1,

r2, … rn+1).By the Pigeonhole Principle (n+1 pigeons and p nests), some state must have been visited more than once (say qx).

Sketch Proof

s = s1 s2 s3 … s’ … s” … sn

q1 r2 r3 … qx … qx … rn+1

q1

qx

rn+1

x

y

zRepetition first occurs when see the

(p+1) state

Nonregular Languages

Consider the language L={0n1n | n0}. Assume L is a regular language. Because of this, there exists a DFA with p states that recognizes L. Consider the string s = 0p1p

from L. Since its length is at least as long as p, it follows from the Pumping Lemma that 1) s = xyz and xykz in A for k0 2) |y| > 0 3) |xy| p

Choose carefully

NonRegular Languages

By (3) xy consists of all 0s. Case 1: |xy| = p xy = 0p-u 0u where u>0 and z = 1p

Consider xz = 0p-u 1p. Pumping Lemma says xz in L. This is a contradiction.

Case 2: |xy| < p xy = 0t where t < p and z = 0p-t 1p

xy = 0t-v0v where v > 0 Consider xy2z = 0t-v0v 0v 0p-t 1p = 0p+v 1p. Pumping Lemma says xy2z in L. This is a contradiction.

Therefore L is not regular.

Another s

What if s = (01)p was chosen instead?

s = (01)p = xyzRegardless how y is chosen, y can always be pumped. For example, if y = (01)k then it can be pumped.

Are Regular Languages Closed Under Other Operations?

Closed under union Closed under intersection (see page 46) Closed under complement (see exercise 1.10) Closed under concatenation Closed under star

Nonregular Languages

Consider the language L2={w in * | w has the same number of 0s as 1s}. Show L2 is not regular.

We know the language A = 0*1* is regularsince it can be represented using regular expressions. Suppose L2 is regular thenA L2 = {0n1n | n0}is regular is a contradiction.Therefore L2 is not regular.

Minimum Pumping Length

The minimum pumping length for a regular language A is the smallest p that is a pumping length of A.

Minimum Pumping Length

What is the minimum pumping length for 01*?

s = 0 = xyz x = , y = 0, z = by Pumping Lemma Can’t pump y!

Let p be greater than zero.s = 01p = xyz x = , y = 0, z = 1p can’t pump y x = 0, y = 1, z = 1p-1 can pump y, |xy|=2 x = , y = 01, z = 1p-1 can’t pump y

2

Minimum Pumping Length

What is the minimum pumping length for 11?

s = 11 = xyz x = , y = 1, z = 1 can’t pump y x = 1, y = 1, z = can’t pump y x = , y = 11, z = can’t pump y

minimum pumping length is 3 (vacuously true)

3