CS 141 Programming Assignment #1 Set: Jan. 23, Wednesday, 2008 Due: Feb. 6, Wednesday, 11:59am, 2008 Problem description: An m x n matrix is a Monge matrix if for all indices i, j, k, and r such that 1 <= i < k <=m and 1 <= j < r <=n, we have A[i,j] + A[k,r] <= A[i,r] + A[k,j]. In other words, whenever we pick 2 rows and 2 columns of a Monge matrix and consider the four elements at the intersections of the rows and the columns, the sum of the upper-left and lower-right elements is less or equal to the sum of the lower-left and upper-right elements. A description of the problem can also be found in CLRS. For example, the following matrix is Monge: 10 17 13 28 23 17 22 16 29 23 24 28 22 34 24 11 13 6 17 7 45 44 32 37 23 36 33 19 21 6 75 66 51 53 34 But the next one is not (why?): 37 23 22 32 21 6 7 10 53 34 30 31 32 13 9 6 43 21 15 8 The goal of this assignment is to have you implement two algorithms concerning Monge matrices as follows. 1. It is known that a necessary and sufficient condition for Monge matrices is: A[i, j] + A[i+1, j+1] <= A[i, j+1] + A[i+1, j] for all i = 1,2,...,m-1 and j = 1,2,...,n-1. Write a C++ program to check if an m x n input matrix is Monge in O(mn) time. 2. Let f(i) denote the index of the column containing the leftmost minimum element in row i of an m x n Monge matrix. Then it is easy to prove that f(1) <= f(2) <= ... <= f(m). Based on this property, (i) write a (straightforward) program to find the minimum elements (not their indices) in each row of an m x n Monge matrix in O(mn) time by finding the minimum elements row by row, and (ii) write a faster program to find the minimum elements in each row of an m x n Monge matrix A based on the following divide-and-conquer algorithm: - Construct a submatrix A' of A consisting of the even-numbered rows of A. Note that, A' is also Monge. - Recursively determine the leftmost minimum in each row of A'. - Then compute the leftmost minimum in the odd-numbered rows of A. Hint for the third step of algorithm (ii): Make a left to right scan, and search for the leftmost minimum elements for the odd-numbered rows in the intervals [1, f(2)], [f(2), f(4)], ..., [f(m-2), f(m)] if m is even or [f(m-1), n] if m is odd. Note: The leftmost minimum is just the minimum when there is no duplication of numbers within the row. If there are duplications, e.g. in 3 7 1 13 9 1 2 5, the leftmost "1" will be considered the leftmost minimum. Detailed specifications of the (C++) program: A) The program will receive the input from a text file in the following format: m n x1 x2 x3 x4 ... y1 y2 y3 y4 ... ... z1 z2 z3 z4 ... where m and n are positive integers indicating the size of the m x n matrix. The rest of the numbers in the matrix are real numbers. A single space separates numbers and linebreaks separate rows. You may assume that there is only 1 matrix in each file. A sample input file is below. However, your program should work on any file in the format given above. 3 5 -2.4 22 16.1 29.7 23.4 45.1 44.3 32.2 37 23.5 80.99 66.6 51.1 53.123 34.2 B) The program should print the result to an output file whose name can be prescribed at the command line. In other words, your program should run on the command line as follows: $ as1 where is the actual name of the input file, the will be the name of the output file, and as1 will be the name of the executable file of your program. C) The output file should give the following information for each matrix: 1) The answer for the first part, i.e. "Yes" if the matrix is Monge and "No" if it is not. 2) If the answer is "No", your program should end here. Otherwise, give the minimum elements for each row. 3) In the latter case, you should also output the numbers of comparisons of matrix elements performed by both of your algorithms for finding the minimum elements (i.e. the straightforward and faster algorithms). These numbers of comparisons reflect the running times of the algorithms. For example, a sample output for the above matrix could be: Yes -2.4 23.5 34.2 straightfoward algorithm used ?? comparisons faster algorithm used ?? comparisons D) Run your program on the test matrices provided along with this assignment. How do the running times (measured in terms of the number of comparisons) of the two algorithms for finding the minimum elements compare? Summarize your results on the test matrices briefly in a file called README, which you should turn in with your program. Note that, we may also test your program on other matrices. Other important requirements: 1. Your program should be executable under LINUX environment. Make sure you use a 'makefile' to compile and link your C++ code, and all C++ source code files are '*.cc' or '*.h' files. 2. The executable name of your program MUST be 'as1'. 3. Your program should have separate files for the main program, prototypes of your functions, classes and their definitions. 4. You should turn in the following files: C++ source files, makefile, and README file. 5. The README file should explain how your program works and other details such as a sketch of the algorithms and data structures, as well as a summary of the results (i.e. comparison of the two algorithms) on the provided test data. 6. Do not forget to provide a makefile so that the reader only needs to type 'make' at the command line to compile your program. WITH NO FUNCTIONAL MAKEFILE, YOU WILL GET ZERO POINT. 7. Include your name, UCR ID, login, lab section, and the assignment number in each and EVERY file. 8. The readability of your program also counts. Use a proper style and add comments where appropriate. Outline of grading scheme: * Soundness, efficiency, and style of the program : 50 points * Correct output : 40 points * README file : 10 points ====================================================================== Total : 100 points