Paren Lab
Paren Lab
Parallel and Sequential Data Structures and Algorithms 15-210 (Spring ’18)
1 Introduction
This assignment is designed to give you some practice with brute force and divide-and-conquer algo-
rithms. You will implement two solutions to the maximum parenthesis distance problem and perform
some analysis of your solutions. Note that this lab is conceptually difficult, so get started early!
2 Files
After downloading the assignment tarball from Autolab, extract the files by running:
from a terminal window on a unix/linux system. Some of the files worth looking at are listed below.
You should only modify the files denoted by *, as these will be the only ones that will be handed in in
terms of code
1. * MkBruteForcePD.sml
2. * MkDivideAndConquerPD.sml
3. Sandbox.sml
4. Tests.sml
written.pdf
which contains the answers to the written parts of the assignment. Your written answers should be
composed in a suitable word processor such as LaTeX, Word, OpenOffice, etc. Handwritten (and
scanned in) answers will NOT be accepted. If you decide to use LaTeX, please see the Piazza Re-
sources section for useful information on various LaTeX tools. Make sure your name is also in
the written.pdf file.
Computing Max Parenthesis Distance 15-210 (Spring 2018)
3 Submission
To submit your assignment to Autolab, generate a handin archive handin.tgz with the command
make package
You can then open the Autolab webpage at https://autolab.andrew.cmu.edu/, login and submit
your handin.tgz file via the “Submit File” link. Please double check your submission on the
Autolab webpage to make sure there were no errors.
Note that your code will not be graded until after the due date. It is your responsibility to test
your code thoroughly before submitting.
Before submission, always, always verify that the files you will submit are indeed the versions that
you intended to submit. Be very careful to NOT submit the wrong lab’s written.pdf, blank files,
or old versions of the files. We will not entertain “Oh no, I submitted the wrong file!” emails or
Piazza posts after the due date. We will adopt a zero-tolerance approach to this, and you will receive
the grade of your most recent submission (which may be subject to late submission penalties!) You
have been warned.
The questions below ask you to organize your solutions in a number of modules written almost
from scratch. Your modules must be named exactly as stated in the handout. Correct code inside an
incorrectly named structure, or a structure that does not ascribe to the specified signatures, will not
receive any credit.
You may not modify any of the signatures or other library code that we give you. We will test your
code against the signatures and libraries that we hand out, so if you modify the signatures your code
will not compile and you will not receive credit.
This assignment requires the use of library code spread over several files. The compilation of this
is orchestrated by the compilation manager through several .cm files. Instructions on how to use CM
can be found in the file cm.pdf under Resources on Piazza.
Style grading for this assignment will be evaluated on a 5 point scale: -2 through +2. Style points
will be added to your final grade from the rest of your homework. You should review the full style
guide available on the Piazza site under Resources.
You should submit your solutions on or before the deadline on the top of the first page. Failure to
do so will cause substantial deductions of your grade. Late submissions of up to a day will scale your
homework grade by 70% (so if you were to get 100% as your grade, you will now get 70%). Late
submissions of up to 2 days will scale your homework grade by 50%. Beyond 2 days, you will receive
0 as your homework grade. This scaling will be handled outside Autolab.
2
Computing Max Parenthesis Distance 15-210 (Spring 2018)
4 Parenthesis Distance
We define a matched sequence of parentheses inductively as
p ::= hi | p p | ( p )
In other words, a matched sequence is one of (a) the empty sequence, (b) the concatenation of two
matched sequences, or (c) a pair of parentheses surrounding a matched sequence. In the third case,
we’ll say that the parenthesis distance of the pair of parentheses is the length of the sequence they
are surrounding. The maximum parenthesis distance (MPD) is the largest of these distances within a
particular sequence. For example, () has an MPD of 0, (()) has an MPD of 2, and ()((())())(())
has an MPD of 6. Note that MPD is defined only for matched sequences which are non-empty.
4.1 Logistics
• Representation: Take a look at support/Paren.sml. This contains the definition of the type
of parentheses as well as some utility functions. Just to be clear: Paren.L and Paren.R are
the “left” and “right” parentheses, respectively. The input () would be given by the sequence
hParen.L, Paren.Ri.
• Modules:
– By default, we tend to keep all of our structures closed. This means that you will always
need to preface identifiers with what structure they come from. For example, to map across
a sequence in Task 4.1, you’ll need to write “Seq.map”.
– For structures which implement an ADT, we use the convention of including a type t in
the signature. For example, the type of parentheses in this assignment should be written
Paren.t.
3
Computing Max Parenthesis Distance 15-210 (Spring 2018)
4.2 Implementation
5 Testing
There are two ways to test your code.
1. In Sandbox.sml, write whatever testing code you’d like. You can then access the sandbox at
the REPL:
- CM.make "sandbox.cm";
...
- open Sandbox;
2. In Tests.sml, add test cases according to the instructions given. Then run the autograder:
- CM.make "autograder.cm";
...
- Autograder.run ();
As usual, it is very important that you thoroughly test your code before you submit. After the deadline,
we will grade your code with a private set of test cases.
4
Computing Max Parenthesis Distance 15-210 (Spring 2018)
6 Analysis
Task 6.1 (15%). Analyze your brute force implementation of parenDist, giving tight Big-O bounds
for its work and span. For cost bounds of sequence functions, you should assume the ArraySequence
implementation. Your score for this problem will depend on the correctness of your code (i.e. you
can’t just ignore Task 4.1 and expect to get free points here for correctly stating that your code is
O(1)).
For each of the following, solve the given work and span recurrences. Show your work, and give your
answers as tight Big-O bounds in terms of n, the length of the input.
Task 6.2 (15%). These recurrences were given in Task 4.2. Solve them to determine the asymptotic
runtime behavior of your code. You may use any method from class: repeated expansion, tree, brick,
or substitution.
⇣n⌘
W (n) = 2 W + O(1)
⇣ n ⌘2
S(n) = S + O(1)
2
Task 6.3 (15%). In Task 4.2, we assumed Seq = ArraySequence. If we instead used TreeSequence,
then your work and span might have looked something like the following.
Solve the work recurrence. You may use any of repeated expansion, tree, brick, or substitution meth-
ods for the span recurrence.
⇣n⌘
W (n) = 2 W + O(log n)
⇣n⌘2
S(n) = S + O(log n)
2
Task 6.4 (15%). Finally, if we used ListSequence, then your work and span might look like the
following. Solve these recurrences using any method from class: repeated expansion, tree, brick, or
substitution.
⇣n⌘
W (n) = 2 W + O(n)
⇣ n ⌘2
S(n) = S + O(n)
2
Task 6.5 (5%). Solve the following recurrence asymptotically using any of the methods:
2n n
W (n) = W ( )W ( ) + kn
3 3