Sunteți pe pagina 1din 11

Review

! Languages and Grammars

CS 301 - Lecture 3 NFA DFA Equivalence Regular Expressions


Fall 2008

! Alphabets, strings, languages

! Regular Languages
! Deterministic Finite Automata ! Nondeterministic Finite Automata

! Today:
! Equivalence of NFA and DFA ! Regular Expressions ! Equivalence to Regular Languages

Equivalence of Machines
Machine ifL(M1 ) =

Example of equivalent machines NFA M1


L(M1 ) = {10} *
0
q0
DFA

M1 is equivalent to machine M 2 L(M 2 ) L(M 2 ) = {10} *

q1

0
q0

M2
1

0,1

q1
0

q2

Convert NFA to DFA


M
NFA

Convert NFA to DFA


NFA DFA

a
q0

M
q0

a a a

q1
b

q2

q1
b

q2

M!

{q0 }

DFA

M!

Begin with a set of start states

{q1, q2 }

{q0 }

DFA start state = Union of NFA Start States

From the NFA start states, where could we get on a? List the set of all possible states you can reach using just an a.

NFA

Convert NFA a to DFA


q0

NFA

Convert NFA to DFA


M
q0

q1
b

q2

a a

q1
b

q2
a

DFA

M!

{q0 }
b

{q1, q2 }
How did we get from q0 to q2 using only a? (think ! )

DFA

M!

{q0 }
b

{q1, q2 }
From new set, where could we get on a?

From the NFA start states, where could we get on b? The set of all possible states you can reach may be empty!

NFA

Convert NFA to DFA


M
q0

Convert NFA to DFA


NFA

a a
b

M
q0

a a
b

q1
b

q2
a
DFA

q1
b

q2
a

DFA

M!

{q0 }
b

M!

{q1, q2 }
From new set, where could we get on b?

{q0 }
b

{q1, q2 }
a, b
From empty set, we can only get back to empyt set.

Convert NFA to DFA


NFA

M
q0

Could This Produce Infinite States?


If the NFA has states

q1
b b

q2
a

q0 , q1, q2 ,...
There are a finite number of NFA states by definition

DFA

M!

{q0 }
b

the DFA has states in the powerset

{q1, q2 }
a, b
If a node has any NFA Final state, mark the node as Final in the DFA.

!, {q0 }{ , q1}{ , q1, q2 }{ , q3 , q4 , q7 } ,....


Powerset of finite set can be big, but it is not infinite!

Procedure NFA to DFA 1.


Initial state of NFA:

q0

Procedure NFA to DFA 2. For every DFAs state {qi , q j ,..., qm }


Compute in the NFA

Initial state of DFA:

{q0 }

! * (qi , a ), ! *( q j , a) ,
Add transition to DFA

= {qi! , q!j ,..., qm !}

...

!} "( {qi , q j ,..., qm }, a )= {qi! , q!j ,..., qm

Procedure NFA to DFA


Repeat Step 2 for all letters in alphabet, until no more transitions can be added.

Procedure NFA to DFA 3. For any DFA state {qi , q j ,..., qm }


If some

q j is a final state in the NFA

Then, {qi , q j ,..., qm } is a final state in the DFA

Proof

Theorem
Take NFA

M
M!

L(M ) = L(M !)

Apply procedure to obtain DFA Then

and

M!

are equivalent :

L(M ) " L(M !)

AND

L(M ) " L(M !)

L(M ) = L(M !)

First we show:

L(M ) " L(M !)


w ! L( M )
M:
q0

w ! L( M )

Take arbitrary: We will prove:

w
w = ! 1! 2 !! k

qf

w " L( M !)
M:
q0
!1
!2
!k

qf

We will show that if

w ! L( M )
!k

More generally, we will show that if in


(arbitrary string)

M:

w = ! 1! 2 !! k

v = a1a2 ! an
a2

M:

q0

!1

!2

qf

M:

q0

a1

qi

qj

ql

an

qm

M!:

!1

!2

!k

{q0 }

w " L( M !)

{q f ,}

M!:

a1
{q0 }

a2
{qi ,} {q j ,}

an
{ql ,} {qm ,}

Proof by induction on Induction Basis:

|v|
Induction hypothesis:

v = a1
q0
a1

1 !| v |! k

v = a1a2 ! ak
M:
q0
a1

M:

qi

qi

a2

qj

qc

ak

qd

M!:

a1
{q0 }
{qi ,}

M!:

a1
{q0 }

a2
{qi ,} {q j ,}

ak
{qc ,} {qd ,}

Induction Step:

| v |= k + 1
v!

Induction Step:

| v |= k + 1
v!

v = a1a2 % ak ak +1 = v!ak +1 $ ! #! "


M:
q0
a1

v = a1a2 % ak ak +1 = v!ak +1 $ ! #! "


M:
q0
a1

qi

a2

qj

qc

ak

qd

qi

a2

qj

qc

ak

qd ak +1 qe

v! M!:
a1
{q0 }

v!
ak
{qc ,} {qd ,}

a2
{qi ,} {q j ,}

M!:

a1
{q0 }

a2
{qi ,} {q j ,}

ak

ak +1
{qe ,}

{qc ,} {qd ,}

v!

v!

Therefore if

w ! L( M )
w = ! 1! 2 !! k
!2
!k

We have shown:

L(M ) " L(M !)

M:

q0

!1

qf
We also need to show:

L(M ) " L(M !)

M!:

!1

!2

!k

(proof is similar)
{q f ,}

{q0 }

w " L( M !)

We will prove: Languages accepted by NFAs

NFAs Accept Regular Languages

Regular Languages
Languages accepted by DFAs

NFAs and DFAs have the same computation power

Step 1 Languages accepted by NFAs Languages accepted by NFAs

Step 2

Regular Languages

Regular Languages

Proof: Every DFA is trivially an NFA Any language L accepted by a DFA is also accepted by an NFA

Proof: Any NFA can be converted to an equivalent DFA Any language L accepted by an NFA is also accepted by a DFA

Regular Expressions
! Regular expressions describe regular languages

Recursive Definition
#, " , ! Given regular expressions r 1 and r2
Primitive regular expressions:

! Example:

(a + b ! c) *

r1 + r2 r1 ! r2 r1 *
Are regular expressions

describes the language

{a, bc} * = {! , a, bc, aa, abc, bca,...}

(r1 )

Examples
A regular expression:
!

Languages of Regular Expressions


L(r ) :
language of regular expression

(a + b " c )* "(c + !)
! Example

Not a regular expression:

(a + b + )

L((a + b " c) *) = {! , a, bc, aa, abc, bca,...}

Formal Definition
! For primitive regular expressions:

Definition (continued)
! For regular expressions r 1 and r2

L(! ) = ! L(" ) = {" } L(a ) = {a}

L(r1 + r2 ) = L(r1 ) ! L(r2 ) L(r1 ! r2 ) = L(r1 ) L(r2 ) L(r1 *) = (L(r1 ))* L((r1 )) = L(r1 )

Example
L((a + b )! a *) = L((a + b )) L(a *) = L(a + b ) L(a *)

Example
! Regular expression

= (L(a ) ! L(b )) (L(a ))* = ({a}! { b}) ({a})* = {a, b}{! , a, aa, aaa,...}
= {a, aa, aaa,..., b, ba, baa,...}

r = (a + b )* (a + bb ) L(r ) = {a, bb, aa, abb, ba, bbb,...}

10

Whats Next
! Read
! Linz Chapter 1, 2.1, 2.2, 2.3, (skip 2.4), Chapter 3 ! JFLAP Startup, Chapter 1, 2.1, (skip 2.2) 3, 4

! Next Lecture Topics from Chapter 3.2 and 3.3


! Regular Expressions and Regular Languages ! Regular Grammars and Regular Languages

! Quiz 1 in Recitation on Wednesday 9/17


! Covers Linz 1.1, 1.2, 2.1, 2.2, 2.3, and JFLAP 1, 2.1 ! Closed book, but you may bring one sheet of 8.5 x 11 inch paper with any notes you like. ! Quiz will take the full hour

! Homework
! Homework 2 is Due Thursday

11

S-ar putea să vă placă și