12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sec. 17.3 Impossible Problems 58117.3 Impossible ProblemsEven the best programmer sometimes writes a program that goes in<strong>to</strong> an infiniteloop. Of course, when you run a program that has not s<strong>to</strong>pped, you do not knowfor sure if it is just a slow program or a program in an infinite loop. After “enoughtime,” you shut it down. Wouldn’t it be great if your compiler could look at yourprogram <strong>and</strong> tell you before you run it that it might get in<strong>to</strong> an infinite loop? Alternatively,given a program <strong>and</strong> a particular input, it would be useful <strong>to</strong> know ifexecuting the program on that input will result in an infinite loop without actuallyrunning the program.Unfortunately, the Halting Problem, as this is called, cannot be solved. Therewill never be a computer program that can positively determine, for an arbitraryprogram P, if P will halt for all input. Nor will there even be a computer programthat can positively determine if arbitrary program P will halt for a specified input I.How can this be? Programmers look at programs regularly <strong>to</strong> determine if they willhalt. Surely this can be au<strong>to</strong>mated. As a warning <strong>to</strong> those who believe any programcan be analyzed in this way, carefully examine the following code fragment beforereading on.while (n > 1)if (ODD(n))n = 3 * n + 1;elsen = n / 2;This is a famous piece of code. The sequence of values that is assigned <strong>to</strong> nby this code is sometimes called the Collatz sequence for input value n. Doesthis code fragment halt for all values of n? Nobody knows the answer. Everyinput that has been tried halts. But does it always halt? Note that for this codefragment, because we do not know if it halts, we also do not know an upper boundfor its running time. As for the lower bound, we can easily show Ω(log n)(seeExercise 3.14).Personally, I have faith that someday some smart person will completely analyzethe Collatz function <strong>and</strong> prove once <strong>and</strong> for all that the code fragment halts forall values of n. Doing so may well give us techniques that advance our ability <strong>to</strong>do algorithm analysis in general. Unfortunately, proofs from computability — thebranch of computer science that studies what is impossible <strong>to</strong> do with a computer— compel us <strong>to</strong> believe that there will always be another bit of program code thatwe cannot analyze. This comes as a result of the fact that the Halting Problem isunsolvable.

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

Saved successfully!

Ooh no, something went wrong!