0% found this document useful (0 votes)
18 views

CSCI570 - Fall2024 HW3 Sep29

Uploaded by

Pranav Pandy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

CSCI570 - Fall2024 HW3 Sep29

Uploaded by

Pranav Pandy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Homework 3

CSCI570 Fall 2024

Due: Oct. 6, 2024

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


that ALG’ is asymptotically no slower than ALG? You need to:


(a) Analyze T (n) using the Master Theorem. (4pts)
(b) Analyze T ′ (n) using the Master Theorem. Analyze and give the largest possible value of a. (6pts)
4. We know that binary search on a sorted array of size n takes Θ(log n) time. Design a similar divide-
and-conquer algorithm for searching in a sorted singly linked list of size n. Discuss its worst-case
runtime complexity.
5. Given an array of N distinct integers, find a subarray [L, R] such that:
1. All numbers in the subarray are between the two endpoints, i.e., the subarray contains all integers
i where L ≤ i ≤ R.
2. The subarray contains the maximum number of integers that satisfy this condition.

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.

(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 number
of distinct combinations of ticket sales to reach fundraising goal of n dollars. (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 runtime complexity of your solution? Explain your answer. (1 point)
8. Jack has gotten himself involved in a very dangerous game called the octopus game where he needs to
pass a bridge which has some unreliable sections. The bridge consists of 3n tiles as shown below. Some
tiles are strong and can withstand Jack’s weight, but some tiles are weak and will break if Jack lands
on them. Jack has no clue which tiles are strong or weak but we have been given that information
in an array called BadTile(3,n) where BadTile (j, i) = 1 if the tile is weak and 0 if the tile
is strong. At any step Jack can move either to the tile right in front of him (i.e. from tile (j, i) to

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

Figure 1: Example of octopus game.

(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 total
number of ways to safely cross the bridge. (4 points)
Make sure you specify
- base cases and their values
- where the final answer can be found (e.g. opt(n), or opt(0,n), etc.)
(d) What is the complexity of your solution? (1 point)
9. Suppose you have a rod of length N , and you want to cut up the rod and sell the pieces in a way
that maximizes the total amount of money you get. A piece of length i is worth pi dollars. Devise a
Dynamic Programming algorithm to determine the maximum amount of money you can get by cutting
the rod strategically and selling the cut pieces.
(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)
10. Given n balloons, indexed from 0 to n − 1. Each balloon is painted with a number on it represented
by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get
nums[i − 1] × nums[i] × nums[i + 1] coins. Here left and right are adjacent indices of i. After the burst,
the left and right then becomes adjacent. You may assume nums[−1] = nums[n] = 1 and they are
not real therefore you cannot burst them. Design a dynamic programming algorithm to find the maxi-
mum coins you can collect by bursting the balloons wisely. Analyze the running time of your algorithm.

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)

You might also like