Mohamed-The Classes P and NP - wmich.edu · NP class, wrt Turing Machines • Problems solvable in...
Transcript of Mohamed-The Classes P and NP - wmich.edu · NP class, wrt Turing Machines • Problems solvable in...
Intractable ProblemsIntractable ProblemsThe Classes P and NP
Mohamed M. El WakilMohamed M. El Wakil
1
AgendaAgenda
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 class5. The NP Complete class
2
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?
3
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!
4
ClassificationClassification
• 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
5
P class wrt ComputersP class, wrt Computers• Problems with at least one algorithm that
l h bl i l i l isolves 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 inputto 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.
6
P class wrt Turing MachinesP class, wrt Turing Machines• Problems solvable in polynomial time using a D t i i ti T i M hi (DTM) b l tDeterministic 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.
7
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 originaltree, that includes all the nodes of the original graph, and minimizes the summation of weights of the edges in the resulting treethe edges in the resulting tree.
8
MWST ExampleProblem Instance
Source: http://en.wikipedia.org/wiki/Kruskal's_algorithm
9
Kruskal's algorithmKruskal 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 tiit 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 treeforest, combining two trees into a single tree
• Otherwise discard that edge 10
MWST ExamplePossible Solution
Source: http://en.wikipedia.org/wiki/Kruskal's_algorithm11
NP class wrt Turing MachinesNP class, wrt Turing Machines
• Problems solvable in polynomial time using a Non Deterministic Turing Machine (NDTM) g ( )belong to the class NP.
– NDTMh f d• A DTM, with two stages of processing: guessing, and
checking.
12
Non Deterministic Turing MachineNon‐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.
13
NP class wrt ComputersNP 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.
14
ImportantImportant
• 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.
15
NP Problem ExampleTravelling 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
16
Solving the TSPSolving 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
17
Polynomial Time ReductionPolynomial Time Reduction
• A problem P1, is polynomially reducible to problem P2, if there is a process that takes an p pinstance of P1 as an input, and outputs a corresponding instance of P2 in polynomialcorresponding instance of P2 in polynomial time.
– P1: a * b
– P2: ((a+b)2 – a2 – b2)/2
18
NP Complete ClassNP‐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 Ptime 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.
19
NP l tNP‐complete problems familyproblems family
tree
20
The NP WorldThe NP World
Source: http://en.wikipedia.org/wiki/Complexity_classes_P_and_NP
21
Intractable ProblemsIntractable ProblemsThe Classes P and NP
Mohamed M. El WakilMohamed M. El Wakil
22