Algo (2024) PSet 02

You might also like

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

50.

004 - Algorithms
January-April Term, 2024
Problem Set 2
Due by: Week 12 Wednesday (10 Apr 2023) 1 pm.
Please submit your homework online via eDimension.

Note: This problem set is on dynamic programming (DP). For algorithm design questions that
specifically ask for algorithms using dynamic programming, no credit will be given if you
do not use dynamic programming.
Following the notation used in the textbook, we write A[l..n] to mean an array A indexed from
1 to n. This means the first entry of A is A[l]. If instead we write A[O .. (n - 1)], then we mean
an array A indexed from Oto n - l, which means the first entry of A is A[0]. Analogously, we
write A[l..m][l..n] to mean a two-dimensional array A with rows indexed from 1 to m and with
columns indexed from 1 to n, so A[il[j] denotes the entry of A in the i-th row and j-th column.

General Grading Rubric: Each question is worth 20 points, of which 14 points are allocated
to a DP algorithm design problem, distributed as follows:
(i) 3 points for clearly specifying (in English) what function(s) and array(s) you are defin­
ing/initializing, and what subproblems you are trying to to solve.
(ii) 4 points for recurrence: 2 points for correct recurrence equation, 1 point for correct range
of indices occurring in the recurrence, and 1 point for correct base case(s).
(iii) 5 points for pseudocode: 1 point for correct structure of a DP algorithm, 1 point for
correct base case(s), and 3 points for correct implementation of the recurrence.
(iv) 2 points for analysis: 1 point each for correct analysis of time and space complexity.

It is up to the student to use either the top-down DP approach, or the bottom-up DP approach.
Question 1. Let n be a positive integer, and consider a game played on a 2 x n grid of squares,
i.e. this grid has 2 rows and n columns. Every square is labeled by a pair (i,j), where i,j are
integer indices satisfying 1 � i � 2 and 1 � j � n,so that if a square is in the i-th row and j-th
column of the grid, then that square has label (i,j). Thus,£,= {(i,j) : 1 � i � 2,1 � j � n} is
the set of all possible labels. Two distinct labels (i,j),(i',j') in £, are called adjacent if exactly
one of the following three mutually exclusive conditions hold: (i) j = j'; (ii) i = i' and j' = j + l;
(iii) i = i' and j' = j - l. In addition to labels, each square with label (i,j) has an integer
value, denoted by v(i ,j)· The integers in the squares can be positive, negative, or zero, and there
could be repeated integers. To play this game, we first need to select a non-empty subset of
labels S � £. Given our selected subset S, it is said that S is valid if it is impossible to find
two distinct labels in S that are adjacent. (S is automatically valid if it has only 1 label.) If
our selected subset S is valid, then our game score is L (i ,j)ES v(i ,j), i.e. the sum of the integers
in the squares corresponding to the labels in S. If instead S is invalid, then our game score will
be 0. Our goal is to maximize our game score.

Figure 1: A visualization of part of a possible grid of squares for Question 1

(i) Is it possible to have a 2 x n grid of squares, such that the only way to achieve the maximum
possible game score for that grid of squares is to select S to be an invalid subset? Please
explain your answer. [2 points]

1
(ii) Consider the 2 x 3 grid of squares depicted in Figure 2 below. If we play our game on this
grid, what is the maximum possible game score? Please justify your answer with details.
[2 points]

ITITI:TI
[!8JIJ
Figure 2: A visualization of a 2 x 3 grid for Question l(ii).

(iii) Consider the 2 x 6 grid of squares depicted in Figure 3 below. If we play our game on this
grid, what is the maximum possible game score? Please justify your answer with details.
[2 points]

Figure 3: A visualization of a 2 x 6 grid for Question l(iii).

(iv) Consider a 2 x n grid of squares for some positive integer n, and consider some 2n integers

where each v(i,j) represents the integer in the square with label (i, j). If we play our game
on this grid, what is the maximum possible game score? Solve the problem using dynamic
programming: [14 points]
• Clearly specify in English all function(s) and array(s) that you define/initialize, and
clearly explain with details what subproblems you are trying to to solve.
• Find a recurrence for this problem. Specify the correct range of indices for the
recurrence, and specify all base case(s) for the recurrence.
• Design an algorithm to solve the problem with dynamic programming. Please give
your algorithm in pseudocode.
• Analyze the time and space complexities of your DP algorithm.

Question 2. Given an m x n grid of squares, you want to cover this grid with tiles. There are
two possible types of tiles: 1 x 1 square tiles, and 2 x 2 square tiles. You may assume that you
have an infinite supply of tiles of the two types. Your goal is to cover the entire m x n grid
without any overlapping tiles, using the minimum possible number of tiles.
(i) Show that 3 is the minimum possible number of tiles needed to cover an entire 2 x 3 grid
without any overlapping tiles. [1 point]
(ii) What is the minimum possible number of tiles needed to cover an entire 4 x 3 grid without
any overlapping tiles? Please justify your answer with details. [2 points]
(iii) What is the minimum possible number of tiles needed to cover an entire 3 x 7 grid without
any overlapping tiles? Please justify your answer with details. [3 points]
(iv) Given positive integers m and n, what is the minimum possible number of tiles needed
to cover an entire m x n grid without any overlapping tiles? Solve the problem using
dynamic programming: [14 points]
• Clearly specify in English all function(s) and array(s) that you define/initialize, and
clearly explain with details what subproblems you are trying to to solve.
• Find a recurrence for this problem. Specify the correct range of indices for the
recurrence, and specify all base case(s) for the recurrence.
• Design an algorithm to solve the problem with dynamic programming. Please give
your algorithm in pseudocode.

2
• Analyze the time and space complexities of your DP algorithm.

Question 3. Imagine that you are an active member of some online gaming site, where you
can earn reward points by pl aying mini-games. There are a total of n different mini-games.
You can play each mini-game as many times as you want. However, upon the completion of
any mini-game, you would be forced to watch a 2-minute long video advertisement. (If you
re-load the online gaming site after completing a mini-game, then the video advertisement will
be re-loaded, and you would still be forced to watch the full 2-minute long video advertisement
before you can proceed to play the next mini-game.) Based on your gaming experience, you
know that you can complete the i-th mini-game in ti minutes and earn ri reward points. You
also know that if you exit a mini-game before completing it, then you will not earn any rewards
points for that mini-game, and you would have to re-start the mini-game (i.e. spend the full ti
minutes to complete the mini-game) if you choose to re-enter the mini-game again. Note that
you do not earn any reward points for watching video advertisements.
Each day, you decide to spend a total of k minutes of gaming time. Here, "gaming time"
includes the time spent on playing mini-games (whether you complete them or not), as well as
the time spent for the forced watching of video advertisements between mini-games. However,
"gaming time" does not include the time you use to switch on your computer, and does not
include the time you may use to log into the online gaming site.
(i) Suppose there are a total of 5 mini-games, where the number of minutes (ti) needed for
completing the i-th mini-game, and the number of reward points (ri) that can be earned
from the completion of this i-th mini-game, are given as follows:
i 1 2 3 4 5
ti 22 26 32 41 43
ri 30 35 40 48 52
If you have a contiguous period of 60 minutes for gaming time, what is the maximum
possible number of reward points that you could earn for this time period? Please justify
your answer with details. [2 points]
(ii) Suppose instead there are a total of 7 mini-games, where the number of minutes (ti)
needed for completing the i-th mini-game, and the number of reward points (ri) that can
be earned from the completion of this i-th mini-game, are given as follows:
i 1 2 3 4 5 6 7
ti 13 16 22 26 32 41 43
ri 15 17 30 35 40 53 58
If you have a contiguous period of 120 minutes for gaming time, what is the maximum
possible number of reward points that you could earn for this time period? Please justify
your answer with details. [4 points]
(iii) Let n and k be positive integers. Assume that there are a total of n mini-games. Suppose
we are also given positive integers t1,... ,tn and r1,. .. ,rn , where each ti denotes the
number of minutes needed for completing the i-th mini-game, and where each ri denotes
the number of reward points that can be earned from the completion of the i-th mini­
game. If you have a contiguous period of k minutes for gaming time, what is the maximum
possible number of reward points that you could earn for this time period? Solve the
problem using dynamic programming: [14 points]
• Clearly specify in English all function(s) and array(s) that you define/initialize, and
clearly explain with details what subproblems you are trying to to solve.
• Find a recurrence for this problem. Specify the correct range of indices for the
recurrence, and specify all base case(s) for the recurrence.

3
• Design an algorithm to solve the problem with dynamic programming. Please give
your algorithm in pseudocode.
• Analyze the time and space complexities of your DP algorithm.

Question 4. Let n be a positive integer, and let A[l..n] be an array. We shall say that A is a
digit array if every entry A[i] is an integer in {0, 1, 2, ... , 9}. (We allow the possibility that the
entries of A could have repeated values.) Let B = [b1, b2,... ,bk] be a (not necessarily contiguous)
subarray of A. (This means that we can find some k indices 1 :S i1 < i2 < · · · < ik :S n such that
b1 = A[i1], b2 = A[i2], . .. , bk = A[ik]- Similarly, the entries of B could have repeated values.)
We say that the subarray B = [b1, b2,... ,bk] is nicely-ordered if the following conditions hold:
• B has at least one entry, i.e. k 2'. 1.
• Any two consecutive entries bj ,bH 1 (for some index j satisfying 1 :S j :S k - 1) must satisfy
((bj +l -bj ) mod 10) = 1. (Informally, it means that bj and bj +l are two consecutive digits
in the recurring sequence of digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, ... )
By default, a subarray B of length 1 is automatically nicely-ordered.
For any digit array A, let £*(A) denote the maximum possible length of a nicely-ordered subarray
of A. Given some digit array A, our goal is to determine the value of £*(A).
(i) Let A be a digit array of length 100, and suppose that among the 100 entries of A, there
are exactly 5 unique values (i.e. exactly 5 digits from among 0, 1, . .. , 9 appear as entries
of A). Is it possible to have £* (A) = 1? In other words, is it possible for the maximum
possible length of a nicely-ordered subarray of A to be exactly 1? If so, please provide
an explicit example. (Your example array A must have exactly 5 unique digits.) If not,
please explain why this is not possible. [2 points]
(ii) Let A be a digit array of length 100, and suppose instead that among the 100 entries of A,
there are exactly 10 unique values (i.e. each possible digit from among 0, 1, . . . , 9 appears
at least once as some entry of A). Among all such possible digit arrays A, what is the
smallest possible value for £*(A)? Please justify your answer with details. [2 points]
(iii) Let A be a specific digit array of length 20, given as follows:

A= [1, 2, 2, 3, 5, 8, 9, 9, 0, 1, 2, 2, 6, 7, 3, 2, 3, 4, 5, �.

What is the value of C*(A) (for this particular array A)? Please justify your answer with
details. [2 points]
(iv) Let n be a positive integer, and let A[l..n] be a digit array. What is the value of £*(A)?
Solve the problem using dynamic programming: [14 points]
• Clearly specify in English all function(s) and array(s) that you define/initialize, and
clearly explain with details what subproblems you are trying to to solve.
• Find a recurrence for this problem. Specify the correct range of indices for the
recurrence, and specify all base case(s) for the recurrence.
• Design an algorithm to solve the problem with dynamic programming. Please give
your algorithm in pseudocode.
• Analyze the time and space complexities of your DP algorithm.

Question 5. Let m 2: 2 and n 2'. 2 be positive integers, let A[l..m][l..n] be a two-dimensional


array whose entries are positive integers, and consider a game played on an m x n grid of squares
(i.e. the grid has m rows and n columns), where the (i, j)-th square refers to the square in the
i-th row and j-th column (for 1 :S i :S m, 1 :S j :S n). We shall use the convention that
the (1, 1)-th square refers to the top-leftmost square, while the (m, n)-th square refers to the
bottom-rightmost square.

4
You begin the game by starting at the (1,1)-th square (top-left of the grid), and your goal is
to perform a sequence of jumps to reach the (m,n)-th square (bottom-right of the grid). Every
jump requires some energy points. Each energy point can be used to move either one square
downwards, or one square rightwards. (You are not allowed to jump leftwards or upwards.) For
example, a single jump from the (1,1)-th square to the (3,4)-th square would require 5 energy
points, since to make this jump, you would have to move 2 squares downwards, and 3 squares
rightwards, for a total of 3 + 2 = 5 energy points.
At each (i,j)-th square, there are a total of A[il[j] energy points available for you to use for
jumping. You do not have to use all available energy points; you can choose to only use some
of the energy points. However, any unused energy points from the (i,j)-th square cannot be
brought over to the subsequent square that you jump to.
(i) Consider the 5 x 5 grid of squares depicted in Figure 4 below. Starting at the (1,1)-th
square (top-leftmost square), what is the minimum possible number of jumps needed for
you to reach the (5, 5)-th square (bottom-rightmost square)? Please justify your answer
with details. [2 points]

2 3 1 1 2
2 2 3 5 2
1 2 2 1 1
3 3 5 2 1
2 2 1 5 8

Figure 4: A depiction of a 5 x 5 grid of squares for Question 5(i), where each (i,j)-th square of
the grid is filled with the integer A[i] [j] , representing the available energy points at that square.

(ii) Let A[l..ml[l..n] be a two-dimensional array whose entries are positive integers. Assume
that A represents the available energy points of the squares in an m x n grid of squares,
for the game described above. Please give a list of conditions for the entries of A, such
that the following hold:
• If array A satisfies all the conditions in the given list of conditions, then it is possible
to start from the (1,1)-th square and reach the (m,n)-th square in at most 2 jumps.
• If array A satisfies does not satisfy all the conditions in the given list of conditions,
then it is NOT possible to start from the (1,1)-th square and reach the (m,n)-th
square in at most 2 jumps.
You may express the conditions for the entries ai,j in terms of m and n. Please justify
and explain your list of conditions in as much details as possible. [4 points]
(iii) Let m 2: 2 and n 2: 2 be positive integers, and let A[l..ml[l..n] be a two-dimensional
array whose entries are positive integers. Assume that A represents the available energy
points of the squares in an m x n grid of squares, for the game described above. Starting
at the (1,1)-th square, what is the minimum possible number of jumps needed to reach
the (m,n)-th square? Solve the problem using dynamic programming: [14 points]
• Clearly specify in English all function(s) and array(s) that you define/initialize, and
clearly explain with details what subproblems you are trying to to solve.
• Find a recurrence for this problem. Specify the correct range of indices for the
recurrence, and specify all base case(s) for the recurrence.
• Design an algorithm to solve the problem with dynamic programming. Please give
your algorithm in pseudocode.
• Analyze the time and space complexities of your DP algorithm.

You might also like