APDS 2324 S1 FirstCall
APDS 2324 S1 FirstCall
Full Name:
Login:
Specify the evaluatePolynomial algorithm, which takes an integer x and an array of integers p
representing a polynomial’s coefficients. This algorithm returns the result of evaluating the
polynomial at x.
Example: For inputs p = [-2, 3, 0, 1] (that is, an array representing the following
polynomial: 𝑝(𝑥) = −2𝑥 ! + 3𝑥 " + 1) and x = 2 it will return -3, after calculating -2·8+3·4+1.
b) Propose a change to the algorithm that would result in a lower asymptotic cost. [0.5 points]
We want to use a recursive sorting algorithm to sort a very large array (millions of elements). We’ve
already had some issues when trying to fit all the data we want to sort in the execution environment
we’re using, where memory is a very scarce resource. Which recursive algorithm would you use?
Justify your answer and briefly explain how the algorithm you picked works.
Problem 4 - Recursive design [3 points]
Design and implement a recursive algorithm that satisfies the following formal specification:
{ a > 0 ˄ b > 0 }
power IN[a, b: integer] OUT[p: integer]
{ p = ab }
Important: You are not allowed to use the pow function (or similar ones) in your design.
a) Define the algorithm’s cases and functions. Code a function that uses them. [1 point]
c) Calculate the cost of your algorithm by applying the corresponding theorem. [1 point]
Given the following problem, with its goals and restrictions, design an algorithm to solve it. Describe
your design process as much as you can (problem type, configuration, solution space size…), as well
as the result and its details (chosen approach, specific logic, optimizations…). You’re encouraged to
use different means (text, drawings, code…) as you see fit.
Books are a blast to read, but doing so requires a fair amount of time. After multiple literature
festivals, Saint George days, Christmas Eves, and birthdays, our list of works to read has grown quite
long.
To tackle the problem we’re facing, we have chosen to discard books until we have a shortened list
that includes the same genres as the original while minimizing the number of works to read.
We own a total of N books, with each one having one or more genres. In total, the original list includes
G genres (thriller, fantasy, historical fiction, algorithmics...). In general, N > G.
Explain how greedy algorithms work, while taking their advantages and disadvantages into account.
Propose a solution to the knapsack problem using this approach.
Note: Specifically, we have a set of N items, each one with its corresponding weight and value. We
want to find a subset of items that maximizes the total value without going over a weight limit L.
Auxiliary notes
5. 𝑄(𝑥) → 𝑡(𝑥) ≥ 0
Muster theorem:
1 𝑛≤1
𝑇(𝑛) ≤ A
𝑎𝑇(𝑛 − 𝑏) + 𝑓(𝑛) 𝑛>1
𝑓(𝑛) ∈ 𝜃(𝑛% )
𝜃(𝑛% ) 𝑎<1
𝑇(𝑛) ∈ F 𝜃(𝑛%&' ) 𝑎=1
𝜃(𝑛% 𝑎$/) ) 𝑎>1
Master theorem:
1 𝑛≤1
𝑇(𝑛) ≤ A
𝑎𝑇(𝑛/𝑏) + 𝑓(𝑛) 𝑛>1
𝑓(𝑛) ∈ 𝜃(𝑛% )
𝜃(𝑛% ) 𝑎 < 𝑏%
𝑇(𝑛) ∈ F 𝜃(𝑛% log 𝑛) 𝑎 = 𝑏%
𝜃(𝑛*+,! (.) ) 𝑎 > 𝑏%
Blank side, use it for drafts