CS 601 Psets
CS 601 Psets
CS 601: Algorithms and Complexity, Fall 2024 (Akash Kumar) Version: 1.0
Submitted by <name>
Grading Policies:
(a) Total points: 100
(b) There are five problems in this pset (each of which have one or more subparts). Please solve/attempt them all.
(c) You are allowed –indeed you are encouraged– to, collaborate with other students in the class. Please list all the people you collaborated
with.
(d) Quite a few problems should be available online. You should feel free to consult online resources, but please use those only after
you spend some serious time thinking about the problems.
(e) I cannot emphasize this point enough. With all the freedom given in the points above, it is imperative that you write your
solutions yourself. Please do not copy/paste what your friends wrote or what electronic material you find online. This defeats the
purpose of solving a problem set. Indeed, if you are caught engaging in unseemly practices, severe action will be taken.
(f) Please write your pseudocode as you see those written in textbooks. Confusing/inscrutable psuedocodes will be penalized with no
room for a regrade.
(g) Please write your mathematical claims rigorously and unambiguously. Again, follow the style laid out in textbook or in the class if
you are unsure. Ambiguous or unclear claims/proofs will be penalized. Your TAs are not here to debug your proofs/algorithms. If
your writeup does not convince them of the correctness, you will rightfully lose points.
1 (15 pts.) Producing Target Sums using DP
We call a sequence of n integers x1 , . . . , xn valid if each xi is in {1, . . . , m}.
(a) (5 pts.) Give a dynamic programming-based algorithm that takes in n, m and a “target” T as input and outputs the number
of distinct valid sequences such that x1 + x2 + . . . + xn = T . Your algorithm should run in time O(m2 n2 ).
(b) (10 pts.) Give an algorithm for the problem in part (a) that runs in time O(mn2 ).
Solution:
2 (30 pts.) Scheduling Jobs on Supercomputers
You’re running a massive physical simulation, which can only be run on a supercomputer. You’ve got access to two (identical)
supercomputers, but unfortunately you have a fairly low priority on these machines, so you can only get time slots on them when
they’d otherwise be idle. You’ve been given information about how much idle computing power is available on each supercomputer
for each of the next n one-hour time slots: you can get ai seconds of computation done on supercomputer A in the ith hour if your
job is running on A at that point, or bi seconds of computation if it running on supercomputer B at that point. During each hour
your job can be scheduled on only one of the two supercomputers. You can move your job from one supercomputer to another at
any point, but it takes an hour to transfer the accumulated data between supercomputers before your job can begin running on the
new supercomputer, so a one-hour time slot will be wasted where you make no progress.
So you need to come up with a schedule, where for each one-hour time slot your job either runs on super- computer A, runs
on supercomputer B, or “moves” (it switches which supercomputer it will use in the next time slot). If your job is running on
supercomputer A for the (i − 1)th hour, then for the ith hour your only two options are to continue running on A or to “move.” The
value of a schedule is the total number of seconds of computation that you get done during the n hours. You want to find a schedule
of maximal value. Design an efficient algorithm to find the value of the optimal schedule, given a1 , a2 , . . . an and b1 , b2 , . . . , bn .
Solution:
3 (20 pts.) Vertex Cover
A vertex cover of a graph G = (V, E) is a subset of vertices S ⊆ V that includes at least one endpoint of every edge in E . Give a
linear-time algorithm for the following task:
Output: The size of the smallest vertex cover of T . For instance, in the following tree, possible vertex covers include {A, B, C, D, E, F, G}
and {A, C, D, F } but not {C, E, F }. The smallest vertex cover has size 3: {B, E, G}.
B A E
F G
C
Solution:
4 (20 pts.) A Game Called Pig
Pig is a 2-player game played with a 6-sided die. On your turn, you can decide either to roll the die or to pass. If you roll the die and
get a 1, your turn immediately ends and you get 1 point. If you instead get some other number, it gets added to a running total and
your turn continues (i.e. you can again decide whether to roll or pass). If you pass, then you get either 1 point or the running total
number of points, whichever is larger, and it becomes your opponent’s turn. For example, if you roll 3, 4, 1 you get only 1 point, but
if you roll 3, 4, 2 and then decide to pass you get 9 points. The first player to get to 100 points wins.
Suppose at some point the player whose turn it is has x points, their opponent has y points, and the running total for this turn so
far is z. Let’s work out what the optimal strategy is.
4.A. (5 pts.) Let W (x, y, z) be the probability that the current player will eventually win if both players play optimally. What is
W (x, y, z) if x + z ⩾ 100?
4.B. (5 pts.) Suppose the current player decides to roll, and gets a 1. What’s the probability that they’ll still win, in terms of the
function W ?
4.C. (5 pts.) Give a recursive formula for W (x, y, z).
4.D. (5 pts.) Describe a dynamic programming algorithm to compute W (x, y, z). If you needed N points to win instead of 100,
what would be the asymptotic runtime of your algorithm?
Solution:
5 (15 pts.) Something on Strings
Let x, y , and z be strings. We want to know if z can be obtained only from x and y by interleaving the characters from x and y such
that the characters in x appear in order and the characters in y appear in order. For example, if x = efficient and y = ALGORITHM,
then it is true for z = effALGiORciIenTHMt, but false for z = efficientALGORITHMextraCHARS (miscellaneous characters),
z = effALGORITHMicien (missing the final t ), and z = randomString (obviously wrong). How can we answer this query
efficiently? Your answer much be able to efficiently deal with strings such as x = aaaaaaaaaab and y = aaaaaaaac.
Solution: