Introduction to the theory of computation

Download Report

Transcript Introduction to the theory of computation

1 Introduction to the theory of
computation
Computer science 是很實際的學科,大多數人
對 computation theory 沒有什麼興趣,除非理
論能夠對他們所要解決的實際問題有幫助。
那為什麼要學習計算理論的課程呢?
 理論提供一些幫助我們了解
computer
science 的觀念;可以建構出一個理論的計算
機,由理論的研究可知道實際電腦的限制。實際
有用的東西常是依據「簡單的理論」而來。
 理論觀念可應用在很多地方。
 這些理論的主題是有趣、令人興奮的。
第一章
1
本章的主要主題
Automata, An automaton is a construct
that possesses all the indispensable
features of a digital computer.
A formal language is an abstraction of the
general characteristics of programming
languages.
之後再討論 mechanical computation 的
概念,也就是 algorithm 的觀念,以及哪
些問題適合用 algorithm 來解、哪些不能
。
第一章
2
outline
1.1 Mathematical preliminaries and notation sets
 介紹一些後面會用到的專有名詞,像: set
theory, functions, relations, trees,
graph
等, deduction,
induction,
contradiction 等證明觀念。
1.2 Three basic concepts
 初步介紹
languages,
grammars,
automata 三種觀念,以後會更嚴謹的介紹
。
1.3 Some applications
 這些觀念的一些應用。
第一章
3
1.1 Mathematical Preliminaries
and Notation
集合 Sets : A set is a collection of elements, without
any structure other than membership.
S = {0, 1, 2}, 可判斷 x  S 與 x  S
S = {i : i > 0, i 是偶數 }
union 聯集、 intersection 交集、 difference 差
S1  S2 = { x: x  S1 or x  S2}
S1  S2 = { x: x  S1 and x  S2}
S1 - S2 = { x: x  S1 and x  S2}
第一章
4
關於集合 (set)
complementation: The complement of a set
S consists of all elements not in S. S
Universal set U: 代表所有可能的元素。
S ={x : x∈U , x∉S }
空集合 Φ 或 ,empty set 或 null set :就是
不包含任何元素的集合。
S   = S -  = S,
S=
∅=U , S=S
第一章
5
關於集合 (set)
DeMorgan’s laws
S 1∪S 2=S 1∩S 2
S 1∩S 2=S 1∪S 2
子集合 subset
若 S1 的元素也都是 S 的元素,則 S1 為 S 的
subset 。 S1  S
若 S 中存在一個元素不屬於 S1 ,則 S1 為 S 的
proper subset( 真子集合 ) 。 S1  S
disjoint set ,互斥集合,交集為空集合者。
S1  S2 = 
第一章
6
關於集合 (set)
有限集合 finite sets ,元素個數為有限者;其他
的為 infinite sets 無限集合。
集合 S 的元素個數記成 |S| 。
部份集合 powerset ,所有子集合所形成的集合
稱為部份集合。令 2S 為 S 的 powerset ,其元
素個數為 2|S| 。
S = {a, b, c}
2S = {, {a}, {b}, {c}, {a,b}, {b, c}, {c, a},
{a, b, c} }.
第一章
7
關於集合 (set)
Cartesian product
S = S1  S2 = {(x, y) : x  S1 and y  S2 }
S1  S2  …  Sn = {(x1, x2, …, xn ) : xi  Si}
Example 1.2 令 S1 = {2, 4}, S2 = {2, 3, 5,
6}
S1  S2 = {(2,2), (2, 3), (2, 5), (2, 6), (4, 2),
(4, 3), (4, 5), (4, 6)}
第一章
8
關於 functions and relations
A function is a rule that assigns to
elements of one set a unique element of
another set.
f : S1 →S2
其中 f 的
subset 、 f
subset 。
domain( 定義域 ) 是
的 range( 值域 ) 是
S1
S2
的
的
如果 S1 就是 f 的 domain ,則 f 稱為
total
function 、否則稱為
partial
function 。
第一章
9
關於 functions and relations
令 f(n) 與 g(n) 是兩個整數函式 (domain 是
整數的 subset) 。 If there exists a positive
constant c such that for all n, f(n) ≤ c
g(n) ,則稱「 f has order at most g 」, f(n) =
O(g(n))
若 |f(n)| ≥ c |g(n)|, 「 f has order at least g 」 ,
f(n) = Ω (g(n))
若存在 c1 與 c2 兩個常數, c1 |g(n)| ≤ |f(n)| ≤ c2
|g(n)|, 「 f and g have the same order of
magnitude 」 , f(n) = Θ (g(n))
第一章
10
關於 functions and relations
Example 1.3
f(n) = 2n2 + 3n
g(n) = n3
h(n) = 10 n2 + 100
則
f(n) = O(g(n))
g(n) = Ω(h(n))
f(n) = Θ(h(n))
只與最高次項有關、其餘的項
目對函式的成長速度關係較少
關於 O, Θ, Ω 在演算法、
離散數學或演算法中有比
較詳細的描述。
第一章
11
關於 functions and relations
函式可以用集合的方式表示
{(x1, y1), (x2, y2), …} ,其中 xi
domain 的元素、 yi 是 range 的元素。
是
一個集合若表示函式的話,每個
xi
只能在
pair 中第一個位置出現一次。
若有某個
xi
出現兩次以上的話,就不是
function ,只能稱它為
relation
。所以
relation 比函式一般化。
第一章
12
關於 functions and relations
一種特別的 relation 稱為 equivalence
relation ; (x,y) 若屬於一個 equivalence
relation 的話,記成 x≡y 。
一個 equivalence relation 必須滿足下列三
個條件: the reflexivity rule( 反身性 ), x≡x
for all x, the symmetry rule( 對稱性 ), if
x≡y
then y≡x 與 the transitivity
rule( 遞移性 ), if x≡y and y≡z , then x≡z.
Example 1.4 除以 3 的餘數是否相同的關係
,是不是 equivalence relation 呢?
第一章
13
關於 Graphs and Trees
圖形 G = (V, E) , V = {v1, v2, …, vn} 是
vertex set 點集合 , E = {e1, e2, … em} 是
edge set 邊集合,兩者皆為有限集合,有限圖
形。
ei = (vj, vk) 是從 vj 連到 vk 的邊、有方向
的邊; ei 是 vj 的 outgoing edge ,是 vk
的 incoming edge 。
邊都有方向的圖形、稱為有向圖
directed
graph, digraph 。圖形的點或邊都可以有名
稱。
第一章
14
關於 Graphs and Trees
v1
v2
v3
Figure 1.1
V={v1,v2,v3}, E={(v1,v3), (v3, v1), (v3, v2), (v3, v3)}
walk (v1, v3), (v3, v3), (v3, v2)
path: 沒有重複邊的 walk, simple path : 沒有重複
點的 path
length, cycle, simple cycle, loop
找兩點間所有的最短路徑。 BFS 的方法。
第一章
15
關於 Graphs and Trees
A tree is a directed graph that has no cycle.
There is exactly one path from the root to
every other vertex.
root 沒有 incoming
edge 、其他點都有一條
incoming edge ;
parent, child, leaves, level: root 到該點的
edge 數 , height 。
ordered trees ,同一個 level 的 node 都有順序
。
degree: in-degree 與 out-degree
第一章
16
關於 Graphs and Trees
ro o t
le v e l 0
H e ig h t = 3
le a f
F ig u r e 1 .2
le v e l 3
第一章
17
關於 proof techniques
必須擁有「了解證明的能力」。
最常用、最重要的兩個證明: proof by induction 歸
納法、 proof by contradiction 矛盾證法、反證法。
歸納法證明通常是證明 P1, P2, … 每個 statement
都是正確的,包括幾個部份
 Basis: 證明 P1, P2, …, Pk, for k ≥ 1, 是正確的
 Inductive assumption: 假設 P1, P2, …, Pn 是正確
的, for some n ≥ k 。

Inductive step :證明 P1, P2, …, Pn → Pn+1, for any n ≥ k
第一章
18
關於 proof techniques
Example 1.5 A binary tree is a tree in
which no parent can have more than two
children. Prove that a binary tree of
height n has at most 2n leaves.
Proof: 令 l(n) 代表高度為 n 的 binary tree
最多的 leaf 數。因此等於要證明 l(n)  2n.
Basis: l(0) = 1 ≤ 20 ,故成立。
Inductive assumption: l(i) ≤ 2i, for i = 0, 1,
…, n
Inductive step: 高度為 n+1 的 binary tree
之 leaf 數最多就是 l(n) 的兩倍,因此
l(n+1) = 2 l(n) ≤ 2×2n = 2n+1 。
第一章
19
由數學歸納法得證。
關於 proof techniques
n
nn1
Example 1.6 證明 S n=∑ i=
2
i=0
Basis: S0 = 0 = 0(0+1)/2
因為 Sn+1 = Sn + n + 1, 所以
Sn+1 = n(n+1)/2 + n + 1
n
= (n+1)(n+2)/2 。
nn1
S
=
i=
∑
n
故由數學歸納法得證
2
i=0
第一章
20
關於 proof techniques
數學歸納法與遞迴的關係
在作遞迴函式時,求 f(n+1) 通常必須由 f(n),
f(n-1), …, f(1) 來求得,這與數學歸納法中的
inductive step 是相同的。
而遞迴函式中,當執行到 n  k 時,就不再作
遞迴呼叫,這也與歸納法中的 basis 相同。
因此用數學歸納法證明出來的方式、都很容易直
接轉換為遞迴函式的寫法。
有些連結網路上的問題也是如此,有遞迴結構圖
形的問題,通常都適合用數學歸納法來證明。
第一章
21
關於 proof techniques
Proof of contradiction 反證法,若我們要證明某
個 statement P 成立,則先假設相反的結果、
就是假設 P 不成立,然後看由這個假設是否能
推導出明顯矛盾的結果。
只要我們的每個推理都是正確的,那就必然導致
這個假設是錯的,因此 P 就必然成立。
第一章
22
關於 proof techniques
Example 1.7 A rational number( 有理數 ) is a
number that can be expressed as the ration of two
integers n and m so that n and m have no common
factor. 否則就是 irrational 。請證明
為
2
irrational 。
Proof: 假設 2 為有理數,令
= n/m ,且 gcd(m,n) =
2
1。 n
n2
2
2
2=

2=

2m
=n
 m
2
m
所以 n 一定是 2 的倍數,令 n = 2k 則
2m2 = n2 Þ 2m2 = 4k2 Þ m2 = 2k2
 2m 也是 2 的倍數, gcd (m, n) ≥ 2 ,產生矛盾。
因此、
因此
第一章
必然為 irrational 。
23
1.2 Three Basic Concepts
這小節將介紹這門課中的三個主要主題的意義:
languages, grammars 與 automata
什麼叫作語言 language ?似乎不容易精確定
義
字典的定義是: a system suitable for the
expression of certain ideas, facts, or
concepts, including a set of symbols and
rules for their manipulation.
這樣的定義不夠 formal ,下面我們要介紹本課
程對 language 的定義。
第一章
24
關於 languages
令 Σ 代表一個 finite, nonempty symbols 的
集合,稱為 alphabet 。
由 Σ 中某些字母連接組成的 sequence 稱為
strings
例如 Σ={a, b}, abab 與 aabbba 是 Σ 中
的 strings 。通常我們用 a, b, c, … 等符號代
表字母、而 u, v, w, … 等符號代表字串。
concatenation:
w = a1a2…an v = b1b2…bm
wv = a1a2…anb1b2…bm
第一章
25
關於 languages
reverse, wR
w = a1a2…an → wR = an…a2a1
length of w, |w|, 字串 w 中的字母個數
empty string 空字串 λ
| λ| = 0
λw = w λ= w
substring
w = vu, u 與 v 都是 w 的 substrings ,
而且 v 是 w 的 prefix , u 是 suffix ,
若 w = abbab ,其 prefix 與 suffix 各有
哪些呢?
第一章
26
關於 languages
|uv| = |u| + |v|, 其中 u, v 都是 strings.
Example 1.8 證明上面的式子。
用數學歸納法證明, a 是字母, |a| = 1 ,而
w 是字串, |wa| = |w| + |a| = |w| + 1 。
假設 v 長度為 n 時 |uv| = |u| + |v| ,當
v 的長度為 n + 1 時,令 v = wa ,則
|uv| = |uwa| = |uw| + 1 = |u| + |w| + 1
= |u| + |v| 。
由數學歸納法得證 |uv| = |u| + |v|
第一章
27
關於 languages
令 w 為一字串 , wn 就是將此字串重複 n 次
的字串
w0 = 
Σ 是 alphabet , Σ* 就是 Σ 中字母所組成之
任意長度的字串所形成的集合。
λ Σ*
Σ+ = Σ* - {}
Σ* 與 Σ+ 都是無窮集合,因為它們的長度沒有
上限。
一個 language 是 Σ* 的子集合,語言 L 中的一
個字串稱為 sentence 。 ( 不考慮意義的問題 )
第一章
28
Example 1.9
Σ={a,b} 則 Σ* = {  ,a, b, aa, ab, ba,
bb, aaa, aab, …}
{a, aa, aab} 就是一個 Σ 的語言, finite
language
L = {anbn: n  0 } 也是一個 Σ 的語
言, aabb, aaaabbbb 都屬於 L ,但 abb
不屬於 L , infinite language 。
大部份有趣的語言都是 infinite
第一章
29
關於 languages
既然語言是集合,所以也可以有 complement,
intersection, difference, union 等集
*
合運算。 L= − L
reverse of L, LR = {wR : w  L}
concatenation of L1 and L2,
L1L2 = { xy : x  L1, y  L2}
Ln 代表 L 自己 concatenate n 次
L0 = {  }, L1 = L
第一章
30
關於 languages
star-closure, L* = L0 ∪ L1 ∪ L2 ∪ …
positive-closure, L+ = L1 ∪ L2 ∪ …
Example 1.10
L = {anbn: n  0 },
L2 = {anbnambm: n  0, m  0 }
LR = {bnan: n  0 }
L*, L+, complement of L 等就不太容易描述
。
第一章
31
關於 Grammars
我們對每天所使用的 language 的定義常常很難描述得
清楚、精確;這節中我們用 grammar 來描述
language 。
那什麼叫作 grammar 呢?英文的 grammar 告訴我
們什麼句子是 well-formed( 合文法的 ) ,例如:
A sentence can consist of a noun phrase
followed by a predicate( 陳述 ).
<sentence> → <noun_phrase><predicate>
<noun_phrase> → <article><noun>
a boy runs
the dog walks
<predicate> → <verb>
the boy walks
<article> → “a” | “the”
<noun> → “boy” | “dog”
<verb> → “runs” | “walks”
第一章
32
Definition 1.1
A grammar G is defined as a quadruple
G = (V, T, S, P),
where V is a finite set of objects called variables,
T is a finite set of objects called terminal symbols,
T 與 alphabet Σ 相似
S  V is a special symbol called the start variable,
P is a finite set of productions(rule).
其中的 V 與 T 都是 nonempty 且 disjoint
第一章
33
關於 Grammars
前面
grammar
的定義中,最重要的是
production rule 的部份。
最一般化的 rule 是
x→y
其中 x 屬於 (V ∪ T)+ 而 y 屬於 (V ∪ T)*
若 w = uxv, 而 z = uyv ,則 w Þ z
重複應用 rules , w1 Þ w2 Þ … Þ wn
*
*
可寫成 w1  w n , w  w
按不同順序來應用 rules 可以產生不同的 strings
一 grammar 所能產生所有的 strings 所成的集
合就是這個 grammar 所定義的語言。
第一章
34
Definition 1.2
Let G = (V, T, S, P) be a grammar. Then the
set
*
*
LG={w ∈T : S  w }
is the language generated by G.
• 如果 w  L(G), 則一定存在下列的 sequence
S Þ w1 Þ w2 Þ … Þ wn Þ w
這樣的 sequence 是 w 的一個 derivation 。
其中 S, w1, w2, …, wn 這些字串都稱為這個
derivation 的 sentential forms
第一章
35
Example 1.11
grammar G = ({S}, {a, b}, S, P) 的 production 是
S → aSb
S→
這個 grammar 會產生什麼樣的 language?
先看幾個例子
是否它所產生的語言就是
{anbn: n  0}?
SÞ
S Þ aSb Þ ab
S Þ aSb Þ aaSbb Þ aabb
第一章
36
Example 1.11
要證明 G 所產生的語言都屬於 {anbn: n  0}
,可以用數學歸納法證明。證明所有的
sentential form 都是 aiSbi 的型式。
另一方面,也必須證明所有 anbn 型式的字串都可以
由 G 來 generate 。就應用 n 次 S→aSb 的 rule 再加
上一次 S →  即可。
第一章
37
Example 1.12
Find a grammar that generates L = {anbn+1: n 
0}
S → Ab
A → aAb
A →λ
S → aSb
S →b
要如何證明 grammar G 可以產生 L 呢?必須
證明兩件事
L 中的每個字串都必須可以由 G 中的 S 產生
G 產生的每個字串都是 L 中的字串。
第一章
38
Example 1.13
令 Σ={a, b}, na(w) 與 nb(w) 代表字串中
a 與 b 出現的次數。下列 grammar 會產生什
麼 language 呢 ?
L = {w: na(w) = nb(w)}
S → SS
S→λ
證明這個 grammar 產生的字串都屬於 L
S → aSb 比較簡單,因為在 rule 中出現一個 a 就
S → bSa 同時出現一個 b 。
要如何證明每個 L 的字串都可以由這個
grammar 產生呢?
第一章
39
Example 1.13
證明 L = {w: na(w) = nb(w)} 中的每個字
串都可以由這個 grammar 產生,分幾個 cases
討論。字串格式分別如下:
 a??…??b: 應用一次 S→aSb, 由歸納法得證
 b??…??a: 應用一次 S→bSa, 由歸納法得證
 a??…??a: 須應用 S→SS ,找到中斷點
 b??…??b: 須應用 S→SS ,找到中斷點
第一章
40
關於 grammar
兩個 grammars 若產生的 language 完全相同,則這
兩個 grammars 是 equivalence 。
Example 1.14 G1 = ({A, S}, {a, b}, S, P),P
如下
S → aSb
S→aAb | 
S
→

A→aAb | 
同樣產生 L(G1) = {anbn: n  0 }
第一章
41
關於 Automata
An automaton is an abstract model of a digital
computer. 有:輸入 (input file 、不能改變、從
左到右 ) ,輸出,暫時的
storage , internal
states( 有限個 ) , control unit( 在各個 internal
states 間轉換的控制單元 )
automaton 隨時都在某個 internal state 的狀態
下,下個時間的 state 由 next-state 或
transition function 決定。而 transition function
則通常是透過目前的 state 、 input 資料與
storage 中的資料來決定下個 state 。
在 state 的轉換過程可能會產生 output 、或改
變 storage 的內容。
第一章
42
關於 Automata
用 configuration 代表某個 state 、 input file
與 temporary storage ; automaton 從一個
configuration 轉換到下一個 configuration 的過
程稱為 move 。
Input file
Control unit
Storage
Output
第一章
43
關於 Automata
前面所說 automata 的型式是一般化的型式,
我們會先介紹一些特別的 automata ,並討論不
同 automata 之間功能的差異。
可以分成
deterministic
automata
與
nondeterministic automata 兩大類。
Deterministic automata 主要特點是由目前的
state 、 input 、 storage 只能產生一種 next
state 。而 non-deterministic automata 則可能
有許多種 next state 可以自由選擇。
Accepter: output 只有 yes 或 no 的
automata 。
Transducer: 可產生其他字串的 automata 。
第一章
44
1.3 Some Applications
這一節中我們會提到幾個應用的例子。
Formal language 、 grammar 與程式語言有很
密切的關係;特別是要精確描述程式語言的定義
時,像 compiler 要檢查哪些 statements 不符
合程式語言的語法時,使用 grammar 來描述是
很適當的。
下面的例子是類似 C 或 Pascal 的變數宣告規
則。
第一章
45
Example 1.15
<id>→<letter><rest>
<rest>→<letter><rest>|<digit><rest>|λ
<letter>→a|b|…|z
<digit>→0|1|…|9
<id> Þ <letter><rest>
Þ a<rest>
Þ a<digit><rest>
Þ a0<rest>
Þ a0
第一章
46
Example 1.16
可以用 automata 來表示前面的 grammar ;
Automata 可以用 graph 的方式來表示;
1
Letter
Digit
2
Letter or digit
3
Letter or digit
第一章
47
Example 1.17
將兩個二進位整數相加,得到一個相加的結果。
n
i
v

x=
a
2
∑ i
x = a0a1…an
i=0
一般二進位數倒過來的表示方式。每次的
input 是 ai 與 bi , 由左至右。
(0,0)/0
(1,0)/1
(0,1)/1 (1,1)/0
No
carry
(0,1)/0
(1,0)/0
(1,1)/1
Carry
(0,0)/1
第一章
48
Exercise 1.3-11
Design a transducer to convert a binary string
into octal. For example, the bit string
001101110 should produce the output 156.
0 /0
0 /λ
0 0
0
0 /λ
1 /λ
λ
1 /λ
0 /λ
0 1
1 0
1
1 /λ
1 /7
第一章
1 1
1 /1
0 /2
1 /3
0 /4
1 /5
0 /6
49