30.10.2014 Views

Deterministic Finite Automata (DFA) Nondeterministic Finite Automata

Deterministic Finite Automata (DFA) Nondeterministic Finite Automata

Deterministic Finite Automata (DFA) Nondeterministic Finite Automata

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Determismistic & <strong>Nondeterministic</strong><br />

<strong>Finite</strong> <strong>Automata</strong><br />

Martin Fränzle<br />

Informatics and Mathematical Modelling<br />

The Technical University of Denmark<br />

02140 Languages and Parsing, MF, Fall 2004 – p.1/22<br />

Preliminaries for Formal Languages:<br />

Alphabets, strings, languages<br />

02140 Languages and Parsing, MF, Fall 2004 – p.2/22


£<br />

¤<br />

£ ¢<br />

¡<br />

£<br />

What you’ll learn<br />

You will be introduced to the basic notions of<br />

Alphabet: A set of “symbols”,<br />

String/word: a sequence of symbols from the alphabet,<br />

Language: a set of strings.<br />

02140 Languages and Parsing, MF, Fall 2004 – p.3/22<br />

Alphabets<br />

Def.: An alphabet is a finite, nonempty set.<br />

Ex.:<br />

The binary alphabet ¥,<br />

the set of all printable ASCII characters,<br />

the set<br />

¡red<br />

£green £blue<br />

yellow £magenta cyan<br />

¥of basic colors.<br />

N.B.: This notion of alphabet is distinctly different from everyday use:<br />

alphabets need not feature an ordering,<br />

the members of an alphabet need not be letters in the usual<br />

sense, but can well be compounds of some kind (e.g.,<br />

tuples).<br />

We will often denote alphabets by the greek letter<br />

¦.<br />

02140 Languages and Parsing, MF, Fall 2004 – p.4/22


§<br />

<br />

<br />

<br />

<br />

<br />

<br />

§<br />

<br />

§<br />

<br />

£<br />

<br />

<br />

<br />

¦<br />

¡<br />

§<br />

¨<br />

¢<br />

£<br />

¤<br />

©<br />

<br />

<br />

§<br />

<br />

§<br />

<br />

<br />

<br />

¢<br />

¤<br />

¢<br />

£ ¢<br />

£ ¢<br />

¢ £<br />

¤ ¢<br />

© <br />

<br />

¡<br />

£ <br />

¥ <br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

§<br />

§<br />

<br />

<br />

§<br />

<br />

<br />

¦<br />

<br />

<br />

¦<br />

<br />

<br />

<br />

<br />

<br />

'<br />

<br />

¦<br />

!<br />

&<br />

<br />

<br />

%<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

¦<br />

§<br />

<br />

¦<br />

<br />

<br />

<br />

<br />

<br />

<br />

¦<br />

¦<br />

<br />

<br />

<br />

'<br />

¦<br />

&<br />

%<br />

<br />

<br />

$<br />

¦<br />

¦<br />

<br />

<br />

¦<br />

Strings (a.k.a. words)<br />

Def.: A string (or word) over an Alphabet<br />

symbols from ¦.<br />

Ex.:<br />

The length of a string §, denoted<br />

for symbols in the string.<br />

is a finite sequence of<br />

¨, is the number of positions<br />

(or, more formally, ) is a string of length 4 over alphabet<br />

¥.<br />

is the string of length 0 over alphabet (as well as over any<br />

other alphabet). It is called the empty string and is generically denoted<br />

, irrespective of the particular alphabet.<br />

Def.: Given strings<br />

the concatenation of<br />

of length<br />

.<br />

def<br />

and<br />

and<br />

is the string<br />

,<br />

02140 Languages and Parsing, MF, Fall 2004 – p.5/22<br />

Powers of an alphabet<br />

Given an alphabet<br />

over<br />

and a natural number<br />

is the set of all strings over<br />

def<br />

def<br />

¦,<br />

"!<br />

#<br />

of length<br />

N,<br />

,<br />

is the set of all nonempty strings<br />

is the set of all strings over<br />

¦.<br />

02140 Languages and Parsing, MF, Fall 2004 – p.6/22


* 5<br />

¥<br />

<br />

¡<br />

6<br />

¥<br />

¦<br />

)<br />

<br />

(<br />

§<br />

,<br />

3<br />

+<br />

=<br />

¤<br />

¥ <<br />

(<br />

+<br />

3<br />

£<br />

¢<br />

¡<br />

¥<br />

<br />

*<br />

<br />

<br />

¥<br />

<br />

<br />

¤<br />

<br />

¡<br />

¡<br />

¥ <br />

<br />

<br />

£<br />

<br />

3 ¥<br />

<br />

£<br />

£ 1<br />

£<br />

)<br />

)<br />

<br />

¤<br />

¤ ¡<br />

£<br />

<br />

£ ¢<br />

;<br />

£ /<br />

¥ :<br />

¤<br />

¤<br />

¤<br />

¦<br />

7<br />

7<br />

¤<br />

£<br />

£<br />

¤<br />

<br />

¤ ¢<br />

¤<br />

<br />

¢<br />

<br />

£ 1<br />

¢<br />

£ 9<br />

¢ £<br />

¢<br />

¡<br />

¢<br />

¤ £<br />

¡<br />

£<br />

<br />

¢<br />

£ ¢<br />

¤<br />

(<br />

¥ -<br />

£ 0<br />

¡<br />

£<br />

;<br />

£ /<br />

£ .<br />

<br />

¤<br />

¥ <<br />

<br />

¤ ¢<br />

¡<br />

£<br />

<br />

-<br />

<br />

£<br />

<br />

¤<br />

£<br />

£<br />

6<br />

<br />

¤ ¢<br />

¤ ¡<br />

<br />

¢<br />

<br />

;<br />

£ ¢<br />

£<br />

¡<br />

§<br />

¤ ¢<br />

¥ :<br />

£<br />

¡<br />

<br />

<br />

<br />

£ 9<br />

¡<br />

8<br />

)<br />

)<br />

¥<br />

3<br />

7<br />

(<br />

4 <br />

2<br />

¡<br />

Languages<br />

Def.: A set of strings over an alphabet<br />

called a language over ¦.<br />

Ex.:<br />

The set of strings consisting of<br />

N forms a language over<br />

The set of strings over<br />

language over<br />

is a language over<br />

is a language (over arbitrary alphabet),<br />

¥:<br />

2,<br />

(i.e., a subset of<br />

0’s followed by<br />

¥:<br />

$) is<br />

1’s for arbitrary<br />

representing a prime number forms a<br />

is a language (over arbitrary alphabet).<br />

Note:<br />

Note: Languages can be finite or infinite!<br />

N<br />

4.<br />

02140 Languages and Parsing, MF, Fall 2004 – p.7/22<br />

Problems<br />

Def.: Given a language over some alphabet ¦, the problem<br />

Given a string over ¦, decide whether or not 6.<br />

Ex.:<br />

Problem of deciding whether some<br />

representation of a prime number.<br />

Problem of deciding whether some<br />

english verb.<br />

Problem of deciding whether a string<br />

is a binary<br />

is:<br />

is the imperfect of an<br />

describes<br />

a game of chess where ‘white’ has won,<br />

a game of chess that has reached a position where ‘white’ can<br />

enforce a win.<br />

Questions concerning a given problem are in particular<br />

whether it can be solved by a computer,<br />

if so, what would be the best algorithm to do so,<br />

how much computational effort this requires.<br />

02140 Languages and Parsing, MF, Fall 2004 – p.8/22


@<br />

£<br />

<br />

<br />

£ ?<br />

<br />

?<br />

£<br />

?<br />

£<br />

<br />

? £<br />

£ <br />

¡<br />

¡<br />

¥<br />

¥<br />

<br />

<br />

<br />

<br />

<br />

?<br />

<br />

?<br />

<br />

<br />

<br />

?<br />

£ ?<br />

<br />

?<br />

<br />

<br />

<br />

<br />

£ ?<br />

?<br />

?<br />

<br />

£ ?<br />

?<br />

<br />

><br />

<strong>Finite</strong> <strong>Automata</strong><br />

The deterministic variant: <strong>DFA</strong>s<br />

02140 Languages and Parsing, MF, Fall 2004 – p.9/22<br />

<strong>DFA</strong>s — informally<br />

An automaton is a computational device that changes its state depending<br />

on the inputs it receives.<br />

A finite automaton has finitely many states for remembering its computation<br />

history.<br />

In a deterministic finite automaton, the current state is completely<br />

determined by the sequence of inputs seen so far.<br />

<strong>Automata</strong> define languages through the following mechanism:<br />

1. Some states are marked as being “accepting”;<br />

2. whenever the automaton is in an accepting state, the string of inputs<br />

seen so far is a member of the language accepted by the automaton.<br />

a<br />

b<br />

a<br />

b<br />

a<br />

a<br />

b<br />

b<br />

a,b<br />

02140 Languages and Parsing, MF, Fall 2004 – p.10/22


N<br />

E<br />

E<br />

H<br />

G<br />

F<br />

E<br />

D<br />

H<br />

¦<br />

G<br />

<br />

<br />

F<br />

C<br />

E<br />

B<br />

D<br />

<br />

H M<br />

¦<br />

<br />

A<br />

L<br />

<br />

E<br />

<br />

E<br />

C<br />

G<br />

B<br />

D<br />

K<br />

B<br />

<br />

¦<br />

A<br />

J<br />

C<br />

F<br />

E<br />

E<br />

I<br />

D<br />

<br />

C<br />

<br />

F<br />

E<br />

N<br />

E<br />

E<br />

<br />

H M<br />

<br />

E<br />

B<br />

D<br />

<strong>DFA</strong>s — formally<br />

A deterministic finite automaton (<strong>DFA</strong>)<br />

1. a finite set of states C,<br />

2. a finite set of input symbols ¦, called the “alphabet of<br />

3. a transition function<br />

4. a start state C,<br />

5. a set of final or accepting states<br />

C,<br />

C.<br />

consist of<br />

A”,<br />

The automaton<br />

1. starts in<br />

2. proceeds from state<br />

F,<br />

to state<br />

if the current input is M,<br />

3. accepts a string ( sequence of symbols) iff that sequence of<br />

inputs drives it from to some G.<br />

02140 Languages and Parsing, MF, Fall 2004 – p.11/22<br />

Transition diagrams<br />

are a graphical notation for FAs.<br />

A transition diagram for a <strong>DFA</strong><br />

1. For each state in<br />

2. if<br />

labelled<br />

there is a node,<br />

is a graph s.t.<br />

then (and only then) there is an arc from<br />

M, (we allow ourselves to collate multiple arcs between the same<br />

nodes into a single one labelled with a list)<br />

3. there is an arrow without source node into the start state,<br />

4. nodes representing accepting states are marked with double<br />

circles.<br />

to<br />

a<br />

b<br />

a<br />

b<br />

a<br />

a<br />

b<br />

b<br />

a,b<br />

02140 Languages and Parsing, MF, Fall 2004 – p.12/22


M<br />

C<br />

¦<br />

K<br />

<br />

H<br />

S<br />

$<br />

¦<br />

B<br />

J<br />

<br />

H<br />

C<br />

E<br />

S<br />

<br />

I<br />

H M<br />

R<br />

D<br />

<br />

E<br />

E <br />

E<br />

B<br />

E<br />

P<br />

D<br />

B<br />

R<br />

D<br />

H M<br />

E <br />

E<br />

E <br />

O<br />

<br />

<br />

<br />

<br />

H<br />

H<br />

E<br />

S<br />

<br />

E <br />

F E<br />

Y<br />

T<br />

E<br />

M<br />

W<br />

V<br />

K<br />

<br />

U<br />

E<br />

<br />

E<br />

B<br />

R<br />

D<br />

D B<br />

T<br />

B<br />

D<br />

E<br />

<br />

<br />

K<br />

H M<br />

H<br />

S<br />

&<br />

¦<br />

<br />

<br />

E<br />

E<br />

J<br />

B<br />

B<br />

R<br />

D<br />

R<br />

D<br />

C<br />

I<br />

D<br />

Transition tables<br />

are tabular representations of transition functions, where information<br />

about initiality and finality of states is added:<br />

F Q<br />

As the state space and the alphabet can be deduced from the<br />

table, transition tables are a complete representation of <strong>DFA</strong>s!<br />

02140 Languages and Parsing, MF, Fall 2004 – p.13/22<br />

Extending the trans. fct. to strings<br />

Given<br />

recursion:<br />

Base case:<br />

Recursion:<br />

def<br />

def<br />

C, we define a function<br />

for each<br />

Recursion<br />

C;<br />

for each<br />

R<br />

S V U X<br />

W<br />

C,<br />

$,<br />

via<br />

¦.<br />

N.B.:<br />

We could as well have defined<br />

def<br />

without altering the function defined.<br />

02140 Languages and Parsing, MF, Fall 2004 – p.14/22


A<br />

H G<br />

<br />

<br />

F<br />

\<br />

E<br />

G<br />

H<br />

D<br />

A<br />

<br />

6<br />

B<br />

C<br />

B<br />

<br />

[[<br />

A<br />

A<br />

H<br />

§<br />

<br />

A<br />

6<br />

B<br />

¦<br />

6<br />

H<br />

<br />

F<br />

6<br />

E<br />

B<br />

R<br />

D<br />

$<br />

¦<br />

<br />

A<br />

§<br />

Z<br />

<br />

H<br />

H<br />

A<br />

B<br />

B<br />

6<br />

6<br />

H<br />

A<br />

B<br />

6<br />

<br />

6<br />

The language of a <strong>DFA</strong><br />

Def: The language<br />

of a a <strong>DFA</strong><br />

def<br />

is<br />

Def: If<br />

The language thus is the set of strings that drive<br />

the initial state to some accepting state.<br />

from<br />

for some <strong>DFA</strong> then we call a regular language.<br />

The regular languages are exactly those languages 6which are<br />

definable by <strong>DFA</strong>s in the sense that for some <strong>DFA</strong> A.<br />

02140 Languages and Parsing, MF, Fall 2004 – p.15/22<br />

<strong>Finite</strong> <strong>Automata</strong><br />

The nondeterministic variant: NFAs<br />

02140 Languages and Parsing, MF, Fall 2004 – p.16/22


H G<br />

<br />

F<br />

E<br />

D<br />

¦<br />

C<br />

B<br />

<br />

A<br />

= <br />

)<br />

]<br />

K<br />

¦<br />

J<br />

D<br />

C<br />

J<br />

C<br />

_<br />

a £<br />

L<br />

¦<br />

B<br />

8<br />

b<br />

G<br />

J<br />

C<br />

^<br />

N<br />

a<br />

I<br />

D<br />

`<br />

_<br />

¡<br />

*<br />

<br />

F<br />

E<br />

`<br />

_<br />

NFAs — informally<br />

Has the freedom to do various different moves when in a state<br />

and seeing some input,<br />

has the help of an “oracle” for performing the right guess,<br />

this is modeled mathematically as<br />

1. the ability to be in various states at once, and<br />

2. accepting a string whenever at least one of those states is<br />

accepting.<br />

a,b<br />

b<br />

b<br />

a<br />

02140 Languages and Parsing, MF, Fall 2004 – p.17/22<br />

NFAs — formally<br />

An nondeterministic finite automaton (NFA)<br />

consist of<br />

1. a finite set of states C,<br />

2. a finite set of input symbols ¦, called the “alphabet of<br />

3. a transition function<br />

with a transition relation<br />

4. a start state C,<br />

5. a set of final or accepting states<br />

The automaton<br />

1. starts in state set<br />

2. proceeds from state set<br />

¥,<br />

to state set<br />

A”,<br />

H, (which may be identified<br />

C),<br />

C.<br />

if the current input is ,<br />

3. accepts a string ( sequence of symbols) iff that sequence of inputs drives<br />

it from to a state set containing some c.<br />

02140 Languages and Parsing, MF, Fall 2004 – p.18/22


N<br />

E<br />

E<br />

H<br />

G<br />

<br />

F<br />

E<br />

'<br />

D<br />

<br />

<br />

E<br />

e<br />

E<br />

<br />

d<br />

¦<br />

%<br />

F E<br />

<br />

C<br />

%<br />

B<br />

'<br />

<br />

E F<br />

M<br />

A<br />

%<br />

E <br />

F E<br />

K<br />

C<br />

H M<br />

<br />

E<br />

B<br />

D<br />

<br />

N<br />

E<br />

Transition diagrams<br />

are a graphical notation for FAs.<br />

A transition diagram for a NFA<br />

1. For each state in<br />

2. if<br />

labelled<br />

there is a node,<br />

is a graph s.t.<br />

then (and only then) there is an arc from<br />

M, (we allow ourselves to collate multiple arcs between the same<br />

nodes into a single one labelled with a list)<br />

3. there is an arrow without source node into the start state,<br />

4. nodes representing accepting states are marked with double<br />

circles.<br />

a,b<br />

to<br />

b<br />

b<br />

a<br />

02140 Languages and Parsing, MF, Fall 2004 – p.19/22<br />

Transition tables<br />

Do now assign sets of states:<br />

' Q<br />

'<br />

E <br />

e<br />

%<br />

<br />

E<br />

02140 Languages and Parsing, MF, Fall 2004 – p.20/22


H<br />

C<br />

B<br />

<br />

]<br />

M<br />

K<br />

¦<br />

A<br />

H<br />

$<br />

<br />

¦<br />

H<br />

B<br />

S<br />

G<br />

<br />

J<br />

<br />

\<br />

F<br />

C<br />

e<br />

E<br />

<br />

D<br />

q<br />

I<br />

R<br />

D<br />

E<br />

p<br />

<br />

H<br />

l k<br />

nm<br />

j hi<br />

C<br />

<br />

'<br />

o <br />

E<br />

E<br />

B<br />

]<br />

<br />

K<br />

¦<br />

E <br />

E<br />

J<br />

B<br />

B<br />

M<br />

<br />

B<br />

D<br />

g<br />

f<br />

A<br />

B<br />

§<br />

B<br />

¦<br />

G<br />

C<br />

H<br />

<br />

F<br />

A<br />

E<br />

B<br />

R<br />

D<br />

[[<br />

$<br />

¦<br />

<br />

§<br />

Z<br />

<br />

H<br />

A<br />

H<br />

H<br />

A<br />

S<br />

H &<br />

<br />

%<br />

B<br />

6<br />

B<br />

6<br />

6<br />

H M<br />

R<br />

D<br />

R<br />

D<br />

C<br />

I<br />

D<br />

Extending the trans. fct. to strings<br />

Given<br />

via recursion:<br />

Base case:<br />

Recursion:<br />

def<br />

def<br />

H, we define a function<br />

for each<br />

C;<br />

for each<br />

C,<br />

$,<br />

¦.<br />

02140 Languages and Parsing, MF, Fall 2004 – p.21/22<br />

The language of an NFA<br />

Def: The language<br />

def<br />

of a an NFA<br />

is<br />

The language thus is the set of strings that drive from the<br />

initial state to a set of states containing some accepting state.<br />

02140 Languages and Parsing, MF, Fall 2004 – p.22/22

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!