This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Qatar University
College of Engineering
Department of Computer Science and Engineering Course: CMPS 323 Design and Analysis of Algorithms Instructor: Dr. Khaled Bashir Shaban Assignment #1 (with Some Answers) 1. Order the following functions by their growth rate:
N, W, N”), N2, N log N, N log log N, N log2 N, N10g(N2), 2/N, 2N, 2””, 37, N21ogN, N3.
Answer: 2/N < 37<¢N<N<NioglogN<NiogNgNiog(N2)
< Niog2N<NL5<N2<N2iogN<N3<2Ni2<2N 2. The following is a pseudocode of a bruteforce algorithm for computing the value of a polynomial
(70¢) 2 an x" + anI X”, + +611 x + do at a given point x. Algorithm Bmtefb reePolynomialEmluetion(P [0. .11], at)
//The algorithm computes the value of polynomial P at a given point a:
[/lnput: Array P[..n] of the coefﬁcients of a polynomial of degree n,
// stored from the lowest to the highest and a number a:
/ / Output: The value of the polynomial at the point :1:
p <— 0.0
for i <— n downto U do power 4— 1 for j <— 1 to 2' do power (— power * a? p <— p + PM =~< power return p a. determine the worst—case efﬁciency class of the algorithm b. Redesign (rewrite) the algorithm so that you have a more efﬁcient solution for this
problem. Algorithm BetteerteForcePolynoméalEmluatéoMPW..11], :33) //The algorithm computes the value of polynomial F at a given point a:
//by the “lowesttohighest term” algorithm //lnput: Array P[0..n.] of the coefﬁcients of a polynomial of degree n,
// from the lowest to the highest, and a number :1: / / Output: The value of the polynomial at the point :1:
p <— P[U]; pmne'r <— 1
for i <— 1 to 11 do
pmner <— power * a:
p <— p + PM =k pmne'r
return p The number of multiplications here is 3. Consider the following algorithm. Algorithm Mystery(n)
//Input: A nonnegative integer n
S <— 0
for i <— 1 to 71 do
8' <— S + i=l= i
return 5
What does this algorithm compute?
What is its basic operation?
How many times is the basic operation executed? What is the efﬁciency class of this algorithm? 9999‘s Suggest an improvement or a better algorithm altogether and indicate its efﬁciency class. 4. Consider a variation of sequential search that scans a list to return the number of occurrences of a
given search key in the list.
a. Write a pseudo code for this algorithm.
b. Will the efﬁciency of this algorithm compared to the classical sequential search differ?
Explain. 5. Indicate whether the ﬁrst function of each of the following pairs has a smaller, same, or larger order
of growth than the second function. a. n(n + 1) and 2000112 b. 1001?,2 and 0.01m3 c. log2 n and 1n n d. logg n and log2 n2 e. 2“’1 and 2” f. (n — 1)! and n! a. 11(11 + 1) a n2 has the same order of growth (quadratic) as 2000112 to
within a constant multiple. b. 100112 (quadratic) has a lower order of growth than 0.01113 (cubic).
c. Since changing a logarithm’s base can be done by the formula
loga n = log£1 blogb n, all logarithmic functions have the same order of growth to within a con
stant multiple. d. log: n = log2 nlog2 n. and logg n2 = 2 log 11. Hence log: n has a higher
order of growth than log2 112. e. 271—1 2 %2“ has the same order of growth as 2“ to within a con
stant multiple. f. (n. — 1)! has a lower order of growth than n! = (n — 1)!n. 6. Use the formal deﬁnitions of O, (9, and Q to determine whether the following assertions are true or
false. a. 71.01 + 1)/2 E O(n3) b. n(n + 1)/2 6 0(7)?)
c. 71(7), + 1)/2 E @(n3) (1. n(n + 1)/2 6 9(7)) 7. For each of the following functions, indicate the class (9002)) the function belongs to. Prove your
Answers (using deﬁnitions or limits). a. (n2 + 1)10 b. V1071? + 771+ 3 c. 2n1g(n + 2)2 + (71+ 2)21g % d. 2n+1 + 311—1 ' 1:  1: . 10
a. lim (“if—2:“: lirn ("ff—1V: lim (“31) == lim (1+?5)1°=1. { 2)1f:
n—roo n—roo n—roo Hence (n2 + 1)10 E 8090). b. “In 1031 +711+3 = lim ,‘1 10112“ 771 3— _ hm 10+_ n+ §2__ _‘/ﬁ'
Hence x/lfm2 + 7n + 3 E 801). C. 2nlg(n + 2)2 + (n + 2)21g % = 2n21g(n + 2) + (n + 2)2(1gn — 1) E
9(n lg n) + 8(n2 lg n) = @032 lg n). d. 2n+1 + 3n1 = 2H2 + 3% e 9(2“) + 9(3“) = 99“) ...
View
Full Document
 Spring '11
 Shaban

Click to edit the document details