Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf ·...

36
Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

Transcript of Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf ·...

Page 1: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

Theory of Computation

Chapter 2

Guan-Shieng Huang

Feb. 24, 2003Feb. 19, 2006

0-0

Page 2: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Turing Machine

K⊲0111000a · · · 01bb ⊔ ⊔ ⊔ ⊔ · · · · · ·

1

Page 3: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Definition of TMs

A Turing Machine is a quadruple M = (K, Σ, δ, s), where

1. K is a finite set of states; (line numbers)

2. Σ is a finite set of symbols including ⊔ and ⊲; (alphabet)

3. δ : K × Σ→ (K ∪ {h,“yes”,“no”})× Σ× {←,→,−}, a transition

function; (instructions)

4. s ∈ K, the initial state. (starting point)

2

Page 4: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

• h: halt, “yes”:accept, “no”: reject

(terminate the execution)

• →: move right, ←: move left, −: stay

(move the head)

• ⊔: blank, ⊲: the boundary symbol

3

Page 5: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

• δ(q, σ) = (p, ρ, D)

While reading σ at line q, go to line p and write out ρ on the

tape. Move the head according to the direction of D.

• δ(q, ⊲) = (p, ρ,→), to avoid crash.

4

Page 6: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Example 2.1

5

Page 7: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Remark

x: input of M

M(x) =

“yes”

“no”

y if M entered h

ր if M never terminates

6

Page 8: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Example 2.2

(n)2 → (n + 1)2 if no overflow happens.

7

Page 9: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Example 2.3 — Palindrome

8

Page 10: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Turing Machines as Algorithms

• L ⊆ (Σ− {⊔, ⊲})∗, a language

• A TM M decides L if for all string x,

x ∈ L⇒M(x) = “yes”

x 6∈ L⇒M(x) = “no”.

• A TM M accepts L if for all string x,

x ∈ L⇒M(x) = “yes”

x 6∈ L⇒M(x) =ր .

9

Page 11: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

• If L is decided by some TM, we say L is recursive.

• If L is accepted by some TM, we say L is recursively

enumerable.

10

Page 12: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Proposition 2.1

If L is recursive, then it is recursively enumerable.

11

Page 13: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Representation of mathematical objects: (data structure)

1. graphs, sets, numbers, ...

2. All acceptable encodings are polynomially related.

(a) binary, ternary

(b) adjacency matrix, adjacency list

However, unary representation of numbers is an exception.

12

Page 14: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

k-string Turing Machines

A k-string Turing machine is a quadruple (K, Σ, δ, s) where

1. K, Σ, s are exactly as in ordinary Turing machines;

2. δ : K × Σk → (K ∪ {h,“yes”,“no”})× (Σ× {←,→,−})k;

13

Page 15: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

An Example

14

Page 16: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

1. If for a k-string Turing machine M and input x we have

(s, ⊲, x, ⊲, ǫ, . . . , ⊲, ǫ)Mt

−→ (H, w1, u1, . . . , wk, uk)

for some H ∈ {h,“yes”,“no”}, then the time required by M on

input x is t.

2. If for any input string x of length |x|, M terminates on input x

within time f(|x|), we say f(n) is a time bound for M .

(worst case analysis)

15

Page 17: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

TIME(f(n)): the set of all languages that can be decided by TMs

in time f(n).

Theorem 2.1

Given any k-string TM M operating within time f(n), we can

construct a TM M ′ operating within time O(f(n)2) and such

that, for any input x, M(x) = M ′(x).

(by simulation)

16

Page 18: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Linear Speedup

Theorem 2.2

Let L ∈ TIME(f(n)). Then, for any ǫ > 0, L ∈ TIME(f ′(n)),

where f ′(n) = ǫ · f(n) + n + 2.

Definition

P =⋃

k≥1 TIME(nk).

17

Page 19: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Space Bounds

A k-string TM with input and output is an ordinary k-string TM

s.t.

1. the first tape is read-only;

(Input cannot be modified.)

2. the last tape is write-only.

(Output cannot be wound back.)

18

Page 20: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Proposition

For any k-string TM M operating with time bound f(n) there is a

(k + 2)-string TM M ′ with input and output, which operates

within time bound O(f(n)).

19

Page 21: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Space Bound for TM

Suppose that, for a k-string TM M and input x,

(s, ⊲, x, . . . , ⊲, ǫ)M

−→ (H, w1, u1, . . . , wk, uk)

where H ∈ {h,“yes”,“no”} is a halting state.

1. The space required by M on input x is∑

k

i=1|wiui|.

2. If M is a machine with input and output, then the space required by

M on input x is∑

k−1

i=2|wiui|.

20

Page 22: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

1. We say that Turing machine M operates within space bound

f(n) if, for any input x, M requires space at most f(|x|).

2. A language L is in the space complexity class SPACE(f(n)) if

there is a TM with I/O that decides L and operates within

space bound f(n).

3. Define L = SPACE(lg(n)).

21

Page 23: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Theorem 2.3

Let L be a language in SPACE(f(n)). Then, for any ǫ > 0,

L ∈ SPACE(2 + ǫ · f(n)).

22

Page 24: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Random Access Machines

Input: (i1, i2, . . . , in)

Output: r0 (accumulator)

Memory: r0, r1, r2, . . . (infinite memory)

k: program counter

Three address modes: (for x)

1. j: direct;

2. ↑ j: indirect;

3. = j: immediate.

(arbitrary large number)

23

Page 25: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%24

Page 26: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Theorem 2.5

If a RAM program Π computes a function φ in time f(n), then

there is a 7-string TM which computes φ in time O(f(n)3).

(by simulation)

25

Page 27: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Nondeterministic Machines

A nondeterministic TM is a quadruple N = (K,Σ, ∆, s), where

1. K, Σ, s are as in ordinary TM;

2. ∆ ⊆ (K × Σ)× [(K ∪ {h,“yes”,“no”})× Σ× {←,→,−}].

26

Page 28: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%27

Page 29: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

1. N decides a language L if for any x ∈ Σ∗, x ∈ L if and only if

(s, ⊲, x)N∗

−→ (“yes”, w, u) for some strings w and u.

2. An input is accepted if there is some sequence of

nondeterministic choice that results in “yes”.

28

Page 30: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

N decides L in time f(n) if

1. N decides L;

2. for any x ∈ Σ∗, if (s, ⊲, x)Nk

−→ (“yes”, w, u), then k ≤ f(|x|).

Let NTIME(f(n)) be the set of languages decided by NTMs within

time f .

29

Page 31: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Let NP =⋃

k≥1 NTIME(nk).

We have

P ⊆ NP .

30

Page 32: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Example 2.9

TSP (D) ∈ NP

1. Write out arbitrary permutation of 1, . . . , n.

2. Check whether the tour indicated by this permutation is less

than the distance bound.

31

Page 33: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Theorem 2.6

Suppose that language L is decided by a NTM N in time f(n).

Then it is decided by a 3-string DTM M in time O(cf(n)), where

c > 1 is some constant depending on N .

(NTIME(f(n)) ⊆⋃

c≥1 TIME(cf(n)).)

32

Page 34: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Example 2.10

• Reachability ∈ NSPACE(lg n) (This is easy.)

• Reachability ∈ SPACE((lg n)2) (In Chapter 7.)

33

Page 35: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Why employ nondeterminism?

34

Page 36: Theory of Computation Chapter 2staffweb.ncnu.edu.tw/shieng/theory/2007_fall/slides/chap2.pdf · Theory of Computation Chapter 2 Guan-Shieng Huang Feb. 24, 2003 Feb. 19, 2006 0-0

'

&

$

%

Exercises

2.8.1, 2.8.4, 2.8.6, 2.8.7, 2.8.8, 2.8.9, 2.8.10, 2.8.11

35