Computer Science 60
Principles of Computer Science
Spring 2011


Assignment 12: More JFLAP, Proofs of Uncomputability, and a little review [100 Points]
Due Monday, April 25 11:59pm

This week's homework is a combination of several ideas you've learned so far in CS60.  First you'll practice building a Turing Machine, and then prove that some languages are not computable at all.  Next you'll have a chance to write the same (simple) program in 3 different languages.

This is the second-to-last (required) homework in CS60.  There will be one more required assignment, and then a totally optional extra credit assignment offered during projects week.

A note about partners: This week problem 1 should be done individually, though as always you can discuss your general approach with anyone else, especially the grutors and profs.  For problem 2 you should again feel free to discuss your approach, but you must write up both proofs individually.  Problem 3 may be done individually or with a partner (and you only need to submit one version between the two of you).

The problems

Problem 1: Turing Machines


(20 points -- this must be done individually)
Build a Turing Machine to accept the langage:
  L = { w | w contains only 0's and the length of w is a power of 2 }.
Although it is not regular, it is decidable. That is to say, there is a Turing machine that accepts it.

We highly recommend spending some time thinking about this problem and sketching a solution on paper before using JFLAP. If you design the Turing Machine carefully, you can build it using fewer than nine states. (Perhaps far fewer -- see the extra credit...)

Save your Turing Machine in a file called part1.jff.

Problem 2: (Un)computing at Nanosoft

(40 points - everyone must submit their own writeup, but you may discuss your approach with any other student, or the grutors or profs)

You have been hired by Nanosoft. Your boss, Gill Bates, drops by your spacious cubicle sipping a refreshing and stimulating Jolt Cola ("All the sugar, and twice the caffeine!").

Gill begins by reminding you that a decider is a program that takes an input and eventually halts and accepts (says "yes" to its inputs) or rejects (says "no" to its input). For example, a decider for the prime numbers would take a number, perhaps encoded in binary, as input and would eventually halt saying "yes" (it's prime) or "no" (it's not prime).

"OK, so here's your first task," says Gill. "We have decided to change our interview process -- we're going to ask all of our job candidates to see if they can write a decider which takes as input a string of 0's and determines whether the number of 0's present is a power of 2."

You sigh, realizing that this would have made for a much less stressful interview than you'd had, what with the complete rewrite of the company's recent operating system you'd had to build... .

"Here's what I need from you," continues Gill. "I need a program which we'll call a Power of Two Decider Tester (POTDT). The POTDT takes as input a program. The POTDT then decides whether P is a legitimate decider for strings whose length is a power of 2. That is, the POTDT eventually halts and says yes if the program it received was a correct submission for this competition and otherwise the POTDT halts and says no. I need this POTDT program written by tomorrow at 6 AM."

Prove to Gill that this cannot be done, not by 6 AM and not EVER! You may use diagrams to help explain your proof, but your proof must be clear, precise, and rigorous. Your submission on this and the following problem will be graded on clarity and precision of explanation.

Here is a link to an example write-up of the proof we did in class, that the blank-tape halting problem is undecidable.

"Drat!" says Gill upon hearing the news. "OK, then, in that case, I'd like you to write a program called a Regularity Checker (RC). The Regularity Checker takes as input a program P (e.g., a Turing Machine) and it needs to determine whether or not the set of strings accepted by P is a regular language. The RC must always halt with an answer of yes or no!" Prove to Gill that this too is impossible. Again, you may use pictures to help you explain your proof, but your proof must be clear, precise, and rigorous.

Note that if a turing machine runs forever on an input, that input is considered REJECTED for the purposes of defining the language that the TM accepts. This might help!
Note: the idea in both of these problems is similar to the examples we did in which we reached a contradiction by creating a halt-checker out of the program that we hope to prove uncomputable. In this case, you'll want to create a halt-checker from a POTDT and a RC. Keep in mind that you can build any function/program/machine you like in order to coax the POTDT and RC into deciding whether a program P halts on an input w.
For this reason, simply citing Rice's theorem isn't allowed for this problem :-)
Submitting these proofs    Submit both of your proofs in a plain-text file named part2.txt.

Part 3: languages review...

(40 points - may be done individually or in a pair)

For this problem, you'll submit programs that solve the "best investing strategy problem" in each of the programming languages we have used this term: Prolog, Java, and Scheme. You may remember this problem from CS 5: here the goal is to review each of the languages we have used (not to challenge your algorithmic-design skills). However, see the extra-credit for an extension of this problem that might offer a greater algorithmic challenge - and other big-O fun, as well! We will also review these languages in the final week of CS 60 (with some additional algorithm-design challenges involved!)

The problem is one you may remember from CS 5: given a list of stock prices, one per day, find the maximum profit you can obtain with a single buy and a single sell transaction. If you could buy and sell on any of the days, then you could buy at the lowest price and sell at the highest one. Here, that is not permitted: you may only sell on a day that is on or after the day you buy. Because you can sell the same day that you buy, the smallest profit you might earn is 0.

As an example, consider the prices represented by this list:

[60, 100, 17, 33, 21, 20, 43, 59, 55, 18, 2]
in which day 0's price is 60, day 1's price is 100, and so on. Although the maximum price is 100 and the minimum price is 2, the maximum (per-share) profit you can obtain by selling on a day after purchasing is 42: by buying on day 2 at a price of 17 and selling on day 7 at a price of 59. Here is the same list of 11 prices without commas for copy-and-paste into Scheme and Java:
60 100 17 33 21 20 43 59 55 18 2

In a comment in each of your files, state the big-O running time of your solution in terms of the number of prices N in the list. In a sentence or two, justify your running time claim - however, you do not have to formally analyze your code.

Each of these programs should find the maximum profit possible for a list of prices - while respecting the constraint that the sell day is the same or later than the buy day. Here are some details for each of the three languages:

3 totally optional and fun extra credit challenges...