Intractable Problems Intractable Problems The Classes P and NP
Mohamed M. El Wakil Mohamed M. El Wakil [email protected]
Agenda Agenda
1. What is a problem?
2. Decidable or not?
2. Decidable or not?
3. The P class 4. The NP Class
5 The NP‐Complete class 5. The NP Complete class
What is a problem?
What is a problem?
• A problem is a question to be answered.
– What is the value of X/Y?
• A problem usually has parameters.p y p
– X, and Y
• A decision problem is a version of the
• A decision problem, is a version of the
problem with only two possible answers: Yes or No!
or No!
– Given two numbers X, and Y, does Y evenly divide X?
X?
• An instance: a specific problem instance
– Does 3 evenly divide 6?
Decidable or not?
Decidable or not?
• A decidable problem, is a problem that could be solved using a computer.
A d id bl bl i bl th t
• An undecidable problem, is a problem that
can never be solved using a computer, neither now or in the future.
• Only decidable problems!
Classification Classification
• We need to classify problems in terms of their computability.p y
Th l
• Three classes:
– P class – NP Class
– NP‐Complete class – NP‐Complete class
P class wrt Computers P class, wrt Computers
• Problems with at least one algorithm that
l h bl i l i l i
solves the problem in polynomial time wrt to the input size.
• Polynomial timePolynomial time
– The number of steps needed relates polynomially to the size of the input
to the size of the input.
– O(n2), O(n9), O(nc), where c is a constant.
but NOT O(n!) O(2n) when n is the size of the – but NOT O(n!), O(2n), when n is the size of the
input.
P class wrt Turing Machines P class, wrt Turing Machines
• Problems solvable in polynomial time using a D t i i ti T i M hi (DTM) b l t Deterministic Turing Machine (DTM) belong to the class P.
• Polynomial time
– The number of moves needed relates polynomially to the size of the input.
• n2, 17n3, 9n4, but NOT 2n
• DTM
– A Turing machine with a tape, head, transition function, and a set of states.
P Problem (MWST) P Problem (MWST)
• Minimum Weight Spanning Tree
– Given a weighted graph G, find the minimum g g p , weight spanning tree.
– In other words, convert the given graph into a tree that includes all the nodes of the original tree, that includes all the nodes of the original
graph, and minimizes the summation of weights of the edges in the resulting tree
the edges in the resulting tree.
MWST Example Problem Instance
Source: http://en.wikipedia.org/wiki/Kruskal's_algorithm
Kruskal's algorithm Kruskal s algorithm
h S bl b l h l f
• The MWST problem belongs to the P class of
problems, since there is an algorithm that solves it i l i l ti
it in polynomial time.
• Kruskal's algorithm O(n2)
– Create a forest F (a set of trees), where each vertex in the graph is a separate tree
– Create a set S containing all the edges in the graph – While S is nonempty
• Remove an edge with minimum weight from S
• If that edge connects two different trees, then add it to the forest, combining two trees into a single tree
forest, combining two trees into a single tree
• Otherwise discard that edge
MWST Example Possible Solution
Source: http://en.wikipedia.org/wiki/Kruskal's_algorithm
NP class wrt Turing Machines NP class, wrt Turing Machines
• Problems solvable in polynomial time using a Non Deterministic Turing Machine (NDTM) g ( ) belong to the class NP.
– NDTM
h f d
• A DTM, with two stages of processing: guessing, and checking.
Non Deterministic Turing Machine Non‐Deterministic Turing Machine
G i
• Guessing:
– Guess a solution, and then write it down to the tape.
– Checking:
• Evaluate the guess to decide whether it solves the problem or not.
• The number of guessed solutions can be either
• The number of guessed solutions, can be either polynomial or exponential.
If th b f d l ti i l i l
• If the number of guessed solutions is polynomial, then, the NDTM is equivalent to a DTM.
NP class wrt Computers NP class, wrt Computers
• Problems that can be solved within an exponential time wrt the input size. p p
Thi i l d bl h b l d i
• This includes problems that can be solved in polynomial time.
Important Important
• A DTM is a NDTM that has a polynomial number of guesses.g
A di h d fi i i f NP h MWST
• According to the definition of NP, the MWST problem is an NP problem.
NP Problem Example
Travelling Salesman Problem (TSP)
Given a number of cities and the costs of traveling from any city to any other city, what is the cheapest round‐trip route that visits each city exactly once and then returns to the starting city?
Source: http://en wikipedia org/wiki/Traveling salesman problem and then returns to the starting city?
Source: http://en.wikipedia.org/wiki/Traveling_salesman_problem
Solving the TSP Solving the TSP
• There is no one single algorithm that solves this problem in polynomial time /p p y
Th l i ll ibl
• The only way, is to enumerate all possible itineraries and checking them one‐by‐one.
F iti th ! t
• For n cities, there are n! routes
Polynomial Time Reduction Polynomial Time Reduction
• A problem P1, is polynomially reducible to
problem P2, if there is a process that takes an
p p
instance of P1 as an input, and outputs a corresponding instance of P2 in polynomial corresponding instance of P2 in polynomial time.
– P1: a * b
– P2: ((a+b)2 – a2 – b2)/2
NP Complete Class NP‐Complete Class
• A problem P is NP‐Complete If:
– P is in NP
– For every problem L in NP, there is a polynomial time reduction from L to P
time reduction from L to P.
If P1 i NP C l t d th i l i l
• If P1 is NP‐Complete, and there is polynomial time reduction from P1 to P2, then P2 is NP‐
Complete.
NP l t NP‐complete problems family problems family
tree
The NP World The NP World
Source: http://en.wikipedia.org/wiki/Complexity_classes_P_and_NP
Intractable Problems Intractable Problems The Classes P and NP
Mohamed M. El Wakil Mohamed M. El Wakil [email protected]