15 Automata
description
Transcript of 15 Automata
![Page 1: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/1.jpg)
Machines That Can’t Count
CS 15-251Lecture 15 Lecture 15 b
ba
b
a
a
a
ba
b
![Page 2: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/2.jpg)
Let me teach you a programming language so simple
that you can learn it in
less than a minute.
![Page 3: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/3.jpg)
Meet “ABA” The Automaton!Meet “ABA” The Automaton!
b
ba
ba
a
a
bab
Input StringInput String ResultResult
abaaba AcceptAccept
aabbaabb RejectReject
aabbaaabba AcceptAccept
AcceptAccept
![Page 4: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/4.jpg)
The Simplest Interesting Machine:The Simplest Interesting Machine:
Finite State MachineFinite State Machine OR OR
Finite Automaton Finite Automaton
![Page 5: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/5.jpg)
Finite set of Finite set of statesstates
A start stateA start state
A set of A set of accepting accepting statesstates
A finite A finite alphabetalphabet
a b #a b #
x 1x 1State State transition transition instructionsinstructions
1 2{ , , , , }o kQ q q q q
Finite AutomatonFinite Automaton
oq
1 2, , ,
ri i iF q q q
:
( , )i j
Q Q
q a q
iq jq
a
![Page 6: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/6.jpg)
How it worksHow it works
•At any point in time, it’s in one of its states (or it has crashed).
•It starts in its start state.
•It “reads” one letter at a time from the input string (going from left to right).
![Page 7: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/7.jpg)
If it is in state qi and reads a then
If it has the transition
Then it changes to state qj
Else it crashes
( , )i jq a q
![Page 8: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/8.jpg)
M accepts the string x if when M reads x it ends in an accepting state.
M rejects the string x if when M reads x it ends in a non-accepting state.
M crashes on x if M crashes while reading x.
Let M=(Q,,F,) be a finite automaton.
![Page 9: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/9.jpg)
The set (or language) accepted by The set (or language) accepted by M is:M is:
x
*|
...
M accepts x
All length k strings over the alphabet
k
* 0
n s
1 2 3
Notice that this is
![Page 10: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/10.jpg)
What is the language What is the language accepted by this machine?accepted by this machine?
L = {a,b}* = all finite strings of a’s and b’s
a,b
![Page 11: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/11.jpg)
What is the language What is the language accepted by this machine?accepted by this machine?
L = all even length strings of a’s and b’s
a,b
a,b
![Page 12: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/12.jpg)
What machine accepts this What machine accepts this language?language?
L = all strings in {a,b}* that contain at least one a
aa,bb
![Page 13: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/13.jpg)
What machine accepts this What machine accepts this language?language?
L = strings with an odd number of b’s and any number of a’s
b aa
b
![Page 14: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/14.jpg)
What is the language What is the language accepted by this machine?accepted by this machine?
L = any string ending with a b
b ba
a
![Page 15: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/15.jpg)
What is the language What is the language accepted by this machine?accepted by this machine?
L = any string with at least two a’s
b ba,ba a
![Page 16: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/16.jpg)
What machine accepts this What machine accepts this language?language?
L = any string with an a and a b
b
ba,b
a
a
b a
![Page 17: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/17.jpg)
What machine accepts this What machine accepts this language?language?
L = strings with an even number of ab pairs
a
bb
a
a
b a
b
![Page 18: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/18.jpg)
L = all strings containing ababb as a consecutive substring
b
a,ba a
b
b
bb
aa
a
a ab aba abab
If I am in state “aba” then the longest prefix of ababb that forms a suffix of the string I have read until now is aba.
![Page 19: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/19.jpg)
Problem:Does the string S appear inside the text T ?
The “grep” ProblemThe “grep” Problem
Input:• text T of length t• string S of length n
Cost: O(nt) comparisons1 2 3, , , , ta a a a
symbols
n Naïve method:
![Page 20: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/20.jpg)
Automata SolutionAutomata Solution
•Build a machine M that accepts any string with S as a consecutive substring.
•Feed the text to M.
•Cost: t comparisons + time to build M.
•As luck would have it, the Knuth, Morris, Pratt algorithm builds M quickly.
•By the way, it can be done with fewer than t comparisons in the worst case!
![Page 21: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/21.jpg)
Real-life uses of finite state Real-life uses of finite state machinesmachines
•grep
•coke machines
•thermostats (fridge)
•elevators
•train track switches
•lexical analyzers for parsers
![Page 22: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/22.jpg)
Let L be a language.
L is called a regular language if there is some finite automaton that accepts L.In this lecture we have seen many regular languages.•
• even length strings
• strings containing ababb
![Page 23: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/23.jpg)
Theorem: Any finite langage is regular.
Proof: Make a machine wth a “path” for each string in the language.
Example: L = {a, bcd, ac, bb}
b
d
a
c b c
![Page 24: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/24.jpg)
Are all languages regular?
![Page 25: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/25.jpg)
NO!NO!Counting Argument
•There are uncountably many L
•There are countably many automata (each automaton can be specified by a finite string)
![Page 26: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/26.jpg)
NO!NO!Halting Argument
•K = { P | P(P) } is undecidable for ideal computers with unlimited memory, i.e., K is not recursive.
•Finite automata can be simulated by computers. (they are a weak sub-class)
![Page 27: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/27.jpg)
The fact that not all languages are regular can be argued more directly (without resorting to macho techniques).
![Page 28: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/28.jpg)
, , , ,n na b ab aabb aaabbb
Consider the language
i.e., a bunch of a’s followed by an equal number of b’s
No finite automaton accepts this language.
Can you prove this?
![Page 29: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/29.jpg)
APE 0835
anbn is not regular. No machine has enough states to keep track of the number of a’s it might encounter.
![Page 30: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/30.jpg)
That is a fairly weak argument. Consider the following example…
![Page 31: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/31.jpg)
Can’t be regular. No machine has enough states to keep track of the number of occurrences of ab.
L = strings where the # of occurrences of the pattern ab is equal to the number of occurrences of the pattern ba
![Page 32: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/32.jpg)
Remember “ABA”?Remember “ABA”?
b
ba
ba
a
a
bab
ABA accepts only the strings with an equal number of ab’s and ba’s!
![Page 33: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/33.jpg)
Professional Strength ProofProfessional Strength Proof
Theorem: anbn is not regular.
Proof: Assume that it is. Then M with k states that accepts it.
For each 0 i k, let Si be the state M is in after reading ai.
i,j k s.t. Si = Sj, but i j
M will do the same thing on aibi and ajbi . (to Si ) (to Si )
But a valid M must reject ajbi and accept aibi.
![Page 34: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/34.jpg)
MORAL:MORAL:
Finite automata can’t count.
![Page 35: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/35.jpg)
AdvertisementAdvertisement
You can learn much more about these creatures in the FLAC course.
Formal Languages, Automata, and Computation
• There is a unique smallest automata for any regular language
• It can be found by a fast algorithm.
• ……
![Page 36: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/36.jpg)
Cellular AutomataCellular Automata•Line up a bunch of identical finite automata in a straight line.
( , , )k i j l
Q Q Q Q
q q q q
kq lq
,i jq q
iq jqkq
•Transitions are based on the states of the machine’s two neighbors or an indicator that a neighbor is missing. (There is no other input.)
•All cells move to their next states at the same time:synchronous transition
![Page 37: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/37.jpg)
The Firing Squad ProblemThe Firing Squad Problem•Five “soldiers” all start in the sleep state. You change the one on the left to the wake state.•All five must get to the fire state at the same time (for the first time).
sleepwakefire
sleepwakefire
sleepwakefire
sleepwakefire
sleepwakefire
sleepwakefire
sleepwakefire
sleepwakefire
sleepwakefire
sleepwakefire
![Page 38: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/38.jpg)
ShorthandShorthand
, , ,Q a b d
Means use this transition when your left neighbor is in any state at all and your right neighbor is in state a,b, or d.
![Page 39: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/39.jpg)
11 22 33 44 55
11 wakewake sleepsleep sleepsleep sleepsleep sleepsleep
22 wakewake22
sleepsleep sleepsleep sleepsleep sleepsleep
33 wakewake33
wakewake33
wakewake33
sleepsleep sleepsleep
44 wakewake44
wakewake44
wakewake44
wakewake44
sleepsleep
55 firefire firefire firefire firefire firefire
sleep
wake wake2 wake3 wake4
fire!
{w2},Q{w},Q {w3},Q
Q,Q
{w4},Q
{sleep,fire,end},Q
Q,Q Q,Q Q,Q
![Page 40: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/40.jpg)
QuestionQuestion
No. Finite automata can’t count!
Can you build the soldier’s finite automaton brain before you know how many soldiers will be in the line?
![Page 41: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/41.jpg)
Don’t jump to conclusions! It is possible to design a single cellular automaton that works for any number of soldiers!
![Page 42: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/42.jpg)
Useful subroutine:method to find the center
after leftmost soldier is awakened.
Define the center of the line to be:
•The guy in the middle if the # of soldiers is odd.
•The two guys in the middle if the the # of soldiers is even.
![Page 43: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/43.jpg)
Your neighbor “notices” that you are in a signaling state.
An automaton can “signal” to its neighbors.
Certain subsets of its states will correspond to signaling.
signal
normaloperation
![Page 44: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/44.jpg)
A Method to Find the CenterA Method to Find the Center• Send a signal back and forth from “end” to “end”.
• Each time it gets to an “end”, send it back, but tell the neighbor of the “end” that it is the “new end”.
•The center(s) will get a “new end” signal from both sides.
• # of steps: 2O n
![Page 45: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/45.jpg)
Slick Center AlgorithmSlick Center Algorithm
• Use two signals: one fast and one slow
• When you get the fast signal, pass it on (or bounce it back) on the next time step.
• When you get the slow signal, hold it for two steps, then pass it on the next time step.
• When you wake and you are leftmost, consider yourself to hold both the slow and fast signal.
• If you get one signal from each side, shout, “I am the center!”
![Page 46: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/46.jpg)
Firing Squad ProblemFiring Squad Problem
•Keep finding the middle recursively: If you’re the center, mark yourself as an “end” and find the centers on each side of you.
•If you are an “end” and your neighbor is starting both signals, send back a “fire next step”.
![Page 47: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/47.jpg)
Firing Squad AnalysisFiring Squad Analysis
3 3 3 3
2 2 2 2 4 2 83 1 1 1
12 2 4 8
32 3
2
n n nn
n
n n
How many steps to find the center?
How many steps to fire?
3 steps
2n
n
2
n
![Page 48: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/48.jpg)
Lowed BoundLowed Bound
Can you see why any solution must use at least 2n steps?
There is a clever way to do it in 2n steps.
![Page 49: 15 Automata](https://reader036.fdocuments.us/reader036/viewer/2022081506/563dbb44550346aa9aabb232/html5/thumbnails/49.jpg)
Other ResearchOther Research
• Different topologies
• Self-stabilizing clocks
• all cells fire simultaneously at regular intervals
• even if you mess with the states of all the cells they get back on schedule (eventually)