Lec 3 (Finite Automata)

26
Lecture Three: Finite Automata Finite Automata, Lecture 3, slide 1 Amjad Ali

Transcript of Lec 3 (Finite Automata)

Page 1: Lec 3 (Finite Automata)

Lecture Three:Finite Automata

Finite Automata, Lecture 3, slide 1

Amjad Ali

Page 2: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 2

DFA: Deterministic Finite Automaton An informal definition (formal version later):

A diagram with a finite number of states represented by circles

An arrow points to one of the states, the unique start state

Double circles mark any number of the states as accepting states

For every state, for every symbol in , there is exactly one arrow labeled with that symbol going to another state (or back to the same state)

Page 3: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 3

DFAs Define Languages

Given any string over , a DFA can read the string and follow its state-to-state transitions

At the end of the string, if it is in an accepting state, we say it accepts the string

Otherwise it rejects The language defined by a DFA is the

set of strings in * that it accepts

Page 4: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 4

The 5-Tuple

Q is the set of states Drawn as circles in the diagram We often refer to individual states as qi

The definition requires at least one: q0, the start state is the alphabet (that is, a finite set of symbols)

A DFA M is a 5-tuple M = (Q, , , q0, F), :where Q is the finite set of states is the alphabet (that is, a finite set of symbols) (Q Q) is the transition functionq0 Q is the start stateF Q is the set of accepting states

Page 5: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 5

The 5-Tuple

F is the set of all those in Q that are accepting states Drawn as double circles in the diagram

is the transition function A function (q,a) that takes the current state q and next

input symbol a, and returns the next state Represents the same information as the arrows in the

diagram

A DFA M is a 5-tuple M = (Q, , , q0, F), where:Q is the finite set of states is the alphabet (that is, a finite set of symbols) (Q Q) is the transition functionq0 Q is the start stateF Q is the set of accepting states

Page 6: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 6

Example: This DFA defines {xa | x {a,b}*} Formally, M = (Q, , , q0, F), where

Q = {q0,q1} = {a,b} F = {q1} Start sate q0

(q0,a) = q1, (q0,b) = q0, (q1,a) = q1, (q1,b) = q0

We could just say M = ({q0,q1}, {a,b}, , q0, {q1})

q0 q1

b

a

a

b

q0

q1

q1 q0

q1 q0

a b

Page 7: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 7

A regular language is one that is L(M) for some DFA M.

For any DFA M = (Q, , , q0, F), L(M) denotes the language accepted by M, which is L(M) = {x * | *(q0, x) F}.

Regular Languages

To show that a language is regular, give a DFA for it. We'll see additional ways later

To show that a language is not regular is much harder; we'll see how later

Page 8: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 8

Example #1:

Q = {q0, q1, q2}

Σ = {a, b, c}Start state is q0

F = {q2}

a b c q0 q0 q0 q1

q1 q1 q1 q2

q2 q2 q2 q2

Since δ is a function, at each step M has exactly one option.

It follows that for a given string, there is exactly one computation.

q1q0q2

a

b

a

b

c c

a/b/c

Page 9: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 9

Example #2:

1 0 0 1 1 q0 q0 q1 q0 q0 q0

One state is final/accepting, all others are rejecting. The above DFA accepts those strings that contain an

even number of 0’s

q0q1

0

0

1

1

Page 10: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 10

Example #3:

a c c c baccepted

q0 q0 q1 q2 q2 q2

a a crejected

q0 q0 q0 q1

Accepts those strings that contain at least two c’s

q1q0q2

a

b

a

b

c c

a/b/c

Page 11: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 11

Example #4:

Give a DFA M such that:

L(M) = {x | x is a string of 0’s and 1’s and |x| >= 2}

q1q0q2

0/1

0/1

0/1

Page 12: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 12

Example #5:

Give a DFA M such that:

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}

q2q0

a

a/b/c

aq1

b/c

b/c

Page 13: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 13

Example #6:

Give a DFA M such that:

L(M) = {x | x is a string of a’s, b’s and c’s such that x

contains the substring aba}

q2q0

a

a/b/c

bq1

c

b/c a

b/c

q3

a

Page 14: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 14

Example #7:

Give a DFA M such that:

L(M) = {x | x is a string of a’s and b’s such that x contains both aa and bb}

q0

b

q7

q5q4 q6

b

b

b

a

q2q1 q3

a

a

a

b

a/bb

a

a

a b

Page 15: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 15

Example #8:

Let Σ = {0, 1}. Give DFAs for {}, {ε}, Σ*, and Σ+.

For {}: For {ε}:

For Σ*: For Σ+:

0/1

q0

0/1

q0

q1q0

0/1

0/1

0/1q0 q1

0/1

Page 16: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 16

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).

Page 17: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 17

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.

Page 18: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 18

Questions: How do we determine whether or not a

given language is regular? How could a program “simulate” a DFA?

Page 19: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 19

The * Function

The function gives 1-symbol moves We'll define * so it gives whole-string results (by

applying zero or more moves) A recursive definition:

*(q,) = q For all w in Σ* and a in Σ

*(q,wa) = (*(q,w),a) That is:

For the empty string, no moves For any string wa (w is any string and a is any final

symbol) first make the moves on w, then one final move on a

Page 20: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 20

M Accepts x

*(q,w) is the state of M ends up in, starting from state q and reading all of string w

So *(q0,w) tells us whether M accepts w :

A string w * is accepted by a DFA M = (Q, , , q0, F) if and only if *(q0, w) F.

Page 21: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 21

Example #1:

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= δ (δ (δ(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.

q0q1

0

0

1

1

Page 22: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 22

Example #2:

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

= δ (δ(δ(q0 , 0), 1),1) by definition of δ

= δ (δ(q1 , 1),1) by definition of δ

= δ (q1 , 1) by definition of δ

= q1

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

q1q0q2

1 1

00

1

0

Page 23: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 23

Example #3:

What is δ(q0, 100)?

δ(q0, 100) = δ (δ*(q0,10), 0) by rule #2

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

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

= δ (δ(δ(q0 , 1), 0),0) by definition of δ

= δ (δ(q0 , 0),0) by definition of δ

= δ (q1 , 0) by definition of δ

= q2

Is 100 accepted? Yes, since δ(q0, 100) = q2 is a final state.

0

q1q0q2

1 1

0

1

0

Page 24: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 24

Formal Definition of ComputationLet M = (Q, , , q0, F) and w = w1w2w3w4…wn be a string where each wi ∑. Then M accepts w if a sequence of states r0, r1, …, rn in Q exists with three conditions:

1. r0 = q0

2. (ri, wi+1) = ri+1 for i = 0, …, n-13. rn F

Formal Definition of DFAA DFA M is a 5-tuple M = (Q, , , q0, F) :

where Q is the finite set of states is the alphabet (that is, a finite set of symbols) (Q Q) is the transition functionq0 Q is the start stateF Q is the set of accepting states

Page 25: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 25

Example #1:

Q = {q0, q1, q2}

Σ = {a, b}Start state is q0

F = {q2}

δ : a b q0 q1 q0

q1 q2 q0

q2 q2 q2

q1q0q2

b

b

a a

a, b

Page 26: Lec 3 (Finite Automata)

Finite Automata, Lecture 3, slide 26

w = a b a b a b a a b w = w1 w2 w3 w4 w5 w6 w7 w8 w9 , |w| = 9 , n = 9

r0 = q0

(ri , wi+1) = ri+1 for i = 0, …, 8

a b a b a b a a b

q0 q1 q0 q1 q0 q1 q0 q1 q2 q2

r0 r1 r2 r3 r4 r5 r6 r7 r8 r9

1. (r0 , w1) = r1 2. (r1 , w2) = r2 3. (r2 , w3) = r3 4. (r3 , w4) = r4

(q0 , a) = q1 (q1 , b) = q0 (q0 , a) = q1 (q1 , b) = q0

5. (r4 , w5) = r5 6. (r5 , w6) = r6 7. (r6 , w7) = r7 8. (r7 , w8) = r8

(q0 , a) = q1 (q1 , b) = q0 (q0 , a) = q1 (q1 , a)= q2

9. (r8 , w9) = r9

(q2 , b) = q2 , As r9=q2 and q2 F, then String is accepted

q1q0q2

b

b

a a

a, b