CS计算机代考程序代写 algorithm flex CS 124 Programming Assignment 2: Spring 2021

CS 124 Programming Assignment 2: Spring 2021
Your name(s) (up to two):
Collaborators: (You shouldn’t have any collaborators but the up-to-two of you, but tell us if you
did.)
No. of late days used on previous psets: No. of late days used after including this pset:
Homework is due Wednesday 2021-03-24 at 11:59pm ET. You are allowed up to twelve (college)/forty (extension school) late days through the semester, but the number of late days you take on each assignment must be a nonnegative integer at most two (college)/four (extension school).
Overview:
Strassen’s divide and conquer matrix multiplication algorithm for n by n matrices is asymptotically faster than the conventional O(n3) algorithm. This means that for sufficiently large values of n, Strassen’s algorithm will run faster than the conventional algorithm. For small values of n, however, the conventional algorithm may be faster. Indeed, the textbook Algorithms in C (1990 edition) suggests that n would have to be in the thousands before offering an improvement to standard multiplication, and “Thus the algorithm is a thoeretical, not practical, contribution.” Here we test this armchair analysis.
Here is a key point, though (for any recursive algorithm!). Since Strassen’s algorithm is a recursive algorithm, at some point in the recursion, once the matrices are small enough, we may want to switch from recursively calling Strassen’s algorithm and just do a conventional matrix multiplication. That is, the proper way to do Strassen’s algorithm is to not recurse all the way down to a “base case” of a 1 by 1 matrix, but to switch earlier and use conventional matrix multiplication. That is, there’s no reason to do a “base case” of a 1 by 1 matrix; it might be faster to use a larger-sized base case, as conventional matrix multiplication might be faster up to some reasonable size. Let us define the cross-over point between the two algorithms to be the value of n for which we want to stop using Strassen’s algorithm and switch to conventional matrix multiplication. The goal of this assignment is to implement the conventional algorithm and Strassen’s algorithm and to determine their cross-over point, both analytically and experimentally. One important factor our simple analysis will not take into account is memory management, which may significantly affect the speed of your implementation.
Tasks:
1. Assume that the cost of any single arithmetic operation (adding, subtracting, multiplying, or dividing two real numbers) is 1, and that all other operations are free. Consider the following variant of Strassen’s algorithm: to multiply two n by n matrices, start using Strassen’s algorithm, but stop the recursion at some size n0, and use the conventional algorithm below that point. You have to find a suitable value for n0 – the cross-over point. Analytically determine the value of n0 that optimizes the running time of this algorithm in this model. (That is, solve the appropriate equations, somehow, numerically.) This gives a crude estimate for the cross-over point between Strassen’s algorithm and the standard matrix multiplication algorithm.
2. Implement your variant of Strassen’s algorithm and the standard matrix multiplication algorithm to find the cross-over point experimentally. Experimentally optimize for n0 and compare the experimen- tal results with your estimate from above. Make both implementations as efficient as possible. The actual cross-over point, which you would like to make as small as possible, will depend on how effi-
1

ciently you implement Strassen’s algorithm. Your implementation should work for any size matrices, not just those whose dimensions are a power of 2.
To test your algorithm, you might try matrices where each entry is randomly selected to be 0 or 1; similarly, you might try matrices where each entry is randomly selected to be 0, 1 or 2, or instead 0, 1, or −1. We will test on integer matrices, possibly of this form. (You may assume integer inputs.) You need not try all of these, but do test your algorithm adequately.
3. Triangle in random graphs: Recall that you can represent the adjacency matrix of a graph by a matrix A. Consider an undirected graph. It turns out that A3 can be used to determine the number of triangles in a graph. To see this, you can see that (ij)th entry in the matrix A2 counts the paths from i to j of lenth two, and the (ij)th entry in the matrix A3 counts the path from i to j of length 3. To count the number of triangles in in graph, we can simply add the entries in the diagonal, and divide by 6. This is because the jth diagonal entry counts the number of paths of length 3 from j to j. Each such path is a triangle, and each triangle is counted 6 times (for each of the vertices in the triangle, it is counted once in each direction).
Create a random graph on 1024 vertices where each edge is included with probability p for each of the following values of p: p = 0.01, 0.02, 0.03, 0.04, and 0.05. Use your (Strassen’s) matrix multiplication code to count the number of triangles in each of these graphs, and compare it to the expected number of trianles, which is 􏲋1024􏲌p3. Create a chart showing your results compared to the epectation.
3
Code setup:
So that we may test your code ourselves as necessary, please make sure your code compiles and runs as follows:
$ make
$ ./strassen 0 dimension inputfile
The flag 0 is meant to provide you some flexibility; you may use other values for your own testing, debugging, or extensions. The dimension, which we refer to henceforth as d, is the dimension of the matrix you are multiplying, so that 32 means you are multiplying two 32 by 32 matrices together. The inputfile is an ASCII file with 2d2 integer numbers, one per line, representing two matrices A and B; you are to find the product AB = C. The first integer number is matrix entry a0,0, followed by a0,1, a0,2, . . . , a0,d−1; next comes a1,0,a1,1, and so on, for the first d2 numbers. The next d2 numbers are similar for matrix B.
Your program should put on standard output (in C: printf, cout, System.out, etc.) a list of the values of the diagonal entries c0,0,c1,1,…,cd−1,d−1, one per line, including a trailing newline. The output will be checked by a script – add no clutter. (You should not output the whole matrix, although of course all entries should be computed.)
The inputs we present will be small integers, but you should make sure your matrix multiplication can deal with results that are up to 32 bits.
Your program will be compiled, run, and tested by a script, so make sure it actually works as speci- fied: ideally, you should be able to type “make”, then “./strassen 0 ” for some dimension and input file, and have it work.
Do not turn in an executable.
What to hand in:
2

As before, you may work in pairs, or by yourself. Hand in a project report (on paper) describing your analytical and experimental work (for example, carefully describe optimizations you made in your implementations). Be sure to discuss the results you obtain, and try to give explanations for what you observe. How low was your cross-over point? What difficulties arose? What types of matrices did you multiply, and does this choice matter?
Your grade will be based primarily on the correctness of your program, the crossover point you find, your interpretation of the data, and your discussion of the experiment.
Hints:
It is hard to make the conventional algorithm inefficient; however, you may get better caching perfor- mance by looping through the variables in the right order (really, try it!). For Strassen’s algorithm:
• Avoid excessive memory allocation and deallocation. This requires some thinking.
• Avoid copying large blocks of data unnecessarily. This requires some thinking.
• Your implementation of Strassen’s algorithm should work even when n is odd! This requires some additional work, and thinking. (One option is to pad with 0’s; how can this be done most effectively?) However, you may want to first get it to work when n is a power of 2 – this will get you most of the credit – and then refine it to work for more general values of n.
3