# Class 54: Algorithm Design, Revisited

Back to Implementing Dictionaries with Binary Search Trees. On to Evaluation.

Held: Tuesday, 6 May 2003

Summary: We conclude the subject matter of the class by considering some techniques for designing algorithms.

Related Pages:

Due

Notes:

• What, if anything, can I do to help you get through this difficult time?
• Reminder: Tomorrow's class is reserved for evaluation. Friday's class will be held at Dairy Barn.

Overview:

• Algorithm Design.
• A Problem: Filling Book Shelves.
• A Solution: Dynamic Programming.

## Abstract Data Type Design

• As you should have noted from the past few weeks, there is a standard process you might use when designing new abstract data types.
• First, make a list of the operations that you might need.
• Next, think about the details of those operations.
• Next, add any related procedures that the details reveal (such as predicates that permit the testing of preconditions).
• Yes, there are many subtleties to this process.

## Algorithm Design

• Algorithm design is often much more complicated.
• Why? There are many different approaches one can take.
• For many problems, simple recursion is an appropriate first design technique:
• How can I simplify the problem, creating a new problem of the same type?
• How does a solution to the simplified problem help me solve the bigger problem?
• What is a simple problem I can solve directly?
• How do we simplify? We know (at least) three basic techniques:
• Remove one element (usually the first) or decrement a value by 1.
• Choose the "best" or "worst" element and remove it. (Greed)
• Divide the input in half (Divide and Conquer).
• At times, none of these techniques work.

## The Book-Shelf Packing Problem

• Here's a simple problem for which none of the previous techniques seems to work well.
• You have N linear inches of book shelf space.
• You have a variety of books with different widths (measuered in whole inches) and values.
• Not all value/width ratios are available.
• If some value/width ratio is available, there are arbitrarily many books with that ratio.
• Given those conditions, we need only pay attention to the maximum value for each width.
• Your goal is to choose a subset of the books such that
• the subset fits on the shelf and
• the subset has the highest possible value.
• How do you do it?
• One bad solution: Try every combination.

## Dynamic Programming

• One cool technique is related to the one we used for Fibonacci (oh so many classes ago):
• Make an array of solutions to simpler problems.
• Base the solution on some combination of the previous solutions.
• How can we use this technique to solve book-packing?
• Compute the array, S, for book shelves with widths 0 to N-1
• For each width, W <= N, compute the value gained by taking a book of that width (VW + S[N-W]).
• Take the maximum of those values.
• We need to do a little bit more work to get the solution in addition to the value, but not much.
• Simply store the book chosen at that point.

## History

Thursday, 15 January 2003 [Samuel A. Rebelsky]

• Created as a mostly-blank outline.

Back to Implementing Dictionaries with Binary Search Trees. On to Evaluation.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 6 09:21:14 2003.
The source to the document was last modified on Thu Jan 16 15:16:16 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Outlines/outline.54.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu