最終目標是介紹 Complexity,但會先從 Automata、Computability 開始。
這裡未指名時通常是無向。
很重要
是一個 finite automaton,其中:
運作方式:自動機從初始狀態 開始,依序讀取輸入字串中的每個符號。根據轉移函數 ,從當前狀態和讀取的符號決定下一個狀態。讀取完所有符號後,若最終狀態屬於接受狀態集合 ,則該字串被接受;否則,該字串被拒絕。
機器 M 所辨識的語言 (language of machine M): 所有被 M 接受的字串所組成的集合 A,寫成 ,稱為 M recognizes A (accepts 也行,但是通常用在 string)。
Let
M accepts w there exists a sequence of states such that:
M recognizes language A .
A Regular Language 是可以被某個 finite automaton 所接受的 language。
Let A and B be languages.
Theorem: The set of regular languages is closed under all three of the regular operations.
Transition function 由 改成 ,其中 是 的 Power set,。也就是可能轉移到輸出的子集中的任一個 state,甚至輸入的 symbol 可以是 。
其實也能在使輸入維持 ,只要把轉移後再經過若干 轉移能到的狀態都新增到輸出中就行了。
轉移條件改為
Nondeterminism is a generalization of determinism: Every DFA (deterministic finite automaton) is automatically an NFA (nondeterministic finite automaton).
NFA 與 DFA 是等價的,也就是對於任意 NFA,都可以找到與其 recognizes 相同 language 的 DFA。對於有 個狀態的 NFA ,對應的 DFA 的定義如下:
因此,以後並不太需要在意 NFA、DFA 的差別。
Corollary: A language is regular an NFA recognize it.
:Regular 有 DFA recognize,而 DFA 是 NFA 的特例。
:NFA recognize 某 language,則對應的 DFA 也 recognize 相同 language。
Let
For each operation, construct a NFA that recognizes the resulting languages, :
另外,事實上對於一些其他 operation 也是 closed,比如:
Symbol 或 Alphabet 經過 Regular Operations 即為 Regular Expression。每個 Expression 只是一個記號,代表一個 set of strings 也就是 language,說成 "The expression describes the language"。
Let be an alphabet. A regular expression over is defined as follows (以下 符號代表 expression 描述的 language):
若沒有括號,順序是 Kleene Star > Concatenation > Union,分別可以類比為次方、乘法、加法。
只挑稍微難理解的
注意正規表達式本身是 notation,不直接等於它描述的 language。因此,應寫 (表示這兩個正規表達式描述相同 language),或 (表示 所描述的 language 是只有 epsilon 的集合)。
A language is regular A regular expression describes it.
: Proof by Construction. Given a regular expression , we can construct a NFA that recognizes the language . (若很明顯會省略)
: Proof by Construction. Convert a DFA to a regular expression. First to a special form of generalized nondeterministic finite automaton (GNFA) then to a regular expression.
GNFA: NFA but can have regular expressions as the input to the transition function.
Use a special form of GNFA:
(可以把非 special form 的 GNFA 轉換成 special form 的 GNFA)
Definition of GNFA (Special Form): where:
Computation 非常簡單,略過。
is a regular language There exists a pumping length such that for any string with , we can write such that:
The converse is not true!!! It can only be used to prove a language is nonregular, but some nonregular languages may satisfy the property. To prove a language is regular, the typical way is to construct an FA or a regular expression.
Let
圖示:
Since , repeating any number of times will not change the state, so .
Also, because , and because .
P (A is a regular language) Q (The Pumping Lemma Condition):
If a language is regular, then there exists a pumping length such that for every string with , we can write satisfying the three conditions:
Contrapositive Logic for Non-regularity ():
The negation of the Pumping Lemma Condition (): For every , there exists a string with such that cannot be written as satisfying the three conditions.
Assume is regular. Let be the pumping length.
Select . This and .
By the Pumping Lemma, must satisfy the three conditions.
Since , and can only contain '0's. So for some .
Consider . This string will have '0's and '1's.
Since , the number of '0's () is not equal to the number of '1's ().
Therefore, . This contradicts the Pumping Lemma. Thus, is nonregular.
For , we can use a similar argument.
(Note: While can prove this using the closure property of regular languages under set difference, here's the direct Pumping Lemma proof.)
Assume is regular. Let be the pumping length.
Select . This (since ) and .
By the Pumping Lemma, must satisfy the three conditions.
Since , and can only contain '0's. Let for some .
Consider . The number of '0's will be , while the number of '1's remains .
Choose . (Since , divides , so is an integer .)
For this , .
In this string, the number of '0's equals the number of '1's.
Therefore, (as ). This contradicts the Pumping Lemma. Thus, is nonregular.
Assume is regular. Let be the pumping length.
Select . This and .
By the Pumping Lemma, must satisfy the three conditions.
Since , .
Since , .
Since is not a perfect square, . This contradicts the Pumping Lemma. Thus, is nonregular.
主要由許多規則組成,把 symbol 分為 variable (包含 start variable) 與 terminal,規則的形式為 ,其中 是 variable, 是 terminal 或 variable 的串接。
衍生或生成 (derivation, generation) 一個字串的過程是:
所有某 grammar 能生成的 strings 的集合稱為 language of the grammar,一樣是 。
能被某 context-free grammar 生成的 language 稱為 context-free language。
若同個 variable 有多個規則,可以以 | 串接,比如
是一個 context-free grammar,其中:
以下大寫代表 variable,小寫代表 terminal。
yield 指替換一步,比如 以 替換為 , yields ,記為 。
derivation 指替換任意步,記為 ,表示 經過任意次替換後變成 。
因此 language of a grammar 寫為 。
其中第二個例子解釋先乘後加的規則可以由生成規則定義,由生成結構判斷順序:比如 與 都只有一種生成方式,所以很明顯要哪個先。
每個 variable 與 terminal 作為節點,以 start variable 為根,每個 variable yield 的 variable 或 terminal 為其子節點,terminal 是 leaf,通常直接畫在最下面一排。
比如上方的「對 a 的加與乘運算」:
例子的第二個使加號由 生成,而乘號以 生成。 因為加法外無括號,必然由 start variable 生成,而不是 先生成乘號再生成加號。
如果同個 string 能以某 grammar 透過多種不同方式 derive,則 the string is derived ambiguously in the grammar。
若對於某 grammar,存在一個 string which is derived ambiguously,則 the grammar is ambiguous。
注意若只是順序不同,不視為不同方式。可以透過一律由 leftmost derivation (每步都替換最左邊的 variable) 來避免替換順序造成的歧義。
有些 CFL 能由 CFG 生成,但必須由 ambiguous CFG 生成,比如 ,稱為 inherently ambiguous。
特殊形式,加上限制後可更簡化,只包含三類規則:
Start variable 不出現在任意規則的右側。
所有 CFG 都可以被轉換為 Chomsky Normal Form。以下 是 variable 或 terminal 的字串。
FA 可視為 State Control 保有狀態,有一個 arrow 指向 input tape,並且只能往右移動。
PDA 作為 FA 的修改版,再加上一個 stack 作為額外的記憶體。
Deterministic PDA 與 Nondeterministic PDA 的能力不等價,但兩者都能 recognize 所有 Context-Free Language,所以以下 PDA 指的都是 Nondeterministic PDA,稱 Deterministic PDA 為 DPDA。
註:設 stack 的頂端在左側。
PDA: where
Computation 課本的定義稍微怪怪的,因此我自己寫。
Let:
accepts there exists a sequence of instantaneous descriptions (IDs) such that:
在圖示時,可能會在邊上標記 ,代表讀取 ,stack 頂端要是 ,移除 並將 放到 stack 頂端。
使 能 push string,由右而左放入 stack,方便表示 transition。對於簡寫 ,新增 state ,以及 transition:
或是 變成:。
實際上維基上的定義及其他課本會使 transition 能直接 push 一個字串進 stack,能用 shorthand 所以是等價的。
因為狀態是有限的,所以自然能 push 的 string 必須是有限的。
A language is context-free some PDA recognizes it.
: Proof by Construction. Given a context-free grammar , we can construct a PDA that recognizes the language .
: Proof by Construction. Given a PDA , we can construct a context-free grammar that generates the language .
首先介紹 Special Form of PDA,以及經過一些看起來很蠢的轉換後把任何 PDA 轉換成這個形式:
對於 的每對 states ,Let ,想要使 derives x 能使機器從 (, empty stack) 走到 (, empty stack)。
這些字串 也能使機器從 (, ) 走到 (, )。
Construction for :
任意 不經任何轉移可產生 ,即 。
任意 產生的非空字串 在 special form of PDA 上第一步必然是 push,且最後一步是 pop,以維持空 stack,可以分成 push 與 pop 的字元是否相同的兩種情況:
兩個方向的證明都是很 trivial 的 induction。
Regular languages 是 context-free languages 的子集,因為任何 FA 都自動是 PDA,只要完全不使用 stack 即可。
Some Non-Context-Free Languages: .
括號內是 Pumping Lemma for RL
is a context-free language There exists a pumping length such that every string with can be written as (), satisfying:
證明:
設 為一個 CFG,其生成語言為 CFL 。令 為 中所有規則右邊部分的 symbol 數的最大值。若 ,則 為 alphabet 的子集,對於生成的 ,令 且 就滿足條件。以下假設 。
在 parse tree 中,是已經 Nondeterministically 從眾多替換選項中選出一個,因此所有節點都至多有 個子節點。對於一個高度 (深度定義改為由 start variable 走幾步,相當於邊的數量、一般定義的高度減一),至多有 個葉節點,也就是最終的 terminal 數量、字串長度。換句話說,至少 長的字串必須來自高度至少 的 parse tree。
設 ,對於某字串 ,其任意 parse tree 的高度至少 ,取其中節點數最少的。
在此 parse tree 中,至少有個葉節點,其從根節點到葉節點的路徑長至少為 。取此路徑上最底下的 個節點,也就是 個 variable 與最後的 terminal。在 variables 中,根據鴿籠原理,必然有某個 variable 重複出現,稱上方的為 ,下方的為 。
使 的子樹葉節點為 , 的子樹葉節點為 , 的剩餘部分給 。以下分別檢測三個條件:
For every , select .