[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.
[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.