Salta al contenuto
Note per Studenti Algorithms in Engineering - Exam of 26 June 2025 Report

Algorithms in Engineering - Exam of 26 June 2025

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

In questa pagina 3

Written exam of 26 June 2025, translated into English from the equivalent course taught in Italian ("Algoritmi per l'Ingegneria", Engineering, University of Padua). The sheet reproduced here is Part 2 only (problem solving): two problems worth 17 and 16 points, 33 points in total. Part 1 (the closed-answer questionnaire on the basics of the course) is not public and is not part of this document. The duration is not printed on the sheet.

Official examination format of the course (2026-27): a written test in two parts, (1) a closed-answer questionnaire on the basics of the course and (2) problems (development and analysis of an algorithm, proof of a property). The second part may be left uncorrected if the first is seriously insufficient; if the test is insufficient, the whole test is retaken. The lecturer may add an optional oral exam. The use of AI is forbidden.

The text of the problems is translated faithfully; the solutions are written here and checked against the official solution of the course: all results coincide, and a few misprints of the official solution are pointed out (they are collected at the end of each exercise). Every algorithm and formula was implemented in Python and run (operation counts included): see "Checks performed" in each exercise. Pseudocode follows the conventions of the notes (Computational Problems, Algorithms and Cost ModelA computational problem is a total, not necessarily single-valued relation between instances and solutions; an algorithm is a function from instances to solutions and solves the problem if its output is always related to its input. Correctness means agreement with the specification; complexity is the cost as a function of input size, by default in the worst case, with unit-cost instructions chosen meaningfully.Computational Problems, Algorithms and Cost Model →).

Exercise 1 (points 17)

Let ωn=ei2π/n\omega_n = e^{i2\pi/n} be the principal nn-th root of unity of order n≥1n \ge 1 in the complex field. For every even natural number n≥4n \ge 4, define the polynomial

pn(x)=(x−ωn1)⋅(x−ωn2)⋅…⋅(x−ωnn/2−1)⋅(x−ωnn/2+1)⋅(x−ωnn/2+2)⋅…⋅(x−ωnn−1).p_n(x) = (x - \omega_n^1)\cdot(x - \omega_n^2)\cdot\ldots\cdot(x - \omega_n^{n/2-1})\cdot(x - \omega_n^{n/2+1})\cdot(x - \omega_n^{n/2+2})\cdot\ldots\cdot(x - \omega_n^{n-1}).

Note that pn(x)p_n(x) vanishes at the nn-th roots of unity, except for ωn0=1\omega_n^0 = 1 and ωnn/2=−1\omega_n^{n/2} = -1. The polynomial pn(x)p_n(x) has degree n−2n - 2, but it is convenient to regard it with degree bound nn and write

pn(x)=∑i=0n−1aixi,p_n(x) = \sum_{i=0}^{n-1} a_i x^i,

where the coefficients a0,a1,…,an−2,an−1=0a_0, a_1, \dots, a_{n-2}, a_{n-1} = 0 are uniquely determined by nn.

In all parts of the problem, by the complexity of an algorithm we mean the number of arithmetic operations on complex operands.

1.(a) [4 points] Describe a simple algorithm A0\mathcal{A}_0 that, given nn as input, returns pn(+1)p_n(+1) and pn(−1)p_n(-1). Analyze the complexity Ta(n)T_a(n) of A0\mathcal{A}_0.

1.(b) [3 points] Does the proposed algorithm A0\mathcal{A}_0 have polynomial complexity? Justify the answer.

1.(c) [5 points] Describe an efficient algorithm (possibly O(n)O(n)) for the computation of the DFT (A0,A1,…,An−1)(A_0, A_1, \dots, A_{n-1}) of the vector (a0,a1,…,an−1)(a_0, a_1, \dots, a_{n-1}) and analyze its complexity Tc(n)T_c(n). (Recall the relation between the DFT and the evaluation of a polynomial at a suitable set of points.)

1.(d) [5 points] Describe an efficient algorithm (possibly O(n)O(n)) for the computation of the coefficients a0,a1,…,an−1a_0, a_1, \dots, a_{n-1} of pn(x)p_n(x), proving its correctness and analyzing its complexity Td(n)T_d(n).

Show the result
  • (a) A0\mathcal{A}_0 evaluates the defining product at +1+1 and at −1-1, sharing the powers of ωn\omega_n: Ta(n)=5n−13T_a(n) = 5n - 13. Since pn(x)=∏h=1n/2−1(x2−ωn2h)p_n(x) = \prod_{h=1}^{n/2-1}(x^2 - \omega_n^{2h}) is an even function, pn(1)=pn(−1)p_n(1) = p_n(-1) and one evaluation is enough: 32n−5\frac 32 n - 5 operations (and 3n−83n - 8 for a single evaluation of the plain product).
  • (b) No. The input is the integer nn, of size ∣⟨n⟩∣=⌊log⁡2n⌋+1\lvert\langle n\rangle\rvert = \lfloor\log_2 n\rfloor + 1 bits, and Ta(n)≥n≥2∣⟨n⟩∣−1T_a(n) \ge n \ge 2^{\lvert\langle n\rangle\rvert - 1}: the cost is exponential in the input size.
  • (c) DFT(a)=(pn(1),0,…,0,pn(−1),0,…,0)\mathrm{DFT}(a) = (p_n(1), 0, \dots, 0, p_n(-1), 0, \dots, 0), with pn(−1)p_n(-1) in position n/2n/2 and zeros elsewhere. Cost Tc(n)=32n−5=O(n)T_c(n) = \frac 32 n - 5 = O(n) (using the plain product it would be 3n−83n - 8, also O(n)O(n)).
  • (d) ai=2n pn(1)a_i = \frac 2n\,p_n(1) for even ii and ai=0a_i = 0 for odd ii; the cost is Td(n)=32n−4=O(n)T_d(n) = \frac 32 n - 4 = O(n). Indeed pn(1)=n2p_n(1) = \frac n2, so ai=1a_i = 1 for even i≤n−2i \le n-2 and ai=0a_i = 0 otherwise: pn(x)=1+x2+x4+⋯+xn−2p_n(x) = 1 + x^2 + x^4 + \dots + x^{n-2}.
Show the full solution

First, what the polynomial is. The nn numbers ωn0,…,ωnn−1\omega_n^0, \dots, \omega_n^{n-1} are the roots of xn−1x^n - 1 (DFT and Roots of UnityThe DFT of order n evaluates a polynomial with degree bound n at the n n-th roots of unity, y_i = p(w^i) with w = e^{2 pi i/n}; it is the product by the Fourier matrix F_n, an invertible Vandermonde matrix. The roots form a multiplicative group and satisfy three lemmas: cancellation (w_{dn}^{dk} = w_n^k, w_n^{n/2} = -1), halving (the squares of the n roots are the n/2 roots of order n/2, each twice) and summation (the sum of the powers of w^k is n if k is a multiple of n, 0 otherwise). Linear convolution theorem: (c|0) = DFT^{-1}{2n}(DFT{2n}(a|0) times DFT_{2n}(b|0)).DFT and Roots of Unity →). pnp_n is the monic polynomial that keeps n−2n - 2 of them and drops the two real ones, ωn0=1\omega_n^0 = 1 and ωnn/2=ω2=−1\omega_n^{n/2} = \omega_2 = -1 (the corollary of the cancellation lemma; here nn is even but not necessarily a power of two). Example, n=4n = 4: p4(x)=(x−i)(x+i)=x2+1p_4(x) = (x - i)(x + i) = x^2 + 1, with coefficient vector (1,0,1,0)(1, 0, 1, 0). The coefficient an−1a_{n-1} is 00 because the degree is n−2n-2 (this is the "an−1=0a_{n-1} = 0" of the text), and an−2=1a_{n-2} = 1 because pnp_n is monic.

Point (a): the simple algorithm A0\mathcal{A}_0. Evaluate the defining formula directly at x=+1x = +1 and at x=−1x = -1. The cost model is the one of the text: each arithmetic operation between complex numbers costs 11 (the constant ωn\omega_n itself and the constants ±1\pm 1 are free). The set of exponents is H={1,…,n−1}∖{n/2}H = \{1, \dots, n-1\} \setminus \{n/2\}, which has n−2n - 2 elements.

A0(n)
  P[1] <- w_n                                         { the constant e^(i 2 pi / n) }
  for h <- 2 to n-1 with h != n/2 do
    P[h] <- P[h-1] * w_n                              { one multiplication per power }
                                                       { (for h = n/2 + 1 use w^(n/2) = -1 as P[h-1]) }
  u <- 1 - P[1];  v <- -1 - P[1]                      { first factors at x = +1 and x = -1 }
  for h <- 2 to n-1 with h != n/2 do
    u <- u * (1 - P[h])                               { 1 subtraction + 1 product }
    v <- v * (-1 - P[h])                              { 1 subtraction + 1 product }
  return (u, v)

Correctness is immediate: u and v are the products of the factors (x−ωnh)(x - \omega_n^h), h∈Hh \in H, at x=1x = 1 and x=−1x = -1, i.e. the definition of pnp_n.

Cost. The powers ωnh\omega_n^h with 2≤h≤n−12 \le h \le n-1, h≠n/2h \ne n/2 are n−3n - 3 values, one multiplication each (the power with h=1h = 1 is the constant). The two evaluations share them. Each evaluation has n−2n - 2 factors (x−ωnh)(x - \omega_n^h), so n−2n - 2 subtractions, and n−3n - 3 products to multiply the n−2n - 2 factors together:

Ta(n)=(n−3)+2(n−2)+2(n−3)=5n−13.T_a(n) = (n - 3) + 2(n - 2) + 2(n - 3) = 5n - 13.

A single evaluation costs (n−3)+(n−2)+(n−3)=3n−8(n-3) + (n-2) + (n-3) = 3n - 8. Without sharing the powers the two evaluations would cost 2(3n−8)=6n−162(3n-8) = 6n - 16; sharing saves n−3n - 3 multiplications and gives 5n−135n - 13.

Better constants. Pair the factor hh with the factor n/2+hn/2 + h, for 1≤h≤n/2−11 \le h \le n/2 - 1. Since ωnn/2+h=−ωnh\omega_n^{n/2 + h} = -\omega_n^h,

pn(x)=∏h=1n/2−1(x−ωnh)(x−ωnn/2+h)=∏h=1n/2−1(x−ωnh)(x+ωnh)=∏h=1n/2−1(x2−ωn2h).p_n(x) = \prod_{h=1}^{n/2-1}(x - \omega_n^h)(x - \omega_n^{n/2+h}) = \prod_{h=1}^{n/2-1}(x - \omega_n^h)(x + \omega_n^h) = \prod_{h=1}^{n/2-1}\bigl(x^2 - \omega_n^{2h}\bigr).

The right-hand side depends only on x2x^2, so pn(x)=pn(−x)p_n(x) = p_n(-x) for every x∈Cx \in \mathbb{C} (pnp_n is an even polynomial). In particular pn(1)=pn(−1)p_n(1) = p_n(-1): one evaluation is enough. The product has n/2−1n/2 - 1 factors and ωn2h=ωn/2h\omega_n^{2h} = \omega_{n/2}^h by the cancellation lemma:

A0'(n)
  m <- n/2
  P[1] <- w_m                                         { constant, w_m = w_n^2 }
  for h <- 2 to m-1 do P[h] <- P[h-1] * w_m          { m-2 multiplications }
  u <- 1 - P[1]
  for h <- 2 to m-1 do u <- u * (1 - P[h])           { m-2 subtractions + m-2 products }
  return (u, u)                                       { p(1) = p(-1) }

At x=1x = 1 we have x2=1x^2 = 1 (no multiplication). The cost is (m−2)+(m−1)+(m−2)=3m−5(m - 2) + (m - 1) + (m - 2) = 3m - 5, i.e.

Ta′(n)=32 n−5=32 n+O(1).T_a'(n) = \tfrac 32\,n - 5 = \tfrac 32\,n + O(1).

This agrees with the official 32n+O(1)\frac 32 n + O(1). Example (n=6n = 6): p6(x)=(x2−ω3)(x2−ω32)=x4−(ω3+ω32)x2+ω33=x4+x2+1p_6(x) = (x^2 - \omega_3)(x^2 - \omega_3^2) = x^4 - (\omega_3 + \omega_3^2)x^2 + \omega_3^3 = x^4 + x^2 + 1, because 1+ω3+ω32=01 + \omega_3 + \omega_3^2 = 0 (summation lemma) and ω33=1\omega_3^3 = 1: p6(1)=3=p6(−1)p_6(1) = 3 = p_6(-1), cost 32⋅6−5=4\frac 32 \cdot 6 - 5 = 4.

Observation (also in the official solution). One has pn(1)=∑iaip_n(1) = \sum_i a_i, but that formula cannot be used to compute pn(1)p_n(1): the coefficients aia_i are not given, they are what the later parts compute.

Typical errors: forgetting that the factor h=n/2h = n/2 is missing (the product would contain x+1x + 1 and would not be pnp_n); counting the powers of ωn\omega_n twice (once for +1+1 and once for −1-1); writing Θ(nlog⁡n)\Theta(n \log n) because the problem "looks like" an FFT: no transform is needed here.

Point (b): is A0\mathcal{A}_0 polynomial? "Polynomial complexity" means polynomial in the size of the input (Computational Problems, Algorithms and Cost ModelA computational problem is a total, not necessarily single-valued relation between instances and solutions; an algorithm is a function from instances to solutions and solves the problem if its output is always related to its input. Correctness means agreement with the specification; complexity is the cost as a function of input size, by default in the worst case, with unit-cost instructions chosen meaningfully.Computational Problems, Algorithms and Cost Model →: size = reasonable measure of the encoding; for an integer it is the number of bits). The input of the problem is the single integer nn, written in binary with ∣⟨n⟩∣=⌊log⁡2n⌋+1\lvert\langle n\rangle\rvert = \lfloor\log_2 n\rfloor + 1 bits. Then

Ta(n)=5n−13≥n≥2⌊log⁡2n⌋=2∣⟨n⟩∣−1(n≥4).T_a(n) = 5n - 13 \ge n \ge 2^{\lfloor\log_2 n\rfloor} = 2^{\lvert\langle n\rangle\rvert - 1} \qquad (n \ge 4).

A function that is at least 2∣⟨n⟩∣−12^{\lvert\langle n\rangle\rvert - 1} is not bounded by any polynomial in ∣⟨n⟩∣\lvert\langle n\rangle\rvert (for every constants c,kc, k one has c sk<2s−1c\,s^k < 2^{s-1} for large ss). So A0\mathcal{A}_0 is exponential in the input size. It is linear in the value of nn, which is not the same: this is the same phenomenon as trial division for primality (see the example on the size of an integer in the cost-model note). The improved version A0′A_0', with 32n−5\frac 32 n - 5 operations, is exponential in the size too: halving the constant does not change the class.

Remark. This concerns the algorithm, not the problem: with pn(x)(x2−1)=xn−1p_n(x)(x^2 - 1) = x^n - 1 (shown in point (d)) one gets pn(±1)=n2p_n(\pm 1) = \frac n2 with a single division, so the problem is solvable with O(1)O(1) arithmetic operations.

Misprint in the official solution: the official text writes "n−8=2log⁡2n+1−8≥2∣⟨n⟩∣−8n - 8 = 2^{\log_2 n + 1} - 8 \ge 2^{\lvert\langle n\rangle\rvert} - 8". But 2log⁡2n+1=2n2^{\log_2 n + 1} = 2n, not nn, and n≥2∣⟨n⟩∣n \ge 2^{\lvert\langle n\rangle\rvert} is false (2∣⟨n⟩∣=2⌊log⁡2n⌋+1>n2^{\lvert\langle n\rangle\rvert} = 2^{\lfloor\log_2 n\rfloor + 1} > n). The correct chain is n−8≥2∣⟨n⟩∣−1−8n - 8 \ge 2^{\lvert\langle n\rangle\rvert - 1} - 8; the conclusion (exponential) is unchanged.

Point (c): the DFT of a=(a0,…,an−1)\mathbf{a} = (a_0, \dots, a_{n-1}). By definition (DFT and Roots of UnityThe DFT of order n evaluates a polynomial with degree bound n at the n n-th roots of unity, y_i = p(w^i) with w = e^{2 pi i/n}; it is the product by the Fourier matrix F_n, an invertible Vandermonde matrix. The roots form a multiplicative group and satisfy three lemmas: cancellation (w_{dn}^{dk} = w_n^k, w_n^{n/2} = -1), halving (the squares of the n roots are the n/2 roots of order n/2, each twice) and summation (the sum of the powers of w^k is n if k is a multiple of n, 0 otherwise). Linear convolution theorem: (c|0) = DFT^{-1}{2n}(DFT{2n}(a|0) times DFT_{2n}(b|0)).DFT and Roots of Unity →) the DFT of the coefficient vector of a polynomial of degree bound nn is the vector of its values at the nn-th roots of unity:

Ah=∑j=0n−1aj ωnhj=pn(ωnh),0≤h≤n−1.A_h = \sum_{j=0}^{n-1} a_j\,\omega_n^{hj} = p_n(\omega_n^h), \qquad 0 \le h \le n - 1.

  • For h∉{0,n/2}h \notin \{0, n/2\}, ωnh\omega_n^h is one of the roots kept in the product, so Ah=pn(ωnh)=0A_h = p_n(\omega_n^h) = 0 (a factor of the product is zero).
  • For h=0h = 0: A0=pn(1)A_0 = p_n(1). For h=n/2h = n/2: An/2=pn(ωnn/2)=pn(−1)=pn(1)A_{n/2} = p_n(\omega_n^{n/2}) = p_n(-1) = p_n(1) (point (a)).

(A0,…,An/2−1,An/2,An/2+1,…,An−1)=(pn(1),0,…,0,pn(−1),0,…,0).(A_0, \dots, A_{n/2-1}, A_{n/2}, A_{n/2+1}, \dots, A_{n-1}) = (p_n(1), 0, \dots, 0, p_n(-1), 0, \dots, 0).

DFT_P(n)
  (u, u) <- A0'(n)                                    { u = p(1) = p(-1) }
  for h <- 0 to n-1 do A[h] <- 0                      { assignments, not arithmetic }
  A[0] <- u;  A[n/2] <- u
  return A

Cost. All the arithmetic is in the call to A0′A_0': Tc(n)=32n−5=O(n)T_c(n) = \frac 32 n - 5 = O(n). Filling the nn entries costs Θ(n)\Theta(n) assignments, which are not arithmetic operations (the output has nn components anyway, so no algorithm can be faster than linear in the size of the output). This is far below the Θ(nlog⁡n)\Theta(n \log n) of the general FFT (Fast Fourier TransformThe FFT computes the DFT_n (n a power of two) by divide and conquer: split the coefficients by index parity, p(x) = p0(x^2) + x p1(x^2), and by the halving lemma two DFTs of order n/2 suffice; y_i = y0_i + w^i y1_i and y_{i+n/2} = y0_i - w^i y1_i. The recurrence T(n) = 2T(n/2) + 2n with T(1) = 0 has exact solution 2 n log2 n. The inverse has matrix F^{-1}{ij} = w^{-ij}/n and is obtained with the FFT on negative powers (then dividing by n outside the recursion) or with a reversed DFT_n(y). LIN_CONV multiplies two polynomials with n coefficients in 12 n log2(2n) + 4n operations.Fast Fourier Transform →) because the vector is highly structured; the same idea appears for sparse inputs in Notable Transforms and Polynomial ExponentiationSome vector families have a known or cheaper DFT than the general FFT: the constant vector (a,...,a) has DFT (na,0,...,0), the vector (a,0,...,0) has DFT (a,...,a); an (n,k)-sparse vector has a DFT made of k copies of the DFT{n/k} of its components at multiples of k, and SPARSE_FFT costs 2 (n/k) log2(n/k) with recurrence T(n,k) = T(n/2,k/2). For the power p(x)^k of a polynomial with n coefficients: either repeated squaring with LIN_CONV, O(nk log(nk)), or evaluate p with the FFT on h = 2^ceil(log2((n-1)k+1)) points, raise each value to the k, and invert, Theta(nk log(nk)).Notable Transforms and Polynomial Exponentiation → (the official solution notes that a\mathbf{a} is also (n,n/2)(n, n/2)-sparse, since ai=0a_i = 0 for odd ii: this is an alternative route).

The official solution writes Tc(n)=Ta(n)=3n−8T_c(n) = T_a(n) = 3n - 8, the cost of one evaluation of the plain product (legitimate: a single evaluation suffices because pn(1)=pn(−1)p_n(1) = p_n(-1)); with the product over x2−ωn2hx^2 - \omega_n^{2h} the constant drops to 32\frac 32. Both are O(n)O(n).

Point (d): the coefficients. The coefficient vector is the inverse DFT of (A0,…,An−1)(A_0, \dots, A_{n-1}) (Fast Fourier TransformThe FFT computes the DFT_n (n a power of two) by divide and conquer: split the coefficients by index parity, p(x) = p0(x^2) + x p1(x^2), and by the halving lemma two DFTs of order n/2 suffice; y_i = y0_i + w^i y1_i and y_{i+n/2} = y0_i - w^i y1_i. The recurrence T(n) = 2T(n/2) + 2n with T(1) = 0 has exact solution 2 n log2 n. The inverse has matrix F^{-1}_{ij} = w^{-ij}/n and is obtained with the FFT on negative powers (then dividing by n outside the recursion) or with a reversed DFT_n(y). LIN_CONV multiplies two polynomials with n coefficients in 12 n log2(2n) + 4n operations.Fast Fourier Transform →: [Fn−1]ij=1nωn−ij[F_n^{-1}]_{ij} = \frac 1n \omega_n^{-ij}). Only A0A_0 and An/2A_{n/2} are nonzero, so only two terms survive:

ai=1n∑h=0n−1Ah ωn−ih=1n(pn(1) ωn0+pn(−1) (ωn−n/2)i)=1n(pn(1)+(−1)i pn(−1)).a_i = \frac 1n \sum_{h=0}^{n-1} A_h\,\omega_n^{-ih} = \frac 1n\Bigl(p_n(1)\,\omega_n^{0} + p_n(-1)\,\bigl(\omega_n^{-n/2}\bigr)^i\Bigr) = \frac 1n\bigl(p_n(1) + (-1)^i\,p_n(-1)\bigr).

(Here ωn−n/2=(ωnn/2)−1=(−1)−1=−1\omega_n^{-n/2} = (\omega_n^{n/2})^{-1} = (-1)^{-1} = -1.) With pn(−1)=pn(1)p_n(-1) = p_n(1):

ai={2n pn(1)i even,0i odd.a_i = \begin{cases} \frac 2n\,p_n(1) & i \text{ even,} \\ 0 & i \text{ odd.} \end{cases}

The vanishing of the odd coefficients also follows directly from the fact that pnp_n is an even polynomial (point (a)): pn(x)=pn(−x)p_n(x) = p_n(-x) forces ai=(−1)iaia_i = (-1)^i a_i.

COEFF(n)
  (u, u) <- A0'(n)                                    { u = p_n(1) }
  c <- u / (n/2)                                      { 1 division: c = (2/n) p_n(1) }
  for i <- 0 to n-1 do
    if i is even then a[i] <- c else a[i] <- 0
  return a

Correctness. By the inverse DFT theorem the computed vector is the unique coefficient vector with DFT equal to the one found in point (c); the chain of equalities above shows that it is exactly COEFF's output (even index: 1n(p(1)+p(−1))=2np(1)\frac 1n(p(1) + p(-1)) = \frac 2n p(1); odd index: 1n(p(1)−p(−1))=0\frac 1n(p(1) - p(-1)) = 0).

Cost. Td(n)=Ta′(n)+1=32n−4=O(n)T_d(n) = T_a'(n) + 1 = \frac 32 n - 4 = O(n) arithmetic operations (plus nn assignments). If the plain product is used, Td(n)=3n−8+O(1)=O(n)T_d(n) = 3n - 8 + O(1) = O(n).

Closed form (not required, but it closes the exercise). The nn roots of unity are the roots of xn−1x^n - 1, a monic polynomial, so xn−1=∏h=0n−1(x−ωnh)=(x−1)(x+1) pn(x)x^n - 1 = \prod_{h=0}^{n-1}(x - \omega_n^h) = (x - 1)(x + 1)\,p_n(x). Hence

pn(x)=xn−1x2−1=1+x2+x4+⋯+xn−2p_n(x) = \frac{x^n - 1}{x^2 - 1} = 1 + x^2 + x^4 + \dots + x^{n-2}

(a geometric sum in x2x^2 with n/2n/2 terms). So pn(1)=n2p_n(1) = \frac n2, c=1c = 1 and ai=1a_i = 1 for even i≤n−2i \le n-2, ai=0a_i = 0 otherwise. Examples: n=4n = 4: (1,0,1,0)(1, 0, 1, 0) and DFT (2,0,2,0)(2, 0, 2, 0); n=6n = 6: (1,0,1,0,1,0)(1, 0, 1, 0, 1, 0) and DFT (3,0,0,3,0,0)(3, 0, 0, 3, 0, 0).

Typical errors: forgetting the division by nn of the inverse DFT; using ωn\omega_n instead of ωn−1\omega_n^{-1} (here harmless, because (−1)i(-1)^i is real, but wrong in general); concluding that all the coefficients with even index are pn(1)p_n(1) instead of 2n pn(1)\frac 2n\,p_n(1); applying the full FFT, which is correct but costs Θ(nlog⁡n)\Theta(n\log n) and needs nn to be a power of two (here nn is only even).

Misprints in the official solution: in point (d) the official text calls the complexity Te(n)T_e(n) instead of Td(n)T_d(n), and refers to "the expression for the AhA_h's from part 1.(d)" where it means part 1.(c). The results are unaffected.

Checks performed. (Python, instrumented counters for the arithmetic operations.) For every even nn from 44 to 6060 (including nn not a power of two, e.g. 6,10,12,34,446, 10, 12, 34, 44): the coefficients of pnp_n (product of the factors for n≤30n \le 30) equal 11 for even i≤n−2i \le n - 2 and 00 otherwise; the values pn(ωnh)p_n(\omega_n^h) computed from the definition give the vector (n2,0,…,n2,0,…,0)(\frac n2, 0, \dots, \frac n2, 0, \dots, 0), i.e. the DFT of point (c); A0\mathcal{A}_0 returns pn(1)=pn(−1)=n2p_n(1) = p_n(-1) = \frac n2 with exactly 5n−135n - 13 operations, A0′A_0' with exactly 32n−5\frac 32 n - 5, COEFF reproduces the coefficient vector with exactly 32n−4\frac 32 n - 4; the single plain evaluation costs 3n−83n - 8. The identity pn(x)=1+x2+⋯+xn−2p_n(x) = 1 + x^2 + \dots + x^{n-2} was checked numerically for n=4,6,8,10,12n = 4, 6, 8, 10, 12 at a complex test point, and n≥2∣⟨n⟩∣−1n \ge 2^{\lvert\langle n\rangle\rvert - 1} for many values of nn.

Exercise 2 (points 16)

Given a vector x\mathbf{x} of n≥1n \ge 1 positive integers, consider the following recurrence, defined for 1≤i,j≤n1 \le i, j \le n:

t(i,j)={xji=1,xn+1−ij=n,max⁡{t(i−1,j+1), t(i−1,j)+t(i,j+1)}(1<i≤n)∧(1≤j<n).t(i, j) = \begin{cases} x_j & i = 1, \\ x_{n+1-i} & j = n, \\ \max\{t(i-1, j+1),\ t(i-1, j) + t(i, j+1)\} & (1 < i \le n) \wedge (1 \le j < n). \end{cases}

2.(a) [10 points] Give the pseudocode of the pair of procedures INIT_T(x)(\mathbf{x}) and REC_T(x,i,j)(\mathbf{x}, i, j) that, given the vector x\mathbf{x} as input, perform the memoized computation of t(n,1)t(n, 1).

2.(b) [6 points] Determine the exact number T(n)T(n) of comparisons and sums between integers performed by the memoized algorithm.

Show the result
  • (a) INIT_T returns x1x_1 if n=1n = 1; otherwise it fills the first row T[1,j]=xjT[1, j] = x_j and the last column T[n+1−i,n]=xiT[n+1-i, n] = x_i with the base values, sets the other entries T[i,j]T[i, j] (2≤i≤n2 \le i \le n, 1≤j≤n−11 \le j \le n-1) to the sentinel 00 (valid because every t(i,j)>0t(i,j) > 0) and returns REC_T(x,n,1)(\mathbf{x}, n, 1). REC_T(x,i,j)(\mathbf{x}, i, j), if T[i,j]=0T[i,j] = 0, computes s=s = REC_T(i−1,j)+(i-1, j) + REC_T(i,j+1)(i, j+1) and m=m = REC_T(i−1,j+1)(i-1, j+1), stores max⁡{m,s}\max\{m, s\} in T[i,j]T[i,j], and returns T[i,j]T[i,j].
  • (b) T(n)=2(n−1)2T(n) = 2(n-1)^2 (one sum and one comparison for each of the (n−1)2(n-1)^2 cells (i,j)(i,j) with 2≤i≤n2 \le i \le n and 1≤j≤n−11 \le j \le n-1).
Show the full solution

What the recurrence asks. The cell (i,j)(i, j) depends on three other cells: (i−1,j)(i-1, j) above, (i,j+1)(i, j+1) to the right and (i−1,j+1)(i-1, j+1) above-right. The first row and the last column are base cases (they are given by x\mathbf{x}; the corner (1,n)(1, n) is consistent: both rules give xnx_n). The goal t(n,1)t(n, 1) is the bottom-left corner. Example, x=(3,1,4,2)\mathbf{x} = (3, 1, 4, 2), n=4n = 4; the table (row ii, column jj) is

j=1j=1 j=2j=2 j=3j=3 j=4j=4
i=1i=1 3 1 4 2
i=2i=2 12 9 8 4
i=3i=3 30 18 9 1
i=4i=4 60 30 12 3

For instance t(2,3)=max⁡{t(1,4), t(1,3)+t(2,4)}=max⁡{2, 4+4}=8t(2,3) = \max\{t(1,4),\ t(1,3) + t(2,4)\} = \max\{2,\ 4 + 4\} = 8 and t(4,1)=max⁡{t(3,2), t(3,1)+t(4,2)}=max⁡{18, 30+30}=60t(4,1) = \max\{t(3,2),\ t(3,1) + t(4,2)\} = \max\{18,\ 30 + 30\} = 60. The last column is xn+1−ix_{n+1-i}: t(2,4)=x3=4t(2,4) = x_3 = 4, t(3,4)=x2=1t(3,4) = x_2 = 1, t(4,4)=x1=3t(4,4) = x_1 = 3.

Point (a): the memoized code (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 →, and the worked pattern in Memoization of Triangular Recurrencesfor the recurrence M(i,j) = M(i+1,j-1) M(i+1,j) M(i,j-1) on the triangle 1 <= i <= j <= n, standard memoized code performs exactly (n-1)(n-2) multiplications, but all entries on a diagonal are equal, which cuts the work to 2(n-2) multiplications; look for regularities before filling a table.Memoization of Triangular Recurrences →). Two design decisions have to be justified.

  1. The sentinel. The table must tell "already computed" from "still to compute". Since all components of x\mathbf{x} are positive, every t(i,j)t(i, j) is positive (induction: base values are components of x\mathbf{x}; a sum of positive numbers and a maximum of positive numbers are positive). So 00 is never a possible value and is a valid sentinel.
  2. Who fills what. INIT_T handles the base cases (first row and last column, written directly from x\mathbf{x}) and sets all the other cells to the sentinel, then makes the only call that starts the recursion. REC_T computes the non-base cells.
INIT_T(x)
  n <- x.length
  if n = 1 then return x[1]                           { t(n,1) = t(1,1) = x_1: base case }
  for i <- 1 to n do
    T[1, i] <- x[i]                                   { first row: t(1,j) = x_j }
    T[n+1-i, n] <- x[i]                               { last column: t(k,n) = x_{n+1-k}, k = n+1-i }
  for i <- 2 to n do
    for j <- 1 to n-1 do T[i, j] <- 0                 { cells still to be computed }
  return REC_T(x, n, 1)

REC_T(x, i, j)
  if T[i, j] = 0 then                                 { only non-base cells can be 0 }
    s <- REC_T(x, i-1, j) + REC_T(x, i, j+1)          { 1 sum }
    m <- REC_T(x, i-1, j+1)
    if m >= s then T[i, j] <- m                       { 1 comparison }
    else T[i, j] <- s
  return T[i, j]

Correctness. Consider the quantity μ(i,j)=(i−1)+(n−j)≥0\mu(i,j) = (i - 1) + (n - j) \ge 0, which is 00 only for the cell (1,n)(1, n). The three calls from (i,j)(i, j) go to cells with μ\mu smaller by 11, 11 and 22 respectively, and they are legitimate cells of the table: i−1≥1i - 1 \ge 1 because i≥2i \ge 2 and j+1≤nj + 1 \le n because j≤n−1j \le n - 1. Hence the recursion terminates, and by induction on μ\mu REC_T(x,i,j)(\mathbf{x}, i, j) returns t(i,j)t(i, j) and leaves T[i,j]=t(i,j)T[i,j] = t(i,j):

  • if the cell is a base cell, T[i,j]T[i,j] already holds the right value and is non-zero, so it is returned directly;
  • if it is a non-base cell with T[i,j]=0T[i,j] = 0, the three recursive calls return the right values by induction (smaller μ\mu), so the stored value is max⁡{t(i−1,j+1), t(i−1,j)+t(i,j+1)}\max\{t(i-1,j+1),\ t(i-1,j) + t(i,j+1)\}, which is t(i,j)t(i,j) by definition;
  • if it is a non-base cell with T[i,j]≠0T[i,j] \ne 0, then it was already computed correctly earlier, by the same argument.

As in the official solution, one can observe that an entry of TT is read in the right-hand side of an assignment only after the corresponding call has completed, so it holds its final value. The case n=1n = 1 is solved directly by INIT_T (the table is not needed).

Typical errors: using 00 as a sentinel without noticing that positivity is what makes it legal (with arbitrary integers the code would recompute cells, or return wrong values); swapping the indices of the last column (T[i,n]←xiT[i, n] \leftarrow x_i instead of xn+1−ix_{n+1-i}); forgetting to initialize the non-base cells to the sentinel; calling REC_T(x,1,n)(\mathbf{x}, 1, n) instead of REC_T(x,n,1)(\mathbf{x}, n, 1); writing the plain recursion without a table (see below, it takes exponential time).

Point (b): exact count. As in Memoization of Triangular Recurrencesfor the recurrence M(i,j) = M(i+1,j-1) M(i+1,j) M(i,j-1) on the triangle 1 <= i <= j <= n, standard memoized code performs exactly (n-1)(n-2) multiplications, but all entries on a diagonal are equal, which cuts the work to 2(n-2) multiplications; look for regularities before filling a table.Memoization of Triangular Recurrences →, count the internal nodes of the call tree, not a recurrence on the operations, because a recurrence describes the purely functional execution in which every node is expanded, whereas here a call on a filled cell is a table read (Divide and Conquer - Paradigm and Call TreeA divide-and-conquer algorithm comes from a substructure property (threshold n0, Divide and Conquer functions) with strictly smaller subinstances; the call tree describes the execution, and D&C has no memory (equal subinstances are recomputed). Complete example: MAX of an array, with correctness by induction, the recurrence, and the exact count n-1 via the theorem on full binary trees.Divide and Conquer - Paradigm and Call Tree →).

  • INIT_T performs only assignments: no sum and no comparison.
  • In the tree of REC_T(x,n,1)(\mathbf{x}, n, 1), a call that finds T[i,j]≠0T[i,j] \ne 0 is a leaf with no work (base cell or already computed cell). A call that finds 00 is an internal node that performs one sum and one comparison: work 22.
  • Which cells are internal nodes, and how many times? The non-base cells are exactly the pairs (i,j)(i, j) with 2≤i≤n2 \le i \le n and 1≤j≤n−11 \le j \le n - 1: that is (n−1)2(n-1)^2 cells. Each of them is reached: from (n,1)(n, 1) one gets to (i,j)(i, j) with j−1j - 1 steps of type (i,j)→(i,j+1)(i, j) \to (i, j+1) (reaching (n,j)(n, j)) followed by n−in - i steps of type (i,j)→(i−1,j)(i, j) \to (i-1, j); all the intermediate cells (i′,j′)(i', j') have i′≥i≥2i' \ge i \ge 2 and j′≤j≤n−1j' \le j \le n-1, so they are non-base and are expanded on the way. Each is an internal node exactly once: after the first expansion T[i,j]=t(i,j)>0T[i,j] = t(i,j) > 0 and every later call is a read.

T(n)=∑i=2n ∑j=1n−12=2(n−1)2.T(n) = \sum_{i=2}^{n}\ \sum_{j=1}^{n-1} 2 = 2(n-1)^2.

Check, n=4n = 4: 2⋅9=182 \cdot 9 = 18 operations (9 sums and 9 comparisons); n=1n = 1: 00. The algorithm is Θ(n2)\Theta(n^2). Without the table the same recurrence takes exponential time: the call tree of the plain recursion has 1,4,19,94,481,2524,13483,729581, 4, 19, 94, 481, 2524, 13483, 72958 nodes for n=1,…,8n = 1, \dots, 8, whereas the memoized code does 2(n−1)2=982(n-1)^2 = 98 operations for n=8n = 8.

Typical errors: counting only the sums, or only the comparisons (the text asks for both, 22 per cell); counting all n2n^2 cells (the base cells do no work: 2n22n^2 instead of 2(n−1)22(n-1)^2); using the operation recurrence T(n)=3T(…)+2T(n) = 3T(\ldots) + 2 of the unmemoized tree; counting (i,j)(i,j) with i=1i = 1 or j=nj = n.

An extra observation (optional, from the official solution). The table is symmetric with respect to the antidiagonal: t(i,j)=t(n+1−j, n+1−i)t(i, j) = t(n + 1 - j,\ n + 1 - i). Proof. Let s(i,j)=t(n+1−j,n+1−i)s(i, j) = t(n+1-j, n+1-i). Then s(1,j)=t(n+1−j,n)=xn+1−(n+1−j)=xjs(1, j) = t(n+1-j, n) = x_{n+1-(n+1-j)} = x_j and s(i,n)=t(1,n+1−i)=xn+1−is(i, n) = t(1, n+1-i) = x_{n+1-i}: the same base cases as tt. For 1<i≤n1 < i \le n and 1≤j<n1 \le j < n, unfolding t(a,b)t(a, b) with a=n+1−j>1a = n+1-j > 1 and b=n+1−i<nb = n+1-i < n and rewriting each term as a value of ss gives s(i,j)=max⁡{s(i−1,j+1), s(i−1,j)+s(i,j+1)}s(i,j) = \max\{s(i-1, j+1),\ s(i-1, j) + s(i, j+1)\}: the same recurrence. Two functions with the same base cases and the same recurrence coincide, so s=ts = t. (In the example above t(2,1)=12=t(4,3)t(2,1) = 12 = t(4,3) and t(3,1)=30=t(4,2)t(3,1) = 30 = t(4,2).) It is therefore enough to compute the cells with i+j≤n+1i + j \le n + 1 and to replace any other cell (i,j)(i,j) by its mirror image (n+1−j,n+1−i)(n+1-j, n+1-i) before reading or writing the table. Now only n(n−1)/2n(n-1)/2 cells are non-base, and the count becomes T′(n)=n(n−1)T'(n) = n(n-1) instead of 2(n−1)22(n-1)^2: the leading constant of n2n^2 halves (n(n−1)n(n-1) against 2(n−1)22(n-1)^2: 1212 against 1818 for n=4n = 4). This was left as a challenge in the official solution; it was implemented and the count checked.

Checks performed. (Python.) The memoized code, written exactly as the pseudocode above, was compared with the direct (unmemoized) evaluation of the recurrence on 6060 random vectors of positive integers for every nn from 11 to 99: the values t(n,1)t(n,1) coincide on all of them, and the counted operations (sums plus comparisons) are exactly 2(n−1)22(n-1)^2 for every case. The example x=(3,1,4,2)\mathbf{x} = (3,1,4,2) gives the table above, t(4,1)=60t(4,1) = 60 and 1818 operations. The antidiagonal symmetry was verified on 4040 random vectors for each nn from 22 to 88, and the version with mirrored cells returns the same values with exactly n(n−1)n(n-1) operations for n=1,…,9n = 1, \dots, 9. The sizes of the unmemoized call tree quoted above come from counting the nodes of the plain recursion. No misprint was found in the official solution of this exercise.

Self-assessment grid
Exercise Points What must be there for full marks Typical errors
1.(a) 4 Algorithm that evaluates the product at ±1\pm 1 with the factor h=n/2h = n/2 excluded; exact or Θ(n)\Theta(n) count: 5n−135n - 13 (shared powers), or 32n+O(1)\frac 32 n + O(1) using pn(x)=∏(x2−ωn2h)p_n(x) = \prod (x^2 - \omega_n^{2h}) Including the missing factor; counting the powers of ωn\omega_n twice; using ∑ai\sum a_i (the aia_i are unknown); claiming Θ(nlog⁡n)\Theta(n\log n)
1.(b) 3 Size of the input is ⌊log⁡2n⌋+1\lfloor\log_2 n\rfloor + 1 bits; Ta(n)≥2size−1T_a(n) \ge 2^{\text{size} - 1} hence exponential, not polynomial Answering "linear, so polynomial" (linear in the value, not in the size); wrong inequality n≥2sizen \ge 2^{\text{size}}
1.(c) 5 DFT == values at ωnh\omega_n^h; zeros except positions 00 and n/2n/2; A0=pn(1)A_0 = p_n(1), An/2=pn(−1)=pn(1)A_{n/2} = p_n(-1) = p_n(1); cost O(n)O(n) Running the general FFT; forgetting pn(−1)=pn(1)p_n(-1) = p_n(1) (two evaluations: still O(n)O(n), but unjustified); putting the second nonzero entry at the wrong position
1.(d) 5 Inverse DFT with only two terms, (−1)i(-1)^i from ωn−in/2\omega_n^{-in/2}, so ai=2npn(1)a_i = \frac 2n p_n(1) (even ii), 00 (odd ii); proof of correctness; cost O(n)O(n) Missing the 1n\frac 1n; ai=pn(1)a_i = p_n(1) for even ii; no proof (correctness must be argued from the inverse DFT theorem or from pnp_n being even)
2.(a) 10 INIT_T with base row, base column (index n+1−in+1-i), sentinel 00 justified by positivity, call on (n,1)(n,1); REC_T with the three calls, one sum, one comparison, table assignment; termination and correctness argued Wrong indices in the last column; sentinel not justified; missing table or initialization; recursion without memoization
2.(b) 6 Internal nodes == the (n−1)2(n-1)^2 non-base cells, each once; work 22 per node (sum and comparison); T(n)=2(n−1)2T(n) = 2(n-1)^2 2n22n^2 (base cells counted); only sums or only comparisons; using the recurrence of the unmemoized tree