Intractable Problems The Classes P and NP Mohamed M. El Wakil mohamed@elwakil.net 1
Agenda 1. What is a problem? 2. Decidable or not? 3. The P class 4. The NP Class 5. TheNP Complete class 2
What is a problem? A problem is a question to be answered. What is the value of X/Y? A problem usually has parameters. X, and Y A decision problem, is a version of the problem with only two possible answers: Yes or No! Given two numbers X, and Y, does Y evenly divide X? An instance: a specific problem instance Does 3 evenly divide 6? 3
Decidable or not? A decidable problem, is a problem that could be solved using a computer. An undecidable problem, is a problem that t can never be solved using a computer, neither now or in the future. Only decidable problems! 4
Classification We need to classify problems in terms of their computability. Three classes: P class NP Class NP Complete class 5
P class, wrt Computers Problems with at least one algorithm that solves the problem in polynomial ltime wrt to the input size. Polynomial time The number of steps needed relates polynomially to the size of the input. O(n 2 ), O(n 9 ), O(n c ), where c is a constant. but NOT O(n!), O(2 n ), when n is the size of the input. 6
P class, wrt Turing Machines Problems solvable in polynomial time using a Dt Deterministic iiti Turing Machine (DTM) bl belong to the class P. Polynomial time The number of moves needed relates polynomially to the size of the input. n 2, 17n 3, 9n 4, but NOT 2 n DTM A Turing machine with a tape, head, transition function, and a set of states. 7
P Problem (MWST) Minimum Weight Spanning Tree Given a weighted graph G, find the minimum weight spanning tree. In other words, convert the given graph into a tree, that includes allthe nodes of the original graph, and minimizes the summation of weights of the edges in the resulting tree. 8
MWST Example Problem Instance Source: http://en.wikipedia.org/wiki/kruskal's_algorithm 9
Kruskal's s algorithm The MWST problem belongs bl to the P class of problems, since there is an algorithm that solves it in polynomial ltime. Kruskal's algorithm O(n 2 ) 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 Otherwise discard that edge 10
MWST Example Possible Solution Source: http://en.wikipedia.org/wiki/kruskal's_algorithm 11
NP class, wrt Turing Machines Problems solvable in polynomial time using a Non Deterministic Turing Machine (NDTM) belong to the class NP. NDTM A DTM, with two stages of processing: guessing, and checking. 12
Non Deterministic Turing Machine 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 polynomial or exponential. If the number of guessed solutions is polynomial, l then, the NDTM is equivalent to a DTM. 13
NP class, wrt Computers Problems that can be solved within an exponential time wrt the input size. This includes problems that can be solved din polynomial time. 14
Important A DTM is a NDTM that has a polynomial number of guesses. According to the dfiii definition of NP, the MWST problem is an NP problem. 15
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 andthen returns to the starting city? Source: http://en.wikipedia.org/wiki/traveling_salesman_problem problem 16
Solving the TSP There is no one single algorithm that solves this problem in polynomial time The only way, is to enumerate all possible itineraries and checking them one by one. For n cities, there are n! routes 17
Polynomial Time Reduction A problem P1, is polynomially reducible to problem P2, if there is a process that takes an instance of P1 as an input, and outputs a corresponding instance of P2 in polynomial time. P1: a * b P2: ((a+b) 2 a 2 b 2 )/2 18
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. If P1 is NP Complete, and there is polynomial l time reduction from P1 to P2, then P2 is NP Complete. 19
NP complete lt problems family tree 20
The NP World Source: http://en.wikipedia.org/wiki/complexity_classes_p_and_np 21
Intractable Problems The Classes P and NP Mohamed M. El Wakil mohamed@elwakil.net 22