Material Type: Assignment;
Class: Algorithms and Abstract Data Types;
Subject: Computer Science;
University: University of California-Santa Cruz;
Term: Fall 1998;
Download Programming Assignment 2 - Sparse Matrix Calculator | CMPS 101 and more Assignments Computer Science in PDF only on Docsity!
CIS101 Handout #11 David Helmbold October 22, 1998 Programming Assignment #2, Sparse Matrix Calculator due Monday November 2 at Midnight The purpose of this assignment is to use your linked module in a two-dimensional structure, and gain further practice with modularity. Matrices appear in a variety of applications, from graphs to optimization problems. In this assignment you will implement a calculator for performing matrix operations that exploits the (expected) sparsencss of the matrices (i.c. the fact that most of the entries will be zero). This will allow you to build a fast matrix multiplier. Note: do not implement Strassen’s method! You will be required to implement three primary operations for matices: multiplication, addition, and finding the transpose. Recall that if A and B are n xn matrices, the matrix product C is also a nxn matrix where Ci (the row ¢ column j element of C) is 7;_, AiwZ%j- If most of the elements in A and B are zero, then very few of these mulliplications will actually need to be done. The matrix sum D = A+ B has entries Dj; = Ajj + Bij. The third operation you will need is the matrix transpose: If EF is the transpose of A, then Ej; = Aji. Therefore the transpose “flips” the rows and colunms of the matrix. Note that there are several ways to represent the values stored in a matrix. The most standard way is as a 2-dimensional array of numbers. An alternative is as a list of Semtrics” where cach entry contains a row, column, and the value stored at the position given by the row and column. Note that if a row/column pair is uot. on the list, then the corresponding value can be assumed to be zcro. Therefore, if most of the em are zero then this method can result in a large space savings. In this assignment you will build a matrix ADT using this kind of idea. ‘Your program should contain a sparse-matrix ADT that represents a matrix as a one-dimensional array of lists, with one list for each row. Each list element represents a single non-zero entry in the matrix. There are lwo ways lo do this. You will need to store the column number and value for the non-zero matrix entry, and may also want to store the row number. One implementation idea is to define a new ADT for matrix entries that ties together the column number and value (and possibly row number) for the entry, and have each “list element” hold a handle to one of these objects. Another way is to add some list functions (and modify others) so that the user of the ADT can store and retrieve both the column number and value (and possibly row, if you choose) from the list clements. As always, you should be constructing a driver in parallel with the ADT to check out that things are working properly. Thus the matrix ADT will be using the functions in the list ADT’s header to help it do its job. The matrix ADT should export operations like the following: g « void NewMatrix(MatrixHandle *M); « void FreeMatrix (MatrixHandle *M); boolean IsAllZero(MatrixHandle M); ¢ void ChangeEntry(MatrixHandle *M; int Row, Col; double NewValue) ; * void MakeAllZero(MatrixHandle M); « void Multiply (MatrixHandle A, B; MatrixHandle Product); « void Add (MatrixHandle A, B; MatrixHandle Sum); « void Transpose(MatrixHandle A; MatrixHandle Transp); * void PrintByRows (MatrixHandle M) ;