DFA - SIUE Computer Science

advertisement
MATH 224 – Discrete Mathematics
Deterministic Finite Automata
Finite State Machines
Set of States
Q
Start State
q0  Q
Accepting States
qf  Q
Alphabet

e.g, {0, 1}
Transitions

(, qi)  qj
4/8/2015
q1
q2
q3
q0
qf1
qf2
qi

qj
1
MATH 224 – Discrete Mathematics
DFA Represented by a Directed Graph
Multitasking in an operating System
Time slice expired
Start
Ready
Queue
Dequeued
Finished with resource
and ready to run
Running
Blocked waiting for a resource,
e.g. data from a disk
Blocked
Waiting
4/8/2015
2
MATH 224 – Discrete Mathematics
DFA for Multiples of 3 in binary
Start
0
0
q0
Example 1
46 remainder 1
qf-3
1
1
1
string
1
States 0 1 2 2 2 2 1
Example 2
q1
51 remainder 0
0
string
q2
0
4/8/2015
1 0 1 1 1 0
1 1 0 0 1 1
States 0 1 3 3 3 1 3
3
MATH 224 – Discrete Mathematics
DFA to Recognize Floating Point
0-9
Start
0-9
q0
.
qf-0
+, –
0-9
e, E
q2
0-9
q5
q1
+, –
q4
q3
0-9
0-9
Qf-1
4/8/2015
0-9
4
MATH 224 – Discrete Mathematics
Crossing the Bridge in Minimum Time 1, 2, 5 and 10 minutes
5 &10
q6
Start
Only 2 of the final states
q0
2 &10
are shown here, one of the
q5
two shortest and one of the
1&2
2&5
next shortest.
1&10
What would the
1
&
5
2 minutes
q1
q4
longest time be?
q3
1
q2 5 minutes
2
3 minutes
1
q8
q7
5
5 &10
q10 6 minutes
q9
13 minutes
q11
q12
2
1&2
q13
15 minutes
4/8/2015
1 &10
19 minutes
qf17
q15 16 minutes 1 & 2
1
qf14 17 minutes
q16 17 minutes
5
MATH 224 – Discrete Mathematics
Other DFAs
Construct a DFA that recognizes legal identifier names in C++ using α to
represent letters and δ to represent the digits 0 through 9.
Construct a DFA to recognize even numbers in binary.
Construct a DFA to recognize multiples of four in binary.
Construct a DFA to recognize multiples of 5 in binary.
Construct a DFA to recognize multiples of 5 in decimal.
4/8/2015
6
MATH 224 – Discrete Mathematics
Representing a DFA in a Computer Program
Probably the simplest way to represent a DFA in a program is to use a 2-dimensional
array, where each column corresponds to a character in the alphabet and each row
corresponds to a state. So for example here is the DFA that recognizes multiples of 3
in binary.
qf-3
4/8/2015
0
1
2
3
0
3
3
1
3
1
1
2
2
1
Blank
Halt
Halt
Halt
Halt
7
MATH 224 – Discrete Mathematics
DFA for Multiples of 3 in binary 0
Start
0
q0
0
q3
1
1
1
1
qf-6
1
q1
0
q2
0
4/8/2015
8
Download