Formal Languages and Automata http mail im tku

  • Slides: 24
Download presentation
形式語言 Formal Languages and Automata 淡江大學 資訊管理系 侯 永 昌 http: //mail. im. tku.

形式語言 Formal Languages and Automata 淡江大學 資訊管理系 侯 永 昌 http: //mail. im. tku. edu. tw/~ychou ftp: //mail. im. tku. edu. tw/Prof_Hou

教科書:An Introduction to Formal Languages and Automata, 5 th Edition, by Peter Linz, Jones

教科書:An Introduction to Formal Languages and Automata, 5 th Edition, by Peter Linz, Jones & Bartlett Learning 出版,開發圖書總經銷 淡江大學資訊管理系侯永昌 1

第二章 有限自動機 (Finite Automata)

第二章 有限自動機 (Finite Automata)

Deterministic Finite Accepter § A deterministic finite accepter (DFA) M = (Q, , ,

Deterministic Finite Accepter § A deterministic finite accepter (DFA) M = (Q, , , q 0, F), where l l l Q is a finite set of internal states is a finite set of symbols called input alphabet : Q × Q is a total function called the transition function q 0 Q is the initial state F Q is a set of finite states 淡江大學資訊管理系侯永昌 3

Deterministic Finite Accepter § Extended transition function *(qi, w) contains qj if and only

Deterministic Finite Accepter § Extended transition function *(qi, w) contains qj if and only if there is a walk in the transition graph from qi to qj label w, where all qi, qj Q, and w * § L(M) = {w * : *(q 0, w) F} 淡江大學資訊管理系侯永昌 4

Deterministic Finite Accepter § 例:M = ({q 0, q 1, q 2}, {0, 1},

Deterministic Finite Accepter § 例:M = ({q 0, q 1, q 2}, {0, 1}, , q 0, {q 1}), is given by l l (q 0, 0) = q 0, (q 1, 0) = q 0, (q 2, 0) = q 2, (q 0, 1) = q 1, (q 1, 1) = q 2, (q 2, 1) = q 1 § L(M) = {101, 0111, 11001, …} L(M) = {00, 1100, …} 雙圈代表final state 淡江大學資訊管理系侯永昌 5

Deterministic Finite Accepter § 例:M = ({q 0, q 1, q 2}, {a, b},

Deterministic Finite Accepter § 例:M = ({q 0, q 1, q 2}, {a, b}, , q 0, {q 1}), is given by l l (q 0, a) = q 0, (q 1, a) = q 2, (q 2, a) = q 2, (q 0, b) = q 1, (q 1, b) = q 2, (q 2, b) = q 2 L = {anb : n 0} q 0 q 1 q 2 a q 0 q 2 b q 1 q 2 Initial state final state Trap state 淡江大學資訊管理系侯永昌 6

Deterministic Finite Accepter § 例:M = ({q 0, q 1, q 2, q 3},

Deterministic Finite Accepter § 例:M = ({q 0, q 1, q 2, q 3}, {a, b}, , q 0, {q 2}), L(M) = {strings with the prefix ab} final state a Initial state b q 0 q 1 q 3 q 2 Trap state q 2 q 2 q 3 q 3 淡江大學資訊管理系侯永昌 7

Deterministic Finite Accepter § 例:M = ({ , 0, 001}, {0, 1}, , ,

Deterministic Finite Accepter § 例:M = ({ , 0, 001}, {0, 1}, , , { , 0, 00}), L(M) = {strings without containing the substring 001} Initial state final state Trap state 淡江大學資訊管理系侯永昌 8

Regular Languages § A language L is called regular if and only if there

Regular Languages § A language L is called regular if and only if there exists some deterministic finite accepter (DFA) M such that L = L(M) § 如果一個語言可以對應到一個 DFA,我們就 稱那個語言是有規律(regular)的語言 § 前幾頁的圖都是DFA,因此所對應的語言都 是regular language 淡江大學資訊管理系侯永昌 9

Regular Languages § L = {awa : w {a, b}*} 是否為regular language? 淡江大學資訊管理系侯永昌 10

Regular Languages § L = {awa : w {a, b}*} 是否為regular language? 淡江大學資訊管理系侯永昌 10

Regular Languages § L = {awa : w {a, b}*}, L 2 = {aw

Regular Languages § L = {awa : w {a, b}*}, L 2 = {aw 1 aaw 2 a : w 1, w 2 {a, b}*} 是否為regular language? L 3 = ? 淡江大學資訊管理系侯永昌 11

Nondeterministic Finite Accepter § A nondeterministic finite accepter (NFA) M = (Q, , ,

Nondeterministic Finite Accepter § A nondeterministic finite accepter (NFA) M = (Q, , , q 0, F), where l l l Q is a finite set of internal states is a finite set of symbols called input alphabet : Q × ( { }) 2 Q is a total function called the transition function q 0 Q is the initial state F Q is a set of finite states § 注意: 2 Q 代表 Q 的 powerset 淡江大學資訊管理系侯永昌 12

Nondeterministic Finite Accepter § 例: § (q 0, a) = {q 1, q 4}

Nondeterministic Finite Accepter § 例: § (q 0, a) = {q 1, q 4} nondeterministic § L(M) = {a 3} {a 2 n : n 1} 淡江大學資訊管理系侯永昌 13

Nondeterministic Finite Accepter § 例: Trap state § -transition nondeterministic § (q 0, 0)

Nondeterministic Finite Accepter § 例: Trap state § -transition nondeterministic § (q 0, 0) = ? , (q 2, 1) = ? § L(M) = { , 1010, …} = {(10)n : n 0} L(M) = {0, 110, 10100, …} 淡江大學資訊管理系侯永昌 14

Nondeterministic Finite Accepter § 例: § § -transition and undefined transitions *(q 1, a)

Nondeterministic Finite Accepter § 例: § § -transition and undefined transitions *(q 1, a) = {q 1, q 2, q 0} *(q 2, ) = {q 2, q 0} *(q 2, aa) = {q 1, q 2, q 0} 淡江大學資訊管理系侯永昌 15

Why Nondeterminism? § A nondeterministic algorithm that can make the best choice would be

Why Nondeterminism? § A nondeterministic algorithm that can make the best choice would be able to solve the problem without backtracking § Nondeterminism is sometimes helpful in solving problems easily § Nondeterminism is an effective mechanism for describing some complicated language concisely § Certain theoretical results are more easily established for nfa’s than dfa’s. § There is no essential difference between these two types of automata 淡江大學資訊管理系侯永昌 16

Equivalence of NFA and DFA § Two finite accepter, M 1 and M 2,

Equivalence of NFA and DFA § Two finite accepter, M 1 and M 2, are said to be equivalence if L(M 1) = L(M 2), that is, if they accept the same language § L(M) = {(10)n : n 0} NFA DFA 淡江大學資訊管理系侯永昌 17

Equivalence of NFA and DFA § 例:將下圖之nfa變成dfa NFA 淡江大學資訊管理系侯永昌 21

Equivalence of NFA and DFA § 例:將下圖之nfa變成dfa NFA 淡江大學資訊管理系侯永昌 21

Equivalence of NFA and DFA § 解: N(q 0, 0) = {q 0, q

Equivalence of NFA and DFA § 解: N(q 0, 0) = {q 0, q 1}, N(q 0, 1) = {q 1} N(q 0, 0) N(q 1, 0) = {q 0, q 1, q 2} N(q 0, 1) N(q 1, 1) N(q 2, 1) = {q 1, q 2} 淡江大學資訊管理系侯永昌 22

Equivalence of NFA and DFA § 解: N(q 0, 0) N(q 1, 0) N(q

Equivalence of NFA and DFA § 解: N(q 0, 0) N(q 1, 0) N(q 2, 0) = {q 0, q 1, q 2} N(q 0, 1) N(q 1, 1) = {q 1, q 2} N(q 1, 0) N(q 2, 0) = {q 2} N(q 1, 1) N(q 2, 1) ={q 2} N(q 1, 0) = {q 2} = N(q 1, 1) N(q 2, 0) = N(q 2, 1) = {q 2} N( , 0) = = N( , 1) DFA 淡江大學資訊管理系侯永昌 23