2870 字
14 分钟
Theory of Computation Quick Reference Notes

持续更新中。

Theory of Computation

  • (Models) What is computation?
  • (Computability) What can be computed?
  • (Complexity) What can be computed efficiently?

Complexity Notation

Notation Analogy
𝑓(𝑛)=𝑂(𝑔(𝑛)) ≤
𝑓(𝑛)=𝑜(𝑔(𝑛)) <
𝑓(𝑛)=Ω(𝑔(𝑛)) ≥
𝑓(𝑛)=𝜔(𝑔(𝑛)) >
𝑓(𝑛)=Θ(𝑔(𝑛)) ≈
𝑓(𝑛)=𝜃(𝑔(𝑛)) =
  • Big O notation: Let 𝑓,𝑔:ℝ→ℝ. We write 𝑓(𝑥)=𝑂(𝑔(𝑥)) if (∃𝑐>0)(∃𝑁)(∀𝑥≥𝑁)(|𝑓(𝑥)|≤𝑐⋅|𝑔(𝑥)|).
  • 𝑂𝑦 notation: Let 𝑓:ℝ2→ℝ and 𝑔:ℝ→ℝ. We write 𝑓(𝑥,𝑦)=𝑂𝑦(𝑔(𝑥)) if (∀𝑦)(∃𝑐=𝑐(𝑦)>0)(∃𝑁=𝑁(𝑦))(∀𝑥≥𝑁)(|𝑓(𝑥,𝑦)|≤𝑐⋅|𝑔(𝑥)|), that is, the constant 𝑐 and the threshold 𝑁 may depend on 𝑦.
  • 𝑂˜ notation: Let 𝑓,𝑔:ℝ→ℝ. We write 𝑓(𝑥)=𝑂˜(𝑔(𝑥)) if (∃𝑐>0)(∃𝑁)(∀𝑥≥𝑁)(|𝑓(𝑥)|≤(log𝑥)𝑐⋅|𝑔(𝑥)|). In other words, 𝑂˜ hides polylogarithmic (log𝑂(1)𝑛) terms.

Alphabets and Languages

  • Alphabet (字母表) : An alphabet is a set of symbols.
    • Binary alphabet (二进制字母表) : {0,1}.
  • String (字符串) : A string over an alphabet is a finite sequence of symbols from the alphabet.
    • Empty string (空串) : 𝜖.
    • Length (长度) : The length of a string is its length as a sequence. Denote the length of a string 𝑤 by |𝑤|.
    • Σ∗: the set of all strings over an alphabet Σ.
    • Σ𝑛: the set of strings of length 𝑛 over an alphabet Σ.
    • Concatenation (连接) : Two strings over the same alphabet can be combined to form a third by the operation of concatenation. The concatenation of strings 𝑥 and 𝑦 is denoted by 𝑥𝑦.
  • Language (语言) : A language is a set of strings over an alphabet, denoted by 𝐿∈Σ∗.
    • Since languages are sets, the usual set operations apply: union (∪, 并) , intersection (∩, 交) , and difference (\, 差) .
    • Complement (补) : Let 𝐿∈Σ∗. The complement of 𝐿, denoted by 𝐿, is Σ∗\𝐿.
    • Concatenation (连接) : If 𝐿1, 𝐿2 are languages, the concatenation of 𝐿1 and 𝐿2 is defined by 𝐿1𝐿2or𝐿1∘𝐿2={𝑤=𝑥𝑦:𝑥∈𝐿1,𝑦∈𝐿2}.
    • Kleene star (Kleene 星号) : Let 𝐿∈Σ∗. The Kleene star of 𝐿 is defined by 𝐿∗={𝑤=𝑤1…𝑤𝑘:𝑘≥0,𝑤𝑖∈𝐿}.
    • Kleene plus (Kleene 加号) : We write 𝐿+ for the language 𝐿𝐿∗. Equivalently, 𝐿+={𝑤=𝑤1…𝑤𝑘:𝑘≥1,𝑤𝑖∈𝐿}.

Problem Encodings

  • Decision problem (判定问题) : asks a yes/no question about the input. It can be represented as a language 𝐿⊆Σ∗, where the answer for input 𝑥 is yes if 𝑥∈𝐿, and no if 𝑥∉𝐿.
    • Reduction to decision problems: computation, search, and optimization problems can all be reduced to decision problem.
  • Computation problem (计算问题) : asks to compute some output from the input, not just a yes/no answer. It can be represented as a partial function (部分函数) 𝑓:Σ∗→Σ∗∪{undefined}.
    • Reducing computation to decision: Let 𝑓:Σ∗→Σ∗. Define 𝐿𝑓={(𝑥,𝑖,𝑏):the𝑖-th bit of𝑓(𝑥)is𝑏}. We can compute 𝑓(𝑥) by making 𝑂(|𝑓(𝑥)|) queries to an oracle for 𝐿𝑓, one query per bit of 𝑓(𝑥). So solving the decision problem 𝐿𝑓 lets us solve the computation problem 𝑓.
  • Search problem (搜索问题) : asks to find a solution satisfying some property. Formally, given 𝑥∈{0,1}∗, find 𝑦∈{0,1}∗ such that 𝑅(𝑥,𝑦) holds, where 𝑅 is a relation.
    • Reducing search to decision: Define 𝐿𝑅={(𝑥,𝑦𝑝):𝑦𝑝is a prefix of some𝑦satisfying𝑅(𝑥,𝑦)}. Once 𝐿𝑅 can be decided, we can find 𝑦 by 𝑂(|𝑦|) queries, extending the prefix 𝑦𝑝 one bit at a time.
  • Optimization problem (优化问题) : asks to find a solution that maximizes or minimizes some objective. Formally, OPT(𝑥)=max{𝑐(𝑥,𝑦):𝑅(𝑥,𝑦)}, where 𝑅 is a relation and 𝑐 is a cost function.
    • Reducing optimization to decision: Define 𝐿OPT={(𝑥,𝑘):∃𝑦such that𝑅(𝑥,𝑦)and𝑐(𝑥,𝑦)≥𝑘}. By binary search, OPT(𝑥) can be computed by making 𝑂(|OPT(𝑥)|) queries to 𝐿OPT.
  • Reasonable encoding: we only consider encodings that are polynomial-time interconvertible. Under this restriction, the choice of encoding does not affect the complexity class a problem belongs to.

Finite Automata

  • The Finite automaton is the simplest computation model, one without memory.
  • A Finite automaton (有限自动机) is a 5-tuple (𝑄,Σ,𝛿,𝑞0,𝐹), where
    • 𝑄 is a finite set called the states (状态) .
    • Σ is a finite set called the alphabet (字母表) .
    • 𝛿 is the transition function (转移函数) .
    • 𝑞0∈𝑄 is the start state (初始状态) .
    • 𝐹⊆𝑄 is the set of accept states (接受状态) .
  • Deterministic Finite Automaton (DFA, 确定型有限自动机) : 𝛿is𝑄×Σ→𝑄.
  • Non-deterministic Finite Automaton (NFA, 非确定型有限自动机) : 𝛿is𝑄×Σ→2𝑄.
    • 𝜖-arrow (𝜖-转移) : 𝛿is𝑄×(Σ∪{𝜀})→2𝑄. It allows the NFA to change state without reading any input symbol.
  • Accepting a string: Let 𝑀=(𝑄,Σ,𝛿,𝑞0,𝐹) be a finite automaton and 𝑤=𝑤1𝑤2…𝑤𝑛 a string where each 𝑤𝑖∈Σ. 𝑀 accepts 𝑤 if there exists a sequence of states 𝑟0,𝑟1,…,𝑟𝑛∈𝑄 such that
    • 𝑟0=𝑞0,
    • 𝛿(𝑟𝑖,𝑤𝑖+1)=𝑟𝑖+1, for 𝑖=0,1,…,𝑛−1,
    • 𝑟𝑛∈𝐹.
  • Accepting a language: If 𝐿 is the set of all strings that a finite automaton 𝑀 accepts, we say 𝑀 recognizes (识别) or accepts (接受) 𝐿, and we write 𝐿(𝑀)=𝐿.
  • Equivalence of NFAs and DFAs : Use the power set (幂集) . An NFA may be in several states at once, so the DFA tracks the set of all states the NFA could currently be in. A state of the new DFA is therefore a set of NFA states, i.e., an element of 𝒫︀(𝑄).

Regular Language

  • Regular language (正则语言) : A language is called a regular language if some finite automaton recognizes it.
    • Regular Languages⊂P⊂Decidable Languages⊂All Languages.
  • Regular operations (正则运算) : union, intersection, difference, complement, concatenation, star…
  • Closure property (闭包性质) of regular languages : if 𝐿1,𝐿2 are regular languages, then so are 𝐿, 𝐿1∪𝐿2, 𝐿1∩𝐿2, 𝐿1∘𝐿2, 𝐿1∗, and 𝐿1\𝐿2. Each closure is proved by constructing a finite automaton that recognizes the resulting language.
    • Closure under union and intersection: Use the Cartesian product (笛卡尔积) construction.
    • Closure under concatenation: Connect every accept state of 𝑁1 to the start state of 𝑁2 by an 𝜖-arrow, so that 𝑁 may switch from the 𝑁1 part to the 𝑁2 part after having read any 𝑥∈𝐿1.
    • Closure under star: Add 𝜖-arrows from every accept state back to the start state so that the machine may repeat the loop any number of times.

Nonregular Languages

  • Almost all languages are non-regular: Every regular language is described by some DFA, and each DFA can be encoded as a finite string. Since there are only countably many such strings, there are only countably many regular languages. The set of all languages is 𝒫︀(Σ∗), and since Σ∗ is countably infinite, |𝒫︀(Σ∗)|=2ℵ0, which is uncountable. Since ℵ0<2ℵ0, almost all languages are non-regular.
  • Pumping lemma (泵引理) : If 𝐿 is a regular language, then there exists 𝑝∈ℕ such that, for any string 𝑠∈𝐿 with |𝑠|≥𝑝, we can write 𝑠=𝑥𝑦𝑧, where 𝑥,𝑦,𝑧∈Σ∗, such that
    • 𝑥𝑦𝑖𝑧∈𝐿 for all 𝑖≥0,
    • |𝑦|>0, and
    • |𝑥𝑦|≤𝑝.
  • Applying the pumping lemma: The lemma is a tool to disprove regularity: assume 𝐿 is regular, then pump a long string 𝑠∈𝐿 and show that some 𝑥𝑦𝑖𝑧 falls outside the language – a contradiction.

Regular Expressions

  • Regular expression (regex, RE, 正则表达式) : an intuitive way to describe patterns in strings. Practical regex tools are built on (and extend) it, and it describes exactly the regular languages.
  • Syntax (语法) : 𝑅 is a regular expression if 𝑅 is
    1. a symbol 𝑠, for some 𝑠∈Σ,
    2. 𝜖,
    3. ∅,
    4. (𝑅1∪𝑅2), where 𝑅1,𝑅2 are regular expressions,
    5. (𝑅1𝑅2), where 𝑅1,𝑅2 are regular expressions,
    6. (𝑅1)∗, where 𝑅1 is a regular expression.
  • Semantics (语义) : The language 𝐿(𝑅) of a regular expression 𝑅 is defined by
    1. 𝐿(𝑠)={𝑠}, for 𝑠∈Σ,
    2. 𝐿(𝜖)={𝜖},
    3. 𝐿(∅)=∅,
    4. 𝐿(𝑅1∪𝑅2)=𝐿(𝑅1)∪𝐿(𝑅2),
    5. 𝐿(𝑅1𝑅2)=𝐿(𝑅1)∘𝐿(𝑅2),
    6. 𝐿(𝑅1∗)=𝐿(𝑅1)∗.
  • Theorem: A language is regular if and only if some regular expression describes it.
    • Regular expression → NFA: Build the NFA by induction, following how the regular expression is built. Three base cases (a single symbol, 𝜖, ∅) and three ways to combine smaller NFAs (union, concatenation, star) – exactly the closure constructions.
    • DFA/NFA → regular expression: Convert the NFA into a generalized NFA (GNFA, 广义 NFA) , whose arrows are labeled by regular expressions, then remove states one by one until only 𝑞start and 𝑞accept remain; the label on the last arrow is the answer.

Turing Machines

  • Hilbert’s program (希尔伯特纲领) : After the crisis in the foundations of mathematics around 1900 (e.g. Russell’s paradox, 1901), Hilbert’s answer in the 1920s was to formalize all of mathematics: a fixed list of axioms (公理) and rules of inference (推理规则) , in which a proof is a finite string of symbols, checkable mechanically. The goal was to show that mathematics is complete, consistent, and decidable.
Hilbert’s goal Answer Who
Complete? No Gödel, 1931
Consistent (provably)? No Gödel, 1931
Decidable? No Turing / Church, 1936
  • The Entscheidungsproblem (判定问题, Hilbert & Ackermann, 1928) : Is there an algorithm that, given any statement of first-order logic, decides whether it is provable?
    • To answer “yes” it suffices to write down one algorithm; to answer “no” one must rule out every possible algorithm, which needs a precise definition of “algorithm” – and that is exactly what the Turing machine supplies.
  • Turing machine (TM, 图灵机) : a 7-tuple (𝑄,Σ,Γ,𝛿,𝑞0,𝑞accept,𝑞reject), where 𝑄,Σ,Γ are all finite sets and
    1. 𝑄 is the set of states (状态) ,
    2. Σ is the input alphabet (输入字母表) , not containing the blank symbol ⊔,
    3. Γ is the tape alphabet (纸带字母表) , where ⊔⊆Γ and Σ⊆Γ,
    4. 𝛿:𝑄×Γ→𝑄×Γ×{𝐿,𝑅} is the transition function (转移函数) ,
    5. 𝑞0∈𝑄 is the start state (初始状态) ,
    6. 𝑞accept∈𝑄 is the accept state (接受状态) , and
    7. 𝑞reject∈𝑄 is the reject state (拒绝状态) , where 𝑞reject≠𝑞accept.
    • One step of the computation has three parts: read the symbol in the current cell; write a new symbol there and update the state; move the head one cell left or right.
    • Configuration (格局) : records everything needed to continue the computation: the current state, the full tape contents, and the head position. Formally, a configuration is written as 𝑢𝑞𝑣, where 𝑞∈𝑄 is the current state, 𝑢,𝑣∈Γ∗, the tape contains 𝑢𝑣 followed by blanks, and the head is on the first symbol of 𝑣.
  • Acceptance: 𝑀 accepts (接受) input 𝑤 if there exists a sequence of configurations 𝐶0,𝐶1,…,𝐶𝑘 such that
    • 𝐶0 is the start configuration: state 𝑞0, with 𝑤 written on the tape and the head at the leftmost cell,
    • each 𝐶𝑖 yields 𝐶𝑖+1, that is, 𝐶𝑖+1 follows from 𝐶𝑖 by one application of 𝛿, and
    • 𝐶𝑘 is an accepting configuration: its state is 𝑞accept.
  • Recognizable language (可识别语言) : The set of strings that 𝑀 accepts is the language recognized by 𝑀, denoted 𝐿(𝑀). 𝐿 is recognizable if there is a TM 𝑀 such that 𝐿(𝑀)=𝐿.
  • Deciding a language: Let 𝐿⊆{0,1}∗ be a language, 𝑀 a Turing machine, and 𝑇:ℕ→ℕ. 𝑀 decides (判定) 𝐿 in time 𝑇(𝑛) if for every 𝑥∈{0,1}∗, 𝑀 halts within 𝑇(|𝑥|) steps, if 𝑥∈𝐿 then 𝑀 accepts 𝑥, and if 𝑥∉𝐿 then 𝑀 rejects 𝑥.
  • Decidable language (可判定语言) : 𝐿 is decidable if some Turing machine decides 𝐿.
    • On an input, a Turing machine may accept, reject, or loop forever. A TM that decides a language always halts – it never loops forever.
    • Every decidable language is recognizable; the converse is not true. For example, the halting problem is recognizable but not decidable.
  • Computational Turing machine (计算图灵机) : a 6-tuple (𝑄,Σ,Γ,𝛿,𝑞0,𝑞end), where 𝑞end∈𝑄 is the end state (终止状态) . 𝑀 computes 𝑓:Σ∗→Σ∗ if, on every input 𝑤∈Σ∗, 𝑀 halts in 𝑞end with 𝑓(𝑤) on its tape.
  • Computable function (可计算函数) : Let 𝑓:{0,1}∗→{0,1}∗∪{undefined} be a possibly partial function. 𝑀 computes 𝑓 within time 𝑇(𝑛) if for every 𝑥∈{0,1}∗ with 𝑓(𝑥)≠undefined, 𝑀 halts on 𝑥 within 𝑇(|𝑥|) steps, with exactly 𝑓(𝑥) on its tape. 𝑓 is computable if some TM computes it.

Variants of Turing Machines

  • The Turing machine model is robust: the basic model and all the variants below have the same computational power, that is, they recognize (or decide) the same class of languages, and each can simulate the other with at most a polynomial slowdown, that is, 𝑇(𝑛) steps become poly(𝑇(𝑛)) steps.
  • Stay option: allow 𝛿:𝑄×Γ→𝑄×Γ×{𝐿,𝑅,𝑆}, where 𝑆 means the head remains in place. If 𝑀 decides 𝐿 in time 𝑇(𝑛), then 𝐿 is decidable in time 𝑂(𝑇(𝑛)) by a standard Turing machine.
  • Change of alphabet: let 𝐿⊆{0,1} and 𝑇(𝑛)≥𝑛2. If 𝐿 is decided in time 𝑇(𝑛) by a Turing machine with tape alphabet Γ, then 𝐿 is decided in time 𝑂(log|Γ|⋅𝑇(𝑛)) by a Turing machine with tape alphabet {0,1,⊔}.
  • One-way Turing machine (单向图灵机) : a Turing machine whose tape is infinite in only one direction.
  • Multitape Turing machine (多带图灵机) : 𝛿:𝑄×Γ𝑘→𝑄×Γ𝑘×{𝐿,𝑅,𝑆}𝑘, with one head per tape. In state 𝑞, if head 𝑖 reads 𝑎𝑖 for every 𝑖, the machine enters 𝑞′, and each head 𝑖 writes 𝑏𝑖 and moves in direction 𝐷𝑖. The input 𝑥 is on tape 1; tapes 2,…,𝑘 are blank. All heads start on cell 0 in state 𝑞0.
  • RAM Turing machine (RAM 图灵机) : a (𝑘+1)-tape Turing machine 𝑀=(𝑄,Σ,Γ,𝛿,𝑞0,𝑞accept,𝑞reject,𝑄access) with
    • tapes 1,…,𝑘 as work tapes (工作带) (tape 1 holds the input) and tape 𝑘+1 as the address tape (地址带) ;
    • a memory 𝐴:ℕ→Γ, initially 𝐴[𝑖]=⊔ for all 𝑖;
    • a set of access states 𝑄access⊆𝑄∖{𝑞accept,𝑞reject}.
    • Γ contains two special symbols Rd and Wr. When 𝑀 enters a state in 𝑄access:
      • Read: if the address tape contains bin(𝑖)Rd, the address tape is erased and 𝐴[𝑖] is written into its first cell.
      • Write: if the address tape contains bin(𝑖)Wr𝛼, then 𝐴[𝑖] is set to 𝛼 and the address tape is erased.
      • Otherwise, nothing happens.
    • Each access counts as one step.
    • The transition function is that of an ordinary (𝑘+1)-tape machine; the memory 𝐴 never appears in 𝛿, it is used only through access states.
  • DTIME: let 𝑇:ℕ→ℕ. A language 𝐿⊆{0,1}∗ is in DTIME(𝑇(𝑛)) if there exists a multitape Turing machine 𝑀 that decides 𝐿 in time 𝑂(𝑇(𝑛)).
  • P: P=⋃𝑐≥1DTIME(𝑛𝑐)=DTIME(𝑛)∪DTIME(𝑛2)∪DTIME(𝑛3)∪…. 𝑃 does not depend on the choice of Turing machine model, since these models can all simulate each other in polynomial time.
  • Two Conjectures (猜想) : with CLIQUE={⟨𝐺,𝑘⟩:graph𝐺has a clique (团) of size𝑘},
    • P≠NP, equivalently CLIQUE∉P.
    • CLIQUE∉DTIME(𝑛).
  • Summary: every variant is simulated with at most polynomial overhead, so the class of decidable languages is the same, and 𝑃 is the same, whichever model we use.
Variant Simulated in time Proof idea
Stay option 𝑂(𝑇(𝑛)) stay = right, then left
Alphabet 𝑂(log|Γ|⋅𝑇(𝑛)) encode each symbol in log2|Γ| bits
One-way TM 𝑂(𝑇(𝑛)) fold the tape into two tracks
Multitape TM 𝑂𝑘(𝑇(𝑛)2) 𝑘 tracks, hats mark heads
RAM TM 𝑂(𝑇(𝑛)2) add a memory tape holding a list of (address, value) pairs

Church–Turing Thesis

  • From C to a Turing machine: every RISC-V RV32I instruction can be simulated by a multitape RAM TM in 𝑂(1) steps, so every C program running in time 𝑇(𝑛) is compiled to RV32I (𝑇(𝑛) steps), simulated by a multitape RAM TM (𝑂(𝑇(𝑛))), merged onto a multitape TM (𝑂(𝑇(𝑛)2)), and encoded over the alphabet {0,1} on a single-tape TM (𝑂(𝑇(𝑛)4)). That is, Every C program running in time 𝑇(𝑛) can be simulated by a single-tape TM over {0,1} in time 𝑂(𝑇(𝑛)4) – polynomial.
  • Other models equivalent to Turing machines:
Model Idea
Register Machine (寄存器机) INC/DEC/JZ on counters (计数器)
λ-calculus (λ 演算) / SKI β-reduction (β-归约)
μ-Recursive Functions (μ-递归函数) prim. recursion + μ (原始递归 + μ)
Post / Markov Systems string rewriting
Cellular Automata (元胞自动机) local rules
Boolean Circuits (布尔电路) circuit families
  • Church–Turing Thesis (CTT, 丘奇–图灵论题) : every function that is effectively computable is computable by a Turing machine.
  • Strong Church–Turing Thesis (强丘奇–图灵论题) : any reasonable model of computation can be simulated by a Turing machine with at most polynomial overhead. Possibly false: quantum computers.
  • Physical Church–Turing Thesis (物理丘奇–图灵论题) : every function that can be computed by a physical system is computable by a Turing machine.

Universal Turing Machine

  • Why a universal Turing machine (UTM, 通用图灵机) ?
    • One machine runs every program, so no special hardware per algorithm.
    • Encoding machines as strings lets us prove computability results.
    • Origin of the stored-program computer (von Neumann architecture, 1945).
  • Stored-program computer (存储程序计算机) : instructions and data share one memory, so a program is just data interpreted as instructions. The UTM is the model, a computer is the realization. Calculators, traffic light controllers and elevator controllers are not universal (fixed logic); laptops, desktops and smartphones are.
  • Encoding a Turing machine as a string: assume every string 𝛼∈{0,1}∗ represents some Turing machine 𝑀𝛼 (if 𝛼 is invalid, 𝑀𝛼 always rejects), and every Turing machine is represented by infinitely many strings.
  • Theorem: there is a 2-tape Turing machine 𝑈 such that for all 𝑥,𝛼∈{0,1}∗, 𝑈(𝑥,𝛼)=𝑀𝛼(𝑥), with the same outcome (accept, reject, or run forever). Moreover, if 𝑀𝛼 halts on 𝑥 within 𝑇 steps, then 𝑈 halts within 𝑂(𝑇log𝑇) steps.