Assignment 1 with Some Answers

Assignment 1 with Some Answers - Qatar University College...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the 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 brute-force algorithm for computing the value of a polynomial (70¢) 2 an x" + an-I 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 coefficients 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 efficiency class of the algorithm b. Re-design (re-write) the algorithm so that you have a more efficient 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 “lowest-to-highest term” algorithm //lnput: Array P[0..n.] of the coefficients 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 efficiency class of this algorithm? 9999‘s Suggest an improvement or a better algorithm altogether and indicate its efficiency 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 efficiency of this algorithm compared to the classical sequential search differ? Explain. 5. Indicate whether the first 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 definitions 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 definitions 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__ _‘/fi' 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 + 3n-1 = 2H2 + 3% e 9(2“) + 9(3“) = 99“)- ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern