University of Virginia, Department of Computer Science
CS200: Computer Science, Spring 2002

Notes: Friday 22 February 2002
Schedule

The exam will be open book and notes. You are not permitted to discuss the exam question with anyone other than the course staff.

You will not be allowed to use a computer on the exam, other than to send email to cs200-staff@cs.virginia.edu to ask for clarification. In particular, you will not be permitted to run DrScheme or any other Scheme interpreter between picking up the exam Monday and returning it Wednesday. You will not be permitted to search the web for help on exam questions.

Knapsack Problem

Each semester, Walenda Wahoo gets $200 to buy books from her scholarship. She has to give back any money she does not spend on books. She got caught last semester claiming Sam Adams, Winter Lager was a book for her history class, so she has to be more careful this semester.

Walenda would like to give back as little money as possible. Here are the books she wants to buy this semester and there prices:
    Abelson and Sussman, Structure and Interpretation of Computer Programs, $65
    Ambrose, Undaunted Courage, $17
    Cope and Hofstadter, Virtual Music, $45
    Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid, $14
    Knuth, The Art of Computer Programming, Volumes 1-3, $150
    Singh, The Code Book: The Science of Secrecy from Ancient Egypt to Quantum Cryptography, $12
    Stephenson, Cryptonomicon, $13
    Viega and McGraw, Building Secure Software: How to Avoid Security Problems the Right Way, $55

She can chose any set of books she wants, but cannot buy the same book twice or spend more than $200. For example, should could buy Abelson and Sussman ($65), Cope and Hofstadter ($45), Singh ($12), Stephenson ($14) and Viega and McGraw ($55) for a total of $191 and give back $9. She could do "better" by buying Cope and Hofstadter ($45) and Knuth ($150) for $195, and only giving back $5. Walenda wants a program that tells her which set of books to buy to spend closest to $200 without going over.

This is an instance of the knapsack problem, which is NP-complete. Public-key cryptosystems have been based on believing finding knapsacks is a hard problem.

The straightforward way to solve it is to find all possible sets of books, and select the one with the price closest to $200.

Finding the Power Set

The power set of S is the set of all subsets of S. We will use lists to represent sets, but remember that the order of elements doesn't matter. For example, (list 2 3) and (list 3 2) both represent the same set.

For example, the power set of (list 1 2 3) is (list () (1) (2) (3) (1 2) (1 3) (2 3) (1 2 3)). An equally good answer is (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)). Note that the size of the power set for a set of size n is 2n. You can see this by thinking about what happens when you add a new element to the set: the power set contains all elements of the power set without the new element and each of those sets with the new element added. Hence, adding a new element doubles the size of the power set.

Define a procedure that takes a list (representing a set) as input and evaluates to its powerset. For example,

> (powerset (list 1 2 3))
(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))
Solving the Knapsack
Define a procedure that takes a list of prices and a spending limit, and evaluates to a subset of the price list that is the subset with the closest total to the spending limit. Start by defining a procedure sumlist that takes a list and evaluates to the sum of the numbers in the list.

For example, to solve Walenda's problem:

> (knapsack (list 65 17 45 14 150 12 13 55) 200)
(65 17 45 14 55)
The total is $196 and she is giving back $4 and buying Abelson and Sussman, Ambrose, Cope and Hofstadter, Hofstadter and Viega and McGraw. (Its important that all the books are different prices.)

Orders of Growth
How much work is your knapsack procedure? Express your answer using Θ and n, the number of different books.

Other Topics
If you can do this problem, the actual exam should seem pretty easy. It won't have anything quite this difficult on it.

The exam may also have questions about:

Knapsack Code

DEAR SIR -- A favorable and a confidential opportunity offering by Mr. Dupont de Nemours, who is revisiting his native country gives me an opportunity of sending you a cipher to be used between us, which will give you some trouble to understand, but, once understood, is the easiest to use, the most indecipherable, and varied by a new key with the greatest facility of any one I have ever known. I am in hopes the explanation inclosed will be sufficient. Let our key of letters be [some figures which are illegible] and the key of lines be [figures illegible] and lest we should happen to lose our key or be absent from it, it is so formed as to be kept in the memory and put upon paper at pleasure; being produced by writing our names and residences at full length, each of which containing 27 letters is divided into two parts of 9. letters each; and each of the 9. letters is then numbered according to the place it would hold if the 9. were arranged alphabetically, thus [so blotted as to be illegible]. The numbers over the letters being then arranged as the letters to which they belong stand in our names, we can always construct our key. But why a cipher between us, when official things go naturally to the Secretary of State, and things not political need no cipher. 1. matters of a public nature, and proper to go on our records, should go to the secretary of state. 2. matters of a public nature not proper to be placed on our records may still go to the secretary of state, headed by the word `private.' But 3. there may be matters merely personal to ourselves, and which require the cover of a cipher more than those of any other character. This last purpose and others which we cannot foresee may render it convenient and advantageous to have at hand a mask for whatever may need it. But writing by Mr. Dupont I need no cipher. I require from him to put this into your own and no other hand, let the delay occasioned by that be what it will.

From Thomas Jefferson's letter to the U.S. Minister to France (Robert R. Livingston), Washington, Apr. 18, 1802.


CS 655 University of Virginia
Department of Computer Science
CS 200: Computer Science
David Evans
evans@virginia.edu
Using these Materials