Complexity Classes
Complexity Classes
Complexity classes
NP-Completeness
2
Why should we care?
• Knowing that they are hard lets you stop beating
your head against a wall trying to solve them…
– Use a heuristic: come up with a method for solving a
reasonable fraction of the common cases.
– Solve approximately: come up with a solution that
you can prove that is close to right.
– Use an exponential time solution: if you really have
to solve the problem exactly and stop worrying about
finding a better solution.
3
Optimization & Decision Problems
• Decision problems
– Given an input and a question regarding a problem,
determine if the answer is yes or no
• Optimization problems
– Find a solution with the “best” value
• Optimization problems can be cast as decision
problems that are easier to study
– E.g.: Shortest path: G = unweighted directed graph
• Find a path between u and v that uses the fewest
edges
• Does a path exist from u to v consisting of at most k edges?
4
Algorithmic vs Problem Complexity
• The complexity of a computational problem
or task is the complexity of the algorithm with the
lowest order of growth of complexity for solving
that problem or performing that task.
– e.g. the problem of searching an ordered list has at
most lgn time complexity.
5
Class of “P” Problems
• Class P consists of (decision) problems that are
solvable in polynomial time
• Polynomial-time algorithms
– Worst-case running time is O(nk), for some constant k
• Examples of polynomial time:
– O(n2), O(n3), O(1), O(n lg n)
• Examples of non-polynomial time:
– O(2n), O(nn), O(n!)
6
Tractable/Intractable Problems
• Problems in P are also called tractable
• Problems not in P are intractable or unsolvable
– Can be solved in reasonable time only for small inputs
– Or, can not be solved at all
7
Example of Unsolvable Problem
• Turing discovered in the 1930’s that there are
problems unsolvable by any algorithm.
• The most famous of them is the halting
problem
– Given an arbitrary algorithm and its input, will that
algorithm eventually halt, or will it continue forever in
an “infinite loop?”
8
Example of Unsolvable Problem
(n is an integer)
While n > 1 do
if Odd(n) then
n = 3n + 1
else
n = n/2
9
Examples of Intractable Problems
10
Intractable Problems
• Can be classified in various categories based on
their degree of difficulty, e.g.,
– NP
– NP-complete
– NP-hard
• Let’s define NP algorithms and NP problems …
11
Nondeterministic and NP Algorithms
13
E.g.: Hamiltonian Cycle
• Given: a directed graph G = (V, E), determine a
simple cycle that contains each vertex in V
– Each vertex can only be visited once
• Certificate:
– Sequence: v1, v2, v3, …, v|V| hamiltonian
not
hamiltonian
14
Is P = NP?
• Any problem in P is also in NP: P
P NP NP
problems in NP
16
Reductions
• Reduction is a way of saying that one problem is
“easier” than another.
• We say that problem A is easier than problem B,
(i.e., we write “A B”)
if we can solve A using the algorithm that solves B.
• Idea: transform the inputs of A to inputs of B
yes
yes
f Problem B no
no
Problem A
17
Polynomial Reductions
18
NP-Completeness (formally)
• A problem B is NP-complete if:
P NP-complete
(1) B NP
NP
(2) A p B for all A NP
19
Implications of Reduction
yes
yes
f Problem B no
no
Problem A
- If A p B and B P, then A P
- if A p B and A P, then B P
20
Revisit “Is P = NP?”
P NP-complete
NP
21
P & NP-Complete Problems
• Euler tour
– G = (V, E) a connected, directed graph find a cycle
that traverses each edge of G exactly once (may visit
a vertex multiple times)
– Polynomial solution O(E)
• Hamiltonian cycle
– G = (V, E) a connected, directed graph find a cycle
that visits each vertex of G exactly once
– NP-complete
22
Examples NP-complete and
NP-hard problems
NP-complete
NP-hard
23