0% found this document useful (0 votes)
4 views5 pages

APDS 2324 S1 Retake Solution

The document outlines a series of problems related to algorithms, including specifications for an isReverse algorithm, cost analysis of given algorithms, a visual representation of MergeSort, and the design of a recursive algorithm. It also discusses a combinatorial optimization problem involving the creation of a card deck with specific constraints and cost minimization. The document emphasizes the need for detailed design descriptions and performance analysis for the proposed solutions.

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)
4 views5 pages

APDS 2324 S1 Retake Solution

The document outlines a series of problems related to algorithms, including specifications for an isReverse algorithm, cost analysis of given algorithms, a visual representation of MergeSort, and the design of a recursive algorithm. It also discusses a combinatorial optimization problem involving the creation of a card deck with specific constraints and cost minimization. The document emphasizes the need for detailed design descriptions and performance analysis for the proposed solutions.

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/ 5

AY 2023 - 2024 Subject APDS (GIA10)

Date July 8th, 2024 Semester First


Duration 1 hour 30 min Call Retake

Full Name: Solution


Login: (not unique)

Problem 1: Algorithm specification [1 point]

Formally specify the isReverse algorithm. Given two non-empty arrays of the same size (named
a and b), this algorithm checks whether they hold the same elements but in reverse order.

Examples: The algorithm retorns true for inputs A=[1,2,3,4], B=[4,3,2,1] and false for
inputs A=[1,2,3,4], B=[4,13,12,1].

{n > 0}

isReverse IN[A, B: array [n] of integer] OUT[r: boolean]

{r = ∀𝑖 ∈ [0, 𝑛) ∶ A[𝑖] = 𝐵[n − 1 − 𝑖]}

Problem 2: Cost analysis [1 point]

Find the asymptotic cost of the following algorithms. You can write your answer on this sheet.

A)

count := 0 Θ(1)
Θ(N)
max := findMaximum(arr) Θ(N)
for i := 0 until N - 1 do
if arr[i] = max then Θ(N)
count := count + 1
Θ(1) Θ(N)
end
end
B)

i := 1
even := 0 Θ(1)
odd := 0
while i < N do
if arr[i] mod 2 then
i := i * 2 O(N)
even := even +1 Ω(log(N))
else
i := i + 1
odd := odd +1
end
end

Problema 3: Recursive sorting [1 point]

Given the following array of numerical vàlues, visually represent the process that the MergeSort
algorithm would follow to sort it.

1 23 12 10 9 5 8 3 13 42
Problem 4: Recursive design [3 points]

Design and implement a recursive algorithm that meets the following formal specification:

{ num > 0 }
secret IN[num: integer] OUT[d: integer]
{ 10d-1 ≤ num < 10d }

a) Define the algorithm’s cases and functions. Code a function that uses them. [1 point]

Cases: bt(num): num < 10 ; e(num): 1 ; bnt(num): num ≥ 10

Functions: s(num): num / 10 ; c(num, d’): d’ + 1

Code:
function secret (num: integer) returns integer
if num < 10 then
return 1
end
return 1 + secret(num / 10)
end

b) Formally verify your design. [1 point]

1. 𝑸(𝒏𝒖𝒎) → 𝒃𝒕 (𝒏𝒖𝒎) ∨ 𝒃𝒏𝒕 (𝒏𝒖𝒎)

num > 0 → num < 10 ˅ num ≥ 10 ; num > 0 → CERT ; TRUE

2. 𝐐(𝒏𝒖𝒎) ∧ 𝐛𝐧𝐭 (𝒏𝒖𝒎) → 𝐐>𝐬(𝒏𝒖𝒎)@

num > 0 ˄ num ≥ 10 → num / 10 > 0 ; num ≥ 10 → num / 10 > 0 ; TRUE

because: num / 10 ≥ 10 / 10 → num / 10 > 0 ; num / 10 ≥ 1 → num / 10 > 0

3. 𝐐(𝒏𝒖𝒎) ∧ 𝐛𝐭 (𝒏𝒖𝒎) → 𝐑>𝒏𝒖𝒎, 𝐞(𝒏𝒖𝒎)@

num > 0 ˄ num < 10 → 101-1 ≤ num < 101 ; 0 < num < 10 → 0 ≤ num < 10 ; TRUE

4. 𝐐(𝒏𝒖𝒎) ∧ 𝐛𝐧𝐭 (𝒏𝒖𝒎) ∧ 𝐑(𝐬(𝒏𝒖𝒎), 𝒅) → 𝐑.𝒏𝒖𝒎, 𝐜(𝒏𝒖𝒎, 𝒅)0

num > 0 ˄ num ≥ 10 ˄ 10d-1 ≤ num / 10 < 10d → 10d-1+1 ≤ num < 10d+1 ;

num ≥ 10 ˄ 10d-1 ≤ num / 10 < 10d → 10d ≤ num < 10d+1 ; TRUE, because:

LHS ===> 10d-1 ≤ num / 10 < 10d ===·10===> 10d ≤ num < 10d+1 ===> RHS
5. 𝐐(𝒏𝒖𝒎) → 𝐭(𝒏𝒖𝒎) ≥ 𝟎 t(num) = num

num > 0 → num ≥ 0 ; TRUE

6. 𝐐(𝒏𝒖𝒎) ∧ 𝐛𝐧𝐭 (𝒏𝒖𝒎) → 𝐭(𝒏𝒖𝒎) > 𝐭>𝐬(𝒏𝒖𝒎)@

num > 0 ˄ num ≥ 10 → num > num / 10 ; num ≥ 10 → num > num / 10 ; TRUE
c) Calculate the cost of your algorithm by applying the corresponding theorem. [1 point]

The Master Theorem must be applied, as the successor function divides the
input. One can identify a=1, b=10, c=0, resulting in a cost of 𝜃>log(𝑛)@,
specifically 𝜃>log(𝑛𝑢𝑚)@.s

Problem 5: Combinatorial optimization [4 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]

Lastly, reason whether your algorithm is efficient. If so, analyze if this can be a problem. Otherwise,
propose some performance improvements. Justify your answers. [1 point]

Context: One of the subject’s professors has been playing a popular card game known as “Arcana:
The Meeting”, and they are interested in figuring out what the perfect deck is. Thus, they’re looking
for an algorithm that, given a list of N unique cards, creates a deck with the following restrictions:

Decks must be formed by 50 cards. There are different types of card, and a deck must contain an
exact amount of each. Specifically, a deck must be formed by 20 land cards, 15 magic cards and 15
creature cards. Keep in mind that one can include up to 4 copies of a card in the list (you can assume
we have said copies, but that they don’t appear again in the input list).

Lastly, we know that each card has a cost. For games to be as quick as possible, we want to optimize
the sum of the card’s costs, finding the deck with the lowest total cost.

This is an optimization problem, specifically a minimization one.


Considering the decisions to take, the configuration can be an array of
size C (number of available cards) storing the number of copies of each
card that will be included in the deck. Considering that one can add 0, 1,
2, 3, or 4 copies of each card to the deck (5 options for each of the C
choices), the size of the solution space is 5C.

5
C
Considering the nature of the problem one could apply either backtracking
with PBCBS or branch and bound. To prune based on restrictions (number of
cards of each type) it would be best to apply marking and keep track of how
many cards of each type are in the deck for each configuration.

Note: The student’s answer is expected to be more detailed, particularly


when it comes to heuristics (if used), as it’s a very open-ended dimension
of the solution’s design.

Note: This problem belongs to the P complexity class, and it can also be
solved by sorting the array and chosing those cards with a lower cost as
many times as possible, respecting the limit by type. Such solutions have
been deemed valid, but reasoning following the subject guidelines was still
expected (type of problem, configuration, etc).

You might also like