CSCI570 - Fall2024 HW3 Sep29
CSCI570 - Fall2024 HW3 Sep29
1. Given a graph, G = (V, E), whose edge weights are integers in the range [0, W ], where W is a relatively
small integer number compare to V and E, we could run Dijkstra’s algorithm to find the shortest
distances from the start vertex to all other vertices. Design a new algorithm that will run in linear
time O(V + E) and therefore outperform Dijkstra’s algorithm. Explain the time complexity. No proof
is needed for the correctness of your algorithm.
2. Solve the following recurrences by giving tight Θ-notation bounds in terms of n for sufficiently large n.
Assume that T (·) represents the running time of an algorithm, i.e., T (n) is positive and non-decreasing,
and for small constants c independent of n, T (c) is also constant.
(a) n
T (n) = 9T + n log n
5
(b)
√ n √
2024
T (n) = 2024T +n
3
(c) √
T (n) = 2T ( n) + log2 n
(d) n
T (n) = 9T + n2 log n
3
(e) n
T (n) = 10T + 2n
2
3. The recurrence T (n) = 7T n2 + n2 describes the running
time of an algorithm ALG. A competing
algorithm ALG’ has a running time of T ′ (n) = aT ′ n4 + n2 log n. What is the largest value of a such
1
Formally, for any index i in the subarray, the condition min(subarray) ≤ nums[i] ≤ max(subarray)
holds true, where min(subarray) and max(subarray) refer to the minimum and maximum values of the
subarray, respectively.
Example 1: For the array [8, 1, 3, 4, 7], the answer is [1, 7].
Example 2: For the array [2, 6, 5, 4, 9, 8], the answer is [2, 9].
You need to:
(a) Describe a D&C algorithm that finds the subarray. Use natural language or pseudo code. (4pts)
(b) Explain why your algorithm finds the subarray. (2pts)
(c) Analyze the time complexity of your algorithm using the Master Theorem. (4pts)
You don’t need to consider situations like such subarray doesn’t exist.
6. Assume a truck with capacity W is loading. There are n packages with different weights, i.e. (w1 , w2 , . . . wn ),
and all the weights are integers. The company’s rule requires that the truck needs to take packages
with exactly weight W to maximize profit, but the workers like to save their energies for after work
activities and want to load as few packages as possible. Assuming that there are combinations of
packages that add up to weight W , design an algorithm to find out the minimum number of packages
the workers need to load.
(a) Define (in plain English) subproblems to be solved. (2 points)
(b) Write a recurrence relation for the subproblems. (3 points)
(c) Using the recurrence formula in part b, write pseudocode using iteration to compute the minimum
number of packages to meet the objective. (2 points)
(d) Make sure you specify base cases and their values; where the final answer can be found. (2 points)
(e) What is the worst case runtime complexity? Explain your answer. (1 point)
7. Imagine you are organizing a charity event to raise funds for a cause you care deeply about. To reach
your fundraising goal, you plan to sell two types of tickets.
You have a total fundraising target of n dollars. Each time someone contributes, they can choose to
buy either a 1-dollar ticket or a 2-dollar ticket. Use Dynamic Programming to find the number of
distinct combinations of ticket sales you can use to reach your fundraising goal of n dollars?
For example, if your fundraising target is 2 dollars, there are two ways to reach it: 1) sell two 1-dollar
tickets; 2) sell one 2-dollar ticket.
2
(j, i + 1)), or diagonally to the left or right (if they exist). (No sideways or backward moves are allowed
and one cannot go from tile (1, i) to (3, i + 1) or from (3, i) to (1, i + 1)).
Using dynamic programming to find out how many ways (if any) there are for Jack to pass this deadly
bridge. In Fig. 1, we show an example of bad tiles in gray and one of the possible ways for Jack to
safely cross the bridge alive.
3
j↑ 2
1
i→ n
Example. If you have the nums = [3, 1, 5, 8]. The optimal solution would be 167, where you burst
balloons in the order of 1, 5, 3 and 8. The array nums after each step is:
[3, 1, 5, 8] → [3, 5, 8] → [3, 8] → [8] → []
And the number of coins you get is 3 × 1 × 5 + 3 × 5 × 8 + 1 × 3 × 8 + 1 × 8 × 1 = 167.
3
(a) Define (in plain English) subproblems to be solved. (2 points)
(b) Write a recurrence relation for the subproblems (3 points)
(c) Using the recurrence formula in part b, write pseudocode to find the solution.(2 points)
(d) Make sure you specify
i. base cases and their values
ii. where the final answer can be found
(2 points)
(e) What is the complexity of your solution? (1 point)