Lecture 2 Finite Automata 2-0 An Example - - PDF document

lecture 2
SMART_READER_LITE
LIVE PREVIEW

Lecture 2 Finite Automata 2-0 An Example - - PDF document

The University of Melbourne Dept. of Computer Science and Software Eng. 433330 Theory of Computation Harald Sndergaard Lecture 2 Finite Automata 2-0 An Example Automaton Imagine a


slide-1
SLIDE 1

✬ ✫ ✩ ✪ The University of Melbourne

  • Dept. of Computer Science and Software Eng.

433–330 Theory of Computation Harald Søndergaard

Lecture 2

Finite Automata

2-0

slide-2
SLIDE 2

✬ ✫ ✩ ✪

An Example Automaton

Imagine a vending machine selling tea or coffee for $2. It accepts 1- and 2-dollar coins. If we let ‘1’ (‘2’) stand for the event that a 1-dollar (2-dollar) coin enters the coin slot, and C (T) stand for the push of button ‘C’ (‘T’) and subsequent delivery of a cup of coffee (tea), then the following automaton describes the acceptable event sequences:

  • 2
  • 1
  • 1,2
  • C,T
  • 1,2
  • That’s “acceptable” from a greedy owner’s point
  • f view, for example, 2T11C22C is accepted, but

111C1T is not.

2-1

slide-3
SLIDE 3

✬ ✫ ✩ ✪

Example 2

Here is an automaton for recognising unsigned number literals in some programming language:

  • 1

d

  • 2

d

  • .
  • E
  • 3

d

  • 4

d

  • E
  • 5

+,−

  • d
  • 6

d

  • 7

d

  • d is an abbreviation for 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

(that is, the digits).

2-2

slide-4
SLIDE 4

✬ ✫ ✩ ✪

Formal Definition

A finite automaton is a 5-tuple (Q, Σ, δ, q0, F), where

  • Q is a finite set of states,
  • Σ is a finite alphabet,
  • δ : Q × Σ → Q is the transition function,
  • q0 ∈ Q is the start state, and
  • F ⊆ Q are the accept states.

Here δ is a total function.

2-3

slide-5
SLIDE 5

✬ ✫ ✩ ✪

Example 3

The automaton M1:

  • q1
  • 1
  • q2

1

  • q3

0,1

  • can be described precisely as

M1 = ({q1, q2, q3}, {0, 1}, δ, q1, {q2}), where δ is given by:

1 q1 q1 q2 q2 q3 q2 q3 q2 q2

The language recognised by M1, L(M1) =        w

  • w contains at least one 1,

and an even number of 0s follow the last 1       

2-4

slide-6
SLIDE 6

✬ ✫ ✩ ✪

Example 4

  • q1

a

  • b
  • q2

b

  • a
  • s

a

  • b
  • r1

b

  • a
  • r2

a

  • b
  • Which language is recognised by this machine?

2-5

slide-7
SLIDE 7

✬ ✫ ✩ ✪

Exercise

Consider the alphabet Σ = {0, 1}. We can interpret strings in Σ∗ as numbers in binary representation. Construct an automaton over Σ to recognise exactly those numbers that are multiples of 5. Hint: Consider five states:

  • q1
  • q3
  • q0
  • q2
  • q4

2-6

slide-8
SLIDE 8

✬ ✫ ✩ ✪

Acceptance, Recognition

We can say precisely what it means for an automaton to accept a string. Let M = (Q, Σ, δ, q0, F) and let w = v1v2 · · · vn be a string from Σ∗. M accepts w iff there is a sequence of states r0, r1, . . . , rn, with each ri ∈ Q, such that

  • 1. r0 = q0
  • 2. δ(ri, vi+1) = ri+1 for i = 0, . . . , n − 1
  • 3. rn ∈ F

M recognises language A iff A = {w | M accepts w}

2-7

slide-9
SLIDE 9

✬ ✫ ✩ ✪

Regular Languages

A language is regular iff there is a finite automaton that recognises it. We shall soon see that there are languages which are not regular.

2-8

slide-10
SLIDE 10

✬ ✫ ✩ ✪

Regular Operations

Recall that for our purposes, a language is simply a set of strings. Let A and B be languages. The regular operations are:

  • Union:

A ∪ B

  • Concatenation:

A ◦ B = {xy | x ∈ A, y ∈ B}

  • Kleene star:

A∗ = {x1x2 · · · xk | k ≥ 0, each xi ∈ A} Note that the empty string, ǫ, is always in A∗.

2-9

slide-11
SLIDE 11

✬ ✫ ✩ ✪

Regular Operations (cont.)

Let A = {aa, abba} and B = {a, ba, bba, bbba, . . .}. A ∪ B = {a, aa, abba, ba, bba, bbba, . . .} A ◦ B = {aaa, abbaa, aaba, abbaba, aabba, abbabba, . . .} A∗ = {ǫ, aa, abba, aaaa, aaabba, abbaaa, abbaabba, aaaaaa, aaaaabba, aaabbaaa, aaabbaabba, . . .} The regular languages are closed under the regular operations. It will be easier to show this after we have considered non-deterministic automata.

2-10

slide-12
SLIDE 12

✬ ✫ ✩ ✪

Nondeterminism

The type of machine we have seen so far is called a deterministic finite automaton, or DFA. We now turn to non-deterministic finite automata, or NFAs. Here is an NFA for recognising the language   w

  • w ∈ {0, 1}∗ has length 3 or more,

and the third last symbol in w is 1   

  • q1

0,1

  • 1
  • q2

0,1

  • q3

0,1

  • q4

Note: No transitions from q4, and two possible transitions when we meet a 1 in state q1.

2-11

slide-13
SLIDE 13

✬ ✫ ✩ ✪

Nondeterminism (cont.)

The NFA is much easier to design and understand than a DFA for the same language:

  • 000
  • 1
  • 100
  • 1
  • 010
  • 1
  • 110
  • 1
  • 001
  • 1
  • 101
  • 1
  • 011
  • 1
  • 111
  • 1
  • This is the simplest DFA that will do the job!

2-12

slide-14
SLIDE 14

✬ ✫ ✩ ✪

Epsilon Transitions

NFAs may also be allowed to move from one state to another without consuming input. Such a transition is called an ǫ transition. Amongst other things this is useful for constructing machines to recognise unions A ∪ B

  • f languages:

machine for A

  • ǫ
  • ǫ
  • machine for B

2-13

slide-15
SLIDE 15

✬ ✫ ✩ ✪

Formal Definition

For any alphabet Σ let Σǫ denote Σ ∪ {ǫ}. An NFA is a 5-tuple (Q, Σ, δ, q0, F), where

  • Q is a finite set of states,
  • Σ is a finite alphabet,
  • δ : Q × Σǫ → P(Q) is the transition function,
  • q0 ∈ Q is the start state, and
  • F ⊆ Q are the accept states.

2-14

slide-16
SLIDE 16

✬ ✫ ✩ ✪

Acceptance, Recognition

The definition of what it means for an NFA N to accept a string says that it has to be possible to make the necessary transitions. Let N = (Q, Σ, δ, q0, F) be an NFA and let w = v1v2 · · · vn where each vi is a member of Σǫ. N accepts w iff there is a sequence of states r0, r1, . . . , rn, with each ri ∈ Q, such that

  • 1. r0 = q0
  • 2. ri+1 ∈ δ(ri, vi+1) for i = 0, . . . , n − 1
  • 3. rn ∈ F

N recognises language A iff A = {w | N accepts w}

2-15