chapter 4 finite automata - comp.utm.my · different kinds of automata •automata are...

63
Chapter 4 Finite Automata by Dr Zalmiyah Zakaria

Upload: doduong

Post on 26-Aug-2018

282 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

Chapter 4

Finite Automata

by

Dr Zalmiyah Zakaria

Page 2: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

2

Models of Computation

Page 3: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

Different Kinds of Automata

• Automata are distinguished by the temporary memory

•Finite Automata : no temporary memory

•Pushdown Automata : stack

•Turing Machines : random access memory

Sept2011 Theory of Computer Science 3

Page 4: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

4

input memory

output memory

temporary memory

Finite

Automaton

Example: Vending Machines

(small computing power)

Finite Automaton

Page 5: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

5

input memory

output memory

Stack

Pushdown

Automaton

Example: Compilers for Programming Languages

(medium computing power)

Push, Pop

Pushdown Automaton

Page 6: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

6

input memory

output memory

Random Access Memory

Turing

Machine

Examples: Any Algorithm

(highest computing power)

Turing Machine

Page 7: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

Power of Automata

Sept2011 Theory of Computer Science 7

Finite

Automata

Pushdown

Automata

Turing

Machine

Less power More power

Solve more

computational problems

Page 8: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

8

Finite Automata

http://www.youtube.com/watch?v=d7ZAcym3DUw

Page 9: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

9

• Finite - Every FA has a finite number of states

• Automata (singular automaton) means “machine”

• A simple class of machines with limited capabilities.

• Good models for computers with an extremely limited amount of memory.

• E.g., an automatic door : a computer with only a single bit of memory.

Finite Automata (FA)

Page 10: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

10

• Elevator controller – state : n-th floor

– input : input received from the buttons.

• Dishwashers

• Washing Machines

• Digital watches

• Calculators

Examples

Page 11: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

11

LIFT: • move to level 1 when person push button 1

• move to level 2 when person push button 2

• move to level 3 when person push button 3

State Diagram

Level 1

Level 2

Level 3

Page 12: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

12

INPUT

1 2 3

STATE Level1 Level1 Level2 Level3

Level2 Level1 Level2 Level3

Level3 Level1 Level2 Level3

State Transition Table States: Level1, Level2, Level3 Input: 1 someone push button 1 2 someone push button 2 3 someone push button 3

Page 13: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

13

Finite Automaton

Input

“Accept” or

“Reject”

String

Finite Automaton

Output

Page 14: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

14

• A finite automaton (FA) is a collection of 3 things:

–A finite set of states, one of them will be the

initial or start state, and some (maybe none) are final states.

–An alphabet of possible input letters.

–A finite set of transition rules that tell for each state and for each input (alphabet) which state to go to next.

Definition of FA

Page 15: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

15

– start state = q1

– final state = q2

– transitions = each arrows

– alphabet = each labels

• When this automaton receives an input string such as 1101, it processes that string and produce output (Accept or Reject).

State Diagram

Initial / start state

final / accepting

state state

transition

Page 16: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

16

Initial Configuration

1q 2q 3q 4qa b b a

5q

a a bb

ba,Input String

a b b a

ba,

0q

Page 17: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

17

Reading the Input

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b b a

ba,

Page 18: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

18

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b b a

ba,

Page 19: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

19

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b b a

ba,

Page 20: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

20

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b b a

ba,

Page 21: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

21

0q 1q 2q 3q 4qa b b a

accept

5q

a a bb

ba,

a b b a

ba,

Input finished

Page 22: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

22

Rejection

1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b a

ba,

0q

Page 23: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

23

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b a

ba,

Page 24: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

24

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b a

ba,

Page 25: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

25

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b a

ba,

Page 26: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

26

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

reject

a b a

ba,

Input finished

Page 27: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

27

Another Rejection

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,

0q

Page 28: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

28

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,

0q

reject

Page 29: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

30

• A finite automaton is a 5-tuple (Q, , , q0, F) where – Q is a finite set of states,

– is a finite set of alphabets,

– : Q x Q is the transition function,

– q0 Q is the start state, and

– F Q is the set of accept states (final states)

Formal Definition

Note: - delta

Page 30: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

31

• M1= (Q,,,q1,F) , where – Q = {q0, q1},

– = {a, b},

– is described as

– q0 is the start state, and

– F = {q0, q1}.

a b

q0 q0 q1

q1 q0 q1

Example : Finite Automaton M1

b

a

ba

q1q0

Page 31: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

32

L(M1) = (a + b)*1+00+01)*

Example : Finite Automaton M1

What is the language of

M1?

b

a

ba

q1q0

Page 32: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

33

Example : Finite Automaton M2

• M2 = (Q, , , q0, F) , where – Q =

– =

– is described as

– q1 is the start state, and

– F = { }.

0 1

q1

q2

q1

0 1 1

q2

0

Page 33: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

34

Example : Finite Automaton M2

What is the language of M2?

L(M2) = {w | w ends in a 1}

q1

0 1 1

q2

0

Page 34: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

35

• If the start state is also a final state, what string does it automatically accept ?

• L(M3) = { w | w is the empty string or ends in a 0}

q1 q2

0

0

1 1

Empty String

Page 35: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

36

Example : Finite Automaton M4

• M4= (Q, , , S, F) , where – Q =

– =

– is described as

– S is the start state, and

– F = { q1, r1 }.

r1 a b

S q1 r1

q1 q1 q2

q2 q1 q2

r1 r2 r1

r2 r2 r1

a

S

q1 r1

q2 r2

a

a a

a b

b

b b

L(M4) = strings that start and end with the same symbol.

# a(a + bb*a)* + b(b + aa*b)*

b

Page 36: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

FA Design : Example

• Question: Design an FA to accept the language of all strings over {0, 1} that contain the string 001 as a substring, e.g.: accept 0010, 1001, 001, 111001111, but not 11, 000.

• Thinking of… for every input, skip over all 1s. If we read 0, note that we may have just seen the first of the 3 symbols 001. Then if we see a 1, go back to skipping over 1s. Then … proceed … to seek for another two 0s ...

Theory of Computer Science 37

Page 37: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

Example : Strings with substring 001

• So we can conclude there are four possibilities: – Haven’t just seen any symbols of 001

– Have just seen a 0

– Have just seen 00

– Have seen 001

• Assign the states q, q0, q00, q001 to these possibilities. We can design the transition by observing that from q reading a 1, we stay in q, but reading a 0 we move to q0. In q0, reading a 1 we return to q, but reading a 0 we move to q00. In q00, reading a 1 we move to q001, but reading a 0 leaves us in q00. finally, in q001 reading a 0 or a 1 leaves us in q001.

Theory of Computer Science 38

Page 38: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

Example : Strings with substring 001

• The start state is q, and the only accept state is q001.

or

Theory of Computer Science 39

Page 39: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

Computation of FA, ⊢M

• Way to describe the computation of a DFA

• What state the DFA is currently in, and what string is left to

process

– (q0, 0010) Machine is in state q0, has 0010 left to process

– (q1, 010) Machine is in state q1, has 010 left to process

– (q3, ) Machine is in state q3 at the end of the

computation (accept iff q3 ∈ F)

• Binary relation ⊢M : What machine M yields in one step

– (q0, 0010) ⊢M (q1, 010)

Theory of Computer Science 43

Page 40: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

Accepted @ Rejected Strings

• To trace all computations that process the string 0010.

• [q0 , 0010] ⊢ [q1 , 010] ⊢ [q2 , 10] ⊢ [q3 , 0] ⊢ [q3 , λ]

• Stop at q3 (final state) and all alphabet is traced (string is empty);

• Hence, string 0010 is accepted by machine.

Theory of Computer Science 44

Page 41: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

45

• So far, every step of a computation follows in a unique way from the preceding step.

• When the machine is in a given state and reads the next input symbol, we know what the next state will be – it is called deterministic computation

• Deterministic Finite Automata (DFA)

Determinism

Page 42: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

Why DFA?

• Why are these machines called “Deterministic Finite Automata”

• Deterministic - Each transition is completely determined by the current state and next input symbol. That is, for each state / symbol pair, there is exactly one state that is transitioned to.

Sept2011 Theory of Computer Science 46

Page 43: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

47

• In a nondeterministic machine, several choices may exist for the next state at any point.

• Nondeterministic Finite Automata (NFA)

• Nondeterminism is a generalization of the determinism, so every DFA is automatically a NFA.

Nondeterminism

Page 44: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

48

NFA

Example of DFA vs. NFA

DFA

Page 45: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

49

Differences between DFA & NFA

• Every state of DFA always has exactly one exiting transition arrow for each symbol in the alphabet while the NFA may has zero, one or more.

• In a DFA, labels on the transition arrows are from the alphabet while NFA can have an arrow with the label .

Page 46: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

55

• When we are at a state with multiple choices to proceed

(including symbol), the machine splits into multiple copies of itself and follow all the possibilities in parallel.

• Each copy of the machine takes one of possible ways to proceed and continuous as before. If there are subsequent choices, the machine splits again.

• If the next input symbol doesn’t appear on any of the arrows exiting the state occupied by a copy of the machine, that copy dies.

• If any one of these copies is in an accept state at the end of the input, the NFA accepts the input string.

How does the NFA work?

Page 47: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

56

• Think of a nondeterministic computation as a tree of possibilities

• The root of the tree corresponds to the start of the computation.

• Every branch point in the tree corresponds to a point in the computation at which the machine has multiple choices.

• The machine accepts if at least one of the computation branches ends in the an accept state.

Tree of possibilities

Page 48: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

57

start

accept or reject

Deterministic computation

reject

accept

Nondeterministic computation

Tree of possibilities

Page 49: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

58

q1

q2

q1

q1

q2 q3 q1

q1

q3

Start

Example: 010110

0

Read symbol

1

0

1

0

1

q4 q4

q1

q3

q3 q1 q2 q4

q4

q4

q3

Page 50: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

59

• Every NFA can be converted into an equivalent DFA.

• Constructing NFAs is sometimes easier than directly construction DFAs.

• NFA may be much smaller than it DFA counterpart.

• NFA’s functioning may be easier to understand.

• Good introduction to non-determinism in more powerful computational models because FA are especially easy to understand.

Properties of NFA

Page 51: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

60

Example: Converting NFA into DFA

NFA: recognizes language which contains 1 in the third position from the end

Equivalent DFA:

Page 52: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

61

• A NFA is a 5-tuple (Q, , , q0, F) , where

– Q is a finite set of states,

– is a finite alphabet,

– : Q x P(Q) is the transition function,

– q0 is the start state, and

– F Q is the set of accept states.

Notation: P(Q) is called power set of Q (a collection of all subsets of Q).

and = {}

Formal definition of NFA

Page 53: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

62

Example: Formal definition of NFA

• Formal definition of N1 is (Q, , , q0, F) , where

– Q = {q1,q2,q3,q4}

– = {0,1}

– is given as

– q0 is the start state, and

– F = {q4}

0 1

q1 {q1} {q1,q2}

q2 {q3} {q3}

q3 {q4}

q4 {q4} {q4}

NFA M1:

Page 54: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

Example of NFA

• L = {w ∈ {a, b} : w starts with a}

• What is the RE for the language? a(a+b)*

• What happen if the 1st input is b?

Theory of Computer Science 63

Page 55: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

Example of NFA

• L = {w ∈ {a, b} : w contains the substring aa}

• What is the RE for the language? (a+b)*aa(a+b)* • What happen if the 1st input is a? • Does the machine accept abaa?

(q0, abaa) (q0, baa) (q0,aa) (q0, a) (q0, e) reject (q1, e) reject (q1, baa) reject (q1, a) (q2, e) accept

Sept2011 Theory of Computer Science 64

Page 56: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

65

Definition:

• A language L is regular if there is FA M

such that L = L(M)

Observation:

• All languages accepted by FAs form the

family of regular languages

Regular Languages

Page 57: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

66

• Examples of regular languages:

{abba} {, ab, abba}

{awa : w {a, b}*} {anb : n 0}

{all strings with prefix ab}

{all strings without substring 001}

There exist automata that accept these languages.

Regular Languages

Page 58: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

67

• There exist languages which are not regular :

• Example :

L = {anbn : n 0}

There is no FA that accepts such a language.

Non-Regular Languages

Page 59: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

68

Example of DFA

• L(M) = {anb : n 0}

a

b ba,

ba,

0q 1q 2q

accept trap state

Page 60: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

84

Example (NFA or DFA ?) • Create a FA for L(M) = {awa : w {a, b}*}

• What happen if the 1st input is b? • What happen if the input are bab, abab, … ?

a a

,a b

0q 2q 3q

Page 61: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

85

Example of DFA

• L(M) = {awa : w {a, b}*}

a

b

ba,

a

b

ba

0q 2q 3q

4q

Page 62: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

86

Example

• L(M) = {all strings without substring 001}

0 00 001

1

0

1

10

0 1,0

Page 63: Chapter 4 Finite Automata - comp.utm.my · Different Kinds of Automata •Automata are distinguished by the temporary memory •Finite Automata : no temporary memory •Pushdown Automata

87

Example

• L(M) = {all strings with prefix ab}

a b

ba,

0q 1q 2q

accept

ba,3q

ab