0% found this document useful (0 votes)
8 views7 pages

APDS 2324 S1 FirstCall Solution

The document outlines a series of algorithmic problems and their solutions, covering topics such as polynomial evaluation, cost analysis of sorting algorithms, recursive design for power calculation, combinatorial optimization for book selection, and greedy algorithms for the knapsack problem. Each problem includes specifications, proposed algorithms, and justifications for the chosen methods. The document emphasizes efficiency and correctness in algorithm design, along with formal verification and cost analysis.

Uploaded by

pablor 140
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

APDS 2324 S1 FirstCall Solution

The document outlines a series of algorithmic problems and their solutions, covering topics such as polynomial evaluation, cost analysis of sorting algorithms, recursive design for power calculation, combinatorial optimization for book selection, and greedy algorithms for the knapsack problem. Each problem includes specifications, proposed algorithms, and justifications for the chosen methods. The document emphasizes efficiency and correctness in algorithm design, along with formal verification and cost analysis.

Uploaded by

pablor 140
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

AY 2023 - 2024 Subject APDS (GIA10)

Date January 25th, 2024 Semester First


Duration 2 hours Call First

Full Name: Solution


Login: (not unique)

Problem 1 - Algorithm specification [1 point]

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 − 𝑖) }

Problem 2 - Cost analysis [1 point]

a) Find the asymptotic cost of the following algorithm: [0.5 points]

if list.size() > 0 then


list.sort() Θ(N·log(N))
min := list.get(0)
i := 1
while i < list.size() do
Θ(N·log(N))
if list.get(i) < min then
min := list.get(i) Ens fixem en com
Θ(N) creix el cost respecte
end N, pel que aquest
i := i + 1 condicional no afecta

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.

The QuickSort algorithm, as it is in-place (doesn’t use auxiliary memory


like the MergeSort algorithm). It’s based on the idea that in a sorted
array each element satisfies the condition that all elements to its left
are smaller than it, while all elements to its right are larger than it (or
the other way around).

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.

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]

Cases: bt(a, b): b = 1 ; e(a, b): a ; bnt(a, b): b > 1

Functions: s(a, b): b – 1 ; c(a, b, y’): a · y’

Code:

function power (a, b: integer) returns integer


if b = 1 then
return a
end
return a · power(a, b – 1)
end
b) Formally verify your design. [1 point]

1. 𝑸(𝒂, 𝒃) → 𝒃𝒕 (𝒂, 𝒃) ∨ 𝒃𝒏𝒕 (𝒂, 𝒃)

a > 0 ˄ b > 0→ b = 1 ˅ b > 1 ; b > 0 → b ≥ 1 ; TRUE

2. 𝐐(𝒂, 𝒃) ∧ 𝐛𝐧𝐭 (𝒂, 𝒃) → 𝐐A𝐬(𝒂, 𝒃)C

a > 0 ˄ b > 0 ˄ b > 1 → a > 0 ˄ b - 1 > 0 ; b > 1 → b > 1 ; TRUE

3. 𝐐(𝒂, 𝒃) ∧ 𝐛𝐭 (𝒂, 𝒃) → 𝐑A𝒂, 𝒃, 𝐞(𝒂, 𝒃)C

a > 0 ˄ b > 0 ˄ b = 1 → a = ab ; a > 0 ˄ b = 1 → a = ab ;

a = a1 ; CERT

4. 𝐐(𝒂, 𝒃) ∧ 𝐛𝐧𝐭 (𝒂, 𝒃) ∧ 𝐑(𝐬(𝒂, 𝒃), 𝐟) → 𝐑A𝒂, 𝒃, 𝐜(𝒂, 𝒃, 𝐩)C

a > 0 ˄ b > 0 ˄ b > 1 ˄ p = ab-1 → a·p = ab ;

a > 0 ˄ b > 1 ˄ p = ab-1 → a·p = ab ; TRUE, given that:

LHS => p = ab-1 => a· p = a·ab-1 => a· p = ab => RHS


5. 𝐐(𝒂, 𝒃) → 𝐭(𝒂, 𝒃) ≥ 𝟎 t(a,b) = b

a > 0 ˄ b > 0 → b ≥ 0 ; TRUE

6. 𝐐(𝒂, 𝒃) ∧ 𝐛𝐧𝐭 (𝒂, 𝒃) → 𝐭(𝒂, 𝒃) > 𝐭A𝐬(𝒂, 𝒃)C

a > 0 ˄ b > 0 ˄ b > 1 → b > b - 1 ; a > 0 ˄ b > 1 → b > b - 1 ; TRUE

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 𝜃(𝑏)

Problem 5 - Combinatorial optimization [3 points]

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.

Approach correctness: [1 point]


Design description: [1 point]
Result and details: [1 point]

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.

Example: Take the following list of books, described by their genres:


[ {A, B, C} , {B, C, D, E} , {D, E, F} , {B, E} , {G} ]
The optimal way to read at least one work of each genre (A, B, C, D, E, F, G) while
minimizing the number of books to read would be to pick the first, third and fifth books.

The problem is one of optimization (specifically minimization), and for


each decision we make, the cost (number of chosen books) either stays the
same or worsens, so we can apply PBCBS. This also implies that we can use
Branch and Bound, which with a good heuristic should be faster than
Backtracking.

To solve this problem, an efficient configuration could consist of an array


of size N where, for each book, we store whether we read it or not. The
size of this combinatorial space would be 2N, as we need to make N decisions
with 2 options each.

2
N

In addition to PBCBS, combinations of this configuration can be pruned


based on the genres that a book contributes. If a book does not introduce
any new genres according to the current configuration, the decision to pick
it can be pruned. To implement this pruning, it would be interesting to
apply marking and memorize which genres have already been selected.

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.

A greedy algorithm generates the solution configuration in a single pass,


without exploring the search space. To do this, a heuristic (criterion) is
established and used at each level to choose a good option (ideally the
best). In other words, it is a non-exhaustive approach that prioritizes
reducing the execution time over the correctness of the solution.

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

Partial formal verification:

1. 𝑄(𝑥) → 𝑏' (𝑥) ∨ 𝑏(' (𝑥)

2. 𝑄(𝑥) ∧ 𝑏(' (𝑥) → 𝑄A𝑠(𝑥)C

3. 𝑄(𝑥) ∧ 𝑏' (𝑥) → 𝑅A𝑥, 𝑒(𝑥)C

4. 𝑄(𝑥) ∧ 𝑏(' (𝑥) ∧ 𝑅(𝑠(𝑥), 𝑦) → 𝑅A𝑥, 𝑐(𝑥, 𝑦)C

Total formal verification:

5. 𝑄(𝑥) → 𝑡(𝑥) ≥ 0

6. 𝑄(𝑥) ∧ 𝑏(' (𝑥) → 𝑡(𝑥) > 𝑡A𝑠(𝑥)C

Muster theorem:

1 𝑛≤1
𝑇(𝑛) ≤ \
𝑎𝑇(𝑛 − 𝑏) + 𝑓(𝑛) 𝑛>1

𝑓(𝑛) ∈ 𝜃(𝑛) )

𝜃(𝑛) ) 𝑎<1
𝑇(𝑛) ∈ _ 𝜃(𝑛)*+ ) 𝑎=1
𝜃(𝑛) 𝑎(/- ) 𝑎>1

Master theorem:

1 𝑛≤1
𝑇(𝑛) ≤ \
𝑎𝑇(𝑛/𝑏) + 𝑓(𝑛) 𝑛>1

𝑓(𝑛) ∈ 𝜃(𝑛) )

𝜃(𝑛) ) 𝑎 < 𝑏)
𝑇(𝑛) ∈ _ 𝜃(𝑛) log 𝑛) 𝑎 = 𝑏)
𝜃(𝑛./0! (2) ) 𝑎 > 𝑏)
Blank side, use it for drafts

You might also like