680 likes | 808 Views
Chapter 10. P and NP. Logarithmic (all bases have the same growth rate) (log n) (log (log n)) Poly-logarithmic (n log n) (n 2 log n) . Polynomial (exponent is constant) (1) – Sub-linear (n 0.001 ) – Sub-linear (n 0.5 ) – Sub-linear (n) - Linear (n 2 ) (n 3 )
E N D
Chapter 10 P and NP
Logarithmic(all bases have the same growth rate) (log n) (log (log n)) Poly-logarithmic (n log n) (n2 log n) ... Polynomial (exponent is constant) (1) – Sub-linear (n0.001) – Sub-linear (n0.5) – Sub-linear (n) - Linear (n2) (n3) ... (n100) Classes of Algorithms
Exponential (bases make a big difference) (2n) (3n) (nn) Factorial k cn < n! < nn where k is some constant, and c is a constant less than n Exponential-Exponential Classes of Algorithms
All Polynomial Algorithms includes linear poly-log log constant sub-linear All Exponential Algorithms includes factorial poly-poly The Great Divide
Problems vs. Algorithms • A single problem, i.e., sorting integers, can have many algorithms with very different run times. • Mergesort (n log n) • Permutationsort (n!)
Problems vs. Algorithms • A specific algorithm’s runtime can vary depending on the input • Quicksort • (n log n) – on most unsorted lists • (n2) – on pre-sorted or partially sorted lists • So, what can we say about the sorting problem?
Problems vs. Algorithms • The sorting problem facts: • Can be solve in (n log n) (mergesort) • Can be solve in (n!) • The sorting problem is Polynomial because there exists at least one Polynomial algorithm for solving the sorting problem.
Problems vs. Algorithms • Some problems have solution algorithms that are intrinsically polynomial • But, some problems can NOT be solved in polynomial time (period!) • At least most people think so... • But, it’s hard to prove that something can’t be done
Example: 1960’s • The Longest Common Subsequence Problem (LCS) • Fasted known algorithm can only solve the problem in (2n) time. • Does there exist an optimal LCS algorithm that has a polynomial runtime? • In 1960, most computer scientists were leaning toward an answer of “no” i.e., there does NOT exist a polynomial algorithm.
Example: 1980’s • In 1981, two guys Smith and Waterman used Dynamic Programming to solve the LCS problem in (n2)-time using (n2)memory. • This discover and many other discoveries led computer scientists to ponder the following question: • Can all problems be solved in Polynomial Time? • Maybe we just haven’t discovered all the good algorithms yet.
P or NP • NP – set of problems were the solution can not be computed deterministically in Polynomial Time • P – set of problems that can be solved deterministically in Polynomial Time NP P
Polynomial Problems • Searching • Sorting • Minimum Spanning Tree • LCS Problem • Shortest Path Problem
Non-deterministically Polynomial Problems • The graph isomorphism problem of determining whether two graphs can be drawn identically • The traveling salesman problem, where we want to know if there is a route of some length that goes through all the nodes in a certain network • The boolean satisfiability problem, where we want to know if a certain formula in propositional logic with boolean variables is satisfiable or not • Knapsack problem • Bin Packing problem
NP (not deterministic) Repeat x times where x = O(nc) Develop an answer in O(nc) time Verify if answer is correct in O(nc) time (Yes or don’t know) If its correct, hooray! You solved the problem, so stop! P NP
NP (not deterministic) Example (the bin packing problem) Repeat x times where x = O(nc) Arrange n items randomly i = 1 For j = 1 to n if(bin[i].total_weight+item[j].weight < bin_capcity) add item[j] to bin[i] else i++; add item[j] to bin[i] if (i < ceil(items.total_weight/bin_capacity)) You solved the problem, so stop! return i
Example (the bin packing problem) Repeat x times where x = O(nc) Arrange n item randomly i = 1 For j = 1 to n if(bin[i].total_weight+item[j].weight < bin_capcity) add item[j] to bin[i] else i++; add item[j] to bin[i] if (i < ceil(items.total_weight/bin_capacity)) You solved the problem, so stop! return i This algorithm is polynomial 0 NP (not deterministic) • This algorithm will always terminate (finite) • When this algorithm returns an answer it is always optimal • Sometimes this algorithm does not return an answer • Because of its random nature this algorithm may return different output given the same input.
0 P (deterministic) • Repeat x times where x = O(nc) • Develop an answer or solution in O(nc) time • Verify if it is correct in O(nc) time (YES or No) • If its correct, hooray! You solved the problem, so stop! NP P
0 P (deterministic) • Repeat x times where x = O(nc) • Develop an answer or solution in O(nc) time • Verify if it is correct in O(nc) time (YES or No) • If its correct, hooray! You solved the problem, so stop! Most Problems in P have straight-forward algorithms that compute a correct or optimal solution. The verification is done as the solution is developed. Consider sorting: every comparison and swap corrects an inversion and get you closer to a correctly sorted list. The algorithms stops when there are no more inversions to fix
P is a subset of NP • 6 out of 10 leading theoretical computer scientists believe this to be true. NP P
P = NP • 1 out of 10 leading theoretical computer scientists believe this to be true. Every problem that can be “verified” in Polynomial time can be solved in Polynomial time. NP = P
P ? NP • 3 out of 10 leading theoretical computer scientists aren’t sure, haven’t made up their minds yet, or they are too busy thinking about ninjas to care. NP ? P
NP Problems • Given a problem with no known polynomial algorithm, can we prove that it can NOT be solved in Polynomial time. • So far, no such proof exists for any NP problem. • And there are hundreds of NP problems with no known polynomial algorithm. • There is a lot of evidence to support the fact that P does not equal NP, but no one can prove it.
NP-Complete • P – problems with polynomial deterministic algorithms • NP – problems with polynomial non-deterministic algorithms • NP Complete – problems in NP that are very likely not in P. NP P NP-Complete
Footnote: Exponential Problems • Example: Given a chess board, determine if a player can win. Output: Possible or impossible. EXP: Problems that can be verified in Exponential time NP P NP-Complete
NP-Complete (TSP Example) • Find the shortest path from A back to A that visits every vertex. 9 B 2 1 C 5 4 A 6 D 3 2 ... 4 7 1 1 5 7 E 4
NP-Complete (TSP Example) Non-deterministic Algorithm: • Generate a random permutation of the vertices and compute total length of the tour (tour_length) • Sum up the N smallest edges (min_tour) • If the tour_length = min_tour • Output optimal answer • Otherwise, don’t give an answer.
NP • Given a new problem, if you can create a non-deterministic polynomial algorithm, then its in NP NP P NP-Complete new problem
P • IF you can create a deterministic polynomial algorithm, then its in P NP P NP-Complete new problem
P or NP • IF you can NOT create a deterministic polynomial algorithm, then you really can’t be sure of anything.. NP P NP-Complete new problem
P or NP • So, there are many problems in NP, • Some are clearly in P, • All the rest are on the edge, i.e., we don’t know if a P algorithm will be discovered or not • But there is convincing evidence that some of the problems on the edge are not in P and these are the NP-complete problems. NP-Complete NP P
NP-Complete • NP-Complete started with several famous problems with no known P algorithm: • Maximum Clique, • Graph Coloring, • 3SAT, • Traveling Salesman • Each of the problems can be “polynomially” transformed into the others. • The transformation algorithm is called a reduction, • The reduction process needs to be polynomial. 3SAT TSP Graph Coloring Max Clique
NP-Complete • New problems can be added to NP-Complete if the following two conditions are satisfied: • The new problem can be reduced into one of the existing problems in NP-Complete • An existing NP-Complete problem can be reduced into the new problem NP-Complete 3SAT TSP New Problem Graph Coloring Max Clique
NP-Complete • Why must we the transformation be bi-directional? • Here the new problem can be reduced to the TSP problem. • Thus, a TSP algorithm could be used to solve the new problem. NP-Complete 3SAT TSP New Problem Graph Coloring Max Clique
NP-Complete • Why must we the transformation be bi-directional? • A reduction exists that can can transform the sorting problem to the TSP problem • But, sorting is a Polynomial problem, so this alone isn’t enough to show that sorting is NP-Complete NP-Complete 3SAT TSP New Problem Graph Coloring Max Clique
NP-Complete • Why must we the transformation be bi-directional? • Here the TSP problem could be reduced to the new problem • Thus, any algorithm that solves the new problem could be used to solve the TSP problem. NP-Complete 3SAT TSP New Problem Graph Coloring Max Clique
NP-Complete • Why must we the transformation be bi-directional? • First, there exists no such reduction to go from TSP to sorting, and this is why Sorting is not NP Complete. • But if such a reduction existed than any algorithm for solving the new problem, could be used to solve all NP-Complete problems NP-Complete 3SAT TSP New Problem Graph Coloring Max Clique
This direction proves that the new problem has an NP Algorithm in the first place NP-Complete • Why must we the transformation be bi-directional? • But this direction is all that matters, right? • Wrong, some NP-Complete problems can be reduced to problems that aren’t even in NP. NP-Complete 3SAT TSP New Problem Graph Coloring Max Clique
NP-Complete • Once both reductions have been proven, the new problem gets added to the set of NP-Complete problems. • By the principle of transitivity, all NP-Complete problems can be reduce to the new problem and • The new problem can be reduced to all NP-Complete problems NP-Complete 3SAT New Problem TSP Graph Coloring Max Clique
The power of NP-Complete • Adds mounting evidence that P != NP • More problems = more opportunity to find a polynomial algorithm for an NP-Complete problem • All you have to do is prove two reductions • One establishes that an NP algorithm exists • The other establishes equivalence (one algorithm can solve all problems)
Proving that P = NP • All you have to do is find a problem with a known polynomial algorithm, and • Develop a reduction from any of the NP-complete problems (over 100). NP-Complete 3SAT Polynomial reduction TSP Problem with known Polynomial Algorithm Graph Coloring Max Clique
Proving that P = NP • This should be really easy because there are... • Tons of problems with Polynomial algorithms, and • Tons of problems in NP-complete that could be reduced... NP-Complete 3SAT Polynomial reduction TSP Problem with known Polynomial Algorithm Graph Coloring Max Clique
Proving that P = NP • This has never been done, which tell you something... • Probably P != NP. NP-Complete 3SAT Polynomial reduction TSP Problem with known Polynomial Algorithm Graph Coloring Max Clique
Imagine if you had a function that could generate a good solution for an NP-complete problem (in polynomial time). Imagine if you had function that could verify the optimality(in polynomial time). Put the algorithm in an infinite loop until an optimal solution was verified. Halting Problem: Will the algorithm every terminate? Harder Halting Problem: Will it terminate in polynomial time? while (!optimal) { a = generator(input); if (is_optimal(a)) optimal = true; } Halting Problem
Halting Problem • If the answer generator is good enough, the algorithm might always stop. • If the answer generator is really good, that algorithm might always stop in polynomial time. Polynomial Halting Problem: • Given N, will the algorithm will finish in O(Nk) time where k is a constant?
Halting Problem • Not only is it impossible to verify or prove that a program will terminate in polynomial time, but... • It is impossible to verify or prove that a program will even terminate (in the general case). • The halting problem is interesting because it shows how difficult it is to prove that an NP-complete problem can NOT be solved in polynomial time.
Halting Problem halt(p, i) • returns true if p describes a program that halts when given as input the string i, and • returns false otherwise. Interesting fact: It is impossible to create a halt(p,i) algorithm that will terminate for all programs.There are some programs where halt(p,i) will run forever and we can prove it.
Halting Problem Important connection If you can’t write an algorithm to determine if an arbitrary program halts then, You can’t write an algorithm to determine if an arbitrary program halts in polynomial time. Thus, there may be no way to prove that a problem can NOT be solved in polynomial time.
Something to ponder... while (!optimal) { a = generator(i); if (is_optimal(a)) optimal = true; } • Imagine if you feed halt(p,i) the following program... ....and its been running for 10 years? • It could still be polynomial (imagine O(n10) for n = 1,000) and halt(p,i) might be just about to finish and return true. • But you will never know if halt(p,i) will eventually return an answer and you could be waiting forever
Halting Problem Some background: • Any program can be encoded as a binary string. • Any input can be encoded as a binary string. • You could actually feed an algorithm its own encoding as an input. • Lets call this algorithm self-cannibalism.
Halting Problem Some background: • I could feed a program itself in the following way... halt(p, p). • Halt-pee-pee is the self-cannibalistic halting problem: Will a program halt if you feed it its own encoding as input? • If we could truly implement the halt algorithm to return an answer for all input, then it should even work on p,p.