In: Computer Science
Question 3.[10 pt]
As indicated in the course book and discussed in the class extensively, there is
generally an efficiency trade-off among execution time, memory usage and communication
overhead for a given algorithm. Please give at least two examples where such a trade-off occurs.
For example, can you give an algorithm example wherein more storage permits faster execution,
or vice versa? Similarly, can you exemplify another algorithm wherein there are trade-offs
among communication and/or memory and/or execution time?
Algorithm Trade-off Example Brief Discussion 1 [5pt]:
Write a paragraph here by explaining the
name of the algorithm and how the performance trade-off.
Algorithm Trade-off Example Brief Discussion 2 [5pt]:
Write a paragraph here by explaining the
5
name of the algorithm and how the performance trade-off occurs.
Question 4. [30 pt]
In this exercise, we start getting familiar withtwo-dimensional arrays, i.e., a
matrix (remember that matrices are nothing but 2D arrays).Matrices are one of the most
fundamental data structures, and they are used in every aspect of Information Technology
including but not limited to AI, cyber-security and software engineering. The most basic
definition of a matrix is here:
https://www.mathsisfun.com/algebra/matrix-introduction.html
Specifically, as an example was given in the above link for 2x2 matrix, you will perform the
most basic arithmetic operation on a matrix,
adding two matrices entry-wise
. Please see the link
below and read
“Entrywise Sum”.
Each corresponding cell in a matrix is added and put into 3
rd
target matrix.
https://en.wikipedia.org/wiki/Matrix_addition
Write a program named “
hw2.py
” that will add two matrices entry-wise. The matrix dimension
is fixed and 5x5 for this exercise. Your program should print an error if the dimensions of the
two matrices are different. In this case, print a message stating that the matrix dimensions does
not match. There are two matrices to be added. One is shown in “hw2-m1.txt”
5 2 0 3 4
2 4 1 1 0
3 0 5 6 1
0 2 6 0 1
4 1 1 0 0
6
The other one is shown in “hw2-m2.txt”
2 0 4 0 1
2 2 1 0 5
0 0 1 0 3
2 1 0 4 1
0 0 4 0 1
Read these two matrices from “.txt” files, and use your own codes to add them,
donot use built-
in functions
. Finally, you should print the result on the screen as
The result is:
7 2 4 3 5
4 6 2 1 5
3 0 6 6 4
2 3 6 4 2
4 1 5 0 1
If you have any doubts, please give me comment...
mat1 = []
fp = open("hw2-m1.txt")
for line in fp.readlines():
mat1.append([int(x) for x in line.split()])
fp.close()
mat2 = []
fp = open("hw2-m2.txt")
for line in fp.readlines():
mat2.append([int(x) for x in line.split()])
fp.close()
isValid = True
if len(mat1)==len(mat2):
for row in range(len(mat1)):
if len(mat1[row])!=len(mat2[row]):
isValid = False
break
if not isValid:
print("the matrix dimensions does not match")
else:
result = []
for row in range(len(mat1)):
result.append([mat1[row][col]+mat2[row][col] for col in range(len(mat1[row]))])
print("The result is:")
for row in result:
for col in row:
print(col, end=' ')
print()