复杂度类别

中文 | English

 

这一章,我们先去搞清楚这些名词的定义:NP、NP-hard、NP-complete、co-NP、P vs NP。

在我接触这些知识之前,我一直觉得这些词语很高级很深奥,今天就让我们来一起探索。

重温:P问题是 能在poly-time复杂度内被解决的问题的集合。

验证

一个问题是否属于 NP 问题,我们并不关心这个问题是否难解,而是关心这个问题的验证!

NP 的核心是:

给你一个 candidate 解,你能否在多项式时间“验证”它是否正确。

这个就是重点中的重点,我们并不关心你是否好解。

NP 的正式定义(但用直观方式表达)

NP = Nondeterministic Polynomial-time。

正式定义:一个问题属于 NP,当且仅当 每个 YES 实例都有一个多项式长度的 certificate,并且该 certificate 可以被 poly-time 验证。

一些 NP 问题的例子

Independet Set Problem 独立集问题

Def. In a graph G=(V,E), we say that a set of nodes SE is a independent set if no two nodes in S are joind by an edge.

即:一个图G的点子集,两两之间没有边。

所以 Independent Set Problem 属于 NP。

SAT Problem and 3-SAT Problem

Clause (子句) 的定义:

Given n boolean variables x1,x2,...,xn, a clause is a disjunction of terms t1t2...tl, where ti{x1,...,xn,x1,...,xn}

其实就是一堆0或1取or的变量。

SAT Problem 的定义:

Given clauses C1,...,Ck, if there is an assignment that can satisfied all the clauses: C1C2,...,Ck.

其实问,能否找到一个赋值方法,让所有子句都满足。

显然,这个问题也可以在 poly-time 时间内完成验证。

因此:NP = 所有能在多项式时间“验证 YES 实例”的问题集合。

求解可能难,但验证必须快。

Clique Problem

Problem CLIQUE(G,k) asks whether, given a graph G, does G contain a k-clique? A k-clique is defined to be a set of k vertices such that each pair of these vertices shares an edge.

给一个图,能否找到k个节点,这k个节点是两两相连的。

Vertex Cover

在一个图里,能否找到一系列点,覆盖所有的边。一条边被覆盖:即两端其中有一个点属于 Vertex Cover 中。

Hamiltonian Cycle(决策版本)

在图G种找一条环路径,遍历每一个点一次。

什么是 NP-hard (NP难)

NP-hard = “至少和 NP 中最难的问题一样难”。

更正式地:

NP-hard 问题是所有 NP 问题都可以 poly-time 规约到它的那些问题。

The Halting Problem is one of the most fundamental results in theoretical computer science. It asks: \ Given a program P and an input x, will P(x) eventually halt, or will it run forever? \ Alan Turing proved in 1936 that there is no algorithm that can always answer this question correctly for all possible programs and inputs.

因此:

NP-complete NP-hard

NP-hard 是一个更大的集合。

关于如何证明一个问题是否属于NP的完整严谨证明过程,我放到本章末尾: 如何证明一个问题属于NP

什么是 NP-complete (NP完全)

NP-complete = NP NP-hard。

换句话说:

一个问题 X 是 NP-complete,当且仅当:

  1. X ∈ NP

  2. NP 中所有问题都可以 poly-time 规约到 X

这些问题是 NP 中最难的“代表性问题”。

常见 NP-complete 的问题:

  1. SAT

  2. 3SAT

  3. CLIQUE

  4. Independent Set

  5. Vertex Cover

  6. Hamiltonian Cycle

  7. TSP(decision)

它们之间可以互相规约,因此本质上难度相同。

P vs NP:计算机科学最重要的问题

通过上面的内容,可以知道:

显然:

PNP

但是否:

P = NP ?

目前未知。

Important
目前结论:P 是否等于 NP 是未知的!

但大多数计算机科学家都相信:

P ≠ NP

如果 P = NP,会发生什么?

但目前,这个问题仍然未决,是千禧年七大难题之一。

如何证明一个问题属于NP

例子一

The Sushi Express wants to implement a new feature, "order batching." The menu consists of a set of disired food items F and set of combos c1,...,cn where each combo is a set of food items along with a price - we represent combo ci=({fi1,fi2,...,fik},pi), where each fij is a food item in the combo. If a student orders ci, they receive each of the food items, and they pay the cost pi. In the new order batching system, the students would input a list of food items that they want, and the order batching system should find the cheapest set of combos, which would include at least one of each desired food item. Write the decision version of this problem, and show via reduction that this decision problem is NP Hard.

这是一道我的作业题,当然,这里先不去证明 HP-Hard, 我们先去证明问题的Decision Version属于 NP。

Define the Decision Version

We define the decision version of the Sushi Batching problem as follows:

Input:

Question:

Does there exists a subset of combos I{1,...,n} such that:

  1. iISiR (Unite to cover all the food you want), and

  2. iIpiB

Showing the Problem is in NP

A certificate can be a subset of combo indices I{1,...,n}. The certifier checks the following in polynomial time:

  1. Compute iIpi and verify it is B

  2. Compute the union iISi and verify that it contains every element of R.

  3. Verify that I has no duplicate indices.

These checks require only scanning sets and summing numbers, all of which take polynomial time. Thus the problem has an efficient certification and is in NP.

例子二

Problem CLIQUE(G,k) asks whether, given a graph G, does G contain a k-clique? A k-clique is defined to be a set of k vertices such that each pair of these vertices shares an edge. Show via reduction that CLIQUE is NP-complete.

同样,先证明问题是属于 NP 的。

Showing the Problem is in NP

Certificate:

A certificate can be a subset of vertices SV with |S|=k.

Certifier:

Given (G,k) and the certificate S, the certifier:

  1. Checks that |S|=k

  2. For every pair (u,v) in S, checks whether (u,v)E

There are at most k/2 pairs, so this verification takes polynomial time in the size of the input graph. Thus CLIQUE has an efficient certifiction and is in NP.

看到这里应该证明的套路已经没问题了,其实证明一个问题属于 NP 是非常简单的,就直接去说这个问题可以在 poly-time 内认证即可。