Computational Complexity
Computational Complexity
Theory
1
Computational complexity theory
• Even when a problem is decidable and thus
computationally solvable in principle, it may
not be solvable in practice if the solution
requires an inordinate amount of resources
(time or space)
• And the computational complexity theory
tries to investigate the time, memory, or
other resources required for solving
computational problems.
• But, we limit our discussion to issues of
time – complexity. 2
Computational complexity theory
Goal: A general theory of the resources
needed to solve computational problems.
What types of resources?
time energy
space
What types of computational problems?
composing a poem optimization
3
Decision problems
A decision problem is a computational
problem with a yes or no answer.
Example: Is the number n prime?
Why focus on decision problems?
4
Decision problems are surprisingly general:
Many other problems can be recast in terms
of decision problems that are essentially
equivalent.
5
Recasting other problems as decision problems
6
Factoring problem: What is the smallest non-
trivial factor of n?
7
Efficiency
insoluble insoluble
hard
soluble
soluble soluble
easy
10
Time Complexity
11
Time Complexity: The number of steps
during a computation
12
Measuring Time Complexity
• Model of computation: TM
• timereq(M) is a function of n:
• If M is deterministic
timereq(M) = f(n) = the maximum
number of steps that M executes on any
input of length n.
• If M is nondeterministic
timereq(M) = f(n) = the number of
steps on the longest path that M executes
on any input of length n.
13
Asymptotic upper bound - O
f(n) O(g(n)) iff there exists a positive
integer k and a positive constant c such
that:
n k, (f(n) c g(n)).
In other words, ignoring some number of
small cases (all those of size less than k),
and ignoring some constant factor c, f(n)
is bounded from above by g(n).
In this case, we’ll say that f is “big-oh” of g
or g asymptotically dominates f or g grows
at least as fast as f does 14
Asymptotic upper bound - O
● n3 O(n3).
● n3 O(n4).
● 3n3 O(n3).
● n3 O(3n).
● n3 O(n !).
● log n O(n). 15
Summarizing O
16
Algorithmic Gaps
We’d like to show:
19
n n
L {a b : n 0}
21
Complexity classes: are set of languages/
functions that can be decided/ computed
within a given resource.
22
Consider a deterministic Turing Machine M
which decides a language L
23
For any string w the computation of M
terminates in a finite amount of transitions
Initial Accept
state or Reject w
24
Decision Time = #transitions
Initial Accept
state or Reject w
25
Consider now all strings of length n
26
TIME TM (n )
1 2 3 4 n
STRING LENGTH
L1 L2
L3
28
Example: L1 {a b : n 0}
n
TIME (n )
L1 {a b : n 0}
n
29
Other example problems in the same class
TIME (n )
L1 {a b : n 0}
n
{ab aba : n , k 0 }
n
{b : n is even }
n
{b : n 3k }
n
30
Examples in class:
TIME (n ) 2
{a b : n 0 }
n n
{ww R : w {a , b }}
{ww : w {a , b }}
31
Examples in class:
TIME (n ) 3
CYK algorithm
L2 { G ,w : w is generated by
context - free grammar G }
Matrix multiplication
L3 { M1 , M2 , M3 : n n matrices
and M1 M2 M3 }
32
Polynomial time algorithms: TIME (n )
k
constant k 0
33
It can be shown: TIME (n k 1 ) TIME (n k )
TIME (n k 1 )
TIME (n ) k
34
The Time Complexity Class P
P TIME (n k
)
k
0
Represents:
• “tractable” problems
35
Class P
{a b }
n
{a b }
n n
{ww }
CYK-algorithm RELPRIME
Matrix multiplication
36
Our first computational complexity class: “P”
Definition: The set of all decision problems soluble in
polynomial time on a Turing machine is denoted P.
decision problems
P
“easy” problems
38
Doesn’t the definition of P depend upon the
computational model used in the statement
of the definition, namely, the Turing
machine?
39
The strong Church-Turing thesis
40
That is, all reasonable deterministic
computational models are polynomially
equivalent.
41
Many important problems aren’t known to be in P
12 km
15 km
19 km 14 km
16 km
43
Traveling salesman decision problem: Given a
network and a number, k, is there a tour
through all the cities of length less than k?
44
Other Examples
Example: the Hamiltonian Path Problem
s t
45
s t
YES!
46
A solution: search exhaustively all paths
nk
L TIME (n ! ) TIME (2 )
Exponential time
Intractable problem
47
The clique problem
48
The clique problem
49
Example: The Satisfiability Problem
Boolean expressions in
Conjunctive Normal Form:
t1 t2 t3 tk clauses
ti x1 x2 x3 x p
Variables
satisfying assignment: a true assignment causing
the output to be 1.
Question: is the expression satisfiable?
50
Example: ( x1 x2 ) ( x1 x3 )
Satisfiable: x1 0, x2 1, x3 1
( x1 x2 ) ( x1 x3 ) 1
51
Example: ( x1 x2 ) x1 x2
Not satisfiable
52
L {w : expression w is satisfiable}
nk
L TIME (2 )
exponential
Algorithm:
search exhaustively all the possible
binary values of the variables
53
Witnesses/Certificates
Example: Factoring decision problem
54
The factoring decision problem has witnesses
to yes instances of the problem – informally,
solutions to the factoring problem, which can
be checked in polynomial time.
There are problems hard to determine the
solution but easy to verify the solution once it
is found
Easy or not?
Of course, very easy.
56
Hamiltonian cycles
– A simple path containing every vertex.
– HAM-CYCLE={<G>: G is a Hamiltonian graph,
i.e. containing Hamiltonian cycle}.
• The naïve algorithm for determining HAM-
CYCLE runs in O(m!)=O(2m) time, where m
is the number of vertices, m n1/2.
• However, given an ordered sequence of m
vertices (called “certificate”), let you
verify whether the sequence is a
Hamiltonian cycle.
• Very easy. In O(n2) time.
57
Class NP
58
Examples: The factoring and TSP decision problems.
60
Non-Determinism
Language class: NTIME (n)
NTIME (n)
L1 L3
L2
Non-Deterministic Algorithm
to accept a string ww :
O(| w |)
•Compare the two tapes
Total time: O(| w |)
63
NTIME (n)
L {ww}
64
In a similar way we define the class
NTIME (T (n))
2 3
Examples: NTIME (n ), NTIME (n ),...
65
Non-Deterministic Polynomial time algorithms:
k
L NTIME (n )
66
The class NP
NP NTIME (n k
)
k
0
67
Example: The satisfiability problem
L {w : expression w is satisfiable}
Non-Deterministic algorithm:
•Guess an assignment of the variables
68
L {w : expression w is satisfiable}
L NP
70
The P vs. NP Question
71
Set L is in P if membership in L can be
decided in poly-time.
Set L is in NP if each x in L has a short
“proof of membership” that can be verified
in poly-time.
Fact: P NP
Question: Does NP P ?
72
The relationship of P to NP
decision problems
NP P
73
Observation:
P NP
Deterministic Non-Deterministic
Polynomial Polynomial
75
Open Problem: P NP ?
Example: Does the Satisfiability problem
have a polynomial time
deterministic algorithm?
77
Example: The Hamiltonian cycle problem: is there a
tour that goes through each vertex in the graph
exactly once?
78
Why Care?
NP Contains Lots of Problems We Don’t Know
to be in P.
Some of them are:
Hamiltonian cycle
Clique
Classroom Scheduling
Packing objects into bins
Scheduling jobs on machines
Finding cheap tours visiting a subset of cities
Allocating variables to registers
Finding good packet routings in networks
Decryption
… 79
To reason about the P=NP problem?
How do I do that?
It may take a long time!
Also, what if I forgot one of the sets in NP?
80
We can describe just one problem L in NP,
such that if this problem L is in P,
then NP P.
81