You are on page 1of 27

Algorithmics

CT065-3.5-3

NP Completeness
Level 3 – Computing (Software Engineering)
Topic & Structure of Lesson

• Motivation
• Introduction
– Computational problems
– Complexity classes
• P
– Definition
– Examples
• NP
– Definition
– Examples

Module Code and Module Title Title of Slides Slide 2 (of 27)
Learning Outcomes

By the end of this lesson you should be


able to:
• Understand concept of complexity classes
• Define the complexity classes P and NP
• Determine what problems lie in P/NP

Module Code and Module Title Title of Slides Slide 3 (of 27)
Motivation

• Suppose that you are organizing housing


accommodations for a group of five hundred
university students. Space is limited and only one
hundred of the students will receive places in the
dormitory. To complicate matters, the Dean has
provided you with a list of pairs of incompatible
students, and requested that no pair from this list
appear in your final choice. This is an example of
what computer scientists call an NP-problem, since
it is easy to check if a given choice of one hundred
students proposed by a coworker is satisfactory
(i.e., no pair taken from your coworker's list also
appears on the list from the Dean's office), however
the task of generating such a list from scratch
seems to be so hard as to be completely impractical.

Module Code and Module Title Title of Slides Slide 4 (of 27)
Motivation

• Indeed, the total number of ways of choosing one


hundred students from the four hundred applicants
is greater than the number of atoms in the known
universe! Thus no future civilization could ever hope
to build a supercomputer capable of solving the
problem by brute force; that is, by checking every
possible combination of 100 students. However, this
apparent difficulty may only reflect the lack of
ingenuity of your programmer. In fact, one of the
outstanding problems in computer science is
determining whether questions exist whose answer
can be quickly checked, but which require an
impossibly long time to solve by any direct
procedure.

Module Code and Module Title Title of Slides Slide 5 (of 27)
Motivation

• Problems like the one in the example


certainly seem to be of this kind, but so far
no one has managed to prove that any of
them really are so hard as they appear, i.e.,
that there really is no feasible way to
generate an answer with the help of a
computer. Stephen Cook and Leonid Levin
formulated the P (i.e., easy to find) versus NP
(i.e., easy to check) problem independently in
1971.

Module Code and Module Title Title of Slides Slide 6 (of 27)
Motivation

Why should we care?


• These NP-complete problems really come up all the
time. Knowing they're hard lets you stop beating your
head against a wall trying to solve them, and do
something better:
• If you can't quickly solve the problem with a good worst
case time, maybe you can come up with a method for
solving a reasonable fraction of the common cases.
• Solve the problem approximately instead of exactly. A lot
of the time it is possible to come up with a provably fast
algorithm, that doesn't solve the problem exactly but
comes up with a solution you can prove is close to right.

Module Code and Module Title Title of Slides Slide 7 (of 27)
Motivation

• Use an exponential time solution anyway. If you really


have to solve the problem exactly, you can settle down
to writing an exponential time algorithm and stop
worrying about finding a better solution.
• Choose a better abstraction. The problem you're trying to
solve presumably comes from ignoring some of the
seemingly unimportant details of a more complicated
real world problem. Perhaps some of those details
shouldn't have been ignored, and make the difference
between what you can and can't solve.

Module Code and Module Title Title of Slides Slide 8 (of 27)
Introduction

• Some computational problems are hard.


• Difficult to find efficient algorithms to solve
them.
• Efficient = polynomial time = O(nk) for some
constant k > 0
• Prove that finding such a solution is
impossible.
• Such proofs typically harder to discover than
efficient algorithms!

Module Code and Module Title Title of Slides Slide 9 (of 27)
Introduction

• given n floppy disks each of size x and m


computer files of sizes y1, y2, y3, ... ym, is it
possible to copy all of the files to the floppy
disks given? , .

Module Code and Module Title Title of Slides Slide 10 (of 27)
Introduction

• Obviously sometimes the answer will be


"yes", and sometimes "no". It depends on the
numbers n,m,x and y1,...,ym. What is needed
is an efficient (polynomial) computer program
or algorithm that will give the answer in all
cases.

Module Code and Module Title Title of Slides Slide 11 (of 27)
Introduction

• Problems like this are of great practical


importance, and an efficient algorithm would
be very useful. Unfortunately no-one has yet
found such an algorithm, and many people
suspect that there isn't such an algorithm. In
principle, it may even be possible to prove
that there is no such algorithm, but again no-
one has managed to do this either.

Module Code and Module Title Title of Slides Slide 12 (of 27)
P and NP

• The relation between the complexity classes


P and NP is one of the most important
open problems in
theoretical computer science and
mathematics.
• It is studied in
computational complexity theory, the part of
the theory of computation dealing with the
resources required during computation to
solve a given problem.

Module Code and Module Title Title of Slides Slide 13 (of 27)
Decision Problems

Computational problems where the intended


output is either yes or no (0 or 1).

Examples:
Given a string T and a string P, is P a substring of
T?

Given 2 sets S and T, do S and T contain the


same set of elements?

Module Code and Module Title Title of Slides Slide 14 (of 27)
Decision Problems

Optimization problems can also be turned


into decision problems.

Example:
Does a weighted graph G, contain a
minimum spanning tree of k weight?

Module Code and Module Title Title of Slides Slide 15 (of 27)
Complexity Class P

• “Deterministic polynomial-time”
• In this theory, the class P consists of all
those decision problems that can be
solved (i.e. a “yes” output) on a
deterministic sequential machine in an
amount of time that is polynomial in the
size of the input

Module Code and Module Title Title of Slides Slide 16 (of 27)
Complexity Class P

• Problem x with size n → solvable in at


most O(nk) time.
• Example: Can integer x be evenly divided
by integer y? Is integer i a prime number?

Module Code and Module Title Title of Slides Slide 17 (of 27)
Complexity Class NP

• "Non-deterministic polynomial-time"
• the class NP consists of all those decision problems
where
a) positive solutions can be verified in polynomial time
by a deterministic Turing machine given the right
information

OR

b) whose solution can be found in polynomial time on


a non-deterministic Turing machine.

Module Code and Module Title Title of Slides Slide 18 (of 27)
Complexity Class NP

• The set of languages that can be verified


in polynomial time.
• A language, L is in NP iff a 2-input
algorithm A exists that when given any
string x in L, there is another string y in L,
where A outputs “yes” on input z = x + y.
• y = certificate for membership into L.
Helps certify that x is really in L.

Module Code and Module Title Title of Slides Slide 19 (of 27)
Complexity Class NP

Example: A friend tells you a given graph G is


Hamiltonian, and offers to prove it by giving you
the vertices in order along the Hamiltonian cycle.
This would be easy to verify by checking to see if
the cycle is a permutation of the vertices of V and
if each consecutive edge in the cycle actually
exists in the graph. This verification algorithm can
be implemented in O(n2) time, where n = size of G.
Therefore, a proof that a Hamiltonian cycle exists
in a graph can be verified in polynomial time.

Module Code and Module Title Title of Slides Slide 20 (of 27)
Complexity Class NP

Certificate = list of vertices in Hamiltonian


cycle.

If graph is Hamiltonian, list of vertices has


enough information to verify this. If not, no
list of vertices exist to fool the verification
algorithm, since the algorithm checks the
proposed “cycle” to be sure.

Module Code and Module Title Title of Slides Slide 21 (of 27)
P and NP Question

Q: Is P = NP?

if positive solutions to a YES/NO problem


can be verified quickly, can the answers also
be computed quickly?

Module Code and Module Title Title of Slides Slide 22 (of 27)
P and NP Question

A: Unknown. Computer scientists do not know, but


most believe P ≠ NP.

P = “easy” to solve.
NP = “easy” to check given solution. Producing
solution from scratch often harder than checking
presented solution.

Therefore, NP includes languages not in P.

Module Code and Module Title Title of Slides Slide 23 (of 27)
P and NP Question

So far, we have
NP Problems

P Problems

Module Code and Module Title Title of Slides Slide 24 (of 27)
P and NP Question

Q: Why is the P = NP question so important


to answer?

A: Come for the next lecture!

Module Code and Module Title Title of Slides Slide 25 (of 27)
Summary

• Introduction to Complexity Classes


–P
– NP
– Is P = NP?

Module Code and Module Title Title of Slides Slide 26 (of 27)
Q&A

Module Code and Module Title Title of Slides Slide 27 (of 27)

You might also like