Uploaded by jannaarix

Homework 1 (2)

advertisement
Georgia State University
CSC 4520/6520
HW 1
Please submit your work typed and in pdf format by 11:59 pm September 24th
First Name: ---------------
Last Name: ----------------------
Problem 1. (10 points)
For each of the following algorithms, indicate (i) a natural size metric for its inputs, (ii) its basic
operation, and (iii) whether the basic operation count can be different for inputs of the same size:
1.
2.
3.
4.
Computing the sum of n numbers
Computing n!
Finding the largest element in a list of n numbers
Sieve of Eratosthenes
Problem 2. (10 points)
Consider a variation of sequential search that scans a list to return the number of occurrences of a
given search key in the list. Does its efficiency differ from the efficiency of classic sequential
search?
Problem 3. (10 points)
For each of the following functions, indicate how much the function’s value will change if its
argument is increased fourfold.
a) √𝑛 b) n c) n2 d) n3 e) 2n
Problem 4. (20 points)
List the following functions according to their order of growth from the lowest to the highest:
(n−2)!, 5log(n+100)10,
22n,
0.001n4 + 3n3 +1, ln2 n,
!
√𝑛 ,
3n.
1
Problem 5. (25 points)
Consider the following algorithm.
ALGORITHM NoName(A[0..n − 1])
//Input: An array A[0..n − 1] of n real numbers
minval ← A[0]; maxval ← A[0]
for i ← 1 to n − 1 do
if A[i] < minval
minval ← A[i]
if A[i] > maxval
maxval ← A[i]
return maxval – minval
a) What does this algorithm compute?
b) What is its basic operation?
c) How many times is the basic operation executed?
d) What is the efficiency class of this algorithm?
Problem 6. (25 points)
Consider the following recursive algorithm.
ALGORITHM Riddle(A[0..n − 1])
//Input: An array A[0..n − 1] of real numbers
if n = 1 return A[0]
else temp ← Riddle(A[0..n − 2])
if temp ≤ A[n − 1] return temp else return A[n − 1]
a) What does this algorithm compute?
b) Set up a recurrence relation for the algorithm’s basic operation count and solve it.
2
Download