CSC 3130: Automata theory and formal languages

34
CSC 3130: Automata theory and formal languages Andrej Bogdanov http://www.cse.cuhk.edu.hk/ ~andrejb/csc3130 The Chinese University of Hong Kong NFA to DFA conversion and regular expressions Fall 2009

description

Fall 2009. The Chinese University of Hong Kong. CSC 3130: Automata theory and formal languages. NFA to DFA conversion and regular expressions. Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130. NFAs are as powerful as DFAs. Obviously, an NFA can do everything a DFA can do - PowerPoint PPT Presentation

Transcript of CSC 3130: Automata theory and formal languages

Page 1: CSC 3130: Automata theory and formal languages

CSC 3130: Automata theory and formal languages

Andrej Bogdanov

http://www.cse.cuhk.edu.hk/~andrejb/csc3130

The Chinese University of Hong Kong

NFA to DFA conversionand regular expressions

Fall 2009

Page 2: CSC 3130: Automata theory and formal languages

NFAs are as powerful as DFAs

• Obviously, an NFA can do everything a DFA can do

• But can it do more?

• Theorem

If a language L is accepted by some NFA, thenit is also accepted by some DFA.

NO!

Page 3: CSC 3130: Automata theory and formal languages

Proof of theorem

• We will show a general way to convert every NFA into an equivalent DFA

• Step 1: Simplify NFA by eliminating -transitions

• Step 2: Convert simplified NFA (without s)We do this first

Page 4: CSC 3130: Automata theory and formal languages

NFA to DFA conversion intuition

1 0

0, 1

q q qNFA:

DFA: 1q q0 or q1

1

q0 or q2

1

0 0

0

Page 5: CSC 3130: Automata theory and formal languages

NFA to DFA conversion intuition

1 0

0, 1

q q qNFA:

DFA: 1q {q0, q1}

1

{q0, q2}

1

0 0

0

Page 6: CSC 3130: Automata theory and formal languages

General method

NFA DFAstates q0, q1, …,

qn

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

one for each subset of states in the NFA

initial state

q0 q0}

transitions

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

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

accepting states

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

Page 7: CSC 3130: Automata theory and formal languages

Why the method works

• At the end, the DFA accepts when it is in a state that contains some accepting state of NFA

• So the DFA accepts only when the NFA accepts too

After reading n symbols, the DFA is in state {qi1,…,qik} if and only if the NFA is in one of the states qi1,…,qik

Page 8: CSC 3130: Automata theory and formal languages

Converting via the general method

1 0

0, 1

q q qNFA:

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

01

0

1

Page 9: CSC 3130: Automata theory and formal languages

Converting via the general method

{q0, q1}

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

{q1, q2}

{q0}

{q1}

{q2}

0, 1

0

1

0

1

0, 1

0

1

0 1

01

0

1

After eliminating the dead states and transitions, we end up with the same picture

Page 10: CSC 3130: Automata theory and formal languages

Proof of theorem

• We will show a general way to convert every NFA into an equivalent DFA

• Step 1: Simplify NFA by eliminating -transitions

• Step 2: Convert simplified NFA (without s)

Page 11: CSC 3130: Automata theory and formal languages

Eliminating -transitions

q0 q1 q2

, 1 0

0

NFA:

Transition table of corresponding NFA:

state

s

inputs

0 1q0

q1

q2

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

Accepting states of NFA:q0, q1, q2

Page 12: CSC 3130: Automata theory and formal languages

Eliminating -transitions

q0 q1 q2, 1 0

0

NFA:

NFA without s: q0 q1 q20, 1 0

0

0

0, 1

0

Page 13: CSC 3130: Automata theory and formal languages

Eliminating -transitions: General method• For every state qi and every symbol a ,

replace every path out of qi like

• For every accept state qf, make accepting all states connected to it via s:

q4

qi q5 q2 q0 q5 a qi

q5

a

qi q5a qi

a

q9 q7 q3 qf

Page 14: CSC 3130: Automata theory and formal languages

Regular expressions

Page 15: CSC 3130: Automata theory and formal languages

Operations on strings

• Given two strings s = a1…an and t = b1…bm, we define their concatenation st = a1…anb1…bm

• We define sn as the concatenation ss…s n times

s = abb, t = cba st = abbcba

s = 011 s3 = 011011011

Page 16: CSC 3130: Automata theory and formal languages

Operations on languages

• The concatenation of languages L1 and L2 is

• Similarly, we write Ln for LL…L (n times)

• The union of languages L1 L2 is the set of all strings that are in L1 or in L2

• Example: L1 = {01, 0}, L2 = {, 1, 11, 111, …}. What is L1L2 and L1 L2?

L1L2 = {st: s L1, t L2}

Page 17: CSC 3130: Automata theory and formal languages

Operations on languages

• The star (Kleene closure) of L are all strings made up of zero or more chunks from L:

– This is always infinite, and always contains

• Example: L1 = {01, 0}, L2 = {, 1, 11, 111, …}. What is L1

* and L2*?

L* = L0 L1 L2 …

Page 18: CSC 3130: Automata theory and formal languages

Constructing languages with operations• Let’s fix an alphabet, say = {0, 1}

• We can construct languages by starting with simple ones, like {0}, {1} and combining them

{0}({0}{1})*all strings that start with 0

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

0(0+1)*

01*+10*

Page 19: CSC 3130: Automata theory and formal languages

Regular expressions

• A regular expression over is an expression formed using the following rules:– The symbol is a regular expression– The symbol is a regular expression– For every a , the symbol a is a regular expression– If R and S are regular expressions, so are R+S, RS

and R*.

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

Page 20: CSC 3130: Automata theory and formal languages

Examples

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

= {0, 1}

Page 21: CSC 3130: Automata theory and formal languages

Examples

(0+1)*= {, 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

Page 22: CSC 3130: Automata theory and formal languages

Examples

(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 divisible by 3

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

Page 23: CSC 3130: Automata theory and formal languages

Examples

((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

Page 24: CSC 3130: Automata theory and formal languages

Examples

((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

0011010 011 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

Page 25: CSC 3130: Automata theory and formal languages

Examples

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

ends in at most two 0s

there can be at most two 0s betweenconsecutive 1s

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

there are never three consecutive 0s

0110010110 001001000

Page 26: CSC 3130: Automata theory and formal languages

Examples

• Write a regular expression forall strings with two consecutive 0s.

= {0, 1}

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

(anything) 00 (anything else)

Page 27: CSC 3130: Automata theory and formal languages

Examples

• Write a regular expression forall strings that do not contain two consecutive 0s.

= {0, 1}

... at most one 0 in every block ending in 1

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

(1 + 01)

( + 0)

(1 + 01)*( + 0)

0110101101010blocks ending in 1last block

Page 28: CSC 3130: Automata theory and formal languages

Examples

• Write a regular expression forall strings with an even number of 0s.

= {0, 1}

even number of zeros = (two zeros)*

two zeros = 1*01*01*

(1*01*01*)*

Page 29: CSC 3130: Automata theory and formal languages

Main theorem for regular languages• Theorem

A language is regular if and only if it is the language of some DFA

DFA NFAregular

expression

regular languages

Page 30: CSC 3130: Automata theory and formal languages

Road map

NFAregular

expression

NFAwithout

DFA

Page 31: CSC 3130: Automata theory and formal languages

M2

Examples: regular expression → NFA• R1 = 0

• R2 = 0 + 1

• R3 = (0 + 1)*

q0 q10

q0 q1

q2 q3

0

q4 q51

q0’ q1’M2

Page 32: CSC 3130: Automata theory and formal languages

General method

regular expr NFA

q0

q0

symbol a q0 q1a

RS q0 q1MR MS

Page 33: CSC 3130: Automata theory and formal languages

General method continued

regular expr NFA

R + S q0 q1

MR

MS

R* q0 q1MR

Page 34: CSC 3130: Automata theory and formal languages

Road map

NFAregular

expression

NFAwithout

DFA