1 / 27

Halting Problem and TSP

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.

kueng
Download Presentation

Halting Problem and TSP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. HaltingProblemandTSP Wednesday, Week 8

  2. 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!)

  3. 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>”); }

  4. 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! …

  5. 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.

  6. 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

  7. 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.

  8. 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.

  9. 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.

  10. 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

  11. 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

  12. 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.

  13. The program P function P (program) { var halts = H(program); if (halts == True) infinite loop; else stop; }

  14. 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

  15. What If P Halts? P P loops H Yes P P program: No P halts

  16. What If P Loops Indefinitely? P P loops H Yes P P program: No P halts

  17. 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.

  18. What Went Wrong? • There’s nothing wrong with P, itself. • The problem must be with the assumption that we could write H.

  19. 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.

  20. 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.

  21. 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.

  22. 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).

  23. TSP Example #1

  24. TSP Answer #1

  25. TSP Example #2

  26. TSP Answer #2

  27. 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.

More Related