Você está na página 1de 29

CS 303: Algorithm Analysis and

Design

NP Completeness

Jigar Patel @ SVNIT, Surat 1 12/08/21


NP-Completeness
● Some problems are intractable:
as they grow large, we are unable to solve them in
reasonable time
● What constitutes reasonable time? Standard
working definition: 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!)

Jigar Patel @ SVNIT, Surat 2 12/08/21


Dealing with Hard Problems

● What to do when we find a problem that


looks hard…

I couldn’t find a polynomial-time algorithm;


I guess I’m too dumb.
(cartoon inspired by [Garey-Johnson, 79])
Jigar Patel @ SVNIT, Surat 3 12/08/21
Dealing with Hard Problems

● Sometimes we can prove a strong lower


bound… (but not usually)

I couldn’t find a polynomial-time algorithm,


because no such algorithm exists!
(cartoon inspired by [Garey-Johnson, 79])
Jigar Patel @ SVNIT, Surat 4 12/08/21
Dealing with Hard Problems

● NP-completeness let’s us show collectively


that a problem is hard.

I couldn’t find a polynomial-time algorithm,


but neither could all these other smart people.
(cartoon inspired by [Garey-Johnson, 79])
Jigar Patel @ SVNIT, Surat 5 12/08/21
Polynomial-Time Algorithms
● Are some problems solvable in polynomial time?
■ Of course: every algorithm we’ve studied provides
polynomial-time solution to some problem
■ We define P to be the class of problems solvable in
polynomial time
● Are all problems solvable in polynomial time?
■ No: Turing’s “Halting Problem” is not solvable by
any computer, no matter how much time is given
■ Such problems are clearly intractable, not in P

Jigar Patel @ SVNIT, Surat 6 12/08/21


NP-Complete Problems
● The NP-Complete problems are an interesting
class of problems whose status is unknown
■ No polynomial-time algorithm has been
discovered for an NP-Complete problem
■ No suprapolynomial lower bound has been proved
for any NP-Complete problem, either
● We call this the P = NP question
■ The biggest open problem in CS

Jigar Patel @ SVNIT, Surat 7 12/08/21


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
■ What the hell is that?

Jigar Patel @ SVNIT, Surat 8 12/08/21


Nondeterminism
● Think of a non-deterministic computer as a computer
that magically “guesses” a solution, then has to
verify that it is correct
■ If a solution exists, computer always guesses it
■ One way to imagine it: a parallel computer that can freely
spawn an infinite number of processes
○ Have one processor work on each possible solution
○ All processors attempt to verify that their solution works
○ If a processor finds it has a working solution
■ So: NP = problems verifiable in polynomial time

Jigar Patel @ SVNIT, Surat 9 12/08/21


P and NP
● Summary so far:
■ P = problems that can be solved in polynomial time
■ NP = problems for which a solution can be verified
in polynomial time
■ Unknown whether P = NP (most suspect not)
● Hamiltonian-cycle problem is in NP:
■ Cannot solve in polynomial time
■ Easy to verify solution in polynomial time

Jigar Patel @ SVNIT, Surat 10 12/08/21


NP-Complete Problems
● We will see that NP-Complete problems are the
“hardest” problems in NP:
■ If any one NP-Complete problem can be solved in
polynomial time…
■ …then every NP-Complete problem can be solved in
polynomial time…
■ …and in fact every problem in NP can be solved in
polynomial time (which would show P = NP)
■ Thus: solve hamiltonian-cycle in O(n100) time, you’ve
proved that P = NP. Retire rich & famous.

Jigar Patel @ SVNIT, Surat 11 12/08/21


Reduction
● The crux of NP-Completeness is reducibility
■ Informally, a problem P can be reduced to another
problem Q if any instance of P can be “easily
rephrased” as an instance of Q, the solution to which
provides a solution to the instance of P
○ What do you suppose “easily” means?
○ This rephrasing is called transformation
■ Intuitively: If P reduces to Q, P is “no harder to
solve” than Q

Jigar Patel @ SVNIT, Surat 12 12/08/21


Using Reductions
● If P is polynomial-time reducible to Q, we denote
this P p Q
● Definition of NP-Complete:
■ If P is NP-Complete, P  NP and all problems R are
reducible to P
■ Formally: R p P  R  NP
● If P p Q and P is NP-Complete, Q is also NP-
Complete
■ This is the key idea you should take away today

Jigar Patel @ SVNIT, Surat 13 12/08/21


Reduction
● Given a Library with lot of good programs
(codes)
● Create a new program by calling sub routines
from the library rather than starting from
scratch.
● How to efficiently use code from other
algorithm to generate new algorithm

Jigar Patel @ SVNIT, Surat 14 12/08/21


Reduction
● Matching in a Graph G = (V,E)
● Is a subset M of E such that no two edges in M
have the same vertex (end point).

a
e

d
b

Jigar Patel @ SVNIT, Surat 15 12/08/21


Reduction
● Perfect Matching
■ Matching M such that all vertices are end points of
exactly one edge in M
■ So |M| = |V| / 2

Jigar Patel @ SVNIT, Surat 16 12/08/21


Reduction
● Problem (Perfect Matching)
■ Input: Graph G
■ Question: Does G have a perfect matching ? (Yes/No)
● Problem (Maximum Matching)
■ Input: Graph G
■ Output: Matching of maximum size
● Can we use Maximum Matching to check for Perfect
Matching ?
● Answer : Yes (If Maximum Matching has |V|/2 edges)
● How about the other way around?

Jigar Patel @ SVNIT, Surat 17 12/08/21


Reduction
● Given an algo for Perfect Matching
● Objective 1
■ Design algo for Max Matching
● Intermediate Objective
■ Design an algo to find the size of a maximum
matching in G

Jigar Patel @ SVNIT, Surat 18 12/08/21


Reduction
● Perfect Matching
■ Feed a Graph G as input
○ If G has a Perfect Matching
 Size of a Maximum Matching is |V| / 2
○ If G does not have a Perfect Matching
 Add a new vertex to G which is connected to all vertices in G
G1
G
V

Jigar Patel @ SVNIT, Surat 19 12/08/21


Reduction
● G1: G + a vertex v connected to all vertices in G
G1
G
V

● If G1 has Perfect Matching


■ G has matching of size |V|-1 / 2

Jigar Patel @ SVNIT, Surat 20 12/08/21


Reduction
● If G1 does not have Perfect Matching
● G2: G1 + a vertex v connected to all vertices in
G
G2
G1
G
V
V1

Jigar Patel @ SVNIT, Surat 21 12/08/21


Reduction
● Gi = Gi-1 + a vertex connected to all vertices in G
● Let Kth is the step where we find a Perfect Matching
■ Gk-1 Does not have Perfect Matching
■ Gk has Perfect Matching
■ Gk has matching of size N-K / 2 (N-K has to be even)
G G

K-1 Vertices K Vertices

Jigar Patel @ SVNIT, Surat 22 12/08/21


Coming Up
● Given one NP-Complete problem, we can prove
many interesting problems NP-Complete
■ Graph coloring (= register allocation)
■ Hamiltonian cycle
■ Hamiltonian path
■ Knapsack problem
■ Traveling salesman
■ Job scheduling with penalities
■ Many, many more

Jigar Patel @ SVNIT, Surat 23 12/08/21


Reduction (Review)
● Efficient Algorithm for problem 1
■ There exists an efficient algorithm for problem 2
● Recall the Scheduling Problem
■ Brute Force Algorithm (Not efficient)
■ Usually search exhaustively through the entire
solution space
■ Takes 2n time

Jigar Patel @ SVNIT, Surat 24 12/08/21


Reduction (Review)
● Efficient: Running time of the algorithm is
bounded by a polynomial in the input size
■ There exists constant c, such that the running time
T(n) is O(nc) where n is the input size
● Compare nc Vs 2n
● In practice if there is such a c, usually c <= 3

Jigar Patel @ SVNIT, Surat 25 12/08/21


Hamiltonian Path
● A Hamiltonian path in a graph G is a path of
length n-1 where n is the number of vertices

● In other words
■ It is a path that contains all vertices

Jigar Patel @ SVNIT, Surat 26 12/08/21


Hamiltonian Cycle
● A Hamiltonian Cycle in a graph is a cycle
which spans all vertices

● Use reduction here

Jigar Patel @ SVNIT, Surat 27 12/08/21


● HC:
■ Input: Graph G
■ Question: Does G have Hamiltonian Cycle
● HP
■ Input: Graph G
■ Question: Does G have Hamiltonian Path

Jigar Patel @ SVNIT, Surat 28 12/08/21


● Given an efficient algorithm for HC
■ Design an efficient algorithm for HP
● Given an efficient algorithm for HP
■ Design an efficient algorithm for HC

Jigar Patel @ SVNIT, Surat 29 12/08/21

Você também pode gostar