Prove the Impossible Sep 28 No program can check whether another program will run into infinite loop. x=0
Today’s Plan Halting Problem Fifteen Puzzle Checker Jumping
Halting Problem Now we study the most famous problem in computer science. The halting problem: Can we write a program which detects infinite loop? We want a program H that given any program P and input I: H(P,I) returns “halt” if P will terminate given input I; H(P,I) returns “loop forever” if P will not terminate given input I. The halting problem: Does such a program H exist? Note that the program H can not just simulate the program P on input I; if P halts on I, then H can return halt successfully; but if P loops forever on I, then H will also loop forever.
Halting Problem We want a program H that given any program P and input I: H(P,I) returns “halt” if P will terminate given input I; H(P,I) returns “loop forever” if P will not terminate given input I. The halting problem: Does such a program H exist? Prove by contradiction: Suppose, by contradiction, that H exists. Both P and I are binary strings. H should be able to determine if P will terminate given itself as the input. That is, H(P,P) will either returns “halt” or “loop forever”.
Halting Problem Construct an “inverter” program K which does the following given input P: if H(P,P) returns “halt”, then K(P) will “loop forever” ; if H(P,P) returns “loop forever”, then K(P) will “halt”. Inverter: Do the opposite to what H says. H(P,I) K(P) Input for K(P) program P P:=P I:=P Input for program H(P,I) output H(P,P) If H(P,P)=halt Loop forever If H(P,P)= loop forever halt
Halting Problem H(P,I) K(P) Input for K(P) program P P:=P I:=P Input for program H(P,I) output H(P,P) If H(P,P)=halt Loop forever If H(P,P)= loop forever halt What happen if K is the input to K? What is K(K)?
Halting Problem What happen if K is the input to K? What is K(K)? H(P,I) K(K) Input for K(K) program K P:=K I:=K Input for program H(P,I) output H(K,K) If H(K,K)=halt Loop forever If H(K,K)= loop forever halt Case 1: Suppose H(K,K) says “halt”, that is H determines K(K) will “halt”. But then K(K) will “loop forever”, which is exactly the opposite to the answer.
Halting Problem Case 2: Suppose H(K,K) says “loop forever”, that is H determines K(K) will “loop”, But then K(K) will “halt”, which is exactly the opposite to the answer. What happen if K is the input to K? What is K(K)? H(P,I) K(K) Input for K(K) program K P:=K I:=K Input for program H(P,I) output H(K,K) If H(K,K)=halt Loop forever If H(K,K)= loop forever halt
Halting Problem In either case, H outputs wrong answer to K(K), this contradicts that such a program exists. The proof is due to Alan Turing (1936); you will learn more in CSC Intuitively, no program can determine whether K halts when given input K, because the program K will do the opposite “after” you give an answer. Q.E.D.
Today’s Plan Halting Problem Fifteen Puzzle Checker Jumping
Invariant Method 1.Find properties (the invariants) that are satisfied throughout the whole process. 2.Show that the target do not satisfy the properties. 3.Conclude that the target is not achievable.
Prove the Possible The secret. Is it always possible to fill in the board by dominos, if two squares of different colors are taken out?
Prove the Possible The secret. The two squares will break the long cycle into two paths, but since they are of different colors both paths will be of even length.
Fifteen Puzzle Move: can move a square adjacent to the empty square to the empty square.
Fifteen Puzzle Initial configuration Target configuration Is there a sequence of moves that allows you to start from the initial configuration to the target configuration?
Invariant Method 1.Find properties (the invariants) that are satisfied throughout the whole process. 2.Show that the target do not satisfy the properties. 3.Conclude that the target is not achievable. What is an invariant in this game?? This is usually the hardest part of the proof.
Hint Initial configuration Target configuration ((1,2,3,…,14,15),(4,4)) ((1,2,3,…,15,14),(4,4)) Hint: the two states have different parity.
Parity Given a sequence, a pair is “out-of-order” if the first element is larger. For example, the sequence (1,2,4,5,3) has two out-of-order pairs, (4,3) and (5,3). Given a state S = ((a1,a2,…,a15),(i,j)) Parity of S = (number of out-of-order pairs + row) mod 2 row number of the empty square More formally, given a sequence (a 1,a 2,…,a n ), a pair (i,j) is out-of-order if i a j.
Hint Initial configuration Target configuration ((1,2,3,…,14,15),(4,4)) ((1,2,3,…,15,14),(4,4)) Clearly, the two states have different parity. Parity of S = (number of out-of-order pairs + row) mod 2
Invariant Method 1.Find properties (the invariants) that are satisfied throughout the whole process. 2.Show that the target do not satisfy the properties. 3.Conclude that the target is not achievable. Invariant = parity of state Claim: Any move will preserve the parity of the state. Proving the claim will finish the impossibility proof. Parity is even Parity is odd
Proving the Invariant Claim: Any move will preserve the parity of the state. Parity of S = (number of out-of-order pairs + row) mod 2 ???? ?a? ???? ???? ???? ?a? ???? ???? Horizontal movement does not change anything…
Proving the Invariant Claim: Any move will preserve the parity of the state. Parity of S = (number of out-of-order pairs + row) mod 2 ???? ?ab1b2 b3?? ???? ???? ?b1b2 b3a?? ???? Row number has changed by 1 To count the change on the number of out-of-order pairs, we can distinguish 4 cases, depending on the relative order of a among (a,b1,b2,b3).
Proving the Invariant Claim: Any move will preserve the parity of the state. Parity of S = (number of out-of-order pairs + row) mod 2 ???? ?ab1b2 b3?? ???? ???? ?b1b2 b3a?? ???? Row number has changed by 1 Case 1: when a is largest, then the number of out-of-order pairs will decrease by three, and since the row number is changed by one, the parity is still the same.
Proving the Invariant Claim: Any move will preserve the parity of the state. Parity of S = (number of out-of-order pairs + row) mod 2 ???? ?ab1b2 b3?? ???? ???? ?b1b2 b3a?? ???? Row number has changed by 1 Case 2: when a is the second largest, then the number of out-of-order pairs will decrease by one, and since the row number is changed by one, the parity is still the same. (The remaining case analysis is the same.)
Proving the Invariant Claim: Any move will preserve the parity of the state. Parity of S = (number of out-of-order pairs + row) mod 2 ???? ?ab1b2 b3?? ???? ???? ?b1b2 b3a?? ???? If there are (0,1,2,3) out-of-order pairs in the current state, there will be (3,2,1,0) out-of-order pairs in the next state. Row number has changed by 1 So the parity stays the same! We’ve proved the claim. Difference is 1 or 3.
Fifteen Puzzle Initial configuration Target configuration Is there a sequence of moves that allows you to start from the initial configuration to the target configuration?
Fifteen Puzzle Initial configuration Target configuration Number of out-of-order pairs = 0 Row of empty square = 4 Parity is even. Number of out-of-order pairs = … + 1 = 14(13)/2 = 91 Row of empty square = 4 Parity is odd. Impossible!
Today’s Plan Halting Problem Fifteen Puzzle Checker Jumping
K=4 x=0 Remember the checker game that we have seen before?
K=5 Sorry there are no slides for this proof. The proof can be found in “Mathematical Gems II” by Honsberger. There are three books on Mathematical Gems and all are excellent. This can also be solved by the invariant method.
Remarks and References The argument used in the halting problem is called the “diagonalization” method. This is originally used by Canton to prove that real numbers are “more” than Integers (see the next slide). This method has many other applications in theoretical computer science. For the fifteen puzzle, one can prove that as long as the two states are of the same parity, then one state can reach the other. See Another interesting application of the invariant method is the Nim game. See The solution of the checker jumping problem is from “Mathematical Gems II”, which is available in our university library.
Question: Is the set of real number countable? Theorem: No surjection mapping positive integers to real numbers. (Appendix) Real Numbers vs Positive Integers The string map to the first natural number The string map to the fifth natural number The opposite of the diagonal It can not be in any row i because its i-th bit is different, and so this string is not mapped!