Spring Semester 2008

January 28, 2008 – May 2, 2008

Assignment 6: Bunches of Theory

Out on: March 4, 2008
Due by: March 10, 2008, noon (before lecture)
Collaboration: None
Grading: Packaging 10%, Style 10%, Performance 10%, Functionality 70%

Overview

The sixth assignment concentrates on more theoretical material we covered in the fifth and sixth week. Note that since there are no programming problems, the breakdown above (and the note about error free code below) don't really apply. :-)

Problem 1: Boolean Lambdas

In class we defined the encoding of true, false, and if into the λ-calculus as follows:

I mentioned that we can also encode the usual and, or and not functions. Do exactly that: For each of those functions, derive an encoding into λ-calculus. Give your encodings as λ-expressions without abbreviations; reduce your λ-expressions as far as possible.

Please make sure to derive your encodings. Don't just "state" the encoding (that would be too easy :-), show us step-by-step how you developed it. Also describe the overall idea you used to get to your encodings verbally.

Problem 2: Additive Lambdas

The tutorial you should be reading this week discusses how to encode basic arithmetic into the λ-calculus. In section 2.1 of the tutorial, you'll find the example addition 2S3 which the tutorial claims is equivalent to SS3 which the tutorial claims is equivalent to 5.

All these claims are great, and Raul Rojas is a trustworthy guy, but for this problem, you must do the complete reduction from 2S3 to 5 explicitly. Be sure to indicate which reductions you perform in each step, and show all your steps.

This is going to be quite tedious, and I apologize for that, but it gives the somewhat fascinating insight that all of this stuff actually works. Of course you are free to use the abbreviations introduced earlier in the tutorial for some of the intermediate steps, but be careful: it's even easier to make a mistake with those sometimes...

Problem 3: Insertion Sort

In lecture, we discussed Bubble Sort and Selection Sort in some detail. We showed that they are both quadratic aka O(n*n) in the size n of the array we wish to sort (in the worst case anyway).

Insertion Sort is yet another simple sorting algorithm for arrays, and it works as follows: Assume that the first element of the array is sorted (trivially true). Go through the (remaining) unsorted portion of the array element by element, and for each try to insert it at the correct position in the sorted portion. For the second element, it either has to stay where it is (if it is greater than the first), or it has to go "before" the first element (if it is less than the first), thus extending the sorted portion by one. Keep going until there is no element left in the unsorted portion. (This is how most people sort a hand of cards.)

Check out this article on Wikipedia for some background information, animations, and a pseudo-code outline of the algorithm.

Assuming the pseudo-code from that article, show that Insertion Sort is also O(n*n) in the worst case, just like Bubble Sort and Selection Sort. Do this in two steps just like we did it in lecture: first derive an "exact" polynomial for the number of basic operations (assignments, arithmetic, comparisons, etc.) executed in the worst case, then argue that the polynomial is in O(n*n).

Problem 4: Binary Insertion Sort

The inner loop of Insertion Sort both locates the spot where an element should go and moves existing elements in the sorted portion out of the way to make room for the new one.

Since the sorted portion is sorted (doh!), we could use Binary Search to locate the correct spot where a new element should be placed in O(log n) time, and then move all the other elements out of the way.

While this will complicate the algorithm, one would hope that it will also improve performance. It does not, however, at least not in the asymptotic analysis. That is, Binary Insertion Sort is still O(n*n), just like plain Insertion Sort. Argue convincingly why using Binary Search to locate the insertion point does not improve the performance of Insertion Sort significantly.

Deliverables

Please turn in a gzip compressed tarball of your assignment; the filename should be cs102-assign-6-login.tar.gz with login replaced by your Unix login name on ugradx.cs.jhu.edu (so I would use cs120-assign-6-phf.tar.gz). The tarball should contain no derived files whatsoever (i.e. no executable files), but allow building all derived files. Include a README file (a plain Unix text file) that contains your answers to written problems, briefly explains what your programs do, and has any other notes you want us to check out before grading.

Grading

For reference, here is a short explanation of the grading criteria. Packaging refers to the proper organization of the stuff you hand in, following the guidelines for Deliverables above. Style refers to programming style, including things like consistent indentation, appropriate identifiers, useful comments, suitable documentation, etc. Simple, clean, readable code is what you should be aiming for. Performance refers to how fast your program can produce the required results compared to other submissions. Design refers to proper modularization and the proper choice of algorithms and data structures. Functionality refers to your programs being able to do what they should according to the specification given above; if the specification is ambiguous and you had to make a certain choice, defend that choice in your README file.

If your programs cannot be built/run you will get no points whatsoever. If your programs fail miserably even once, i.e. terminate with an exception of any kind or dump core, we will take off 10%. Finally, make sure to include your name and email address in every file you turn in!

Bonus Problem

Do the suggested project five from the tutorial: Define the factorial function in the λ-calculus! Note that we won't give you extra points for this, but we'll give you extra kudos. :-)