APDS 2324 S1 Retake Solution
APDS 2324 S1 Retake Solution
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}
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
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]
Code:
function secret (num: integer) returns integer
if num < 10 then
return 1
end
return 1 + secret(num / 10)
end
num > 0 ˄ num < 10 → 101-1 ≤ num < 101 ; 0 < num < 10 → 0 ≤ num < 10 ; TRUE
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 ≥ 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
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.
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.
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: 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).