Uploaded by ZHUOYA LI

hw4 (1)

advertisement
Homework 4
Math 182, Winter 2023
Due date: Friday, Feburary 10th at 3 pm PST.
Submission instructions: You should submit a single pdf containing all your answers on Gradescope and
select the relevant pages for each question. The programming problem is optional and we do not plan to
grade them. If you wish to discuss or see a solution to the programming problem, visit me during office hours.
Any late submissions or emailed submissions will not be accepted. You can also find a LaTeX template that
you may use for typesetting your homework.
Question 1: Dynamic coins
Design a dynamic programming algorithm to solve the problem from question 1 on homework 2.
Recall that in that problem, you are given a list of coin values v1 , . . . , vn , and a price p and you want
to find the smallest number of coins whose values add up to p. Assume that you have an unlimited
supply of each type of coin and that there is always some choice of coins whose values add up to p. For
example, if the coins have values 1, 2, 7, and 20 and the cost of the item you want to buy is 31, then the
minimum number of coins required is 4. You can buy an item with one coin of value 20, one coin of
value 7, and two coins of value 2.
Question 2: Longest common subsequence
Design a dynamic programming algorithm to solve the following problem: given two sequences of
integers, a1 , . . . , an and b1 , . . . , bm , find the length of the longest sequence which is a subsequence of
both of them. For example, if the sequences are 1, 2, 3, 4, 5, 6, 7 and 5, 7, 2, 4, 8, 1, 6, 7, 7 then the longest
common subsequence is 2, 4, 6, 7, which has length 4.
Question 3: Cut the cloth
Suppose you have a large rectangular piece of cloth of dimensions n × m, where n and m are positive
integers. Also, for each pair of positive integers, (w, l), there is a fixed price at which you can sell
rectangular pieces of cloth with dimensions w × l. You also have a machine that can cut any rectangular
piece of cloth into two pieces along any horizontal or vertical line. You want to find the maximum
amount of money you can get for your cloth if you cut them up in an optimal way. Design an algorithm
to solve this problem.
You should assume that you are given the integers n and m and an array P such that for each pair of
positive integers w and l, P[w, l] is the price for which you can sell a rectangular piece of cloth of
dimensions w × l. Also assume that P[w, l] = P[l, w] for all w, l (i.e. you cannot get a higher price
for a piece of cloth by rotating it 90◦ ).
Math 182, Winter 2023
Homework 4
Question 4: Programming problem: Chain matrix multiplication
In this problem, you will write a program to solve the chain matrix multiplication problem discussed in
lecture. However, instead of just returning the minimum number of multiplications, you will also return
the optimal placement of parentheses. For example, if the matrices are A1 , A2 , A3 , A4 with dimensions
2 × 3, 3 × 1, 1 × 5 and 5 × 1, respectively, then the minimum number of multiplications required is 13
and the optimal placement of parentheses is
(A1 × A2 ) × (A3 × A4 ).
Input. The first line of this file consists of a single integer t—the number of test cases. The first line of
each test case contains a number n—the number of matrices to multiply. You may assume n ≥ 1. The
second line contains a list of n positive integers, separated by spaces, indicating the number of rows in
each matrix. The third line also contains a list of n positive integers, separated by spaces, indicating
the number of columns in each matrix. In all there are 3t + 1 lines in the file.
Output. For each test case, you should print two lines. On the first, print the minimum number of
multiplications needed to find the product of all the matrices in the test case (where, as in lecture, we
will assume that to find the product of an n × m matrix and an m × p matrix takes nmp multiplications).
On the second, print the optimal way to put parentheses around the matrices. However, instead of
each matrix you should simply print an uppercase A and for each multiplication you should print a
lowercase x. For example, if the optimal parenthesization is (A1 × A2 ) × (A3 × A4 ) then you should
print (AxA)x(AxA).
input
4
1
2
3
2
2
3
3
4
5
4
4
5
3
4
5 2
2 7
5 2 3
2 3 6
output
0
A
24
AxA
96
(AxA)xA
124
(AxA)x(AxA)
Note that any unambiguous parenthesization is acceptable—for example instead of (AxA)x(AxA) in the
last example above you could print (((A)x(A))x((A)x(A))).
Download