View
215
Download
0
Category
Tags:
Preview:
Citation preview
1
Controlling My Office Temperature
Let’s model when I’m happy with my office temperature.– Duncan Hall office thermostats are attached to count-down dial
timer.– Assume it’s always hot enough to want A/C on when in office.
• Whether I’m in my office.• Timer status.
InOff
InOn
OutOff
OutOn
What info representsthe current state?
?
?
2
Controlling My Office Temperature
Let’s model when I’m happy with my office temperature.– Duncan Hall office thermostats are attached to count-down dial
timer.– Assume it’s always hot enough to want A/C on when in office.
• I move in/out of my office.• I turn timer up.• Timer times out.
InOff
InOn
OutOff
OutOn
Move
Move
Move
Move
Up
Up
Up
Up
Timeout
Timeout
Timeout
Timeout
?Error
Move,Timeout,
Up
What are the possible actions??
?
3
Controlling My Office Temperature
Let’s model when I’m happy with my office temperature.– Duncan Hall office thermostats are attached to count-down dial
timer.– Assume it’s always hot enough to want A/C on when in office.
InOff
InOn
OutOff
OutOn
Move
Move
Move
Move
Up
Up
Up
Up
Timeout
Timeout
Timeout
Timeout
?In this example, it’s arbitrary.At beginning of workday,• I’m out of the office, AND• the A/C is off.
Error
Move,Timeout,
Up
What state do we start in??
?
4
Controlling My Office Temperature
Let’s model when I’m happy with my office temperature.– Duncan Hall office thermostats are attached to count-down dial
timer.– Assume it’s always hot enough to want A/C on when in office.
By the problem definition,I’m happy if• I’m out of the office, OR• the A/C is on.
InOff
InOn
OutOff
OutOn
Move
Move
Move
Move
Up
Up
Up
Up
Timeout
Timeout
Timeout
Timeout
?Error
Move,Timeout,
Up
5
Controlling My Office Temperature
Let’s model when I’m happy with my office temperature.– Duncan Hall office thermostats are attached to count-down dial
timer.– Assume it’s always hot enough to want A/C on when in office.
Input 1, a typical workday:Move, Up, Timeout,Up, Move, Timeout,Move, Up
Input 2, “arbitrary”:Move, Move, Timeout,Up, Up
InOff
InOn
OutOff
OutOn
Move
Move
Move
Move
Up
Up
Up
Up
Timeout
Timeout
Timeout
Timeout
?Error
Move,Timeout,
Up
What is the end state for…?
?
6
Deterministic Finite Automata
Controllers like this are common examples ofdeterministic finite automata (DFAs).
• Example of machines, describing a relatively simple form of computation.
7
DFAs as Programming
Defining a DFA is a kind of programming.
• Problem definition– Includes defining possible actions & accepting
condition.
• States structure of program– Includes designating which are initial, which final.
• Transitions program
Learn programming techniques by example & by theory.
8
DFAs: Formal Definition
DFA M = (Q, , , q0, F)
Q = states a finite set = alphabet a finite set = transition function a total function in Q Qq0= initial/starting state q0 Q
F = final states F Q
9
DFAs: Example 1
Let Qme = {In,Out}, Qtimer = {Off,On}.
Q = Qme Qtimer {Error}
= {Move, Up, Timeout} = {(([Out,Off],Move),[In,Off]),
…}q0 = [Out,Off]
F = {[a,On] | a Qme}
{[Out,b] | b Qtimer}
InOff
InOn
OutOff
OutOn
Move
Move
Move
Move
Up
Up
Up
Up
Timeout
Timeout
Timeout
Timeout
?Error
Move,Timeout,
Up
10
DFAs: Definition Pragmatics
DFA definitions are presented via diagrams or tuple notation.The most interesting part is the transitions ().Defining effectively defines the states (Q), too.
Want an easily understandable presentation of :– It’s easy to accidentally omit info in diagrams.
– Small examples: usually show as a table or diagram.– Large examples: usually show as quantified equations.
11
DFAs: Example 2
strings over {a,b} with at least 3 a’s
1 a 2 a 3+ a0 aa a a
b b b
12
DFAs: Example 2 Lessons
DFAs can count or recognize up to a fixed number.
Useful to name states mnemonically.
13
DFAs: Example 3
strings over {a,b} with length mod 3 = 0
1 20
14
DFAs: Example 3 Lesson
DFAs can count or recognize up to a fixed number.– Can combine with modular counting.– Can combine with approximation, e.g., 0, 1, 2, 3, many.
A few:• error-checking and -correcting codes, including parity• length of grocery store lines• # of cars waiting at traffic light• elevator floor counter
?Example uses??
15
DFAs: Example 4
strings over {a,b} without 3 consecutive a’s
A simple example of “strings not of the form …”.
1 a 2 a Hasaaa0 a
a a a
bb
b
16
DFAs: Example 4 Lessons
Often useful to have an “error state”.
Can swap final/nonfinal status to complement described language.
17
DFAs: Example 5
strings over {a,b} with next-to-last symbol = a
…aa …aba
…ba …bb
a
b
b
a
bb
b
a
a
a
a
a
b
b
b
18
DFAs: Example 5 Lesson
DFAs can have a fixed-size “memory”.
19
DFAs: Acceptance
Early letters used for symbol meta-variables.
Later letters used for string meta-variables.
Defined inductively on length of string: (q,) = q (q,aw) = ((q,a),w)
q Q, a , w *
Definition of DFA acceptance:DFA M accepts x path labeled x, from initial state to
some final state:
DFA (Q,,,q0,F) accepts x (q0,x) F.
Most easily defined using : Q * QSet of all strings over alphabet.
Must use all of x.
20
DFAs: A Property of
Base case, |x| = 0:
By induction on length of x:
adjacency represents concatenation of strings
Prove (q,xy) = ( (q,x),y). qQ, x,y*
x = , and xy = y. Definition of empty string (q,x) = q Definition of
= ( (q,x),y) Plugging in q= (q,x)
Conclusion holds.
(q,xy) = (q,y) Pluging in x=
21
DFAs: A Property of
By induction on length of x:
Inductive case, |x| = n+1:
Prove (q,xy) = ( (q,x),y). qQ, x,y*
x = aw, for some a and w*. |w| = n.
= ((q,a),wy) Definition of
= ( ((q,a),w),y) Induction
(q,xy) = (q,awy) Plugging in x=aw
= ( (q,aw),y) Definition of
Conclusion holds.
22
DFAs: Additional Notes
These DFAs are not the only solutions to these problems.Just like computer programs are not unique solutions.Illustrated the most straightforward solutions.
These DFAs only accept or reject.Most real-world uses rely on having output.Will look other kinds later.
23
Nondeterministic Finite Automata
“Nondeterminism” implies having a choice.
Multiple possible transitions from a state on a given symbol.(q,a) is a set of states : Q Pow(Q)Can be empty, so no need for error/nonsense state.Acceptance: exist path to a final state?
I.e., try all choices.
Often used with different view of computation:Guess result (path taken), and check if possible with given input.
Also allow transitions on no input: : Q ( {}) Pow(Q)
24
NFAs: Formal Definition
NFA M = (Q, , , q0, F)
Q = states a finite set = alphabet a finite set = transition function a total function in
Q ( {}) Pow(Q)q0= initial/starting state q0 Q
F = final states F Q
25
NFAs: Example 1
strings over {a,b} with at least 3 a’s
1 a 2 a 3+ a0 aa a a
b b b
Same as the DFA for this problem.
26
NFAs: Example 1 Lesson
Nondeterminism isn’t always useful.
27
NFAs: Example 2
strings over {a,b} with next-to-last symbol = a
Loop until we “guess” which is the next-to-last a.
a
…a…a…
28
NFAs: Example 2
Formal definition:
Q = {…,…a,…a} = {a,b} =
q0 = …
F = {…a}
Input
a b
… {…,…a}
{…}
…a {…a} {…a}
…a
Sta
te
29
NFAs: Example 2 Lessons
Some problems can be solved by a much simpler NFA than DFA.
NFAs don’t need a transition from every state for each input.
30
NFAs: Example 3
strings over {0,1,2} having(either 0-or-more 0’s or 0-or-more 1’s)
followed by 0-or-more 2’s
0
2s1
2
0s
1s
31
NFAs: Example 3 Lesson
Problem decomposition: transitions can separate subparts of NFA into easy chunks.
32
DFAs with Output
Have seen FAs with Yes/No output.– Convenient for theory.– Practical uses include
• Parity checking• Error detection• Single-bit control signal generation• Pattern matching• Circuit verification
Pragmatically, often want more general output.• Multi-bit control signal generation• Tokenization of input for parsing• Pattern matching with grep-like output• Circuits for bit-wise operations• Communicating automata• Communication protocols
33
Transducers (= Mealy Machines)
DFA + output on transitions
M = (Q, , , , , q0)
Q,,,q0 same as in regular DFA.No F: Output is more general than Yes/No acceptance.
= output alphabet = output mapping : Q
Output string is of same length as input string.Very simple extension.
34
Many Variations Possible
Moore machinesDFA + output on states: Q Mealy outputs |x| symbols – each transition.Moore outputs |x|+1 symbols – each state.
More flexible output– Output comes from {} or *.– As in upcoming example.
Output can represent external action.– E.g., Increment_Counter.– Action not seen by FA.
35
Example: A Tiny Tokenizer
Input Token
a 1
ab 2
abba
3
cab 4
States = All valid prefixes of tokens.All other transitions to an error state.
a ab abb
c ca cab
abba
a
b b a
c a b
Sp 4
Sp 3Sp 2Sp 1
Sp
= {a,b,c,Sp}
Tokens must be separated by Sp.
Recommended