CS277 Homework 1 - Consider the following polynomial

Question # 00856621 Posted By: wildcraft Updated on: 06/26/2024 02:17 AM Due on: 06/26/2024
Subject Computer Science Topic General Computer Science Tutorials:
Question
Dot Image

Algorithms And Analysis

CS277 - Homework 1

Total points: 85

1. Consider the following polynomial with n terms, where ai is the ith coefficient:

P (x) = n−1∑ i=0

aix i = a0 + a1x+ a2x

2 + ...+ an−1x n−1

Below is pseudocode of a naive algorithm that calculates P (x) for some input x:

1 Function pow(x, i): 2 // x is a number, i is an integer, returns xi

3 exp := 1 4 p := 1 5 while p ≤ i do 6 exp = exp ∗ x 7 p += 1

8 return exp

9 Function calculatePoly1(x, a, n): 10 // x is a number, a is a list of numbers, n is the length of a 11 sum := 0 12 i := 0 13 while i < n do 14 sum += a[i] * pow(x, i) 15 i += 1

16 return sum

(a) (4 points) Prove a tight bound for the runtime complexity of calculatePoly1 using sum- mations.

(b) (4 points) Give a tight bound for the space complexity of calculatePoly1. Explain your bound.

Assume that all integers and floats require the same, constant amount of memory.

Note: Unless otherwise specified, we ignore the space required to store the input argu- ments, since this will remain the same for all algorithms that solve the same problem.

(c) (10 points) Propose an algorithm calculatePoly2 with asymptotically better runtime compared to calculatePoly1 and provide its pseudocode.

Give tight bounds for your algorithm’s runtime and space complexity. Explain your bounds.

1

Your answer may not use a built-in exponentiation operator (like xˆn or x**n). You may use other basic operators like addition, multiplication, etc.

2. For each of the following questions, briefly explain your answer.

(a) (4 points) If an algorithm’s best case runtime is Θ(n), is it possible that it takes Θ(n2) time on some inputs?

(b) (4 points) If an algorithm’s best case runtime is Θ(n), is it possible that it takes Θ(n2) time on all inputs?

(c) (4 points) If an algorithm’s worst case runtime is O(n2), is it possible that it takes o(n2) time on some inputs?

(d) (4 points) If an algorithm’s worst case runtime is O(n2), is it possible that it takes o(n2) time on all inputs?

3. (6 points) Prove the following bound using the inequality definition of O: 18n lg(n) + 2n2 = O(n2)

4. (10 points) Consider all exponential functions of the form f(n) = kn, where k is a positive constant.

Do there exist two exponential functions of this form with different bases (e.g. f(n) = 2n and g(n) = 3n) such that their growth rates are asymptotically equivalent?

Prove your answer.

5. (10 points) Prove the property of transitivity for O: For any two asymptotically non-negative functions f and g, if f(n) = O(g(n)) and g(n) = O(h(n)), then f(n) = O(h(n)).

6. (25 points) Order the following functions from least to greatest growth rate, such that for any two functions f and g, if f comes before g, then f(n) = O(g(n)). You must prove each ranking.

For example, if the functions were f, g, h and you gave the order g, h, f , then you would need to prove that g(n) = O(h(n)), g(n) = O(f(n)), and h(n) = O(f(n)).

• f1(n) = √ n

• f2(n) = nn

• f3(n) = n0.00001

• f4(n) = n!

• f5(n) = lg(n)

Hint: Reusing results previously proved in this assignment may save you a lot of work.

Page 2

Dot Image
Tutorials for this Question
  1. Tutorial # 00852110 Posted By: wildcraft Posted on: 06/26/2024 02:18 AM
    Puchased By: 2
    Tutorial Preview
    The solution of CS277 Homework 1 - Consider the following polynomial...
    Attachments
    CS277_Homework_1_-_Consider_the_following_polynomial.ZIP (18.96 KB)

Great! We have found the solution of this question!

Whatsapp Lisa