Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
Computational Models #1
Handout Mode
Nachum Dershowitz & Yishay Mansour
March 13-15, 2017
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 1 / 41
Lecture Outline
I MotivationI Languages, words and alphabetsI Finite automata and regular languagesI Regular operations
I Sipser, Chapter 1
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 2 / 41
Time Magazine
• Put the right kind of software into a computer, and it will do whatever you want it to. There may be limits on what you can do with the machines themselves, but there are no limits on what you can do with software.
Chess
• Games: 101050
• Positions: 1040
• Positions: 20, 400, 5362, 71852, 809896(?), 9132484(?), ...
states initial
terminal
Part I
Languages, words and alphabets
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 3 / 41
Languages, words and alphabets
Definition 1An alphabet ⌃ is a finite set of letters.
I ⌃ = {a, b, c, . . . , z} – the English alphabet.I ⌃ = {↵,�, �, . . . , ⇣} – the Greek alphabet.I ⌃ = {0, 1} – the binary alphabet.I ⌃ = {0, 1, . . . ,9} – the digital alphabet.
Definition 2A word (i.e., string) over ⌃, is a finite sequence of letters from ⌃.
The collection of all strings over ⌃ is denoted by ⌃⇤.For {0, 1}, the binary alphabet, ", 1, 0, 000000000, 1111111000 are allmembers of ⌃⇤.Definition 3A language over ⌃ is a (possibly infinite) subset of ⌃⇤.
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 4 / 41
Language Examples
I Modern English.I Ancient Greek.I All prime numbers, written using digits.I A = {w 2 {0, 1}⇤ : w has at most seventeen 0’s}.I B = {0n1n : n � 0}.I C = {w 2 {0, 1}⇤ : w has an equal number of 0’s and 1’s}.
Make sure you understand what the above notions stand for...
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 5 / 41
Part II
Finite Automata
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 6 / 41
Example: A One-Way Automatic Door
frontpad
rearpad
door
I open when person approachesI hold open until person clearsI don’t open when someone standing behind door
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 7 / 41
The Automatic Door as DFA
closed open
FRONT
NEITHER
FRONTREARBOTH
REARBOTH
NEITHER
I States:I OPENI CLOSED
I Sensor:I FRONT: someone on front padI REAR: someone on rear padI BOTH: someone(s) on both padsI NEITHER no one on either pad.
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 8 / 41
The Automatic Door as DFA
A DFA is Deterministic Finite Automata
closed open
FRONT
NEITHER
FRONTREARBOTH
REARBOTH
NEITHER
neither front rear bothclosed closed open closed closedopen closed open open open
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 9 / 41
DFA: Informal definition
The machine M1:
q1
0
q2 3q
11 0
0,1
I States: q1, q2, and q3.I Start state: q1 (arrow from “outside”).I Accept state: q2 (double circle).I State transitions: arrows tagged with letters.
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 10 / 41
DFA: Informal definition (cont.)
q1
0
q2 3q
11 0
0,1
I On an input stringI DFA begins in start state q1I after reading each symbol, DFA makes
state transition with matching label.I After reading last symbol, DFA “produces" output:
I accept if DFA is an accepting state.I reject otherwise.
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 11 / 41
DFA: Informal definition (cont..)
q1
0
q2 3q
11 0
0,1
What happens on the following input strings:I 1101I 0010I 01100I In general?!
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 12 / 41
DFA: Informal definition (cont...)
q1
0
q2 3q
11 0
0,1
This DFA acceptsI All input strings that end with a 1I All input strings that contain at least one 1, and end with an even
number of 0’sI No other strings
Proof: ?
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 13 / 41
DFA - Formal Definition
Definition 4A deterministic finite automaton (DFA) is a 5-tuple (Q,⌃, �, q0,F ),where
IQ is a finite set called the states
I ⌃ is a finite set called the alphabetI � : Q ⇥ ⌃ ! Q is the transition functionI
q0 2 Q is the start stateI
F ✓ Q is the set of accept states
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 14 / 41
Back to M1
q1
0
q2 3q
11 0
0,1
M1 = (Q,⌃, �, q1,F ) whereI
Q = {q1, q2, q3}, ⌃ = {0, 1},
I the transition function � is
0 1q1 q1 q2q2 q3 q2q3 q2 q2
Iq1 is the start state
IF = {q2}.
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 15 / 41
Another Example
q1
q2
a b
a
a a
a
b
b
b
b
r1
s
r2
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 16 / 41
Formal Model of Computation
Definition 5
M = (Q,⌃, �, q0,F ) accepts w 2 ⌃⇤ if b�M
(q0,w) 2 F .
Definition 6 (b�)b�
M
: Q ⇥ ⌃⇤ 7! Q is defined by
b�M
(q,w) =
⇢�(b�(q,w1,...,n�1),wn
), n = |w | � 1q, w = ".
.
Iw1,...,k stands for the word w1, . . . ,wk
— the k -letter prefix of w
(w1,...,0 = w; = ")
I Note that b�M
(q,�) = �(q,�) for � 2 ⌃.I We write b� when M is clear from the context.
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 17 / 41
An equivalent definition
Definition 7 (Equivalent definition)M = (Q,⌃, �, q0,F ) accepts w = w1w2 . . .wn
, if 9r0, . . . , rn
2 Q s.t.,I
r0 = q0.I �(r
i
,wi+1) = r
i+1, for all 0 i < n.I
r
n
2 F .
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 18 / 41
The language of a DFA
Definition 8L(M), the language of a DFA M, is the set of strings that M accepts.
We sometime say that M accepts the language L(M).
IM may accept many strings
IM accepts only one language.
What language does M accept if it accepts no strings?
Definition 9A language is called regular, if some deterministic finite automatonaccepts it.
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 19 / 41
Example: DFA M2
IQ = {q1, q2}, ⌃ = {0, 1}, F = {q2}, � =?
I What is L(M2) := {w 2 {0, 1}⇤ : b�(q1,w) = q2}?
Theorem 10L(M2) = {w 2 {0, 1}⇤ : #1(w) is odd}. #1(w) — number of ones in
w.
I Proof by induction on the word lengthI What does it mean?
The j ’th assumption: w 2 {0, 1}j is in L(M) iff #1(w) is odd
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 20 / 41
Proving L(M2) = L0 := {w 2 ⌃⇤ : #1(w) is odd}
IBasis (length 0): " /2 L0 and b�(q1, ✏) = q1 =) ✏ /2 L(M2).
IStep: assume hypothesis holds for words of length j � 0.
I Let x = y� 2 {0, 1}j+1 for � 2 {0, 1} (hence, |y | = j).I If #1(y) is even. By assumption b�(q1, y) = q1.
I � = 1 =)1. #1(x) is odd =) x 2 L0
2. b�(q1, x) := �(b�(q1, y),�) = �(q1, 1) = q2 =) x 2 L(M2).I � = 0 =)
1. #1(x) is even =) x /2 L0
2. b�(q1, x) := �(b�(q1, y),�) = �(q1, 0) = q1 =) x /2 L(M2).
I If #1(y) is odd...I We proved for x 2 {0, 1}j+1: x 2 L(M2) () x 2 L0
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 21 / 41
The language of M1
q1
0
q2 3q
11 0
0,1
Theorem 11L(M1) = {w102k : k � 0,w 2 {0, 1}⇤}
Proof:Claim 12 (implies the theorem)
Let L0i
= {x 2 {0, 1}⇤ : b�(q1, x) = q
i
} and letI L1 = {0k : k � 0}I L2 = {w102k : k � 0,w 2 {0, 1}⇤}I L3 = {w102k+1 : k � 0,w 2 {0, 1}⇤}
Then, L0i
= Li
for every i 2 {1, 2, 3}
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 22 / 41
Proving Claim ??
We need to prove that 8i 2 {1, 2, 3}:
x 2 Li
() x 2 L0i
.
Recall that
x 2 L0i
() b�(x) = q
i
I Proof by induction on word length.I Induction basis: Easy to see that hypothesis holds for ✏.I Induction step: Assume hypothesis holds for words of length j � 0.I Let x = y� 2 {0, 1}j+1 for � 2 {0, 1}.I We prove the hypothesis for x , separately for each i 2 {1, 2, 3}
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 23 / 41
L1 = {0k : k � 0}
Recall x = y� 2 {0, 1}j+1 for � 2 {0, 1}.
Proving: x 2 L1 =) b�(q1, x) = q1 ( =) x 2 L01).
Ix = 0j+1, y = 0j and � = 0.
I Since y 2 L1, by i.h. b�(q1, y) = q1
I Therefore, b�(q1, x) = �(b�(q, y),�) = �(q1, 0) = q1.
Proving:
b�(q1, x) = q1 =) x 2 L1.
I Let q
y
= b�(q1, y) (hence, b�(q1, x) = �(qy
,�) = q1)I
q
y
= q1 and � = 0. (?)I By i.h. y = 0j .I Hence, x = y� = 0j0 = 0j+1 2 L1.
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 24 / 41
L2 = {w102k : k � 0,w 2 {0, 1}⇤}
Recall x = y� 2 {0, 1}j+1 for � 2 {0, 1}.
Proving x 2 L2 =) b�(q1, x) = q2.
I Assume � = 1I Since �(q
i
, 1) = q2 for any i =) b�(q1, x) = q2.
I Assume x = w102k for k > 0 ( =) y = w102k�1 and � = 0)I Hence, y 2 L3.I By i.h. b�(q1, y) = q3I Thus, b�(q1, x) = �(q3, 0) = q2.
Proving
b�(q1, x) = q2 =) x 2 L2.
I Assume � = 1 =) x 2 L2. (?)I Assume � = 0
Iq
y
:= b�(q1, y) = q3I By i.h. y = w102k+1 for some k � 0I Therefore x = y� = w102k+10 2 L2.
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 25 / 41
L3 = {w102k+1 : k � 0,w 2 {0, 1}⇤}
Recall x = y� 2 {0, 1}j+1 for � 2 {0, 1}.
Proving x 2 L3 =) b�(q1, x) = q3.
Ix = w102k+1, y = w102k and � = 0
Iy 2 L2
I By i.h. b�(q1, y) = q2.I Therefore, b�(q1, x) = �(q2, 0) = q3.
Proving
b�(q1, x) = q3 =) x 2 L3.
I Let q
y
= b�(q1, y)
I Hence, q
y
= q2 and � = 0 (?)I By i.h. y = w102k
I Therefore, x = y� = w102k0 2 L3.
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 26 / 41
Part III
Regular Operations
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 27 / 41
Additional examples of regular languages
Let ⌃ = {0, 1}.I {w 2 {0, 1}⇤ : #1(w) ⌘ 0 mod 7}.I Sequence of 0 followed by sequence of 1, i.e., {0m1n : m, n � 0}.I Any finite language.
All the above languages are regular
Is there a simple proof?
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 28 / 41
The regular operations
Let A and B be languages.
The union operation:
A[B = {x : x 2 A _ x 2 B}
The concatenation operation:
AkB = {xy : x 2 A ^ y 2 B}
The star operation:
A⇤ = {x1x2 . . . xk
: k � 0 and each x
i
2 A}
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 29 / 41
The regular operations – Examples
Let A= {good, bad} and B = {boy, girl}.
UnionA [ B = {good, bad, boy, girl}
Concatenation
AkB = {goodboy, goodgirl, badboy, badgirl}
StarA⇤ = {", good, bad, goodgood, goodbad, badbad, badgood, . . .}
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 30 / 41
Closure under union
Theorem 13If L1 and L2 are regular languages, then so is L1 [ L2.
Approach to Proof:I Some DFA M1 accepts L1
I Some DFA M2 accepts L2
I Construct DFA M that accepts L1 [ L2.
Attempted Proof Idea:I first emulate M1, andI if M1 doesn’t accept, then emulate M2.
What’s wrong with this?Fix: Emulate both machines simultaneously.
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 31 / 41
Closure Under Union: Correct Proof
SupposeI
M1 = (Q1,⌃, �1, q1,F1) accepts L1,I
M2 = (Q2,⌃, �2, q2,F2) accepts L2.Define M as follows (M will accept L1 [ L2):
IQ = Q1 ⇥ Q2.
I ⌃ is the same.I For each (r1, r2) 2 Q and a 2 ⌃,
�((r1, r2), a) = (�1(r1, a), �2(r2, a))
Iq0 = (q1, q2)
IF = {(r1, r2) : r1 2 F1 or r2 2 F2}.
I Formal proof (next slide) |(hey, why not choose F = F1 ⇥ F2?)
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 32 / 41
Correctness of the construction
Claim 14
L(M) = L(M1) [ L(M2).
Follows by the next claim.
Claim 15
b�M
((q1, q2), x) = (b�1(q1, x), b�2(q2, x)).
Proof: By induction on word length. DIY ... |Proving Claim ??:
Ix 2 L(M1) =) b�1(q1, x) = r1 2 F1. (similar if x 2 L(M2).)
I By Claim ??, b�M
((q1, q2), x) = (r1, ·) 2 F =) x 2 L(M).
Ix 2 L(M) =) b�
M
((q1, q2), x) = (r1, r2) 2 F .I By Claim ??, (r1, r2) = (b�1(q1, x), b�2(q2, x)).I Hence, either r1 2 F1 or r2 2 F2 =) x 2 L(M1) [ L(M2).
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 33 / 41
What about concatenation?
Theorem 16If L1, L2 are regular languages, then so is L1kL2.
Example: L1= {good, bad} and L2 = {boy, girl}.
L1kL2 = {goodboy, goodgirl, badboy, badgirl}
This is much harder to prove.
Idea: Simulate M1 for a while, then switch to M2.
Problem: But when do you switch?
This leads us into non-determinism, wait for next class...
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 34 / 41
Part IV
Non-deterministic Finite Automata
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 35 / 41
NFA — non-deterministic Finite Automata
q4
q1q2
q3
0
0,1 0,1
10
I May have more than one transition labeled with the same symbol,I May have no transitions labeled with a certain symbol,I May have transitions labeled with ", the symbol of the empty
string. Will deal with this latter
Every DFA “is” also an NFA.
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 36 / 41
Non-deterministic computation
q4
q1q2
q3
0
0,1 0,1
10
What happens when more than one transition is possible?I The machine “splits” into multiple copiesI Each branch follows one possibilityI Together, branches follow all possibilities.I If the input doesn’t appear, that branch “dies”.I Automaton accepts if some branch accepts.
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 37 / 41
Computation on 1001
q4
q1q2
q3
0
0,1 0,1
10
q1
q1
q2
q1
q2
q1 q3
q1q4
1
0
0
1
symbol
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 38 / 41
Why non-determinism?
Theorem 17 (Informal, to be proved soon)Deterministic and non-deterministic finite automata, accept exactly the
same set of languages.
Q.: So why do we need NFA’s?
Design a finite automaton for the language L — all binary strings witha 1 in their third-to-the-last position?
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 39 / 41
NFA for L
q4
q1q2
q3
1
0,1
0,10,1
I “Guesses” which symbol is third from the last, andI checks that indeed it is a 1.I If guess is premature, that branch “dies”, and no harm occurs.
Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 40 / 41
DFA for LI Have 8 states, encoding the last three observed letters.I A state for each string in {0, 1}3.I Add transitions on modifying the suffix, give the new letter.I Mark as accepting, the strings 1 ⇤ ⇤
1
q000
q111q001
q010
q011
q100
q101
q110
0
0 0
0
000
1 1
1
1 1 1
DFA has few bugs...Nachum Dershowitz & Yishay Mansour Computational Models #1 March 13-15, 2017 41 / 41