Next: What to submit
Up: Write once, solve thrice:
Previous: Optimal structure of a
Your job
You'll need to download
testChain.cpp,
chain.h,
Makefile. You should be able to
click (or shift-click) on the appropriate link.
Once you've downloaded the appropriate files, you have these tasks:
- Implement minPartition(int i, int k) (see
chain.h). This returns a minimal
partition (a list of components, see below) of the range
i...k of a chain. Some definitions:
- range i...k
- For a polygon, this means the
(sub)polygon from vertex i to vertex k. For
a matrix chain, this means the (sub)chain from matrix
i to matrix k-1. For a BST this means the
(sub)BST from node i+1 to node k-1.
- component

- A triple of integers that specify a
component of a partition. For a polygon this
specifies the triangle with vertices vertex
, vertex
,
and vertex
. For a matrix chain this specifies the
parenthesization of matrices
through
and then of
matrices
through
. For a BST this specifies that
node
is the root of a (sub)tree with left child comprised
of nodes
through
and right child comprised of
nodes
through
.

- The weight of component
. For a
polygon, this is the perimeter of the triangle with vertices
vertex
, vertex
, and vertex
. For a chain of matrices
this is the product
, where
is the number
of rows in matrix
,
the number of rows in matrix
,
and
the number of columns in matrix
. For a BST
this is the sum of frequencies
.
- link
- The basic unit of a chain. For a vertexChain, this is a
vertex with a pair of double values for its coordinates. For a
matrixChain, this is a single matrix with a pair of doubles
describing its dimensions (number of rows, number of columns). For
a bstChain, this is a node with a pair of doubles representing its
key and frequency.
- Implement partitionTally(componentNode *partitionList)
(see chain.h). This returns
the sum of the weights
of components in the linked list
partitionList.
- Implement constructors for classes
chain
in a file called
chain.cpp, vertexChain
in a file called vertexChain.cpp,
matrixChain
in a file called
matrixChain.cpp, and
bstChain
in a file called
bstChain.cpp.
- Implement any other functions, constants, or types needed to
make testChain.cpp
work
when it is provided with input of the form described below. You may
not change testChain.cpp. You may add to
chain.h, but you may not change any functions or
types originally declared there.
For a chain with
links (e.g. a polygon with
sides, a chain of
matrices, a BST with
nodes), your code should have worst-case
running time. Your solution should combine a recursive
algorithm with memoization. Your solution should use generic code,
rather than three repetitive solutions.
Here is what testChains.cpp expects on standard input:
- One of the strings polygon, matrix, or
bst, followed by white space. This indicates the type of
chain that is being partitioned.
- An integer (we'll call it
) followed by white space. This
indicates the number of links in the chain.
pairs of double literals, separated and followed by white
space. These are the values of the links.
- A single double literal, followed by white space. This
represents the (approximate) calculated minimum value for partitioning
this chain.
As an example, look at
triangle.chn
or any of the
other examples in
../Code/TestChains. Once
your code is working, you should be able to see the following test
result:
./testChains < triangle.chn
Chain type: polygon Minimal partition
Next: What to submit
Up: Write once, solve thrice:
Previous: Optimal structure of a
Danny Heap
2002-11-27