Polynomial-time Algorithms • Most of the algorithms we have seen so far have been polynomial-time algorithms Introduction to NP Completeness • Input size n => worst-case running time of n k , Chapter 9 where k is a constant CPTR 318 Can all problems be solved in polynomial time? 1 2 Some Problems Cannot be Solved at All Halt Parameters • Write a computer program (or procedure or • p is just a string of symbols: – String of characters for source code, or algorithm) Halt that accepts two inputs: – String of bytes for compiled machine code – p , the string representation of a computer program that • Both of these representations are ultimately bitstrings, we can accepts a single string as input say that p can be boiled down to a single integer – i , a string that serves as input to p • i is also ultimately a bitstring that maps to a single integer bool Halt(String p, String i) bool Halt(String p, String i) { { bool result = false; bool result = false; if ( isValidProgram(p) ) if ( isValidProgram(p) ) // The magic goes here! // The magic goes here! if ( program p halts when run on input string i ) if ( program p halts when run on input string i ) result = true; result = true; return result; return result; } } 3 4 What does Halt Do? Pictorially • Halt determines if p terminates when given input i bool Halt(String p, String i) { • Said another way, Halt determines if i causes p to go into an bool result = false; infinite loop if ( isValidProgram(p) ) // The magic goes here! if ( program p halts when run on input string i ) bool Halt(String p, String i) result = true; { return result; bool result = false; } if ( isValidProgram(p) ) // The magic goes here! false if ( program p halts when run on input string i ) p,i result = true; Halt return result; } true 5 6 1
The Halting Problem Undecidability • Alan Turing, 1936 • The Halting Problem is undecidable – that is, no algorithm can solve it false false p,i p,i Halt Halt true true Perhaps it just a hard problem, and no one has yet to think up a solution 7 8 A Computer Program as Input? Proof • Is this even possible? • We must prove that no such algorithm exists • Yes, consider • The proof by contradiction: So, what’s the problem? – compilers – Suppose such a algorithm, Halt , exists – code formatters – Devise a new algorithm, Loop_If_Halts : bool Loop_If_Halts(String p) { if ( Halt(p, p) ) prog.cpp prog.exe compiler while ( true ) {} // Loop forever return true; } 9 10 Proof Program Loop_If_Halts bool Loop_If_Halts(String p) bool Loop_If_Halts(String p) { { if ( Halt(p, p) ) if ( Halt(p, p) ) while ( true ) while ( true ) {} // Loop forever {} // Loop forever return true; return true; } } • If p corresponds to a valid program that accepts a single Loop_If_Halts bitstring as an argument, then Halt is used to see if p halts false true on itself p p,p Halt • If p halts on p , then Loop_If_Halts never returns (it goes true into an infinite loop) • If p does not halt on p , then Loop_If_Halts terminates and returns true 11 12 2
A Clever Application of The Contradiction Loop_If_Halts Loop_If_Halt s Loop_If_Halts, • What about calling false true Loop_If_Halts Loop_If_Halts Halt Loop_If_Halts(Loop_If_Halts) ? true Loop_If_Halt s Loop_If_Halts, } false true • If Loop_If_Halts(Loop_If_Halts) Loop_If_Halts Loop_If_Halts Halt does not terminate, then it terminates true • That is, run Loop_If_Halts on itself } • Pass to the executing program the bitstring • If Loop_If_Halts(Loop_If_Halts) representing its own encoding terminates, then it does not terminate 13 14 Ramifications of the Halting Tractable vs. Intractable Problems Problem • While some problems are impossible, some are just • You may be able to construct a routine to address the halting hard problem that works in limited situations, but . . . • Problems with algorithmic solutions that run in • You cannot devise a Halt procedure that will work under all circumstances polynomial time are considered tractable • Given any proposed Halt procedure you can devise an input – tractable = easy that will cause it to fail – efficient solutions • Many interesting questions about computer programs are • Problems with algorithmic solutions that run in equivalent to the halting problem: superpolynomial time are considered intractable – Will a particular section of code be executed? – intractable = hard – Will a program halt on all input? – Will a program halt on any input? – Inefficient solutions 15 16 Tractable vs. Intractable Problems Tractable vs. Intractable Problems • Shortest path • Shortest path vs. longest path 3 – Shortest path: Dijkstra’s Algorithm O(|E|∙|V|) 5 1 3 – Longest path (even if all edge weights are 1; that 4 5 is, the edges are unweighted) intractable? 7 7 7 3 0 2 4 • Euler tour vs. Hamiltonian cycle 8 8 2 1 – Euler tour: a cycle that traverses each edge in a 3 graph exactly once (vertices may be revisited) 5 4 6 8 O(|E|) 3 – Hamiltonian cycle: a simple cycle that contains • Longest path? every vertex in the graph intractable? 17 18 3
P vs. NP Solution Certificate • P is the set of problems that are solvable in • In the Hamiltonian cycle problem, the polynomial time certificate would be the sequence H = < v 1 , v 2 , v 3 , …, v V > – O( n k ) algorithms, where k is a constant, are known for for all such problems • Note that in polynomial time one can verify • NP is the set of problems whose solution can that be checked in polynomial time (polynomial in – The length of H is | V | the size of the input to the original problem) – All elements of G appear in H – A certificate of the solution is used for the check – For all i = 1, 2, 3, …, | V | – 1 • P is a subset of NP • ( v i , v i +1 ) is in E • ( v | V | , v 1 ) is in E 19 20 NP-complete Problems NP-complete Problems • No polynomial-time algorithms have been found to • Some NP problems are special solve any NP-complete problem • An NP-complete problem is as hard as any • No one has proven that a polynomial-time algorithm other problem in NP does not exist for an NP-complete problem • If a polynomial solution can be found for an • The search has been going on for over 40 years! NP-complete problem, all NP problems can be • Superficially, several NP-complete problems appear solved in polynomial time very similar to problems that have polynomial-time solutions – Any problem in NP can be converted into an • The big question: P ≠ NP? instance of an NP-complete problem in polynomial time 21 22 NP-complete Problems Is NP-completeness a Problem? • Most computer scientists believe that NP- • NP-complete problems crop up more often than you might complete problems are intractable think (Remember many appear to be very similar to “easy” problems) • If you can show that a problem in NP-complete, you know • Many NP-complete problems have been that it is quite possibly intractable studied for decades and • If it is NP-complete, do not waste your time trying to devise – no polynomial-time algorithms have been found an exact solution to the general problem (A lot of smart people have been trying unsuccessfully for over 40 years!) – Given this, it seems hard to believe that all of the • The problem must be solved — what are your options? NP-complete problems have polynomial solutions! – Devise an approximation algorithm (Not the exact solution – But, . . . no one has proved that such algorithms but good enough) do not exist – Concentrate on a tractable special case 23 24 4
How to Show a Problem is NPC 1. Take a known NP-complete problem (there are hundreds) 2. Demonstrate a process that converts an instance of the NP-complete problem into an instance of your problem in polynomial time That proves your problem is NP-complete Why? 25 5
Recommend
More recommend