# COMS W3261 CS Theory Lecture 19: The Classes P and NP

## 1. Time Complexity

• We now turn our attention to recursive languages and ask the question "How quickly can a Turing machine determine whether a given input string w is in a given recursive language L?"
• We say that a function T(n) is O(f(n)) [read "big-O of f(n)"] if there exists an integer n0 and a constant c > 0 such that for all integers nn0, we have T(n) ≤ cf(n).
• Big-O notation allows us to ignore constant factors and just focus on asymptotic behavior. E.g., T(n) = 2n2 is O(n2).
• Big-O notation allows us to ignore low-order terms. E.g., T(n) = 2n2 + 3n + 4 is O(n2).
• A Turing machine M is of time complexity T(n) if on any input of length n, M halts after making at most T(n) moves.
• Note that time complexity is a worst-case measure. If a Turing machine M is of time complexity T(n), then it makes at most T(n) moves on any input of length n.

## 2. The Classes P and NP

• We define P to be the set of languages L such that L = L(M) for some deterministic Turing machine M of time complexity T(n) where T(n) is a polynomial function of n.
• A problem that cannot be solved in polynomial time is said to be intractable.
• NP is the set of languages L such that L = L(M) for some nondeterministic Turing machine M where on any input of length n, there are no sequences of more than T(n) moves of M where T(n) is a polynomial function of n.
• At present, it is not known whether all problems in NP can be solved in polynomial time. The question of whether P = NP is one of the most important open problems in computer science and mathematics.
• The Clay Mathematics Institute has identified the P vs NP question as one of its seven Millennium Prize Problems and offers a one million dollar prize for its solution.

## 3. Polynomial-Time Reductions

• In our study of problems that can be solved in polynomial time, we restrict ourselves to polynomial-time reductions.
• A polynomial-time reduction is an algorithm that maps any instance I of problem A into an instance J of problem B in a number of steps that is a polynomial function of the length of I such that I is in A iff J is in B.
• Note that in a polynomial-time reduction the length of J must be a polynomial function of the length of I.

## 4. NP-Complete Problems

• A language L is NP-complete if
1. L is in NP and
2. For every language L' in NP there is a polynomial-time reduction of L' to L.
• A language satisfying condition (2) is said to be NP-hard.
• If A is NP-complete, B is in NP, and there is a polynomial-time reduction of A to B, then B is NP-complete.
• If any one NP-complete problem is in P, then P = NP.
• Examples of problems in P
• Determining whether a set of integers has a subset whose sum is negative.
• Finding the cheapest path between a pair of nodes in a graph.
• Lots of others.
• Examples of NP-complete problems
• Determining whether a set of integers has a nonempty subset whose sum is zero ("subset sum"). Note that we can verify a zero-sum subset in deterministic polynomial time, but finding if one exists is difficult.
• Finding a cycle in a graph that contains each node exactly once ("the Hamiltonian cycle problem"). Again, note that verifying that a cycle is Hamiltonian is easy, but finding a Hamiltonian cycle is hard.
• Lots of others.
• Examples of problems in NP not known to be in P or to be NP-complete

## 5. Boolean Expressions

• Boolean expressions are generated by the following CFG:
• ``````
E → E ∨ T | T
T → T ∧ F | F
F → ( E ) | ¬ F | var
``````
`var` represents a variable whose value can be either 1 (for true) or 0 (for false).
The operator ∧ stands for logical AND, ∨ for logical OR, and ¬ for logical NOT.
Note that the grammar gives the operator ¬ the highest precedence, then ∧, then ∨.

## 6. The Satisfiability Problem

• A truth assignment for a boolean expression assigns either the value true (1) or the value false (0) to each of the variables in the expression.
• The value E(T) of an expression E given a truth assignment T is the result of evaluating E with each variable x in E replaced by T(x).
• A truth assignment T satisfies E if E(T) = 1.
• An expression E is satisfiable if there exists a truth assignment T that satisfies E.
• The satisfiability problem (SAT) is to determine whether a given boolean expression is satisfiable.
• The value of E = x ∧ ¬(yz) given the truth assignment T(x) = 1, T(y) = 0, T(x) = 0 is 1. [1 ∧ ¬(0 ∨ 0) = 1]. Thus, E is satisfiable.
• The expression E = x ∧ (¬xy) ∧ ¬y is not satisfiable because none of the four truth assignments to the variables x and y causes E to have the value 1.
• We shall shortly prove that SAT is NP-complete.

## 7. Practice Problems

1. Is the function nlog n polynomial or exponential, where log is to the base 2?
2. Show that the function 2n2 is O(n2).
3. Show that the function 2n2 + 3n + 4 is O(n2).
4. Show that if f(n) and g(n) are polynomial functions, then f(g(n)) is a polynomial function.
5. If M is a nondeterministic Turing machine of time complexity T(n), then show that M can be simulated by a deterministic Turing machine of time complexity O(2T(n)).
6. Show that P and NP closed under the following operations:
1. reversal
2. union
3. concatenation
4. Kleene closure
7. Show that P is closed under complementation.
8. List all satisfying truth assignments for x ∧ (y ∧ ¬x) ∧ (z ∨ ¬y).