MELJUN CORTES Automata manual handouts

41
June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS FINITE AUTOMATA It is a mathematical model of a system with discrete inputs and outputs. It consist of finite set of a sets and a set of transitions from state to state the occur on input symbol chosen from alphabet (). CONVENTIONS: 1. states q’s and p’s 2. q0 initial state 3. 0, 1, a, b input symbols 4. w, x, y, z strings of input symbols. NOTE: Finite Automata are limited in strength but they are a thoroughly understood subclass of more powerful computational models. a further reason for studying FA is their applicability to the design of several common types of computer algorithms and programs. Example: Lexical analysis phase of Compilers is often based on the simulation of automation. Three (3) Ways Of Presenting Finite Automata 1. State Diagram (DIAGRAM) Vertices denotes the state Edges transition function Initial State Final State Example: State Diagram Page 1 / aav x,y

description

MELJUN CORTES Automata manual handouts

Transcript of MELJUN CORTES Automata manual handouts

Page 1: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

FINITE AUTOMATA

It is a mathematical model of a system with discrete inputs and outputs. It consist of finite set of a sets and a set of transitions from state to state the

occur on input symbol chosen from alphabet ().

CONVENTIONS:1. states q’s and p’s2. q0 initial state3. 0, 1, a, b input symbols4. w, x, y, z strings of input symbols.

NOTE:Finite Automata

are limited in strength but they are a thoroughly understood subclass of more powerful computational models.

a further reason for studying FA is their applicability to the design of several common types of computer algorithms and programs.

Example:Lexical analysis phase of Compilers is often based on the simulation of

automation.

Three (3) Ways Of Presenting Finite Automata

1. State Diagram (DIAGRAM)Vertices denotes the stateEdges transition function

Initial State Final State

Example:State Diagram

2. State Table

Page 1 / aav

x,y

x,y

Page 2: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

State Table

x yq0

q1 q1

*q1 q0 q0

3. Transition Function ( set of states, set of input alphabet )

Transition Function (q0,x) = q1 (q0,y) = q1 (q1,x) = q0 (q1,y) = q0

TWO TYPES OF FINITE AUTOMATA :

1. Deterministic Finite Automata (DFA)

One-way, infinite tape, broken into cells One-way, read-only tape head. Finite control, I.e., a program, containing the position of the read head, current

symbol being scanned, and the current “state.” A string is placed on the tape, read head is positioned at the left end, and the

DFA will read the string one symbol at a time until all symbols have been read. The DFA will then either accept or reject.

For each input symbol in , there is exactly one transition of each state (possibly back to the state itself).

It do not accept empty strings. It is a quintuple where M = (Q, , q0, , F)

where: Q = finite set of all sets

= finite set of input symbolsq0 = initial state = transition functionF = set of final state

Page 2 / aav

Finite Control

00110

Page 3: MELJUN CORTES Automata manual handouts

q0 q1

0

0

1

1

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

Transition DiagramExamples:1. Diagrams that ends with 10.

State Diagram

Transition Table

0 1q0

q0 q1

q1 q2 q1*q2 q0 q1

Strings that can be derived: 10 010

0110 01010

Transition Function (q0,0) = q0 (q0,1) = q1 (q1,0) = q2

(q1,1) = q1 (q2,0) = q0 (q2,1) = q1

2. Draw a transition diagram that will end with 001.

3.

1 0 0 1 1q0 q0 q1 q0 q0 q0

One state is final/accepting, all others are rejecting.

Page 3 / aav

Page 4: MELJUN CORTES Automata manual handouts

q0 q1

0

0

1

1

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

The above DFA accepts those strings that contain an even number of 0’s

4. Accepts those strings that contain at least two c’s

a c c c b accepted q0 q0 q1 q2 q2 q2

a a c rejected q0 q0 q0 q1

Extension of δ to Stringsδ^ : (Q x Σ*) –> Qδ^(q,w) – The state entered after reading string w having started in state q.

Formally:1) δ^(q, ε) = q, and2) For all w in Σ* and a in Σ δ^(q,wa) = δ (δ^(q,w), a)

Recall Example #3:

What is δ^(q0, 011)? Informally, it is the state entered by M after processing 011 having started in state q0.

Formally: δ^(q0, 011) = δ (δ^(q0,01), 1) by rule #2

= δ (δ ( δ^(q0,0), 1), 1) by rule #2= δ (δ (δ (δ^(q0, λ), 0), 1), 1) by rule #2

Page 4 / aav

q1q0 q2

a

b

a

b

c c

a/b/c

Page 5: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

= δ (δ (δ(q0,0), 1), 1) by rule #1= δ (δ (q1, 1), 1) by definition of δ= δ (q1, 1) by definition of δ= q1 by definition of δ

Is 011 accepted? No, since δ^(q0, 011) = q1 is not a final state.Note that:

δ^ (q,a) = δ(δ^(q, ε), a) by definition of δ^, rule #2

= δ(q, a) by definition of δ^, rule #1Therefore: δ^ (q, a1a2…an) = δ(δ(…δ(δ(q, a1), a2)…), an)Hence, we can use δ in place of δ^: δ^(q, a1a2…an) = δ(q, a1a2…an)

Definitions for DFAs Let M = (Q, Σ, δ,q0,F) be a DFA and let w be in Σ*. Then w is accepted by M iff

δ(q0,w) = p for some state p in F. Let M = (Q, Σ, δ,q0,F) be a DFA. Then the language accepted by M is the set:

L(M) = {w | w is in Σ* and δ(q0,w) is in F} Another equivalent definition:

L(M) = {w | w is in Σ* and w is accepted by M} Let L be a language. Then L is a regular language iff there exists a DFA M

such that L = L(M).

Let M1 = (Q1, Σ1, δ1, q0, F1) and M2 = (Q2, Σ2, δ2, p0, F2) be DFAs. Then M1 and M2

are equivalent iff L(M1) = L(M2).

Notes:– A DFA M = (Q, Σ, δ,q0,F) partitions the set Σ* into two sets: L(M) and Σ* -

L(M). – If L = L(M) then L is a subset of L(M) and L(M) is a subset of L.– Similarly, if L(M1) = L(M2) then L(M1) is a subset of L(M2) and L(M2) is a

subset of L(M1). – Some languages are regular, others are not. For example, if

L1 = {x | x is a string of 0's and 1's containing an even number of 1's} and

L2 = {x | x = 0n1n for some n >= 0} then L1 is regular but L2 is not.

• Questions:– How do we determine whether or not a given language is regular?– How could a program “simulate” a DFA?

Page 5 / aav

Page 6: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

Seatwork:

1. Give the following DFA M such that:a. L(M) = {x | x is a string of 0’s and 1’s and |x| >= 2} no. states 3b. L(M) = {x | x is a string of (zero or more) a’s, b’s and c’s such

that x does not contain the substring aa} # states 3c. L(M) = {x | x is a string of a’s, b’s and c’s such that x contains the

substring aba} # states 4d. L(M) = {x | x is a string of a’s and b’s such that x contains both aa and

bb} no of states 8

Nondeterministic Finite State Automata (NFA)

*RECALL: In FA or DFA, for each input there is exactly one transition output of each state.

Let’s try to modify the finite automaton by alowing zero, one or more transitions from a state on the same input symbol. The modified model is called Non-Deterministic Finite Automaton.

If for some of Q, a, (q,a) does either to a unique state or several states or not states at all, then the FA is a NFA.

A Non-Deterministic Finite Automaton is a quintuple M=(Q,,q0,,F)where: Q = set of states = set of input alphabets

= transition function δ: (Q x Σ) –> 2Q -2Q is the power set of Q, the set of all subsets of Q δ(q,s)

-The set of all states p such that there is a transition labeled s from q to pq0 = initial state F = set of final state

It allows zero, one or more transitions for every inputs symbol. Empty string accepted A sequence of symbols, say a1, a2, ...an is accepted by NFA if there exists a

sequence of transition corresponding to the input sequence that leads from the initial states to the final state.

Example: Construct a NFA that accepts string ending with 011.

Page 6 / aav

Page 7: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

(q0,011)--> q3

To check if 1011 is a valid string q0q0q1q2q2 valid

q0q0q0 not valid* The reason for the word “nondeterministic” is that we are in a state where

there are multiple outgoing edges all having the same input symbol – we have a choice of next state.

*The only difference between a NFA & DFA is that in DFA the next state function takes us to a uniquely defined state whereas in NFA the next state takes us to a set of states.

Example : NFA Diagram

State Table0 1

q0 {q0,q3} {q0,q1}q1 0 {q2}q2 {q2} {q2}q3 {q4} 0q4 {q4} {q4}

Transition Function Let the input be 01001

(q0, ) = q0 (q0, 0) = ((q0, )0) = (q0,0)={q0,q3} (q1, 01) = ((q0,0)1) =(q0,1) U (q3,1)={q0,q1}U{ }={q0,q1}

(q1, 010) = ((q0,01)0)=(q0,0) U (q1,0)={q0,q3}U{ }={q0,q3}

(q2, 0100) = ((q0,010)0)=(q0,1) U (q3,1) U (q3,0)={q0,q3} U {q4}={q0,q3,q4}

(q2, 01001) = ((q0,1) U (q3,1) U (q4,1) = {q0,q1} U {} U {q4} = {q0,q1,q4}

Page 7 / aav

Page 8: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

Example: pair os 0’s or pair of 1’sQ = {q0, q1, q2 , q3 , q4} Σ = {0, 1} Start state is q0 F = {q2, q4}

Determining if a given NFA accepts a given string (001) can be done algorithmically:

Each level will have at most n states

Another example (010): All paths have been explored, and none lead to an accepting state

Let Σ = {a, b, c}. Give an NFA M that accepts: L = {x | x is in Σ* and x contains ab}

Is L a subset of L(M)? Is L(M) a subset of L?• Is an NFA necessary? Could a DFA accept L? Try and give an equivalent DFA as

an exercise.• Designing NFAs is not a typical task.

Let Σ = {a, b}. Give an NFA M that accepts:L = {x | x is in Σ* and the third to the last

symbol in x is b}

Page 8 / aav

Page 9: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

Extension of δ to Strings and Sets of StatesWhat we currently have: δ : (Q x Σ) –> 2Q

What we want (why?): δ : (2Q x Σ*) –> 2Q We will do this in two steps, which will be slightly different from the book, and we will make use of the following NFA.

Step #1: Given δ: (Q x Σ) –> 2Q define δ#: (2Q x Σ) –> 2Q as follows:1) δ#(R, a) = δ(q, a) for all subsets R of Q, and symbols a in Σ

Note that: δ#({p},a) = δ(q, a) by definition of δ#, rule #1 above = δ(p, a)Hence, we can use δ for δ#

δ({q0, q2}, 0) These now make sense, but previouslyδ({q0, q1, q2}, 0) they did not.

Example:δ({q0, q2}, 0) = δ(q0, 0) U δ(q2, 0)

= {q1, q3} U {q3, q4}= {q1, q3, q4}

δ({q0, q1, q2}, 1) = δ(q0, 1) U δ(q1, 1) U δ(q2, 1)

= {} U {q2, q3} U {}= {q2, q3}

Step #2: Given δ: (2Q x Σ) –> 2Q define δ^: (2Q x Σ*) –> 2Q as follows:δ^(R,w) – The set of states M could be in after processing string w, having starting from any state in R.

Formally:2) δ^(R, ε) = R for any subset R of Q3) δ^(R,wa) = δ (δ^(R,w), a) for any w in Σ*, a in Σ, and subset R of Q

Note that:δ^(R, a) = δ(δ^(R, ε), a) by definition of δ^, rule #3 above

= δ(R, a) by definition of δ^, rule #2 aboveHence, we can use δ for δ^

δ({q0, q2}, 0110) These now make sense, but previouslyδ({q0, q1, q2}, 101101) they did not.

Page 9 / aav

Page 10: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

Example:

What is δ({q0}, 10)?Informally: The set of states the NFA could be in after processing 10,having started in state q0, i.e., {q1, q2, q3}.

Formally: δ({q0}, 10) = δ(δ({q0}, 1), 0) = δ({q0}, 0) = {q1, q2, q3} Is 10 accepted? Yes!

What is δ({q0, q1}, 1)?δ({q0 , q1}, 1) = δ({q0}, 1) U δ({q1}, 1)

= {q0} U {q2, q3}= {q0, q2, q3}

What is δ({q0, q2}, 10)?δ({q0 , q2}, 10) = δ(δ({q0 , q2}, 1), 0)

= δ(δ({q0}, 1) U δ({q2}, 1), 0)= δ({q0} U {q3}, 0)= δ({q0,q3}, 0)= δ({q0}, 0) U δ({q3}, 0)= {q1, q2, q3} U {}= {q1, q2, q3}

δ({q0}, 101) = δ(δ({q0}, 10), 1)= δ(δ(δ({q0}, 1), 0), 1)= δ(δ({q0}, 0), 1)= δ({q1 , q2, q3}, 1)= δ({q1}, 1) U δ({q2}, 1) U δ({q3}, 1) = {q2, q3} U {q3} U {}= {q2, q3} Is 101 accepted? Yes!

Page 10 / aav

Page 11: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

Definitions for NFAs• Let M = (Q, Σ, δ,q0,F) be an NFA and let w be in Σ*. Then w is accepted by M iff

δ({q0}, w) contains at least one state in F. • Let M = (Q, Σ, δ,q0,F) be an NFA. Then the language accepted by M is the set:

L(M) = {w | w is in Σ* and δ({q0},w) contains at least one state in F} • Another equivalent definition:

L(M) = {w | w is in Σ* and w is accepted by M}

Notes:– δ(q,s) may not be defined for some q and s (why?).– Informally, a string is said to be accepted if there exists a path to some state in F.– The language accepted by an NFA is the set of all accepted strings.– Question: How does an NFA find the correct/accepting path for a given string?– NFAs are a non-intuitive computing model.– We are primarily interested in NFAs as language defining devices, i.e., do NFAs

accept languages that DFAs do not?– Other questions are secondary, including practical questions such as whether or

not there is an algorithm for finding an accepting path through an NFA for a given string,

EQUIVALENCE OF DFAs AND NFAsTheorem: For every NFA model, there exist one and only one equivalent DFA Let M’ be the equivalent DFA of a NFA M.

M’=(Q’, ,’,q0’,F’)Example: Transform the given NFA to DFA

Q’={{},q0,q1,q2,{q0,q1},{q0,q1},{q0,q2},{q1,q2},{q0,q1,q2}} F =q2 F’={q2,{q0,q2},{q1,q2},{q0,q1,q2}}

Transition Table of NFA

0 1q0 {q0} {q0,q

1}q1 {q1,q

2}{}

q2 {q2} {}

Page 11 / aav

Page 12: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

Transition Function’ ({},0) = {}’ ({},1) = {}’ (q0,0) = q0’ (q0,1) = {q0,q1}

’ (q1,0) = {q1,q2}’ (q1,1) = {}’ (q2,0) = q2’ (q2,1) = {}

’ ((q0,q1),0) = {q0,0} U qq0 U {q1,q2} = {q0,q1,q2}’ ((q0,q1),1) = {q0,1) U (q1,1) = {q0,q1} U q0,q1}’ ((q0,q2),0) = {q0,0) U (q2,0) = {q0} U {q2} = {q0,q2}’ ((q0,q2),1) = {q0,1) U (q2,1) = {q0,q1} U {q2} = {q0,q1,q2}’ ((q1,q2),0) = {q1,0) U (q2,0) = {q1,q2} U {q2} = {q1,q2}’ ((q0,q1,q2),0) = {q0,0) U (q1,0) U (q2,0) = {q0} U {q1,q2} U {q2} = {q0,q1,q2}’ ((q0,q1,q2),1) = {q0,1) U (q1,1) U (q2,1) = {q0,q1} U {} U {} = {q0,q1}

State Table of DFA

0 1q0 {q0} {q0,q1}q1 {q1,q2} {}q2 {q2} {}q0,q1 {q0,q1,q

2}{q0,q1}

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

{q0,q1,q2}

{q0,q1}

{} {} {}

Transition Diagram Of DFA

Page 12 / aav

q0 q0,q1

q0,q1,q2

q0,q2

Page 13: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

• Do DFAs and NFAs accept the same class of languages?– Is there a language L that is accepted by a DFA, but not by any NFA?– Is there a language L that is accepted by an NFA, but not by any DFA?

• Observation: Every DFA is an NFA.• Therefore, if L is a regular language then there exists an NFA M such that L =

L(M).• It follows that NFAs accept all regular languages.• But do NFAs accept more?

Consider the following DFA: 2 or more c’s Q = {q0, q1, q2} Σ = {a, b, c} Start state is q0 F = {q2}

An Equivalent NFA:Lemma 1: Let M be an DFA. Then there exists a NFA M’ such that L(M) = L(M’).Proof: Every DFA is an NFA. Hence, if we let M’ = M, then it follows that L(M’) = L(M).Lemma 2: Let M be an NFA. Then there exists a DFA M’ such that L(M) = L(M’).Proof: (sketch)

Let M = (Q, Σ, δ,q0,F).Define a DFA M’ = (Q’, Σ, δ’,q’

0,F’) as:Q’ = 2Q Each state in M’ corresponds to a = {Q0, Q1,…,} subset of states from Mwhere Qu = [qi0, qi1,…qij]F’ = {Qu | Qu contains at least one state in F}q’

0 = [q0]

δ’(Qu, a) = Qv iff δ(Qu, a) = Qv

Example: empty string or start and end with 0Q = {q0, q1} Σ = {0, 1} Start state is q0 F = {q1}

Construct DFA M’ as follows

Page 13 / aav

Page 14: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

δ({q0}, 0) = {q1} => δ’([q0], 0) = [q1]δ({q0}, 1) = {} => δ’([q0], 1) = [ ]δ({q1}, 0) = {q0, q1} => δ’([q1], 0) = [q0, q1]δ({q1}, 1) = {q1} => δ’([q1], 1) = [q1]δ({q0, q1}, 0) = {q0, q1} => δ’([q0, q1], 0) = [q0, q1]δ({q0, q1}, 1) = {q1} => δ’([q0, q1], 1) = [q1]δ({}, 0) = {} => δ’([ ], 0) = [ ]δ({}, 1) = {} => δ’([ ], 1) = [ ]

Theorem: Let L be a language. Then there exists an DFA M such that L = L(M) iff there exists an NFA M’ such that L = L(M’).

Proof:(if) Suppose there exists an NFA M’ such that L = L(M’). Then by Lemma 2 there exists an DFA M such that L = L(M). (only if) Suppose there exists an DFA M such that L = L(M). Then by Lemma 1 there exists an NFA M’ such that L = L(M’).

Corollary: The NFAs define the regular languages.

Note: Suppose R = {}δ(R, 0) = δ(δ(R, ε), 0)

= δ(R, 0)= δ(q, 0)= {} Since R = {}

NFAs with ε Moves It is quintuple M = {Q,q0,F} where the transition functions maps.

:Q x (U {Є}) 2Q

(q,a)={p | (q,a) p and a=Є or aЄwill consist of all states p such that there is a transition labeled a from q to p where a is either Є or a symbol in the

Є-closure(q)= { p | , a path from q to p marked Є }Є-closure(p)= UqЄp Є-closure where p is a set of states: Q x Q (q,w)p | q,w) contains p including edges labeled Є(q,Є) = Є-closure (q)

*(q,w) will be all states p such that one can go from q to p doing a path labeled w, perhaps including edges labeled Є.

• Sometimes referred to as an NFA-ε other times, simply as an NFA.Example:

Page 14 / aav

Page 15: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

transition:(q0,a)q0(q0,Є)q1

(q1,Є)q2(q2,a)q3

Є-closure:

EQUIVALENT NFA of NFAЄ

Theorem: For each NFA’s there is one and only one equivalent NFA.NFA or NFA’s: M = {Q, q0, F}Let Equivalent NFA be M’ = {Q, q0, F1}

where: F’ = {F U Є–closure (qo)1 l Єclosure (qo) contains state of F }

Example:1. Convert the given NFA to NFA.

NFAЄ

0 1 2 q0 {q0

}{} {} q1

q1 {} q1 {} q2q2 {} {} q2 {}NFA

0 1 2q0 {q0,q1,q

2}{q1,q2}

{q2}

q1 0 {q1,q2}

{q2}

q2 {} {} {q2}

F’ = {F U Є–closure (q0)} F’ = {q0,q1,q2}

Transformed NFA

2. Convert the given NFA to NFA.

Page 15 / aav

Є-closure (q0) = {q0,q1,q2}Є-closure (q1) = {q1,q2}Є-closure(p) = Є-closure of {q0,q1} = Є-closure {q0} U Є-closure {q1}= {q0,q1,q2} U {q1,q2}

Page 16: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

F = {q3,q5} Є-closure (q0) = {q0,q1,q4} q0 = {q0} = {a,b}

NFAЄ

a b Eq0 {} {} {q1,q4

}q1 {q1,q2

}q1 {}

q2 {q3} {} {}q3 {q3} {q3} {}q4 {} {q4} {}q5 {} {q5} {}

NFAa b

q0 {q1,q2}

{q1,q5}

q1 {q1,q2}

{q1}

q2 {q3} 0q3 {q3} {q3}q4 {} {q5}q5 {} {q5}

Page 16 / aav

Page 17: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

F’ = {q3,q5}

Transformed NFA

Example:

- A string w = w1w2…wn is processed as w = ε*w1ε*w2ε* … ε*wnε*

- Example: all computations on 00:

0 ε 0q0 q0 q1 q2

Informal Definitions• Let M = (Q, Σ, δ,q0,F) be an NFA-ε.• A String w in Σ* is accepted by M iff there exists a path in M from q0 to a state in

F labeled by w and zero or more ε transitions.• The language accepted by M is the set of all strings from Σ* that are accepted by

M.

Step #2: Given δ: (2Q x (Σ U {ε})) –> 2Q define δ^: (2Q x Σ*) –> 2Q as follows:δ^(R,w) – The set of states M could be in after processing string w, having starting from any state in R.

Formally:2) δ^(R, ε) = ε-closure(R) - for any subset R of Q

Page 17 / aav

Page 18: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

3) δ^(R,wa) = ε-closure(δ(δ^(R,w), a)) - for any w in Σ*, a in Σ, and subset R of QCan we use δ for δ^?

Lemma 1: Let M be an NFA. Then there exists a NFA-ε M’ such that L(M) = L(M’).Proof: Every NFA is an NFA-ε. Hence, if we let M’ = M, then it follows that L(M’) = L(M).

Lemma 2: Let M be an NFA-ε. Then there exists a NFA M’ such that L(M) = L(M’).Proof: (sketch)

Let M = (Q, Σ, δ,q0,F) be an NFA-ε.Define an NFA M’ = (Q, Σ, δ’,q0,F’) as:

F’ = F U {q0} if ε-closure(q0) contains at least one state from FF’ = F otherwise

δ’(q, a) = δ^(q, a) - for all q in Q and a in ΣNotes: - δ’: (Q x Σ) –> 2Q is a function

– M’ has the same state set, the same alphabet, and the same start state as M

– M’ has no ε transitions

Theorem: Let L be a language. Then there exists an NFA M such that L= L(M) iff there exists an NFA-ε M’ such that L = L(M’).Proof:(if) Suppose there exists an NFA-ε M’ such that L = L(M’). Then by Lemma 2 there exists an NFA M such that L = L(M). (only if) Suppose there exists an NFA M such that L = L(M). Then by Lemma 1 there exists an NFA-ε M’ such that L = L(M’).Corollary: The NFA-ε machines define the regular languages.

OPERATIONS ON LANGUAGESLet L, L1, L2 be subsets of Σ*Concatenation: L1L2 = {xy | x is in L1 and y is in L2}Concatenating a language with itself: L0 = {ε}

Li = LLi-1, for all i >= 1

Kleene Closure: L* = Li = L0 U L1 U L2 U…

L1 ={a, abc, ba}, on Σ ={a,b,c}

Page 18 / aav

Page 19: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

Then, L2 = {aa, aabc, aba, abca, abcabc, abcba, baa, baabc, baba}

L3= {a, abc, ba}. L2 L* = {ε, L1, L2, L3, . . .}

Positive Closure: L+ = Li = L1 U L2 U…

REGULAR EXPRESSIONS– A regular expression is used to specify a language, and it does so precisely.– Regular expressions are very intuitive.– Regular expressions are very useful in a variety of contexts.– Given a regular expression, an NFA-ε can be constructed from it automatically.– Thus, so can an NFA, a DFA, and a corresponding program, all automatically!

Definition of a Regular Expression• Let Σ be an alphabet. The regular expressions over Σ are:

– Ø Represents the empty set { }– ε Represents the set {ε}– a Represents the set {a}, for any symbol a in Σ

Let r and s be regular expressions that represent the sets R and S, respectively.

– r+s Represents the set R U S (precedence 3)– rs Represents the set RS (precedence 2)– r* Represents the set R* (highest precedence)– (r) Represents the set R (not an op, provides precedence)

If r is a regular expression, then L(r) is used to denote the corresponding language.

Examples: Let Σ = {0, 1}1. (0 + 1)* All strings of 0’s and 1’s2. 0(0 + 1)* All strings of 0’s and 1’s, beginning with a 03. (0 + 1)*1 All strings of 0’s and 1’s, ending with a 14. (0 + 1)*0(0 + 1)* All strings of 0’s and 1’s containing at least one 05. (0 + 1)*0(0 + 1)*0(0 + 1)* All strings of 0’s and 1’s containing at least two 0’s6. (0 + 1)*01*01* All strings of 0’s and 1’s containing at least two 0’s

Page 19 / aav

Page 20: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

7. (1 + 01*0)* All strings of 0’s and 1’s containing an even number of 0’s

8. 1*(01*01*)* All strings of 0’s and 1’s containing an even number of 0’s

9. (1*01*0)*1* All strings of 0’s and 1’s containing an even number of 0’s

Basis: OP(r) = 0

Then r is either Ø, ε, or a, for some symbol a in Σ

Inductive Hypothesis: Suppose there exists a k ³ 0 such that for any regular expression r where 0 £ OP(r) £ k, there exists an NFA-ε such that L(M) = L(r). Furthermore, suppose that M has exactly one final state.

Inductive Step: Let r be a regular expression with k + 1 operators (OP(r) = k + 1), where k + 1 >= 1.

Case 1) r = r1 + r2Since OP(r) = k +1, it follows that 0<= OP(r1), OP(r2) <= k. By the inductive hypothesis there exist NFA-ε machines M1 and M2 such that L(M1) = L(r1) and L(M2) = L(r2). Furthermore, both M1 and M2 have exactly one final state.

Construct M as:

Page 20 / aav

Page 21: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

Case 2) r = r1r2Since OP(r) = k+1, it follows that 0<= OP(r1), OP(r2) <= k. By the inductive hypothesis there exist NFA-ε machines M1 and M2 such that L(M1) = L(r1) and L(M2) = L(r2). Furthermore, both M1 and M2 have exactly one final state.

Construct M as:

Case 3) r = r1*Since OP(r) = k+1, it follows that 0<= OP(r1) <= k. By the inductive hypothesis there exists an NFA-ε machine M1 such that L(M1) = L(r1). Furthermore, M1 has exactly one final state.

Construct M as:

Example:

r = 0(0+1)*

r = r1r2r1 = 0r2 = (0+1)*r2 = r3*r3 = 0+1

Page 21 / aav

Page 22: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

r3 = r4 + r5r4 = 0r5 = 1

Seatwork:Construct the equivalent NFAe of the ff, reg. expression:1. 02. 013. 0+14. 1*5. 11+0

CONTEXT-FREE GRAMMARS Context-free Grammars describe context-free languages. Regular set is an example of Context Free language. Context-Free grammar (CFG) is a quadruple G = (V,T,P,S) where V – set of variables {S,B}

T – set of terminals (a,b,c) P – Production or rule (How to form the string) S – the starting symbol

Examples : Construct CFG for the following:1. ab*c = {ac,abc,abbc,abbbc,….}

given: S aBcB bBB Є

ac abc abbbbbcS aBc S aBc S aBc aЄc abBc abbBc ac abЄc abbbBc abc abbbbBc

abbbbbBc abbbbbЄc abbbbbc

2. Construct a CFG for anbn; n>0={ab,aabb,aaabbb..}V=SS=ST={a,b}

Page 22 / aav

Page 23: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

P: S aSb S Є

ab aabbS aSb S aSb

aЄb aaSbb ab aaЄbb

aabbSeatwork:

Construct a CFG for the following:a. 0*1 = {1,01,001,..} V=S,B T={0,1} S=S P: S B1

B 0BB Є

1. 12. 0001

b. (0+1)* S 1s|0s S Є

3. 014. 111001

c. ambn | m>= n S aMb M aMb | aM | Є

5. aabb6. aaabb

d. {anbncmdm | n,m > 0} S abcd| aMbcNd M aMb | Є

N cNd | Є7. aabbcd8. aabbccdd

e. {xmyxm| m >= 0} S xSx |y9. xyx10. xxxyxxx

Page 23 / aav

Page 24: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

PUSHDOWN AUTOMATA

Context Free Grammar’s machine counterpart.(Just as the RE have an equivalent automaton-FA).

Non deterministic device.(Deterministic version is only a subset of all CFL’s). Have input tape, finite control and a stack Essentially a FA with control of both input and stack “FILO” list.

Stack – is a string of symbols from some alphabet. The leftmost symbol of the stack is considered to be the “tape” of the stack.

MOVES1. An input symbol is used.

Depending on the input symbol, the top symbol on the stack, and state of the finite control, a number of choices are possible. Each choice consist of next state for the finite control and a (possibly empty) string of symbols to replace the top stack symbol. After selecting a choice, the input head is advanced one symbol.

2. Input symbol is not used (Є-move). Similar to the first except that the input symbol is not used and the input

head is not advanced after the move. Allows the PDA to manipulate the stack without reading input symbols.

WAYS TO DEFINE THE LANGUAGE ACCEPTED BY PDA no final state – empty both input tape & stack

1. Set of all inputs for which some sequence of moves causes the PDA to empty a stack.

2. Designate some states as final sates define the accepted language as the set of all inputs for which some choice of moves causes the PDA to enter final state.

with final state – empty input tapeM = (Q, , Г, , qo, Zo, F)

where: Q = finite set of all sets

= finite set of input symbolsГ = stack alphabetq0 = in Q, initial stateZo = in Г, start symbolF = ⊆ Q, set of final states = mapping Q x ( U {Є}) x Г to Q x Г*

Page 24 / aav

Page 25: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

(q, a, Z) = {(P1, y1)…(Pm, ym)} initial state Pop Push

Input Destination StateExample:

M = ( {q1,q2}, {0,1}, {R, B, G}, , q1, R, q2 )

1. ( q1, 0, R ) = {( q1, BR )}2. ( q1, 1, R ) = {( q1, GR )}3. ( q1, 0, B ) = {( q1, BB )}, {( q2, Є )}4. ( q1, 0, G ) = {( q1, BG )}5. ( q1, 1, B ) = {( q1, GB )}6. ( q1, 1, G ) = {( q1, GG )}, {( q2, Є )}7. ( q2, 0, B ) = {( q2, Є )}8. ( q2, 1, G ) = {( q2, Є )}9. ( q1, Є, R ) = {( q2, Є )}10. ( q2, Є, R ) = {( q2, Є )}

Determine if the input 001100 is valid for PUSHDOWN AUTOMATA.(q1,001100,R)

(q1,01100,BR) (q2,001100,Є)

(q1,1100,BBR) (q2,1100,R) (q2,1100,Є)

(q1,100,GBBR) (q1,0,BGGBBR)

(q1,00,GGBBR) (q2,00,BBR)

(q1,0,BGGBBR) ( q2, 0, BR ) ( q2, Є, R ) ( q2, Є, Є ) Accepted

(q1,Є,BBGGBBR) (q2,Є,GGBBR)

? ?

Page 25 / aav

Page 26: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

Example: (balanced parentheses)M = ({q1}, {“(“, “)”}, {L, #}, δ, q1, #, Ø)δ:

(1) δ(q1, (, #) = {(q1, L#)}(2) δ(q1, ), #) = Ø(3) δ(q1, (, L) = {(q1, LL)}(4) δ(q1, ), L) = {(q1, ε)}(5) δ(q1, ε, #) = {(q1, ε)}(6) δ(q1, ε, L) = Ø

• Goal: (acceptance)– Terminate in a non-null state– Read the entire input string– Terminate with an empty stack

Transition Diagram:

Current Input Stack Transition(()) #()) L# (1) - Could have applied rule (5), )) LL# (3) but it would have ) L# (4) done no goodε # (4)ε - (5)

Example PDA: For the language {x | x = wcwr and w in {0,1}*}M = ({q1, q2}, {0, 1, c}, {R, B, G}, δ, q1, R, Ø)

δ:(1) δ(q1, 0, R) = {(q1, BR)} (7) δ(q2, 0, B) = {(q2, ε)}(2) δ(q1, 0, B) = {(q1, BB)} (8) δ(q2, ε, R) = {(q2, ε)}(3) δ(q1, 0, G) = {(q1, BG)} (9) δ(q1, 1, R) = {(q1, GR)}(4) δ(q1, c, R) = {(q2, R)} (10) δ(q1, 1, B) = {(q1, GB)}(5) δ(q1, c, B) = {(q2, B)} (11) δ(q1, 1, G) = {(q1, GG)}(6) δ(q1, c, G) = {(q2, G)} (12) δ(q2, 1, G) = {(q2, ε)}

Notes:– Only rule #8 is non-deterministic.– Rule #8 is used to pop the final stack symbol off at the end of a

computation.

Page 26 / aav

Page 27: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

State Input Stack Rule Applied Rules Applicableq1 01c10 R (1)q1 1c10 BR (1) (10)q1 c10 GBR (10) (6)q2 10 GBR (6) (12)q2 0 BR (12) (7)q2 ε R (7) (8)q2 ε ε (8) -

Seatwork: For the language {x | x = wwr and w in {0,1}*}M = ({q1, q2}, {0, 1}, {R, B, G}, δ, q1, R, Ø)δ:(1) δ(q1, 0, R) = {(q1, BR)} (6) δ(q1, 1, G) = {(q1, GG),

(q2, ε)} (2) δ(q1, 1, R) = {(q1, GR)} (7) δ(q2, 0, B) = {(q2, ε)}

(3) δ(q1, 0, B) = {(q1, BB), (q2, ε)} (8) δ(q2, 1, G) = {(q2, ε)} (4) δ(q1, 0, G) = {(q1, BG)} (9) δ(q1, ε, R) = {(q2, ε)}

(5) δ(q1, 1, B) = {(q1, GB)} (10) δ(q2, ε, R) = {(q2, ε)}

State Input Stack Rule Applied Rules Applicable1. q1 000000 R (1), (9)2. q1 010010 R

TURING MACHINE• Generalize the class of CFLs:

Page 27 / aav

Page 28: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

Another Part of the Hierarchy:

• Recursively enumerable languages are also known as type 0 languages.• Context-sensitive languages are also known as type 1 languages.• Context-free languages are also known as type 2 languages.• Regular languages are also known as type 3 languages.

• TMs model the computing capability of a general purpose computer, which informally can be described as:

– Effective procedure• Finitely describable• Well defined, discrete, “mechanical” steps• Always terminates

– Computable function• A function computable by an effective procedure

• TMs formalize the above notion.• Church-Turing Thesis: There is an effective procedure for solving a problem if

and only if there is a TM that halts for all inputs and solves the problem.– There are many other computing models, but all are equivalent to or

subsumed by TMs. There is no more powerful machine (Technically cannot be proved).

• DFAs and PDAs do not model all effective procedures or computable functions, but only a subset.

Deterministic Turing Machine (DTM)

Page 28 / aav

Page 29: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

• Two-way, infinite tape, broken into cells, each containing one symbol.• Two-way, read/write tape head.• Finite control, i.e., a program, containing the position of the read head, current

symbol being scanned, and the current state.• An input string is placed on the tape, padded to the left and right infinitely with

blanks, read/write head is positioned at the left end of input string.• In one move, depending on the current state and the current symbol being

scanned, the TM 1) changes state, 2) prints a symbol over the cell being scanned, and 3) moves its’ tape head one cell left or right.

• Many modifications possible.

A DTM is a seven-tuple:M = (Q, Σ, Γ, δ, q0, B, F)

Q A finite set of statesΓ A finite tape alphabetB A distinguished blank symbol, which is in ΓΣ A finite input alphabet, which is a subset of Γ– {B}q0 The initial/starting state, q0 is in QF A set of final/accepting states, which is a subset of Qδ A next-move function, which is a mapping (i.e., may be undefined) from

Q x Γ –> Q x Γ x {L,R}

Intuitively, δ(q,s) specifies the next state, symbol to be written, and the direction of tape head movement by M after reading symbol s while in state q.

Page 29 / aav

Page 30: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

Example : {0n1n | n >= 1}

0 1 X Y B q0 (q1, X, R) - - (q3, Y, R) - q1 (q1, 0, R) (q2, Y, L) - (q1, Y, R) - q2 (q2, 0, L) - (q0, X, R) (q2, Y, L) - q3 - - - (q3, Y, R) (q4, B, R) q4 - - - - -

Sample Computation: (on 0011)q00011 |— Xq1011

|— X0q111|— Xq20Y1|— q2X0Y1|— Xq00Y1|— XXq1Y1|— XXYq11|— XXq2YY|— Xq2XYY|— XXq0YY|— XXYq3Y|— XXYYq3|— XXYYBq4

Page 30 / aav

Page 31: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

Example: M = ({qo, q1… q4}, {0,1}, {0,1,x,y,B}, , q0, B, q4)

0 1 x Y B qo q1,x,R) ------- ------- (q3,y,R) ------- q1 q1,0,R

) (q2,y,L) ------- (q1,y,R) -------

q2 (q2,0,L) ------- (q0,x,R) (q2,y,L) ------- q3 ------- ------- ------- (q3,y,R) (q4,B,R) q4 ------- ------- ------- ------- -------

Determine if the given inputs are accepted by TM1. 0011

State Head Position qo 0011 q1 x011 q1 x011 q2 x0y1 q2 x0y1 qo x0y1 q1 xxy1 q1 xxy1

q2 xxyy q2 xxyy qo xxyy q3 xxyy q3 xxyyB q4 xxyyBB

Therefore, accepted

2. 0011

State Head Position qo 0011 01 q1 x01101 q1 x01101 q2 x0y101 q2 x0y101 qo x0y101 q1 xxy101 q1 xxy101

q2 xxyy01 q2 xxyy01

Page 31 / aav

Page 32: MELJUN CORTES Automata manual handouts

June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS

qo xxyy01 q3 xxyy01 q3 xxyy01 ?

Therefore, not accepted

Seatwork: Construct a DTM for each of the following.– {w | w is in {0,1}* and w ends in 00}– {w | w is in {0,1}* and w contains at least 2 0’s}– {w | w is in {0,1}* and w contains at least one 0 and one 1}

Q = {q0, q1, q2}Γ = {0, 1, B}Σ = {0, 1}F = {q2}δ:

0 1 B q0 (q0, 0, R) (q0, 1, R) (q1, B, L) q1 (q2, 0, R) - - q2 - - -

Page 32 / aav