Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

HW4 Sol

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CSE 431/531: Algorithm Analysis and Design Spring 2021

Homework 4
Instructor: Shi Li Deadline: 4/16/2021

Your Name: Your Student ID:

Problems 1 2 3 Total
Max. Score 12 14 14 40
Your Score

Problem 1 Consider the instance of the weighted job scheduling problem in Figure 1.
The goal is to select a maximum-weight set of mutually-compatible jobs. Each rectangle
denotes a job. The two numbers inside a rectangle respectively denote the index and
the weight of the correspondent job, and the left and right sides of a rectangle denote
its starting time and finishing time. For example, the job 1 has weight 50, starting time
0 and finishing time 2. The jobs are already sorted in non-decreasing order of finishing
times. If a job completes, another job can start immediately. For example, Job 1 and
Job 5 are compatible.

(6, 90) (12, 90)

(3, 140) (9, 160)

(1, 50) (7, 150) (13, 40)

(5, 100) (11, 100)

(2, 90) (8, 130) (14, 30)

(4, 120) (10, 180)


time
0 1 2 3 4 5 6 7 8 9 10 11

Figure 1: Weighted Job Scheduling Instance.

Given the optimum solution for the instance using the dynamic programming algo-
rithm you learned in the class. You need to show the value of each cell, how it is computed,
and how the optimum solution is retrieved.

1
opt[0] = 0
opt[1] = max{opt[0], opt[0] + 50} = 50 b[1] = Y
opt[2] = max{opt[1], opt[0] + 90} = 90 b[2] = Y
opt[3] = max{opt[2], opt[0] + 140} = 140 b[3] = Y
opt[4] = max{opt[3], opt[0] + 120} = 140 b[4] = N
opt[5] = max{opt[4], opt[1] + 100} = 150 b[5] = Y
opt[6] = max{opt[5], opt[2] + 90} = 180 b[6] = Y
opt[7] = max{opt[6], opt[2] + 150} = 240 b[7] = Y
opt[8] = max{opt[7], opt[4] + 130} = 270 b[8] = Y
opt[9] = max{opt[8], opt[5] + 160} = 310 b[9] = Y
opt[10] = max{opt[9], opt[5] + 180} = 330 b[10] = Y
opt[11] = max{opt[10], opt[6] + 100} = 330 b[11] = N
opt[12] = max{opt[11], opt[7] + 90} = 330 b[12] = N
opt[13] = max{opt[12], opt[8] + 40} = 330 b[13] = N
opt[14] = max{opt[13], opt[9] + 30} = 340 b[14] = Y

The optimum weight is 340. To recover the optimum solution, we have the following
trace of i: 14(Y ) → 9(Y ) → 5(Y ) → 1(Y ) → 0. So, the optimum solution is {1, 5, 9, 14}

Problem 2 In this problem, you need to use dynamic programming to solve the Knap-
sack problem with unlimited copies of each item.
Formally, we are given n items indexed by [n] = {1, 2, 3, · · · , n}. Each item i ∈ [n] has
an integer value vi ≥ 1 and an integer price pi ≥ 1. Each item i has unlimited number of
copies. You have a budget of P . The goal of the problem is to buy a set of items with
the budget P so as to maximize the value. Now you can buy many copies of each item.
Formally 2 , · · · , cn ), where each ci is a non-negative integer
P you need to find a vector (c1 , cP
and ni=1 ci pi ≤ P , so as to maximize ni=1 ci vi .
For example, suppose we have n = 3 items, with values v1 = 16, v2 = 30 and v3 = 60
and prices p1 = 30, p2 = 50 and p3 = 80, and the budget is P = 200. Then consider three
different solutions:
• Buy 4 copies of item 2. The price is 50 × 4 ≤ 200. The value is 30 × 4 = 120.
• Buy 2 copies of item 1, 1 copy of item 2, and 1 copy of item 3. The price is
30 × 2 + 50 + 80 ≤ 20. The value is 16 × 2 + 30 + 60 = 122.
• Buy 1 copy of item 1 and 2 copies of item 3. The price is 30 + 80 × 2 ≤ 200. The
value is 16 + 60 × 2 = 136.
The third solution gives the maximum value. Indeed, it is the best solution for the
instance. So, for the instance, the maximum value is 136, and the ci values are c1 =
1, c2 = 0 and c3 = 2.
Design an O(nP )-time algorithm to solve the problem. You need to output both the
maximum value and the ci ’s that achieve the value.

2
Definition of the cells: for every integer 0 ≤ i ≤ n and 0 ≤ P 0 ≤ P , we define opt[i, P 0 ]
to be the value of the instance with items 1, 2, 3, · · · , i and budget P 0 .
Then the recursion for computing the cells:



 0 if i = 0
opt[i − 1, P 0 ]

if i > 0 and pi > P 0
opt[i, P 0 ] = (
opt[i − 1, P 0 ]
if i > 0 and pi ≤ P 0

max opt[i, P 0 − p ] + v



i i

In the algorithm, we compute opt[i, P 0 ] using the following formula for every i from
0 to n, and for every P from 0 to P 0 , in that order. Define b[i, P 0 ] = Y if i > 0, P 0 ≥ pi
and opt[i, P 0 − pi ] + vi > opt[i − 1, P 0 ]. Define b[i, P 0 ] = N for all other cases. Then the
following procedure will recover the optimum solution:
1: let c[i] ← 0 for every i = 1, 2, 3, · · · , n
2: i ← n, P 0 ← P
3: while i > 0 do
4: if b[i, P 0 ] = Y then
5: c[i] ← c[i] + 1, P 0 ← P 0 − pi
6: else
7: i←i−1
8: return c
The running time of the algorithm is O(nP ) as computing each cell opt[i, P 0 ] times
O(1) time and there are O(nP ) different cells.
You will get the full score if you have the texts written in red, since that already
shows you know how to solve the problem. Here I am trying to explain the recursion for
opt[i, P 0 ] for i > 0. There are two types of solutions:
• In the solution, we do not take any copy of item i. The optimum solution of this
type has value opt[i − 1, P 0 ].
• In the solution, we take at least one copy of item i. After taking the one copy of
item i, our budget becomes P − pi . Since we have unlimited copies of each item,
in the residual problem we still have item i available. So, the optimum solution of
this type has value opt[i, P 0 − pi ] + vi . Of course, we have this type only if pi ≤ P 0 .

Problem 3 (From leetcode.com) There are n balloons in a row. Each balloon is painted
with a positive integer number on it. You are asked to burst all the balloons.
If you burst the i-th remaining balloon in the row, you will get nums[i−1]×nums[i]×
nums[i + 1] coins, where nums[t] for any t is the number on the t-th remaining balloon
in the row. If i − 1 = 0 or i + 1 is more than the number of balloons, then treat it as if
there is a balloon with a 1 painted on it.
Return the maximum coins you can collect by bursting the balloons wisely. For
example, if there are initially 4 balloons in the row with numbers 3,1,5,8 on them. Then
the maximum coins you can get is 167. This is how the array of numbers change when
you burst the balloons: (3, 1, 5, 8) → (3, 5, 8) → (3, 8) → (8) → (). The coins you get is
3 × 1 × 5 + 3 × 5 × 8 + 1 × 3 × 8 + 1 × 8 × 1 = 167.

3
Design an O(n3 )-time algorithm to solve the problem. For convenience, you only need
to output the maximum number of coins you can get.
Let A be the input array of n numbers. For simplicity, assume the balloons 0 and
n + 1 have number 1 on them but they can not be burst (so, A[0] = A[n + 1] = 1). Then
in the problem, we need to burst all balloons except for balloons 0 and n + 1, and get
the maximum coins.
Definition of the cells: for every pair of integers i, j with 0 ≤ i < j ≤ n + 1, we define
f [i, j] to be the maximum coins we can get by bursting balloons i + 1 to j − 1.
Recursion for computing the cells: for every i, j with 0 ≤ i < j ≤ n + 1
(
0 if j − i = 1
f [i, j] =
maxk:i<k<j (opt[i, k] + opt[k, j] + A[i] × A[k] × A[j]) if j − i ≥ 2

In the algorithm, we computing all the cells f [i, j] in non-decreasing order of j − i,


and output f [0, n + 1]. The algorithm runs in O(n3 ) time since there are O(n2 ) cells and
each cell takes O(n) time to compute.

You might also like