



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Details about a programming assignment for cmps 101 students, focusing on algorithms and abstract data types related to matrix operations and sparse matrix representation. The assignment involves creating a java program capable of performing fast matrix operations on sparse matrices, which can save space and time. Students are required to understand the concept of sparse matrices, matrix multiplication, and various matrix operations like addition, subtraction, scalar multiplication, and transposition. They will also need to modify their list adt and implement a matrix adt to efficiently represent and manipulate sparse matrices.
Typology: Assignments
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Algorithms and Abstract Data Types Summer 2009
In this assignment you will create a calculator for performing matrix operations that exploits the
of non-zero entries (abbreviated NNZ) is small compared to the total number of entries (^) n^2. The result will be a Java program capable of performing fast matrix operations, even on very large matrices, provided they are sparse.
kj n k
1
Thus the element in the i th^ row and j th^ column of C is the vector dot product of the i th^ row of A and the j th column of B. If we consider addition and multiplication of real numbers to be our basic operations, then the above formula can be computed in time ( n^3 ), which is impractical for matrix sizes n of more than a few thousand. If it so happens that A and B are sparse, then a great many of these arithmetic operations
S (^) ij A (^) ij B ij and Dij Aij Bij The scalar product of a real number x with A is denoted xA^ , and has ij^ th^ entry (^ xA^ ) ij x Aij. The transpose of A , denoted (^) AT , is the matrix T whose ij th entry is the ji th entry of A : Tij^ A^ ji. In other words, transposition “flips” the rows and columns of A. Each of these operations can be computed in time ( n^2 ), and just as for multiplication, their cost can be improved upon significantly when A and B are sparse. As one would expect, the cost of a matrix operation depends heavily on the choice of data structure used to represent the matrix operands. There are several ways to represent a matrix with real entries. The
is that all of the above matrix operations have a straight-forward implementation. In this project you will represent a matrix as a 1-dimensional array of Lists. Each List will represent one row of the Matrix, but only the non-zero entries will be stored. Therefore List elements must store not just the matrix entries, but the columns in which those entries reside. For example, the matrix below would have the following representation as an array of Lists.
expense of more complicated algorithms for performing standard matrix operations. Designing these algorithms in terms of List operations will constitute the majority of the work you do on this assignment. It will be necessary to make some minor changes to your List ADT from pa1. You must first convert your List ADT from a List of ints to a List of Objects. This entails changing certain field types, declaration statements, method parameters, and return types from int to Object. The Objects referred to by these List elements will be defined in the Matrix ADT specified below. Second, it will be necessary to eliminate the List operations copy() and cat() (which was optional anyway.) All other List operations from pa1 will be retained. The equals() operation however will be altered slightly so as to override, rather than overload, Object's built in equals() method. This is done by changing it's signature from boolean equals(List L), as in pa1 to public boolean equals(Object x), which is it's signature in the superclass Object. Indeed, all equals() methods in this project should carry this same signature. File Formats The top level client for this project will be called Sparse.java. It will take two command line arguments giving the names of the input and output files, respectively. The input file will begin with a single line containing three integers n , a , and b , separated by spaces. The second line will be blank, and the
space separated list of three numbers: two integers and a double, giving the row, column, and value of the corresponding matrix entry. After another blank line, will follow b lines specifying the non-zero entries
here as Entry. Thus Entry will have two fields that store types int and double respectively. Entry must also contain its own equals() and toString() methods which override the corresponding methods in the Object superclass. Your Matrix class will represent a matrix as an array of Lists of Entry Objects. It is highly recommended that these Lists be maintained in column sorted order. Your Matrix ADT will export the following operations. // Constructor //////////////////////// Matrix(int n) // Makes a new n x n zero Matrix. pre: n>= // Access functions /////////////////// int getSize() // Returns n, the number of rows and columns of this Matrix int getNNZ() // Returns the number of non-zero entries in this Matrix public boolean equals(Object x) // overrides Object's equals() method // Manipulation procedures //////////// void makeZero() // sets this Matrix to the zero state Matrix copy()// returns a new Matrix having the same entries as this Matrix void changeEntry(int i, int j, double x) // changes ith row, jth column of this Matrix to x // pre: 1<=i<=getSize(), 1<=j<=getSize() Matrix scalarMult(double x) // returns a new Matrix that is the scalar product of this Matrix with x Matrix add(Matrix M) // returns a new Matrix that is the sum of this Matrix with M // pre: getSize()==M.getSize() Matrix sub(Matrix M) // returns a new Matrix that is the difference of this Matrix with M // pre: getSize()==M.getSize() Matrix transpose() // returns a new Matrix that is the transpose of this Matrix Matrix mult(Matrix M) // returns a new Matrix that is the product of this Matrix with M // pre: getSize()==M.getSize() // Other functions //////////////////// public String toString() // overrides Object's toString() method It is required that your program perform these operations efficiently. Let n be the number of rows in A , and let a and b denote the number of non-zero entries in A and B respectively. Then functions copy(), changeEntry(), scalarMult(), and transpose() should have cost (^ n^ a ) when applied to A , in worst case. Functions add() and sub() can be implemented to run in time (^ n^ a b ), and mult() should run in time ( n a b ). It will be helpful to include a private function with signature private static double dot(List P, List Q) that computes the vector dot product of two matrix rows represented by Lists P and Q. Use this function together with function transpose() to help implement mult(). Similar helper functions for the operations add() and sub() will also be useful. What to Turn In Your project will be structured in three files: Sparse.java, Matrix.java, and List.java. The main program, Sparse, will handle the input and output files and is the client of Matrix, which is itself the client of List.
Note that Sparse is not itself a direct client of List, since it need not call any List operations. You will also write separate client modules ListTest.java and MatrixTest.java to test the List and Matrix ADTs in isolation. Also submit a README and a Makefile that creates an executable jar file called Sparse. Thus seven files in all will be turned in: Sparse.java, Matrix.java, MatrixTest.java, List.java, ListTest.java, Makefile, and README. Submit these to the assignment name pa3 by the due date. As always start early and ask questions.