CSCI 136: Assignment 4 -- Compression

Due 3/8/98


Description

A common need with computers is to store massive amounts of information about an object. A good example is storing graphic images. In order to save space both on disks and in transmission of information across the internet, researchers have designed algorithms to compress data. In this lab you will learn one of these compression techniques.

A graphic image can be represented by a two dimensional array of information about the colors of various picture elements (or pixels). At high resolution the image may be composed of 1000 rows and 1000 columns of information, leading to the need to store information on 1,000,000 pixels per image. Needless to say this creates serious problems for storing and transmitting these images. However most images tend to have contiguous groups of pixels, each of which are the same color. We can take advantage of this by trying to encode information about the entire block in a relatively efficient manner.

The basic idea of our encoding will be to represent a block of pixels with the same color by simple recording the first place where we encounter the new color and only recording information when we see a new color. For instance suppose we have the following table of information (where we will imagine each number represents a color):

2 2 2 3 3
2 3 3 3 3
3 1 1 1 3

If we imagine tracing through the table from left to right starting with the top row and going through successive rows then we notice that we only need to record the following entries:

2 - - 3 -
2 3 - - -
- 1 - - 3

Rather than recording this in a two-dimensional table, it will now generally be more efficient to keep this information in a linear list:

(0,0)*2 (0,3)*3 (1,0)*2 (1,1)*3 (2,1)*1 (2,4)*3
In this program you are to design a class which will represent one of these compressed tables. You will do this in two stages:
  1. First extend DoublyLinkedList to a class CurDoublyLinkedList. This class should have extra capability to move to any desired element of the list (called the current element) and then either add a new node after this element or to remove the current element. Your new class should support all of the old methods of the Lists as well as first(), last(), next(), back(), isOffRight(), isOffLeft(), getCurrent(), addAfterCurrent(Object value), and deleteCurrent(). Specifications for these methods can be found in the attached code.

  2. Use this to design the class CompressedTable implementing the interface TwoDTable. The two key public methods of this class are updateInfo(row,col,newInfo) and getInfo(row,col). Again, specifications can be found in the attached code. This class will have an instance variable of type CurDoublyLinkedList (as well as other instance variables).

By the day of lab I will have a program available which allows the user to interact with a window to create and manipulate an image which will be stored using a CompressedTable. Your classes can be linked in with my program, resulting in a very efficient way of storing such images.

Here is a demo of the program:


I have provided you with a lot of code here, but you will find that much of the omitted code is quite tricky. This project will require you to be very careful in developing the code for the methods. Look carefully at the provided code and design your methods very carefully. In particular, be sure to test your code carefully as it is developed as you will likely make several logical errors if you are not extremely careful.

The updateInfo method of CompressedTable is probably the trickiest code to write. Here is a brief outline of the logic.

  1. Find the node of the list which encodes the position being updated.
  2. If the new information is the same as that in the node then nothing needs to be done. Otherwise determine if the node represents exactly the position being updated.
  3. If so, update the value of the node, otherwise add a new node representing the new position
  4. If you are not careful you may change several positions in the table to the new value. Avoid this be considering putting in a new node representing the position immediately after the position with the new value.
  5. If there is already a node with this successor position then nothing needs to be done. Otherwise add a new node with the successor position and the original value.

Extra Credit

As you add more information to the table, you will notice that the table is no longer as efficient in space, because several consecutive entries may have the same values. Make the representation more efficient by dropping later values if they can be subsumed by earlier ones.

For example, the list

(0,0)*2 (0,3)*3 (1,0)*3 (1,1)*3 (2,1)*1 (2,4)*3
can be replaced by the much simpler list:
(0,0)*2 (0,3)*3 (2,1)*1 (2,4)*3
For extra credit, modify the updateInfo method of CompressedTable to eliminate consecutive items with the same value. The amount of extra credit received will be proportional to the efficiency of your algorithm. Ideally this optimization will only take O(1) time each time something is inserted in the table.

What to hand in

This is your most complex program yet. As a result you should bring a very complete design for your program to lab on Wednesday. We will be grading your designs of the two methods deleteCurrent of CurDoublyLinkedList and updateInfo of CompressedTable. While these should not be written in Java, we will need to see the complete logic of the methods. These designs will be worth 10 to 20% of your grade for this lab so do a good, careful job. Remember that you must draw pictures and look at all possible special cases in order to get these right. Make an extra copy for yourself because we will be collecting these.

Your final program will be due on Sunday, March 8 at 11:59pm. As usual place all of your code (including projects with test code) in a folder and drop it off in the CSCI 136 drop-off folder.