0% found this document useful (0 votes)
2 views6 pages

HW6 Handout

This document outlines the guidelines and requirements for Assignment 6 in the CompSci 330 course at Duke University, due on October 28, 2024. It includes a recommended three-step process for completing homework, typesetting and submission instructions, collaboration policies, and grading criteria. Additionally, it presents several algorithmic problems that students must solve, including recurrence relations, northwesterly points in a 2D plane, twirled arrays, and a package managing robot algorithm.

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)
2 views6 pages

HW6 Handout

This document outlines the guidelines and requirements for Assignment 6 in the CompSci 330 course at Duke University, due on October 28, 2024. It includes a recommended three-step process for completing homework, typesetting and submission instructions, collaboration policies, and grading criteria. Additionally, it presents several algorithmic problems that students must solve, including recurrence relations, northwesterly points in a 2D plane, twirled arrays, and a package managing robot algorithm.

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/ 6

CompSci 330 Design and Analysis of Algorithms

Assignment 6, Fall 2024 Duke University

TODO: Add your name(s) here

Due Date: October 28, 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 (Recurrences Zoo). Solve the following recurrence relations and give a Θ bound for
each of them. If you use the master theorem, state the values involved and which the recurrence stat-
isfies in addition to the conclusion. For parts (g)–(k) in particular, consider unrolling/expanding2
the recurrence to observe a pattern then simplify the resulting sum.
(a) T (n) = 2T (n/3) + 1
(b) T (n) = 5T (n/4) + n
(c) T (n) = 7T (n/7) + n
(d) T (n) = 9T (n/3) + n2
(e) T (n) = 8T (n/2) + n3
(f) T (n) = 49T (n/25) + n3/2 log n
(g) T (n) = T (n − 1) + 2
(h) T (n) = T (n − 1) + nc where c ≥ 1 is a constant
(i) T (n) = T (n − 1) + cn where c > 1 is a constant
(j) T (n) = 2T (n − 1) + 1

(k) T (n) = T ( n) + 1

2
All of the recurrences can be solved without the master theorem, which itself can be proven with this technique.
This technique is often visualized as a “recursion tree” as seen in lecture for the runtime analysis of mergesort.

2
Problem 2 (Northwest) Let S = {pi = (xi , yi ) : 1 ≤ i ≤ n} be a set of n points on a 2D plane.
We say that a point pi is northwest (NW) of pj if xi ≤ xj and yi ≥ yj , treating the (−x)-direction
(resp., (+y)-direction) as west (resp., north). A point pi is a northwesterly point of S if there are
no other points in S NW of pi . In the example below, the larger red points are the northwesterly
points of S.

Describe an algorithm that returns the northwesterly points of S in O(n log n) time. You can
assume3 that the points of S are already sorted in increasing order of x-coordinate, breaking ties by
the greater y-coordinate; e.g., (1, 5) comes before (1, 3). Justify the correctness of your algorithm,
then state and justify its runtime, e.g., by unrolling/expanding, the recursion tree method, or
applying master theorem.
[Hint: Divide S into two sets based on the x-coordinates of points, and “conquer.” What is the
appropriate merge/combine step to obtain the overall solution?
3
This ordering could be obtained in O(n log n) time, e.g., using mergesort.

3
Problem 3 (Twirling) An array A[1..n] of n distinct integers (no duplicates) is considered
twirled if it consists of two non-empty contiguous sequences, L and R, that are sorted in increasing
order and all elements in L are greater than those in R. In other words, if R instead appeared left
of L, the entire resulting sequence would be sorted in increasing order.

For example, in the following twirled array, the left subsequence is underlined and has length 6,
the right subsequence is overlined and has length 4, and indeed all elements in the left subsequence
are greater than those in the right subsequence:
[12,14,19,23,25,27,2,3,5,7]
Describe an O(log n)-time algorithm to return the length of the left subsequence of a given twirled
array A. Justify its correctness, then state and justify its runtime, e.g., by unrolling/expanding,
the recursion tree method, or applying master theorem.
[Hint: Consider a variant of binary search. Make sure that your recursive calls are always made
on twirled arrays, as your recursive algorithm will likely assume and make use of this fact.]

4
Problem 4 (Package Managing) ** Autograder available 10/22 @ 5pm **
The FlipMaster is a new two-armed robot to organize packages on a conveyor belt, where the
packages are sequenced one after another. (If their manufacturer has any luck, they will be acquired
by Amazon for an enormous sum.)
The setting in which the FlipMaster is to be used is as follows. Every package on the conveyor has
an identifier (ID) on it, which is a positive integer. The FlipMaster can interact with the packages
with two basic operations:
• peek(i): It returns the ID of the i-th package from the left in O(1) time, and
• flip(i,j): For two indices i < j, it uses its two arms to squeeze the i-th to (j − 1)-th
packages together, flips them around to reverse their ordering, then sets them back down. In
effect, this reverses the subsequence of packages from index i to j − 1, inclusive4 .
Example: Let S = [4, 5, 2, 1, 3] be the sequence of IDs of the packages from left to right on the
conveyor belt. Then peek(1) returns 5 and flip(1,4) flips the 1st through 3rd packages so that the
IDs become [4, 1, 2, 5, 3], where the underlined symbols are those in the affected subsequence.
Let f be a FlipMaster object which has n packages on its conveyor belt, let i, j be indices between
0 and n, and let p be a positive integer (the “pivot” ID). Design and implement a divide-and-
conquer recursive method flartition(f,i,j,p) using only peek and flip operations on S, and
returns an index k where:
• all packages with indices i to k − 1 have IDs less than p, and
• all packages with indices k to j − 1 have IDs at least p.
In other words, the returned index k is the first index of a package on the FlipMaster’s conveyor
belt that is at least p. If all packages have IDs less than p, then k = j, and if all packages have IDs
at least p, then k = i.
Example: For the sequence of package IDs S = [4, 9, 330, 1, 2, 5, 8, 16], a valid value of S after
Flartition(f, 2, 7, 6) is [4, 9, 2, 1, 5, 330, 8, 16], where the underlined elements are those in the af-
fected subsequence. The return value of the call is 5, which is the index of the first element (330)
at least the pivot value of 6. The orderings of the elements less than/at least the pivot value 6 are
arbitrary.
[Hint: Split the problem into two halves, conquer with recursion, then combine their results into a
solution. The median index of the i-th through (j − 1)-th packages can be computed as (i + j)/2,
rounded down.]
Assume that peek(i) takes O(1) time and flip(i,j) takes O(max{1, j − i}) time. The empirical
running time of your solution will be compared to an algorithm with worst-case asymptotic running
time O(n log n) for calls of the form flartition(f,0,n,p) on a FlipMaster f with a conveyor belt
of n packages.
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.
4
Note that the j-th is not included in the flip.

5
• Python. Download the template file flartition.py and FlipMaster class file flipmaster.py
from Canvas. Complete the method flartition.py, without ever directly accessing
the c or s variables of a FlipMaster. Only its methods should be used. Note that
the number of packages on the FlipMaster’s conveyor belt can be accessed with len(). See
flipmaster.py for all of its methods.
You should submit only flartition.py to the Gradescope item “Homework 6 (Python)”
with the complete function flartition, a top-level function that takes as input a FlipMaster
object, two indices, and a positive integer. The function returns an integer k, as defined
above. The function header is:

def flartition(fm:FlipMaster, i:int, j:int, p:int): -> int

• Java. Download the template file flartition.py and FlipMaster class file flipmaster.py
from Canvas. You should submit only Flartition.java to the Gradescope item “Homework
6 (Java).” Note that the number of packages on the FlipMaster’s conveyor belt can be
accessed with .length(). See FlipMaster.java for all of its methods.
You should submit only Flartition.java to the Gradescope item “Homework 6 (Java)”
with the complete method flartition, a top-level method that takes as input a FlipMaster
object, two indices, and a positive integer. The method returns an integer k, as defined above.
The method header is:

public int flartition(int[] s, int i, int j, int p);

You might also like