Formal Languages and Automata http mail im tku

  • Slides: 38
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

第九章 圖林機 (Turing Machines)

第九章 圖林機 (Turing Machines)

Turing Machines § Regular language finite automata (no storage) § Context-free language pushdown automata

Turing Machines § Regular language finite automata (no storage) § Context-free language pushdown automata (with stack) § Simple languages, such as {anbncn} and {ww}, are not context-free § What would happen if we have more flexible storage? Will we get more powerful language families? 淡江大學資訊管理系侯永昌 3

Turing Machines § A Turing machine M is defined by M = (Q, ,

Turing Machines § A Turing machine M is defined by M = (Q, , q 0, �, F) where l l l l Q is the set of internal states is the input alphabet is a finite set of symbols called the tape alphabet : Q × × {L, R} is the transition function � is a special symbol called the blank q 0 Q is the initial state F Q is a set of finite states 淡江大學資訊管理系侯永昌 4

Turing Machines § Turing machine starts in the given initial state with some information

Turing Machines § Turing machine starts in the given initial state with some information on the tape. It then goes through a sequence of steps controlled by the transition function 可以左右移動 淡江大學資訊管理系侯永昌 5

Turing Machines § Turing machine will halt whenever it enters a final state §

Turing Machines § Turing machine will halt whenever it enters a final state § Turing machine is said to halt whenever it reaches a configuration for which is not defined § 例:下圖顯示轉換前和轉換後之情況 (q 0, a) = (q 1, d, R) 淡江大學資訊管理系侯永昌 7

Turing Machines § A move from one configuration to another will be denoted by├,

Turing Machines § A move from one configuration to another will be denoted by├, Thus if (q 1, c) = (q 2, e, R), then the move abq 1 cd├ abeq 2 d is made whenever the internal state is q 1, the tape contains abcd, and the read-write head is on the c * § The symbol├ has the usual meaning of an arbitrary number of moves § The symbol├M are used in arguments to distinguish between several machines 淡江大學資訊管理系侯永昌 12

Turing Machines § Let M = (Q, , q 0, �, F) be a

Turing Machines § Let M = (Q, , q 0, �, F) be a Turing machine. Then any string a 1…ak-1 qaka k+1…an, with ai and q 1 Q, is an instantaneous description of M. § A move a 1…ak-1 q 1 aka k+1…an├ a 1…ak-1 bq 2 a k+1 …an is possible if and only if (q 1, ak) = (q 2, b, R) § A move a 1…ak-1 q 1 aka k+1…an├ a 1…q 2 ak-1 ba k+1 …an is possible if and only if (q 1, ak) = (q 2, b, L) 淡江大學資訊管理系侯永昌 14

Turing Machines § M is said to be halt staring from some initial configuration

Turing Machines § M is said to be halt staring from some initial configuration x 1 qix 2 if x 1 qix 2├* y 1 qjay 2 for any qj and a, for which (qj, a) is undefined. § The sequence of configuration leading to a halt will be called a computation § x 1 qx 2├* indicates that the machine goes into a loop and never halts 淡江大學資訊管理系侯永昌 15

Turing Machines as Language Accepters § Let M = (Q, , q 0, �,

Turing Machines as Language Accepters § Let M = (Q, , q 0, �, F) be a Turing machine. Then the language accepted by M is L(M) = {w * + : q 0 w├ x 1 qf x 2 for some qf F, x 1, x 2 *} § 圖林機所接受的語言不包含空字串 淡江大學資訊管理系侯永昌 16

Turing Machines as Language Accepters § 例:For = {0, 1}, design a Turing machine

Turing Machines as Language Accepters § 例:For = {0, 1}, design a Turing machine that accepts the language denoted by the regular expression 00* § 解:Q = {q 0, q 1}, F = {q 1}, (q 0, 0) = (q 0, 0, R), (q 0, �) = (q 1, �, R) § 雖然空字串也會將圖林機帶進終止狀態,但依 照 page 17 的定義,圖林機所接受的語言不包 含空字串 淡江大學資訊管理系侯永昌 17

Turing Machines as Language Accepters § § § § § Q = {q 0,

Turing Machines as Language Accepters § § § § § Q = {q 0, q 1, q 2, q 3, q 4}, = {a, b, x, y, �}, F = {q 4} (q 0, a) = (q 1, x, R), (q 1, a) = (q 1, a, R) (q 1, y) = (q 1, y, R), (q 1, b) = (q 2, y, L) (q 2, y) = (q 2, y, L), (q 2, a) = (q 2, a, L) (q 2, x) = (q 0, x, R) * q 0 aa…abb…b├ xq 0 a…ayb…b q 0 aa…abb…b├* xxq 0…ayy…b (q 0, y) = (q 3, y, R), (q 3, y) = (q 3, y, R) (q 3, �) = (q 4, �, R) 淡江大學資訊管理系侯永昌 19

Turing Machines as Language Accepters § q 0 aabb├ xq 1 abb├ xaq 1

Turing Machines as Language Accepters § q 0 aabb├ xq 1 abb├ xaq 1 bb├ xq 2 ayb├ q 2 xayb ├ xq 0 ayb├ xxq 1 yb├ xxyq 1 b├ xxq 2 yy├ xq 2 xyy├ xxq 0 yy├ xxyq 3 by├ xxyyq 3� ├ xxyy�q 4� § 例:For = {a, b}, design a Turing machine that accepts L = {anbncn : n 1} § 構思過程:與上個例子相同的做法,只是更複 雜一點,將 a, b, c 改變成 x, y, z 而已 淡江大學資訊管理系侯永昌 20

Turing Machines as Transducers § anbncn 不是 context-free 語言,因此不能被 pushdown automata 所接受,但是 Turing machine

Turing Machines as Transducers § anbncn 不是 context-free 語言,因此不能被 pushdown automata 所接受,但是 Turing machine 可以接受 anbncn,表示 Turing machine 的能力比所 pushdown automata 更強 § Turing machines are not only interesting as language accepters, they also provide us with a simple abstract model for digital computers in general § Since the primary purpose of a computer is to transform input to output, it acts as a transducer 淡江大學資訊管理系侯永昌 21

Turing Machines as Transducers § We can view a Turing machine transducer M as

Turing Machines as Transducers § We can view a Turing machine transducer M as an implementation of a function f is defined by w = f(w), provided that q 0 w├* qfw for some final state qf § A function f with domain D is said to be Turingcomputable or just computable if there exists some Turing machine M = (Q, , q 0, �, F) such that q 0 w├* qf f(w) for all w D 淡江大學資訊管理系侯永昌 22

Turing Machines as Transducers § 例:Given two positive integers x and y, design a

Turing Machines as Transducers § 例:Given two positive integers x and y, design a Turing machine that computes x + y § 解︰我們假設正整數的大小以它有幾個連續的 1來表示。w(x) {1}+,|w(x)| = x。希望設計 一個Turing machine執行下列的運算︰ q 0 w(x)0 w(y)├* qf w(x + y)0 淡江大學資訊管理系侯永昌 23

Turing Machines as Transducers § § § Q = {q 0, q 1, q

Turing Machines as Transducers § § § Q = {q 0, q 1, q 2, q 3, q 4}, (q 0, 1) = (q 0, 1, R), (q 1, 1) = (q 1, 1, R), (q 2, 1) = (q 3, 0, L), (q 3, �) = (q 4, �, R) F = {q 4}, (q 0, 0) = (q 1, 1, R) (q 1, �) = (q 2, �, L) (q 3, 1) = (q 3, 1, L) § q 0111011├ 1 q 011011├ 11 q 01011├ 111 q 0011 ├ 1111 q 111├ 11111 q 11├ 111111 q 1�├ 11111 q 21├ 1111 q 310├ q 3� 111110├ q 4111110 淡江大學資訊管理系侯永昌 24

Turing Machines as Transducers § q 0111├ xq 011├ xxq 01├ xxxq 0�├ xxq

Turing Machines as Transducers § q 0111├ xq 011├ xxq 01├ xxxq 0�├ xxq 1 x├ xx 1 q 2�├ xxq 111├ xq 1 x 11├ x 1 q 211├ x 11 q 21├ x 111 q 2�├ x 11 q 111├ x 1 q 1111├ xq 11111├ q 1 x 1111├ 1 q 21111├ 11 q 2111├ 111 q 211├ 1111 q 21├ 11111 q 2�├ 1111 q 111├ 111 q 1111├ 11 q 11111├ 1 q 111111├ q 1111111├ 淡江大學資訊管理系侯永昌 q 1� 111111├ q 3111111 26

Turing Machines as Transducers § 例:Construct a Turing machine that will halt in a

Turing Machines as Transducers § 例:Construct a Turing machine that will halt in a final state qy if x y, and that will halt in a nonfinal state qn if x < y. More precisely, the machine is to perform the computation q 0 w(x)0 w(y)├* qy w(x)0 w(y) if x y, q 0 w(x)0 w(y)├* qn w(x)0 w(y) if x < y § 解︰類似page 18 -20 的做法,只是此時我們不 是比較 a, b 的個數,而是比較 0 前後的 1。比 到最後的結果不外乎︰xx… 110 xx…x� 或 xx…x 0 xx…x 11�。前者,我們想往右找 1 時, 卻碰到空字元;後者左邊已經沒有 1 了,右 淡江大學資訊管理系侯永昌 27 邊卻還有1

Combining Turing Machines for Complicated Tasks § 例︰Design a Turing machine that computes the

Combining Turing Machines for Complicated Tasks § 例︰Design a Turing machine that computes the function f(x, y) = x + y if x y, =0 if x < y § 解: 淡江大學資訊管理系侯永昌 28

Combining Turing Machines for Complicated Tasks § 利用 page 27 的做法可以決定 x, y 哪一個比較

Combining Turing Machines for Complicated Tasks § 利用 page 27 的做法可以決定 x, y 哪一個比較 大,利用 page 23 -24 的做法可以求 x + y * § q. C, 0 w(x)0 w(y)├ q. A, 0 w(x)0 w(y) if x y * § q. C, 0 w(x)0 w(y)├ q. E, 0 w(x)0 w(y) if x < y * § q. A, 0 w(x)0 w(y)├ q. A, f w(x + y)0 * § q. E, 0 w(x)0 w(y)├ q. E, f 0 淡江大學資訊管理系侯永昌 29

Combining Turing Machines for Complicated Tasks § 例︰Consider the microinstruction “if a then qj

Combining Turing Machines for Complicated Tasks § 例︰Consider the microinstruction “if a then qj else qk” with the following interpretation. l If the Turing machine read an a, then regardless of its current state, it is to go into state qj without changing the tape content or moving the read-write head. l If the symbol read is not an a, the machine is to go into state qk without changing anything 淡江大學資訊管理系侯永昌 30

Combining Turing Machines for Complicated Tasks § (qi, a) = (qj 0, a, R)

Combining Turing Machines for Complicated Tasks § (qi, a) = (qj 0, a, R) for all qi Q § (qi, b) = (qk 0, a, R) for all qi Q and all b {a} § (qj 0, c) = (qj, c, L) for all c § (qk 0, c) = (qk, c, L) for all c 淡江大學資訊管理系侯永昌 31

Combining Turing Machines for Complicated Tasks § 例︰Design a Turing machine that multiplies two

Combining Turing Machines for Complicated Tasks § 例︰Design a Turing machine that multiplies two positive integers in unary notation § 解: 1、重複步驟 2 -3,直至 x 中沒有 1 為止 2、將 x 中的一個 1 改變為 a 3、將最左邊的 0 改變成 0 y 4、最後將所有的 a 都再改回為 1 淡江大學資訊管理系侯永昌 34

Turing Thesis § Turing machines can perform not only the simple operations, but also

Turing Thesis § Turing machines can perform not only the simple operations, but also more complex process as well, describable by block diagrams or pseudocode. § We might suspect that a Turing machine begins to approach a typical computer in power § Turing thesis, not prove yet, states that any computation that can be carried out by mechanical means can be performed by some Turing machinethe Turing machine model 淡江大學資訊管理系侯永昌 35

Turing Thesis § Observations: l Anything that can be done on any existing digital

Turing Thesis § Observations: l Anything that can be done on any existing digital computer can also be done by a Turing machine l No one has yet been able to suggest a problem, solvable by what we intuitively consider an algorithm, for which a Turing machine program cannot be written l Alternative models have been proposed for mechanical computation, but none of them is more powerful than the Turing machine model 淡江大學資訊管理系侯永昌 36

Turing Thesis § An algorithm for a function f : D R is a

Turing Thesis § An algorithm for a function f : D R is a Turing machine M, which given as input any d D on its tape, eventually halts with the correct answer f(d) R on its tape. Specifically, we can require that * q 0 d├ qf f(d), qf F, for all d D 淡江大學資訊管理系侯永昌 37