Kleene's Theorem

Preview:

DESCRIPTION

Kleene's Theorem. We have defined the regular languages , using regular expressions, which are convenient to write down and use. We have also defined the languages which are accepted by FSAs , which make it easy to tell whether a string is a member of the language. - PowerPoint PPT Presentation

Citation preview

Kleene's Theorem

We have defined the regular languages, using regular expressions, whichare convenient to write down and use.

We have also defined the languages which are accepted by FSAs, which make it easy to tell whether a string is a member of the language.

Theorem: Kleene's theorem

A language L is accepted by a FSA iff L is regular

Not only are regular expressions andFSA's equivalent, there are algorithmsallowing us to translate between the two.

Recall: Regular Expressions (REs)

(i) denotes {}, denotes {}, t denotes {t} for t T;(ii) if r and s are regular expressions denoting languages R and S, then

(r + s) denoting R + S,(rs) denoting RS, and(r*) denoting R* are regular expressions;

(iii) nothing else is a regular expression over T.

Let T be an alphabet. A regular expression over T defines a language over T as follows:

Note: a recursive definition of the language of REs

Algorithm: Reg Ex => NDFSA (overview page)

Let L be a regular language over T. We will create A, a NDFSA accepting L. Recall: (Q,I,F,T,E)

if L = {}, then A = ({q} , {q} , {q}, T , {})

if L = {}, then A = ({q} , {q} , {} , T, {})

if L = {t}, then A = ({p,q} , {p} , {q} , T , {(p,t,q)})

if L = L1 + L2 then obtainA1 = (Q1 , {i1} , {f1} , T , E1) L1 = L(A1)A2 = (Q2 , {i2} , {f2} , T , E2) L2 = L(A2)

A = (Q1 Q2 {i,f}, {i} , {f} , T , E1 E2 {(i,,i1),(i,,i2),(f1,,f),(f2,,f)})

if L = L1L2 then obtain A1 and A2 as aboveA = (Q1 Q2 , {i1} , {f2} , T , E1 E2 {(f1,,i2)})

if L = L1* then obtain A1 as aboveA = (Q1 {i,f} , {i}, {f}, T, E1 {(i,,i1),(i,,f),(f1,,f),(f1,,i1)})

Regular Expression => NDFSA (with added comments)

Where necessary, draw the DFAs constructed here!

Let L be a regular language over T. We will create A, a NDFSA accepting L. Recall: (Q,I,F,T,E)

if L = {}, then A = ({q},{q},{q},T,{}) (I=F)

if L = {}, then A = ({q},{q},{},T,{}) (F={})

if L = {t}, then A = ({p,q},{p},{q},T, {(p,t,q)})

NB: So far, the NDFSAs we’re constructing have exactly one initial state and at most one final state. Later constructs will keep it that way! (We return to the case where L={} later.)

Regular Expression => NDFSA (with added comments)

Let L be a regular language over T. We will create A, a NDFSA accepting L. Recall: (Q,I,F,T,E)

if L = L1 + L2 then obtainA1 = (Q1,{i1},{f1},T,E1) L1 = L(A1)A2 = (Q2,{i2},{f2},T,E2) L2 = L(A2)

A = (Q1 Q2 {i,f},{i},{f},T, E1 E2 {(i,,i1),(i,,i2),(f1,,f),(f2,,f)})Start with i. Following , do either A1 or A2. End in f.Nondeterminism (and edges)can be useful!

i

i1

i2

f1

f2

f

A1

A2

Regular Expression => NDFSA (with added comments)

Let L be a regular language over T. We will create A, a NDFSA accepting L. Recall: (Q,I,F,T,E)

if L = L1 + L2 then obtainA1 = (Q1,{i1},{f1},T,E1), L1 = L(A1)A2 = (Q2,{i2},{f2},T,E2), L2 = L(A2)

A = (Q1 Q2 {i,f},{i},{f},T,E1 E2 {(i,,i1),(i,,i2),(f1,,f),(f2,,f)})(Start with i. Following , do either A1 or A2. End in f.Nondeterminism can be useful!)

if L = L1L2 then obtain A1 and A2 as aboveA = (Q1 Q2,{i1},{f2},T,E1 E2 {(f1,,i2)}) ({(f1,,i2)}) links the end of A1 with the start of A2)

if L = L1* then obtain A1 as aboveA = (Q1 {i,f} ,{i},{f},T, E1 {(i,,i1),(i,,f),(f1,,f),(f1,,i1)}) The edge (i,,f) stands for 0 strings in L1

The edge (f1,,i1) causes a loop

Example: Regular Expression => NDFSA

Let L = (b+ab)(b+ab)*, T = {a,b}

Find NDFSA's for1. (b+ab)

1.1. b1.2. ab

1.2.1. a1.2.2. b

2.(b+ab)*2.1. (b+ab) (same as 1.)

1.2.1 = ({1,2},{1},{2},T,{(1,a,2)})1.2.2 = ({3,4},{3},{4},T,{(3,b,4)})

1.2 = ({1,2,3,4},{1},{4},T,{(1,a,2), (2,,3) ,(3,b,4)})1.1 = ({5,6},{5},{6},T,{(5,b,6)})

1 = ({1,2,3,4,5,6,7,8},{7},{8},T, { (7,,1),(7,,5), (1,a,2),(2,,3),(3,b,4), (5,b,6), (4,,8),(6,8) })

2.1 = ({9,10,11,12,13,14,15,16},{15},{16},T, { (15,,9),(15,,13), (9,a,10),(10,,11), (11,b,12),(13,b,14), (12,,16),(14,,16) })

Example (cont.)

2. = ({9,10,11,12,13,14,15,16,17,18},{17},{18},T, {(17,,15),(17,,18),(15,,9),(15,,13),(9,a,10), (10,,11),(11,b,12),(13,b,14),(12,,16), (14,,16),(16,,18),(16,,15)})

A = ({1,2,...,18},{7},{18},T, { (7,,1),(7,,5),(1,a,2),(2,,3),(3,b,4),(5,b,6), (4,,8),(6,8), (8,17), (17,,15),(17,,18), (15,,9),(15,,13),(9,a,10),(10,,11),(11,b,12), (13,b,14),(12,,16),(14,,16),(16,,18),(16,,15) })

a b

b

a b

b

7

1 2 3 4

5 6

8 17 15

13 14

9 10 11 12

16 18

This is nondeterministic ... butwe know that any NDFSA can beconverted into a DFSA (although we skipped the details of how this is done)

where we are at the moment ...

• We’ve seen how for each Regular Expression one can construct an NDFSA that accepts the same language

• Now let’s do the reverse: given a NDFSA Expression, we construct a regular expression that denotes the same language

Recall: FSAs

A Finite State Automaton (FSA) is a 5-tuple (Q, I, F, T, E) where:Q = states = a finite set;I = initial states = a nonempty subset of Q;F = final states = a subset of Q;T = an alphabet;E = edges = a subset of Q (T + ) Q.

FSA = labelled, directed graph = set of nodes (some final/initial) +

directed arcs (arrows) between nodes + each arc has a label from the alphabet.

Algorithm: FSA -> Regular Expression

2. Algorithm: create unique final state (informal)

Input: (Q, I, F, T, E)Q := Q {f} (where f Q)for each q F, E:= E {(q,,f)}F := {f}

A regular finite state automaton (RFSA) is a FSA where the edge labels may be regular expressions.

An edge labelled with the regular expression rindicates that we can move along that edge on input of any string included in r.

1. create unique initial state2. create unique final state3. unique FSA ->Regular Expression

1. a unique initial state is created in the same way

(3) Unique FSA -> Regular Expresssion

Let A be a FSA with unique initial and final states. In a number of steps, A can be converted to a RFSA with just one edge, whose label is the required Regular Expression.

Here’s the start of the proof:

While there are states in Q\{i,f}begin

For each state p in Q with more than one edgeto itself, labelled r1,r2,...,rn, replace all those edges by (p,r1+r2+...+rn, p).

For each pair of states p,q in Q with more thanone edge from p to q labelled r1,r2,...,rn, replace all those edges by (p,r1+r2+...+rn, q).

(....)

Unique FSA -> Regular Expresssion

Let A be a FSA with unique initial and final states.A can be converted to a RFSA.

While there are states in Q\{i,f}begin

For each state p in Q with more than one edgeto itself, labelled r1,r2,...,rn, replace all those edges by (p,r1+r2+...+rn, p).

For each pair of states p,q in Q with more thanone edge from p to q labelled r1,r2,...,rn, replace all those edges by (p,r1+r2+...+rn, q).

select a state s {i,f}For each pair of states p,q (s) s.t. there areedges (p,r1,s) and (s,r2,q)beginif there is an edge (s,r3,s)

add the edge (p,r1r3*r2,q)else add the edge (p,r1r2,q)

endremove all edges to or from sremove all states & edges with no path from i

endreturn r, where E = (i,r,f).

p s q

r3

r1 r2

r1 r3* r2

Example: FSA -> Regular Expression

1

2 3

4

a

b

b

b

a

a a,b

Example: FSA -> Regular Expression

1

2 3

4

a

b

b

b

a

a a,b

create unique initial and final states; add a+b loop

1

2 3

4

a

b

b

b

a

a a+b

i

f

Example: FSA -> Regular Expression

1

2 3

4

a

b

b

b

a

a a,b

create unique initial and final states; add a+b loop

1

2 3

4

a

b

b

b

a

a a+b

i

f

remove state 2 - edges are 1-3, 1-4, 4-3, 4-4

1

3

4

aa

b

b

aa

a+b

i

f

ab

ab

Example: FSA -> Regular Expression

1

3

4

aa

b

b

aa

a+b

i

f

ab

ab

Example (cont.)

1

3

4

aa

b+ab

b+ab

aa

a+b

i

f

combine: b+ab, b+ab

Example (cont.)

1

3

4

aa

b+ab

b+ab

aa

a+b

i

f

remove state 3 - no edges

1 4b+ab

i f

b+ab

Example (cont.)

1

3

4

aa

b+ab

b+ab

aa

a+b

i

f

remove edge pairs remove state 3 - no edges

1 4b+ab

i f

b+ab

remove state 4 - edge is 1-f

1i f

(b+ab)(b+ab)*

Example (cont.)

1

3

4

aa

b+ab

b+ab

aa

a+b

i

f

remove edge pairs remove state 3 - no edges

1 4b+ab

i f

b+ab

remove state 4 - edge is 1-f

1i f

(b+ab)(b+ab)*

remove state 1 - edge is i-f

i f(b+ab)(b+ab)*

expression is: (b+ab)(b+ab)*

Is this a proper proof?

• The first half (RE=>FSA) is unproblematic– Proof follows the (recursive) definition of

the language of REs

– Wrinkle: A may lack a final state (the case where L={})

• The second half (FSA=>RE):– Algorithm not fully specified. (e.g., “select a state s”)

Does the order in which states are selected not matter?

– Is the resulting RE always equivalent to the initial FSA?

• These wrinkles can be ironed out

Recommended