Theory of computation Lec3 dfa

42
Theory of Computation Lecture 3: Finite Automata Cairo University FCI Dr. Hussien Sharaf Computer Science Department [email protected] CS 611

description

Theory of computation

Transcript of Theory of computation Lec3 dfa

Page 1: Theory of computation Lec3 dfa

Theory of Computation

Lecture 3: Finite Automata

Cairo UniversityFCI

Dr. Hussien SharafComputer Science [email protected]

CS 611

Page 2: Theory of computation Lec3 dfa

2

Finite Automata FAIt started as a simple automatic device with no

memory. We still need it to study how to model a device and model its capability.

Its goal is to act as a recognizer for specific a language/pattern.

Any problem can be presented in form of a decidable problem that can be answered by Yes/No.

A problem can be concatenated to one of its possible solutions and can be seen as a string that matches a pattern.

Hence FA (machine with limited memory) can solve any problem.

Dr. Hussien M. Sharaf

Page 3: Theory of computation Lec3 dfa

3

FA = “a 5-tuple “ (Q, Σ, , q0, F)

1. Q: {q0, q1, q2, …} is set of states.

2. Σ: {a, b, …} set of alphabet.3. (delta): represents the set of transitions

that FA can take between its states.: Q x Σ→Q

Q x Σ to Q, this function: Takes a state and input symbol as arguments. Returns a single state.

4. q0 Q is the start state.

5. F Q is the set of final/accepting states.Dr. Hussien M. Sharaf

Deterministic Finite Automata DFA

Page 4: Theory of computation Lec3 dfa

: Q x Σ→QMaps from domain of (state, letter) to range of

states.

Transition function

Q x ΣQ

(q0, a)

(q2, b)(q1, b)

q1

q2

q3

Dr. Hussien M. Sharaf 4

(q0, b)

Page 5: Theory of computation Lec3 dfa

Lets name it Conditional consumption instead of transition

λ: Q x Q → ΣMaps from domain of (states, states) to range of

letters.

Renaming function to λ

(Lamda)

Q x Q Σ(q0, q1)(q2, q3)(q1, q3)

a

Dr. Hussien M. Sharaf 5

b

c

Allows more than one link from domain to codomain

Not recommended

Page 6: Theory of computation Lec3 dfa

6

How does FA work?1. Starts from a start state.2. Loop

Reads a letters from the input

3. Until input string finishes4. If the current state is a final state then

Input string is accepted.

5. ElseInput string is NOT accepted.

But how can FA be designed and represented?

Dr. Hussien M. Sharaf

Page 7: Theory of computation Lec3 dfa

7

Representation of FA: GraphStates= nodes

Starting state denoted by –Final state(s) denoted by +

Transition function =directed arrows connecting states.

Build an FA for RE “a*b(a+b)*”

Dr. Hussien M. Sharaf

S1

- +S2b

a a,b

Page 8: Theory of computation Lec3 dfa

8

Representation of FA: Table

Dr. Hussien M. Sharaf

a b-s1 s1 s2

+s2 s2 s2

Rows = states

input symbols

Final statesIndicated by “+”

“-” sign forstart state

Page 9: Theory of computation Lec3 dfa

9

Example1.1Build an FA that accepts only aab

Dr. Hussien M. Sharaf

S1-

S3

aS2

a b+S4

S5

bb a a,b

a bS1 S2 S5

S2 S3 S5

S3 S5 S4

S4 S5 S5

a,b

Page 10: Theory of computation Lec3 dfa

10

Example1.2Build an FA that accepts only aab

Dr. Hussien M. Sharaf

S1-

S3

aS2

a b+S4

a bS1 S2 ?

S2 S3 ?

S3 ? ?

S4 ? ?

Page 11: Theory of computation Lec3 dfa

11

Ex2(a+b)*

Dr. Hussien M. Sharaf

a, b

±

Page 12: Theory of computation Lec3 dfa

12

FA accepting nothing1. FA with no final states

Dr. Hussien M. Sharaf

a

-

a,b

b

2. FA with disconnected graph. Start state does not have a path to the final state.

a

-

a,b

b

+b

Page 13: Theory of computation Lec3 dfa

13

Ex3All words with even count of letters.

((a+b)(a+b))*

Dr. Hussien M. Sharaf

2

a, b

a, b

Page 14: Theory of computation Lec3 dfa

14

Ex4.1All words that start with “a”

a(a+b)*

Dr. Hussien M. Sharaf

1-

2

b

a 3 + a,

b

1-

2b

a 3 +

a,b

a,b Does not accept all inputs

Page 15: Theory of computation Lec3 dfa

15

Ex4.2All words that start with “a”

a(a+b)*

Dr. Hussien M. Sharaf

1-

2b

a 3 +

a,b

a,b

Special accept state for string “a”, might give better performance in hardware implementation

Page 16: Theory of computation Lec3 dfa

16

Ex5All words that start with triple letter

(aaa+bbb)(a+b)*

Dr. Hussien M. Sharaf

1-

2a 3

a,b

4b 5b

6+

b

a a

Page 17: Theory of computation Lec3 dfa

17

Ex6

{aa, ba, baba, aaaa, bbba, abba, aaabaa, …}

All words with even count of letters and ends with “a”. (a+b)a ((a+b)a (b(a+b)a)* )*

Dr. Hussien M. Sharaf

-a,b

+

a,b

5b

a

a,b

Page 18: Theory of computation Lec3 dfa

18

Ex7

{aa, ba, baba, aaaa, ab, bb, bababa, aaba, …}

All words with even count of letters having “a” in an even position from the start, where the first letter is letter number one.

(a+b)a((a+b)a)*Dr. Hussien M. Sharaf

-

a,b

Page 19: Theory of computation Lec3 dfa

19

Ex8 Cohen 2nd edition page 63Consider the following FA:

Dr. Hussien M. Sharaf

•Whenever aa exists, the first “a” must take us to state 4 or state 2.

•Either way, the next a takes us to state 4. Similar situation with “bb”.In summary: this FA accepts strings that have a double letter in them.(a + b)*(aa + bb)(a + b)*

Page 20: Theory of computation Lec3 dfa

20

Ex9 Cohen 2nd edition page 64Consider the following FA:

Dr. Hussien M. Sharaf

•Accepts all words with b as the third letter and reject all other words.

•A word that has fewer than three letters fails.

RE1: (aab + abb + bab + bbb)(a + b)*OR :(a + b)(a + b)(b)(a + b)* = (a + b)2 b (a + b)*Notice that this last formula is not strictly speaking a regular expression, since it uses the symbol “2”

Page 21: Theory of computation Lec3 dfa

21

Have you see any memory?Ex10All strings that end in ab.Do we need a memory to remember the last two letters?

Dr. Hussien M. Sharaf

-

a

b

……

+

a

b

a

b

a

b

b

b

b

b

a

Page 22: Theory of computation Lec3 dfa

22

Ex11Even-Even :All the words that end in

state 3 have an even number of b's but an odd number of a's.

All words that end in state 4 have an odd number of a's and an odd number of b's.

Any EVEN-EVEN words must end in state 1 and be accepted.

Dr. Hussien M. Sharaf

Page 23: Theory of computation Lec3 dfa

23Dr. Hussien M. Sharaf

Automaton

CPU

input memory

output memory

Program memory

temporary memory

Automaton

Page 24: Theory of computation Lec3 dfa

24Dr. Hussien M. Sharaf

Finite Automaton

Input

String

Output

String

FiniteAutomaton

Page 25: Theory of computation Lec3 dfa

25Dr. Hussien M. Sharaf

Different Kinds of AutomataAutomata are distinguished by the temporary memory:

• Finite Automata: no temporary memory

• Pushdown Automata: stack

• Turing Machines: random access memory

Page 26: Theory of computation Lec3 dfa

26Dr. Hussien M. Sharaf

Finite

Automata

Pushdown

Automata

Turing

Machine

Power of Automata

Page 27: Theory of computation Lec3 dfa

27

Non Deterministic Finite Automata NFAThere is a fixed number of states but we can be in multiple

states at one time.

NFA = “a 5-tuple “ (Q, Σ, , q0, F)Q A finite set of statesΣ A finite input alphabet union {Λ}q0 The initial/starting state, q0 is in QF A set of final/accepting states, which is a subset of Qδ A transition function, which is a total function from Q x Σ to 2Q , this function:

Takes a state and input symbol as arguments. Returns a set of states instead a single state as in

DFA.δ: (Q x Σ) –> 2Q -2Q is the power set of Q, the set of

all subsets of Q δ(q,s) is a function from Q x S to 2Q (but not to Q)

Dr. Hussien M. Sharaf

Page 28: Theory of computation Lec3 dfa

NFAA finite automaton is deterministic if It has no edges/transitions labeled with

epsilon/lamda. For each state and for each symbol in the

alphabet, there is exactly one edge labeled with that symbol.

Page 29: Theory of computation Lec3 dfa

29

1q 2q

3q

a

a

a

0q

}{aAlphabet =

NFA NFA travels all possible paths, and so it remains in many states

at once. As long as at least one of the paths results in an accepting state, the NFA accepts the input.

Dr. Hussien M. Sharaf

Page 30: Theory of computation Lec3 dfa

30

1q 2q

3q

a

a

a

0q

Two choices

}{aAlphabet =

NFADr. Hussien M. Sharaf

Page 31: Theory of computation Lec3 dfa

31

No transition

1q 2q

3q

a

a

a

0q

Two choicesNo transition

}{aAlphabet =

NFADr. Hussien M. Sharaf

Page 32: Theory of computation Lec3 dfa

32

An NFA accepts a string:if there is a computation of the NFAthat accepts the string

i.e., all the input string is processed and the automaton is in an accepting state

NFADr. Hussien M. Sharaf

Page 33: Theory of computation Lec3 dfa

33

a a

0q

1q 2q

3q

a

a

Acceptance Example 1

a

NFADr. Hussien M. Sharaf

Page 34: Theory of computation Lec3 dfa

34

a a

0q

1q 2qa

a

a

NFADr. Hussien M. Sharaf

3q

Page 35: Theory of computation Lec3 dfa

35

a a

0q

a

a

a

NFADr. Hussien M. Sharaf

3q “reject”

2q “accept”1q

Page 36: Theory of computation Lec3 dfa

36

aa is accepted by the NFA:

0q

1q 2q

3q

a

a

a

“accept”

0q

1q 2qa

a

a

3q “reject”

because this computationaccepts aa

this computationis ignored

NFADr. Hussien M. Sharaf

Page 37: Theory of computation Lec3 dfa

37Dr. Hussien M. Sharaf

An NFA rejects a string:if there is no computation of the NFAthat accepts the string.

• All the input is consumed and the automaton is in a non final state

• The input cannot be consumed (indecidable input)

OR

For each computation:

NFA

Page 38: Theory of computation Lec3 dfa

38

a is rejected by the NFA:

0q

1q 2qa

a

a

3q “reject”

0q

1q 2qa

a

a

3q

“reject”

All possible computations lead to rejection

NFA

Dr. Hussien M. Sharaf

Page 39: Theory of computation Lec3 dfa

39

aaais rejected by the NFA:

0q

1q 2q

3q

a

a

a

“reject”

0q

1q 2qa

a

a

3q “reject”

All possible computations lead to rejection

NFA

Dr. Hussien M. Sharaf

Page 40: Theory of computation Lec3 dfa

40

Deterministic and Nondeterministic Automata

Deterministic Finite Automata (DFA)One transition per input per state No -moves

Nondeterministic Finite Automata (NFA)Can have multiple transitions for one input in a

given stateCan have -moves

Page 41: Theory of computation Lec3 dfa

41

Assignment 4Proof that NFA and DFA are equivalent.

Constructive type of proofs might be the most appropriate.

Be formal and simple. Don’t exceed 2-3 slides.You might study the proof from “Sipser” book

or “Models Of Computation”.Your answer might be delivered in form of

slides to Eng. Eman“[email protected]” and CC to my email [email protected].

You will present your slides in 5-7 minutes.

Dr. Hussien M. Sharaf

Page 42: Theory of computation Lec3 dfa

42

Thank you

Dr. Hussien M. Sharaf