CSE 312 PS1
Problem 1 (1.1.1) What is an algorithm and what are its characteristics? Are algorithms language‐
specific? Explain the derivation of the term "algorithm".
An algorithm is a sequence of clear instructions for solving a specific type of problem. Algorithms are not
language‐specific. They can be implemented in any general purpose programming language. The term
comes from Al‐Khwarizmi, who was a Persian mathematician in the late 8th and early 9th century. Al‐
Khwarizmi proposed a set of rules for systematic solution of arithmetic problems.
Problem 2 Design an algorithm that searches for an array inside a larger array. Your algorithm should
return the starting index of the smaller array in the larger array, if exists. Otherwise, it should return ‐1.
For example, for the lists 13, 5, 12, 7, 24, 2 and 12, 7, 24, your algorithm should return 2.
Algorithm search(A[0...n‐1],B[0...m‐1])
i<‐0
while i<n‐m+1
j<‐0
while j<m AND A[i+j] = B[j]
j<‐j+1
if j = m
return i
i<‐i+1
return ‐1
Problem 3 (1.1.5) Design an algorithm to merge two sorted lists of numbers. For example, for the lists 2,
5, 7, 12, 13, 24 and 1, 6, 15, 17, 35, the output should be 1, 2, 5, 6, 7, 12, 13, 15, 17, 24, 35. What is the
maximum number of comparisons your algorithm makes if the lengths of the two given lists are n and
m, respectively?
Algorithm merge(A[0...n‐1],B[0...m‐1])
i<‐0, j<‐0, k<‐0
while i<n AND j<m
if A[i]<B[j]
C[k]<‐A[i]
i<‐i+1
else
C[k]<‐B[j]
j<‐j+1
k<‐k+1
while i<n
C[k]<‐A[i]
i <- i+1
k<‐k+1
while j<m
C[k]<‐B[j]
j <- j+1
k<‐k+1
In the worst case scenario, we take elements from A and B alternately. In that case, we will have made
n+m‐1 comparisons.
Problem 4 (1.1.6) Find gcd(31415,14142) by applying Euclid's algorithm. Estimate how many times
faster it will be to find this greatest common divisor compared with the algorithm based on checking
consecutive integers from min(n,m) down to gcd(n,m).
Euclid's algorithm states gcd(n,m) = gcd(m,n mod m). Therefore;
gcd(31415,14142) = gcd(14142,3131)
= gcd(3131,1618)
= gcd(1618,1513)
= gcd(1513,105)
= gcd(105,43)
= gcd(43,19)
= gcd(19,5)
= gcd(5,4)
= gcd(4,1)
= gcd(1,0)
=1
The greatest common divisor of 31415 and 14142 is 1. We made 10 divisions with Euclid's algorithm. If
we started from the smaller number and counted down to 1, we would have made 2x14142 divisions
(we would have to divide both numbers). Overall Euclid's algorithm is 28284/10 = 2828.4 times faster on
this example.
CSE 312 PS2
Problem 1 (2.1.2a) Consider the problem of finding the difference of 2 nxm matrices. Write a
pseudocode that solves this problem and do its complexity analysis.
Algorithm matrixDifference(A[0...n‐1,0...m‐1],B[0...n‐1,0...m‐1])
for i<‐ 0 to n‐1
for j<‐ 0 to m‐1
C[i,j]=A[i,j]‐B[i,j]
return C
n 1 m 1
n 1
n 1
i 0 j 0
i 0
i 0
1 m m1 mn
t n, m nm
Problem 2 (2.1.9) For each of the following pairs of functions, indicate whether the first function has
lower, same or higher order of growth than the second function. lower--> faster
a. nn 1 vs. 2000n 2
higher-->slower
same
b. 100n 2 vs. 0.01n3
lower
c. log 2 n vs. ln n
same since log x n
log n
log x
d. log 22 n vs. log 2 n 2
higher since log 2 n 2 2 log 2 n
e. 2 n 1 vs. 2n
same since 2n 1
2n
2
f. n 1! vs. n!
higher
lower since n! nn 1!
Problem 3 (2.2.7) Prove the following assertions by using the definitions of the notations involved, or
disprove them by giving specific counter example.
a. If t n O g n , then g n t n
If t n O g n , c0 , n0 | t n c0 g n n n0
Then, c1 , n0 | c1t n g n n n0 where c1 1 c0 (note that both c0 and c1 are positive constant)
Therefore, g n t n
b. g n g n where 0
If f n g n , c0 , c1 , n0 | c0g n t n c1g n n n0
Then, c2 , c3 , n0 | c2 g n t n c3 g n n n0 where c2 c0 and c3 c1 . Note that c2 and c3
are positive and constant.
Therefore, f n g n
Problem 4 (2.3.6) Consider the following algorithm:
Algorithm enigma (A[0...n‐1,0...n‐1])
for i<‐0 to n‐2
for j<‐i+1 to n‐1
if A[i,j]!=A[j,i]
return false
return true
a. What does this algorithm compute?
This algorithm checks if the input matrix is symmetric or not.
b. What is its basic operation?
The comparison, A[i,j]!=A[j,i].
c. How many times is the basic operation executed?
n 2 n 1
n2
i 0 j i 1
i 0
1 n i 1 n 1 n 2 ... 1
nn 1
2
This is the exact number of iterations in the worst case scenario (symmetric matrix).
d. What is the efficiency class of this algorithm? n 2