You are on page 1of 10

Research Assignment

Name: Ehtisham Aslam

Reg No: 201-S19-010

Department: BSCS 5th Semester

Subject: DAA

Date: 23-06-2016

Ehtisham Aslam Insturctor: Engr Salman Shah


Q.1. Prove that Halting Problem is undecidable. Justify your proof with an example.

Some problems take a very long time to solve, so we use algorithms


that give approximate solutions. There are some problems that a
computer can never solve, even the world's most powerful computer
with infinite time: the undecidable problems.

An undecidable problem is one that should give a "yes" or "no"


answer, but yet no algorithm exists that can answer correctly on all
inputs.

The halting problem


Alan Turing proved the existence of undecidable problems in 1936 by
finding an example, the now famous "halting problem":

Based on its code and an input, will a particular program ever finish
running?

For example, consider this program that counts down:

num ← 10

REPEAT UNTIL (num = 0) {

DISPLAY(num)

num ← num - 1

That program will halt, since num eventually becomes 0.

Ehtisham Aslam Insturctor: Engr Salman Shah


Compare that to this program that counts up:

num ← 1

REPEAT UNTIL (num = 0) {

DISPLAY(num)

num ← num + 1

It counts up forever, since num will never equal 0.

Algorithms do exist that can correctly predict that the first program


halts and the second program never does. These are simple programs
which don't change based on different inputs.

However, no algorithm exists that can analyze any program's code


and determine whether it halts or not.

In order to prove that such an algorithm cannot possibly exist, Turing


used a "proof of contradiction".

We start by imagining that an algorithm does exist that can determine


a program's haltability.

Then we propose a program called HaltChecker that takes two inputs,


a program's code and an input for that program. It then uses that
hypothetical haltability algorithm to return either "halts" or "never".

Ehtisham Aslam Insturctor: Engr Salman Shah


This flow chart visualizes HaltChecker:

If we input those earlier programs into HaltChecker, we'd see the


counting-down program results in "halts" and the counting-up program
results in "never". Note that it's not actually running the programs, it's
looking at their code and deciding based on the code structure.

But now we propose a program called Reverser. It takes a single


input, a program's code. It sends HaltChecker that program as both
the program to analyze and the input for the
program. HaltChecker then determines if the input program halts when

Ehtisham Aslam Insturctor: Engr Salman Shah


given itself as input. If HaltChecker returns "halts", Reverser runs an
infinite loop. If HaltChecker returns "never", Reverser immediately
returns.

This flow chart visualizes Reverser:

If we input the counting-down program into Reverser, it will see


that HaltChecker returns "halts" and thus decide to loop infinitely.

Ehtisham Aslam Insturctor: Engr Salman Shah


If we input the counting-up program into Reverser, it will see
that HaltChecker returns "never" and thus decide to return
immediately.

Ehtisham Aslam Insturctor: Engr Salman Shah


Reverser is a strange program indeed. It halts when it finds out that
another program doesn't halt and likes to go on forever when it finds
out another program does halt. That's weird but that's okay, strange
programs are allowed in the world.

But here's where it all falls apart: what happens if we


input Reverser itself into Reverser?

HaltChecker analyzes the Reverser code to determine whether it


halts. If it decides that it doesn't halt, then it returns
"never". Reverser sees that result and returns immediately.

Ehtisham Aslam Insturctor: Engr Salman Shah


But, wait a second! HaltChecker just claimed that Reverser never
halts, and then it went ahead and halted. HaltChecker did not give us
a correct answer.

What if HaltChecker returns "halts" instead? When Reverser sees that


result, it loops infinitely.

Ehtisham Aslam Insturctor: Engr Salman Shah


HaltChecker just claimed that Reverser halts, and yet, it went on
forever. Once again, HaltChecker did not give us a correct answer. In
fact, there's no way for HaltChecker to give us a correct answer in this
situation.

Thus, we've proved that a perfectly correct halt-predicting algorithm


can never exist and that the halting problem is undecidable.

It took me a while to really understand the halting problem. If you're


having some trouble too, this animated video might be helpful.

Ehtisham Aslam Insturctor: Engr Salman Shah


More undecidable problems
Computer scientists and mathematicians have discovered many more
undecidable problems. Quite a few of those, once simplified, look like
another case of the halting problem. Generally, all the undecidable
problems revolve around the difficulty of determining properties about
the input and output of programs.

It's helpful to realize when we're dealing with an undecidable problem.


We can then accept that our program can't correctly answer yes or no
on all inputs, and come up with a different approach.

Ehtisham Aslam Insturctor: Engr Salman Shah

You might also like