Salta al contenuto
Note per Studenti Algorithms in Engineering - Exam of 9 July 2025 Report

Algorithms in Engineering - Exam of 9 July 2025

Questa pagina non ha ancora la versione ripasso: qui sotto c'è il testo completo.

In questa pagina 3

This is the translation of Part 2 of the written exam of 9 July 2025 of the equivalent course taught in Italian at the University of Padua ("Algoritmi per l'Ingegneria", Information Engineering, UniPD). The sheet contains only the second part (problem solving): two exercises, worth 16 and 17 points, 33 points in total; the duration is not printed on the sheet. Part 1 of the exam (a closed-answer questionnaire on the basics of the course) is not public and is not reproduced here.

The official exam format of this course is a written test in two parts: (1) a closed-answer questionnaire on the basics of the course; (2) problems (development and analysis of an algorithm, proof of a property). The second part may be left uncorrected if the first part is seriously insufficient; if the test is insufficient, the whole test must be taken again. The instructor may add an optional oral exam. Use of AI is forbidden.

The text of the problems is translated faithfully. The solutions below were written for this course and checked against the official solution (which is in English) and with Python programs (details at the end of each solution). Pseudocode follows the style of the notes (Longest Common Subsequencethe longest common subsequence of X (m symbols) and Y (n symbols) is found by dynamic programming on pairs of prefixes with l(i,j) = 0 if i = 0 or j = 0, 1 + l(i-1,j-1) if x_i = y_j, max of the two neighbours otherwise; bottom-up costs mn comparisons, reconstruction O(m+n), pure divide and conquer costs C(m+n,m) - 1 internal nodes, and space drops to Theta(min{m,n}) if only the length is needed.Longest Common Subsequence →, String Alignment and Longest Palindromic Subsequencetwo string problems solved by dynamic programming like the LCS; optimal DNA alignment with score matrix S and gap penalty g uses C(i,j) = max{C(i-1,j-1) + s, C(i-1,j) + g, C(i,j-1) + g} in Theta(mn) time and Theta(min{m,n}) space for the cost alone, while the longest palindromic subsequence works on substrings with p(i,j) = 2 + p(i+1,j-1) if x_i = x_j, else max of the two shorter substrings, scanned by diagonals with n(n-1)/2 comparisons.String Alignment and Longest Palindromic Subsequence →).

Exercise 1 (points 16)

Consider the following recurrence relation, defined for arbitrary positive values of the parameter nn:

T(n)={12n≤5,T(⌊n3⌋)+2T(⌈n4⌉−1)+2n+1n>5.T(n) = \begin{cases} 12 & n \le 5, \\ T\left(\left\lfloor \frac n3 \right\rfloor\right) + 2T\left(\left\lceil \frac n4 \right\rceil - 1\right) + 2n + 1 & n > 5. \end{cases}

Using parametric induction, determine a constant cc such that T(n)≤cnT(n) \le cn for every n>0n > 0.

Show the result

c=13c = 13: the constraints are c≥12c \ge 12 (base cases) and c≥13c \ge 13 (inductive step), so T(n)≤13nT(n) \le 13n for every n>0n > 0.

Show the full solution

This is exactly the setting of Parametric InductionParametric induction proves a bound T(n) <= cn+d (or >=) with unknown constants c and d: simulate the induction proof, collect constraints on c and d from the base and the step HP => TH, and if their intersection is nonempty pick the best pair. For MAX it gives exactly T(n) = n-1; it can fail even for true guesses (add a parameter or a lower-order term).Parametric Induction →: we simulate an induction proof of the guess T(n)≤cnT(n) \le cn, with cc an unknown constant that is the same for all nn, we collect the constraints that make every step valid, and at the end we choose a value of cc that satisfies all of them. The recurrence has floors and ceilings and unbalanced subproblems (n3\frac n3 and n4\frac n4), so no closed form is available and the Master TheoremThe Master Theorem for T(n) = aT(n/b) + w(n) compares w(n) with the threshold n^(log_b a). Case 1 (w polynomially smaller): Theta(n^(log_b a)); case 2 (same order): Theta(n^(log_b a) log n); case 3 (polynomially larger and regular): Theta(w(n)). Full proof from the levels of the tree, extended version with floors and ceilings, a table of applications, and the misuse when no case applies (w = n log n).Master Theorem → does not apply as it stands: parametric induction is the right tool.

Step 1: base cases [B]. For 1≤n≤51 \le n \le 5 we have T(n)=12T(n) = 12, and the guess requires 12≤?cn12 \overset?\le cn, that is c≥12nc \ge \frac{12}{n}. There is one constraint per base value, and they are all implied by the most restrictive one, which is the one with the smallest nn:

n=1:c≥12.n = 1:\quad c \ge 12.

(Typical error: checking only one value of nn, for instance n=5n = 5, which gives c≥125c \ge \frac{12}{5} and would produce a constant that is too small.)

Step 2: hypothesis [HP]. For n>5n > 5 assume T(m)≤cmT(m) \le cm for every mm with 0<m<n0 < m < n. The two recursive arguments must be inside this range:

  • ⌊n3⌋≥2\lfloor \frac n3 \rfloor \ge 2 and ⌊n3⌋<n\lfloor \frac n3 \rfloor < n;
  • ⌈n4⌉−1≥1\lceil \frac n4 \rceil - 1 \ge 1 for n≥6n \ge 6 (for n=6n = 6: ⌈1.5⌉−1=1\lceil 1.5 \rceil - 1 = 1), and ⌈n4⌉−1<n\lceil \frac n4 \rceil - 1 < n.

So [HP] can be applied to both terms (and the argument is never 00, where the guess would say T(0)≤0T(0) \le 0 and the recurrence is not defined).

Step 3: inductive step [HP ⇒\Rightarrow TH]. We want T(n)≤cnT(n) \le cn for n>5n > 5. We use the definition, [HP] on both terms (the coefficient 22 and c>0c > 0 keep the inequality direction), and the always-valid bounds ⌊n3⌋≤n3\lfloor \frac n3 \rfloor \le \frac n3 and ⌈n4⌉≤n4+1\lceil \frac n4 \rceil \le \frac n4 + 1:

T(n)=T(⌊n3⌋)+2T(⌈n4⌉−1)+2n+1≤c⌊n3⌋+2c(⌈n4⌉−1)+2n+1(HP)≤c n3+2c(n4+1−1)+2n+1(floor and ceiling bounds)=c(13+12)n+2n+1=c 56 n+2n+1 ≤? cn.\begin{aligned} T(n) &= T\left(\left\lfloor \tfrac n3 \right\rfloor\right) + 2T\left(\left\lceil \tfrac n4 \right\rceil - 1\right) + 2n + 1\\ &\le c\left\lfloor \tfrac n3 \right\rfloor + 2c\left(\left\lceil \tfrac n4 \right\rceil - 1\right) + 2n + 1 &&\text{(HP)}\\ &\le c\,\tfrac n3 + 2c\left(\tfrac n4 + 1 - 1\right) + 2n + 1 &&\text{(floor and ceiling bounds)}\\ &= c\left(\tfrac13 + \tfrac12\right)n + 2n + 1 = c\,\tfrac56\,n + 2n + 1 \ \overset?\le\ cn. \end{aligned}

The last symbol ≤?\overset?\le is a condition on cc, not a true inequality. Moving c56nc\frac56 n to the right-hand side:

c n6 ≥? 2n+1.c\,\tfrac n6 \ \overset?\ge\ 2n + 1.

How to read this condition. For n>5n > 5, that is n≥6n \ge 6 (it is the range in which the step is used), we need the smallest cc such that cn6≥2n+1c\frac n6 \ge 2n + 1 for all n≥6n \ge 6. Dividing by nn: c6≥2+1n\frac c6 \ge 2 + \frac1n. The right-hand side is decreasing in nn, so the worst case is the smallest nn, that is n=6n = 6:

c6≥2+16=136  ⟺  c≥13.\frac c6 \ge 2 + \frac16 = \frac{13}6 \iff c \ge 13.

Geometrically: the lines yc=c6xy_c = \frac{c}{6}x all pass through the origin; we need one above the line y=2x+1y = 2x + 1 for all x≥6x \ge 6. The line ycy_c has slope c6>2\frac c6 > 2 (for c>12c > 12) while y=2x+1y = 2x + 1 has slope 22, so once ycy_c is above at the first admissible point it stays above: it is enough to impose it at x=6x = 6, that is c≥2⋅6+1=13c \ge 2 \cdot 6 + 1 = 13.

Step 4: intersection. The constraints are c≥12c \ge 12 (base) and c≥13c \ge 13 (step), so the set of admissible constants is c≥13c \ge 13; for an upper bound we take the smallest:

c=max⁡{12,13}=13,T(n)≤13n for all n>0.■c = \max\{12, 13\} = 13, \qquad T(n) \le 13n \ \text{for all } n > 0. \qquad \blacksquare

Typical errors. Forgetting that the step is used for n≥6n \ge 6 only and trying to impose c6≥2+1n\frac c6 \ge 2 + \frac1n at n=1n = 1; forgetting the +1+1 in 2n+12n + 1 and obtaining c≥12c \ge 12 from the step as well (the constant would then be 1212, which does not satisfy the step with the bounds used here); using the bound ⌈n4⌉−1≤n4−1\lceil \frac n4 \rceil - 1 \le \frac n4 - 1, which is false (for n=5n = 5 it would say 1≤0.251 \le 0.25); not checking that the arguments of TT lie in [1,n−1][1, n - 1].

A remark on sharpness (not required). The constant 1313 is sufficient, but it is not the best one. A more careful bound on the ceiling, ⌈n4⌉≤n+34\lceil \frac n4 \rceil \le \frac{n + 3}4 (valid for integers), gives ⌈n4⌉−1≤n−14\lceil \frac n4 \rceil - 1 \le \frac{n - 1}4, and the step becomes

c(n3+n−12)+2n+1≤cn  ⟺  c(n6+12)≥2n+1  ⟺  c≥6(2n+1)n+3=12−30n+3,c\left(\tfrac n3 + \tfrac{n - 1}2\right) + 2n + 1 \le cn \iff c\left(\tfrac n6 + \tfrac12\right) \ge 2n + 1 \iff c \ge \frac{6(2n + 1)}{n + 3} = 12 - \frac{30}{n + 3},

which holds for every nn as soon as c≥12c \ge 12. So with this finer estimate the constraint from the step is no longer stronger than the one from the base and parametric induction yields c=12c = 12, which is the best possible constant for the guess T(n)≤cnT(n) \le cn, because T(1)=12T(1) = 12. The exam asks only for a constant, and 1313 is the value of the official solution.

Verification with a program. The recurrence was evaluated for every n≤2⋅106n \le 2 \cdot 10^6: T(n)≤13nT(n) \le 13n holds everywhere, and the largest ratio T(n)n\frac{T(n)}n is exactly 1212, attained at n=1n = 1 (so c=12c = 12 is also correct, as in the remark). The first values are T(1),…,T(8)=12,12,12,12,12,49,51,53T(1), \dots, T(8) = 12, 12, 12, 12, 12, 49, 51, 53. For 6≤n<1056 \le n < 10^5 the program checked that both recursive arguments lie in [1,n−1][1, n - 1], that ⌈n4⌉−1≤n4\lceil \frac n4 \rceil - 1 \le \frac n4, and that the chain of the step with c=13c = 13 ends in 13⋅56n+2n+n6≤13n13 \cdot \frac56 n + 2n + \frac n6 \le 13n (in fact with equality), and that at n=6n = 6 the step with c=13c = 13 gives 65≤7865 \le 78.

Exercise 2 (points 17)

A string Z=⟨z1,z2,…,zk⟩Z = \langle z_1, z_2, \dots, z_k \rangle is called antipalindromic if it has even length and, for k≥2k \ge 2, satisfies z1+h≠zk−hz_{1+h} \ne z_{k-h} for 0≤h≤k−10 \le h \le k - 1. (So Z=ϵZ = \epsilon is antipalindromic; strings of odd length are not antipalindromic.)

Given as input an arbitrary string XX of length n≥1n \ge 1 defined over some alphabet Σ\Sigma, we want to determine the length of one of its Longest Antipalindromic Subsequences (SALM(X)\text{SALM}(X)). (Observe that this length will always be an even number.)

2.(a) [6 points] Prove an optimal substructure property for the problem on the space {Xi..j:1≤i≤j≤n}\{X_{i..j} : 1 \le i \le j \le n\} of the substrings of XX.

2.(b) [3 points] Give a recurrence for the length of SALM(Xi..j)\text{SALM}(X_{i..j}) of a generic subinstance Xi..jX_{i..j}.

2.(c) [5 points] Give the iterative bottom-up pseudocode COMPUTE_SALM(X)(X) that, given an input string X=⟨x1,x2,…,xn⟩X = \langle x_1, x_2, \dots, x_n \rangle, returns the length of a SALM(X)\text{SALM}(X), arguing its correctness.

2.(d) [3 points] Let T(n)T(n) be the number of comparisons between characters of the alphabet Σ\Sigma performed by the algorithm developed in Point 2.(c). Evaluate T(n)T(n) exactly.

Show the result
  • (a) For Z=SALM(Xi..j)Z = \text{SALM}(X_{i..j}): if i=ji = j then Z=ϵZ = \epsilon; if j=i+1j = i + 1 then Z=ϵZ = \epsilon when xi=xjx_i = x_j and Z=⟨xi,xj⟩Z = \langle x_i, x_j \rangle when xi≠xjx_i \ne x_j; if j>i+1j > i + 1 and xi≠xjx_i \ne x_j, there is a ZZ with z1=xiz_1 = x_i, zk=xjz_k = x_j and Z2..k−1Z_{2..k-1} a SALM(Xi+1..j−1)\text{SALM}(X_{i+1..j-1}); if j>i+1j > i + 1 and xi=xjx_i = x_j, ZZ is a SALM\text{SALM} of Xi+1..jX_{i+1..j} or of Xi..j−1X_{i..j-1}.
  • (b) With s(i,j)=∣SALM(Xi..j)∣s(i, j) = |\text{SALM}(X_{i..j})|: s(i,j)={0i=j,2j=i+1, xi≠xj,0j=i+1, xi=xj,2+s(i+1,j−1)j>i+1, xi≠xj,max⁡{s(i+1,j),s(i,j−1)}j>i+1, xi=xj.s(i, j) = \begin{cases} 0 & i = j, \\ 2 & j = i + 1,\ x_i \ne x_j, \\ 0 & j = i + 1,\ x_i = x_j, \\ 2 + s(i + 1, j - 1) & j > i + 1,\ x_i \ne x_j, \\ \max\{s(i + 1, j), s(i, j - 1)\} & j > i + 1,\ x_i = x_j. \end{cases}
  • (c) Fill an n×nn \times n table SS by increasing diagonals ℓ=j−i+1=1,2,3,…,n\ell = j - i + 1 = 1, 2, 3, \dots, n and return S[1,n]S[1, n]; time and space Θ(n2)\Theta(n^2).
  • (d) T(n)=(n2)=n(n−1)2=Θ(n2)T(n) = \binom n2 = \frac{n(n - 1)}2 = \Theta(n^2).
Show the full solution

This is the same scheme as the Longest Common Subsequencethe longest common subsequence of X (m symbols) and Y (n symbols) is found by dynamic programming on pairs of prefixes with l(i,j) = 0 if i = 0 or j = 0, 1 + l(i-1,j-1) if x_i = y_j, max of the two neighbours otherwise; bottom-up costs mn comparisons, reconstruction O(m+n), pure divide and conquer costs C(m+n,m) - 1 internal nodes, and space drops to Theta(min{m,n}) if only the length is needed.Longest Common Subsequence → and especially as the longest palindromic subsequence in String Alignment and Longest Palindromic Subsequencetwo string problems solved by dynamic programming like the LCS; optimal DNA alignment with score matrix S and gap penalty g uses C(i,j) = max{C(i-1,j-1) + s, C(i-1,j) + g, C(i,j-1) + g} in Theta(mn) time and Theta(min{m,n}) space for the cost alone, while the longest palindromic subsequence works on substrings with p(i,j) = 2 + p(i+1,j-1) if x_i = x_j, else max of the two shorter substrings, scanned by diagonals with n(n-1)/2 comparisons.String Alignment and Longest Palindromic Subsequence →: substrings (not prefixes) are needed because a character is removed from both ends of the string, the table is scanned by diagonals, and the comparisons are counted one per pair i<ji < j. See Dynamic Programming - Paradigm and Memoizationdynamic programming improves on divide and conquer by storing the solutions of repeated subinstances in a table; Fibonacci shows the gap (F_n - 1 additions recursively, n - 1 with a table), memoization is recursion plus a global table, and optimization problems follow a five-step scheme built on optimal substructure and cut-and-paste.Dynamic Programming - Paradigm and Memoization → for the general paradigm.

First: what the definition says. For k=4k = 4 the conditions z1+h≠z4−hz_{1+h} \ne z_{4-h} are z1≠z4z_1 \ne z_4 (for h=0h = 0 and h=3h = 3) and z2≠z3z_2 \ne z_3 (for h=1h = 1 and h=2h = 2): the string is "anti-mirror", every character differs from the one in the mirror position. For odd kk the value h=k−12h = \frac{k - 1}2 would require z(k+1)/2≠z(k+1)/2z_{(k+1)/2} \ne z_{(k+1)/2}, which is impossible: this is why odd lengths are excluded and why s(i,j)s(i, j) is always even. Examples over {a,b,c}\{a, b, c\}: abcaabca is not antipalindromic (z1=z4z_1 = z_4), abcbabcb is (a≠ba \ne b, b≠cb \ne c), aabbaabb is (a≠ba \ne b and a≠ba \ne b), aaaa is not.

2.(a) Optimal substructure

For a substring Xi..jX_{i..j} let Z=⟨z1,…,zk⟩=SALM(Xi..j)Z = \langle z_1, \dots, z_k \rangle = \text{SALM}(X_{i..j}) be realized by indices i≤i1<i2<⋯<ik≤ji \le i_1 < i_2 < \dots < i_k \le j (so zr=xirz_r = x_{i_r} and kk is even). We prove five properties.

Property 1 (i=ji = j). Xi..iX_{i..i} has length 11: its subsequences are ϵ\epsilon and ⟨xi⟩\langle x_i \rangle, and the second has odd length, so Z=ϵZ = \epsilon.

Property 2 (j=i+1j = i + 1 and xi=xjx_i = x_j). The subsequences are ϵ\epsilon, ⟨xi⟩\langle x_i \rangle, ⟨xj⟩\langle x_j \rangle, ⟨xi,xj⟩\langle x_i, x_j \rangle; the last one has z1=z2z_1 = z_2, so it is not antipalindromic. Hence Z=ϵZ = \epsilon.

Property 3 (j=i+1j = i + 1 and xi≠xjx_i \ne x_j). The string ⟨xi,xj⟩\langle x_i, x_j \rangle is antipalindromic (z1≠z2z_1 \ne z_2) and, being the whole substring, no longer subsequence exists. Hence Z=⟨xi,xj⟩Z = \langle x_i, x_j \rangle.

(In these three cases ZZ is unique.)

Property 4 (j>i+1j > i + 1 and xi≠xjx_i \ne x_j). There exists a SALM(Xi..j)\text{SALM}(X_{i..j}) ZZ with z1=xiz_1 = x_i, zk=xjz_k = x_j and such that Z2..k−1Z_{2..k-1} is a SALM(Xi+1..j−1)\text{SALM}(X_{i+1..j-1}).

Proof. Start from any SALM\text{SALM} ZZ of Xi..jX_{i..j}. Since xi≠xjx_i \ne x_j, the string ⟨xi,xj⟩\langle x_i, x_j \rangle is an antipalindromic subsequence of length 22, so k≥2k \ge 2.

  1. We can force the two end characters. Let Z′=⟨xi,z2,…,zk−1,xj⟩Z' = \langle x_i, z_2, \dots, z_{k-1}, x_j \rangle (replace the first character by xix_i and the last by xjx_j). It is still a subsequence of Xi..jX_{i..j}: the new first index is i≤i1<i2i \le i_1 < i_2 and the new last index is j≥ik>ik−1j \ge i_k > i_{k-1}. It is still antipalindromic: for the outer pair the condition is xi≠xjx_i \ne x_j, true by assumption, and for every inner pair (z1+h,zk−h)(z_{1+h}, z_{k-h}) with 1≤h≤k−21 \le h \le k - 2 nothing has changed. It has the same length kk. So Z′Z' is a SALM(Xi..j)\text{SALM}(X_{i..j}) with the required first and last character.
  2. The middle part is an antipalindromic subsequence of Xi+1..j−1X_{i+1..j-1}. The indices i2,…,ik−1i_2, \dots, i_{k-1} are strictly between ii and jj, and the conditions on the inner pairs are exactly the conditions defining Z2..k−1′Z'_{2..k-1} as antipalindromic (for k=2k = 2 it is ϵ\epsilon).
  3. It is optimal (cut and paste). If Xi+1..j−1X_{i+1..j-1} had an antipalindromic subsequence WW longer than Z2..k−1′Z'_{2..k-1}, the string ⟨xi⟩W⟨xj⟩\langle x_i \rangle W \langle x_j \rangle would be an antipalindromic subsequence of Xi..jX_{i..j} (outer pair xi≠xjx_i \ne x_j, inner pairs from WW) of length ∣W∣+2>k|W| + 2 > k: this contradicts the optimality of Z′Z'. □\square

Remark (why "there exists"). The statement says that some SALM\text{SALM} has this form, not that every SALM(Xi..j)\text{SALM}(X_{i..j}) does (the stronger statement is false). Example: X=aabX = aab, i=1i = 1, j=3j = 3, x1=a≠b=x3x_1 = a \ne b = x_3: the SALM\text{SALM}s have length 22 and are both ⟨x1,x3⟩\langle x_1, x_3 \rangle and ⟨x2,x3⟩\langle x_2, x_3 \rangle, and the second one does not use x1x_1. The weaker statement is enough to get a correct algorithm: for the recurrence only the length counts, and the proof exhibits one optimal solution of the required form. This is the same situation as for the greedy choice, where it is enough to show that at least one optimal solution is compatible with the choice.

Property 5 (j>i+1j > i + 1 and xi=xjx_i = x_j). Every SALM(Xi..j)\text{SALM}(X_{i..j}) ZZ is a SALM\text{SALM} of Xi+1..jX_{i+1..j} or of Xi..j−1X_{i..j-1}.

Proof. If ZZ used both the first and the last position, that is i1=ii_1 = i and ik=ji_k = j (this requires k≥2k \ge 2), then z1=xi=xj=zkz_1 = x_i = x_j = z_k, contradicting the antipalindrome condition for h=0h = 0. Hence i1>ii_1 > i or ik<ji_k < j (or both; also if Z=ϵZ = \epsilon, which is a subsequence of both substrings). In the first case ZZ is a subsequence of Xi+1..jX_{i+1..j}, in the second one of Xi..j−1X_{i..j-1}. In both cases ZZ must be optimal for that substring: otherwise a longer antipalindromic subsequence of the substring would also be an antipalindromic subsequence of Xi..jX_{i..j}, longer than ZZ (cut and paste). □\square

Typical error: in Property 5 claiming that ZZ is the best of the two substrings and also may use xix_i and xjx_j together: for equal end characters they can never be both used, whereas for different end characters they are always used.

2.(b) The recurrence

Let s(i,j)=∣SALM(Xi..j)∣s(i, j) = |\text{SALM}(X_{i..j})|, for 1≤i≤j≤n1 \le i \le j \le n. Properties 1 to 5 give directly, with ∣Z∣=0|Z| = 0 or 22 in the base cases, ∣Z∣=2+∣Z2..k−1∣=2+s(i+1,j−1)|Z| = 2 + |Z_{2..k-1}| = 2 + s(i + 1, j - 1) in Property 4, and in Property 5 the longer of the two lengths (both substrings are candidates, since every antipalindromic subsequence of them is one of Xi..jX_{i..j}):

s(i,j)={0i=j,2j=i+1 ∧ xi≠xj,0j=i+1 ∧ xi=xj,2+s(i+1,j−1)j>i+1 ∧ xi≠xj,max⁡{s(i+1,j), s(i,j−1)}j>i+1 ∧ xi=xj.s(i, j) = \begin{cases} 0 & i = j, \\ 2 & j = i + 1 \ \land \ x_i \ne x_j, \\ 0 & j = i + 1 \ \land \ x_i = x_j, \\ 2 + s(i + 1, j - 1) & j > i + 1 \ \land \ x_i \ne x_j, \\ \max\{s(i + 1, j),\ s(i, j - 1)\} & j > i + 1 \ \land \ x_i = x_j. \end{cases}

The values s(i,j)s(i, j) are 00 or obtained from other values by adding 22, or are a maximum of two values: by induction they are all even, as required by the text.

Note the difference with the palindromic case: there the equal end characters are used (+2+2) and different ones are discarded (max); here it is the opposite.

2.(c) Bottom-up algorithm

Order of the computation. Let ℓ=j−i+1\ell = j - i + 1 be the length of Xi..jX_{i..j}. The base cases are the lengths ℓ=1\ell = 1 and ℓ=2\ell = 2. For ℓ≥3\ell \ge 3 the cell (i,j)(i, j) depends on (i+1,j−1)(i + 1, j - 1) (length ℓ−2\ell - 2), (i+1,j)(i + 1, j) and (i,j−1)(i, j - 1) (length ℓ−1\ell - 1). So filling the upper triangle diagonal by diagonal, with ℓ\ell increasing, guarantees that every cell read has already been written. (Scanning by rows from i=ni = n down to 11, left to right, would also work; scanning by increasing rows would not, because (i+1,⋅)(i + 1, \cdot) would not exist yet.)

COMPUTE_SALM(X)
  n <- length(X)
  for i <- 1 to n - 1 do
    S[i, i] <- 0
    if x_i != x_{i+1} then S[i, i+1] <- 2
    else S[i, i+1] <- 0
  S[n, n] <- 0
  { the first two diagonals, l = 1 and l = 2, contain the base cases }
  for l <- 3 to n do
    for i <- 1 to n - l + 1 do
      j <- i + l - 1
      if x_i != x_j then S[i, j] <- S[i+1, j-1] + 2
      else S[i, j] <- max(S[i+1, j], S[i, j-1])
  return S[1, n]

Correctness. By induction on ℓ\ell we show that, when the algorithm ends, S[i,j]=s(i,j)S[i, j] = s(i, j) for every cell of the upper triangle. For ℓ=1\ell = 1 and ℓ=2\ell = 2 the initialization loop writes exactly the values of the first three lines of the recurrence in (b). For ℓ≥3\ell \ge 3 assume the claim for all lengths smaller than ℓ\ell: the cell (i,j)(i, j) with j−i+1=ℓj - i + 1 = \ell reads (i+1,j−1)(i + 1, j - 1) (length ℓ−2≥1\ell - 2 \ge 1, so i+1≤j−1i + 1 \le j - 1 and it is in the triangle), (i+1,j)(i + 1, j) and (i,j−1)(i, j - 1) (length ℓ−1≥2\ell - 1 \ge 2), all already computed and correct by induction; the if applies the last two cases of the recurrence, which are correct by (a) and (b). For ℓ=n\ell = n there is exactly one cell, (1,n)(1, n), and S[1,n]=s(1,n)=∣SALM(X)∣S[1, n] = s(1, n) = |\text{SALM}(X)|. The algorithm is also correct for n=1n = 1 (the first loop does not run and S[1,1]=0S[1, 1] = 0) and for n=2n = 2 (the second loop does not run). ■\blacksquare

Space. The table has Θ(n2)\Theta(n^2) cells. (Optional: since the diagonal ℓ\ell needs only the diagonals ℓ−1\ell - 1 and ℓ−2\ell - 2, three vectors of length nn are enough to compute the length in O(n)O(n) space.)

Worked example. X=aabbabX = aabbab (n=6n = 6). The table s(i,j)s(i, j) (only i≤ji \le j):

1: a 2: a 3: b 4: b 5: a 6: b
1: a 0 0 2 4 4 4
2: a 0 2 2 2 4
3: b 0 0 2 2
4: b 0 2 2
5: a 0 2
6: b 0

Some cells by hand: (1,2)(1, 2): x1=x2=ax_1 = x_2 = a, j=i+1j = i + 1, so 00. (1,3)(1, 3): a≠ba \ne b, 2+s(2,2)=22 + s(2, 2) = 2. (1,4)(1, 4): a≠ba \ne b, 2+s(2,3)=2+2=42 + s(2, 3) = 2 + 2 = 4 (the string aabbaabb itself is antipalindromic). (2,5)(2, 5): x2=x5=ax_2 = x_5 = a, max⁡{s(3,5),s(2,4)}=max⁡{2,2}=2\max\{s(3, 5), s(2, 4)\} = \max\{2, 2\} = 2. (1,6)(1, 6): a≠ba \ne b, 2+s(2,5)=42 + s(2, 5) = 4. The answer is s(1,6)=4s(1, 6) = 4.

2.(d) Number of comparisons

The algorithm compares two characters of Σ\Sigma exactly once for every substring of length ℓ≥2\ell \ge 2 (every pair i<ji < j): in the initialization loop for ℓ=2\ell = 2 and in the main loop for ℓ≥3\ell \ge 3 (the additions and the maxima are on integers, not on characters of Σ\Sigma). Exactly:

T(n)=(n−1)⏟ℓ=2+∑ℓ=3n(n−ℓ+1)=(n−1)+∑m=1n−2m=(n−1)+(n−2)(n−1)2=(n−1)n2=(n2).T(n) = \underbrace{(n - 1)}_{\ell = 2} + \sum_{\ell = 3}^{n} (n - \ell + 1) = (n - 1) + \sum_{m = 1}^{n - 2} m = (n - 1) + \frac{(n - 2)(n - 1)}2 = \frac{(n - 1)n}2 = \binom n2 .

(For ℓ\ell the inner loop runs n−ℓ+1n - \ell + 1 times, and m=n−ℓ+1m = n - \ell + 1 goes from n−2n - 2, for ℓ=3\ell = 3, down to 11, for ℓ=n\ell = n.) In other words, T(n)=(n2)=Θ(n2)T(n) = \binom n2 = \Theta(n^2), one comparison for each of the (n2)\binom n2 pairs of positions i<ji < j. For n=6n = 6, as in the example: T(6)=15=5+4+3+2+1T(6) = 15 = 5 + 4 + 3 + 2 + 1. Typical errors: counting also the nn base cases ℓ=1\ell = 1 (no comparison there); writing n2n^2 or n22\frac{n^2}2 (the question asks for the exact value).

Verification with programs. (i) COMPUTE_SALM was implemented with a comparison counter and compared with a brute force over all subsequences (checking the antipalindrome definition literally, z1+h≠zk−hz_{1+h} \ne z_{k-h} for all hh, odd lengths rejected) on 3000 random strings with n≤13n \le 13 over alphabets of 2 to 5 symbols, and on every substring Xi..jX_{i..j} of length up to 88 of each of them: all values s(i,j)s(i, j) agree, they are all even, and the counter is exactly n(n−1)2\frac{n(n - 1)}2 every time. (ii) The three-vector version of the diagonal scan (the optional remark) returns the same length on 10001000 further random strings. (iii) The remark in (a): for aabaab the exhaustive search finds exactly two SALM\text{SALM}s, with 00-based index sets {0,2}\{0, 2\} and {1,2}\{1, 2\}, so not all of them use the first character; for abababab and aabbaabb the unique SALM\text{SALM} is the whole string. (iv) The table of the worked example and its answer 44 and its 1515 comparisons coincide with the program's output. Official solution. Same results; one typo: in Property 4 the official text writes Z2..m−1Z_{2..m-1}, but mm is never defined, it should be Z2..k−1Z_{2..k-1} (kk being the length of ZZ).

Self-assessment grid
exercise points what must be there for full marks typical errors
Exercise 1 16 guess T(n)≤cnT(n) \le cn with the base constraints (most restrictive at n=1n = 1: c≥12c \ge 12); [HP] valid because both arguments are in [1,n−1][1, n - 1]; step with ⌊n3⌋≤n3\lfloor \frac n3 \rfloor \le \frac n3 and ⌈n4⌉≤n4+1\lceil \frac n4 \rceil \le \frac n4 + 1, leading to cn6≥2n+1c \frac n6 \ge 2n + 1 for n≥6n \ge 6, hence c≥13c \ge 13; intersection c=13c = 13 only one base value checked; ignoring that the step holds for n≥6n \ge 6 only; dropping the +1+1 and obtaining c≥12c \ge 12 from the step; wrong bound for the ceiling; constant depending on nn
Exercise 2(a) 6 five properties (two base cases of length ≤2\le 2 with the antipalindrome check, case xi≠xjx_i \ne x_j with the replacement of the end characters and the cut-and-paste argument on Xi+1..j−1X_{i+1..j-1}, case xi=xjx_i = x_j where the two ends cannot both be used); the statement for xi≠xjx_i \ne x_j is "there exists" a SALM of that form assuming every SALM uses both ends when xi≠xjx_i \ne x_j; forgetting that equal ends cannot be both used; no cut-and-paste for the optimality of the middle part; properties stated without proof
Exercise 2(b) 3 the five-line recurrence on s(i,j)s(i, j): 00, 22, 00 for the bases, 2+s(i+1,j−1)2 + s(i + 1, j - 1) and max⁡\max for the other two cases swapping the roles of equal and different ends (palindrome-style recurrence); base value 11 for i=ji = j; missing the case j=i+1j = i + 1 with equal characters
Exercise 2(c) 5 diagonal scan with ℓ\ell increasing, initialization of ℓ=1,2\ell = 1, 2, update by the recurrence, return S[1, n]; correctness by induction on ℓ\ell showing that every cell read is already correct scan by increasing rows (cells not yet computed); loop bounds i≤n−ℓ+1i \le n - \ell + 1 wrong; missing base diagonals; no correctness argument
Exercise 2(d) 3 T(n)=(n−1)+(n−1)(n−2)2=n(n−1)2=(n2)T(n) = (n - 1) + \frac{(n - 1)(n - 2)}2 = \frac{n(n - 1)}2 = \binom n2, with the explanation of the one comparison per pair i<ji < j asymptotic Θ(n2)\Theta(n^2) only; n2n^2; counting the base diagonal ℓ=1\ell = 1 or the integer operations