Salta al contenuto
Note per Studenti Algorithms in Engineering - Mock Exam 3 Report

Algorithms in Engineering - Mock Exam 3

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

In questa pagina 13

Mock exam built on the model of the written exams of the equivalent Italian course (Algoritmi per l'Ingegneria, Ing. Informatica UniPD, taught in Italian at Padova); it is a simulation, not a real paper. The official exam has two parts: a first part of closed-answer questions on the basics of the course and a second part of problems (design and analysis of an algorithm, proof of a property). The first parts of the real papers are not public: from the official sample papers only the format is known (about ten short questions, one answer each), so the number of questions, the points and the duration are a proposal of this mock: Part 1 has 10 quizzes of 1 point each (four options, exactly one correct, only the letter is written); Part 2 has 2 exercises of 11 points each; total 32 points, 2 hours and 30 minutes, no material allowed. As in the real exam, the second part is not worth correcting if the first one is very poor.

The topics are: recurrences and the Master Theorem, structured matrices, fast exponentiation, DFT and FFT, dynamic programming (LCS, longest palindromic subsequence, matrix chain), greedy scheduling and Huffman codes, hybrid algorithms and parametric induction. Questions and exercises are new (they are not copies of the real exams nor of the other mocks). All numerical results were checked by running the algorithms: the hybrid product against the definition on random inputs with an exact count of the scalar operations, Huffman on Fibonacci frequencies with every tie-breaking for n=3,…,25n=3,\dots,25, the matrix chain against all parenthesizations, and every quiz against a direct computation.

Quiz 1

Which of the following recurrences, with T(1)=1T(1) = 1 and nn a power of two, has solution Θ(n2log⁡n)\Theta(n^2 \log n)?

  • (a) T(n)=2 T(n/2)+n2T(n) = 2\,T(n/2) + n^2
  • (b) T(n)=8 T(n/2)+n2T(n) = 8\,T(n/2) + n^2
  • (c) T(n)=4 T(n/2)+n2T(n) = 4\,T(n/2) + n^2
  • (d) T(n)=4 T(n/2)+nT(n) = 4\,T(n/2) + n
Show the result

(c)

Show the full solution

Apply 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 →: compare w(n)w(n) with the threshold nlog⁡ban^{\log_b a}.

  • (a) a=2a=2, b=2b=2: threshold nn, w=n2=Ω(n1+ε)w = n^2 = \Omega(n^{1+\varepsilon}) with regularity (2(n/2)2=12n22(n/2)^2 = \tfrac12 n^2): Case 3, Θ(n2)\Theta(n^2).
  • (b) a=8a=8: threshold n3n^3, w=n2=O(n3−ε)w = n^2 = O(n^{3-\varepsilon}): Case 1, Θ(n3)\Theta(n^3).
  • (c) a=4a=4: threshold n2n^2, w=n2=Θ(n2)w = n^2 = \Theta(n^2): Case 2, Θ(n2log⁡n)\Theta(n^2 \log n) (every level of the tree costs n2n^2, the levels are log⁡2n\log_2 n).
  • (d) a=4a=4: threshold n2n^2, w=n=O(n2−ε)w = n = O(n^{2-\varepsilon}): Case 1, Θ(n2)\Theta(n^2).

Typical mistake: answering (a) or (d) because "n2n^2 appears".

Quiz 2

Let AA and BB be supercirculant matrices of order 44 with first rows r(A)=(2,1,0,3)r(A) = (2, 1, 0, 3) and r(B)=(1,4,2,1)r(B) = (1, 4, 2, 1). What is the first row of ABAB?

  • (a) (2,4,0,3)(2, 4, 0, 3)
  • (b) (9,15,17,7)(9, 15, 17, 7)
  • (c) (17,7,9,15)(17, 7, 9, 15)
  • (d) (15,15,11,7)(15, 15, 11, 7)
Show the result

(b)

Show the full solution

Use the recursive product of Supercirculant and Circulant MatricesSupercirculant matrices (alternating recursive quadrants) have only n distinct values: sum in n operations, product in 4n log2 n + n with the trick (A1+A2)(B1+B2) and (A1-A2)(B1-B2) instead of 4 calls (Theta(n^2)); their eigenvalues are the Hadamard transform of the first row (FHT, n log2 n). Circulant matrices are polynomials in the rotation matrix R, their product is the cyclic convolution of the first rows, and the Fourier matrix diagonalizes them: product in O(n log n) with the FFT.Supercirculant and Circulant Matrices →: with A=(A1A2A2A1)A = \begin{pmatrix} A_1 & A_2 \\ A_2 & A_1 \end{pmatrix}, B=(B1B2B2B1)B = \begin{pmatrix} B_1 & B_2 \\ B_2 & B_1 \end{pmatrix} the product is supercirculant with blocks C1=A1B1+A2B2C_1 = A_1B_1 + A_2B_2 and C2=A1B2+A2B1C_2 = A_1B_2 + A_2B_1, and U=(A1+A2)(B1+B2),V=(A1−A2)(B1−B2),C1=12(U+V),C2=12(U−V).U = (A_1 + A_2)(B_1 + B_2),\quad V = (A_1 - A_2)(B_1 - B_2),\quad C_1 = \tfrac12 (U + V),\quad C_2 = \tfrac12 (U - V). Here A1↔(2,1)A_1 \leftrightarrow (2, 1), A2↔(0,3)A_2 \leftrightarrow (0, 3), B1↔(1,4)B_1 \leftrightarrow (1, 4), B2↔(2,1)B_2 \leftrightarrow (2, 1).

  • A1+A2↔(2,4)A_1 + A_2 \leftrightarrow (2, 4), B1+B2↔(3,5)B_1 + B_2 \leftrightarrow (3, 5). For order 22 the same formulas on scalars: sums 66 and 88, differences −2-2 and −2-2, products 4848 and 44, so U=(48+42,48−42)=(26,22)U = \bigl(\tfrac{48+4}{2}, \tfrac{48-4}{2}\bigr) = (26, 22).
  • A1−A2↔(2,−2)A_1 - A_2 \leftrightarrow (2, -2), B1−B2↔(−1,3)B_1 - B_2 \leftrightarrow (-1, 3): sums 00 and 22, differences 44 and −4-4, products 00 and −16-16, so V=(−8,8)V = (-8, 8).
  • C1=12(U+V)=(9,15)C_1 = \tfrac12 (U + V) = (9, 15), C2=12(U−V)=(17,7)C_2 = \tfrac12 (U - V) = (17, 7).

The first row of ABAB is (C1∣C2)=(9,15,17,7)(C_1 \mid C_2) = (9, 15, 17, 7). Check by definition: entry (0,0)(0,0) is r(A)⋅r(B)=2+4+0+3=9r(A) \cdot r(B) = 2 + 4 + 0 + 3 = 9.

Wrong options: (a) is the componentwise product of the rows; (c) swaps the two halves C1,C2C_1, C_2; (d) is the cyclic convolution of the rows, which is the product of circulant (not supercirculant) matrices.

Quiz 3

The Fibonacci matrix M=(1110)M = \begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix} is raised to the power 4545 with the recursive algorithm POWER (repeated squaring). Each product of two 2×22 \times 2 matrices is done with the iterative row-by-column algorithm. How many products between scalars are executed in total?

  • (a) 88
  • (b) 4040
  • (c) 352352
  • (d) 6464
Show the result

(d)

Show the full solution

In Fast Exponentiation by Repeated SquaringPower x^n with an associative operator having an identity, cost = number of applications of the operator; STUPID_POWER costs n-1, the substructure x^n = (x^floor(n/2))^2 (times x for odd n) gives POWER with T(n) = floor(log2 n) + (number of 1 bits of n) - 1, Theta(log n) (worst case 2 floor(log2 n), n = 2^t - 1); iterative versions POWER2 and POWER3 from the binary representation of n; example with the Fibonacci matrix; unit-cost model valid only if the operator does not make operands grow.Fast Exponentiation by Repeated Squaring → the number of applications of the operator is ⌊log⁡2n⌋+ν(n)−1\lfloor \log_2 n \rfloor + \nu(n) - 1, with ν(n)\nu(n) the number of 11 bits. For n=45=1011012n = 45 = 101101_2: ⌊log⁡245⌋=5\lfloor \log_2 45 \rfloor = 5, ν(45)=4\nu(45) = 4, so 5+4−1=85 + 4 - 1 = 8 matrix products (the chain is 45→22→11→5→2→145 \to 22 \to 11 \to 5 \to 2 \to 1 with costs 2,1,2,2,12, 1, 2, 2, 1). The product of two 2×22 \times 2 matrices has 23=82^3 = 8 scalar products (Matrix Multiplication - Iterative and Recursive Block ProductCost model on n x n matrices (scalar operations, size n or N = n^2); sum in n^2 operations (optimal); iterative row-by-column product with exact cost 2n^3 - n^2; the fractal property of the block product proved on indices; recursive algorithm RMUL with T(n) = 8T(n/2) + n^2, Theta(n^3) and exact solution 2n^3 - n^2: it performs the same operations as the iterative one.Matrix Multiplication - Iterative and Recursive Block Product →): 8⋅8=648 \cdot 8 = 64.

Wrong options: (a) counts matrix products, not scalar ones; (b) 5⋅85 \cdot 8 forgets the multiplications by MM for the odd exponents; (c) 44⋅844 \cdot 8 is the cost of the definition-based algorithm. As a check, the top-right entry of M45M^{45} is F45=1 134 903 170F_{45} = 1\,134\,903\,170 (with F1=F2=1F_1 = F_2 = 1).

Quiz 4

What is DFT8(x)\mathrm{DFT}_8(\mathbf{x}) for x=(3,0,0,0,5,0,0,0)\mathbf{x} = (3, 0, 0, 0, 5, 0, 0, 0)?

  • (a) (8,−2,8,−2,8,−2,8,−2)(8, -2, 8, -2, 8, -2, 8, -2)
  • (b) (8,0,0,0,−2,0,0,0)(8, 0, 0, 0, -2, 0, 0, 0)
  • (c) (8,8,0,0,−2,−2,0,0)(8, 8, 0, 0, -2, -2, 0, 0)
  • (d) (4,−1,4,−1,4,−1,4,−1)(4, -1, 4, -1, 4, -1, 4, -1)
Show the result

(a)

Show the full solution

The vector is (8,4)(8, 4)-sparse: xi=0x_i = 0 whenever ii is not a multiple of 44 (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 →). Its transform is 44 copies of DFT8/4(x0,x4)=DFT2(3,5)=(3+5,3−5)=(8,−2)\mathrm{DFT}_{8/4}(x_0, x_4) = \mathrm{DFT}_2(3, 5) = (3 + 5, 3 - 5) = (8, -2), because ω2=−1\omega_2 = -1. Directly: yi=3+5 ω84i=3+5(−1)iy_i = 3 + 5\,\omega_8^{4i} = 3 + 5(-1)^i, which is 88 for even ii and −2-2 for odd ii (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 →).

Wrong options: (b) puts the nonzero values at frequencies 00 and 44 only, as if the DFT were as sparse as the input; (c) is not the transform of this vector (a nonzero value at index 44 gives ω84i=(−1)i\omega_8^{4i} = (-1)^i in every component, never zeros); (d) is half of the correct vector, a mistake of dividing by 22.

Quiz 5

The polynomial p(x)p(x) has 99 coefficients and one wants all the coefficients of p(x)12p(x)^{12} with the FFT (evaluate on the hh roots of unity, raise each value to the 1212-th power, invert). What is the minimum order hh of the DFT (a power of two) that can be used?

  • (a) 6464
  • (b) 9696
  • (c) 128128
  • (d) 256256
Show the result

(c)

Show the full solution

pp has degree at most 88, so p12p^{12} has degree at most 8⋅12=968 \cdot 12 = 96, i.e. (n−1)k+1=97(n-1)k + 1 = 97 coefficients. The values on hh points determine a polynomial with at most hh coefficients, so one needs h≥97h \ge 97; the first power of two is h=128h = 128 (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 →). 6464 is too small (97>6497 > 64: wrap-around would corrupt the coefficients); 9696 is not a power of two; 256256 works but is not minimal.

Quiz 6

The inverse transform INV_FFT is computed as the course does (the FFT with ωn−1\omega_n^{-1} on the vector, then a division of every component by nn outside the recursion). How many operations between complex numbers does it perform for order n=128n = 128?

  • (a) 17921792
  • (b) 19201920
  • (c) 20482048
  • (d) 13441344
Show the result

(b)

Show the full solution

The FFT costs 2nlog⁡2n2n \log_2 n complex operations (four per butterfly, n/2n/2 butterflies per level; 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 →): 2⋅128⋅7=17922 \cdot 128 \cdot 7 = 1792. The final division of the n=128n = 128 components adds 128128: TINV(n)=2nlog⁡2n+n=1920T_{\text{INV}}(n) = 2n \log_2 n + n = 1920.

Wrong options: (a) forgets the divisions; (d) is 32nlog⁡2n\frac32 n \log_2 n, the cost with precomputed powers of ωn\omega_n and no divisions; (c) is 2nlog⁡2n+2n2n \log_2 n + 2n rounded to a power of two, with no derivation behind it.

Quiz 7

Let X=⟨e,n,g,i,n,e,e,r⟩X = \langle e, n, g, i, n, e, e, r \rangle and Y=⟨g,e,n,e,r,a,t,i,o,n⟩Y = \langle g, e, n, e, r, a, t, i, o, n \rangle. What is the length of a longest common subsequence of XX and YY?

  • (a) 44
  • (b) 55
  • (c) 33
  • (d) 66
Show the result

(a)

Show the full solution

Fill the table ℓ(i,j)\ell(i, j) of 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 →: ℓ=0\ell = 0 on row and column 00, ℓ(i,j)=1+ℓ(i−1,j−1)\ell(i,j) = 1 + \ell(i-1, j-1) if xi=yjx_i = y_j, otherwise max⁡{ℓ(i−1,j),ℓ(i,j−1)}\max\{\ell(i-1, j), \ell(i, j-1)\}.

ε\varepsilon g e n e r a t i o n
ε\varepsilon 0 0 0 0 0 0 0 0 0 0 0
e 0 0 1 1 1 1 1 1 1 1 1
n 0 0 1 2 2 2 2 2 2 2 2
g 0 1 1 2 2 2 2 2 2 2 2
i 0 1 1 2 2 2 2 2 3 3 3
n 0 1 1 2 2 2 2 2 3 3 4
e 0 1 2 2 3 3 3 3 3 3 4
e 0 1 2 2 3 3 3 3 3 3 4
r 0 1 2 2 3 4 4 4 4 4 4

The bottom-right cell is 44. Two common subsequences of length 44 are ⟨e,n,e,r⟩\langle e, n, e, r \rangle and ⟨e,n,i,n⟩\langle e, n, i, n \rangle. Typical mistake: counting letters in common (ee, nn, gg, ii, rr are all in both strings) instead of respecting the order.

Quiz 8

Let X=⟨c,a,r,b,a,r,a,c,t,o,r⟩X = \langle c, a, r, b, a, r, a, c, t, o, r \rangle. What is the length of a longest palindromic subsequence of XX?

  • (a) 55
  • (b) 66
  • (c) 88
  • (d) 77
Show the result

(d)

Show the full solution

Use p(i,j)p(i, j) of 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 →: p(i,i)=1p(i,i) = 1; if xi=xjx_i = x_j then p(i,j)=2+p(i+1,j−1)p(i,j) = 2 + p(i+1, j-1) (with p(i,i+1)=2p(i,i+1) = 2); otherwise p(i,j)=max⁡{p(i+1,j),p(i,j−1)}p(i,j) = \max\{p(i+1, j), p(i, j-1)\}. The first row of the table, by diagonals, is p(1,j), j=1,…,11:1, 1, 1, 1, 3, 3, 5, 7, 7, 7, 7.p(1, j),\ j = 1, \dots, 11:\quad 1,\ 1,\ 1,\ 1,\ 3,\ 3,\ 5,\ 7,\ 7,\ 7,\ 7. For instance x1=x8=cx_1 = x_8 = c gives p(1,8)=2+p(2,7)p(1, 8) = 2 + p(2, 7), then x2=x7=ax_2 = x_7 = a gives 2+p(3,6)2 + p(3, 6), then x3=x6=rx_3 = x_6 = r gives 2+p(4,5)2 + p(4, 5) with p(4,5)=1p(4, 5) = 1 (b≠ab \ne a): 2+2+2+1=72 + 2 + 2 + 1 = 7, the palindrome ⟨c,a,r,b,r,a,c⟩\langle c, a, r, b, r, a, c \rangle. The last three letters t,o,rt, o, r do not improve it: p(1,11)=7p(1, 11) = 7 (all the other branches of the recurrence give at most 77; a length 88 would need four disjoint pairs of equal letters nested around the centre, which XX does not have). Another palindrome of length 77 is ⟨c,a,r,a,r,a,c⟩\langle c, a, r, a, r, a, c \rangle.

Typical mistake: taking the longest substring palindrome (⟨a,r,b,a,r,a⟩\langle a, r, b, a, r, a \rangle is not one; the longest substring palindromes have length 33).

Quiz 9

The matrix chain algorithm MCM (bottom-up, diagonal by diagonal) is applied to a chain of n=8n = 8 matrices. Counting two multiplications between integers (the three dimensions pi−1pkpjp_{i-1} p_k p_j) for every value of kk examined, how many multiplications does it perform?

  • (a) 8484
  • (b) 504504
  • (c) 168168
  • (d) 336336
Show the result

(c)

Show the full solution

In Matrix Chain Multiplicationgiven the dimension vector p of a chain of n compatible matrices, find the parenthesization minimizing scalar products; parenthesizations are Catalan numbers (exponential), the subtrees of the last product are optimal (cut-and-paste), c(i,j) = min over k of c(i,k) + c(k+1,j) + p_(i-1) p_k p_j, divide and conquer costs 3^(n-1) - 1, bottom-up by increasing diagonals costs (n^3 - n)/3 products, and memoization never saves anything.Matrix Chain Multiplication → the cost is T(n)=2∑ℓ=2n(ℓ−1)(n−ℓ+1)=n3−n3T(n) = 2 \sum_{\ell = 2}^{n} (\ell - 1)(n - \ell + 1) = \frac{n^3 - n}{3}: for n=8n = 8, 512−83=168\frac{512 - 8}{3} = 168. Check: the number of values of kk is ∑ℓ=28(ℓ−1)(9−ℓ)=7+12+15+16+15+12+7=84\sum_{\ell=2}^{8} (\ell - 1)(9 - \ell) = 7 + 12 + 15 + 16 + 15 + 12 + 7 = 84, and each costs 22 multiplications.

Wrong options: (a) counts only the values of kk, without the factor 22; (b) is n3−nn^3 - n, forgetting the division by 33 in the sum; (d) is 2⋅1682 \cdot 168, counting the factor 22 twice.

Quiz 10

Five jobs with execution times t=(8,3,5,2,6)t = (8, 3, 5, 2, 6) are run on one machine, one after the other. The waiting time of a job is the sum of the times of the jobs run before it. What is the minimum possible cumulative waiting time (sum of the waiting times of all the jobs)?

  • (a) 3333
  • (b) 5353
  • (c) 5757
  • (d) 6363
Show the result

(a)

Show the full solution

By Greedy Exercises - CD Tracks, Shortest-First Scheduling and Boxinghow to set up and prove a greedy exercise (greedy choice, clean-up, algorithm, greedy choice property by exchange, optimal substructure by contradiction, induction) applied to three problems with empty clean-up: CD tracks (fill the first CD, Theta(n)), shortest-first scheduling (sort by duration, SJF holds for every optimum, O(n log n)) and (2,1)-boxing (pair the heaviest with the lightest, Theta(n)).Greedy Exercises - CD Tracks, Shortest-First Scheduling and Boxing → the optimum is the Shortest-Job-First order 2,3,5,6,82, 3, 5, 6, 8 with waits 0,2,5,10,160, 2, 5, 10, 16: total 3333. Equivalently ∑j=1n−1(n−j) tπ(j)=4⋅2+3⋅3+2⋅5+1⋅6=33\sum_{j=1}^{n-1} (n - j)\, t_{\pi(j)} = 4 \cdot 2 + 3 \cdot 3 + 2 \cdot 5 + 1 \cdot 6 = 33. All 120120 orders were checked: none gives less.

Wrong options: (b) is the given order 8,3,5,2,68, 3, 5, 2, 6 (waits 0,8,11,16,180, 8, 11, 16, 18); (c) is the sum of the completion times in the optimal order (2+5+10+16+24=572 + 5 + 10 + 16 + 24 = 57); (d) is the longest-first order (0+8+14+19+220 + 8 + 14 + 19 + 22).

Exercise 1 (points 11)

A polynomial p(x)=a0+a1x+⋯+an−1xn−1p(x) = a_0 + a_1 x + \dots + a_{n-1} x^{n-1} with nn coefficients is represented by the vector a=(a0,…,an−1)\mathbf{a} = (a_0, \dots, a_{n-1}), and n=2kn = 2^k. The product of pp and qq (vector b\mathbf{b}) has 2n−12n - 1 coefficients. The cost is the number of sums, differences and products between scalars; assignments, copies and shifts of vectors cost 00. The algorithm CONV that computes the product by definition (two nested loops, the first term of each coefficient is assigned, not added) costs 2n2−2n+12n^2 - 2n + 1.

The hybrid algorithm GCONV has a threshold n0n_0 (a power of two, 1≤n0≤n1 \le n_0 \le n) and works as follows. Write h=n/2h = n/2 and p=plo+xh phip = p_{lo} + x^h\, p_{hi}, q=qlo+xh qhiq = q_{lo} + x^h\, q_{hi}, where plo,phip_{lo}, p_{hi} have the hh lower and the hh higher coefficients of pp, and the same for qq.

  • Base: if n≤n0n \le n_0 return CONV(a,b)(\mathbf{a}, \mathbf{b}).
  • Divide: compute s=plo+phis = p_{lo} + p_{hi} and t=qlo+qhit = q_{lo} + q_{hi} (componentwise sums of the coefficient vectors, hh coefficients each).
  • Recurse: P0=GCONV(plo,qlo)P_0 = \texttt{GCONV}(p_{lo}, q_{lo}), P2=GCONV(phi,qhi)P_2 = \texttt{GCONV}(p_{hi}, q_{hi}), P1=GCONV(s,t)P_1 = \texttt{GCONV}(s, t), each with n−1n - 1 coefficients.
  • Conquer: M=P1−P0−P2M = P_1 - P_0 - P_2 (componentwise) and return P0+xhM+x2hP2P_0 + x^h M + x^{2h} P_2, adding the three vectors placed at positions 00, hh, 2h2h.

1.(a) [2 points] Prove that GCONV is correct.

1.(b) [3 points] Show that the work of a non-base call (Divide plus Conquer) is exactly w(n)=4n−4w(n) = 4n - 4 operations. Write the recurrence T(n,n0)T(n, n_0) of the cost of GCONV and solve it exactly for n=n02mn = n_0 2^m with the recursion tree.

1.(c) [2 points] Find the power of two n0n_0 that minimizes the coefficient of nlog⁡23n^{\log_2 3} in T(n,n0)T(n, n_0). For that n0n_0, from which nn on is GCONV cheaper than CONV, and how much does it cost for n=1024n = 1024?

1.(d) [4 points] For nn not a power of two a simplified model of the cost is W(n)={2n2−2n+11≤n≤43 W(⌊n/2⌋)+4n−4n>4.W(n) = \begin{cases} 2n^2 - 2n + 1 & 1 \le n \le 4 \\ 3\,W(\lfloor n/2 \rfloor) + 4n - 4 & n > 4. \end{cases} With parametric induction show that there are c>0c > 0 and d,e∈Rd, e \in \mathbb{R} such that W(n)≤c nlog⁡23+d n+eW(n) \le c\, n^{\log_2 3} + d\, n + e for all n≥1n \ge 1: write the constraints on c,d,ec, d, e coming from the base and from the step, and find the smallest cc they allow.

Show the result

(a) M=P1−P0−P2=ploqhi+phiqloM = P_1 - P_0 - P_2 = p_{lo} q_{hi} + p_{hi} q_{lo}, so the returned polynomial is ploqlo+xh(ploqhi+phiqlo)+x2hphiqhi=pqp_{lo} q_{lo} + x^h (p_{lo} q_{hi} + p_{hi} q_{lo}) + x^{2h} p_{hi} q_{hi} = pq.

(b) w(n)=n+2(n−1)+(n−2)=4n−4w(n) = n + 2(n-1) + (n-2) = 4n - 4. T(n,n0)=2n2−2n+1T(n, n_0) = 2n^2 - 2n + 1 for n≤n0n \le n_0, T(n,n0)=3 T(n/2,n0)+4n−4T(n, n_0) = 3\,T(n/2, n_0) + 4n - 4 otherwise, and for n=n02mn = n_0 2^m T(n,n0)=3m (2n02+6n0−1)−8n+2=nlog⁡23 2n02+6n0−1n0log⁡23−8n+2.T(n, n_0) = 3^m\,(2n_0^2 + 6n_0 - 1) - 8n + 2 = n^{\log_2 3}\,\frac{2n_0^2 + 6n_0 - 1}{n_0^{\log_2 3}} - 8n + 2.

(c) The coefficient g(n0)=2n02+6n0−1n0log⁡23g(n_0) = \frac{2n_0^2 + 6n_0 - 1}{n_0^{\log_2 3}} takes the values 7, 6.333, 6.111, 6.481, 7.4947,\ 6.333,\ \mathbf{6.111},\ 6.481,\ 7.494 for n0=1,2,4,8,16n_0 = 1, 2, 4, 8, 16: the optimum is n0=4n_0 = 4. GCONV with n0=4n_0 = 4 costs 55⋅3m−8n+255 \cdot 3^m - 8n + 2 (with n=4⋅2mn = 4 \cdot 2^m), equals CONV for n=4n = 4 (2525) and is cheaper for every n≥8n \ge 8 (103103 against 113113 for n=8n = 8). For n=1024n = 1024: 352 665352\,665 against 2 095 1052\,095\,105 (about one sixth).

(d) Constraints: d=−8d = -8, e≤−4e \le -4 from the step; c≥1−d−ec \ge 1 - d - e from the base n=1n = 1 (the binding one). Smallest c=13c = 13: W(n)≤13 nlog⁡23−8n−4W(n) \le 13\, n^{\log_2 3} - 8n - 4 for all n≥1n \ge 1.

Show the full solution

(a) Correctness. Use the substructure of 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 →: threshold n0n_0, base solved by the correct CONV, subinstances of size h<nh < n (the sums s,ts, t also have hh coefficients, since they add vectors of equal length). By induction on nn the three recursive calls return the products P0=ploqloP_0 = p_{lo} q_{lo}, P2=phiqhiP_2 = p_{hi} q_{hi}, P1=(plo+phi)(qlo+qhi)P_1 = (p_{lo} + p_{hi})(q_{lo} + q_{hi}) (this is the same idea of Gauss as in the product of complex numbers with three real products, see Structured Block Matrices - Reducing Recursive CallsFor recursively structured matrix families (ARB, SIR, RCA, supercirculant) the method is: count the free parameters (n, not n^2), write the block product, prove closure under sum, difference and product by induction, count the distinct block products, and reduce them with sums and differences (2 calls for SC and RCA, 3 for ARB and SIR). If the product leaves the family one cannot recurse on it, which is why one asks for a matrix-vector product instead.Structured Block Matrices - Reducing Recursive Calls →). Expanding, P1=ploqlo+ploqhi+phiqlo+phiqhi ⟹ M=P1−P0−P2=ploqhi+phiqlo.P_1 = p_{lo} q_{lo} + p_{lo} q_{hi} + p_{hi} q_{lo} + p_{hi} q_{hi} \ \Longrightarrow\ M = P_1 - P_0 - P_2 = p_{lo} q_{hi} + p_{hi} q_{lo}. Then pq=(plo+xhphi)(qlo+xhqhi)=ploqlo+xh(ploqhi+phiqlo)+x2hphiqhi=P0+xhM+x2hP2.pq = (p_{lo} + x^h p_{hi})(q_{lo} + x^h q_{hi}) = p_{lo} q_{lo} + x^h (p_{lo} q_{hi} + p_{hi} q_{lo}) + x^{2h} p_{hi} q_{hi} = P_0 + x^h M + x^{2h} P_2. The positions fit: P0P_0 occupies 0,…,n−20, \dots, n-2, MM occupies h,…,3h−2h, \dots, 3h - 2, P2P_2 occupies 2h,…,4h−2=2n−22h, \dots, 4h - 2 = 2n - 2, so the result has the 2n−12n - 1 coefficients of the product. □\square (Typical mistake: forgetting to subtract P0P_0 and P2P_2, which leaves the extra terms ploqlo+phiqhip_{lo} q_{lo} + p_{hi} q_{hi} in the middle.)

(b) Work, recurrence, exact solution.

Work of a non-base call. Divide: ss and tt cost hh sums each, 2h=n2h = n. Conquer: MM needs two componentwise subtractions of vectors of n−1n - 1 components, 2(n−1)2(n-1). The final sum has overlaps only where two of the three vectors have a coefficient in the same position: P0P_0 and MM share positions h,…,2h−2h, \dots, 2h - 2 (h−1h - 1 positions), MM and P2P_2 share 2h,…,3h−22h, \dots, 3h - 2 (h−1h - 1 positions), P0P_0 and P2P_2 share none (P0P_0 ends at 2h−22h - 2, P2P_2 starts at 2h2h); every other coefficient is copied (cost 00). So 2(h−1)=n−22(h - 1) = n - 2 additions, and w(n)=n+2(n−1)+(n−2)=4n−4(w(2)=4).w(n) = n + 2(n - 1) + (n - 2) = 4n - 4 \qquad (w(2) = 4).

Recurrence. With T0(n0)=2n02−2n0+1T_0(n_0) = 2n_0^2 - 2n_0 + 1 (cost of CONV on n0n_0 coefficients): T(n,n0)={2n2−2n+1n≤n03 T(n/2,n0)+4n−4n>n0.T(n, n_0) = \begin{cases} 2n^2 - 2n + 1 & n \le n_0 \\ 3\,T(n/2, n_0) + 4n - 4 & n > n_0. \end{cases}

Recursion tree (Recurrence Relations - Recursion Tree and General FormulaThe recurrence tree has the shape of the call tree of the worst instance and T(n) is the sum of the works of its nodes. For the family T(n) = s(n) T(f(n)) + w(n) with base T0 for n <= n0, counting nodes per level with the iterates of f and the level f*(n,n0) gives the closed formula T(n) = sum over levels of [product of s] w + [product of s] T0, proved by induction and applied to merge sort, Hanoi and square-root recurrences.Recurrence Relations - Recursion Tree and General Formula →): s=3s = 3, f(n)=n/2f(n) = n/2, w(n)=4n−4w(n) = 4n - 4. With n=n02mn = n_0 2^m the last non-base level is f∗=m−1f^\ast = m - 1; level ℓ\ell has 3ℓ3^\ell nodes of size n/2ℓn/2^\ell and work 4n/2ℓ−44n/2^\ell - 4; the 3m3^m leaves have size n0n_0. So T=∑ℓ=0m−13ℓ(4n2ℓ−4)+3m(2n02−2n0+1).T = \sum_{\ell=0}^{m-1} 3^\ell \Bigl(\frac{4n}{2^\ell} - 4\Bigr) + 3^m (2n_0^2 - 2n_0 + 1). The first part is 4n∑ℓ<m(32)ℓ=8n((32)m−1)4n \sum_{\ell<m} (\tfrac32)^\ell = 8n\bigl((\tfrac32)^m - 1\bigr), and since n=n02mn = n_0 2^m we have n(32)m=n03mn(\tfrac32)^m = n_0 3^m: it equals 8n03m−8n8 n_0 3^m - 8n. The second part is 4⋅3m−12=2⋅3m−24 \cdot \frac{3^m - 1}{2} = 2 \cdot 3^m - 2, subtracted. Summing, T(n,n0)=8n03m−8n−2⋅3m+2+3m(2n02−2n0+1)=3m(2n02+6n0−1)−8n+2.T(n, n_0) = 8n_0 3^m - 8n - 2 \cdot 3^m + 2 + 3^m (2n_0^2 - 2n_0 + 1) = 3^m (2n_0^2 + 6n_0 - 1) - 8n + 2. With 3m=(n/n0)log⁡233^m = (n/n_0)^{\log_2 3} one gets the second form, Θ(nlog⁡23)\Theta(n^{\log_2 3}) (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 → gives the same order: a=3a = 3, b=2b = 2, w=Θ(n)w = \Theta(n), Case 1). Checks: m=0m = 0 gives 2n02+6n0−1−8n0+2=2n02−2n0+12n_0^2 + 6n_0 - 1 - 8n_0 + 2 = 2n_0^2 - 2n_0 + 1; n0=1n_0 = 1, n=2n = 2: 3+4⋅2−4=7=21−16+23 + 4 \cdot 2 - 4 = 7 = 21 - 16 + 2; n0=4n_0 = 4, n=8n = 8: 3⋅55−64+2=1033 \cdot 55 - 64 + 2 = 103. The formula was also confirmed by counting the operations of an implementation on random inputs, for n0∈{1,2,4,8,16}n_0 \in \{1, 2, 4, 8, 16\} and n≤256n \le 256, with the output compared to the definition.

(c) Choice of the threshold. The leading coefficient is g(n0)=(2n02+6n0−1)/n0log⁡23g(n_0) = (2n_0^2 + 6n_0 - 1) / n_0^{\log_2 3}, the cost of the 3m3^m leaves plus the work of the mm levels above them, per unit of nlog⁡23n^{\log_2 3}. As in Hybrid AlgorithmsHybridization combines two algorithms for the same problem so the result has the asymptotics of the better one and, if possible, lower constants. Choosing the algorithm only by the starting size (STUPID_HYBRID) does not help; changing the base case of the recursion (H_MUL: iterative product for n <= n0 at every leaf of Strassen's tree) gives cost n^(log2 7)(2n0+5)/n0^(log2 7 - 2) - 6n^2, minimal for n0 = 8 with leading constant about 3.92.Hybrid Algorithms →: a larger n0n_0 replaces levels of the tree (where ww is linear) with CONV (quadratic), a smaller one leaves too many nodes. Values:

n0n_0 1 2 4 8 16 32
g(n0)g(n_0) 7.0007.000 6.3336.333 6.111\mathbf{6.111} 6.4816.481 7.4947.494 9.2149.214

The real minimizer of gg (zero of g′g': (4−2log⁡23) x2+(6−6log⁡23) x+log⁡23=0(4 - 2\log_2 3)\,x^2 + (6 - 6\log_2 3)\,x + \log_2 3 = 0) is x≈3.71x \approx 3.71, so among powers of two the optimum is n0=4n_0 = 4. Then T(n,4)=55⋅3m−8n+2T(n, 4) = 55 \cdot 3^m - 8n + 2 with n=4⋅2mn = 4 \cdot 2^m, and CONV(n)=2n2−2n+1=32⋅4m−8⋅2m+1\mathtt{CONV}(n) = 2n^2 - 2n + 1 = 32 \cdot 4^m - 8 \cdot 2^m + 1. The difference D(m)=CONV(n)−T(n,4)=32⋅4m+24⋅2m−55⋅3m−1D(m) = \mathtt{CONV}(n) - T(n, 4) = 32 \cdot 4^m + 24 \cdot 2^m - 55 \cdot 3^m - 1 is 00 for m=0m = 0 (n=4n = 4, both cost 2525), 1010 for m=1m = 1 (113−103113 - 103), 112112 for m=2m = 2 (481−369481 - 369). For m≥1m \ge 1 it is positive: D(1)=10>0D(1) = 10 > 0 and D(m+1)−3D(m)=32⋅4m−24⋅2m+2>0D(m+1) - 3D(m) = 32 \cdot 4^m - 24 \cdot 2^m + 2 > 0, so D(m+1)>3D(m)>0D(m+1) > 3D(m) > 0 (induction). Hence the hybrid is cheaper for every n≥8n \ge 8. For n=1024n = 1024 (m=8m = 8): 55⋅6561−8192+2=352 66555 \cdot 6561 - 8192 + 2 = 352\,665, against 2 095 1052\,095\,105 of CONV (ratio 0.1680.168); the other thresholds give 405 153405\,153 (n0=1n_0 = 1), 365 787365\,787 (n0=2n_0 = 2), 374 535374\,535 (n0=8n_0 = 8). Typical mistake: choosing the threshold from the cost for small nn only, or taking n0=3n_0 = 3 or 55 (not powers of two: the sizes n/2ℓn/2^\ell would stop being integers).

(d) Parametric induction (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 →). Let γ=log⁡23\gamma = \log_2 3. Guess: W(n)≤c nγ+d n+eW(n) \le c\,n^\gamma + d\,n + e for all n≥1n \ge 1, with c>0c > 0 and d,ed, e real. The simplest guess W(n)≤c nγW(n) \le c\,n^\gamma cannot work: for even nn the step would need 3c(n/2)γ+4n−4≤c nγ3c(n/2)^\gamma + 4n - 4 \le c\,n^\gamma, i.e. 4n−4≤04n - 4 \le 0, false. A lower-order linear term is needed, as in the notes.

Base (1≤n≤41 \le n \le 4): 2n2−2n+1≤c nγ+d n+e2n^2 - 2n + 1 \le c\,n^\gamma + d\,n + e, i.e. c≥W(n)−dn−enγc \ge \dfrac{W(n) - dn - e}{n^\gamma} for n=1,2,3,4n = 1, 2, 3, 4.

Step (n≥5n \ge 5, so 2≤⌊n/2⌋<n2 \le \lfloor n/2 \rfloor < n). By [HP] W(n)≤3(c⌊n/2⌋γ+d⌊n/2⌋+e)+4n−4.W(n) \le 3\bigl(c\lfloor n/2\rfloor^\gamma + d\lfloor n/2\rfloor + e\bigr) + 4n - 4. Two rounding facts: ⌊n/2⌋≤n/2\lfloor n/2 \rfloor \le n/2 and x↦xγx \mapsto x^\gamma is increasing, so 3c⌊n/2⌋γ≤3c (n/2)γ=c nγ3c\lfloor n/2 \rfloor^\gamma \le 3c\,(n/2)^\gamma = c\,n^\gamma (because 2γ=32^\gamma = 3); and for d<0d < 0, ⌊n/2⌋≥(n−1)/2\lfloor n/2 \rfloor \ge (n-1)/2 gives 3d⌊n/2⌋≤3d2(n−1)3d\lfloor n/2 \rfloor \le \tfrac{3d}{2}(n - 1). (If d≥0d \ge 0 the bound 3d⌊n/2⌋≤3d2n3d\lfloor n/2\rfloor \le \frac{3d}{2} n leaves the term (d2+4)n(\frac d2 + 4) n positive and growing, which no constant can absorb: so d<0d < 0.) Then W(n)≤c nγ+(3d2+4)n−3d2+3e−4 ≤? c nγ+d n+e  ⟺  (d2+4)n≤3d2−2e+4.W(n) \le c\,n^\gamma + \Bigl(\tfrac{3d}{2} + 4\Bigr) n - \tfrac{3d}{2} + 3e - 4 \ \overset{?}{\le}\ c\,n^\gamma + d\,n + e \iff \Bigl(\tfrac d2 + 4\Bigr) n \le \tfrac{3d}{2} - 2e + 4. This must hold for all n≥5n \ge 5. If d>−8d > -8 the left side grows with nn and the condition fails. If d=−8d = -8 the left side is 00 and the condition is 0≤−8−2e0 \le -8 - 2e, i.e. e≤−4e \le -4. If d<−8d < -8 the left side decreases with nn, the binding case is n=5n = 5 and it gives e≤−d−162e \le \frac{-d - 16}{2} (for example d=−10d = -10, e≤−3e \le -3).

Intersection. The base needs c≥W(n)−dn−enγc \ge \frac{W(n) - dn - e}{n^\gamma} for n=1,2,3,4n = 1, 2, 3, 4. For d=−8d = -8, e=−4e = -4 the four bounds are 13, 8.33, 7.19, 6.7813,\ 8.33,\ 7.19,\ 6.78: the largest, 1313, comes from n=1n = 1. For any admissible pair the bound from n=1n = 1 is 1−d−e1 - d - e, and since the step forces e≤−4e \le -4 when d=−8d = -8 and e≤−d−162e \le \frac{-d-16}{2} when d<−8d < -8, we get 1−d−e≥9−d2≥131 - d - e \ge 9 - \frac d2 \ge 13 for d<−8d < -8 and 1−d−e≥131 - d - e \ge 13 for d=−8d = -8, with equality only at (d,e)=(−8,−4)(d, e) = (-8, -4). So the smallest cc is c=13:W(n)≤13 nlog⁡23−8n−4for all n≥1.c = 13:\qquad W(n) \le 13\,n^{\log_2 3} - 8n - 4 \quad \text{for all } n \ge 1. Check by a normal induction: base n=1n = 1: 13−8−4=1=W(1)13 - 8 - 4 = 1 = W(1); n=2n = 2: 39−16−4=19≥539 - 16 - 4 = 19 \ge 5; n=3n = 3: ≈46.2≥13\approx 46.2 \ge 13; n=4n = 4: 81≥2581 \ge 25. Step: as above with d=−8d = -8, e=−4e = -4, since (d2+4)n=0≤−8−2e=0\bigl(\tfrac{d}{2} + 4\bigr) n = 0 \le -8 - 2e = 0. Numerically W(5)=31W(5) = 31, W(100)=7815≤18 421W(100) = 7815 \le 18\,421, and the inequality holds for every n<200 000n < 200\,000.

Remark. The constant 1313 is imposed by the single value n=1n = 1 (where the bound is tight); asking the guess only for n≥2n \ge 2 the same method gives c=25/3≈8.33c = 25/3 \approx 8.33. Both are above the true asymptotic constant 6.116.11 of the power-of-two case: parametric induction gives a valid bound, not the best one. As the notes say, the method can fail or be loose, and a failure does not disprove the guess.

Exercise 2 (points 11)

A file uses n≥3n \ge 3 characters c1,…,cnc_1, \dots, c_n, and cjc_j occurs FjF_j times, where F1=F2=1F_1 = F_2 = 1 and Fj=Fj−1+Fj−2F_j = F_{j-1} + F_{j-2} are the Fibonacci numbers (1,1,2,3,5,8,…1, 1, 2, 3, 5, 8, \dots). The Huffman algorithm is used with the conventions of the course: the trie has the left child lighter (frequency not greater) than the right child, the edge to the left child is labelled 00 and the edge to the right child 11; when a leaf and an internal node have the same frequency the leaf is extracted first.

2.(a) [2 points] For n=6n = 6 draw the Huffman trie, list the six codewords and compute the cost B(T)=∑cf(c) dT(c)B(T) = \sum_c f(c)\, d_T(c).

2.(b) [4 points] For a generic nn prove by induction the shape of the trie and give the codeword and the length of cjc_j, for 1≤j≤n1 \le j \le n.

2.(c) [2 points] Compute B(Tn)B(T_n) in closed form, the average codeword length, and compare with a fixed-length code for n=6n = 6.

2.(d) [3 points] (Dynamic programming.) Four matrices A1,A2,A3,A4A_1, A_2, A_3, A_4 have dimension vector p=(4,7,3,10,5)p = (4, 7, 3, 10, 5), i.e. AiA_i is pi−1×pip_{i-1} \times p_i. With the bottom-up algorithm MCM find the minimum number of scalar products to compute A1A2A3A4A_1 A_2 A_3 A_4 and an optimal parenthesization; say how many values of kk the algorithm examines and what the left-to-right order (((A1A2)A3)A4)(((A_1 A_2) A_3) A_4) costs.

Show the result

(a) Codewords c6=0c_6 = 0, c5=10c_5 = 10, c4=110c_4 = 110, c3=1110c_3 = 1110, c1=11110c_1 = 11110, c2=11111c_2 = 11111 (frequencies 8,5,3,2,1,18, 5, 3, 2, 1, 1); B(T)=45B(T) = 45 (=2+4+7+12+20= 2 + 4 + 7 + 12 + 20, the internal nodes).

(b) The trie is a "comb": cnc_n is a leaf child of the root, cn−1c_{n-1} a leaf child of the right child, and so on. Codeword of cjc_j: 1 n−j 01^{\,n-j}\,0 for 3≤j≤n3 \le j \le n (length n−j+1n - j + 1); c1=1 n−2 0c_1 = 1^{\,n-2}\,0 and c2=1 n−1c_2 = 1^{\,n-1} (length n−1n - 1).

(c) B(Tn)=Fn+4−n−4B(T_n) = F_{n+4} - n - 4; total frequency Fn+2−1F_{n+2} - 1; average length Fn+4−n−4Fn+2−1→3+52≈2.618\frac{F_{n+4} - n - 4}{F_{n+2} - 1} \to \frac{3 + \sqrt 5}{2} \approx 2.618. For n=6n = 6: 4545 bits against 6060 for the fixed code (33 bits per character), average 2.252.25 against 33.

(d) Minimum 294294 products, parenthesization ((A1A2)(A3A4))((A_1 A_2)(A_3 A_4)); 1010 values of kk (2020 multiplications between dimensions); left to right: 404404.

Show the full solution

(a) The case n=6n = 6. Frequencies 1,1,2,3,5,81, 1, 2, 3, 5, 8 (total 2020). Run of the algorithm of Huffman Codescharacter-by-character compression needs an injective variable-length code, which prefix codes guarantee; a prefix code is a binary trie with one leaf per character of cost B(T) = sum of f(c) d_T(c), the optimal trie is full with n-1 internal nodes, Huffman repeatedly merges the two lowest-frequency trees using a priority queue (2n-1 extractions, O(n log n)), and optimality follows from the greedy choice (two rarest characters are deepest sibling leaves, by exchange) and substructure B(T) = B(T') + f(x) + f(y).Huffman Codes →:

step extracted (left, right) node created queue after the step
start c1 1, c2 1, c3 2, c4 3, c5 5, c6 8c_1\,1,\ c_2\,1,\ c_3\,2,\ c_4\,3,\ c_5\,5,\ c_6\,8
1 c1 1, c2 1c_1\,1,\ c_2\,1 z1=2z_1 = 2 c3 2, z1 2, c4 3, c5 5, c6 8c_3\,2,\ z_1\,2,\ c_4\,3,\ c_5\,5,\ c_6\,8
2 c3 2, z1 2c_3\,2,\ z_1\,2 z2=4z_2 = 4 c4 3, z2 4, c5 5, c6 8c_4\,3,\ z_2\,4,\ c_5\,5,\ c_6\,8
3 c4 3, z2 4c_4\,3,\ z_2\,4 z3=7z_3 = 7 c5 5, z3 7, c6 8c_5\,5,\ z_3\,7,\ c_6\,8
4 c5 5, z3 7c_5\,5,\ z_3\,7 z4=12z_4 = 12 c6 8, z4 12c_6\,8,\ z_4\,12
5 c6 8, z4 12c_6\,8,\ z_4\,12 z5=20z_5 = 20 z5 20z_5\,20

In step 2 the leaf c3c_3 and the node z1z_1 have the same frequency 22: by the convention the leaf is extracted first, so it is the left child. The trie (weights; 00 left, 11 right):

                      (20)
                    0/    \1
                 c6:8      (12)
                         0/    \1
                      c5:5      (7)
                              0/   \1
                           c4:3    (4)
                                 0/   \1
                              c3:2    (2)
                                     0/  \1
                                  c1:1    c2:1

Codewords: c6=0c_6 = 0, c5=10c_5 = 10, c4=110c_4 = 110, c3=1110c_3 = 1110, c1=11110c_1 = 11110, c2=11111c_2 = 11111. Cost: B(T)=8⋅1+5⋅2+3⋅3+2⋅4+1⋅5+1⋅5=8+10+9+8+5+5=45.B(T) = 8 \cdot 1 + 5 \cdot 2 + 3 \cdot 3 + 2 \cdot 4 + 1 \cdot 5 + 1 \cdot 5 = 8 + 10 + 9 + 8 + 5 + 5 = 45. Check with the internal nodes (B(T)B(T) is the sum of their weights): 2+4+7+12+20=452 + 4 + 7 + 12 + 20 = 45.

(b) The shape, for generic nn. First a lemma: ∑i=1kFi=Fk+2−1\sum_{i=1}^{k} F_i = F_{k+2} - 1 for k≥1k \ge 1. Induction: k=1k = 1: 1=F3−11 = F_3 - 1; if true for kk, then ∑i=1k+1Fi=Fk+2−1+Fk+1=Fk+3−1\sum_{i=1}^{k+1} F_i = F_{k+2} - 1 + F_{k+1} = F_{k+3} - 1.

Define the comb CmC_m (m≥2m \ge 2): C2C_2 is a root with the leaves c1c_1 (left) and c2c_2 (right); for m≥3m \ge 3, CmC_m is a root whose left child is the leaf cmc_m and whose right child is Cm−1C_{m-1}. By the lemma the weight of CmC_m (sum of the frequencies of c1,…,cmc_1, \dots, c_m) is Fm+2−1F_{m+2} - 1.

Claim P(k)P(k), for 1≤k≤n−11 \le k \le n - 1: after kk iterations of the loop of HUFFMAN the queue contains exactly Ck+1C_{k+1} and the leaves ck+2,…,cnc_{k+2}, \dots, c_n.

Base k=1k = 1. The initial weights are 1,1,2,3,…1, 1, 2, 3, \dots: the two minima are c1,c2c_1, c_2 (every other leaf weighs at least 22), and merging them gives C2C_2 (which of the two is the left child is irrelevant for the shape).

Step. Assume P(k)P(k) with k≤n−2k \le n - 2. The queue holds Ck+1C_{k+1} of weight Fk+3−1F_{k+3} - 1 and the leaves of weights Fk+2<Fk+3≤Fk+4≤…F_{k+2} < F_{k+3} \le F_{k+4} \le \dots (strict because Fk+3=Fk+2+Fk+1F_{k+3} = F_{k+2} + F_{k+1} with Fk+1≥1F_{k+1} \ge 1). So ck+2c_{k+2} and Ck+1C_{k+1} are strictly lighter than every other element (Fk+3−1<Fk+3F_{k+3} - 1 < F_{k+3} and Fk+2<Fk+3F_{k+2} < F_{k+3}): they are the two extracted nodes. Their order: Fk+2≤Fk+3−1  ⟺  Fk+1≥1F_{k+2} \le F_{k+3} - 1 \iff F_{k+1} \ge 1, always true, and with equality only when Fk+1=1F_{k+1} = 1, i.e. k=1k = 1 (the tie c3c_3 against C2C_2 of step 2, where the convention puts the leaf on the left). So the leaf ck+2c_{k+2} is the left child, Ck+1C_{k+1} the right one, and the new node is exactly Ck+2C_{k+2}, of weight Fk+3−1+Fk+2=Fk+4−1F_{k+3} - 1 + F_{k+2} = F_{k+4} - 1. This is P(k+1)P(k+1). □\square

After n−1n - 1 iterations the queue contains only CnC_n: the Huffman trie is the comb CnC_n. Reading the paths: CmC_m is reached from Cm+1C_{m+1} by an edge 11, and its leaf cmc_m by an edge 00. To reach the leaf cjc_j with 3≤j≤n3 \le j \le n one goes through Cn,Cn−1,…,Cj+1C_n, C_{n-1}, \dots, C_{j+1} (that is n−jn - j edges labelled 11) to CjC_j and then takes the left edge: e(cj)=1 n−j 0,dT(cj)=n−j+1(3≤j≤n).e(c_j) = 1^{\,n-j}\,0,\qquad d_T(c_j) = n - j + 1 \quad (3 \le j \le n). The leaves c1,c2c_1, c_2 hang from C2C_2, which is reached with n−2n - 2 ones: e(c1)=1 n−2 0e(c_1) = 1^{\,n-2}\,0, e(c2)=1 n−1e(c_2) = 1^{\,n-1}, depth n−1n - 1 both. For n=6n = 6 this gives the codewords of (a). The code is prefix-free (all the leaves are leaves of a trie). The longest codeword has n−1n - 1 bits, so the length grows linearly with nn.

Typical mistakes: thinking that the tie at step 2 can change the cost (it only exchanges the codewords of c3c_3 and of the leaves below; the cost is the same, and the run with the opposite convention was also checked: every tie-breaking gives the same cost); comparing Fk+2F_{k+2} with Fk+3F_{k+3} instead of with Fk+3−1F_{k+3} - 1, the weight of the comb.

(c) Cost and average length. The internal nodes are C2,…,CnC_2, \dots, C_n with weights Fk+2−1F_{k+2} - 1, so by the formula B(T)=∑v internalw(v)B(T) = \sum_{v \text{ internal}} w(v) of Huffman Codescharacter-by-character compression needs an injective variable-length code, which prefix codes guarantee; a prefix code is a binary trie with one leaf per character of cost B(T) = sum of f(c) d_T(c), the optimal trie is full with n-1 internal nodes, Huffman repeatedly merges the two lowest-frequency trees using a priority queue (2n-1 extractions, O(n log n)), and optimality follows from the greedy choice (two rarest characters are deepest sibling leaves, by exchange) and substructure B(T) = B(T') + f(x) + f(y).Huffman Codes →, and the lemma, B(Tn)=∑k=2n(Fk+2−1)=(∑i=4n+2Fi)−(n−1)=(Fn+4−1−(1+1+2))−(n−1)=Fn+4−n−4.B(T_n) = \sum_{k=2}^{n} (F_{k+2} - 1) = \Bigl(\sum_{i=4}^{n+2} F_i\Bigr) - (n - 1) = \bigl(F_{n+4} - 1 - (1 + 1 + 2)\bigr) - (n - 1) = F_{n+4} - n - 4. Check n=6n = 6: F10−10=55−10=45F_{10} - 10 = 55 - 10 = 45. The file has Fn+2−1F_{n+2} - 1 characters (the lemma with k=nk = n), so the average length is Fn+4−n−4Fn+2−1.\frac{F_{n+4} - n - 4}{F_{n+2} - 1}. Since Fn+4/Fn+2→φ2=3+52≈2.618F_{n+4} / F_{n+2} \to \varphi^2 = \frac{3 + \sqrt 5}{2} \approx 2.618, the average length tends to 2.6182.618 bits whatever nn, while the maximum length is n−1n - 1 (values: n=6n = 6: 2.252.25; n=10n = 10: 363/143=2.54363 / 143 = 2.54; n=20n = 20: 2.6172.617; n=30n = 30: 2.6182.618). A fixed-length code needs ⌈log⁡2n⌉\lceil \log_2 n \rceil bits: for n=6n = 6, 33 bits per character, i.e. 6060 bits for the 2020 characters against 4545 (a quarter less); for n=30n = 30, 55 bits against 2.6182.618. The skewed frequencies make Huffman gain more and more.

(d) Matrix chain (Matrix Chain Multiplicationgiven the dimension vector p of a chain of n compatible matrices, find the parenthesization minimizing scalar products; parenthesizations are Catalan numbers (exponential), the subtrees of the last product are optimal (cut-and-paste), c(i,j) = min over k of c(i,k) + c(k+1,j) + p_(i-1) p_k p_j, divide and conquer costs 3^(n-1) - 1, bottom-up by increasing diagonals costs (n^3 - n)/3 products, and memoization never saves anything.Matrix Chain Multiplication →). A1A_1 is 4×74 \times 7, A2A_2 is 7×37 \times 3, A3A_3 is 3×103 \times 10, A4A_4 is 10×510 \times 5. With m[i,j]=min⁡i≤k<j{m[i,k]+m[k+1,j]+pi−1pkpj}m[i,j] = \min_{i \le k < j} \{m[i,k] + m[k+1,j] + p_{i-1} p_k p_j\} and m[i,i]=0m[i,i] = 0, by increasing diagonals:

  • ℓ=2\ell = 2: m[1,2]=4⋅7⋅3=84m[1,2] = 4 \cdot 7 \cdot 3 = 84, m[2,3]=7⋅3⋅10=210m[2,3] = 7 \cdot 3 \cdot 10 = 210, m[3,4]=3⋅10⋅5=150m[3,4] = 3 \cdot 10 \cdot 5 = 150.
  • ℓ=3\ell = 3, cell (1,3)(1,3): k=1k = 1: 0+210+4⋅7⋅10=4900 + 210 + 4 \cdot 7 \cdot 10 = 490; k=2k = 2: 84+0+4⋅3⋅10=20484 + 0 + 4 \cdot 3 \cdot 10 = 204. Minimum 204204, s[1,3]=2s[1,3] = 2. Cell (2,4)(2,4): k=2k = 2: 0+150+7⋅3⋅5=2550 + 150 + 7 \cdot 3 \cdot 5 = 255; k=3k = 3: 210+0+7⋅10⋅5=560210 + 0 + 7 \cdot 10 \cdot 5 = 560. Minimum 255255, s[2,4]=2s[2,4] = 2.
  • ℓ=4\ell = 4, cell (1,4)(1,4): k=1k = 1: 0+255+4⋅7⋅5=3950 + 255 + 4 \cdot 7 \cdot 5 = 395; k=2k = 2: 84+150+4⋅3⋅5=29484 + 150 + 4 \cdot 3 \cdot 5 = 294; k=3k = 3: 204+0+4⋅10⋅5=404204 + 0 + 4 \cdot 10 \cdot 5 = 404. Minimum 294294, s[1,4]=2s[1,4] = 2.
i\ji \backslash j 1 2 3 4
1 0 84 204 294
2 0 210 255
3 0 150
4 0

Reconstruction: s[1,4]=2s[1,4] = 2 splits into ⟨A1,A2⟩\langle A_1, A_2 \rangle and ⟨A3,A4⟩\langle A_3, A_4 \rangle (each a single product), so the optimum is ((A1A2)(A3A4))((A_1 A_2)(A_3 A_4)) with 84+150+4⋅3⋅5=29484 + 150 + 4 \cdot 3 \cdot 5 = 294 scalar products. The values of kk examined are ∑ℓ=24(ℓ−1)(5−ℓ)=3+4+3=10=n3−n6\sum_{\ell=2}^{4} (\ell - 1)(5 - \ell) = 3 + 4 + 3 = 10 = \frac{n^3 - n}{6}, i.e. 2020 multiplications between dimensions. The five parenthesizations cost 294294, 395395 (A1(A2(A3A4))A_1(A_2(A_3A_4))), 404404 (left to right: 84+4⋅3⋅10+4⋅10⋅5=84+120+20084 + 4 \cdot 3 \cdot 10 + 4 \cdot 10 \cdot 5 = 84 + 120 + 200), 690690 and 700700: the optimum is unique and the left-to-right order costs 110110 more. Typical mistake: using pipkpjp_i p_k p_j in place of pi−1pkpjp_{i-1} p_k p_j in the cost of the last product.

Self-assessment grid
Exercise Points What must be there for full marks Typical mistakes
Quiz 1 1 Case 2 of the Master Theorem: a=4a = 4, b=2b = 2, w=Θ(n2)w = \Theta(n^2), answer (c) Looking only at the exponent of ww; confusing Cases 2 and 3
Quiz 2 1 Recursive product with UU and VV, first row (9,15,17,7)(9, 15, 17, 7) Componentwise product of the rows; cyclic convolution (circulant, not supercirculant)
Quiz 3 1 5+4−1=85 + 4 - 1 = 8 matrix products, each 88 scalar products: 6464 Counting matrix products or 4444 products; forgetting the multiplications by MM
Quiz 4 1 (8,4)(8,4)-sparse vector: DFT2(3,5)=(8,−2)\mathrm{DFT}_2(3, 5) = (8, -2) repeated 44 times Using the wrong sparsity pattern; dividing by 22
Quiz 5 1 Degree bound (n−1)k+1=97(n-1)k + 1 = 97, first power of two 128128 Taking nk=108nk = 108 or 9696; choosing 6464
Quiz 6 1 2nlog⁡2n+n=19202n \log_2 n + n = 1920 Forgetting the nn divisions; 32nlog⁡2n\frac32 n \log_2 n
Quiz 7 1 LCS table with last cell 44; order respected Counting common letters regardless of order
Quiz 8 1 Table by diagonals, p(1,11)=7p(1, 11) = 7, palindrome ⟨c,a,r,b,r,a,c⟩\langle c, a, r, b, r, a, c \rangle Confusing subsequence and substring
Quiz 9 1 n3−n3=168\frac{n^3 - n}{3} = 168 with two products per kk Counting only the values of kk (8484); n3−nn^3 - n
Quiz 10 1 SJF order, waits 0,2,5,10,160, 2, 5, 10, 16, total 3333 Using the given order; summing completion times
1.(a) 2 Gauss identity M=ploqhi+phiqloM = p_{lo} q_{hi} + p_{hi} q_{lo}, positions of the three vectors, induction Not subtracting P0P_0 and P2P_2; no induction on the size
1.(b) 3 w(n)=4n−4w(n) = 4n - 4 with the n−2n - 2 overlaps counted; recurrence with base 2n02−2n0+12n_0^2 - 2n_0 + 1; exact 3m(2n02+6n0−1)−8n+23^m (2n_0^2 + 6n_0 - 1) - 8n + 2 with checks Counting all 3h3h additions of the shifted sum; forgetting the leaf term 3mT03^m T_0
1.(c) 2 g(n0)g(n_0) minimized at n0=4n_0 = 4 (value 6.1116.111), cheaper than CONV for every n≥8n \ge 8, 352 665352\,665 for n=1024n = 1024 Non-power-of-two threshold; comparing only for n=4n = 4
1.(d) 4 Guess with c nγ+d n+ec\,n^\gamma + d\,n + e, why c nγc\,n^\gamma fails, rounding facts, d=−8d = -8, e≤−4e \le -4, base constraints, c=13c = 13 One-parameter guess; using ⌊n/2⌋≤n/2\lfloor n/2 \rfloor \le n/2 with d<0d < 0; not checking the base n=1n = 1
2.(a) 2 Run of the algorithm with the tie rule, trie, codewords, B=45=2+4+7+12+20B = 45 = 2 + 4 + 7 + 12 + 20 Wrong order at the tie; mixing frequencies and depths
2.(b) 4 Lemma on ∑Fi\sum F_i, invariant P(k)P(k) with the comb Ck+1C_{k+1}, strict inequalities, codewords 1n−j01^{n-j}0 and the two deepest leaves No induction (only examples); ignoring the tie; missing c1,c2c_1, c_2
2.(c) 2 B=∑k=2n(Fk+2−1)=Fn+4−n−4B = \sum_{k=2}^n (F_{k+2} - 1) = F_{n+4} - n - 4, average length tending to φ2\varphi^2, comparison 4545 against 6060 Off-by-one in the sum of the Fibonacci numbers; forgetting that the file has Fn+2−1F_{n+2} - 1 characters
2.(d) 3 Table mm and ss, 294294 with ((A1A2)(A3A4))((A_1 A_2)(A_3 A_4)), 1010 values of kk, left to right 404404 Wrong dimensions in pi−1pkpjp_{i-1} p_k p_j; not reconstructing the parenthesization