You are on page 1of 34

Unit-4

Bikash Ranjan Bag 6 12/4/2016


● Some problems are intractable:
as they grow large, we are unable to solve
them in reasonable time or in polynomial time
■ On an input of size n the worst-case running time
is O(nk) for some constant k
■ Polynomial time: O(n2), O(n3), O(1), O(n lg n)
■ Not in polynomial time: O(2n), O(nn), O(n!)

Bikash Ranjan Bag 6 12/4/2016


P and NP
● As mentioned, P is set of problems that can be solved in
polynomial time
● NP (nondeterministic polynomial time) is the set of problems
that can be solved in polynomial time by a nondeterministic
computer
● The problem belongs to class P if it’s easy to find a solution
for the problem. The problem belongs to NP, if it’s difficult to
find a solution.
● P is subset of NP

Bikash Ranjan Bag 6 12/4/2016


Polynomial Time algorithms

● Linear Search-O(n)
● Binary Search- O(lg n)
● Insertion Sort-O(n2)
● Merge Sort- O(n lg n)
● Matrix Chain Multiplication-O(n3)

Bikash Ranjan Bag 6 12/4/2016


Non Deterministic Polynomial Time

● 0/1 Knapsack- O(2n)


● Travelling salesman- O(2n)
● Sum of subset- O(2n)
● Graph coloring- O(2n)
● Hamiltonian cycle- O(2n)

Bikash Ranjan Bag 6 12/4/2016


P and NP

● P = problems that can be solved in polynomial


time
● NP = problems for which a solution can be
verified in polynomial time
● Hamiltonian-cycle problem is in NP:
■ Cannot solve in polynomial time
■ Easy to verify solution in polynomial time

Bikash Ranjan Bag 6 12/4/2016


● An example of an NP-Complete problem:
■ A Hamiltonian cycle of an undirected graph is a
simple cycle that contains every vertex
■ The Hamiltonian-cycle problem: given a graph G,
does it have a Hamiltonian cycle?

Bikash Ranjan Bag 6 12/4/2016


Reduction
● The most important thing of NP-Completeness is reducibility
■ Informally, a problem P can be reduced to another problem Q if any instance of P
can be easily transform as an instance of Q, the solution to which provides a
solution to the instance of P
■ Intuitively: If P reduces to Q, P is “no harder to solve” than Q
● Reduction means if there is any kind of relationship or similarity between two
problems. So if problem A can be solved in polynomial time, then problem B
can also be solved in polynomial time. Similarly if problem A can be solved
in non-deterministic polynomial(NP) time, then problem B also can be solved
in non-deterministic polynomial(NP) time.
● The important thing is that finding the relation between problem A and B
should be in polynomial time.

Bikash Ranjan Bag 6 12/4/2016


Reducibility

● An example:
■ P: Given a set of Booleans, is at least one TRUE?
■ Q: Given a set of integers, is their sum positive?
■ Transformation: (x1, x2, …, xn) = (y1, y2, …, yn)
where yi = 1 if xi = TRUE, yi = 0 if xi = FALSE

Bikash Ranjan Bag 6 12/4/2016


NP-Hard and NP-Complete
● NP-Hard :
■ If a problem can’t be solved in non deterministic polynomial time, then the problem is
called NP-hard problem.
■ Let a problem “M” is NP-hard. If a problem “N” is reduced to M, then the problem N
also becomes a NP-hard problem.
● NP-Complete
■ If the NP-hard problem can be solved in non-deterministic polynomial time, then the
problem is called NP-complete.

David Luebke 10 07-Mar-21


● As we have discussed, P is sub set of NP.
There might be a situation where P=NP,
means in future, we may have polynomial
time(P) solution of a NP problem.

Bikash Ranjan Bag 6 12/4/2016


NP-Complete problems
● Boolean satisfiability problem (SAT)
● Knapsack problem
● Hamiltonian path problem
● Subgraph isomorphism problem
● Subset sum problem
● Clique problem
● Independent set problem
● Dominating set problem
● Graph coloring problem

Bikash Ranjan Bag 6 12/4/2016


Proving NP-Completeness

● What steps do we have to take to prove a


problem P is NP-Complete?
■ Pick a known NP-Complete problem Q
■ Reduce Q to P
○ Describe a transformation that maps instances of Q to
instances of P, s.t. “yes” for P = “yes” for Q
○ Prove the transformation works
○ Prove it runs in polynomial time
■ Oh yeah, prove P  NP (What if you can’t?)

David Luebke 13 07-Mar-21


Hamiltonian Cycle  TSP

● The well-known traveling salesman problem:


■ Optimization variant: a salesman must travel to n
cities, visiting each city exactly once and finishing
where he begins. How to minimize travel time?
■ Model as complete graph with cost c(i,j) to go from
city i to city j
● How would we turn this into a decision
problem?
■ A: ask if  a TSP with cost < k

David Luebke 14 07-Mar-21


Hamiltonian Cycle  TSP

● The steps to prove TSP is NP-Complete:


■ Prove that TSP  NP (Argue this)
■ Reduce the undirected hamiltonian cycle problem
to the TSP
○ So if we had a TSP-solver, we could use it to solve the
hamilitonian cycle problem in polynomial time
○ How can we transform an instance of the hamiltonian
cycle problem to an instance of the TSP?
○ Can we do this in polynomial time?

David Luebke 15 07-Mar-21


The TSP

● Random asides:
■ TSPs (and variants) have enormous practical
importance
○ E.g., for shipping and freighting companies
○ Lots of research into good approximation algorithms
■ Recently made famous as a DNA computing
problem

David Luebke 16 07-Mar-21


The 3-CNF Problem

● Thm 36.10: Satisfiability of Boolean formulas


in 3-CNF form (the 3-CNF Problem) is NP-
Complete
■ Proof: Nope
● The reason we care about the 3-CNF problem
is that it is relatively easy to reduce to others
■ Thus by proving 3-CNF NP-Complete we can
prove many seemingly unrelated problems
NP-Complete

David Luebke 17 07-Mar-21


3-CNF  Clique

● What is a clique of a graph G?


● A: a subset of vertices fully connected to each
other, i.e. a complete subgraph of G
● The clique problem: how large is the
maximum-size clique in a graph?
● Can we turn this into a decision problem?
● A: Yes, we call this the k-clique problem
● Is the k-clique problem within NP?

David Luebke 18 07-Mar-21


3-CNF  Clique

● What should the reduction do?


● A: Transform a 3-CNF formula to a graph, for
which a k-clique will exist (for some k) iff the
3-CNF formula is satisfiable

David Luebke 19 07-Mar-21


3-CNF  Clique

● The reduction:
■ Let B = C1  C2  …  Ck be a 3-CNF formula
with k clauses, each of which has 3 distinct literals
■ For each clause put a triple of vertices in the graph,
one for each literal
■ Put an edge between two vertices if they are in
different triples and their literals are consistent,
meaning not each other’s negation
■ Run an example:
B = (x  y  z)  (x  y  z )  (x  y  z )
David Luebke 20 07-Mar-21
3-CNF  Clique

● Prove the reduction works:


■ If B has a satisfying assignment, then each clause
has at least one literal (vertex) that evaluates to 1
■ Picking one such “true” literal from each clause
gives a set V’ of k vertices. V’ is a clique (Why?)
■ If G has a clique V’ of size k, it must contain one
vertex in each triple (clause) (Why?)
■ We can assign 1 to each literal corresponding with
a vertex in V’, without fear of contradiction

David Luebke 21 07-Mar-21


Clique  Vertex Cover

● A vertex cover for a graph G is a set of vertices


incident to every edge in G
● The vertex cover problem: what is the
minimum size vertex cover in G?
● Restated as a decision problem: does a vertex
cover of size k exist in G?
● Thm 36.12: vertex cover is NP-Complete

David Luebke 22 07-Mar-21


Clique  Vertex Cover

● First, show vertex cover in NP (How?)


● Next, reduce k-clique to vertex cover
■ The complement GC of a graph G contains exactly
those edges not in G
■ Compute GC in polynomial time
■ G has a clique of size k iff GC has a vertex cover of
size |V| - k

David Luebke 23 07-Mar-21


Clique  Vertex Cover

● Claim: If G has a clique of size k, GC has a


vertex cover of size |V| - k
■ Let V’ be the k-clique
■ Then V - V’ is a vertex cover in GC
○ Let (u,v) be any edge in GC
○ Then u and v cannot both be in V’ (Why?)
○ Thus at least one of u or v is in V-V’ (why?), so
edge (u, v) is covered by V-V’
○ Since true for any edge in GC, V-V’ is a vertex cover

David Luebke 24 07-Mar-21


Clique  Vertex Cover

● Claim: If GC has a vertex cover V’  V, with


|V’| = |V| - k, then G has a clique of size k
■ For all u,v  V, if (u,v)  GC then u  V’ or
v  V’ or both (Why?)
■ Contrapositive: if u  V’ and v  V’, then
(u,v)  E
■ In other words, all vertices in V-V’ are connected
by an edge, thus V-V’ is a clique
■ Since |V| - |V’| = k, the size of the clique is k

David Luebke 25 07-Mar-21


General Comments

● Literally hundreds of problems have been


shown to be NP-Complete
● Some reductions are profound, some are
comparatively easy, many are easy once the
key insight is given
● You can expect a simple NP-Completeness
proof on the final

David Luebke 26 07-Mar-21


Other NP-Complete Problems

● Subset-sum: Given a set of integers, does there


exist a subset that adds up to some target T?
● 0-1 knapsack: when weights not just integers
● Hamiltonian path: Obvious
● Graph coloring: can a given graph be colored
with k colors such that no adjacent vertices are
the same color?
● Etc…

David Luebke 27 07-Mar-21


Boolean satisfiability problem (SAT)

● it asks whether the variables of a given Boolean formula can


be consistently replaced by the values TRUE or FALSE in
such a way that the formula evaluates to TRUE. If this is the
case, the formula is called satisfiable. On the other hand, if no
such assignment exists, the function expressed by the formula
is FALSE for all possible variable assignments and the
formula is unsatisfiable. For example, the formula "a AND
NOT b" is satisfiable because one can find the
values a = TRUE and b = FALSE, which make (a AND
NOT b) = TRUE. In contrast, "a AND NOT a" is
unsatisfiable.

Bikash Ranjan Bag 6 12/4/2016


Knapsack problem

● Given a set of items, each with a weight and a


value, determine the number of each item to
include in a collection so that the total weight
is less than or equal to a given limit and the
total value is as large as possible.

Bikash Ranjan Bag 6 12/4/2016


Hamiltonian path problem

● a path in an undirected or directed graph that


visits each vertex exactly once

Bikash Ranjan Bag 6 12/4/2016


Travelling salesman problem

● Given a list of cities and the distances between


each pair of cities, what is the shortest possible
route that visits each city exactly once and
returns to the origin city

Bikash Ranjan Bag 6 12/4/2016


Subgraph isomorphism problem

● the subgraph isomorphism problem is a


computational task in which
two graphs G and H are given as input, and
one must determine whether G contains
a subgraph that is isomorphic to H

Bikash Ranjan Bag 6 12/4/2016


Subset sum problem

● The problem is this: given a set (or multiset) of


integers, is there a non-empty subset whose
sum is zero? For example, given the set {−7,
−3, −2, 5, 8}, the answer is yes because the
subset {−3, −2, 5} sums to zero. The problem
is NP-complete.

Bikash Ranjan Bag 6 12/4/2016


NP Hard Problems

● The circuit-satisfiability problem


● Set Cover
● Vertex Cover
● Graph coloring
● Travelling Salesman Problem

Bikash Ranjan Bag 6 12/4/2016

You might also like