Title: Mathematical Foundations of Computer Science
1Mathematical Foundations of Computer Science
- Chapter 2 Finite Automata
2Languages
- A language (over an alphabet S) is any subset of
the set of all possible strings over S . The
set of all possible strings is written as S. - Example
- S a, b, c
- S ?, a, b, c, ab, ac, ba, bc, ca, aaa,
- one language might be the set of strings of
length less than or equal to 2. - L ?, a, b, c, aa, ab, ac, ba, bb, bc, ca, cb,
cc
3Finite Automata
- A deterministic finite automaton (dfa) is a
mathematical model for a machine that can accept
certain types of languages. - The singular is automaton, the plural is automata.
4Finite Automata
"guts" of the machine
on/off switch
accept or reject
tape read head
input tape
A finite automaton
5Finite Automata
- The machine, or automaton, when on, will at any
moment be in some "state". - This type of machine has only a finite number of
states. - The machine goes into new states as it reads the
input tape. - When the light is on, the string up to that point
is "accepted". When off, it is "rejected". - The machine has to read all the way to the last
character on the tape.
6Finite Automata
- A deterministic finite automaton (dfa) is a
quintuple M (Q, S , d, q0, F) , where - Q is a finite set of states,
- S is a finite set of symbols (the alphabet),
- q0 is one of the states in Q (called the initial
state), - F is a subset of Q and represents the final, or
accepting states of M, - and d Q ? S ? Q is the transition function of
M. - The transition function specifies, for each state
and symbol, the next state the machine will enter.
7Finite Automata
- The transition function specifies, for each state
and symbol, the next state the machine will
enter. - When we write d(q1, b) q2, we are saying that
if the machine is in state q1 and the next symbol
read is b, then the state of the machine becomes
state q2. - The machine always starts in the initial state
looking at the leftmost symbol on the tape. - The machine stops after reading the rightmost
symbol on the tape.
8Finite Automata
- The transitive closure of d , written as d(q1,
w) q2 is used to represent the fact that the
machine, when in state q1, winds up in state q2
by following the path that exists for the
transitions specified by the series of symbols in
w. - For example, if d(q1, a) q2, d(q2, b) q3 and
d(q3, a) q4, then d(q1, aba) q4.
9Finite Automata
- The language accepted by a dfa is exactly the set
of strings over the alphabet that result in the
machine ending in an accepting state. The
language is symbolized as L(M). - The transition function makes the automaton
deterministic. At each moment in time, the
machine has exact instructions as to what will
happen next.
10Bubble Diagrams
- In order to visualize a particular finite
automaton, we draw a bubble diagram, (or bubble
chart). - Here's a simple example
- The circles represent states, with a double
circle being an accepting state. - The arcs represent the transitions of the
transition function.
11Bubble Diagrams
- What language is accepted by this machine?
- L x x ? a, b and x has an
even number of b's
12Example 1
- Here is another example of a finite automaton M,
in "bubble diagram" form - What is d(q1, b)?
- What is d(q0. abbaba)?
- Given that q3 is the only accepting state, what
is L(M), the language is accepted by this
machine? - Strings beginning with an a and containing at
least two a's.
q1
q3
13Example 2
- Construct a dfa that accepts the languageL x
? a, b x begins and ends with the sequence
aab.
b
a
a
a
a
a
b
AA
A
AA
A
AAB
b
a
a
b
accepting state. Why?
b
x
b
b
a,b
"trap" state
14Example 2
- Construct a dfa that accepts the languageL x
? a, b x begins and ends with the sequence
aab.
b
a
a
a
a
a
b
q2
q4
q5
q1
q3
q0
b
a
a
b
bubble diagram format
b
q6
b
b
qT
a,b
15Example 2
Table format
16Example 3
- Construct a dfa that accepts the languageL x
? a, b x has an even number of a's and an odd
number of b's.
a
OE
EE
a
b
b
b
b
a
EO
OO
a
17Example 4
- Construct a dfa that accepts the languageL x
? a, b x consists of even number of a's
followed by an odd number of b's.
a
b
a,b
a
b
a
b
b
a
18Finite Automata
- That's how finite automata are built, and that's
how they work. - Some automata are easy to construct others quite
tricky or difficult. - Representations
- bubble diagram format
- good for construction
- good for visualizing behavior
- table format
- good for computer simulation
- good for applying certain algorithms
19Chapter 2, Section 1Homework
20Non-deterministic finite automata
- Notice how annoying all the necessary detail is.
Perhaps we can come up with a more powerful and
more convenient automaton.
21Non-deterministic finite automata
- A non-deterministic finite automaton (nfa) is a
quintuple M (Q, S , d, q0, F) , where Q is a
finite set of states, S is a finite set of
symbols (the alphabet), q0 is one of the states
in Q (called the initial state), F is a subset of
Q and represents the final, or accepting states
of M, and d Q ? S, ? ? 2Q is the transition
function of M.
22Non-deterministic finite automata
- d Q ? S, ? ? 2Q means that if the machine is
in state q and sees an a (or perhaps merely
ignoring the input ? he can wind up in any of
the states within some set of states. - d(q, a) q1, q4, q7 goes to any of three
states. - d(q, a) "dies"
- d(q, ?) q, p goes to either of 2 states
without looking at the input tape.
23Non-deterministic finite automata
- The only difference is in the transition
function. - We now allow the machine to go to a new state
WITHOUT looking at a symbol. (? transitions) - We also allow the machine the choice of going to
any of a number of states for a given symbol!!!
(non-deterministic transitions)
24Non-deterministic finite automata
- We have just made this machine smart, as we will
shortly see. - Having non-deterministic transitions means that
we can specify no transition for a qiven state
and symbol as well, which makes design a little
less cluttered because we don't have worry about
"useless" stuff.
25Non-deterministic finite automata
- If there is no transition specified,
- the machine merely dies.
- It does not accept the string and can accept no
more input, - just like for specifying trap states in a dfa.
- But if there is ANY path from the initial state
to an accepting state for a given string - that string is accepted and belongs to L(M).
- even if only one out of a million paths leads to
an accepting state!!!! - Now thats smart!
26Constructing an nfa
- Construct an nfa that accepts the languageL x
? a, b x begins and ends with the sequence
aab.
b
end of string aab
q3
start of string aab
middle of string anything
27Non-deterministic finite automata
- But nfa's are no more powerful than dfa's !!!
- If a language can be recognized by an nfa,
- it can be recognized by a dfa.
- We "prove" this by demonstrating an algorithm
that converts any nfa into a corresponding dfa. - The book describes the algorithm, but it's hard
to understand we'll look at how it works by
studying the last example.
28Chapter 2, Section 2Homework
29Converting nfa's to dfa's
- Look at the nfa we just constructed
- d(q0, a) q1 so d (Q0, a) in the
corresponding dfa would equal Q1. The subscript
of Q represents the set of states that the nfa
could wind up in. - d(q0, b) so d (Q0, b) in the
corresponding dfa would equal QT, the trap state.
30Converting nfa's to dfa's
- Of course d (QT, a) d(QT, b) QT since it's
the trap state. - Now we proceed to state Q1 and draw arcs for a
and for b. - We keep going until we add no more new states to
the dfa and all the transitions have been
specified. - Note that since d(q3, a) q4, q5, d(Q3, a)
Q45
31Converting nfa's to dfa's
b
because of the ? transition, if the nfa is in q3,
it could just as well be in q4. (the ? closure of
q3 is q3, q4
32Converting nfa's to dfa's
- Start with the initial state of the nfa
- Create a corresponding initial state in the dfa
- For each symbol of the alphabet, find the set of
states that you could wind up in. Create a
corresponding state in the dfa. - Keep going until there are no more states, and no
more transitions need to be drawn. - Note When there are lambda transitions, you
have to look at the ? closure. - Remember a ?a a?
- Any state in the dfa which has in it the number
of an accepting state of the nfa becomes an
accepting state.
33Chapter 2, Section 3Homework
34Chapter 2 Homework Summary
- 2.1/1-15
- 2.2/2-12
- 2.3/1-6, 8, 12