0% found this document useful (0 votes)
14 views5 pages

HW5 Handout

This document outlines the guidelines for Assignment 5 in the CompSci 330 course at Duke University, due on October 21, 2024. It includes instructions on how to approach homework problems, typesetting and submission requirements, collaboration policies, and grading criteria. The assignment consists of four problems related to algorithm design, including pottery firing optimization, interval covering, building height adjustment, and a flight connection matrix problem.

Uploaded by

benmatz132
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)
14 views5 pages

HW5 Handout

This document outlines the guidelines for Assignment 5 in the CompSci 330 course at Duke University, due on October 21, 2024. It includes instructions on how to approach homework problems, typesetting and submission requirements, collaboration policies, and grading criteria. The assignment consists of four problems related to algorithm design, including pottery firing optimization, interval covering, building height adjustment, and a flight connection matrix problem.

Uploaded by

benmatz132
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/ 5

CompSci 330 Design and Analysis of Algorithms

Assignment 5, Fall 2024 Duke University

TODO: Add your name(s) here

Due Date: October 21, 2024

How to Do Homework. We recommend the following three step process for homework to help
you learn and prepare for exams.
1. Give yourself 15-20 minutes per problem to try to solve on your own, without help or external
materials, as if you were taking an exam. Try to brainstorm and sketch the algorithm for
applied problems. Don’t try to type anything yet.
2. After a break, review your answers. Lookup resources or get help (from peers, office hours,
Ed discussion, etc.) about problems you weren’t sure about.
3. Rework the problems, fill in the details, and typeset your final solutions.

Typesetting and Submission. Your solutions should be typed and submitted as a single pdf on
Gradescope. Handwritten solutions or pdf files that cannot be opened will not be graded. LATEX1
is preferred but not required. You must mark the locations of your solutions to individual problems
on Gradescope as explained in the documentation. Any applied problems will request that you
submit code separately on Gradescope to be autograded.

Writing Expectations. If you are asked to provide an algorithm, you should clearly and unam-
biguously define every step of the procedure as a combination of precise sentences in plain English
or pseudocode. If you are asked to explain your algorithm, its runtime complexity, or argue for
its correctness, your written answers should be clear, concise, and should show your work. Do not
skip details but do not write paragraphs where a sentence suffices.

Collaboration and Internet. If you wish, you can work with a single partner (that is, in groups
of 2), in which case you should submit a single solution as a group on Gradescope. You can use
the internet, but looking up solutions or using LLMs is unlikely to help you prepare for exams. See
the homework webpage for more details.

Grading. Theory problems will be graded by TAs on an S/I/U scale. Applied problems typically
have a separate autograder where you can see your score. See the course policy webpage for details
about homework grading.

1
If you are new to LATEX, you can download it for free at latex-project.org or you can use the popular and free (for
a personal account) cloud-editor overleaf.com. We also recommend overleaf.com/learn for tutorials and reference.

1
Problem 1 (Pottery Firing). Phoebe is an artisan potter that specializes in terracotta. She
offers a vast catalog of vases with various sizes, colors, and shapes, and she has just received an
immensely large and complicated order for n different designs of vases, V1 , V2 , . . . , Vn . Each design
Vi takes a distinct number of minutes, si , for Phoebe to first sculpt by hand, and then another
distinct number of minutes, fi , for her to finish by firing it in her kiln. For purposes of this problem,
suppose her kiln can fit any number of sculpted vases to fire and that it is OK for fully-fired vases
to remain in the kiln until all vases are done firing.
Phoebe can only sculpt one design at a time, but immediately after she finishes sculpting a
vase she adds it to the others in the kiln (if any) so that she can begin sculpting another vase. Once
all vases are sculpted and fired in the kiln for at least as long as they all require, she pulls them
all out at once and her job is done. Your goal is to help Phoebe determine which order she should
sculpt the n designs in order to minimize the time when her job is done.
(a) Prove that ordering the designs in increasing order of si is not optimal by providing a coun-
terexample by providing an input for which the ordering does not yield the best possible end
time.
(b) Same as the first part, but ordering by increasing si + fi .
(c) Same as the first part, but ordering by decreasing si + fi .
(d) State an ordering for Phoebe to use and prove that it is optimal. You may find the following
proof template useful2 :
(i) Compare your ordering to that of an arbitrary optimal ordering: If the orderings are not
identical, then there are some pairs of designs in the optimal ordering that are out-of-
order with respect to your ordering.

(ii) Consider the optimal ordering with the fewest such pairs.

(iii) Show that swapping two adjacent out-of-order designs in that ordering reduces the num-
ber of out-of-order pairs without increasing the overall end time.

(iv) Conclude that, because the resulting order has fewer out-of-order pairs and optimal end
time, your ordering is indeed optimal by contradiction.

2
This is a proof by smallest counterexample instead of induction, which is functionally equivalent but perhaps
more intuitive here.

2
Problem 2 (Intervals). Let X be a set of n intervals on the real line labeled 1, . . . , n. We say
that a subset of intervals Y ⊆ X covers X if the union of all intervals in Y is equal to the union of
all intervals in X. The size of a cover is just the number of intervals.

Figure 1: A set of intervals, with a cover (shaded) of size 7 (not necessarily the optimal cover).

Describe a greedy O(n log n)-time algorithm to compute the smallest cover of X. Assume
that your input consists of two arrays L[1..n] and R[1..n], where (L[i], R[i]) is the i-th interval in
X. Note that X as given is ordered arbitrarily. For simplicity, assume that no two endpoints of
intervals are equal; that is, all elements of L, R are distinct. Prove the correctness of the algorithm
(including an exchange argument) and analyze its runtime complexity.

3
Problem 3 (Main Street). Flatville, IL is a small town with a single street, Main Street, which
runs through it from east to west. At the center of town there are n contiguous, rectangular
buildings along the south side of Main Street, each exactly 30 feet wide and 80 feet deep, which
overlook the town square on the north side of the street.
The mayor of Flatville thinks the shadows of the buildings make an unsightly, jagged shape on the
lawn of the town square due to the varying heights of the buildings, especially when the sun is
directly overhead at noon. For their first act as mayor, they have declared that the buildings along
Main Street must be remodeled by either reducing or increasing the height of the buildings in order
to make an aesthetically pleasing shadow. For example, the mayor would ideally make buildings
exactly the same height, say, 52 feet tall. However, this would require drastic remodeling of much
taller and much shorter buildings. Instead, the mayor wishes to minimize the pairs of adjacent
buildings with different heights, allowing a maximum change of δ > 0. That is, the height of each
building may be reduced by at most δ or increased up to at most δ. See the figure below.
Let H[1..n] be an array of integral heights of the buildings in their order on Main Street (that
is, H[i] > 0 is the height of the i-th building from east to west, and let δ > 0 be a parameter.
Describe and analyze an O(n)-time algorithm that reports the fewest possible number of adjacent
pairs of buildings with different heights after adjusting their heights by up to ±δ. See the figure
below.
104
92 80
76
64
60 52 60
48

36 36 36
20
12

Figure 2: n = 12 buildings on Main Street overlooking the lawn of the town square (not drawn to
scale). The original heights of the buildings are shown above in black, and a candidate solution
for δ = 45 is shown in blue, where the dotted line above/below the top of building denotes its new
height. This candidate solution has k = 1 which is the best possible for δ = 45.

4
Problem 4 (Applied). **Autograder available 10/8 @ 5pm**
The NotUnited airline company from Homework 2 is expanding from its m existing locations with
n new locations, by adding pairs of connecting flights. The input consists of two arrays, A[1..m]
and B[1..n], of non-negative integers where A[i] ≥ 0 is the number of new connections required by
the existing i-th location and B[j] ≥ 0 is the number of new connections required by the new j-th
location.
The goal is to compute m × n matrix F [1..m, 1..n] where F [i, j] = 1 if there is a new flight between
the i-th existing location and j-th new location and F [i, j] = 0 indicates no such new flight, that
meets the new flight constraints (or otherwise indicate no such matrix exists). In particular, F
must meet the following constraints:
• For all i, 1 ≤ i ≤ m, the i-th existing location has nj=1 F [i, j] = A[i] connections, and
P

• For all j, 1 ≤ j ≤ n, the j-th new location has ni=1 F [i, j] = B[j] connections.
P

For example, with A = [1, 3, 2] and B = [2, 2, 2], a valid matrix F is:
 
1 0 0
1 1 1
0 1 1

However, with A = [1, 3, 2] and before but B = [0, 3, 3], there is no solution.
The empirical running time of your solution will be compared to an algorithm with worst-case
asymptotic running time O(mn log(m + n)). Consider what a greedy strategy could look like in the
context of this problem in order to populate the matrix F appropriately. Language-specific details
follow. You can use whichever of Python or Java you prefer. You will receive automatic feedback
when submitting, and you can resubmit as many times as you like up to the deadline.
• Python. You should submit a file called not united.py to the Gradescope item “Homework
5 (Python).” The file should define (at least) a top level function notunited that takes as
input two lists of ints, A and B. The function returns a list of lists F:list[list[int]] that
represents matrix F , or returns None if no such matrix exists. The function header is:

def not united(A:list[int], A:list[int]): -> list[list[int]]

• Java. You should submit a file called NotUnited.java to the Gradescope item ”Homework
5 (Java).” The file should define (at least) a top level function NotUnited that takes in two
int[] that represent arrays A and B. The function returns an int[][] that represents
matrix F , or returns null if no such matrix exists. The method header is:

public int[][] notUnited(int[] a, int[] b);

You might also like