APDS 2324 S1 FirstCall Solution
APDS 2324 S1 FirstCall Solution
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.
{ TRUE }
evaluatePolynomial IN[x: integer, p: array[n] of integer] OUT[r: integer]
{ r = Σ𝑖 ∈ [0, 𝑛) ∶ 𝑝[𝑖] · pow(𝑥, 𝑛 − 1 − 𝑖) }
end
print(min)
end
b) Propose a change to the algorithm that would result in a lower asymptotic cost. [0.5 points]
Given that the goal is to find the array’s smallest element (which can be
achieved in linear cost regardless of the order of the elements), there is
no need to sort the array.
Problem 3 - Recursive sorting [1 point]
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.
Therefore, the array is sorted by choosing a pivot for each recursive call,
which is the element that the rest will be compared to in order to determine
what should be moved to the left or to the right. The strategy for choosing
a pivot (heuristic) can be varied, but it’s common to pick an arbitrary
value such as the middle one.
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]
Code:
a = a1 ; CERT
c) Calculate the cost of your algorithm by applying the corresponding theorem. [1 point]
Given that the successor function subtracts, the Muster Theorem is applied.
We identify a=1, b=1, c=0, resulting in a cost of 𝜃(𝑛), specifically 𝜃(𝑏)
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.
2
N
A possible heuristic for estimating the total cost from partial cost could
consider the quantity of genres yet to be covered. From there, one can be
optimistic to varying degrees when calculating the minimum number of books
that would need to be chosen based on the accumulated total. In the case
of finding a strictly optimistic heuristic, it could be used for safe
pruning.
Problem 6 - Greedy [1 point]
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.
The knapsack problem can be solved with a heuristic that prioritizes the
value of the objects, but it’s also suitable to consider the weight. For
example, objects could be sorted based on the value/weight ratio
(descending) and the algorithm could prioritize taking the first ones,
skipping those that, due to the accumulation of objects, no longer fit into
the knapsack.
Auxiliary notes
5. 𝑄(𝑥) → 𝑡(𝑥) ≥ 0
Muster theorem:
1 𝑛≤1
𝑇(𝑛) ≤ \
𝑎𝑇(𝑛 − 𝑏) + 𝑓(𝑛) 𝑛>1
𝑓(𝑛) ∈ 𝜃(𝑛) )
𝜃(𝑛) ) 𝑎<1
𝑇(𝑛) ∈ _ 𝜃(𝑛)*+ ) 𝑎=1
𝜃(𝑛) 𝑎(/- ) 𝑎>1
Master theorem:
1 𝑛≤1
𝑇(𝑛) ≤ \
𝑎𝑇(𝑛/𝑏) + 𝑓(𝑛) 𝑛>1
𝑓(𝑛) ∈ 𝜃(𝑛) )
𝜃(𝑛) ) 𝑎 < 𝑏)
𝑇(𝑛) ∈ _ 𝜃(𝑛) log 𝑛) 𝑎 = 𝑏)
𝜃(𝑛./0! (2) ) 𝑎 > 𝑏)
Blank side, use it for drafts