CS140 -- Lab 5


You are not allowed...

You are not allowed to modify bitmatrix.h or bitmatrix_editor.cpp.

Bit-matrices

This lab is all about bit-matrices. These are matrices whose values can only be zero or one, and arithmetic is modulo 2. You would be surprised how powerful these are. They are used in fault-tolerant storage applications, among other things, which is why I am fond of them.

Let's take some simple examples. Here are four bit-matrices:

C003.txt
C244.txt
RV.txt
CV.txt

When you add bit-matrices, you simply add elements in corresponding rows and columns, modulo two. So, for example:

+ =

and

+ =

Bit-matrix multiplication is just like standard matrix multiplication, except that addition and multiplication of the individual elements is done modulo two. For example, multiplying RV.txt and CV.txt will yield a 1 X 1 matrix whose value is:

(1*1) + (0*0) + (1*1) + (0*1) + (0*1) + (0*0) + (0*0) + (1*1)

This is 1+0+1+0+0+0+0+1 which equals 3%2 which equals 1. Thus:

* =

Similarly:

* =

* =

and

* =

That last example is interesting -- when the product of two square matrices is equal to the identity matrix, then these two matrices are inverses of each other. Some matrices, like the one below, have no inverses:


Bitmatrix.h

You have a big job. Behold bitmatrix.h:

#include <string>
#include <iostream>
#include <vector>
using namespace std;

class Bitmatrix {
  public:
    Bitmatrix(string fn);            // Read from a file
    Bitmatrix(int rows, int cols);   // Create an empty bitmatrix
    void Print(int w);               // Print it on standard ouptput with spaces & blank lines 
    void Write(string fn);           // Write it to a file either as hex or zeros & ones
    void PGM(string fn, int pixels, int border);  // Write it to a pgm file 
    int Rows();
    int Cols();
    void Set(int row, int col, char val); // Set the specified element to val
    char Val(int row, int col);           // Return the specified element
    void Swap_Rows(int r1, int r2);
    void R1_Plus_Equals_R2(int r1, int r2);
    Bitmatrix *Copy();
  protected:
    vector <string> M; // The matrix.  Elements are '0' or '1'.
};

Bitmatrix *Sum(Bitmatrix *a1, Bitmatrix *a2);
Bitmatrix *Product(Bitmatrix *a1, Bitmatrix *a2);
Bitmatrix *Sub_Matrix(Bitmatrix *a1, vector <int> &rows);
Bitmatrix *Inverse(Bitmatrix *m);

class HTE {
  public:
    string key;
    Bitmatrix *bm;
};

typedef vector <HTE *> HTVec;

class BM_Hash {
  public:
    BM_Hash(int size);
    void Store(string &key, Bitmatrix *bm);
    Bitmatrix *Recall(string &key);
    HTVec All();
  protected:
    vector <HTVec> table;
};

The Bitmatrix class implements methods that allow you to create, read, write and manipulate bit-matrices. Here are the methods:

The only piece of data in a bitmatrix is a vector of strings named M. If M is storing a r by c matrix, then it will contain r strings, each of which has c characters. The characters are either '0' for zero or '1' for one.

So, you have to implement 10 methods (there are 12, but I've implemented two of them for you), and only one of them (PGM()) is difficult. Were I you, I'd wait until the end to do PGM().

You have to implement four procedures which operate on pointers. Since these are not part of the data structure, you have to use Rows() Cols(), Set(), and Val() to implement them. You should not modify the input matrices in any way.

The first three of these are very easy. Inverse() will be tougher.

Finally, you are also going to implement a hash table to store bit-matrices with keys that are strings. You should use the djb_hash() function from class as the hash function and you should use separate chaining as the collision resolution mechanism. Your hash table is a vector of vectors of pointers to HTE's (hash table entries). Each hash table entry stores a key and a pointer to a bit-matrix.

The BM_Hash class has four methods:

You should not call new or delete on bit-matrices when you implement any of the hash table methods. You should call new when you create a new HTE. That is the only time that you will call new in the hash table methods.


Starter Code

The file bitmatrix_start.cpp provides dummy implementations for everything in the lab, with the exception of Bitmatrix(string fn) and Print(). The nice thing about it is that it will compile with any program that uses bitmatrix.h. However, it won't run correctly. It's a good starting place for you. Make sure that you copy bitmatrix_start.cpp to bitmatrix.cpp before you submit your code!.

The testing program matrix_editor.cpp

I've written bitmatrix_editor.cpp as a program that uses all of the features of bitmatrix.h. You run it with an optional prompt as a command line argument. If you don't specify a prompt, it will not print a prompt. This is nice because you can treat the editor as an interactive editor, or you can write scripts for it.

Copy bitmatrix_editor.cpp, bitmatrix.h and makefile to your directory. Then copy bitmatrix_start.cpp to bitmatrix.cpp and type make:

UNIX> make
g++ -c bitmatrix_editor.cpp
g++ -c bitmatrix.cpp
g++ -o bitmatrix_editor bitmatrix_editor.o bitmatrix.o
UNIX> 
Now you have a version of bitmatrix_editor that only works for what I have implemented. However, let's explore those. Go ahead and copy the following matrix files too: C003.txt, C244.txt, CM4_3.txt, CV.txt, RV.txt, t3.txt and t4.txt. Bitmatrix_editor reads lines of text from standard input. Blank lines and lines that begin with '#' are ignored. Otherwise, the first word on a line is a command and the remaining words are arguments.

At all times, there is one "current matrix." You may also store and recall matrices with single-word keys. Three simple commands are:

Since I have implemented these for you in bitmatrix_start.cpp, they will work without any extra effort from you:
UNIX> bitmatrix_editor "BM-Editor>"
BM-Editor> READ C003.txt
BM-Editor> PRINT
10000001
11000000
01100001
00110001
00011001
00001100
00000110
00000011
BM-Editor> PRINT 4
1000 0001
1100 0000
0110 0001
0011 0001

0001 1001
0000 1100
0000 0110
0000 0011
BM-Editor> READ RV.txt
BM-Editor> PRINT
10100001
BM-Editor> PRINT 4
1010 0001
BM-Editor> QUIT
UNIX> 
bitmatrix_editor also implements the following commands, which of course will only work when you implement the proper methods and procedures: With these last four commands, bitmatrix_editor checks to make sure that you haven't modified the input matrices.

Some examples with the above matrices:

UNIX> bitmatrix_editor 'BM-Editor>'
BM-Editor> READ C003.txt
BM-Editor> STORE C003
BM-Editor> READ C244.txt
BM-Editor> STORE C244
BM-Editor> READ CV.txt
BM-Editor> STORE CV
BM-Editor> READ RV.txt
BM-Editor> STORE RV
BM-Editor> READ t3.txt
BM-Editor> STORE t3
BM-Editor> READ t4.txt
BM-Editor> STORE t4
BM-Editor> RECALL RV
BM-Editor> PRINT
10100001
BM-Editor> ALL
CV                               8 X   1
RV                               1 X   8
t3                              16 X   8
t4                               8 X  16
C003                             8 X   8
C244                             8 X   8
BM-Editor> SUM C003 C003
BM-Editor> PRINT
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
BM-Editor> SUM C003 C244
BM-Editor> PRINT
11111110
11111111
10000001
00111110
11100001
11110000
11111000
11111100
BM-Editor> PRODUCT RV CV
BM-Editor> PRINT
1
BM-Editor> PRODUCT C003 C244
BM-Editor> PRINT
10000000
01000000
00100000
00010000
00001000
00000100
00000010
00000001
BM-Editor> PRODUCT C003 CV
BM-Editor> PRINT
0
1
0
1
1
1
0
1
BM-Editor> RECALL t3
BM-Editor> PRINT 8
10000001
11000000
01100001
00110001
00011001
00001100
00000110
00000011

01111111
00111111
11100000
00001111
11111000
11111100
11111110
11111111
BM-Editor> RECALL t4
BM-Editor> PRINT 8
10000001 01111111
11000000 00111111
01100001 11100000
00110001 00001111
00011001 11111000
00001100 11111100
00000110 11111110
00000011 11111111
BM-Editor> PRODUCT t3 t4
BM-Editor> PRINT 8
10000010 10000000
01000001 01000000
10100010 00100000
01010011 00010000
00101011 00001000
00010101 00000100
00001010 00000010
00000101 00000001

10000000 11010101
01000000 11101010
00100000 10100000
00010000 00000101
00001000 01010111
00000100 10101011
00000010 01010101
00000001 10101010
BM-Editor> PRODUCT t4 t3
BM-Editor> PRINT 8
01010111
10101011
00000010
01010110
01111100
10111110
01011111
10101111
BM-Editor> RECALL C003
BM-Editor> INVERT
BM-Editor> PRINT
01111111
00111111
11100000
00001111
11111000
11111100
11111110
11111111
BM-Editor> RECALL C244
BM-Editor> INVERT
BM-Editor> PRINT
10000001
11000000
01100001
00110001
00011001
00001100
00000110
00000011
BM-Editor> PRODUCT CV RV
BM-Editor> PRINT
10100001
00000000
10100001
10100001
10100001
00000000
00000000
10100001
BM-Editor> INVERT
Matrix not invertible.
BM-Editor> QUIT
UNIX>

Inverting a bit-matrix

Inverting a bit-matrix is easier than doing a general matrix inversion. However, the steps are the same. Suppose you want to invert the square matrix M. What you do is make a copy of M, and create an identity matrix of the same size as M. Call this matrix Inv. Then you perform "SWAP" and "+=" operations on the copy of M to turn it into an identity matrix. You perform the exact same operations on Inv. When you're done, the inverse of the original matrix is in Inv

Let's perform an example on the matrix in Inv-Ex.txt:

10110
01011
10111
11001
01100

You first go through each row of M from the first to the last, doing the following steps:

We'll do this for our example:

Action M Inv
Start
i=0
No swap necesary.
Set row 2 = row 2 + row 0.
i=0 still.
Set row 3 = row 3 + row 0.
i=1
No swap necesary.
Set row 3 = row 3 + row 1.
i=1 still.
Set row 4 = row 4 + row 1.
i=2
Swap row 2 and row 3
i=2 still.
Set row 4 = row 4 + row 2.
i=3
Swap row 3 and row 4

When you're done with this pass, M will be an upper-triangular matrix. Now, you start with the last row and go to the first row. Suppose you are in row i: When you are done with this step, M will be the identity matrix, and Inv will be the inverse of the original M:

Action M Inv
Start:
i=4
No action necessary.
i=3
Set row 3 = row 3 + row 4.
i=2
No action necessary.
i=1
Set row 1 = row 1 + row 3.
i=1 still.
Set row 1 = row 1 + row 4.
i=0
Set row 0 = row 0 + row 2.
i=0 still.
Set row 0 = row 0 + row 3.

Finally, let's double-check ourselves:

BM-Editor> READ Inv-Ex.txt
BM-Editor> STORE Inv-Ex
BM-Editor> INVERT
BM-Editor> PRINT
01101
11011
11010
00111
10100
BM-Editor> STORE Inv
BM-Editor> PRODUCT Inv Inv-Ex
BM-Editor> PRINT
10000
01000
00100
00010
00001
BM-Editor> 

Gradescript Rubric

First, this lab will count double.

The gradescript will have up to 200 test cases. 30 percent of them will test matrix inversion and 20 percent of them will test the PGM file. Your PGM doesn't have to match mine in format; however, its size and pixels must match mine exactly.