Assignments | Course Home | Schedule & Lecture Notes

Saint Louis University

Computer Science 314
Algorithms

Michael Goldwasser

Spring 2008

Dept. of Math & Computer Science

Homework Assignment 06

Dynamic Programming

Contents:


Overview

Topic: Dynamic Programming
Related Reading: Ch. 6
Due: Friday, 2 May 2008, 1:10pm

Please make sure you adhere to the policies on academic integrity.


Practice Problems


Materials to Submit

For this assignment, we are going to go with two implementation problems. For each you will be asked to submit a working implementation (language of your choice) as well as a brief writeup explaining the high-level structure of your dynamic programming problem. Namely, give an outline of:

  1. Describe the space of subproblems (most notably, the parameterization you use).
  2. How many subproblems are there in total?
  3. Give a clear and concise recursive formula which is used to compute the values of the subproblems (including any relevant base cases).
  4. How long does it take to compute the value of a single subproblem using the recursive formula?
  5. How do the subproblem solutions relate to the original goal?
  6. What is the asymptotic running time of your complete program?


Problems to be Submitted (40 points)

  1. (20 points)

    Exercise 6 of Chapter 6 (p. 317-318) describes a model for "pretty printing"lines of text given a maximum line width L. It is based on summing penalties computed as the square of the remaining number of spaces left at the end of a line.

    You are to implement a solution for that problem, however, with one minor change to the model definition. According to the book, a penalty is assessed for all lines, includiing the last; For this assignment, no penalty should be assessed for the final line (as extra slack there is a reasonable trait).

    Write a program named pretty which implements the approach suggested by this problem. Your program should take two command-line arguments, of the form:

          pretty filename L
    
    It should break the original file into presumed "words" based on any form of separating whitespace, and then should compute an optimal layout for the text on a line of width L. Formally, your output should include a first line which is the optimal value of the square of the slacks. Following that, you should output the full text based upon the optimal parition, formatting it on a "page" as shown here with width of 55:
    23
    +-------------------------------------------------------+
    |Call me Ishmael. Some years ago, never mind how long   |
    |precisely, having little or no money in my purse, and  |
    |nothing particular to interest me on sure, I thought I |
    |would sail about a little and see the watery part of   |
    |the world.                                             |
    +-------------------------------------------------------+
    

    A collection of inputs, together with solutions for various widths can be downloaded here .

    Please note that for some of these inputs, there is more than one layout that achieves the optimal value. For example, the above example for ishmael with width 55 could be displayed as

    23
    +-------------------------------------------------------+
    |Call me Ishmael. Some years ago, never mind how long   |
    |precisely, having little or no money in my purse, and  |
    |nothing particular to interest me on sure, I thought   |
    |I would sail about a little and see the watery part of |
    |the world.                                             |
    +-------------------------------------------------------+
    
    This is the only ambiguous example for the ishmael input, however there are many distinct optimal layouts for all of the declaration or moby examples (in fact, the number of distinct optimal layouts for Moby Dick using 40 colums is 4814945566941474631475780164971994191649746425129036276743010247097935668098220462277520269592003627983710127294136847386370559601812072477043533644651317984501222975183065923977476353181737032626197620544697263331654481371291670310445023443859704266411815441430712402289997175081649767094728871933977191435534832975132394763037361030823936000000000000000000000000)

  2. (20 points)

    As a plug for next year's ACM Programming Contest, we borrow a problem modeled after ones that appear in such contests.

    The goal of this problem is to determine the minimum amount that must be spent on gas to drive from one city to another, given information about the distances and prices at intermediate gas stations.

    Formally, we model the problem as follows.

    File Format: you may assume that the input is given as a text file named gas.txt with the following format. The first line is an integer specifying the overall distance D. The second line is an integer specifying the tank capacity T. The third line is an integer specifying the number of gas stations S. Following that will be S additional lines, one per station ordered in increasing distance from the start. Each station is described with two integers. The first integer is the distance between the original city and the station; the second is the (integral) price charged per unit of gas at this station.

    Output Format: Your program should output a single line to stdout, specifying the minimum amount of money that must be spent to get to the goal. If it is not possible to get there (e.g., because two stations are two far apart from each other) you should output the word Impossible.

    Resource Usage: The typical rule for these programming contests is that your program be able to complete its task within one minute on a typical PC (i.e., presumably only a few seconds on turing).

    Examples: available here. Files are named with the form rand_D_T_S_answer.txt


Michael Goldwasser © 2008
CSCI 314, Spring 2008
Last modified: Wednesday, 23 April 2008
Assignments | Course Home | Schedule & Lecture Notes