11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 17.3 Impossible Problems 55917.3.2 The Halting Problem Is UnsolvableWhile there might be intellectual appeal to knowing th<strong>at</strong> there exists some functionth<strong>at</strong> cannot be computed by a computer program, does this mean th<strong>at</strong> there is anysuch useful function? After all, does it really m<strong>at</strong>ter if no program can compute a“nonsense” function such as shown in Bin 4 of Figure 17.7? Now we will proveth<strong>at</strong> the Halting Problem cannot be computed by any computer program. The proofis by contradiction.We begin by assuming th<strong>at</strong> there is a function named halt th<strong>at</strong> can solve theHalting Problem. Obviously, it is not possible to write out something th<strong>at</strong> does notexist, but here is a plausible sketch of wh<strong>at</strong> a function to solve the Halting Problemmight look like if it did exist. Function halt takes two inputs: a string representingthe source code for a program or function, <strong>and</strong> another string representing the inputth<strong>at</strong> we wish to determine if the input program or function halts on. Function haltdoes some work to make a decision (which is encapsul<strong>at</strong>ed into some fictitiousfunction named PROGRAM HALTS). Function halt then returns true if the inputprogram or function does halt on the given input, <strong>and</strong> false otherwise.bool halt(String prog, String input) {if (PROGRAM HALTS(prog, input))return true;elsereturn false;}We now will examine two simple functions th<strong>at</strong> clearly can exist because thecomplete code for them is presented here:// Return true if "prog" halts when given itself as inputbool selfhalt(String prog) {if (halt(prog, prog))return true;elsereturn false;}// Return the reverse of wh<strong>at</strong> selfhalt returns on "prog"void contrary(String prog) {if (selfhalt(prog))while (true); // Go into an infinite loop}Wh<strong>at</strong> happens if we make a program whose sole purpose is to execute the functioncontrary <strong>and</strong> run th<strong>at</strong> program with itself as input? One possibility is th<strong>at</strong>the call to selfhalt returns true; th<strong>at</strong> is, selfhalt claims th<strong>at</strong> contrarywill halt when run on itself. In th<strong>at</strong> case, contrary goes into an infinite loop (<strong>and</strong>thus does not halt). On the other h<strong>and</strong>, if selfhalt returns false, then halt isproclaiming th<strong>at</strong> contrary does not halt on itself, <strong>and</strong> contrary then returns,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!