Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Halting Problem and TSP 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! ”); }

4 Example Output Help, I’m in an infinite loop! …

5 Definition Design a program, H, that will do the following: 1.Take as its input a description of a program P (in binary, say). 2.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 programHalts? 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 programHalts? 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. H 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 What does P do ? H program Yes No

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

15 What Can We Do With P? Let’s give P a copy of itself as its input. H Yes No program:

16 What If P Halts? H Yes No program:

17 What If P Loops Indefinitely? H Yes No program:

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

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

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

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

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

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

24 TSP Example #1

25 TSP Answer #1

26 TSP Example #2

27 TSP Answer #2

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


Download ppt "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."

Similar presentations


Ads by Google