Docsity

Computer Science II - Programming Assignment 1 | COP 3503, Assignments of Computer Science

Toggle sidebar
bg1
Title: “Program Assignment 1: Analyzing Program Performance”
Points: 100 points – 60pts for coded portion, 40 points for written analysis
Due Date: Wednesday September 25, 2002
Objectives: (1) To reinforce the concepts underlying the asymptotic analysis of a program’s
performance behavior. (2) Begin to program in Java.
Description: Create a Java program that produces the necessary data to determine the asymptotic
behavior of three different algorithms that perform operations on matrices. Do this according to the
steps shown below.
1. Create 2 randomly filled matrices (see below for the details). You will need to experiment
with the sizes of the two matrices so that you get non-zero execution times – see below.
Start with 25 x 25 square matrices and work upwards until each algorithm gives a non-zero
execution time for a given size of the matrices.
2. Determine the execution time required to run each of the algorithms given below on 5
different size matrices where each size gives a non-zero execution time. Thus you will
generate at total of fifteen different timing values, five for each algorithm. Note: while your
program must correctly compute the values for these three matrix operations, you do not
need to include this in your output, although you should verify that the algorithms are
executing properly. (See below for more details on matrix operations.)
Algorithm #1 – Matrix Addition
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
A[i][j] = B[i][j] + C[i][j];
Algorithm #2 – Matrix Multiplication
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
for (k = A[i][j] = 0; k < n; k++)
A[i][j] += B[i][k] * C[k][j];
Algorithm #3 – Matrix Transposition
for (i = 0; i < n-1; i++)
for (j = i+1; j < n; j++){
temp = A[i][j];
A[i][j] = A[j][i];
A[j][i] = temp;
}
3. Part 2 will produce 5 different time values for each of the three algorithms. Use these values
as the empirical data to calculate T(N)/F(N) values for the algorithms to determine if the
algorithms are O(N), O(N2), or O(N3). Put these calculations and their results in the write-up
Page 1
COP 3503H – Programming Assignment #1 – Fall 2002
pf3
pf4
pf5

Partial preview of the text

Download Computer Science II - Programming Assignment 1 | COP 3503 and more Assignments Computer Science in PDF only on Docsity!

Title: “Program Assignment 1: Analyzing Program Performance” Points: 100 points – 60pts for coded portion, 40 points for written analysis Due Date: Wednesday September 25, 2002 Objectives: (1) To reinforce the concepts underlying the asymptotic analysis of a program’s performance behavior. (2) Begin to program in Java. Description: Create a Java program that produces the necessary data to determine the asymptotic behavior of three different algorithms that perform operations on matrices. Do this according to the steps shown below.

  1. Create 2 randomly filled matrices (see below for the details). You will need to experiment with the sizes of the two matrices so that you get non-zero execution times – see below. Start with 25 x 25 square matrices and work upwards until each algorithm gives a non-zero execution time for a given size of the matrices.
  2. Determine the execution time required to run each of the algorithms given below on 5 different size matrices where each size gives a non-zero execution time. Thus you will generate at total of fifteen different timing values, five for each algorithm. Note: while your program must correctly compute the values for these three matrix operations, you do not need to include this in your output, although you should verify that the algorithms are executing properly. (See below for more details on matrix operations.) Algorithm #1 – Matrix Addition for (i = 0; i < n; i++) for (j = 0; j < n; j++) A[i][j] = B[i][j] + C[i][j]; Algorithm #2 – Matrix Multiplication for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = A[i][j] = 0; k < n; k++) A[i][j] += B[i][k] * C[k][j]; Algorithm #3 – Matrix Transposition for (i = 0; i < n-1; i++) for (j = i+1; j < n; j++){ temp = A[i][j]; A[i][j] = A[j][i]; A[j][i] = temp; }
  3. Part 2 will produce 5 different time values for each of the three algorithms. Use these values as the empirical data to calculate T(N)/F(N) values for the algorithms to determine if the algorithms are O(N), O(N^2 ), or O(N^3 ). Put these calculations and their results in the write-up

COP 3503H – Programming Assignment #1 – Fall 2002

portion of your assignment. How accurate are your numbers? Did you get what you expected? References: Notes: Lecture Notes for Days 2, 3 & 4 Restrictions: Your source file shall begin with comments containing the following information: / Name: Course: Assignment title: Date: / Input Specification: Internal to the source code. Output Specification: Timing information and any other output produced by your program can be written to the screen for this program. All other information should be included in your write-up. Deliverables: (1) Source code file on a floppy disk with the following information: Your name, COP 3503H, Assignment title, and Date. (2) The write-up for this assignment described in other parts of this document. (3) Place hard copies of (1) and (2) along with your disk in a large envelope, so that you do not need to fold the printouts or your report. CLEARLY, label the envelope with the following information: your name, your id number, COP 3503H, Assignment title, and the date. Write-up: A complete write-up is one of the deliverables for this assignment. A separate document (see the course web-site) describes the make-up of this write-up. Along with the items described in that separate document you will need to include the following in your report:

  1. For each algorithm, determine the asymptotic running time, in terms of Big-Oh notation, of the algorithm. Look at the examples in the lecture notes to see how this is done and follow that format in your write-up.
  2. For each algorithm, can you think of a different technique that would solve the same problem, yet result in an asymptotic running time less than the algorithm that you actually executed. Don’t code this, just comment on it. Additional Information: Shown below are two segments of code that will be helpful in constructing your programs.

Example: Martix B 2 3 2 1 4 6 3 2 2 Matrix C 4 3 2 1 3 3 2 2 4 Matrix C = A[n,m] + B[n,m] 2 + 4 = 6 3 + 3 = 6 2 + 2 = 4 1 + 1 = 2 4 + 3 = 7 6 + 3 = 9 3 + 2 = 5 2 + 2 = 4 2 + 4 = 6 Algorithm #2 – Matrix Multiplication for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = A[i][j] = 0; k < n; k++) A[i][j] += B[i][k] * C[k][j]; In general: matrix multiplication of two matrices B[n,m] and C[m,r] is defined as: ^  ^                    (b[n, 1 ] c[ 1 , 1 ] b[n, 2 ] c[ 2 , 1 ] ... b[n, m (b[ 2 , 1 ] c[ 1 , 1 ] b[ 2 , 2 ] c[ 2 , 1 ] ... b[ 2 , m (b[ 1 , 1 ] c[ 1 , 1 ] b[ 1 , 2 ] c[ 2 , 1 ] ... b[ 1 ,m ] A[ i, j ]  (Note that the dimensions of A are n  r, in the general case, since m = m) Since we are dealing with only square matrices n = r. Example: Matrix B 2 3 2 1 4 6

Matrix C 4 3 2 1 3 3

Matrix A = B  C (24 + 31 + 22) (23 + 33 + 22) (22 + 33 + 24) (14 + 41 + 62) (13 + 43 + 62) (12 + 43 + 64) (34 + 21 + 22) (33 + 23 + 22) (32 + 23 + 2*4) Thus Matrix A is: 15 19 21 20 27 38 18 19 20 Algorithm #3 – Matrix Transposition for (i = 0; i < n-1; i++) for (j = i+1; j < n; j++){ temp = A[i][j]; A[i][j] = A[j][i]; A[j][i] = temp; } In general, the transpose of a matrix is the matrix that results from the interchanging of its rows and columns. Given a matrix B, its transpose is generally denoted as BT. Example: Matrix B 2 3 2