Programming Assignment 2 - Tri-partitioning

Computer Algorithms II, Fall 2007


Due Date

This assignment is due by 5:30 p.m. on Tuesday, 16 October,

. See the assignment turn-in page (last modified on 14 January 2006) for instructions on turning in your assignment.

Background

The three possibly different-size vectors vi, vj and vk of non-negative integers are tri-partitioned if all the numbers in vi are congruent to 0 modulo 3, all the numbers in vj are congruent to 1 modulo 3, and all the numbers in vk are congruent to 2 modulo 3.

The Problem

Write a function with the prototype

struct answer {
  unsigned v1, v2, v3, moves;
  }

typedef std::vector<unsigned> ivec;

answer tripartition(ivec iv1, ivec iv2, ivec iv3);

that accepts three vectors of non-negative integers and determines the minimum number of moves required to tri-partition the vectors. A move occurs whenever a number is shifted from one vector to a different vector (shifting a number within the same vector is not a move). The answer returned by tripartition() should contain the following values:

If there is more than one minimal tri-partition possible, tripartition() can return any of them.

The answer struct, the ivec typedef and the tripartition prototype are defined in tripartition.h in the assignment directory /export/home/class/cs-306/pa2.

You can test your procedure by compiling it with the file

/export/home/class/cs-306/pa2/main.o
Running the resulting executable calls tripartition() and compares the answer returned with my answer. If you get no output, then everything's fine; otherwise you'll get an error message indicating a problem.

The -s command-line option takes an integer argument and uses it as the seed for the random-number generator. You can run your code repeatedly on the same set of randomly-generated data by passing in the same value with -s on each execution. The default is to generate a different set of randomly-generated data with each execution.

The source file for main() will not be made available because it contains my answer to the problem.

Your implementation of tripartition() should not exploit any extra properties of the input data; I'll be generating different input data when I test your assignments.

To be correct, your solution must be efficient; that is, you solution should take no more than twice the execution time my solution takes on the same input. The test executable keeps track of execution times. The -t command-line option prints the execution time taken by your version of tripartition().


This page last modified on 12 October 2007.