halting problem and tsp wednesday, week 8. background - halting problem common error: program goes...

27
Halting Problem and TSP Wednesday, Week 8

Upload: roderick-jefferson

Post on 17-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Halting Problem and TSP

Wednesday, Week 8

Page 2: 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

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

Page 3: 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

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>”);

}

Page 4: 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

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!

Page 5: 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

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.

Page 6: 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

Halting Problem

In other words,Program H will always halt and give the

correct answer no matter what program has been given as input.

Hprogram Halts? Yes or No

Page 7: 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

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.

Page 8: 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

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.

Page 9: 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

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.

Page 10: 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

Assume Halting Problem OK

Let H be a program (or sub-program) that determines whether a program will halt.

Hprogram Halts? Yes or No

Page 11: 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

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.

Hprogram

program

Page 12: 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

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.

Page 13: 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

The program P

function P (program){

var halts = H(program);if (halts == True)

infinite loop;else

stop;}

Page 14: 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

What Can We Do With P?

Let’s give P a copy of itself as its input.

HYes

No

program:

Page 15: 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

What If P Halts?

HYes

No

program:

Page 16: 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

What If P Loops Indefinitely?

HYes

No

program:

Page 17: 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

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.

Page 18: 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

What Went Wrong?

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

Page 19: 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

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.

Page 20: 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

Conclusions

Self-referentiality is a real problem with programsRice’s theorem says that “Any nontrivial property of programs is undecidable.”Thus, we can’t write programs to answer questions about programs.

Page 21: 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

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.

Page 22: 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

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

Page 23: 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

TSP Example #1

Page 24: 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

TSP Answer #1

Page 25: 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

TSP Example #2

Page 26: 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

TSP Answer #2

Page 27: 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

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.