Consider the following process
Consider the following process
At all times you have a single positive integer x, which is initially equal to some value n. In each step, you can either decrement x by 1 or halve x (only if x is
even). Your goal is to produce 1 given a starting value n. For example, you can reach 1 starting from the integer 10 in four steps as follows: 10 → 5 → 4 → 2 → 1 using an decrement, a
halving, another decrement and finally another halving. Obviously you can get to 1 from any integer n using exactly n − 1 decrements, but for almost all values of n, this is horribly inefficient.
Describe an algorithm to compute the minimum number of steps required to reach 1 from any given integer n. Solution: Algorithm: We first find the binary representation of n and list them
from least significant bit to most significant bit (right to left). At each step, if the current bit in consideration is 1, then we perform a decrement, otherwise we halve. Once the halving
happens, then we can proceed to the next bit since we have reduced to the number of bits in the number. The remaining steps were not asked for, but are presented for your benefit.
Algorithm Correctness: Our process is to (un)reproduce the binary representation of n, starting from the least significant bit, hence it terminate if and only if x = 0. That proves the
correctness of the algorithm. Running time: Computing the binary representation of n requires O(log n) time and since we perform at most two operations per bit, that also requires O(log n)
time and the whole algorithm runs in O(log n) time. Proof of Optimality: First we note that the number of steps required decreases monotonically the closer you get to 0. We only need to
show that any optimal algorithm will also require as many steps as our algorithm does. Suppose for contradiction that there is another algorithm that uses fewer steps. We find the first place
where the two algorithm’s chosen operation differ. If our algorithm does a halving while the other algorithm does a decrement, then because of the monotonic property of number of steps,
our algorithm will be faster. On the other hand, if our algorithm does a decrement while the other algorithm does a halving, then the other algorithm must be performing an illegal step since
our algorithm will always choose halving if it can and will only perform a decrement if the halving step is illegal. Thus, while our algorithm produces the correct output in that stage, the other
algorithm will produce an incorrect output/perform an illegal step.
Complete the following description of a dynamic programming algorithm which will compute the smallest number of drone drops required to guaranteed to find the correct floor at which
the drone breaks. Solution: 1. English Description: Let D(x, i) be the minimum number of drone drops required to find the threshold floor (lowest floor which breaks the drone) in a x-floor
building given access to i drones. If the drone breaks, then we know we only need to look at the floors below, that is, x − 1 floors, using the remaining i − 1 drones. On the other hand, if the
drone doesn’t break, then we can use drone i again but will only need to consider the top n − x floors. Since we do not know whether we’ll need to look at the bottom x − 1 floors or the top n
− x floors, we’ll pretend that we’re always forced to pick the worst option and consider the case that forces us to make as many moves as possible. However, we still do control the value of x
and can pick the best possible floor x for the first drop attempt out of all possible x ∈ {1, . . . , n}. 2. Recurrence: D(x, i) = { 1 x = 1 | x i = 1 | 1 + min_x∈{1,...,n} (max (D(x − 1, i − 1), D(n − x, i)))
else
Flow: Complete the flow network below such that it can be used to determine if there is a way to assign doctors to night shifts. If there is a way, you should describe what the assignments
are and otherwise, you should output that it is not possible. solve the problem. Solution: Capacity of m on s to doctors ensures that no doctor can work more than m night shifts. Capacity
limit of sk allows us to check if the max flow saturates these edges which implies that the requirements for each night shift are satisfied. The edges from doctor to shifts are created based on
the personal constraints Pi that doctors have. An assignment is possible if the max flow saturates all edges from shifts to the sink. Alternatively, there is an assignment if the maximum flow is
equal to ∑n k=1 sk. To find the assignment, we use the non-zero flow edges to say that any doctor to shift edge which has a flow of 1 means that that doctor has been assigned to that
particular night shift. Diagram:
DP: Describe the iterative approach (clearly, pseudocode preferred) that will be used to calculate the value of Cn for a given n ≥ 0 based on the following recurrence. Your approach should
be based on a dynamic programming based memoization of the necessary subproblems required to compute the value of Cn. Your approach must only require looking at each entry in the
memoized DP table once to compute Cn for any value of n. C0 = 1 and Cn = ∑n i=1 Ci−1Cn−i for n > 0 Solution: We will create an array to store intermediate values of Cj and utilize this array
to quickly find the next element Cn rather than recalculating all previous values of Cj . Initialize our array C with 1 since C0 = 1. For computing any intermediate value Cj we utilize two
pointers to the array C the left one set to ℓ = 1 and the other set to r = j − 1. At each step, we multiply the values Cℓ and Cr and add it to out running total for Cj . Then ℓ is incremented by 1
and r is decremented by 1. This continues until ℓ ≥ r, at which point we double our running total. If ℓ > r, we are done and return this new total. Otherwise, if ℓ = r, then j must have been odd
so we additionally add Cr · Cr to the new total and return this. We utilize the symmetry in the summation in the recursive formula for Cj to only visit va lues in the C array once to compute Cj
for a new j.
Given an array of integers A, the subsequence with the maximum sum can be found using one pass(es). During the pass(es) we delete all numbers n satisfying the condition n < 0. This will
leave only positive numbers left. In case all numbers satisfy the earlier condition then the subsequence with maximum sum will be the subsequence containing all numbers.
Complete the provided (incomplete) framing of this optimization problem in the language of a linear program. Table 1: Arbitrage opportunities Commodity Transaction 1 Transaction 2
Transaction 3 Max allowed trade I -1 3.14 4.3 100 II +2.5 -2.7 1.41 70 III +0.5 1 -1.5 20 Transaction amount t1 t2 t3 max(−1 + 2.5 + 0.5) · t1 + (3.14 − 2.7 + 1) · t2 + (4.3 + 1.41 − 1.5) · t3 (1) s.t. 1
· t1 ≤ 100 (2) 2.7 · t2 ≤ 70 (3) 1.5 · t2 ≤ 20 (4) t1, t2, t3 ≥ 0 (5) Explain what each line in the LP accomplishes. Line 1: Maximizes the profit which is the net change in currency holdings due to
each transaction (depending on how that transaction affects currency holdings). Line 2: Ensures that we do not perform more than 100 trades involving commodity I. Line 3: Ensures that we
do not perform more than 70 trades involving commodity II. Line 4: Ensures that we do not perform more than 20 trades involving commodity III. Line 5: We cannot perform negative
amounts of transactions.
Given two sorted arrays A and B containing n integers each, find the median of all 2n integers. That is, find the median of the combined/merged array. Your algorithm should run in O(lg n)
time. Which means that you would not be able to perform the actual merge operation on the arrays to find the median. Thus your algorithm should somehow utilize the sorted nature of A
and B when finding the median. Solution: We solve a more general problem, finding the kth smallest element from two sorted arrays of poten- tially different lengths. First, compare the
“middle” elements of the two arrays. If they are equal then that’s the median since half the elements in both arrays are less than this, so in total half of all the integers are smaller than this
which makes this the median. If middle element of A is smaller, then we know that the first n elements in the merged array can contain elements from the right half of B, so perform binary
search for the middle element (or get the next largest number) of A in the right half of B and hit index i. Once that has been found, our goal changes to finding the nth smallest element in the
first half of A and upto this newly found index in B. A similar step can be done if the middle element of A is larger. To handle general kth smallest element, if k is more than half the sizes of A
and B then we know that one of the left halves can be discarded (followed by an adjustment to the value of k). If k is less than half the sizes of A and B then we know that one of the right
halves can be discarded. Thus, in each step we are able to discard half of one of the two arrays, which gives us a binary search like recurrence which would give us a logarithmic runtime
matching the required O(lg n) run time.
Describe an O(m + n) algorithm that searches for a target value t in an m × n integer matrix M which has the following properties: • Integers in each row are sorted in ascending from left to
right. • Integers in each column are sorted in ascending from top to bottom. 1 4 7 11 15 2 5 8 12 19 3 6 9 16 22 10 13 14 17 24 18 21 23 26 30 Table 2: Example of the sorted search grid
Solution: We perform a binary search like algorithm. First we compare the target with the middle element of the grid (middle row, middle column) denoted by rm, cm. If they are equal then
we have found the target and return these indices. If that element is smaller than the target t then we recursively search in the subgrid defined by ((rm + 1, 1), (m, cm)), the subgrid defined
by ((rm + 1, cm + 1), (m, n)) and the subgrid defined by ((1, cm + 1), (rm, n)). In particular we are able to discard the whole subgrid defined by ((1, 1), (rm, cm)). Alternatively, if the target t is
smaller than the middle element, then we proceed like the previous case except this time we will discard the subgrid defined by ((rm, cm), (m, n)) and recurse on subgrids ((1, 1), (rm − 1, cm
− 1)), ((rm, 1), (m, cm − 1)) and ((1, cm), (rm − 1, n)). At each step we discard 1 4 of the grid, giving us the recurrence T (N ) = 3T (N/4) + O(1) where N = (max(m, n))2. Solving this recurrence
gives us T (N ) = O(lg N ) = O(2 lg max(m, n)) = O(m + n).
You have been called in to help the leader of Pillowtown to determine the smallest number of hallways that would need personnel placed so that whichever path the elite shock troops of
Sgt. Chang might take, your doomsday device can be deployed to defend Pillowtown’s base. You can assume that the blueprint of Greendale is available to you, depicting the rooms where
the bases are as well as all the hallways and their intersections. Solution: This is essentially an application of Menger’s theorem. We want to place surveillance on as few hallways as possible
to ensure that all paths from the Blanketsburg base to the Pillowtown base are covered. This corresponds to removing as few edges as possible while still managing to disconnect every path
from a start vertex to an end vertex. This is precisely what Menger’s theorem tells us about edge disjoint paths and we’ve already seen how to do that using Max-flow by using unit capacity
edges.
Everything is same as Minimum Cost Flow, except instead of mini- mizing cost, we want to maximize it. Solution: We utilize the fact that in min cost flow, the price/cost of sending flow
through an edge is allowed to be negative. Specifically, we use the fact that minimization of x is equivalent to maximization of −x. So, to solve a maximum cost flow problem, we would
reduce it to min cost flow by simply creating new prices p′(e) = −p(e).
The way AonDor’s magic works is by selecting a subset of a points on a grid as conduits for raw energy. However care needs to be taken that you do not select more than the allowed
number of conduits per row and column. The allowed number of selections differs by row and column for each grid. All of these control what the specific effects of the magic are and the
more points that are chosen, the more powerful the magic. As a young Elantrian learning this magic, you have been tasked to find the largest selection that can be made given a grid. That is,
you are given an n × n grid with each index (i, j) filled with one of {0, 1} where 1 indicates that the point (i, j) could be chosen and 0 indicates that (i, j) cannot be chosen. Along with the grid,
you also have an upper limit ri on how many points can be chosen for each row i, and an upper limit cj on how many points can be chosen for each column j. Design an algorithm, that takes
the grid, ri values, and cj values as input and chooses the largest possible subset of points to unleash the maximum possible magical energy. Solution: We’ll solve this using max-flow. Create
a source node s and a sink node t. Create a node for each (i, j) that can be selected. Create a node for each row i and each column j. Connect source s to each i with capacity ri, and connect
each j to sink t with capacity cj . Connect each (i, j) with an incoming unit capacity directed arc from row node i and with an outgoing directed arc of unit capacity to column node j. Compute
max-flow, this will be the number of (i, j) points that can be selected and the specific selection will be all the nodes (i, j) which have an incoming flow of 1.
LP Max: A catering company is to make lunch for a business meeting. It will serve ham sandwiches, light ham sandwiches, and vegetarian sandwiches. A ham sandwich has 1 serving of
vegetables, 4 slices of ham, 1 slice of cheese, and 2 slices of bread. A light ham sandwich has 2 serving of vegetables, 2 slices of ham, 1 slice of cheese and 2 slices of bread. A vegetarian
sandwich has 3 servings of vegetables, 2 slices of cheese. and 2 slices of bread. A total of 10 bags of ham are available, each of which has 40 slices; 18 loaves of bread are available, each with
14 slices; 200 servings of vegetables are available, and 15 bags of cheese, each with 60 slices, are available. Given the resources, how many of each sandwich can be produced if the goal is to
maximize the number of sandwiches? Solution: Let x, y, z be the number of ham, light ham and vegetarian sandwiches. Considering the con- straints this gives us the following LP: max x + y +
z s. t. 4x + 2y + 0z ≤ 400 2x + 2y + 2z ≤ 252 2x + 2y + 2z ≤ 252 x, y, z ≥ 0. ||||| LP Min: A company is creating a meal replacement bar. They plan to incorporate peanut butter, oats, and dried
cranberries as the primary ingredients. The nutritional content of 10 grams of each is listed below, along with the cost, in cents, of each ingredient. Find the amount of each ingredient they
should use to minimize the cost of producing a bar containing a minimum of 15g of each ingredient, at least 10g of protein and at most 14g of fat. Peanut Butter (10g) Oats (10g) Cranberries
(10g) Proteins (grams) 2.5 1.7 0 Fat (grams) 5 0.7 0.1 Cost (cents) 6 1 2 Table 2: Ingredients, nutrients and costs Solution: Let p, a, and c be the amount of peanut butter, oats and cranberries
that get used. min 6p + 1a + 2c s.t. 2.5p + 1.7a + 0c ≥ 10 5p + 0.7a + 0.1c ≥ 14 p, a, c ≥ 1.5
DP: Use the following recurrence along with a dynamic programming implementation to efficiently compute the nth Catalan number. Cn = { 1 n = 1 ∑n i=1 Ci−1Cn−i otherwise
Given a denomination of positive coins c1, c2, . . . , cm and a value n as input how many ways can you make change for n. For example, with coins being {1, 2, 3} the number of ways to get
the value 4 is 4 as follows: {1, 1, 1, 1}, {1, 1, 2}, {2, 2}, {3, 1}. With coins being {2, 5, 3, 6} the number of ways to get the value 10 is 5 as follows: {2, 2, 2, 2, 2}, {2, 2, 3, 3}, {2, 2, 6}, {2, 3, 5}, {5,
5}. Solution: • Logic: Let us arbitrarily arrange the coins as c1, c2, . . . , cm. Now for any number i we can say that the number of ways to get to i using the coins from c1 to cj will need to
count the ways that use no cj coin and will also need to count the ways to use at least one cj coin. These are separate events but they also account for all possibilities. Thus, they become our
subproblems. If cj is not used, then we still need to make i but now we only use c1 to cj−1. On the other hand, if cj is used at least once, then we only need to make i − cj and we still have all
of c1 to cj to use (because cj may be used more than once). • The Bellman Equation: Let OP T (i, j) denote the number of ways to make the value i using the coins from c1 to cj . OP T (i, j) =
0 if i < 0 or j = 0 1 if i = 0 OP T (i, j − 1) + OP T (i − cj , j) otherwise • Iterative algorithm: The iterative algorithm would build up the OP T table from bottom up. It would first fill in the
values when i = 0 and when j = 0. From there, it would iterate over all available coins (upto j) and see how many ways can higher up i can be filled. • Number of subproblems: The index i
varies from 0 to n and the index j varies from 0 to m. So a total of O(nm) problems need to be solved. • Time per subproblem: Each recursive call is just an addition. So O(1). • Total time for
memoization: O(nm). • Final output: OP T (n, m).
Given an array A[1...n] of letters, compute the length of the longest palindrome subsequence of A. A sequence P [1...m] is a palindrome if P [i] = P [m − i + 1] for every i. For example,
RACECAR is a palindrome, while REAR is not a palindrome. And the longest palindrome subsequence in the string ABETEB is BEEB. English Description of problem: We create a new array
where P al[i, j] will contain the length of the longest palindrome subsequence of the the subarray of A that starts at index i and ends at index j. • English Description of logic: It’s easy to see
that if A[i] = A[j], for some i < j, we can include that in palindrome subsequence and increase i by 1 and decrease j by 1. And if they’re not equal, we either increase i or decrease j, but not
both. • Recurrence Relation (and Base Case) P al[i, j] = Pal[i, j] = 1 i = j max(P al[i, j + 1], P al[i − 1, j]) A[i] ̸ = A[j] 2 + P al[i − 1, j + 1] A[i] = A[j] • Iterative Algorithm: Now it’s not too clear
how we can memoize P al[i, j] correctly (but it can be done carefully). The next solution is an alternative way we can look at the recursion that uses length as a parameter.
Describe algorithms that will allow you to find and store the required metadata quickly given a n node BST T as input. 2. (∗) BST usage involves updating nodes; describe how each of these
metadata values can be kept updated as elements are added or removed from T . Solution: 1. Algorithm: To find each of the required metadata, use dynamic program to memoize at the
node itself and use the recursive definitions for the respective metadata as detailed below. Each recursion should be based on either the parent node of the current node or the left and right
children of the current node. Recursion: Depth of a Node: depth[node] = depth[parent] +1 Height of a Node: height[node] = max(height[left], height[right]) +1 Subtree Size of a Node:
size[node] = size[left] + size[right] +1 Rank of a Node: The rank of a node is computed through in-order traversal. A counter keeps track of current rank and is assigned to the current node. As
the algorithm traverses through the BST, the counter increments by 1 for each node. 2. Depth of a Node: • If the node removed is above the current node, subtract 1 from the depth. If the
node removed is below the current node, no change is needed. • If the node added is above the current node, add 1 to the depth. If the node added is below the current node, no change is
needed. Height of a Node: 1Every node’s key is at least as large as the largest key in the left subtree and at most as large as the smallest key in the right subtree. • If the node removed is
above the current node, no change is needed. If the node removed is below the current node, subtract 1 from the height. • If the node added is above the current node, no change is
required. If the node added is below the current node, add 1 to the height. Subtree Size of a Node: • If the node removed is above the current node, no change is needed. If the node
removed is below the current node, subtract 1 from the size. • If the node added is above the current node, no change is required. If the node added is below the current node, add 1 to the
size. Rank of a Node: • If the node removed is to the left of the current node, subtract 1 from the rank. If the node removed is to the right of the current node, no change is needed. • If the
node added is to the left of the current node, add 1 from the rank. If the node added is to the right of the current node, no change is needed.
You are helping the Department of Computer Science at Uskees University create a new flexible curriculum with a complex set of graduation requirements. The department offers n different
courses, and there are m dierent requirements. Each requirement specifies a subset of the n courses and the number of courses that must be taken from that subset. The subsets for
different requirements may overlap, but each course can be used to satisfy at most one requirement. For example, suppose there are n = 5 courses A, B, C, D, E and m = 2 graduation
requirements: • You must take at least 2 courses from the subset {A, B, C}. • You must take at least 2 courses from the subset {C, D, E}. A student who has only taken courses B, C, D cannot
graduate, but a student who has taken either A, B, C, D or B, C, D, E can graduate. Given m requirements and the list of courses a student has taken, determine whether the student can
graduate or not. Solution: Create a source s and a sink t. Create a vertex for each course that the student has taken and for each of the m requirements that they require to graduate. Add
edges from each requirement to the sink with capacity equal to the number of courses from the corresponding subset that the student must have taken to consider that requirement as
satisfied. Create edges from the source to each course that has been taken with capacity 1. For every course create an edge to each requirement which contains that course in its subset with
infinite capacity. Find the max flow in this flow network and if all edges from requirements to the sink have been saturated then it means that the courses the student has taken are sufficient
to satisfy all the requirement for graduation. Due to the capacity from source to courses being 1, each course can only count towards a single requirement.
Suppose you are given an arbitrary directed graph G = (V, E) with arbitrary edge weights w : E → R, and two special vertices s, t. Each edge in G is colored either red, white, or blue to indicate
how you are permitted to modify its weight: • You may increase, but not decrease, the length of any red edge. • You may decrease, but not increase, the length of any blue edge. • You may
not change the length of any black edge. Your task is to modify the edge weights, subject to the color constraints, so that every path from s to t has exactly the same length. Both the given
weights and the new weights of the edges can be positive, negative, or zero. Assume every edge in G lies on at least one path from s to t, and that G has no isolated vertices (it is connected).
Solution: We will reduce this to a linear program. Let δe be a variable denoting the change that we will make to edge e. Further, let pi denote the list of edges in path i from s to t. We add a
constraint that the length of the paths (sum of weights) after making the change δe must be equal for any two pair of paths from s to t. max 1 s.t. ∑ e∈pi w(e) + δe = ∑ e′∈pj w(e′) + δe′ ∀
paths pi, pj δe = 0 ∀e colored black δe ≥ 0 ∀e colored red δe ≤ 0 ∀e colored blue. ||||| Find the maximum and minimum of an array of integers. Solution: Use a similar construction as we did
for sum/average of an array of integers. However, the lower bounds will all be 0 for every edge except the t to s edge. That edge uses l = u = 1. This will ensure that exactly one flow has to go
through the network and since we are doing min cost circulation, this will ensure that the flow goes through the minimum price edge which is the minimum integer value in the array. Using
the reduction for max cost flow (using negative of the original prices) will allow us to also find the maximum value in the array. ■ 8. (∗) Given a directed graph G = (V, E) find the shortest path
between every pair of vertices u and v. Solution: We utilize the reduction for finding the shortest path from s to every other node and repeatedly use it by setting s to be each node from the
graph in turn. ■ 9. (∗) Given an undirected graph G = (V, E) with weights w : E → R≥0 on edges, find a minimum spanning tree T of G. Solution: Approach 1: We will reduce this to a linear
program. Let xe be an indicator variable which is 1 whenever the edge e is a part of the MST and is 0 otherwise. max ∑ e ∈V wexe Minimize weight of picked edges s.t. ∑ e∈E xe = |V | − 1 Fix
the total number of edges picked ∑ e={u,v}∈E xe ≥ 1 ∀u ∈ V Ensure picking 1 edge adjacent to every vertex xe ∈ {0, 1} One downside of the above approach is that we are using integer linear
programming and not standard LP. So we also provide another reduction.