You are on page 1of 74

# COMP4600: Lectures 09-11

Complexity Theory
Beata Faller
Lecture Notes to Chapter 34 of
Cormen, Leiserson, Rivest, Stein:
Introduction to algorithms
2012
Complexity Theory
Computational Complexity Theory studies how difcult algorithmic problems are.
Given an algorithmic problem, we may ask are:
Can we solve the problem efciently?
What running time can we achieve?
When do we call an algorithm efcient?
Why have no fast algorithms been developed ? (For some problems)
COMP4600: Lectures 09-11 2012 1
NP-completeness
NP-completeness is concerned with polynomial running time.
Due to the lack of lower bounds, we compare algorithms to each other.
Some problems are harder than many others. This indicates that there might not be
fast algorithms that solve them.
Showing that problems are hard requires tools that are different from those used in
the design of efcient algorithms. Complexity theory provides these tools.
COMP4600: Lectures 09-11 2012 2
NP-completeness
Outline
1. Polynomial time: Informal and formal notions, the class P
2. Polynomial time verication: Verication algorithms, the class NP
3. NP-complete problems and Reductions: What is NP-completeness, what are
reductions, Circuit-SAT (an NP-complete problem)
4. NP-completeness proofs: SAT, 3-CNF SAT, reduction techniques
5. NP-complete problems: examples
COMP4600: Lectures 09-11 2012 3
34.1 Polynomial time
For various problems, we know polynomial time algorithms:
Sorting: O(nlogn)
Shortest path: O(m+nlogn)
Matrix Multiplication: O(n

) O(n
3
)
Max-Flow, Min-Cut: O(n
3
)
Primality: O((logn)
7
)
Minimum Spanning Tree: O(m+nlogn)
COMP4600: Lectures 09-11 2012 4
34.1 Polynomial time
An algorithm has polynomial running time if it runs in time O(n
k
) for some xed k.
Recall that O(a
1
n
d
+a
2
n
d1
+. . . +a
d1
n+a
d
) O(n
d
).
We consider polynomial time algorithms as tractable. (This is arguably not
appropriate for running times of (n
100
).)
Advantage of considering polynomial time:
Invariant under many computational models (RAM, Turing machine, parallel
computation with polynomially many processors)
Closed under: addition, multiplication, composition.
COMP4600: Lectures 09-11 2012 5
34.1 Polynomial time: Abstract problems
What is a (computational) problem?
An abstract problem has a set of instances I, and a set of solutions S. For every
instance there is a solution.
Instance of SHORTEST-PATH problem: a graph, a source vertex s and a target
vertex t.
Solution of SHORTEST-PATH problem: sequence of vertices forming a shortest path
from s to t.
Note: There may be more than one solution to an instance.
COMP4600: Lectures 09-11 2012 6
34.1 Polynomial time: Abstract problems
We will only consider decision problems.
A decision problem is a problem with solution set S = No,Yes (equivalently 0, 1).
Decision problem associated with SHORTEST-PATH:
Instance i = G, s, t, k asks: Is there a path in G of length at most k from s to t?
Every optimization problem is associated with a decision problem.
Solving the optimization problem is at least as hard as solving the decision problem.
COMP4600: Lectures 09-11 2012 7
34.1 Polynomial time: Encodings
For a computer, problem instances must be encoded.
An encoding maps a set of abstract objects S to strings over an alphabet.
Examples of encodings of N:
Binary encoding: 0 0, 1 1, 2 10, 3 11, . . .
Unary encoding: 0 , 1 1, 2 11, 3 111, . . .
COMP4600: Lectures 09-11 2012 8
34.1 Polynomial time: Encodings
A concrete problem is a problem whose instance set is the set of binary
strings 0, 1

.
An algorithm solves a problem in time O(T(n)), if for all inputs of length n the
algorithm produces a solution in O(T(n)) time steps.
A problem is polynomial-time solvable if there is an algorithm that solves it in
polynomial time (i.e., O(n
k
) for some xed k).
The complexity class P is the set of concrete decision problems that are solvable in
polynomial time.
COMP4600: Lectures 09-11 2012 9
34.1 Polynomial time: Encodings
Via encodings, abstract problems can be turned into concrete problems.
However, polynomial-time solvability may depend on the encoding that is used.
Example: Unary vs. binary encoding: Input length k vs logk. An algorithm that runs
in O(k) for unary runs in O(2
logk
) for binary.
In practice (usually) reasonable encodings turn out to be equivalent.
A function f : 0, 1

0, 1

## is polynomial-time computable if there is a

polynomial-time algorithm that computes f (x) when given x as input.
Two encodings are polynomially related if there are polynomial-time computable
functions f
12
and f
21
that transform the encodings into each other.
Example: Graphs given as adjacency list or adjacency matrix.
COMP4600: Lectures 09-11 2012 10
34.1 Polynomial time: Encodings
For an encoding e of an abstract problem Q we dene e(Q) as the concrete problem
associated with Q.
Lemma 1 (34.1). If e
1
and e
2
are two polynomially related encodings of an abstract
decision problem Q, then e
1
(Q) P if and only if e
2
(Q) P.
Proof. Only need to show one direction. Suppose e
1
(Q) can be solved in time O(n
k
).
Given an encoding e
2
(i) of instance i we can compute the encoding e
1
(i)
in O(n
c
) = O([e
2
(i)[
c
).
To solve e
2
(Q) we proceed as follows:
Given an encoded instance e
2
(i) compute e
1
(i). We know [e
1
(i)[ O([e
2
(i)[
c
).
Use the algorithm solving e
1
(Q) on instance e
1
(i). This takes time at
most O([e
1
(i)[
k
) O

O([e
2
(i)[
c
)

O([e
2
(i)[
ck
).
COMP4600: Lectures 09-11 2012 11
34.1 Polynomial time: A formal-language framework
Concrete decision problems can be treated using formal languages.
An alphabet is a nite set of symbols.
A language L over is a subset of strings over (i.e., L

).
The empty string is .
Operations on Languages
union, intersection
complement: L =

L
concatenation of L
1
and L
2
is L
1
L
2
= x
1
x
2
: x
1
L
1
and x
2
L
2

## closure (or Kleene star): L

= LLLLLL. . .
COMP4600: Lectures 09-11 2012 12
34.1 Polynomial time: A formal-language framework
For a concrete decision problem Q we dene the Language L as the set of instances
L = x

: Q(x) = 1.
Algorithms vs Languages:
An algorithm A accepts a string if A(x) = 1.
An algorithm A rejects a string if A(x) = 0.
The language accepted by an algorithm A is L = x

: A(x) = 1.
COMP4600: Lectures 09-11 2012 13
34.1 Polynomial time: A formal-language framework
A language L is decided by an algorithm A, if every string in L is accepted by A and
every string not in L is rejected by A.
Algorithms do not always halt. Thus there is a difference between A decides a
language and A accepts a language.
An algorithm accepts L in polynomial time if A accepts L and for all x L algorithm A
accepts x in O(n
k
) (for some constant k).
An algorithm decides L in polynomial time if A decides L and for all x 0, 1

## algorithm A decides whether x L in O(n

k
) (for some constant k).
COMP4600: Lectures 09-11 2012 14
34.1 Polynomial time: A formal-language framework
Example Problem:
INPUT: a linked list.
TASK: decide whether starting at the root one reaches a NIL pointer.
An algorithm: Start at the root and repeatedly jump to the position of the next pointer.
If NIL is reached accept.
The algorithm accepts all inputs for which NIL can be reached starting from the root.
It accepts in polynomial time but it does not decide in polynomial time.
COMP4600: Lectures 09-11 2012 15
34.1 Polynomial time: A formal-language framework
Recall: The complexity class P is the set of concrete decision problems that are
solvable in polynomial time.
We obtain an alternative denition:
P = L 0, 1

## : there is an algorithm that decides L in polynomial time.

The languages that can be decided in polynomial time are exactly the languages that
can be accepted in polynomial time:
Theorem 1 (34.2).
P = L 0, 1

## : there is an algorithm that accepts L in polynomial time

COMP4600: Lectures 09-11 2012 16
34.1 Polynomial time: A formal-language framework
Proof. If a language is decided in polynomial time, it is accepted in polynomial time.
Thus, we only need to show one subset inclusion.
Need to show: If a language L is accepted in polynomial time by some algorithm A, it
is also decided by some algorithm A
/
in polynomial time.
A accepts in time O(n
k
), thus there is a c N such that A accepts in time at most cn
k
.
We design A
/
as follows: Simulate algorithm A for cn
k
steps keeping track of the
number of steps that have been performed. If A has accepted, then accept.
Otherwise reject.
Correctness: Algorithm A
/
decides L.
Running time: Simulating A takes only a constant factor longer than running A itself.
Thus A
/
has polynomial running time.
COMP4600: Lectures 09-11 2012 17
34.2 Polynomial-time verication
Some problems are hard to solve, but they are easy to verify when a certicate is
Examples:
Factorization: Given a set of integers, it is easy to verify they are the prime
factors of some number n.
Traveling salesman problem: Given a walk in an edge-weighted graph, it is easy
to verify that it goes through all the nodes and has length no greater than l.
Maximum clique: Given a set of vertices of a graph, it is easy to verify that they
form a clique.
Chromatic number: Given a colouring of the vertices of a graph, it is easy to
verify that it is a proper k-colouring.
COMP4600: Lectures 09-11 2012 18
34.2 Polynomial-time verication
Example: The problem HAMILTON-CYCLE.
INPUT: A graph.
TASK: Find a simple cycle in the input graph that includes every vertex.
If such a cycle exists, the graph is called Hamiltonian.
COMP4600: Lectures 09-11 2012 19
34.2 Polynomial-time verication
Cormen et al., p1062
COMP4600: Lectures 09-11 2012 20
34.2 Polynomial-time verication
HAMILTON-CYCLE can be veried in linear time.
How fast can we decide HAMILTON-CYCLE?
The brute force algorithm takes (n!) = ((

m)!).
Better algorithms are known:
O(2
n
) [Held, Karp] (1962)
O(1. 657
n
) [Bj orklund] (2010)
No polynomial time algorithm is known.
COMP4600: Lectures 09-11 2012 21
34.2 Polynomial-time verication: Verication algorithms
A verication algorithm is an algorithm A that takes two arguments:
a normal input string x
a string y called a certicate
A veries x if there exists a certicate y such that A(x, y) = 1.
The language veried by A is
L = x 0, 1

: y 0, 1

## such that A(x, y) = 1.

Example: A Hamiltonian cycle serves as a certicate that a graph is Hamiltonian.
COMP4600: Lectures 09-11 2012 22
34.2 Polynomial-time verication: The Complexity Class NP
The complexity class NP is the class of languages that can be veried in
polynomial-time.
Thus: L NP if there exists a verication algorithm running in polynomial time and a
constant c such that
L = x 0, 1

## : y with [y[ O([x[

c
) and A(x, y) = 1.
HAMILTON-CYCLE NP.
Observation: P NP
Proof: For L P there is a polynomial time algorithm A that decides L. Using no
certicate (i.e., y = ) this algorithm veries L in polynomial time.
It is unknown whether P = NP.
COMP4600: Lectures 09-11 2012 23
34.2 Polynomial-time verication: The Complexity Class NP
The complexity class co-NP is the class of languages L such that L NP.
Open problems concerning NP:
P = NP?
L NP L NP (equivalent co-NP = NP)?
P = NPco-NP?
COMP4600: Lectures 09-11 2012 24
34.2 Polynomial-time verication: The Complexity Class NP
Cormen et al., p1065
COMP4600: Lectures 09-11 2012 25
34.3 NP-completeness and reducibility
Informally NP-complete problems are the hardest problems in NP.
If some NP-complete problem is in P then P = NP.
A formal denition requires the notion of a reduction.
Language L
1
polynomial-time reduces to L
2
if there exists a polynomial-time
computable function f : 0, 1

0, 1

## such that for all x 0, 1

x L
1
if and only if f (x) L
2
.
Write L
1

p
L
2
.
COMP4600: Lectures 09-11 2012 26
34.3 NP-completeness and reducibility
Cormen et al., p1068
COMP4600: Lectures 09-11 2012 27
34.3 NP-completeness and reducibility
Lemma 2 (34.3). Let L
1
, L
2
0, 1

be two languages.
If L
1

p
L
2
then L
2
P L
1
P.
Proof.
A
2
: polynomial-time algorithm that decides L
2
.
f : polynomial-time computable reduction reducing L
1
to L
2
.
We need: A
1
, a polynomial-time algorithm that decides L
1
.
Given x 0, 1

## compute f (x). Run A

2
to decide f (x) L
2
. If f (x) L
2
then output
Yes, otherwise output No.
Correctness: By the denition of reduction.
Running time: [ f (x)[ is polynomial in [x[ and A
2
runs in polynomial time, thus the
total running time is polynomial in [x[.
COMP4600: Lectures 09-11 2012 28
34.3 NP-completeness and reducibility
Cormen et al., p1069
COMP4600: Lectures 09-11 2012 29
34.3 NP-completeness and reducibility: NP-completeness
A language L is NP-complete if
L NP and
L
/

p
L for every L
/
NP.
If we only know that the second property holds then L is called NP-hard.
NPC is the class of all NP-complete languages.
COMP4600: Lectures 09-11 2012 30
34.3 NP-completeness and reducibility: NP-completeness
Theorem 2 (34.4). If some NP-complete problem is in P then P = NP.
Proof. Suppose L P and L NPC.
Let L
/
NP be any language.
Since L NPC we know L
/

p
L.
Thus (by Lemma 2 (34.3)) we conclude L
/
P.
COMP4600: Lectures 09-11 2012 31
34.3 NP-completeness and reducibility: Circuit satisability
Do NP-complete problems exist?
A rst NP-complete problem: Circuit satisability.
Boolean combinational circuits are built from basic building blocks.
We consider building blocks with 1 or 2 inputs (wires) and 1 output, they are logic
gates.
The gates are:
NOT gate
AND gate
OR gate
COMP4600: Lectures 09-11 2012 32
34.3 NP-completeness and reducibility: Circuit satisability
Cormen et al., p1071
COMP4600: Lectures 09-11 2012 33
34.3 NP-completeness and reducibility: Circuit satisability
A boolean circuit consists of several gates that are connected by wires.
We consider only circuits that have one output. (decision problem)
A truth assignment is a set of values for all inputs of a circuit.
A circuit is satisable if it has a truth assignment that produces an output value 1.
Circuits can be encoded in a similar fashion as graphs. Their size is the number of
gates plus the number of wires used.
COMP4600: Lectures 09-11 2012 34
34.3 NP-completeness and reducibility: Circuit satisability
Cormen et al., p1072
COMP4600: Lectures 09-11 2012 35
34.3 NP-completeness and reducibility: Circuit satisability
Lemma 3 (34.5). Circuit satisability is in NP
Proof. We need: Algorithm A that veries circuit satisability in polynomial time.
Input: Encoding of a circuit. Certicate: Truth assignment for all wires.
Algorithm A: For each logic gate in the circuit compute the output of the gate and
check whether the output wire has the correct assignment. If the output is correct for
every gate, and the output wire of the whole circuit is 1 then output 1. Otherwise,
output 0.
Correctness If a circuit is satisable, then there is an assignment of the wires that will
make algorithm A output 1. If a circuit is not satisable, then every assignment for
the wires is inconsistent with the gates, or the output is 0.
Running time The running time is polynomial in the number of gates, thus polynomial
in the input size.
COMP4600: Lectures 09-11 2012 36
34.3 NP-completeness and reducibility: Circuit satisability
Circuit satisability can be veried in O(n) (where n = size of the circuit).
Circuit satisability can be decided in O(2
k
n) (when k = the number of inputs) using
brute force.
COMP4600: Lectures 09-11 2012 37
34.3 NP-completeness and reducibility: Circuit satisability
How algorithms work depends on the machine model we use.
The memory of a typical computer contains:
the input,
space assigned for the output,
a program counter keeping track of the instructions to be performed, and
information written to the program counter that controls the program ow.
The entirety of all memory at any point in time is a conguration.
An execution of an instruction maps a conguration to another conguration.
The hardware that performs the change of conguration can be simulated by a
boolean circuit.
COMP4600: Lectures 09-11 2012 38
34.3 NP-completeness and reducibility: Circuit satisability
Lemma 4 (34.6). Circuit satisability is NP-hard.
Proof sketch. Let L NP be a language. We need to construct a polynomial time
reduction F from L to CIRCUIT-SAT.
There is an algorithm A that veries L in time O(n
k
), for some k.
We break the congurations of algorithm A into different parts: The program for A,
the program counter and auxiliary machine state, the input x, the certicate y and the
working storage.
The circuit M simulates the hardware which transforms the congurations into one
another.
The congurations over time are simulated by a sequence of O(n
k
) circuits.
COMP4600: Lectures 09-11 2012 39
34.3 NP-completeness and reducibility: Circuit satisability
Cormen et al., p1075
COMP4600: Lectures 09-11 2012 40
34.3 NP-completeness and reducibility: Circuit satisability
The input variables of the circuit produced by F are the corresponding inputs in the
rst circuit that correspond to the storage of y.
The output is a specic output contained in the last circuit of the family.
The reduction F outputs the circuit.
Correctness: We need to show that the output circuit is satisable if and only if there
exists a y such that A(x, y) = 1. If such a y exists, then the assignment of y to the
inputs of the circuit produces a 1 at the output bit, since A terminates with output 1 in
at most O(n
k
) steps.
If C is satisable then a satisfying assignment y is a certicate for x.
COMP4600: Lectures 09-11 2012 41
34.3 NP-completeness and reducibility: Circuit satisability
Running time: The number of bits in a conguration is polynomial in n: An algorithm
that runs in O(n
k
) can use at most O(n
k
) bits of the memory.
The combinational circuit implementing M has size polynomial in the length of a
conguration. The conguration has size O(n
k
), thus the circuit for M has size
polynomial in n.
The total circuit contains O(n
k
) copies of M. Thus, the total size is polynomial in n.
Each construction step takes polynomial time, thus the entire circuit can be
constructed in polynomial time.
COMP4600: Lectures 09-11 2012 42
34.3 NP-completeness and reducibility: Circuit satisability
Theorem 3 (34.7). Circuit satisability is NP-complete.
Proof. Lemma 3 and 4 together give the theorem.
COMP4600: Lectures 09-11 2012 43
34.4 NP-completeness proofs
Knowing one NP complete problem allows us to derive more NP-complete problems:
Theorem 4 (34.8). If L is a language and L
/

p
L for a language L
/
NPC then L
is NP-hard. If additionally L NP then L NPC.
Proof. L
/
is NP-complete. Thus for all languages L
//
NP we have L
//

p
L
/
L,
thus L
//

p
L.
Therefore L is NP-hard.
If additionally L NP then L is NP-complete
COMP4600: Lectures 09-11 2012 44
34.4 NP-completeness proofs
How to prove that a language L is NP-complete?
1. Show L NP.
2. Choose an language L
/
NPC.
3. Design a function f that maps instances of L
/
to instances of L.
4. Prove that f is a reduction: show for all x 0, 1

x L
/
if and only if f (x) L.
5. Prove that f can be computed in polynomial time.
This method is called reduction [Karp (1972)].
COMP4600: Lectures 09-11 2012 45
34.4 NP-completeness proofs: Formula satisability
We use the reduction technique to show that satisability of boolean formulas is
NP-complete.
A SAT instance consists of:
n boolean variables x
1
, x
2
, . . . x
n
,
m boolean operators taken from: (AND), (OR), (NOT),
(implication), (if and only if)
parentheses (w.l.o.g we assume the number of parentheses is O(m))
A SAT formula is satisable, if there exists a truth assignment for the variables, such
that the formula evaluates to 1.
COMP4600: Lectures 09-11 2012 46
34.4 NP-completeness proofs: Formula satisability
Example instances of SAT:
(x
1
x
2
) x
3
:
satisable with x
1
= 0, x
2
= 0, x
3
= 0
((x
1
x
2
) ((x
1
x
3
) x
4
)) x
2
:
satisable with x
1
= 0, x
2
= 0, x
3
= 1, x
4
= 1
(x
1
x
2
) (x
1
x
2
):
not satisable
The naive algorithm takes (2
n
).
COMP4600: Lectures 09-11 2012 47
34.4 NP-completeness proofs: Formula satisability
Theorem 5 (34.9, Cook (1976)). SAT (Satisability of boolean formulas) is
NP-complete.
Proof. 1. SAT NP
We show: SAT can be veried in polynomial time. Certicate: Satisfying assignment.
The verication algorithm: Replace each variable with its truth value; recursively
(from inside out) evaluate each expression. If the nal value is 1 then accept.
2. We reduce from the language CIRCUIT-SAT.
3. We design a function that transforms a circuit into a SAT formula.
Each input is a variable. Additionally each wire is a variable. For each gate we
design a clause, a small formula that expresses the logic of the gate.
We combine all these formulas and an additional formula consisting only of the
variable for the output wire using AND connectives.
COMP4600: Lectures 09-11 2012 48
34.4 NP-completeness proofs: Formula satisability
Cormen et al., p1081
= x
10
(x
4
(x
3
))
(x
5
(x
1
x
2
))
(x
6
(x
4
))
(x
7
(x
1
x
2
x
4
))
(x
8
(y
5
x
6
))
(x
9
(x
6
x
7
)))
(x
10
(x
7
x
8
x
9
))
COMP4600: Lectures 09-11 2012 49
34.4 NP-completeness proofs: Formula satisability
4. The constructed formula f (C) is satisable, if and only if the circuit C is satisable:
If C has a satisfying assignment, using this assignment we get a truth value for each
internal wire. Assigning this value to each corresponding variable of the SAT formula
gives a satisfying assignment.
If f (C) is satisable, using the truth assignment of the input variables gives a
satisfying assignment for the circuit C.
5. The reduction can be computed in polynomial time.
For each gate we can construct the corresponding formula in time linear in the size
of the gate. Joining the formulas takes linear time in the number of gates.
COMP4600: Lectures 09-11 2012 50
34.4 NP-completeness proofs: 3-CNF SAT
SAT is a useful problem to prove that other problems are NP-complete.
To make the reductions easier, we show that a restricted version: 3-CNF SAT is
NP-complete.
3-CNF SAT (3-conjunctive normal form satisability) is the language that consists of
satisable boolean formulas which are created in the following way:
Create a set of literals of the variables [e.g., x
1
or x
3
],
form OR clauses that each contain three literals [e.g., (x
3
x
2
x
6
)],
form a conjunction (using AND) of the clauses.
Example:
(x
1
x
1
x
2
) (x
3
x
2
x
4
) (x
1
x
3
x
4
)
COMP4600: Lectures 09-11 2012 51
34.4 NP-completeness proofs: 3-CNF SAT
Theorem 6 (34.10). 3-CNF SAT is NP-complete.
Proof. 1. 3-CNF SAT NP
It is easy to verify whether a formula is in 3-conjunctive normal form. Thus,
since SAT NP, we conclude 3-CNF SAT NP.
2. We reduce from the language SAT.
3. We design a function that transforms a SAT formula into a 3-CNF SAT formula.
For this, we design a parse tree from a given formula.
COMP4600: Lectures 09-11 2012 52
34.4 NP-completeness proofs: 3-CNF SAT
Example: = ((x
1
x
2
) ((x
1
x
3
) x
4
)) x
2
Cormen et al., p1083
By associativity, using parenthesize, we can construct a binary tree from a formula.
COMP4600: Lectures 09-11 2012 53
34.4 NP-completeness proofs: 3-CNF SAT
For each node in the tree we assign a variable and design a clause that says: the
nodes truth value is computed from the children according to the operation of the
node.
We form the conjunction of all these clauses together with a clause for the root.

/
= y
1
(y
1
(y
2
x
2
))
(y
2
(y
3
y
4
))
(y
3
(x
1
x
2
))
(y
4
y
5
)
(y
5
(y
6
x
4
))
(y
6
(x
1
x
3
)))
We need to transform the clauses into 3-CNF formulas.
COMP4600: Lectures 09-11 2012 54
34.4 NP-completeness proofs: 3-CNF SAT
For a formula
/
i
that has three variables, we build its truth-table. We form a DNF
formula (OR of ANDs) that is equivalent to
/
i
: For this we use all entries of the truth
table that evaluate to 0, use the appropriate literals and form their conjunction. The
disjunction of these clauses is equivalent to
/
i
.
Example:

/
1
= (y
1
(y
2
x
2
))
y
1
y
2
x
2
(y
1
(y
2
x
2
))
1 1 1 0
1 1 0 1
1 0 1 0
1 0 0 0
0 1 1 1
0 1 0 0
0 0 1 1
0 0 0 1

/
1

(y
1
y
2
x
2
) (y
1
y
2
x
2
) (y
1
y
2
x
2
) (y
1
y
2
x
2
)

.
COMP4600: Lectures 09-11 2012 55
34.4 NP-completeness proofs: 3-CNF SAT

(y
1
y
2
x
2
) (y
1
y
2
x
2
) (y
1
y
2
x
2
) (y
1
y
2
x
2
)

## DeMorgans laws say:

(ab) = ab
(ab) = ab
Using the laws we translate the DNFs into CNFs

(y
1
y
2
x
2
) (y
1
y
2
x
2
) (y
1
y
2
x
2
) (y
1
y
2
x
2
)

(y
1
y
2
x
2
) (y
1
y
2
x
2
) (y
1
y
2
x
2
) (y
1
y
2
x
2
)
COMP4600: Lectures 09-11 2012 56
34.4 NP-completeness proofs: 3-CNF SAT
Each constructed clause has at most 3 variables.
It remains to ensure that each clause has at least three variables.
For this we use dummy variables p and q.
A clause with two variables (l
1
l
2
) is transformed into
(l
1
l
2
p) (l
1
l
2
p).
A clause with one variable (l
1
) is transformed into
(l
1
q p) (l
1
qp) (l
1
q p) (l
1
qp).
COMP4600: Lectures 09-11 2012 57
34.4 NP-completeness proofs: 3-CNF SAT
4. We need to show that the produced formula is satisable, if and only if the original
formula is satisable.
The rst step that constructs a formula using a binary tree preserves satisability.
The second and third step transform the formula into one that is algebraically
equivalent.
5. The reduction can be performed in polynomial time:
Using the binary search tree we construct one variable and one clause per
connective (node in the binary tree).
Using the truth table every clause is replaced by at most 8 clauses in disjunctive
normal form. Their number does not change using DeMorgans laws.
The dummy variables turn a clause into at most 4 new clauses.
The total number of clauses is linear in the number original variables and
connectives. Each step takes polynomial time.
COMP4600: Lectures 09-11 2012 58
34.5 NP-complete problems
NP-complete problems occur in many areas of computer science and mathematics.
Several thousand are known, the list still growing.
Knowing a variety of NP-complete problems is helpful, since each NP-complete
problem can be used to show that other problems are NP-complete.
COMP4600: Lectures 09-11 2012 59
34.5 NP-complete problems: The clique problem
Given a graph G and an integer k, the clique problem asks whether there exists a
clique of size k in G (i.e., k pairwise adjacent vertices.)
A naive algorithm tries all possible subsets of size k, and thus takes (k
2

n
k

) time.
This is exponential in n when k n/2.
Theorem 7 (34.11). The clique problem is NP-complete
COMP4600: Lectures 09-11 2012 60
34.5 NP-complete problems: The clique problem
Proof.
1. Given a graph and a subset of k vertices, we can verify in time O(k
2
) O(n
2
) that
the vertices form a clique. Thus the clique problem is in NP.
2. We reduce 3-CNF-SAT to the clique problem.
3. Let = C
1
C
2
. . . C
k
be a 3-CNF-SAT formula. Each clause has 3 literals.
For each clause C
r
= (l
r
1
l
r
2
l
r
3
) add three vertices v
r
1
, v
r
2
, v
r
3
to the graph.
Two vertices are joined by an edge, if they originate from different triples and they
are not negations of each other.
The reduction outputs the graph and the integer k.
5. The graph can be computed in polynomial time, it has as many vertices as there
are literals in the original formula.
COMP4600: Lectures 09-11 2012 61
34.5 NP-complete problems: The clique problem
Example: = (x
1
x
2
x
3
) (x
1
x
2
x
3
) (x
1
x
2
x
3
)
Cormen et al., p1088
COMP4600: Lectures 09-11 2012 62
34.5 NP-complete problems: The clique problem
4. We show the transformation is a reduction.
If is a satisable formula, there is a satisfying assignment.
Each clause contains at least one true literal. Pick a true literal for each clause.
Let V
/
be the set of k vertices corresponding to the literals.
Claim: The set V
/
forms a clique.
We have to show: any two distinct vertices v
r
i
, v
s
j
V
/
We know i ,= j, since we only pick one literal from each clause.
The literals are not negations of each other, since there is an assignment that
satises both. Thus the corresponding vertices are adjacent.
COMP4600: Lectures 09-11 2012 63
34.5 NP-complete problems: The clique problem
Conversely, suppose V
/
is a clique of size k in G.
All vertices V
/
must be in different triples. Thus there is one vertex per triple. Assign
truth values such that each literal corresponding to a vertex in V
/
becomes true. This
is possible since no variable appears negated and unnegated.
Under this assignment each clause is true, thus the formula is true.
Thus the graph has a clique of size k if and only if the clause is satisable.
COMP4600: Lectures 09-11 2012 64
34.5 NP-complete problems: The vertex-cover problem
A vertex cover of a graph is a subset V
/
of the vertices such that for all edges at
least one endpoint is in V
/
.
The vertex-cover problem asks, given a graph G and an integer k, whether there
exists a vertex cover of size k.
Theorem 8 (34.12). Vertex-cover is NP-complete
COMP4600: Lectures 09-11 2012 65
34.5 NP-complete problems: The vertex-cover problem
Proof.
1. Given a subset V
/
of the vertices we can check in O(n
2
) whether V
/
is a vertex
cover.
Thus vertex-cover is in NP.
2. We reduce the clique problem to the vertex-cover problem.
3. Given a graph G and integer k (input for clique problem) we output G and nk.
G is the complement of G.
COMP4600: Lectures 09-11 2012 66
34.5 NP-complete problems: The vertex-cover problem
4. Let V
/
be a clique of size k in G. Then V V
/
is a vertex cover of G: If e is an edge
in G, then not both endpoints are in V
/
, thus e is covered by V V
/
in G.
Conversely, if V
/
is a vertex cover in G, of size [V[ k then, then V V
/
is a clique of
size k in G: Suppose u and v are both vertices in V V
/
and they are not-adjacent
in G. Then (u, v) is an edge in G. Neither u nor v is in V
/
, but V
/
is a vertex cover, a
5. Computing the complement of a graph can be performed in O(n
2
).
COMP4600: Lectures 09-11 2012 67
34.5 NP-complete problems: Hamiltonian Cycle
Recall the Hamiltonian cycle problem which asks whether there exists a simple cycle
through all vertices in an input graph.
Theorem 9 (34.13). Hamiltonian Cycle is NP-complete.
1. We already know HAMILTON-CYCLE NP.
2. We reduce from vertex-cover.
3. For each edge in the input graph G we use a widget, these widgets are then
assembled with selector nodes to form a graph.
COMP4600: Lectures 09-11 2012 68
34.5 NP-complete problems: Hamiltonian Cycle
Cormen et al., p1092
COMP4600: Lectures 09-11 2012 69
34.5 NP-complete problems: Hamiltonian Cycle
Cormen et al., p1093
COMP4600: Lectures 09-11 2012 70
34.5 NP-complete problems: Hamiltonian Cycle
4. Facts:
The widgets can only be traversed in the 3 ways indicated.
If an edge is traversed from a selector vertex to a widget W
uv
entering at [u, v, 1] then
all widgets corresponding to an edge incident with u have to be traversed
subsequently.
We need to show: The original graph has a vertex cover of size k if and only if there
is a Hamiltonian cycle in the constructed graph.
Given a vertex cover we traverse each widget depending on whether the edge is
covered by both endpoints or not. If each edge is covered then each widget is
traversed.
COMP4600: Lectures 09-11 2012 71
34.5 NP-complete problems: Hamiltonian Cycle
Given a Hamiltonian cycle we choose a set of vertices according to the
widget-vertices to which the selector vertices are connected by the cycle. This gives
us a vertex cover.
5. The number of widgets is equal to the number of edges in the original graph. A
widget has constant size. The number of edges in the new graph is quadratic in the
number of vertices, which is linear in the number of widgets (no multi-edges). The
construction can be performed in polynomial time.
COMP4600: Lectures 09-11 2012 72
34.5 NP-complete problems: Traveling Salesman
For a graph in which all edges are given a length (i.e., a distance between endpoints)
the traveling-salesman problem asks for the shortest tour that visits all vertices.
Theorem 10 (34.14). The traveling salesman problem is NP-hard.
Proof sketch. Reduction from Hamiltonian cycle.
Given a graph G form the complete graph K
n
and assign where to each edge weight
1 if it was an edge in G and weight 2 otherwise.
Show that the new graph has a tour of length n if and only if the original graph has a
Hamiltonian cycle.
COMP4600: Lectures 09-11 2012 73