CS 361: Algorithms and Data Structures
Homework 4
Due date: Wednesday 11/11/15 in class



Assignments will be collected at the beginning of the class period. Please bring a hardcopy to turn in. Your assignment can be hand-written or typed.

For the questions on the homework that ask you to "design an algorithm", your answer should include the following:
  1. Description: a written description of how your algorithm works (at least one paragraph)
  2. Pseudocode: pseudocode for your algorithm. The pseudocode should give sufficient detail to make an analysis straightforward (e.g. don't hide a for-loop in an english phrase) while still being high-level enough that it can be easily read.
  3. Running Time:A statement of the worst-case running time of your algorithm in Big-O notation. Please justify your running time using your pseudocode.
Dynamic Programming Questions [25 points]
  1. [15 points] Given an array of positive integers A = [a1 a2 ... an] and an upper-bound W, design a bottom-up dynamic programming algorithm to find a subset of A with the largest possible sum that does not exceed W. The subset does not need to be contiguous.

    For example, suppose we have an array A=[4 9 11 30 5 2] and an upper bound of W=20. Here are some possible subsets of A whose sums do not exceed 20:

    The subset {4} has a sum of 4
    The subset {9, 5} has a sum of 14
    The subset {11, 5, 2} has a sum of 18

    Your algorithm should return back the actual subset. In this case, either {9, 11} or {4, 11, 5} since both of these subsets sum to 20.

  2. [10 points] A directed acyclic graph (DAG) is a graph where the edges have a direction and there are no cycles in the graph. For example, consider the directed acyclic graph shown below:


    Notice that this DAG also has weights associated with each edge. You can think of the weights as representing some notion of "distance" between two nodes.

    Suppose G is a weighted DAG with vertices V={1,2, ...n}. We can represent G using a matrix with n rows and n columns where G[i][j]=wij is the weight of the edge that starts at node i and goes to node j or -1 if no such edge exists.

    Design a memoized dynamic programming algorithm to find the shortest path between two given nodes. The input to your algorithm should be the matrix G, the starting node, and the ending node. Note: In solving this problem, you may in fact end up solving the more general problem of finding the shortest path from the start node to all other nodes in the graph -- that's okay.

    Extra Credit: Translate your memoized dynamic programming algorithm into a bottom-up dynamic programming algorithm.

Greedy Algorithm questions [25 points]
  1. [15 points] In class, we developed a greedy algorithm to solve the unweighted interval scheduling problem. At each step, we made the greedy choice of choosing the job with the earliest finish time. However, there are lots of other greedy choices we could have considered instead:

    • Choose the job with the latest start time
    • Choose the job with the earliest start time
    • Choose the job with the shortest duration
    • Choose the job that conflicts with the fewest number of remaining jobs

    For each greedy choice listed above, either (1) prove that this greedy choice would indeed lead to an optimal solution or (2) give a counterexample that shows the greedy choice would result in a suboptimal solution.

  2. [10 points] Design a greedy algorithm that returns n cents in change using the fewest number of quarters, dimes, nickels, and pennies. Prove that your algorithm yields an optimal solution.


Last modified: Fri Jan 24 10:58:47 PST 2014