The purpose of this assignment is to practice your analysis of algorithm complexity. It is recommended that this assignment be printed, and that the work be done right on it. This assignment is to be submitted in paper form to the assignment boxes, just to the left of the elevators, on the second floor of the Goldberg CS Building. For each of the algorithms below:
Derive the cost function for the algorithm. Be sure to show your work like we do in lectures.
State the complexity of the algorithm in Big-Oh.
Prove that the derived cost function is in the stated order (big-Oh).
Problem 0: Reverse
1 2 3 4 5 6 7 8 9
input: vals[n] output: vals reversed
for i = 0 ... n/2 t = vals[i] vals[i] = vals[n-i-1] vals[n-i-1] = t
return vals
Problem 1: Multiplication
1 2 3 4 5 6 7 8 9 10 11
input: A[n][n] and B[n][n] // 2D arrays of size n x n output: C[n][n]
C = array[n][n] // Assume O(1) cost to create 2D array
for i = 0 ... n for j = 0 ... n C[i][j] = 0 for k = 0 ... n C[i][j] = C[i][j] + A[i][k] x B[k][j] return C
input: X[n], Y[n] output: length of longest common sequence between X and Y
C = Array[n][n] for i = 0 ... n C[i,0] = 0 C[0,i] = 0
for i = 1 ... n for j = 1 ... n if X[i] == Y[j] C[i,j] = C[i-1,j-1] + 1 else if C[i,j-1] < C[i-1,j] C[i,j] = C[i-1,j] else C[i,j] = C[i,j-1] return C[n,n]
Problem 5: Sorting, again
1 2 3 4 5 6 7 8 9 10 11
input: vals[n] output: sorted vals[n]
for i = 1 ... n for j = i ... 1 if vals[i] >= vals[i-1] break v = vals[i] vals[i] = vals[i-1] vals[i-1] = v return vals
Problem 6: Factorization
1 2 3 4 5 6 7 8 9 10 11 12
input: n-bit integer X // see not below the algorithm output: list of prime factors and their multiplicity
acc = X for i = 2 ... sqrt( X ) count = 0 while acc % i == 0 acc = acc / i count = count + 1
if count > 0 print i (count)
Note: Recall that the range of values of an n-bit integer is 0 2^n.
Problem 7: Pivot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
input: vals[n] output: vals divided by a pivot at val[0]
low = 1 high = n - 1
while low <= high if vals[0] <= vals[high] t = vals[high] vals[high] = vals[low] vals[low] = t low = low + 1 else high = high - 1
function sort( V[n] ): if n > 2 m=n/2 sort( V[0:m] ) sort( V[m,n] )
A = array[n] // assume 1 operation to create array i=0 j=m k=0 while i < m and j < n if V[i] [<= V[j] A[k] = V[i] i=i+1 else A[k] = V[j] j=j+1 k=k+1
if j < n i=j
for l = k ... n A[l] = V[i] i=i+1
for l = 0 ... n V[l] = A[l] return V
Hints and Suggestions
Print this assignment and show your work right on it.
Feel free to use O(1) instead of specific constants when analyzing the algorithms.
Be sure to show your work!
Grading
Each problem will be graded out of 10 points:
4 marks for deriving the correct cost function. The cost function should be of the correct order, and it should be clear how it was derived from the algorithm. One mark for the correct answer, three marks for showing your work.
2 marks for identifying the correct order of the function (big Oh). One mark for the correct order, and one mark for correct notation. I.e., no multiplicative constants, or multiple terms.
4 marks for justifying your answer. Follow the steps discussed in lecture. Approximately, 1 mark per step.
What to Hand In
Submit you assignment in hardcopy (paper form) to the submission box, which is located to the left of the elevators, on the second floor of the CS Goldberg Building.