+ All Categories
Home > Documents > Formal languages and automata theorycs322d.yolasite.com/resources/CS322D_NFA&DFA&RE_Part2.pdf ·...

Formal languages and automata theorycs322d.yolasite.com/resources/CS322D_NFA&DFA&RE_Part2.pdf ·...

Date post: 17-Mar-2020
Category:
Upload: others
View: 24 times
Download: 0 times
Share this document with a friend
51
T. Najla Arfawi 2 nd Term - 2016 CS 322 D: Formal languages and automata theory Tutorial NFA DFA Regular Expression
Transcript

T. Najla Arfawi 2nd Term - 2016

CS 322 D: Formal languages

and automata theory

Tutorial – NFA – DFA Regular Expression

Finite Automata

Finite Automata

1. Q - States

2. S - Alphabets

3. d - Transitions

4. q0 - Initial state

5. F - Final state(s) / Accepting state(s)

T. Najla Arfawi - 2nd Term 2016 2

Deterministic Finite Automata

There must be a transition corresponding to each alphabet at

each state

T. Najla Arfawi - 2nd Term 2016 3

Nondeterministic Finite Automata

Can be at multiple states at the same time

Can go to multiple states on one alphabet

May have no transitions on an alphabet (die)

T. Najla Arfawi - 2nd Term 2016 4

Every NFA can be converted into a DFA for the same language.

NFA → DFA in two easy steps

We do this first

➊ Eliminate e-transitions

➋ Convert simplified NFA

5 T. Najla Arfawi - 2nd Term 2016

NFA → DFA: states

1 0

0, 1

q0 q1 q2 NFA:

DFA: {q0, q1}

{q0, q2} {q0, q1, q2}

{q1, q2}

{q0}

{q1}

{q2}

DFA has a state for every subset of NFA states 6 T. Najla Arfawi - 2nd Term 2016

NFA → DFA: transitions

1 0

0, 1

q0 q1 q2 NFA:

DFA: {q0, q1}

{q0, q2} {q0, q1, q2}

{q1, q2}

{q0}

{q1}

{q2}

0

1

0

1

0

1

0 1

0

1

0

1 0, 1

7 T. Najla Arfawi - 2nd Term 2016

NFA → DFA: accepting states

1 0

0, 1

q0 q1 q2 NFA:

DFA: {q0, q1}

{q0, q2} {q0, q1, q2}

{q1, q2}

{q0}

{q1}

{q2}

0

1

0

1

0

1

0 1

0

1

0

1 0, 1

NFA accepts if it contains a DFA final state 8 T. Najla Arfawi - 2nd Term 2016

NFA → DFA: dead state elimination

1 0

0, 1

q0 q1 q2 NFA:

DFA: {q0, q1}

{q0, q2}

{q0}

0

1

0

1

0 1

{q0, q1, q2} 0

1

{q1, q2}

{q1}

{q2}

0

1

0

1

0, 1

At the end, you can eliminate the unreachable states 9 T. Najla Arfawi - 2nd Term 2016

General method

NFA DFA

states q0, q1, …, qn

q0}, {q1}, {q0,q1}, …, {q0,…,qn}

one for each subset of states in the NFA

initial state q0 q0}

transitions d d’({qi1,…,qik}, a) =

d(qi1, a) ∪…∪ d(qik, a)

accepting

states

F Q F’ = {S: S contains some state in F}

10 T. Najla Arfawi - 2nd Term 2016

NFA → DFA in two easy steps

➊ Eliminate e-transitions

➋ Convert simplified NFA ✔

11 T. Najla Arfawi - 2nd Term 2016

Eliminating e-transitions

q0 q1 q2

e, 1 0

0

e NFA:

NFA without es:

{q1, q2}

0 1

q0

q1

q2

{q0, q1, q2}

{q0, q1, q2}

Accepting states: q2 , q1 , q0

12 T. Najla Arfawi - 2nd Term 2016

Eliminating e-transitions

NFA:

new NFA:

q0 q1 q2

e, 1 0

0

e

q0 q1 q2 0, 1

0

0

0, 1

0 0

{q1, q2}

0 1

q0

q1

q2

{q0, q1, q2}

{q0, q1, q2}

13 T. Najla Arfawi - 2nd Term 2016

Eliminating e-transitions

Paths with es are replaced by a single transition

States that can reach final state by e are all accepting

q5 q0 q2 q0 q3 e e e a

q4

q3

q5

a

e

e

q5

q3 a q3

a

e e

e

q9 q7 q3 q2

14 T. Najla Arfawi - 2nd Term 2016

Regular Expression

• The symbols and e are regular expressions

• Every a in S is a regular expression

• If R and S are regular expressions, so are RUS,

R0S and R*

Remember for R*, * could be 0

T. Najla Arfawi - 2nd Term 2016 15

Regular Languages

Languages that can be represented by a DFA / NFA / RE

DFA = NFA = RE (in term of equivalence)

When asked if a language L is regular, represent L using a

DFA / NFA / RE

T. Najla Arfawi - 2nd Term 2016 16

Checklist

1. Always pay attention to S

2. For DFA, out degree of each state must be equal |S|

e.g. S = {0, 1, 2}, L = {w: w begins with 01}

3. For NFA, pay attention to e-transitions

4. Remember to specify the initial state

5. Make sure you have considered the input e

e.g. L = {w: w contains even number of 01}

T. Najla Arfawi - 2nd Term 2016 17

Exercise

S = {0, 1}

L = {w: w is non-empty and the sum of the digits in w

is divisible by 5}

T. Najla Arfawi - 2nd Term 2016 18

Idea

Need to keep track of the sum of digits

Arrive at the state qk when sum of digits = k

q0 q1

0 0

1 q2

0

1 q3

0

1 1 q4

0

q5

0

1 …

T. Najla Arfawi - 2nd Term 2016 19

Idea

Need to keep track of the sum of digits

Arrive at the state qk when sum of digits = k

k divisible by 5 iff k mod 5 = 0

Keep track of the remainder instead

q0 q1

0 0

1 q2

0

1 q3

0

1 1 q4

0

1

T. Najla Arfawi - 2nd Term 2016 20

Answer

Handle the input e

q0 q1

0 0

1 q2

0

1 q3

0

1 1 q4

0

1

qs 0

1

T. Najla Arfawi - 2nd Term 2016 21

Converting an NFA to a DFA

Eliminate e

for each state i

for each alphabet x

for each state j

if i can reach j using one x and e(s)

add an edge from i to j with label x

q0 q1

0 q2

e

q0 q1

e q2

1

q0 q1

0 q2

0

q0 q1 q2

1

1

q0 q1

e q2

1 q0 q1 q2

1

1 q3

e q3

1 1

T. Najla Arfawi - 2nd Term 2016 22

Converting an NFA to a DFA

Eliminate e

Remember to consider the case when the initial state can

reach accepting states on e(s)

q0 q1

e q2

1

q0 q1 q2

1

1

T. Najla Arfawi - 2nd Term 2016 23

Converting an NFA to a DFA

• Every possible subsets of Q is a state in the DFA

• Going to multiple states at the same time in the NFA

= going to a subset of Q, which is now a state in the

DFA

q0

q1 1

q2 1

{q0} {q1, q2} 1

T. Najla Arfawi - 2nd Term 2016 24

Exercise

q0 q1

0

e

0

q2

1 0, 1 1

T. Najla Arfawi - 2nd Term 2016 25

Eliminating e-transitions q0 can reach q0 on 0 (q0 -> q1 -> q0)

q0 can reach q1 on 0 (q0 -> q1)

q1 can reach q0 on 0 (q1 -> q0)

q1 can reach q1 on 0 (q1 -> q1)

The rest of the transitions remain unchanged

0 1

q0 q0, q1 q2

q1 q0, q1

q2 q1 q1, q2

q0 q1

0

e

0

q2

1 0, 1 1

T. Najla Arfawi - 2nd Term 2016 26

Eliminating e-transitions

q0 is the initial state

q0 can reach an accepting state on e (i.e. q1)

therefore q0 is also an accepting state

q0 q1

0

0

q2

1 0, 1 1

0

0

0 1

q0 q0, q1 q2

q1 q0, q1

q2 q1 q1, q2

q0 q1

0

e

0

q2

1 0, 1 1

T. Najla Arfawi - 2nd Term 2016 27

Converting NFA to DFA

0 1

{q0} {q0, q1} {q2}

{q1} {q0, q1} ∅

{q2} {q1} {q1, q2}

q0 q1

0

0

q2

1 0, 1 1

0

0

T. Najla Arfawi - 2nd Term 2016 28

Converting NFA to DFA

Starting from initial state {q0} 0 1

{q0} {q0, q1} {q2}

{q1} {q0, q1} ∅

{q2} {q1} {q1, q2}

{q0} {q0, q1} 0

{q2}

1

T. Najla Arfawi - 2nd Term 2016 29

Converting NFA to DFA

Check if every state has |S| outgoing transitions

No

“Fix” {q2} and {q0, q1}

{q0, q1} goes to {q0, q1} ∪ {q0, q1} = {q0, q1} on 0

{q0, q1} goes to {q2} ∪ ∅ = {q2} on 1

0 1

{q0} {q0, q1} {q2}

{q1} {q0, q1} ∅

{q2} {q1} {q1, q2}

{q0} {q0, q1} 0

{q2}

1

0

{q1}

0

{q1, q2}

1

1 T. Najla Arfawi - 2nd Term 2016 30

Converting NFA to DFA

Check if every state has |S| outgoing transitions

No

“Fix” {q1} and {q1, q2}

{q1, q2} goes to {q0, q1} ∪ {q1} = {q0, q1} on 0

{q1, q2} goes to ∅ ∪ {q1, q2} = {q1, q2} on 1

0 1

{q0} {q0, q1} {q2}

{q1} {q0, q1} ∅

{q2} {q1} {q1, q2}

{q0} {q0, q1} 0

{q2}

1

0

{q1}

0

{q1, q2}

1

1

0

0

1

1

T. Najla Arfawi - 2nd Term 2016 31

Converting NFA to DFA

Check if every state has |S| outgoing transitions

No

“Fix” ∅

∅ = die

0 1

{q0} {q0, q1} {q2}

{q1} {q0, q1} ∅

{q2} {q1} {q1, q2} 0, 1

{q0} {q0, q1} 0

{q2}

1

0

{q1}

0

{q1, q2}

1

1

0

0 ∅

1

1 T. Najla Arfawi - 2nd Term 2016 32

Converting NFA to DFA

Check if every state has |S| outgoing transitions

Yes

Accepting states = states that contain q0 or q1

0 1

{q0} {q0, q1} {q2}

{q1} {q0, q1} ∅

{q2} {q1} {q1, q2}

q0 q1

0

0

q2

1 0, 1 1

0

0

0, 1

{q0} {q0, q1} 0

{q2}

1

0

{q1}

0

{q1, q2}

1

1

0

0 ∅

1

1 T. Najla Arfawi - 2nd Term 2016 33

Converting NFA to DFA

Done

q0 q1

0

e

0

q2

1 0, 1 1 0, 1

{q0} {q0, q1} 0

{q2}

1

0

{q1}

0

{q1, q2}

1

1

0

0 ∅

1

1

T. Najla Arfawi - 2nd Term 2016 34

Regular expressions

35 T. Najla Arfawi - 2nd Term 2016

String concatenation

st = abbbab

s = x1…xn st = x1…xny1…ym t = y1…ym

ts = bababb

ss = abbabb

sst = abbabbbab

st = abbbab s = abb t = bab

ts = bababb

ss = abbabb

sst = abbabbbab

36 T. Najla Arfawi - 2nd Term 2016

Operations on languages

The concatenation of languages L1 and L2 is

The n-th power of Ln is

The union of L1 and L2 is

L1L2 = {st: s L1, t L2}

Ln = {s1s2...sn: s1, s2, ..., sn L}

L1 L2 = {s: s L1 or s L2}

37 T. Najla Arfawi - 2nd Term 2016

Operations on languages

The star of L are all strings made up of zero or more chunks

from L:

Example: L1 = {01, 0}, L2 = {e, 1, 11, 111, …}.

What is L1* and L2

*?

L* = L0 L1 L2 …

38 T. Najla Arfawi - 2nd Term 2016

Example

L1 = {0, 01}

L10 = {e}

L1* are all strings that start

with 0 and do not contain consecutive 1s L1

2 = {00, 001, 010, 0101}

L11 = {0, 01}

L13 = {000, 0001, 0010, 00101,

0100, 01001, 01010, 010101}

(plus the empty string)

L1*: 00100001

00110001

is in L1*

is not in L1*

10010001 is not in L1*

39 T. Najla Arfawi - 2nd Term 2016

Example

L2 = {e, 1, 11, 111, …}

L20 = {e}

L21 = L2

any number of 1s

L2* = L2

0 L21 L2

2 …

= {e} L21 L2

2 …

= L2

L2* = L2

L22 = L2

L2n = L2 (n ≥ 1)

40 T. Najla Arfawi - 2nd Term 2016

Combining languages

We can construct languages by starting with simple ones, like

{0}, {1} and combining them

{0}({0}{1})*

({0}{1}*)({1}{0}*)

0(0 + 1)*

01* + 10*

all strings that start with 0

0 followed by any number of 1s, or 1 followed by any number of 0s

41 T. Najla Arfawi - 2nd Term 2016

Regular expressions

A regular expression over S is an expression formed using the following rules:

The symbols and e are regular expressions

Every a in S is a regular expression

If R and S are regular expressions, so are R+S, RS and R*.

+ is union operator U

A language is regular if it is represented by a regular expression

42 T. Najla Arfawi - 2nd Term 2016

Analyzing regular expressions

01*

(01*)(01)

0 followed by any number of 1s

= 0(1*) = {0, 01, 011, 0111, …}

= {001, 0101, 01101, 011101, …}

0 followed by any number of 1s and then 01

S = {0, 1}

43 T. Najla Arfawi - 2nd Term 2016

Analyzing regular expressions

(0+1)* = {e, 0, 1, 00, 01, 10, 11, …} any string

0+1 = {0, 1}

(0+1)*01(0+1)*

strings of length 1

any string that contatins the pattern 01

(0+1)*010 any string that ends in 010

44 T. Najla Arfawi - 2nd Term 2016

Analyzing regular expressions

(0+1)(0+1)

(0+1)(0+1)(0+1)

strings of length 2

strings of length 3

((0+1)(0+1))*+((0+1)(0+1)(0+1))*

((0+1)(0+1))* strings of even length

((0+1)(0+1)(0+1))* strings of length a multiple of 3

all strings whose length is even or a mutliple of 3 = strings of length 0, 2, 3, 4, 6, 8, 9, 10, 12, ...

45 T. Najla Arfawi - 2nd Term 2016

Analyzing regular expressions

((0+1)(0+1)+(0+1)(0+1)(0+1))*

(0+1)(0+1)

(0+1)(0+1)(0+1)

strings of length 2

strings of length 3

(0+1)(0+1)+(0+1)(0+1)(0+1) strings of length 2 or 3

strings that can be broken in blocks, where each block has length 2 or 3

46 T. Najla Arfawi - 2nd Term 2016

Analyzing regular expressions

((0+1)(0+1)+(0+1)(0+1)(0+1))*

strings that can be broken in blocks, where each block has length 2 or 3

00110 10 011 e 1 011010110 ✓ ✓ ✓ ✓ ✓ ✗

this includes all strings except those of length 1

((0+1)(0+1)+(0+1)(0+1)(0+1))* = all strings except 0 and 1

47 T. Najla Arfawi - 2nd Term 2016

Analyzing regular expressions

(1+01+001)*(e+0+00)

ends in at most two 0s

there can be at most two 0s between consecutive 1s

Guess: (1+01+001)*(e+0+00) = {x: x does not contain 000}

there are never three consecutive 0s

0110010110 0010010 00 e

48 T. Najla Arfawi - 2nd Term 2016

Writing regular expressions

Write a regular expression for

all strings with two consecutive 0s. S = {0, 1}

(0+1)*00(0+1)*

(anything) 00 (anything else)

49 T. Najla Arfawi - 2nd Term 2016

Writing regular expressions

Write a regular expression for

all strings that do not contain two consecutive 0s. S = {0, 1}

1*(011*)*(e + 0)

1110110101101010

every 0

followed by one or more 1s

maybe a 0 at the end some 1s at the beginning

... every middle 0 followed by one or more 1s

... and at most one 0 in the last block

(011*)

(e + 0)

50 T. Najla Arfawi - 2nd Term 2016

Writing regular expressions

Write a regular expression for

all strings with an even number of 0s. S = {0, 1}

even number of zeros = (two zeros)*

two zeros = 1*01*01*

(1*01*01*)*

51 T. Najla Arfawi - 2nd Term 2016


Recommended