Вы находитесь на странице: 1из 291

Big picture

All languages Decidable Turing machines NP P Context-free Context-free grammars, push-down automata Regular Automata, non-deterministic automata, regular expressions

DFA (Deterministic Finite Automata) 0 q0 1 0 0 1 1 0 1 qa

DFA (Deterministic Finite Automata) 0 q0 1 States Transitions 0 0 1 1 0 1 qa

, this DFA has 4 states

labelled with elements of the alphabet = {0,1}

DFA (Deterministic Finite Automata) 0 q0 1 Computation on input w:

1 1 qa

0 0 1 q0

Begin in start state

Read input string in a one-way fashion Follow the arrows matching input symbols When input ends: ACCEPT if in accept state REJECT if not

DFA (Deterministic Finite Automata) 0 q0 1 0 1 Example: Input string w = 0011 0 1 0 1 qa

DFA (Deterministic Finite Automata)


always start in start state

0 1 0 0 1

1 qa

q0 1

Example: Input string w = 0011

DFA (Deterministic Finite Automata) 0 q0 1 0 1 Example: Input string w = 0011 0 1 0 1 qa

DFA (Deterministic Finite Automata) 0 q0 1 0 1 Example: Input string w = 0011 0 1 0 1 qa

DFA (Deterministic Finite Automata) 0 q0 1 0 1 Example: Input string w = 0011 0 1 0 1 qa

DFA (Deterministic Finite Automata) 0 q0 1 0 1 Example: Input string w = 0011 0 1 0 1 qa

DFA (Deterministic Finite Automata) 0 q0 1 0 1 Example: Input string w = 0011 ACCEPT because end in accept state 0 1 0 1 qa

DFA (Deterministic Finite Automata) 0 q0 1 0 1 Example: Input string w = 010 0 1 0 1 qa

DFA (Deterministic Finite Automata)


always start in start state

0 1 0 0 1

1 qa

q0 1

Example: Input string w = 010

DFA (Deterministic Finite Automata) 0 q0 1 0 1 Example: Input string w = 010 0 1 0 1 qa

DFA (Deterministic Finite Automata) 0 q0 1 0 1 Example: Input string w = 010 0 1 0 1 qa

DFA (Deterministic Finite Automata) 0 q0 1 0 1 Example: Input string w = 010 0 1 0 1 qa

DFA (Deterministic Finite Automata) 0 q0 1 0 1 Example: Input string w = 010 REJECT because does not end in accept state 0 1 0 1 qa

DFA (Deterministic Finite Automata) 0 q0 1 0 1 Example: Input string w = 01 w = 010 w = 0011 ACCEPT REJECT ACCEPT 0 1 0 1 qa

w = 00110 REJECT

DFA (Deterministic Finite Automata) M := q0 1 0 1 0 0 1 0 1 qa

M recognizes language

L(M) = { w : w starts with 0 and ends with 1 } L(M) is the language of strings causing M to accept Example: 0101 is an element of L(M), 0101 L(M)

Example M := = {0,1} q0 0

1 1

q1 0

00 causes M to accept, so 00 is in L(M) 00 L(M) 01 does not cause M to accept, so 01 not in L(M), 01 L(M) 0101 011010 L(M) L(M)

01101100 L(M)

Example M := = {0,1} q0 0

1 1

q1 0

L(M) = {w : w has an even number of 1 } Note: If there is no 1, then there are zero 1, zero is an even number, so M should accept. Indeed 0000000 L(M)

Example M := = {0,1} 0 1

L(M) = ?

Example M := = {0,1} 0 1

L(M) = every possible string over {0,1} = {0,1}*

Example = {0,1} M := q0 1

0 1

L(M) = ?

Example = {0,1} M := q0 1

0 1

L(M) = all strings over {0,1} except empty string = {0,1}* - { }

Example = {0,1} M := q0 1 0

0 1 0 0 1 1

L(M) = ?

Example = {0,1} M := q0 1 0

0 1 0 0 1 1

L(M) = { w : w starts and ends with same symbol } Memory is encoded in what ?

Example = {0,1} M := q0 1 0

0 1 0 0 1 1

1 Remember 0

Remember 1 0

L(M) = { w : w starts and ends with same symbol } Memory is encoded in states. DFA have finite states, so finite memory

Convention: M := q0 1 We already saw that 0 1 0 0 1 0 1 qa

L(M) = { w : w starts with 0 and ends with 1 } The arrow q0 1 leads to a sink state. If followed, M can never accept

Convention: M := q0 0 0 1 0 1 qa

Don't need to write such arrows: If, from some state, read symbol with no corresponding arrow, imagine M goes into sink state that is not shown, and REJECT. This makes pictures more compact.

Another convention: List multiple transition on same arrow: 0,1,2

Means 2

0 1

This makes pictures more compact.

Example = {0,1} M= 0,1 0,1

L(M) = ?

Example = {0,1} M= 0,1 0,1

L(M) = 2 = {00,01,10,11}

Example from programming languages: Recognize strings representing numbers: = {0,1,2,3,4,5,6,7,8,9, +, -, . } + 0,...,9 0,...,9 Note: 0,...,9 means 0,1,2,3,4,5,6,7,8,9: 10 transitions 0,...,9 0,...,9

Example from programming languages: Recognize strings representing numbers: = {0,1,2,3,4,5,6,7,8,9, +, -, . } + 0,...,9 Possibly put sign (+, -) Possibly put decimal point Follow with arbitrarily many digits, possibly none 0,...,9 0,...,9 0,...,9

Follow with arbitrarily many digits, but at least one

Example from programming languages: Recognize strings representing numbers: = {0,1,2,3,4,5,6,7,8,9, +, -, . } + 0,...,9 Input w = 17 Input w = + Input w = -3.25 ACCEPT REJECT ACCEPT 0,...,9 0,...,9 0,...,9

Input w = +2.35-. REJECT

Example = {0,1}

What about { w : w has same number of 0 and 1 } Can you design a DFA that recognizes that? It seems you need infinite memory We will prove later that

there is no DFA that recognizes that language !

Next: formal definition of DFA

Useful to prove various properties of DFA Especially important to prove that things CANNOT be recognized by DFA.

Useful to practice mathematical notation

State diagram of a DFA:

One or more states Exactly one start state Some number of accept states Labelled transitions exiting each state, for every symbol in 1

Definition: A finite automaton (DFA) is a 5-tuple (Q, , , q0, F) where Q is a finite set of states is the input alphabet

: Q X Q is the transition function q 0 in Q is the start state


F Q is the set of accept states Q X is the set of ordered pairs (a,b) : a Q, b

Example {q,r,s}X{0,1}={(q,0),(q,1),(r,0),(r,1),(s,0),(s,1)}

q0 0

1 1

q1 0

Example: above DFA is 5-tuple (Q, , , q0, F) where Q = { q0, q1}

= {0,1} (q 0 ,0) = ?

q0 0

1 1

q1 0

Example: above DFA is 5-tuple (Q, , , q0, F) where Q = { q0, q1} (q0 ,1) = ?

= {0,1} (q 0 ,0) = q0

q0 0

1 1

q1 0

Example: above DFA is 5-tuple (Q, , , q0, F) where Q = { q0, q1} (q0 ,1) = q1 (q1 ,1) = q0

= {0,1} (q 0 ,0) = q0

(q1 ,0) = q1

q0 in Q is the start state F=?

q0 0

1 1

q1 0

Example: above DFA is 5-tuple (Q, , , q0, F) where Q = { q0, q1} (q0 ,1) = q1 (q1 ,1) = q0

= {0,1} (q 0 ,0) = q0

(q1 ,0) = q1

q0 in Q is the start state F = { q0} Q is the set of accept states

Definition: A DFA (Q, , , q0, F) accepts a string w if w = w1 w2 wk (the k symbols of w) where, 1 i k, wi is in

sequence of k+1 states r0, r1, .., rk in Q such that: r0 = q0 ri+1 = (ri ,wi+1 ) 0 i < k rk is in F

(ri = state DFA is in after reading i-th symbol in w)

Example

q0 0

1 1

q1 0

Above DFA (Q, , , q0, F) accepts w = 011

Example

q0 0

1 1

q1 0

Above DFA (Q, , , q0, F) accepts w = 011 w = 011 = w1 w2 w3 w 1 = 0 w2 = 1 w 3 = 1

Example

q0 0

1 1

q1 0

Above DFA (Q, , , q0, F) accepts w = 011 w = 011 = w1 w2 w3 w 1 = 0 w2 = 1 w 3 = 1

WE MUST SHOW THAT sequence of 3+1=4 states r , r , r , r 0 1 2 3 in Q


that:

r0 = q0 ri+1 = (ri ,wi+1 ) 0 i < 3 r3 is in F

Example

q0 0

1 1

q1 0

Above DFA (Q, , , q0, F) accepts w = 011 w = 011 = w1 w2 w3 w 1 = 0 w2 = 1 w 3 = 1 consider 4 states r0 := ?

Example

q0 0

1 1

q1 0

Above DFA (Q, , , q0, F) accepts w = 011 w = 011 = w1 w2 w3 r0 = q0 w 1 = 0 w2 = 1 w 3 = 1 OK consider 4 states r0 := q0 r1 := ?

Example

q0 0

1 1

q1 0

Above DFA (Q, , , q0, F) accepts w = 011 w = 011 = w1 w2 w3 r0 = q0 r1 = (r0 ,w1 )=(q0 ,0 ) = q0 w 1 = 0 w2 = 1 w 3 = 1 r2:= ? OK OK consider 4 states r0 := q0 r1 := q0

Example

q0 0

1 1

q1 0

Above DFA (Q, , , q0, F) accepts w = 011 w = 011 = w1 w2 w3 r0 = q0 r1 = (r0 ,w1 )=(q0 ,0 ) = q0 r2 = (r1 ,w2 )=(q0 ,1 ) = q1 w 1 = 0 w2 = 1 w 3 = 1 r2:= q1 OK OK OK r3:= ? consider 4 states r0 := q0 r1 := q0

Example

q0 0

1 1

q1 0

Above DFA (Q, , , q0, F) accepts w = 011 w = 011 = w1 w2 w3 r0 = q0 r1 = (r0 ,w1 )=(q0 ,0 ) = q0 r2 = (r1 ,w2 )=(q0 ,1 ) = q1 r3 = (r2 ,w3 )=(q1 ,1 ) = q0 r3 = q0 in F w 1 = 0 w2 = 1 w 3 = 1 r2:= q1 OK OK OK OK OK DONE! r3:= q0 : consider 4 states r0 := q0 r1 := q0

Definition: For a DFA M, we denote by L(M) the set L(M) := { w : M accepts w}

of strings accepted by M:

We say M accepts or recognizes the language L(M)

Definition: A language L is regular if DFA M : L(M) = L

In the next lectures we want to:

Understand power of regular languages Develop alternate, compact notation to specify

regular languages Example: Unix command grep '\<c.*h\>' file selects all words starting with c and ending with h in file

Understand power of regular languages: Suppose A, B are regular languages, what about not A := { w : w is not in A }

A U B := { w : w in A or w in B } A o B := { w 1 w2 : w1 in A and w2 in B }

A* := { w1 w2 wk : k 0 , wi in A for every i } Are these languages regular?

Understand power of regular languages: Suppose A, B are regular languages, what about not A := { w : w is not in A }

A U B := { w : w in A or w in B } A o B := { w 1 w2 : w1 in A and w2 in B }

A* := { w1 w2 wk : k 0 , wi in A for every i } Terminology: Are regular languages closed under not, U, o, * ?

Theorem: If A is a regular language, then so is (not A)

Theorem: If A is a regular language, then so is (not A)

Proof idea: ?????????? the set of accept states

Theorem: If A is a regular language, then so is (not A)

Proof idea: Complement the set of accept states

Theorem: If A is a regular language, then so is (not A)

Proof idea: Complement the set of accept states

Example: M :=

q0 0
L(M) =

1 1 0

q1

{ w : w has even number of 1}

Theorem: If A is a regular language, then so is (not A)

Proof idea: Complement the set of accept states M' := 1 1 0 q1 q0 0

Example: M :=

1 1 0

q0 0
L(M) =

q1

L(M') = not L(M) = { w : w has odd number of 1}

{ w : w has even number of 1}

Theorem: If A is a regular language, then so is (not A)

Formal construction: Given DFA M = (Q, , , q0, F) such that L(M) = A Construct DFA M' = (Q, , , q0, F') F' := not F

L(M') = not A because M' accepts w M does not accept w

Theorem: If A is a regular language, then so is (not A)

Formal construction: Use formal definition Given DFA M = (Q, , , q0, F) such that L(M) = A of accept to see this! Construct DFA M' = (Q, , , q0, F') F' := not F

L(M') = not A because M' accepts w M does not accept w

Example = {0,1} M= 0,1 0,1

L(M) = 2 = {00,01,10,11} What is a DFA M' : L(M') = not 2 = all strings except those of length 2 ?

Example = {0,1} M' = 0,1 0,1 0,1 0,1

L(M') = not 2 = {0,1}* - {00,01,10,11} Do not forget the convention about the sink state!

Suppose A, B are regular languages, what about not A := { w : w is not in A } REGULAR

A U B := { w : w in A or w in B } A o B := { w 1 w2 : w1 in A and w2 in B }

A* := { w1 w2 wk : k 0 , wi in A for every i } Other three are more complicated!

Plan: we introduce NFA prove that NFA are equivalent to DFA prove A U B, A o B, A* regular, using NFA

Non deterministic finite automata (NFA)

DFA: given state and input symbol, unique choice for next state,

deterministic:

Next we allow multiple choices, non-deterministic

1 1

We also allow -transitions: can follow without reading anything

Example of NFA b a q1

q0 a,b a q2

Intuition of how it computes:

Accept string w if there is a way to follow transitions that ends in accept state Transitions labelled with symbol in = {a,b} must be matched with input transitions can be followed without matching

Example of NFA q0 b a q1 a,b a q2

Example:

Accept a Accept baaa

(first follow -transition )

ANOTHER Example of NFA q1 a,b Example:

b a b q3

b q0

q2

Accept bab Reject ba

(two accepting paths, one uses the -transition) (two possible paths, but neither has final state = q1)

Definition: A non-deterministic finite automaton (NFA) is a 5-tuple (Q, , , q0, F) where Q is a finite set of states is the input alphabet

: Q X ( U {} ) Powerset(Q) q 0 in Q is the start state


F Q is the set of accept states Recall: Powerset(Q) = set of all subsets of Q Example: Powerset({1,2}) = ?

Definition: A non-deterministic finite automaton (NFA) is a 5-tuple (Q, , , q0, F) where Q is a finite set of states is the input alphabet

: Q X ( U {} ) Powerset(Q) q 0 in Q is the start state


F Q is the set of accept states Recall: Powerset(Q) = set of all subsets of Q Example: Powerset({1,2}) = {, {1}, {2}, {1,2} }

q0 0, 1

q1

Example: above NFA is 5-tuple (Q, , , q0, F) where Q = { q0, q1}

= {0,1} (q 0 ,0) = ?

q0 0, 1

q1

Example: above NFA is 5-tuple (Q, , , q0, F) where Q = { q0, q1}

= {0,1} (q 0 ,0) = {q0} (q0 ,1) = ?

q0 0, 1

q1

Example: above NFA is 5-tuple (Q, , , q0, F) where Q = { q0, q1} (q0 ,) = ?

= {0,1} (q 0 ,0) = {q0} (q0 ,1) = {q0, q1}

q0 0, 1

q1

Example: above NFA is 5-tuple (Q, , , q0, F) where Q = { q0, q1} (q0 ,) =

= {0,1} (q 0 ,0) = {q0} (q0 ,1) = {q0, q1}

(q1 ,0) = ?

q0 0, 1

q1

Example: above NFA is 5-tuple (Q, , , q0, F) where Q = { q0, q1} (q0 ,) =

= {0,1} (q 0 ,0) = {q0} (q0 ,1) = {q0, q1}

(q1 ,0) =

(q1 ,1) = ?

q0 0, 1

q1

Example: above NFA is 5-tuple (Q, , , q0, F) where Q = { q0, q1} (q0 ,) = (q1 ,) = ?

= {0,1} (q 0 ,0) = {q0} (q0 ,1) = {q0, q1}

(q1 ,0) =

(q1 ,1) =

q0 0, 1

q1

Example: above NFA is 5-tuple (Q, , , q0, F) where Q = { q0, q1} (q0 ,) = (q1 ,) = {q1}

= {0,1} (q 0 ,0) = {q0} (q0 ,1) = {q0, q1}

(q1 ,0) =

(q1 ,1) =

q0 in Q is the start state F=?

q0 0, 1

q1

Example: above NFA is 5-tuple (Q, , , q0, F) where Q = { q0, q1} (q0 ,) = (q1 ,) = {q0}

= {0,1} (q 0 ,0) = {q0} (q0 ,1) = {q0, q1}

(q1 ,0) =

(q1 ,1) =

q0 in Q is the start state F = { q1} Q is the set of accept states

Definition: A NFA (Q, , , q0, F) accepts a string w if integer k, k strings w1 , w2 , , wk such that w = w1 w2 wk where 1 i k, wi U {}

(the symbols of w, or )

sequence of k+1 states r0, r1, .., rk in Q such that: r0 = q0 ri+1

(ri ,wi+1 ) 0 i < k

rk is in F

Differences with DFA are in green

Back to first example NFA: b a q1

q0 a,b a q2

Accepts w = baaa w1 = b, w2 = a, w3 = a, w4= , w5 = a Accepting sequence of 5+1 = 6 states: r0 = ?

Back to first example NFA: b a q1

q0 a,b a q2

Accepts w = baaa w1 = b, w2 = a, w3 = a, w4= , w5 = a Accepting sequence of 5+1 = 6 states: r0 = q0, r1 = ?

Back to first example NFA: b a q1

q0 a,b a q2

Accepts w = baaa w1 = b, w2 = a, w3 = a, w4= , w5 = a Accepting sequence of 5+1 = 6 states: r0 = q0, r1 = q1, r2 = ? Transitions: r1 (r0,b) = {q1}

Back to first example NFA: b a q1

q0 a,b a q2

Accepts w = baaa w1 = b, w2 = a, w3 = a, w4= , w5 = a Accepting sequence of 5+1 = 6 states: r0 = q0, r1 = q1, r2 = q2, r3 = ? Transitions: r1 (r0,b) = {q1} r2 (r1,a) = {q1,q2}

Back to first example NFA: b a q1

q0 a,b a q2

Accepts w = baaa w1 = b, w2 = a, w3 = a, w4= , w5 = a Accepting sequence of 5+1 = 6 states: r0 = q0, r1 = q1, r2 = q2, r3 = q0, r4 = ? Transitions: r1 (r0,b) = {q1} r3 (r2,a) = {q0} r2 (r1,a) = {q1,q2}

Back to first example NFA: b a q1

q0 a,b a q2

Accepts w = baaa w1 = b, w2 = a, w3 = a, w4= , w5 = a Accepting sequence of 5+1 = 6 states: r0 = q0, r1 = q1, r2 = q2, r3 = q0, r4 = q2, r5 = ? Transitions: r1 (r0,b) = {q1} r3 (r2,a) = {q0} r2 (r1,a) = {q1,q2} r4 (r3,) = {q2}

Back to first example NFA: b a q1

q0 a,b a q2

Accepts w = baaa w1 = b, w2 = a, w3 = a, w4= , w5 = a Accepting sequence of 5+1 = 6 states: r0 = q0, r1 = q1, r2 = q2, r3 = q0, r4 = q2, r5 = q0 Transitions: r1 (r0,b) = {q1} r3 (r2,a) = {q0} r2 (r1,a) = {q1,q2} r4 (r3,) = {q2} r5 (r4,a) = {q0}

NFA are at least as powerful as DFA, because DFA are a special case of NFA

Are NFA more powerful than DFA? Surprisingly, they are not: Theorem:

For every NFA N there is DFA M : L(M) = L(N)

Theorem:

For every NFA N there is DFA M : L(M) = L(N)

Construction without transitions Given NFA N (Q, , , q, F) Construct DFA M (Q', , ', q', F') where: Q' := Powerset(Q) q' = {q}

F' = { S : S Q' and S contains an element of F} '(S, a) := U s S (s,a) = { t : t (s,a) for some s S }

It remains to deal with transitions Definition: Let S be a set of states. s in S traveling along 0 or more transitions }

E(S) := { q : q can be reached from some state

We think of following transitions at beginning, or

right after reading an input symbol in

Theorem:

For every NFA N there is DFA M : L(M) = L(N)

Construction including transitions Given NFA N (Q, , , q, F) Construct DFA M (Q', , ', q', F') where: Q' := Powerset(Q) q' = E({q})

F' = { S : S Q' and S contains an element of F} '(S, a) := E( U s S (s,a) ) = { t : t E( (s,a) ) for some s S }

Example: NFA DFA conversion NFA DFA

{1,3}

{3}

a
2

b a,b

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

QDFA = Powerset(QNFA) = Powerset({1,2,3}) = {,{1},{2},{3},{1,2}...}


{1}
{1,2}

Example: NFA DFA conversion NFA DFA

{1,3}

{3}

a
2

b a,b

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

qDFA = E({qNFA}) = E({1}) = {1,3}


{1}
{1,2}

Example: NFA DFA conversion NFA DFA

{1,3}

{3}

a
2

b a,b

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

FDFA = {S : S contains an element of FNFA}


{1}
{1,2}

Example: NFA DFA conversion NFA DFA

{1,3}

{3}

a
2

b a,b

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

DFA({1}, a) = E(FA(1, a)) = E() =


{1}
{1,2}

Example: NFA DFA conversion NFA DFA

{1,3}

{3}

a
2

b a,b

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

DFA({1}, b) = E(FA(1, b)) = E({2}) = {2}

b
{1}
{1,2}

Example: NFA DFA conversion NFA DFA

{1,3}

{3}

a
2

b a,b

a
3 {2}

a b
{1}

{2,3}

{1,2,3}

DFA({2}, a) = E(FA(2, a)) = E({2,3}) = {2,3}

{1,2}

Example: NFA DFA conversion NFA DFA

{1,3}

{3}

a
2

b a,b

a
3 {2}

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

DFA({2}, b) = E(FA(2, b)) = E({3}) = {3}

Example: NFA DFA conversion NFA DFA a b


3 {2}

{1,3}

{3}

a
2

b a,b

a b
{1}

{2,3}

{1,2,3}

DFA({3}, a) = E(FA(3, a)) = E({1}) = {1,3}

{1,2}

Example: NFA DFA conversion NFA DFA a b


3 {2}

{1,3}

{3}

a
2

b a,b

a b
{1}

{2,3}

{1,2,3}

DFA({3}, b) = E(FA(3, b)) = E() =

{1,2}

Example: NFA DFA conversion NFA DFA a b


3 {2}

{1,3}

{3}

a
2

b a,b

a b
{1}

{2,3}

DFA({2,3}, a) = E(FA(2,a) U FA(3,a)) = E({2,3} U {1}) = {1,2,3}

a
{1,2}

{1,2,3}

Example: NFA DFA conversion NFA DFA a b


3 {2}

{1,3}

{3}

a
2

b a,b

b
{2,3}

a b
{1}

DFA({2,3}, b) = E(FA(2,b) U FA(3,b)) = E({3} U ) = {3}

a
{1,2}

{1,2,3}

Example: NFA DFA conversion NFA a


{1,3}

DFA a b b

{3}

a
2

b a,b

a
3 {2}

b
{2,3}

a b
{1}

DFA({1,3}, a) = E(FA(1,a) U FA(3,a)) = E( U {1}) = {1,3}

a
{1,2}

{1,2,3}

Example: NFA DFA conversion NFA a


{1,3}

DFA a b a b
{1}
{1,2}

{3}

a
2

b a,b

a
3

b
{2}

b
{2,3}

DFA({1,3}, b) = E(FA(1,b) U FA(3,b)) = E({2} U ) = {2}

{1,2,3}

Example: NFA DFA conversion NFA a


{1,3}

DFA a b a b
{1}

{3}

a
2

b a,b

a
3

b
{2}

b
{2,3}

DFA({1,2}, a) = E(FA(1,a) U FA(2,a)) = E( U {2,3}) = {2,3}

a a
{1,2}

{1,2,3}

Example: NFA DFA conversion NFA a


{1,3}

DFA a b a b
{1}

{3}

a
2

b a,b

a
3

b
{2}

b
{2,3}

DFA({1,2}, b) = E(FA(1,b) U FA(2,b)) = E({2} U {3}) = {2,3}

a a,b
{1,2}

{1,2,3}

Example: NFA DFA conversion NFA a


{1,3}

DFA a b a b
{1}

{3}

a
{1,2,3}

a
2

b a,b

a
3

b
{2}

b
{2,3}

DFA({1,2,3}, a) =E(
FA

(1,a) U FA(2,a) U FA(3,a))

a a,b
{1,2}

=E( U {2,3} U {1}) = {1,2,3}

Example: NFA DFA conversion NFA a


{1,3}

DFA a b a b
{1}

{3}

a
2

b a,b

a
3

b
{2}

b b
{2,3}

DFA({1,2,3}, b) =E(
FA

(1,b) U FA(2,b) U FA(3,b))

a a,b
{1,2}

{1,2,3}

=E({2} U {3} U ) = {2,3}

Example: NFA DFA conversion NFA a


{1,3}

DFA a b a b
{1}

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

{3}

a
2

b a,b

a
3

b
{2}

b
{2,3}

DFA(, a) = DFA(, b) =

a a,b
{1,2}

Example: NFA DFA conversion NFA a


{1,3}

DFA a b a b

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

{3}

a
2

b a,b

a
3

b
{2}

b
{2,3}

We can delete the unreachable states.

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

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

QDFA = Powerset(QNFA) = Powerset({1,2,3}) = {,{1},{2},{3},{1,2}...}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

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

qDFA = E({qNFA}) = E({1}) = {1}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

a
{2} {1,2,3}

FDFA = {S : S contains an element of FNFA} {2,3}

{1,3}

{1,2}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

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

DFA({1}, a) = E(FA(1, a)) = E() =

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

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

b
{1,2,3}

DFA({1}, b) = E(FA(1, b)) = E({2,3}) = {1,2,3}

{1,2}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

a
{2} {2,3}

b
{1,2,3}

DFA({2}, a) = E(FA(2, a)) = E({3}) = {1,3}

a
{1,3}

{1,2}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

a b
{2} {2,3}

b
{1,2,3}

DFA({2}, b) = E(FA(2, b)) = E() =

a
{1,3}

{1,2}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

a b
{2} {2,3}

b
{1,2,3}

DFA({3}, a) = E(FA(3, a)) = E() =

a
{1,3}

{1,2}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

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

b
{1,2,3}

DFA({3}, b) = E(FA(3, b)) = E() =

a
{1,3}

{1,2}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

a,b b
{2}

b
{1,2,3}

DFA({1,2}, a) = E(FA(1,a) U FA(2,a)) {2,3} = E( U {3}) = {1,3}

a
{1,3}

{1,2}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

a,b b
{2}

b
{1,2,3}

b a
{1,3}

DFA({1,2}, b) = E(FA(1,b) U FA(2,b)) {2,3} = E({2,3} U ) = {1,2,3}

{1,2}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

a,b b
{2}

b
{1,2,3}

DFA({1,3}, a) = E(FA(1,a) U FA(3,a)) {2,3} = E( U ) =

a
{1,3}

b a
{1,2}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

a,b b
{2}

b
{1,2,3}

DFA({1,3}, b) = E(FA(1,b) U FA(3,b)) {2,3} = E({2,3} U ) = {1,2,3}

b
{1,3}

b a
{1,2}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

a,b b
{2}

b
{1,2,3}

DFA({2,3}, a) = E(FA(2,a) U FA(3,a)) {2,3} = E({3} U ) = {1,3}

b
{1,3}

b a
{1,2}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

a,b b b
{2}

b
{1,2,3}

DFA({2,3}, b) = E(FA(2,b) U FA(3,b)) {2,3} = E( U ) =

b
{1,3}

b a
{1,2}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

a,b b b
{2}

b
{1,2,3}

DFA({1,2,3}, a) =E(
(1,a) U FA(2,a) U FA(3,a)) FA

b
{1,3}

a a

b
{1,2}

{2,3}

=E( U {3} U ) = {1,3}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

a,b b b
{2}

b
{1,2,3}

DFA({1,2,3}, b) =E(
(1,b) U FA(2,b) U FA(3,b)) FA

b
{1,3}

a a

b
{1,2}

{2,3}

=E({2,3} U U ) = {1,2,3}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{3} 2 {1}

b b
3

a,b b b
{2}

a a,b a b
{1,3}

b
{1,2,3}

a a

b
{1,2}

DFA(, a) = DFA(, b) =

{2,3}

ANOTHER Example: NFA DFA conversion NFA


1

DFA
{1} 2

b b
3

a a,b a b
{1,3}

b
{1,2,3}

We can delete the unreachable states.

Summary: NFA and DFA recognize the same languages We now return to the question:

Suppose A, B are regular languages, what about not A := { w : w is not in A } REGULAR

A U B := { w : w in A or w in B } A o B := { w 1 w2 : w1 in A and w2 in B }

A* := { w1 w2 wk : k 0 , wi in A for every i }

Theorem: If A, B are regular languages, then so is A U B := { w : w in A or w in B }

Proof idea: Given DFA MA : L(MA) = A, DFA MB : L(MB) = B, Construct NFA N : L(N) = A U B MA U MB = N

MA U

MB =

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, DFA MB = (QB, , B, qB, FB) : L(MB) = B,

Construct NFA N = (Q, , , q, F) where: Q := ?

MA U

MB =

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, DFA MB = (QB, , B, qB, FB) : L(MB) = B, Construct NFA N = (Q, , , q, F) where: Q := {q} U Q U Q A B , F := ?

MA U

MB =

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, DFA MB = (QB, , B, qB, FB) : L(MB) = B, Construct NFA N = (Q, , , q, F) where: Q := {q} U Q U Q A B , F := FA U FB

(r,x) := { A(r,x) } if r in QA and x (r,x) := ? if r in QB and x

MA U

MB =

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, DFA MB = (QB, , B, qB, FB) : L(MB) = B, Construct NFA N = (Q, , , q, F) where: Q := {q} U Q U Q A B , F := FA U FB

(r,x) := { A(r,x) } if r in QA and x (r,x) := { B(r,x) } if r in QB and x (q,) := ?

MA U

MB =

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, DFA MB = (QB, , B, qB, FB) : L(MB) = B, Construct NFA N = (Q, , , q, F) where: Q := {q} U Q U Q A B , F := FA U FB

(r,x) := { A(r,x) } if r in QA and x (r,x) := { B(r,x) } if r in QB and x (q,) := {qA, qB} We have L(N) = A U B

Example Is L = {w in {0,1}* : |w| is divisible by 3 OR w starts with a 1} regular?

Example Is L = {w in {0,1}* : |w| is divisible by 3 OR w starts with a 1} OR is like U, so try to write L = L1 U L2 where L1, L2 are regular regular?

Example Is L = {w in {0,1}* : |w| is divisible by 3 OR w starts with a 1} OR is like U, so try to write L = L1 U L2 where L1, L2 are regular L1 = {w : |w| is div. by 3} L2 = {w : w starts with a 1} regular?

Example Is L = {w in {0,1}* : |w| is divisible by 3 OR w starts with a 1} OR is like U, so try to write L = L1 U L2 where L1, L2 are regular L1 = {w : |w| is div. by 3} M1 = 0,1 0,1 0,1 L2 = {w : w starts with a 1} regular?

L(M1) = L1

Example Is L = {w in {0,1}* : |w| is divisible by 3 OR w starts with a 1} OR is like U, so try to write L = L1 U L2 where L1, L2 are regular L1 = {w : |w| is div. by 3} M1 = 0,1 0,1 0,1 L(M2) = L2 L2 = {w : w starts with a 1} M2 = 1 0,1 regular?

L(M1) = L1

Example Is L = {w in {0,1}* : |w| is divisible by 3 OR w starts with a 1} OR is like U, so try to write L = L1 U L2 where L1, L2 are regular L1 = {w : |w| is div. by 3} M= 0,1 1 0,1 0,1 L2 = {w : w starts with a 1} 0,1 L(M) = L(M1) U L(M2) = L 1 U L2 =L L is regular. regular?

We now return to the question:

Suppose A, B are regular languages, then not A := { w : w is not in A } REGULAR

A U B := { w : w in A or w in B } REGULAR A o B := { w 1 w2 : w1 in A and w2 in B }

A* := { w1 w2 wk : k 0 , wi in A for every i }

Theorem: If A, B are regular languages, then so is A o B := { w : w = xy for some x in A and y in B }. Proof idea: Given DFAs M , M A B for A, B MA construct NFA N : L(N) = A o B. MB o N =

MA o

MB

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, DFA MB = (QB, , B, qB, FB) : L(MB) = B,

Construct NFA N = (Q, , , q, F) where: Q := ?

MA o

MB

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, DFA MB = (QB, , B, qB, FB) : L(MB) = B, Construct NFA N = (Q, , , q, F) where: Q := Q U Q A B , q := ?

MA o

MB

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, DFA MB = (QB, , B, qB, FB) : L(MB) = B, Construct NFA N = (Q, , , q, F) where: Q := Q U Q A B , q := qA , F := ?

MA o

MB

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, DFA MB = (QB, , B, qB, FB) : L(MB) = B, Construct NFA N = (Q, , , q, F) where: Q := Q U Q A B , q := qA , F := FB

(r,x) := ?

if r in QA and x

MA o

MB

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, DFA MB = (QB, , B, qB, FB) : L(MB) = B, Construct NFA N = (Q, , , q, F) where: Q := Q U Q A B , q := qA , F := FB

(r,x) := { A(r,x) } if r in QA and x (r,) := ? if r in FA

MA o

MB

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, DFA MB = (QB, , B, qB, FB) : L(MB) = B, Construct NFA N = (Q, , , q, F) where: Q := Q U Q A B , q := qA , F := FB

(r,x) := { A(r,x) } if r in QA and x (r,) := { q B } if r in FA


(r,x) := ?

if r in QB and x

MA o

MB

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, DFA MB = (QB, , B, qB, FB) : L(MB) = B, Construct NFA N = (Q, , , q, F) where: Q := Q U Q A B , q := qA , F := FB

(r,x) := { A(r,x) } if r in QA and x (r,) := { q B } if r in FA


(r,x) := { B(r,x) } if r in QB and x We have L(N) = A o B

Example Is L = {w in {0,1}* : w contains a 1 after a 0} regular? Note: L = {01, 0001001, 111001, }

Example Is L = {w in {0,1}* : w contains a 1 after a 0} regular? Let L0 = {w : w contains a 0} L1 = {w : w contains a 1}. Then L = L0 o L1.

Example Is L = {w in {0,1}* : w contains a 1 after a 0} regular? Let L0 = {w : w contains a 0} L1 = {w : w contains a 1}. M0 = 1 0 L(M0) = L0 0,1 Then L = L0 o L1.

Example Is L = {w in {0,1}* : w contains a 1 after a 0} regular? Let L0 = {w : w contains a 0} L1 = {w : w contains a 1}. M0 = 1 0 L(M0) = L0 0,1 Then L = L0 o L1. M1 = 0 1 L(M1) = L1 0,1

Example Is L = {w in {0,1}* : w contains a 1 after a 0} regular? Let L0 = {w : w contains a 0} L1 = {w : w contains a 1}. M = 1 0 0,1 0 1 Then L = L0 o L1. 0,1

L(M) = L(M0) o L(M1) = L0 o L1 = L L is regular.

We now return to the question:

Suppose A, B are regular languages, then not A := { w : w is not in A } REGULAR

A U B := { w : w in A or w in B } REGULAR A o B := { w 1 w2 : w1 A and w2 B } REGULAR


A* := { w1 w2 wk : k 0 , wi in A for every i }

Theorem: If A is a regular language, then so is A* := { w : w = w1...wk, wi in A for i=1,...,k }

Proof idea: Given DFA MA : L(MA) = A, Construct NFA N : L(N) = A* MA N

*
=

MA

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, Construct NFA N = (Q, , , q, F) where:

Q := ?

MA

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, Construct NFA N = (Q, , , q, F) where: Q := {q} U Q , F := ? A

MA

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, Construct NFA N = (Q, , , q, F) where: Q := {q} U Q , F := {q} U F A A

(r,x) := ?

if r in QA and x

MA

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, Construct NFA N = (Q, , , q, F) where: Q := {q} U Q , F := {q} U F A A

(r,x) := { A(r,x) } if r in QA and x (r,) := ? if r in {q} U FA

MA

Construction: Given DFA M A = (QA, , A, qA, FA) : L(MA) = A, Construct NFA N = (Q, , , q, F) where: Q := {q} U Q , F := {q} U F A A

(r,x) := { A(r,x) } if r in QA and x (r,) := { qA } if r in {q} U FA We have L(N) = A*

Example Is L = {w in {0,1}* : w has even length} regular?

Example Is L = {w in {0,1}* : w has even length} regular? Let L0 = {w : w has length = 2}. Then L = L0*.

Example Is L = {w in {0,1}* : w has even length} regular? Let L0 = {w : w has length = 2}. Then L = L0*. M0 = 0,1 0,1 L(M0) = L0

Example Is L = {w in {0,1}* : w has even length} regular? Let L0 = {w : w has length = 2}. Then L = L0*. M = 0,1 0,1

L(M) = L(M0)* = L0* = L L is regular.

We now return to the question:

Suppose A, B are regular languages, then not A := { w : w is not in A }

A U B := { w : w in A or w in B } A o B := { w 1 w2 : w1 in A and w2 in B }

A* := { w1 w2 wk : k 0 , wi in A for every i }

are all regular!

We now return to the question:

Suppose A, B are regular languages, then not A := { w : w is not in A }

A U B := { w : w in A or w in B } A o B := { w 1 w2 : w1 in A and w2 in B }

A* := { w1 w2 wk : k 0 , wi in A for every i }

What about A B := { w : w in A and w in B } ?

We now return to the question:

Suppose A, B are regular languages, then not A := { w : w is not in A }

A U B := { w : w in A or w in B } A o B := { w 1 w2 : w1 in A and w2 in B }

A* := { w1 w2 wk : k 0 , wi in A for every i }

De Morgan's laws: A B = not ( (not A) U (not B) ) By above, (not A) is regular, (not B) is regular, (not A) U (not B) is regular, not ( (not A) U (not B) ) = A B regular

We now return to the question:

Suppose A, B are regular languages, then not A := { w : w is not in A }

A U B := { w : w in A or w in B } A o B := { w 1 w2 : w1 in A and w2 in B }

A* := { w1 w2 wk : k 0 , wi in A for every i } A B := { w : w in A and w in B }

are all regular

How to specify a regular language? Write a picture complicated

Write down formal definition complicated (q0 ,0) = q0, Use symbols from and operations *, o, U good ({0} * U {1}) o {001}

Regular expressions: anything you can write with , , symbols from , and operations *, o, U Conventions:

Write a instead of {a} So if = {a,b} then = a U b

Write AB for A o B Write for U a a


Operation * has precedence over o, and o over U

so 1 U 01* means 1U(0(1)*) Example: 110, 0*, *, *001*, ()*, 01 U 10

Definition Regular expressions RE over are: a R R' R U R' R* if a in if R, R' are RE if R, R' are RE if R is RE

Definition The language described by RE: L() = L() = {} L(a) = {a} L(R R') = L(R) o L(R') L(R U R') = L(R) U L(R') L(R*) = L(R)* if a in

Example = { a, b} RE

Language ?

ab U ba a* (a U b)* a*ba* *b* *aab* ()* (a*ba*ba*)* a*baba*a

Example = { a, b} RE

Language {ab, ba}

ab U ba a* (a U b)* a*ba* *b* *aab* ()* (a*ba*ba*)* a*baba*a

Example = { a, b} RE

Language {ab, ba} {, a, aa, } = { w : w has only a}

ab U ba a* (a U b)* a*ba* *b* *aab* ()* (a*ba*ba*)* a*baba*a

Example = { a, b} RE

Language {ab, ba} {, a, aa, } = { w : w has only a} all strings

ab U ba a* (a U b)* a*ba* *b* *aab* ()* (a*ba*ba*)* a*baba*a

Example = { a, b} RE

Language {ab, ba} {, a, aa, } = { w : w has only a} all strings {w : w has exactly one b}

ab U ba a* (a U b)* a*ba* *b* *aab* ()* (a*ba*ba*)* a*baba*a

Example = { a, b} RE

Language {ab, ba} {, a, aa, } = { w : w has only a} all strings {w : w has exactly one b} {w : w has at least one b}

ab U ba a* (a U b)* a*ba* *b* *aab* ()* (a*ba*ba*)* a*baba*a

Example = { a, b} RE

Language {ab, ba} {, a, aa, } = { w : w has only a} all strings {w : w has exactly one b} {w : w has at least one b} {w : w contains the string aab}

ab U ba a* (a U b)* a*ba* *b* *aab* ()* (a*ba*ba*)* a*baba*a

Example = { a, b} RE

Language {ab, ba} {, a, aa, } = { w : w has only a} all strings {w : w has exactly one b} {w : w has at least one b} {w : w contains the string aab} {w : w has even length}

ab U ba a* (a U b)* a*ba* *b* *aab* ()* (a*ba*ba*)* a*baba*a

Example = { a, b} RE

Language {ab, ba} {, a, aa, } = { w : w has only a} all strings {w : w has exactly one b} {w : w has at least one b} {w : w contains the string aab} {w : w has even length} {w : w contains even number of b}

ab U ba a* (a U b)* a*ba* *b* *aab* ()* (a*ba*ba*)* a*baba*a

Example = { a, b} RE

Language {ab, ba} {, a, aa, } = { w : w has only a} all strings {w : w has exactly one b} {w : w has at least one b} {w : w contains the string aab} {w : w has even length} {w : w contains even number of b} (anything o = )

ab U ba a* (a U b)* a*ba* *b* *aab* ()* (a*ba*ba*)* a*baba*a

Theorem: For every RE R there is NFA M: L(M) = L(R)

Theorem: For every RE R there is NFA M: L(M) = L(R) Construction:

R=

M := ?

Theorem: For every RE R there is NFA M: L(M) = L(R) Construction:

R= R=

M := M := ?

Theorem: For every RE R there is NFA M: L(M) = L(R) Construction:

R= R= R=a

M := M := M := ?

Theorem: For every RE R there is NFA M: L(M) = L(R) Construction:

R= R= R=a

M := M := M :=

R = R U R' ?

Theorem: For every RE R there is NFA M: L(M) = L(R) Construction:

R= R= R=a

M := M := M :=

R = R U R' use construction for A U B seen earlier R = R o R' ?

Theorem: For every RE R there is NFA M: L(M) = L(R) Construction:

R= R= R=a

M := M := M :=

R = R U R' use construction for A U B seen earlier R = R o R' use construction for A o B seen earlier R = R* ?

Theorem: For every RE R there is NFA M: L(M) = L(R) Construction:

R= R= R=a

M := M := M :=

R = R U R' use construction for A U B seen earlier R = R o R' use construction for A o B seen earlier R = R* use construction for A* seen earlier

Example: RE NFA

RE = (ab U a)*

Example: RE NFA

RE = (ab U a)*

Ma =

L(Ma)=L(a)

Example: RE NFA

RE = (ab U a)*

Ma =

Mb =

L(Ma)=L(a)

L(Mb)=L(b)

Example: RE NFA

RE = (ab U a)*

Mab =
a

L(Mab)=L(ab)

Example: RE NFA

RE = (ab U a)*

Mab =
a

Ma =
b

L(Mab)=L(ab)

L(Ma)=L(a)

Example: RE NFA

RE = (ab U a)*

Mab U a =
a a b

L(Mab U a)=L(ab U a)

Example: RE NFA

RE = (ab U a)*

M(ab U a)* =
a a

L(M(ab U a)*)=L((ab U a)*)=L(RE)

ANOTHER Example: RE NFA

RE =( U a)ba*

ANOTHER Example: RE NFA

RE =( U a)ba*

M = L(M)=L()

ANOTHER Example: RE NFA

RE =( U a)ba*

M = L(M)=L()

Ma =

L(Ma)=L(a)

ANOTHER Example: RE NFA

RE =( U a)ba*

M U a =
a

L(M U a)=L( U a)

ANOTHER Example: RE NFA

RE =( U a)ba*

M U a =
a

Mb =

L(Mb)=L(b)

L(M U a)=L( U a)

ANOTHER Example: RE NFA

RE =( U a)ba*

M( U a)b =
a b

L(M( U a)b)=L(( U a)b)

ANOTHER Example: RE NFA

RE =( U a)ba*

M( U a)b =
a b

Ma =

L(Ma)=L(a)

L(M( U a)b)=L(( U a)b)

ANOTHER Example: RE NFA

RE =( U a)ba*

M( U a)b =
a b

Ma* =

L(Ma*)=L(a*)

L(M( U a)b)=L(( U a)b)

ANOTHER Example: RE NFA

RE =( U a)ba*

M( U a)ba* =
a b a

L(M( U a)ba*)=L(( U a)ba*)=L(RE)

Recap: Here means can be converted to We have seen: RE NFA DFA Next we see: In two steps: DFA RE DFA Generalized NFA RE

Generalized NFA (GNFA) a U b* q0 Nondeterministic Transitions labelled by RE Read blocks of input symbols at a time a*b* ab qa

Generalized NFA (GNFA) a U b* q0 Convention: Unique final state Exactly one transition between each pair of states except nothing going into start state nothing going out of final state If arrow not shown in picture, label = a*b* ab qa

Definition: A generalized finite automaton (GNFA) is a 5-tuple (Q, , , q , q ) where 0 a


Q is a finite set of states

is the input alphabet : (Q - {q }) X (Q {q }) Regular Expressions a 0

q0 in Q is the start state qa in Q is the accept state

Definition: GNFA (Q, , , q0, qa) accepts a string w if integer k, k strings w1 , w2 , , wk * such that w = w1 w2 wk (divide w in k strings)

sequence of k+1 states r0, r1, .., rk in Q such that: r0 = q0 wi+1 L((ri ,ri+1 )) 0 i < k r k = qa

Differences with NFA are in green

Example q0 a* q1

b* ab qa

Accepts w = aaabbab w1=?

Example q0 a* q1

b* ab qa

Accepts w = aaabbab w1=aaa w2=?

Example q0 a* q1

b* ab qa

Accepts w = aaabbab w1=aaa w2=bb w3=ab r0=q0 r1=?

Example q0 a* q1

b* ab qa

Accepts w = aaabbab w1=aaa w2=bb w3=ab r0=q0 r1=q1 r2=? w1 = aaa L((r0,r1)) = L((q0,q1)) = L(a*)

Example q0 a* q1

b* ab qa

Accepts w = aaabbab w1=aaa w2=bb w3=ab r0=q0 r1=q1 r2=q1 r3 = ? w1 = aaa L((r0,r1)) = L((q0,q1)) = L(a*) w2 = bb L((r1,r2)) = L((q1,q1)) = L(b*)

Example q0 a* q1

b* ab qa

Accepts w = aaabbab w1=aaa w2=bb w3=ab r0=q0 r1=q1 r2=q1 r3 = qa w1 = aaa L((r0,r1)) = L((q0,q1)) = L(a*) w2 = bb L((r1,r2)) = L((q1,q1)) = L(b*) w3 = ab L((r2,r3)) = L((q1,qa)) = L(ab)

Theorem: DFA M GNFA N : L(N) = L(M) Construction: To ensure unique transition between each pair: 1 0 To ensure unique final state, no transitions ingoing start state, no transitions outgoing final state: 1U0

Theorem: GNFA N RE R : L(R) = L(N) Construction: If N has 2 states, then N = thus R := S q0 S qa

If N has > 2 states, eliminate some state qr q0, qa : for every ordered pair qi, qj (possibly equal) that are connected through qr R2 R1R2*R3 U R4 R3 R1 qi qr qj qi qj R4 Repeat until 2 states remain

Example: DFA GNFA RE

DFA

a
q1

b b,c
q2

Example: DFA GNFA RE

GNFA
q0

a
q1

b bUc
q2

qa

Example: DFA GNFA RE a


q0

b bUc
q2

q1

qa

Eliminate q1: re-draw GNFA with all other states b


q0 q2

qa

Example: DFA GNFA RE a


q0

b bUc
q2

q1

qa

Eliminate q1: find a path through q1 b


q0 q2

qa

Example: DFA GNFA RE a


q0

b bUc
q2

q1

qa

Eliminate q1: add edge to new GNFA b


q0

a* (b U c)

q2

qa

Example: DFA GNFA RE a


q0

b bUc
q2

q1

qa

Eliminate q1: simplify RE on new edge b


q0

a* (b U c)

q2

qa

Example: DFA GNFA RE a


q0

b bUc
q2

q1

qa

Eliminate q1: if no more paths through q1, start over b


q0

a* (b U c)

q2

qa

Example: DFA GNFA RE b


q0

a* (b U c)

q2

qa

Eliminate q2: re-draw GNFA with all other states

q0

qa

Example: DFA GNFA RE b


q0

a* (b U c)

q2

qa

Eliminate q2: find a path through q2

q0

qa

Example: DFA GNFA RE b


q0

a* (b U c)

q2

qa

Eliminate q2: add edge to new GNFA

q0

a* (b U c) b*

qa

Example: DFA GNFA RE b


q0

a* (b U c)

q2

qa

Eliminate q2: simplify RE on new edge

q0

a* (b U c) b*

qa

Example: DFA GNFA RE b


q0

a* (b U c)

q2

qa

Eliminate q2: if no more paths through q2, start over

q0

a* (b U c) b*

qa

Example: DFA GNFA RE

q0

a* (b U c) b*

qa

Only two states remain:

RE = a* (b U c) b*

ANOTHER Example: DFA GNFA RE a

DFA

q1

b c
q2

q3

a b

ANOTHER Example: DFA GNFA RE a

GNFA
q0

q1

b c
q2

q3

qa

a b

ANOTHER Example: DFA GNFA RE a


q0

q1

b c
q2

q3

qa

Eliminate q1: all other states

a b

re-draw GNFA with

q0 q2

q3

qa

a b

ANOTHER Example: DFA GNFA RE a


q0

q1

b c
q2

q3

qa

Eliminate q1: find a path through q1

a b

q0 q2

q3

qa

a b

ANOTHER Example: DFA GNFA RE a


q0

q1

b c
q2

q3

qa

Eliminate q1: add edge to new GNFA

a b a*b
q2

q0

q3

qa

a b

ANOTHER Example: DFA GNFA RE a


q0

q1

b c
q2

q3

qa

Eliminate q1: find another path through q1

a b a*b
q2

q0

q3

qa

a b

ANOTHER Example: DFA GNFA RE a


q0

q1

b c
q2

q3

qa

Eliminate q1: add edge to new GNFA

a b a*b

q0

q3 q2

qa

a*c

a b

ANOTHER Example: DFA GNFA RE a


q0

q1

b c
q2

q3

qa

Eliminate q1: find another path through q1

a b a*b

q0

q3 q2

qa

a*c

a b

ANOTHER Example: DFA GNFA RE a


q0

q1

b c
q2

q3

qa

Eliminate q1: add edge to new GNFA

a b a*b

don't forget current q2 q3 edge!

q0

q3 q2

qa

a*c

a U ca*b b

ANOTHER Example: DFA GNFA RE a


q0

q1

b c
q2

q3

qa

Eliminate q1: find another path through q1

a b a*b

q0

q3 q2

qa

a*c

a U ca*b b

ANOTHER Example: DFA GNFA RE a


q0

q1

b c
q2

q3

qa

Eliminate q1: add edge to new GNFA

a b a*b

don't forget current q2 q2 edge!

q0

q3 q2

qa

a*c

a U ca*b b U ca*c

ANOTHER Example: DFA GNFA RE a


q0

q1

b c
q2

q3

qa

Eliminate q1:

a b a*b

when no more paths through q1, start over (and simplify REs)
q0

q3 q2

qa

a*c

a U ca*b b U ca*c

ANOTHER Example: DFA GNFA RE a*b a*c Eliminate q2: re-draw GNFA with all other states
q0 q2 q3

q0

qa

a U ca*b b U ca*c

a*b

q3

qa

ANOTHER Example: DFA GNFA RE a*b a*c Eliminate q2: find a path through q2 a*b
q2 q3

q0

qa

a U ca*b b U ca*c

q0

q3

qa

ANOTHER Example: DFA GNFA RE a*b a*c Eliminate q2: add edge to new GNFA a*b U a*c(b U ca*c)*(a U ca*b)
q0 q3 q2 q3

q0

qa

a U ca*b b U ca*c

qa

ANOTHER Example: DFA GNFA RE a*b a*c Eliminate q2: when no more paths through q2, start over a*b U a*c(b U ca*c)*(a U ca*b)
q0 q3 q2 q3

q0

qa

a U ca*b b U ca*c

qa

ANOTHER Example: DFA GNFA RE

a*b U a*c(b U ca*c)*(a U ca*b)


q0 q3

qa

Eliminate q3: re-draw GNFA with all other states

q0

qa

ANOTHER Example: DFA GNFA RE

a*b U a*c(b U ca*c)*(a U ca*b)


q0 q3

qa

Eliminate q3: find a path through q3

don't forget: no arrow means

q0

qa

ANOTHER Example: DFA GNFA RE

a*b U a*c(b U ca*c)*(a U ca*b)


q0 q3

qa

Eliminate q3: add edge to new GNFA

a*b U a*c(b U ca*c)*(a U ca*b) * q0

qa

ANOTHER Example: DFA GNFA RE

a*b U a*c(b U ca*c)*(a U ca*b)


q0 q3

qa

Eliminate q3: when no more paths through q3, start over (and simplify REs) don't forget: *=
qa

q0

a*b U a*c(b U ca*c)*(a U ca*b)

ANOTHER Example: DFA GNFA RE

q0

a*b U a*c(b U ca*c)*(a U ca*b)

qa

Only two states remain:

RE = a*b U a*c(b U ca*c)*(a U ca*b)

Recap: Here means can be converted to

RE DFA NFA
Any of the three recognize exactly the regular languages (initially defined using DFA)

These conversions are used every time you enter an RE, for example for pattern matching using grep

The RE is converted to an NFA Then the NFA is converted to a DFA The DFA representation is used to pattern-match

Optimizations have been devised, but this is still the general approach.

What language is NOT regular? Is { 0n 1n : n 0 } = {, 01, 0011, 000111, } regular?

Pumping lemma: L regular language p 0 w L, |w| p x,y,z : w= xyz, |y|> 0, |xy| p i 0 : xyiz L Recall y0 = , y1 = y, y2 = yy, y3 = yyy, ...

Pumping lemma: L regular language p 0 w L, |w| p x,y,z : w= xyz, |y|> 0, |xy| p i 0 : xyiz L We will not see the proof. But here's the idea: p := |Q| for DFA recognizing L If w L, |w| p, then during computation 2 states must be the same q Q y = portion of w that brings back to q can repeat y and still accept string

Pumping lemma: L regular language p 0 w L, |w| p x,y,z : w= xyz, |y|> 0, |xy| p i 0 : xyiz L Useful to prove L NOT regular. Use contrapositive: L regular language A same as (not A) L not regular A

Pumping lemma (contrapositive) p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L To prove L not regular it is enough to prove not A Not A is the stuff in the box. not A L not regular

Proving something like bla bla bla bla bla means winning a game Theory is all about winning games!

Example NAME THE BIGGEST NUMBER GAME

Two players: You, Adversary. Rules:

First Adversary says a number. Then You say a number. You win if your number is bigger. Can you win this game?

Example NAME THE BIGGEST NUMBER GAME

Two players: You, Adversary. Rules:

First Adversary says a number. Then You say a number. You win if your number is bigger. You have winning strategy: if adversary says x, you say x+1

Example NAME THE BIGGEST NUMBER GAME

Two players: You, Adversary. , xy:y>x Rules:

First Adversary says a number. Then You say a number. You win if your number is bigger. You have winning strategy: if adversary says x, you say x+1

Claim is true

Another example: Theorem: NFA N DFA M : L(M) = L(N) We already saw a winning strategy for this game What is it?

Another example: Theorem: NFA N DFA M : L(M) = L(N) We already saw a winning strategy for this game The power set construction.

Games with more moves: Chess, Checkers, Tic-Tac-Toe You can win if move of the Adversary move You can make move of the Adversary move You can make : You checkmate

Pumping lemma (contrapositive) p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L Rules of the game: Adversary picks p, You pick w L of length p, Adversary decomposes w in xyz, where |y| > 0, |xy|p You pick i 0 Finally, you win if xyiz L L not regular

Theorem: L := {0n 1n : n 0} is not regular Proof: Use pumping lemma Adversary moves p You move w := 0p 1p Adversary moves x,y,z You move i := 2 You must show xyyz L: Since |xy|p and w = xyz = 0p 1p , y only has 0 So xyyz = 0p + |y| 1p Since |y| > 0, this is not of the form 0n 1n DONE
p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L

Theorem: L := {w : w has as many 0 as 1} not regular Same Proof: Use pumping lemma Adversary moves p You move w := ?
p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L

Theorem: L := {w : w has as many 0 as 1} not regular Same Proof: Use pumping lemma Adversary moves p You move w := 0p 1p Adversary moves x,y,z You move i := ?
p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L

Theorem: L := {w : w has as many 0 as 1} not regular Same Proof: Use pumping lemma Adversary moves p You move w := 0p 1p Adversary moves x,y,z You move i := 2 You must show xyyz L: Since |xy|p and w = xyz = 0p 1p , y only has 0 So xyyz = ?
p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L

Theorem: L := {w : w has as many 0 as 1} not regular Same Proof: Use pumping lemma Adversary moves p You move w := 0p 1p Adversary moves x,y,z You move i := 2 You must show xyyz L: Since |xy|p and w = xyz = 0p 1p , y only has 0 So xyyz = 0p + |y| 1p Since |y| > 0, not as many 0 as 1 DONE
p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L

Theorem: L := {0j 1k : j > k} is not regular Proof: Use pumping lemma Adversary moves p You move w := ?
p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L

Theorem: L := {0j 1k : j > k} is not regular Proof: Use pumping lemma Adversary moves p You move w := 0p+1 1p Adversary moves x,y,z You move i := ?
p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L

Theorem: L := {0j 1k : j > k} is not regular Proof: Use pumping lemma Adversary moves p You move w := 0p+1 1p Adversary moves x,y,z You move i := 0 You must show xz L: Since |xy|p and w = xyz = 0p+1 1p , y only has 0 So xz = 0p + 1 |y|

p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L

1p

Since |y| > 0, this is not of the form 0j 1k with j > k

Theorem: L := {uu : u {0,1}* } is not regular Proof: Use pumping lemma Adversary moves p You move w := ?
p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L

Theorem: L := {uu : u {0,1}* } is not regular Proof: Use pumping lemma Adversary moves p
p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p

You move w := 0p1 0p 1 i 0 : xyiz L Adversary moves x,y,z You move i := ?

Theorem: L := {uu : u {0,1}* } is not regular Proof: Use pumping lemma Adversary moves p
p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p

You move w := 0p 1 0p 1 i 0 : xyiz L Adversary moves x,y,z You move i := 2 You must show xyyz L: Since |xy|p and w = xyz = 0p 1 0p 1 , y only has 0 So xyyz = 0p + |y| 1 0p 1 Since |y| > 0, first half of xyyz only 0, so xyyz L

2 n Theorem: L := { 1 : n 0 } is not regular

Proof: Use pumping lemma Adversary moves p You move w := ?

p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L

2 n Theorem: L := { 1 : n 0 } is not regular

Proof: Use pumping lemma Adversary moves p You move w := 1p You move i := ?
2

p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L

Adversary moves x,y,z

2 n Theorem: L := { 1 : n 0 } is not regular

Proof: Use pumping lemma Adversary moves p You move w := 1p You move i := 2 You must show xyyz L: Since |xy|p, |xyyz| ?
2

p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L

Adversary moves x,y,z

2 n Theorem: L := { 1 : n 0 } is not regular

Proof: Use pumping lemma Adversary moves p You move w := 1p You move i := 2 You must show xyyz L:
2

p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L

Adversary moves x,y,z

Since |xy|p, |xyyz| p2 + p < (p+1)2 Since |y| > 0, |xyyz| > ?

2 n Theorem: L := { 1 : n 0 } is not regular

Proof: Use pumping lemma Adversary moves p You move w := 1p You move i := 2 You must show xyyz L:
2

p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L

Adversary moves x,y,z

Since |xy|p, |xyyz| p2 + p < (p+1)2 Since |y| > 0, |xyyz| > p2 So |xyyz| cannot be what ?

2 n Theorem: L := { 1 : n 0 } is not regular

Proof: Use pumping lemma Adversary moves p You move w := 1p You move i := 2 You must show xyyz L:
2

p 0 w L, |w| p x,y,z : w = xyz, |y| > 0, |xy| p i 0 : xyiz L

Adversary moves x,y,z

Since |xy|p, |xyyz| p2 + p < (p+1)2 Since |y| > 0, |xyyz| > p2 So |xyyz| cannot be a square. xyyz L

Big picture

All languages Decidable Turing machines NP P Context-free Context-free grammars, push-down automata Regular Automata, non-deterministic automata, regular expressions

Вам также может понравиться