Discrete Mathematics
Ch t 6 L Fi it
- - Chapter 6: Languages: Finite
State Machine
Hung-Yu Kao (高宏宇) Department of Computer Science and Information Engineering, N l Ch K U National Cheng Kung University
Discrete Mathematics -- Chapter 6: Languages: Finite Ch t 6 L Fi - - PowerPoint PPT Presentation
Discrete Mathematics -- Chapter 6: Languages: Finite Ch t 6 L Fi it State Machine Hung-Yu Kao ( ) Department of Computer Science and Information Engineering, N National Cheng Kung University l Ch K U Outline 6.1 Language:
Hung-Yu Kao (高宏宇) Department of Computer Science and Information Engineering, N l Ch K U National Cheng Kung University
6.1 Language: The Set Theory of Strings 6 2 Finite State Machines: A First Encounter 6.2 Finite State Machines: A First Encounter 6.3 Finite State Machines: A Second Encounter
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
2
finite number of internal states where the machine remembers t i i f ti h it i i ti l t t certain information when it is in a particular state.
processing of information by a computer. processing of information by a computer.
+
∈Z n
and
ion juxtaposit the denotes where }, , | { 2) 1)
1 1
y x xy y x xy
n n
∑ ∈ ∑ ∈ = ∑ ∑ = ∑
+ Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
3
j p }, , | { ) y y y y
E 6 1 L t ∑
l h b t
Ex 6.1 : Let ∑ an alphabet.
i b l h i ld } { } 11 , 10 , 01 , 00 { }, 1 , { e.g., }, , | { 2, If
3 3 2 2
dd b d b y x xy n ∑ ∑ = ∑ = ∑ ∑ ∈ ∑ ∈ = ∑ =
Definition 6 2: For an alphabet ∑ we define ∑0 ={ } where
λ
. etc , , , e.g., strings, symbol
5 contain would }, , , , , {
3 3
cdd acb aaa e d c b a ∑ = ∑
λ
Definition 6.2: For an alphabet ∑ we define ∑
{ }, where denotes the empty string, i.e., the string consisting of no symbols taken from ∑. λ
since } { ) 1 ( λ λ ∑ ∉ ∑ ⊄
λ
| | 1 | } { | because } { ) 2 ( since } { ) 1 ( φ λ φ λ λ λ = ≠ = ≠ ∑ ∉ ∑ ⊄
Definition 6.3: If ∑ is an alphabet, then
U U
∗ ∈ ∞ = +
∑ ∑ ∑ = ∑ = ∑
+
1
) 1 (
n n n n n Z
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
4
U∞
= ∗
∑ = ∑ ) 2 (
n n
Ex 6.2 : Let ∑ = {0,1} the set ∑* consists of all finite strings of 0’s
and 1’s together with the empty string. (how about ∑+?)
If ∑ = {ß 0 1
9 + / } where ß denotes the blank (space) Here in
If ∑ = {ß,0,1,…,9,+,-,/,}, where ß denotes the blank (space). Here in
∑* we find familiar arithmetic expression such as (7+5)/(2-3).
Definition 6.4:
, , , If
2 1 2 2 1 1 2 1
y y y w x x x w w w
n m
⋅ ⋅ ⋅ = ⋅ ⋅ ⋅ = ∑ ∈
+
. all for and if ) ( equal are and say then we , , , , , , , , and
2 1 2 1 2 1 2 1
i y x n m w w w w y y y x x x
i i n m
= = = ∑ ∈ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
5
Definition 6.5:
. 1 for where , Let
2 1
≤ ≤ ∑ ∈ ∑ ∈ ⋅ ⋅ ⋅ =
+
n i x x x x w
i n
0. || || and ||, || by donated , is
length The = λ w n w
Definition 6.6:
, , , , Let
2 1 2 1
⋅ ⋅ ⋅ = ⋅ ⋅ ⋅ = ∑ ∈
+
y y y y x x x x y x
n m
. : and
ion concatenat The . , , , , , , , and
2 1 2 1 2 1 2 1
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ = ∑ ∈ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ y y y x x x xy y x y y y x x x
n m n m
. : and
ion concatenat The . , : and
ion concatenat The λ λλ λ λ λ λ λ = = = x x x x x
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
6
Definition 6.7:
, , , by
powers the define we ,
2 1
xx x x x x x x = = = ∑ ∈
∗
λ
Definition 6.8:
. where , , , ,
1 2 3
N n xx x xx x
n n
∈ ⋅ ⋅ ⋅ = ⋅ ⋅ ⋅ =
+
ffi h i i f ffi ll d i Si il l prefix. proper a be to is then , if and ,
prefix a called is then , and , If λ λ ≠ = ∑ ∈
∗
x y w x xy w y x
Definition 6.9:
. n suffix whe proper a is it ,
suffix a called i Similarly, λ ≠ x w s y f b i ll d i h d If ∑∗ . substring proper a call we , from different is and
least at When .
substring a called is then , and , , If y z x w y xyz w z y x λ = ∑ ∈
∗ Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
7
Definition 6.10:
l t th ll hi h b t th i l d Thi .
language a called is
subset any , alphabet given a For φ ∑ ∑ ∑
∗
i h ∑ h l h b f 26 l 10 di i d h i l b l d i
. language empty the call which we , subset the includes This φ
given implementation of C++, the collection of executable programs for that implementation constitutes a language.
language, as could a particular set of such programs.
Since languages are sets, we can form the union, intersection, and
symmetric difference of two languages.
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
8
Definition 6.11:
, and
ion concatenat the , , languages any alphabet an For B A B A ∑ ⊆ ∑
∗
. } , | { is , denoted B b A a ab AB ∈ ∈
Ex 6.10 :
}. , { }, , , { languages finite the be , let and }, , , { Let y B z xy x A B A z y x = = = ∑ λ | || | 2 3 6 5 | | 2) | | 6 5 | | 1) } , , , , , { and } , , , , { Then B A AB BA AB yz yxy yx z xy x BA zy xyy z xy x AB ≠ = ≠ = = = | || | 2 3 6 5 | | 2) B A AB = ⋅ = ≠ =
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
9
Theorem 6.1:
C B A ∑ ⊆ ∑ ) ( ) ( b) } { } { ) . , , let , alphabet an For
*
λ λ CA BA A C B AC AB C B A CA BA A C B AC AB C B A BC A C AB A A A ∩ ⊆ ∩ ∩ ⊆ ∩ ∪ = ∪ ∪ = ∪ = = = ) ( f) ) ( e) ) ( d) ) ( c) ) ( ) ( b) } { } { a) λ λ x CA BA A C B AC AB C B A ∑ ∈ ∩ ⊆ ∩ ∩ ⊆ ∩ , For (f) ) ( f) ) ( e)
*
: Proof CA BA A z C y yz x A z B y yz x A z C B y yz x A C B x ∈ ∈ = ∈ ∈ = ⇒ ∈ ∩ ∈ = ⇒ ∩ ∈ d ) and for ( and ) and for ( and where ) ( CA BA A C B CA BA x CA x BA x ∩ ⊆ ∩ ∴ ∩ ∈ ⇒ ∈ ∈ ⇒ ) ( and
A C B CA BA yy y A xy y C y xx x B ) ( } , { }, , { }, , , { ∉ = = =
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
10
CA BA A C B ∩ ⊆ ∩ ∴ ) (
A C B xyy CA BA xyy ) ( , ∩ ∉ ∩ ∈
Definition 6.12:
: follows as languages
construct can we language given a For
*
∑ ⊆ A .
closure positive the , b) } , { , and , }, { a)
1 1
λ = ∈ ∈ = ∈ = =
∈ + + +
+
A A A A b A ab|a A n A A A
n n n n
U
Z
Z 1994)
Kleene, Cole Stephen logician American the
honor (in A.
closure Kleene the called is * A language The }. { c)
*
λ ∪ =
+
A A
Ex 6.11 :
; } { (2) }; { (1) then } { and } { If ∈ = = = = ∑ N n x A A x A z y x
n n
λ }. | { (4) and }; 1 | { (3) ; }, { (2) }; { (1) then }, { and }, , , { If
*
≥ = ≥ = ∈ = = = = ∑
+
n x A n x A N n x A A x A z y x
n n
λ
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
11
Lemma 6.1:
*
then , If . , languages with alphabet, an be Let ⊆ ⊆ ∑ ⊆ ∑
n n
B A B A B A
1 1
, for truth the Assuming ) ii ( 1, (i) ⊆ ⇒ ⊆ = = ⊆ = =
k k
B A B A k n B B A A n : Proof
1 1 1 1
, ), hypothesis induction ( . , , i.e. , If ) iii (
+
∈ ∈ ∴ ⊆ ⇒ ⊆ ∈ ∈ ∈ =
k k k k k k k k
B x B x B A B A A x A x A x x x Q
1 1 1 1 + + +
⊆ ⇒ = ∈ = ⇒
k k k k k
B A B BB x x x
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
12
Theorem 6.2:
b) a) , , languages and alphabet an For
* * *
A B A AB A B A ⊆ ⊆ ∑ ⊆ ∑ ) ( ) ( ) ( f) e) d) c) b) a)
* * * * * * * * * * *
A A A A A A A A A AA B A B A B A B A A B A AB A = = = = = = ⊆ ⇒ ⊆ ⊆ ⇒ ⊆ ⊆ ⊆
+ + + + +
] ) ( ) [( (g) ) ( ) ( ) ( g)
* * * * * * * * * * *
B A B A B A B A B A ∪ = ∪ = ∪ = ∪ : Proof (d)] [by ) ( ) ( ) ( ) ( , (i) ] ) ( ) [( (g)
* * * * * * * *
B A B A B A B A B B A A B A B A ∪ ⊆ ∪ ⇒ ∪ ⊆ ∪ ⇒ ⊆ ⊆ ∪ = ∪ (f)] d (d) [b ) ( ) ) ( ( ) ( ) ( ) ( (d)] [by ) ( , , (ii)
* * * * * * * * * * * *
B A B A B A B A B A B A B A B A B A ∪ ⊆ ∪ ⇒ ∪ ⊆ ⇒ ∪ ⊆
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
13
(f)] and (d) [by ) ( ) ) ( ( ) ( B A B A B A ∪ = ∪ ⊆ ∪ ⇒
Ex 6.14 :
where languages he consider t {0 1} alphabet an For
*
A ∑ ⊆ = ∑ : follows as y recursivel language this define can We etc. 0111, 10, 01, 0, e.g., 0, symbol the
exactly contains in each word where , languages he consider t {0,1} alphabet an For A A A ∑ ⊆ = ∑ words the in include want to we process recursive For the 2) that us tells step base Our 1) y g g , , , , A A ∈ . for 1, and 1 A x x x ∈
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
14
Definition (palindrome):
d t d i f l Th . in consider , alphabet an Given
* 2 1
x x x x
R n
∑ ⋅ ⋅ ⋅ = ∑ 1) : follows as y recursivel string a
reversal the define can We . denoted is
reversal The
1 1
x x x x x
R n n R
= ⋅ ⋅ ⋅ =
−
λ λ . ) ( ) ( define we here,
where can write then we , if , For 2) 1)
1
z y zy x y z zy x x n
R R R n n
= = ∑ ∈ ∑ ∈ = ∑ ∈ ∈
+
N λ λ
) (
* R R R
x x x x x x ⇒ ∑ ∈
|| || 1 || || 1 || | | (iii) , || | | (ii) || || (i) induction al mathematic By
1 1
k k k x x = = : Proof
. ) ( ,
1 2 2 1 2 1
x x x x x x = ⇒ ∑ ∈
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
15
. ) ( ) ( ) ( ) ( , || || , 1 || || , , 1 || | | (iii)
1 2 1 2 1 2 2 1 2 1 2 1 1 1 1 1 R R R R R R R R R
x x zy x z y x z x y x zy x x k y z zy x k x = = = = = = = = + =
peppermint (P) and spearmint (S).
p quarters and returns the necessary change.
package of peppermint-flavored chewing gum. p g p pp g g
t0 t1 t2 t3 t4 State (1) s0 (4) s1 (5c) (7) s2 (10c) (10) s3 (20c) (13) s0 Input (2) 5c (5) 5c (8) 10c (11) W Output (3) Nothing (6) Nothing (9) Nothing (12) P Output (3) Nothing (6) Nothing (9) Nothing (12) P
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
16
D fi iti 6 13
Definition 6.13
; for states internal
set the where ), , , , , ( tuple
is machine state finite A M S w v O I S M = = ; for alphabet
the ; for alphabet input the ; for states internal
set the M O M I M S = = =
v w 0 1 0 1
; function
the is : ; function state next the is : O I S w S I S v → × → ×
s0 s0 s1 0 0 s1 s2 s1 0 0 s s s 1
where ), , , , , ( machine state finite he Consider t w v O I S M =
s2 s0 s1 0 1
. table state by the given are , and 0,1}, { }, , , { ), , , , , (
2 1
w v O I s s s S = = =
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
17
Wh t i th t t t i f th i t t i 1100101101?
I (O).
State s0 v(s0 , 1)= s1 v(s1 , 0)= s2 v(s2 , 1)= s1 v(s1 , 0)= s2 Input 1 1 Output w(s0 1)= 0 w(s1 1)= 0 w(s2 1)= 1 w(s1 0)= 0 Output w(s0 , 1) 0 w(s1 , 1) 0 w(s2 , 1) 1 w(s1 , 0) 0
0,0 1,0
v w 1 1
s0 s1 s2
Start 1,0 0,0
0 1 0 1 s0 s0 s1 0 0 s1 s2 s1 0 0
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
18
1,1 0,0
s2 s0 s1 0 1
E t i
h t t t t ti F S I ∗
s x s v x x s v v x x s v s x s v = = = =
3 2 2 2 1 1 2 1 1 2 1 1
) , ( ) ), , ( ( ) , ( ) , (
have we , state any at starting , For
1 2 1
S s I x x x
k
∈ ∈ ⋅ ⋅ ⋅
∗
s x s v x x x s v v v x x x s v
s x s v s
= = =
= 4 3 3 3 ) , ( 2 1 1 3 2 1 1
) , ( ) ), , ) , ( ( ( ) , (
3 2 2 2
4 43 4 42 1 4 3 4 2 1 y x s w S s x s v x x x s v
k k k k
= ∈ = = ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
+1 2 1 1
) ( ) , ( ) , ( y y y x s w x s w x s w x x x s w y y x s w x s w x x s v w x s w x x s w y x s w = = = = = =
3 2 1 3 3 2 2 1 1 3 2 1 1 2 1 2 2 1 1 2 1 1 1 1 2 1 1 1 1 1
) , ( ) , ( ) , ( ) , ( ) , ( ) , ( ) ), , ( ( ) , ( ) , ( ) , ( S s s s v O y y y x s w x s w x s w x x x s w
k k k k
∈ ∈ ⋅ ⋅ ⋅ = ⋅ ⋅ ⋅ = ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
∗ 2 1 2 2 1 1 2 1 1
all for ) ( Also ) , ( ) , ( ) , ( ) , ( λ
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
19
S s s s v ∈ =
1 1 1
all for ) , ( Also, λ
E 6 19 A
i l bi dd i fi it t t hi th t
use to obtain x + y.
01101 , 00111
1 2 3 4 5 1 2 3 4 5
= = = = y y y y y y x x x x x x
g ,
01101 , 00111
1 2 3 4 5 1 2 3 4 5
y y y y y y x x x x x x
Serial binary
1 2 3 4 5
x x x x x x =
1 2 3 4 5
z z z z z z =
adder
1 2 3 4 5 1 2 3 4 5
y y y y y y =
1 2 3 4 5
z z z z z z
1 1 1
+1 +1 +1 +1
x = 0 0 1 1 1 + y = 0 1 1 0 1 z = 1 0 1 0 0
third addition first addition
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
20
Th i l bi dd i d l d b fi it t t hi M (S I O
v, w). S = {s0, s1}, where si indicates a carry of i; I = {00, 01, 10, 11}; O = {0, 1}; and v, w are given in the following state table.
v w 00 01 10 11 00 01 10 11 s0 s0 s0 s0 s1 0 1 1 0 s1 s0 s1 s1 s1 1 0 0 1
+1 00, 0 01, 1 10, 1 11 0 01, 0 10, 0 11, 1 +1
s0 s1
Start 11, 0 00, 1
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
21
S dditi l hi l t t th d i f t
Some additional machines relevant to the design of computer
hardware, e.g., sequence recognizer.
the sequence 111.
{0, 1}*{111}
0, 0 1, 1
s0 s1 s2
Start 1, 0 1, 0 0, 0 0, 0
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
22
equivalent
E 6 21 C
t t hi th t t l i th
in a position that is a multiple of three. p p
Start 1, 0 1, 1 0, 0 1, 0
s0 s1 s2
Start 0 0 0, 0 0, 0 0, 0 1, 0
s3 s4
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
23
0, 0 1, 0
E 6 22 C
t t hi th t t l i ( ) th
that is a multiple of four. p
Start 1, 1 0, 0 0, 0 1, 0
s0 s1 s2
1, 0 0, 0
s3
0, 0
s0 s1 s2
1, 0 0, 0
s3
0, 0 1, 0
s4
0, 0
s
1, 0
s
0, 0 1, 0 0, 0 1, 0 Start 0, 0 1, 1
4
0, 0 1 0
s5 s6
1, 0
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
24
1, 0
precisely those strings in the language A = {01, 0011, 000111,…} =
? } | 1 {
+
Z i
i i
? } | 1 {
+
∈Z i
i i
State s0 s1 s2 … si si+1 … sj sj+1 … sn sn+1 … s2n s2n+1 Input 1 1 1 Output 1 1 1 Output 1 1 1 State s0 s1 s2 si sj+1 sn sn+1 s2n s2n+1
1 2
…
i j 1
…
n n 1
…
2n 2n 1
Input 1 1 1 Output 1 1 1
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
25
. 1 | | let , in strings recognize can } , , , , { n S A w v O I S M ≥ = = i i i l i h l b | | , , , states 1 by s 0' 1 process will language in the 1 Consider
1 1 1
S s s s n n M A
n n n
⋅ ⋅ ⋅ + + ⇒
+ +
1 ) ( ) 1 ( h 1 i tables) the (see columns remove can We states two exists principle, pigeonhole by , | |
1 ) ( ) 1 (
i j M i j s s n S
n i j n j i
− ∴ = =
+ − − +
Q contradict recognize cannot fact, In 1 ) ( ) 1 ( where , 1 recognizes
1 ) ( ) 1 (
x M A x n i j n x M
n i j n
∴ ⇒ ∉ + < − − + = ⇒
+ +
. in strings g recognizin machine state finite a construct cannot We contradict A ∴ ∴
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
26
O it d l hi
One-unit delay machine
0, 0 0, 0
Output 0 when receive input s0 s1
Start 0, 1 1, 0
Output 0 when eceive input s2
1, 0
Output 1 when receive input
1, 1
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
27
T it d l hi
Two-unit delay machine
0, 0
s3
0, 0
s1 s3
0, 0 0, 1 1, 0
s0 s1
Start 0, 0 1, 0
s4
0, 1
s0 s1
Start 0, 0 1, 0
s4
0, 0 1, 1 1, 0
s2 s5
1 0 0, 0 1, 0
s2 s5
1, 0 0, 0 1, 0 0, 1 1, 0
s6
1, 0
s6
1 1 1, 0
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
28
1, 1
M1
D fi iti 6 14
w v O I S M machine state finite a is } , , , , { =
j i j i i j j i
s s s s s x s v s s s reachable s S s s Fig.) in the , , from reachable is (e.g., ) , (
if from is , , a) } , , , , {
2 1 3
= = ∈ s I x s x s v s s x I x s x s v transient s Fig.) in the (e.g., all for ) , ( if sink a is c) Fig.) in the (e.g., implies for ) , ( if is b)
3 * 2 *
∈ = = ∈ = λ
(出去回不來)
I S I S
M w v O I S M w w S I S v v I I S S
submachine a is } , , , , { , | , : | if , , d)
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1
= = → × = ⊆ ⊆
× × i j j i
s s S s s from reachable is , , any for if connected Strongly e) ∈
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
29
D fi iti 6 15
Definition 6.15
if to from ) ( a called is states. distinct two be , let , machine state finite a For s s sequence transition transfer I x s s M
j i
∈
+
|| | | || || ) , ( with b) ) , ( a) if to from ) ( a called is x y s y s v I y s x s v s s sequence transition transfer I x
j i j i j i
≥ ⇒ = ∈ = ∈
+
v w
from s0 to s2 in the state table.
|| | | || || ) ( ) y y y
j i
0 1 0 1 s0 s1 s6 s1 s5 s0 0 1 0 1 2
s2 s3 s4 s1 s2 s4 s0 s2 s1 0 1 0 1 0 1 1 1 s5 s6 s3 s5 s3 s6 1 1 1 1
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
30
Construct a state diagram for a finite state
s1
0, 0
0, 1
s1 s0
Start 1, 0 1, 0 0, 0
s0
Start
s2
1, 1
s2
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
31
Let M be the finite state machine in the following figure. For
22
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
32
6.1: 2, 12, 20 6.2: 4, 8
6.3: 4, 7(c), 8 Supplementary: 6 Supplementary: 6
Discrete Mathematics Discrete Mathematics – – CH6 CH6 2009 Spring 2009 Spring
33