270 likes | 425 Views
Halting Problem and TSP. Wednesday, Week 8. Background - Halting Problem. Common error: Program goes into an infinite loop. Wouldn’t it be nice to have a tool that would warn us that our program has this bug? (We could make a lot of money if we could develop such a tool!). Example.
E N D
HaltingProblemandTSP Wednesday, Week 8
Background - Halting Problem • Common error: Program goes into an infinite loop. • Wouldn’t it be nice to have a tool that would warn us that our program has this bug? (We could make a lot of money if we could develop such a tool!)
Example • Infinite loop - we get into a loop but for some reason we never get out of it. • for (i = 0; i < 10; i--) { document.writeln(“Help, I’m in an infinite loop!<BR>”); }
Example Output Help, I’m in an infinite loop! Help, I’m in an infinite loop! Help, I’m in an infinite loop! Help, I’m in an infinite loop! Help, I’m in an infinite loop! Help, I’m in an infinite loop! …
Definition Design a program, H, that will do the following: • Take as its input a description of a program P (in binary, say). • Halt eventually and answer "yes" if P will eventually halt, or halt and answer "no" if P will run forever.
Halting Problem In other words, Program H will always halt and give the correct answer no matter what program has been given as input. H program Halts? Yes or No
Proving the Halting Problem • We want to prove that the halting problem is non-computable. • In other words, there is no algorithm that we can use to tell whether or not a program will halt. • We will use proof by contradiction.
Proof by Contradiction • Prove: if x+y >= 2, then x>=1 or y>=1. • Assume that x+y>=2 and that x<1 and y<1. • Then, x+y < 1+1 = 2. • This is a contradiction since x+y >=2! • Thus our assumption that x<1 and y<1 is false. Note: NOT(x>=1 or y>=1) = (x<1 and y<1) by DeMorgan’s Laws.
Proof by Contradiction: • We’ll assume that the Halting Problem CAN be computed. • We’ll develop another program that uses the Halting Problem function. • We’ll find ourselves caught in a paradox (the contradiction). • We’ll have proven that our original assumption is false.
Assume Halting Problem OK • Let H be a program (or sub-program) that determines whether a program will halt. H program Halts? Yes or No
Let’s Build Another Program • Let P be a program that uses H. • For any given program, P will call H and pass it the given program. P H program program
What does P do? • Now, P acts as follows: • P takes a program as input and feeds the program to H as input. • If H answer yes, then P will enter an infinite loop and run forever. • If H answer no, then P will stop.
The program P function P (program) { var halts = H(program); if (halts == True) infinite loop; else stop; }
What Can We Do With P? • Let’s give P a copy of itself as its input. P P loops H Yes P P program: No P halts
What If P Halts? P P loops H Yes P P program: No P halts
What If P Loops Indefinitely? P P loops H Yes P P program: No P halts
The Paradox • If P is a program that halts when given itself as its input, then, when given itself as input, P will go into an infinite loop. • If P is a program that loops indefinitely when given itself as its input, then , when given itself as input, P will halt immediately.
What Went Wrong? • There’s nothing wrong with P, itself. • The problem must be with the assumption that we could write H.
So, Proof by Contradiction: • We assumed that the Halting Problem COULD be computed. • We developed another program that used the Halting Problem function. • We found ourselves caught in a paradox (the contradiction). • We proved that the Halting Problem is not computable.
Conclusions • Self-referentiality is a real problem with programs • Rice’s theorem says that “Any nontrivial property of programs is undecidable.” • Thus, we can’t write programs to answer questions about programs.
Intractability • The Traveling Salesperson Problem is intractable. • Proving it is beyond the scope of this class. • We will just understand the problem and how hard it is.
Traveling Sales Problem • A salesperson has a group of cities that he/she needs to visit. • There are a bunch of distances between the cities. • Our salesperson has to visit all the cities by following a path with the least distance (or cost).
Traveling Sales Problem • Conclusions: • The only correct algorithm we could come up with had to examine all possible paths. • The only correct algorithm that anyone has come up with has to examine all possible paths. • Thus this algorithm is O(n!) and intractable.