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

Algorithms in Engineering - Mock Exam 1

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 course in Italian (Algoritmi per l'Ingegneria, UniPD). The real test has two parts: a closed-answer questionnaire on the basics of the course (Part 1) and problems of design and analysis of an algorithm and of proof of a property (Part 2). The questionnaire of the real exams is not public: the number of questions, the points and the duration below are our proposal, drawn from the official sample questionnaires of the Italian course.

  • Part 1 (10 points): Quiz 1-10, four options each, exactly one correct, 1 point each; only the answer is written, without working.
  • Part 2 (22 points): Exercise 1 (11 points) and Exercise 2 (11 points).
  • Total: 32 points. Duration: 2 hours and 30 minutes (proposal). No notes, books or calculators.
  • Cost model, unless stated otherwise: only operations between scalars (sums, differences, products, comparisons between numbers) cost 11; assignments, copies and index computations cost 00.

The quizzes and the exercises are new: they are not the ones of the official tests nor of the other mock exams. Every result was checked by running a program: the loop counts, the recurrences (evaluated by running them), the operation count of the FFT (by instrumenting the code) and the DFT values against the direct definition; for Exercise 1 the closure, the symmetry and the commutativity on 1000 random pairs of matrices up to order 1616, the three-product algorithm against the matrix product, and the exact operation counts for n=1,…,128n = 1, \dots, 128; for Exercise 2 the greedy algorithm against all the permutations on 3000 random instances with up to 77 jobs, the substructure identity and the exchange step on every permutation. The correct letters of the quizzes are spread as A: 2, B: 2, C: 3, D: 3.

Quiz 1

An algorithm receives a positive integer mm written in binary on bb bits (so 2b−1≤m<2b2^{b-1} \le m < 2^b) and computes 1+2+⋯+m1 + 2 + \dots + m with a loop that performs m−1m - 1 additions between integers, each of unit cost. Taking as size the number bb of bits of the input, the number of additions is:

  • A. Θ(b)\Theta(b), linear in the size.
  • B. Θ(b2)\Theta(b^2), quadratic in the size.
  • C. Θ(2b)\Theta(2^b), exponential in the size.
  • D. Θ(log⁡b)\Theta(\log b), logarithmic in the size.
Show the result

C.

Show the full solution

The cost is a function of the size of the instance, not of the value it represents (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 →). Here the value mm lies between 2b−12^{b-1} and 2b−12^b - 1, so the number of additions m−1m - 1 lies between 2b−1−12^{b-1} - 1 and 2b−22^b - 2: it is Θ(2b)\Theta(2^b), exponential in the number of bits. The same happens with trial division to test primality (≈m=2b/2\approx \sqrt m = 2^{b/2} steps).

  • A, B, D are false: they treat mm as if it were the size ("Θ(m)\Theta(m) is linear") instead of its logarithm.

Typical mistake: answering "linear" because the loop is a single loop. It is linear in the value mm, not in the length of its encoding.

Quiz 2

How many times is the command x = x + 1 executed by the following fragment when n=8n = 8?

for i = 1 to n-1:
    for j = i+1 to n:
        for k = j to n:
            x = x + 1
  • A. 8484
  • B. 5656
  • C. 120120
  • D. 112112
Show the result

A. 8484.

Show the full solution

The three loops count the triples (i,j,k)(i, j, k) with 1≤i<j≤k≤n1 \le i < j \le k \le n. Group them by jj: for each jj there are j−1j - 1 values of ii (1≤i≤j−11 \le i \le j - 1) and n−j+1n - j + 1 values of kk (j≤k≤nj \le k \le n). With h=j−1h = j - 1: T(n)=∑j=2n(j−1)(n−j+1)=∑h=1n−1h (n−h)=n (n−1)n2−(n−1)n(2n−1)6=n(n−1)(n+1)6.T(n) = \sum_{j=2}^{n} (j-1)(n-j+1) = \sum_{h=1}^{n-1} h\,(n-h) = n\,\frac{(n-1)n}{2} - \frac{(n-1)n(2n-1)}{6} = \frac{n(n-1)(n+1)}{6}. For n=8n = 8: 8⋅7⋅96=84\frac{8 \cdot 7 \cdot 9}{6} = 84.

Second reading: the condition i<j≤ki < j \le k means i<j<k+1i < j < k + 1 with k+1≤n+1k + 1 \le n + 1, i.e. a choice of 33 distinct values out of n+1n + 1: (93)=84\binom{9}{3} = 84.

  • B ((83)=56\binom 83 = 56) counts the triples with k>jk > j strictly: it forgets that kk starts from jj.
  • C ((103)\binom{10}{3}) and D are results of wrong bounds.

Quiz 3

Let T(1)=2T(1) = 2 and T(n)=3 T(n/2)+nT(n) = 3\,T(n/2) + n for n=2k>1n = 2^k > 1. What is T(16)T(16)?

  • A. 130130
  • B. 324324
  • C. 276276
  • D. 292292
Show the result

D. T(16)=292T(16) = 292.

Show the full solution

Recurrence 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 →): the node of size mm has work mm, every node has 33 children of half size, the leaves (size 11) cost T(1)=2T(1) = 2.

level nodes size work per node level work
0 11 1616 1616 1616
1 33 88 88 2424
2 99 44 44 3636
3 2727 22 22 5454
4 (leaves) 8181 11 22 162162

Total 16+24+36+54+162=29216 + 24 + 36 + 54 + 162 = 292. Closed form: T(n)=4⋅3k−2nT(n) = 4 \cdot 3^k - 2n (check: k=4k = 4, 4⋅81−32=2924 \cdot 81 - 32 = 292; T(2)=3⋅2+2=8=12−4T(2) = 3 \cdot 2 + 2 = 8 = 12 - 4).

  • A is 130130: the leaves were counted as free (T(1)=0T(1) = 0).
  • B is 4⋅814 \cdot 81: the term −2n-2n was forgotten.
  • C is 292−16292 - 16: the work of the root was forgotten.

Quiz 4

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

  • A. T(n)=2 T(n/2)+n2T(n) = 2\,T(n/2) + n^2
  • B. T(n)=4 T(n/2)+n2T(n) = 4\,T(n/2) + n^2
  • C. T(n)=8 T(n/2)+n2T(n) = 8\,T(n/2) + n^2
  • D. T(n)=4 T(n/2)+n3T(n) = 4\,T(n/2) + n^3
Show the result

B.

Show the full solution

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

aa, bb threshold w(n)w(n) case result
A 22, 22 nn n2=Ω(n1+ε)n^2 = \Omega(n^{1+\varepsilon}), regularity 2(n/2)2=12n22(n/2)^2 = \frac12 n^2 3 Θ(n2)\Theta(n^2)
B 44, 22 n2n^2 n2=Θ(n2)n^2 = \Theta(n^2) 2 Θ(n2log⁡n)\Theta(n^2 \log n)
C 88, 22 n3n^3 n2=O(n3−ε)n^2 = O(n^{3-\varepsilon}) 1 Θ(n3)\Theta(n^3)
D 44, 22 n2n^2 n3=Ω(n2+ε)n^3 = \Omega(n^{2+\varepsilon}), regularity 4(n/2)3=12n34(n/2)^3 = \frac12 n^3 3 Θ(n3)\Theta(n^3)

The logarithmic factor appears only in the balanced case (Case 2): every level of the tree has the same work n2n^2 and there are log⁡2n\log_2 n levels.

Quiz 5

The product of two n×nn \times n matrices is computed with the hybrid algorithm: if n≤n0n \le n_0 it uses the iterative product, which performs 2n3−n22n^3 - n^2 operations between scalars; if n>n0n > n_0 it makes one step of Strassen's algorithm, i.e. 77 recursive calls on matrices of order n2\frac n2 plus 92n2\frac 92 n^2 operations between scalars for the divisions and the recombinations. How many operations between scalars does it perform for n=32n = 32 and n0=4n_0 = 4?

  • A. 6451264512
  • B. 7134471344
  • C. 6520065200
  • D. 111505111505
Show the result

C. 6520065200.

Show the full solution

The recursion tree is cut at size n0=4n_0 = 4 (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 →, Strassen's AlgorithmStrassen computes the n x n product with 7 recursive calls instead of 8, using 10 matrix additions in Divide and 8 in Conquer: T(n) = 7T(n/2) + (9/2)n^2, Theta(n^(log2 7)) with exact solution 7n^(log2 7) - 6n^2. Its leading constant 7 is more than three times that of the iterative algorithm (2), so Strassen beats it only from n = 655 (from 1024 for powers of two).Strassen's Algorithm →): the internal levels are m=log⁡2324=3m = \log_2 \frac{32}{4} = 3 (sizes 32,16,832, 16, 8) and the leaves have size 44.

  • Leaves: 73=3437^3 = 343 leaves, each costing 2⋅43−42=1122 \cdot 4^3 - 4^2 = 112: 343⋅112=38416343 \cdot 112 = 38416.
  • Internal levels: level ℓ\ell has 7ℓ7^\ell nodes of size 32/2ℓ32 / 2^\ell and work 92(322ℓ)2\frac 92 \left( \frac{32}{2^\ell} \right)^2: 92 (1024+7⋅256+49⋅64)=92⋅5952=26784.\tfrac 92 \,(1024 + 7 \cdot 256 + 49 \cdot 64) = \tfrac 92 \cdot 5952 = 26784.

Total 38416+26784=6520038416 + 26784 = 65200. With the closed form T(n,n0)=7m(2n03+5n02)−6n2T(n, n_0) = 7^m (2n_0^3 + 5n_0^2) - 6n^2: 343⋅(128+80)−6144=71344−6144=65200343 \cdot (128 + 80) - 6144 = 71344 - 6144 = 65200.

  • A is 2⋅323−3222 \cdot 32^3 - 32^2, the purely iterative algorithm (n0=32n_0 = 32): the hybrid with n0=4n_0 = 4 is slightly cheaper.
  • B is 343⋅208343 \cdot 208: the term −6n2-6n^2 was forgotten.
  • D is pure Strassen (n0=1n_0 = 1): 7⋅75−6⋅322=1115057 \cdot 7^5 - 6 \cdot 32^2 = 111505.

Quiz 6

Pure Strassen's algorithm (recursion down to 1×11 \times 1 matrices, one scalar product at the leaves) is applied to two matrices of order n=4n = 4. How many operations between scalars does it perform in total?

  • A. 247247
  • B. 112112
  • C. 343343
  • D. 175175
Show the result

A. 247247.

Show the full solution

T(1)=1T(1) = 1 and T(n)=7 T(n2)+92n2T(n) = 7\,T(\frac n2) + \frac 92 n^2 (Strassen's AlgorithmStrassen computes the n x n product with 7 recursive calls instead of 8, using 10 matrix additions in Divide and 8 in Conquer: T(n) = 7T(n/2) + (9/2)n^2, Theta(n^(log2 7)) with exact solution 7n^(log2 7) - 6n^2. Its leading constant 7 is more than three times that of the iterative algorithm (2), so Strassen beats it only from n = 655 (from 1024 for powers of two).Strassen's Algorithm →: 1010 additions in Divide and 88 in Conquer, i.e. 1818 operations on matrices of order n2\frac n2, each costing n24\frac{n^2}{4}):

  • T(2)=7⋅1+92⋅4=7+18=25T(2) = 7 \cdot 1 + \frac 92 \cdot 4 = 7 + 18 = 25;
  • T(4)=7⋅25+92⋅16=175+72=247T(4) = 7 \cdot 25 + \frac 92 \cdot 16 = 175 + 72 = 247.

Closed form: 7nlog⁡27−6n2=7⋅49−96=2477 n^{\log_2 7} - 6n^2 = 7 \cdot 49 - 96 = 247.

  • B is the iterative product, 2⋅43−42=1122 \cdot 4^3 - 4^2 = 112: for small nn it is cheaper than Strassen (247>112247 > 112).
  • D is 7⋅257 \cdot 25: the additions of the top level were forgotten.
  • C is 737^3: wrong depth of the recursion.

Quiz 7

What are the real part and the imaginary part of ω20135\omega_{20}^{135}, where ω20=e2πi/20\omega_{20} = e^{2\pi i / 20}?

  • A. Re=0\mathrm{Re} = 0 and Im=1\mathrm{Im} = 1
  • B. Re=1\mathrm{Re} = 1 and Im=0\mathrm{Im} = 0
  • C. Re=−1\mathrm{Re} = -1 and Im=0\mathrm{Im} = 0
  • D. Re=0\mathrm{Re} = 0 and Im=−1\mathrm{Im} = -1
Show the result

D. ω20135=−i\omega_{20}^{135} = -i.

Show the full solution

Powers of ωn\omega_n repeat with period nn (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 →): 135=6⋅20+15135 = 6 \cdot 20 + 15, so ω20135=ω2015\omega_{20}^{135} = \omega_{20}^{15}. By the cancellation lemma ωdndk=ωnk\omega_{dn}^{dk} = \omega_n^k with d=5d = 5, n=4n = 4, k=3k = 3: ω2015=ω43=i3=−i\omega_{20}^{15} = \omega_4^3 = i^3 = -i. In angles: 1520⋅360∘=270∘\frac{15}{20} \cdot 360^\circ = 270^\circ.

  • A is ω205\omega_{20}^5 (90∘90^\circ), B is ω200\omega_{20}^0, C is ω2010\omega_{20}^{10}.

Typical mistake: not reducing the exponent and getting the angle 135⋅18∘=2430∘135 \cdot 18^\circ = 2430^\circ without removing the full turns (2430−6⋅360=2702430 - 6 \cdot 360 = 270).

Quiz 8

Let x=(2,1,0,1)\mathbf x = (2, 1, 0, 1). What is y=DFT4(x)\mathbf y = \mathrm{DFT}_4(\mathbf x)?

  • A. (4,2,2,2)(4, 2, 2, 2)
  • B. (4,2,0,2)(4, 2, 0, 2)
  • C. (4,0,0,0)(4, 0, 0, 0)
  • D. (4,−2,0,−2)(4, -2, 0, -2)
Show the result

B. y=(4,2,0,2)\mathbf y = (4, 2, 0, 2).

Show the full solution

yi=∑j=03xj ω4ijy_i = \sum_{j=0}^{3} x_j\, \omega_4^{ij} with ω4=i\omega_4 = i and Ω4=(1,i,−1,−i)\Omega_4 = (1, i, -1, -i) (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 →):

  • y0=2+1+0+1=4y_0 = 2 + 1 + 0 + 1 = 4;
  • y1=2+1⋅i+0+1⋅i3=2+i−i=2y_1 = 2 + 1 \cdot i + 0 + 1 \cdot i^3 = 2 + i - i = 2;
  • y2=2+1⋅(−1)+0+1⋅(−1)3=2−1−1=0y_2 = 2 + 1 \cdot (-1) + 0 + 1 \cdot (-1)^3 = 2 - 1 - 1 = 0;
  • y3=2+1⋅i3+0+1⋅i9=2−i+i=2y_3 = 2 + 1 \cdot i^3 + 0 + 1 \cdot i^9 = 2 - i + i = 2.

Check with the FFT split (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 →): even indices (2,0)(2, 0) give (2,2)(2, 2), odd indices (1,1)(1, 1) give (2,0)(2, 0); then y0=2+2=4y_0 = 2 + 2 = 4, y2=2−2=0y_2 = 2 - 2 = 0, y1=2+i⋅0=2y_1 = 2 + i \cdot 0 = 2, y3=2−i⋅0=2y_3 = 2 - i \cdot 0 = 2. Since x\mathbf x is real and symmetric, y\mathbf y is real, with y1=y3y_1 = y_3.

  • D has the wrong sign on the odd components; C would be the DFT of a constant vector.

Quiz 9

In the FFT algorithm of the course each iteration of the final loop (the one that combines the two half-size results) performs 44 operations between complex numbers: the product ωni yi[1]\omega_n^i\, y^{[1]}_i, a sum, a difference and the update of the current power of ωn\omega_n. How many operations between complex numbers does the algorithm perform in total on a vector of length n=64n = 64?

  • A. 384384
  • B. 576576
  • C. 768768
  • D. 80648064
Show the result

C. 768768.

Show the full solution

The loop has n2\frac n2 iterations, so the work of a node of size nn is 4⋅n2=2n4 \cdot \frac n2 = 2n and T(1)=0T(1) = 0, T(n)=2 T(n2)+2nT(n) = 2\,T(\frac n2) + 2n (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 →). Every level of the tree costs 2n2n in total and there are log⁡2n\log_2 n levels of internal nodes (the leaves cost nothing): T(n)=2nlog⁡2nT(n) = 2n \log_2 n. For n=64n = 64: 2⋅64⋅6=7682 \cdot 64 \cdot 6 = 768. Check by the Master Theorem: a=b=2a = b = 2, w=2n=Θ(n)w = 2n = \Theta(n), Case 2, Θ(nlog⁡n)\Theta(n \log n) (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 →).

  • A is nlog⁡2nn \log_2 n (one operation per pair of values).
  • B is 32nlog⁡2n\frac 32 n \log_2 n, the count with the powers of ωn\omega_n precomputed (one product and two sums per iteration).
  • D is 2n(n−1)2n(n-1), the cost of evaluating the polynomial at the nn roots with Horner's rule.

Quiz 10

Let T(1)=1T(1) = 1 and T(n)=4 T(n/2)+n2log⁡2nT(n) = 4\,T(n/2) + n^2 \log_2 n for nn a power of 22. Which statement is true?

  • A. By Case 1 of the Master Theorem, T(n)=Θ(n2)T(n) = \Theta(n^2).
  • B. By Case 2 of the Master Theorem, T(n)=Θ(n2log⁡n)T(n) = \Theta(n^2 \log n).
  • C. By Case 3 of the Master Theorem, T(n)=Θ(n2log⁡n)T(n) = \Theta(n^2 \log n).
  • D. The Master Theorem does not apply; unfolding the recurrence gives T(n)=Θ(n2log⁡2n)T(n) = \Theta(n^2 \log^2 n).
Show the result

D.

Show the full solution

The threshold is nlog⁡24=n2n^{\log_2 4} = n^2 and w(n)=n2log⁡2nw(n) = n^2 \log_2 n (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 →). Case 1 needs w=O(n2−ε)w = O(n^{2-\varepsilon}) and Case 2 needs w=Θ(n2)w = \Theta(n^2): both false, because ww is larger than n2n^2. Case 3 needs w=Ω(n2+ε)w = \Omega(n^{2+\varepsilon}) for some ε>0\varepsilon > 0, but log⁡nnε→0\frac{\log n}{n^\varepsilon} \to 0: the gap is only logarithmic, so Case 3 fails too. The theorem does not apply.

Unfolding with the general formula (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 →), n=2kn = 2^k: level ℓ\ell has 4ℓ4^\ell nodes of work (n2ℓ)2log⁡2n2ℓ\left( \frac{n}{2^\ell} \right)^2 \log_2 \frac{n}{2^\ell}, i.e. n2(k−ℓ)n^2 (k - \ell) per level, and there are 4k=n24^k = n^2 leaves of cost 11: T(n)=n2∑ℓ=0k−1(k−ℓ)+n2=n2(k(k+1)2+1)=Θ(n2log⁡2n).T(n) = n^2 \sum_{\ell=0}^{k-1} (k - \ell) + n^2 = n^2 \left( \frac{k(k+1)}{2} + 1 \right) = \Theta(n^2 \log^2 n). Check: T(2)=4+4=8T(2) = 4 + 4 = 8 and the formula gives 4⋅(1+1)=84 \cdot (1 + 1) = 8; T(4)=32+16⋅2=64=16⋅4T(4) = 32 + 16 \cdot 2 = 64 = 16 \cdot 4.

Applying Case 2 or Case 3 anyway (options B and C) gives the wrong order n2log⁡nn^2 \log n.

Exercise 1 (points 11)

A matrix of order n=2kn = 2^k, k≥0k \ge 0, with complex entries is called a Fibonacci-block matrix (FBM) if n=1n = 1 (any scalar), or n>1n > 1 and M=(ABBA+B)M = \begin{pmatrix} A & B \\ B & A + B \end{pmatrix} with AA and BB FBM of order n2\frac n2. For example, an FBM of order 22 is (abba+b)\begin{pmatrix} a & b \\ b & a+b \end{pmatrix}; taking A=(1112)A = \begin{pmatrix} 1 & 1 \\ 1 & 2 \end{pmatrix} and B=(2002)B = \begin{pmatrix} 2 & 0 \\ 0 & 2 \end{pmatrix} one gets the FBM of order 44 M=(1120120220310214).M = \begin{pmatrix} 1 & 1 & 2 & 0 \\ 1 & 2 & 0 & 2 \\ 2 & 0 & 3 & 1 \\ 0 & 2 & 1 & 4 \end{pmatrix}. The cost model counts the operations between scalars (cost 11); assignments and copies are free.

  • (a) [3 points] Prove by induction on kk that the FBM of order nn are closed under sum, difference and product, that every FBM is symmetric and that two FBM of the same order commute.
  • (b) [1 point] Prove that an FBM of order nn is determined by its first row, and give the exact number of operations needed to sum two FBM given by their first rows.
  • (c) [3 points] The block formula of the product needs 44 recursive products of matrices of order n2\frac n2 (besides sums). Design an algorithm FB_MUL, which receives the first rows of two FBM and returns the first row of their product, with only 33 recursive calls. Write the pseudocode and prove that it is correct.
  • (d) [3 points] Write the recurrence for the number T(n)T(n) of operations of FB_MUL, solve it exactly with the general formula for recurrences, check the formula for n=1,2,4n = 1, 2, 4, and give the order of growth with the Master Theorem.
  • (e) [1 point] Apply FB_MUL to the FBM of order 44 with first rows r=(1,1,2,0)\mathbf r = (1, 1, 2, 0) and s=(2,0,1,1)\mathbf s = (2, 0, 1, 1) and say how many operations are performed. How many would the algorithm with 44 recursive calls perform for n=4n = 4?
Show the result

(a) The FBM are closed under sum, difference, scalar multiple and product; they are symmetric and commutative. For M=(ABBA+B)M = \begin{pmatrix} A & B \\ B & A+B \end{pmatrix}, N=(CDDC+D)N = \begin{pmatrix} C & D \\ D & C+D \end{pmatrix}: MN=(EFFE+F)MN = \begin{pmatrix} E & F \\ F & E+F \end{pmatrix} with E=AC+BDE = AC + BD, F=AD+BC+BDF = AD + BC + BD.

(b) The first row of MM is (r(A)∣r(B))(\mathbf r(A) \mid \mathbf r(B)), which determines MM: there are only nn free parameters. Sum of two FBM: exactly nn operations (sum of the first rows).

(c) U=ACU = AC, V=BDV = BD, Z=(A+B)(C+D)Z = (A+B)(C+D); then E=U+VE = U + V and F=Z−UF = Z - U. The first row of the product is (r(E)∣r(F))(\mathbf r(E) \mid \mathbf r(F)).

(d) T(1)=1T(1) = 1, T(n)=3 T(n2)+2nT(n) = 3\,T(\frac n2) + 2n; T(n)=5 nlog⁡23−4nT(n) = 5\,n^{\log_2 3} - 4n (so T(1)=1T(1) = 1, T(2)=7T(2) = 7, T(4)=29T(4) = 29); T(n)=Θ(nlog⁡23)≈Θ(n1.585)T(n) = \Theta(n^{\log_2 3}) \approx \Theta(n^{1.585}) (Case 1).

(e) r(MN)=(4,4,8,5)\mathbf r(MN) = (4, 4, 8, 5), with 2929 operations. With 44 recursive calls: T4(n)=5n2−3n2T_4(n) = \frac{5n^2 - 3n}{2}, so 3434 operations for n=4n = 4.

Show the full solution

(a) Closure, symmetry, commutativity. Let Fk\mathcal F_k be the set of FBM of order 2k2^k. We prove by induction on kk the statement P(k)P(k): for all M,N∈FkM, N \in \mathcal F_k, (i) MT=MM^T = M; (ii) M+NM + N, M−NM - N and cMcM (cc a scalar) belong to Fk\mathcal F_k; (iii) MN∈FkMN \in \mathcal F_k and MN=NMMN = NM. The method is the one for the structured families (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 →, 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 →): write the block product with the row-by-column rule and use the hypothesis on the blocks.

Base k=0k = 0. Scalars: a 1×11 \times 1 matrix is symmetric, sums, products and multiples of scalars are scalars, and scalars commute.

Step. Assume P(k−1)P(k-1) and let M=(ABBA+B)M = \begin{pmatrix} A & B \\ B & A+B \end{pmatrix}, N=(CDDC+D)N = \begin{pmatrix} C & D \\ D & C+D \end{pmatrix} with A,B,C,D∈Fk−1A, B, C, D \in \mathcal F_{k-1}. (Note that A+BA + B and C+DC + D are in Fk−1\mathcal F_{k-1} by (ii), so the definition is consistent.)

(i) Symmetry. Transposing a block matrix transposes each block and exchanges the off-diagonal ones: MT=(ATBTBT(A+B)T)=MM^T = \begin{pmatrix} A^T & B^T \\ B^T & (A+B)^T \end{pmatrix} = M, because AA, BB and A+BA + B are symmetric by hypothesis.

(ii) Sum, difference, multiple. M+N=(A+CB+DB+D(A+C)+(B+D))M + N = \begin{pmatrix} A + C & B + D \\ B + D & (A + C) + (B + D) \end{pmatrix}, which has the form of an FBM with blocks A′=A+CA' = A + C and B′=B+DB' = B + D, in Fk−1\mathcal F_{k-1} by hypothesis. The same holds for M−NM - N and cMcM.

(iii) Product. Block row-by-column product (fractal property of the product, 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 →); the order of the factors is kept:

  • top-left: AC+BD=:EAC + BD =: E;
  • top-right: AD+B(C+D)=AD+BC+BD=:FAD + B(C + D) = AD + BC + BD =: F;
  • bottom-left: BC+(A+B)D=BC+AD+BDBC + (A + B)D = BC + AD + BD, the same sum as FF (same products, in the same order);
  • bottom-right: BD+(A+B)(C+D)=AC+AD+BC+2BD=(AC+BD)+(AD+BC+BD)=E+FBD + (A+B)(C+D) = AC + AD + BC + 2BD = (AC + BD) + (AD + BC + BD) = E + F.

So MN=(EFFE+F)MN = \begin{pmatrix} E & F \\ F & E + F \end{pmatrix}, where EE and FF are sums of products of FBM of order n2\frac n2, hence in Fk−1\mathcal F_{k-1} by (ii) and (iii) at order k−1k - 1: MNMN is an FBM. For the commutativity compute NMNM in the same way: its blocks are E′=CA+DBE' = CA + DB and F′=CB+DA+DBF' = CB + DA + DB. By the commutativity at order k−1k - 1, CA=ACCA = AC, DB=BDDB = BD, CB=BCCB = BC, DA=ADDA = AD, so E′=EE' = E and F′=FF' = F and NM=MNNM = MN. ■\blacksquare

Typical mistakes: checking only the sum and forgetting the product (the delicate part); checking that the bottom-left block equals the top-right one by assuming commutativity of blocks when it is not needed (here the two expressions coincide term by term); using the symmetry of MM without proving it for A+BA + B.

(b) First row. The first row of M=(ABBA+B)M = \begin{pmatrix} A & B \\ B & A+B \end{pmatrix} is the concatenation r(M)=(r(A)∣r(B))\mathbf r(M) = (\mathbf r(A) \mid \mathbf r(B)) of the first rows of AA and BB. By induction on kk the first row determines the matrix: for n=1n = 1 the matrix is its only entry; for n>1n > 1, r(M)\mathbf r(M) gives r(A)\mathbf r(A) and r(B)\mathbf r(B) (its two halves), these give AA and BB by hypothesis and then MM. In other words, if p(n)p(n) is the number of free parameters, p(1)=1p(1) = 1 and p(n)=2 p(n2)p(n) = 2\,p(\frac n2) (the block A+BA + B brings no new information), so p(n)=np(n) = n and not n2n^2.

Sum. By (a), r(M+N)=r(M)+r(N)\mathbf r(M + N) = \mathbf r(M) + \mathbf r(N) (componentwise): exactly nn operations. Recursively, TS(1)=1T_S(1) = 1 and TS(n)=2 TS(n2)T_S(n) = 2\,T_S(\frac n2), i.e. TS(n)=nT_S(n) = n. Subtraction costs the same.

Why the algorithm works on first rows. If FB_MUL returned the whole matrix, it would have to build the block E+FE + F at each level, i.e. one more sum at every node, which breaks the count. With the first rows, the block E+FE + F is never needed: it is determined by EE and FF and is never computed.

(c) Three recursive calls. The block formula of (a) needs the four products ACAC, BDBD, ADAD, BCBC (and BDBD again, computed once). The trick is the one of Gauss for complex numbers and of Karatsuba (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 →, model "Gauss"): put U=AC,V=BD,Z=(A+B)(C+D)=AC+AD+BC+BD.U = AC, \qquad V = BD, \qquad Z = (A + B)(C + D) = AC + AD + BC + BD. Then E=AC+BD=U+V,F=AD+BC+BD=Z−U.E = AC + BD = U + V, \qquad F = AD + BC + BD = Z - U. The factors A+BA + B and C+DC + D are FBM by (a), so all three products are products of two FBM of order n2\frac n2 and the recursion is possible.

FB_MUL(a, b)                  { a, b: first rows of two FBM of order n = 2^k }
  n <- length(a)
  if n = 1 then return [a[1] * b[1]]
  * a = (a1 | a2),  b = (b1 | b2): halves of length n/2 *
  ap <- VSUM(a1, a2);  bp <- VSUM(b1, b2)       { DIVIDE: 2 sums of vectors of length n/2 }
  U <- FB_MUL(a1, b1)                           { RECURSE: 3 calls }
  V <- FB_MUL(a2, b2)
  Z <- FB_MUL(ap, bp)
  E <- VSUM(U, V);  F <- VSUB(Z, U)             { CONQUER: 2 operations on vectors of length n/2 }
  return (E | F)                                { concatenation: first row of the product }

(VSUM and VSUB are the componentwise sum and difference of two vectors of the same length mm, with mm operations.)

Correctness (induction on kk). The statement is: FB_MUL returns the first row of MNMN when a=r(M)\mathbf a = \mathbf r(M) and b=r(N)\mathbf b = \mathbf r(N). Base: n=1n = 1, the product of two scalars. Step: a1\mathbf a_1, a2\mathbf a_2, b1\mathbf b_1, b2\mathbf b_2 are the first rows of AA, BB, CC, DD; by (b) ap and bp are the first rows of A+BA + B and C+DC + D. By the induction hypothesis UU, VV, ZZ are the first rows of ACAC, BDBD, (A+B)(C+D)(A+B)(C+D). By the linearity of the first row, E=U+VE = U + V is the first row of AC+BDAC + BD and F=Z−UF = Z - U is the first row of Zmat−AC=AD+BC+BDZ_{mat} - AC = AD + BC + BD. By (a) the first row of MNMN is (r(Emat)∣r(Fmat))(\mathbf r(E_{mat}) \mid \mathbf r(F_{mat})). ■\blacksquare

(d) Recurrence and solution. The internal work of a node of size nn is: 22 sums in Divide and 22 operations in Conquer, each on vectors of length n2\frac n2 and cost n2\frac n2: 4⋅n2=2n4 \cdot \frac n2 = 2n (the concatenation is free). So T(1)=1,T(n)=3 T ⁣(n2)+2n(n≥2).T(1) = 1, \qquad T(n) = 3\,T\!\left(\tfrac n2\right) + 2n \quad (n \ge 2). General formula (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 →) with s=3s = 3, f(n)=n2f(n) = \frac n2, w(n)=2nw(n) = 2n, n0=1n_0 = 1, T0=1T_0 = 1, n=2kn = 2^k: level ℓ\ell (0≤ℓ≤k−10 \le \ell \le k - 1) has 3ℓ3^\ell nodes of work 2n2ℓ\frac{2n}{2^\ell}, and there are 3k=nlog⁡233^k = n^{\log_2 3} leaves of cost 11: T(n)=∑ℓ=0k−13ℓ⋅2n2ℓ+3k=2n⋅(3/2)k−13/2−1+3k=4⋅3k−4n+3k=5⋅3k−4n=5 nlog⁡23−4n,T(n) = \sum_{\ell=0}^{k-1} 3^\ell \cdot \frac{2n}{2^\ell} + 3^k = 2n \cdot \frac{(3/2)^k - 1}{3/2 - 1} + 3^k = 4 \cdot 3^k - 4n + 3^k = 5 \cdot 3^k - 4n = 5\,n^{\log_2 3} - 4n, using n(32)k=3kn \left( \frac 32 \right)^k = 3^k. Check: T(1)=5−4=1T(1) = 5 - 4 = 1; T(2)=15−8=7T(2) = 15 - 8 = 7 (directly: 3⋅1+2⋅2=73 \cdot 1 + 2 \cdot 2 = 7); T(4)=45−16=29T(4) = 45 - 16 = 29 (directly: 3⋅7+8=293 \cdot 7 + 8 = 29).

Master Theorem (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 →): a=3a = 3, b=2b = 2, w(n)=2nw(n) = 2n, threshold nlog⁡23≈n1.585n^{\log_2 3} \approx n^{1.585}. Since 2n=O(nlog⁡23−ε)2n = O(n^{\log_2 3 - \varepsilon}) with ε=0.5\varepsilon = 0.5, it is Case 1 (the leaves dominate): T(n)=Θ(nlog⁡23)T(n) = \Theta(n^{\log_2 3}). This is much less than Θ(n2)\Theta(n^2), the size of the matrix, because the matrix has only nn free parameters.

Typical mistakes: writing w(n)=4nw(n) = 4n (counting the four operations at size nn instead of n2\frac n2); forgetting that A+BA + B and C+DC + D need a sum each; counting T(1)=0T(1) = 0; applying Case 2 because "ww is linear".

(e) Numerical example and comparison. r=(1,1,2,0)\mathbf r = (1, 1, 2, 0) is the first row of the matrix MM shown in the text; s=(2,0,1,1)\mathbf s = (2, 0, 1, 1) is that of N=(2011021211311214)N = \begin{pmatrix} 2 & 0 & 1 & 1 \\ 0 & 2 & 1 & 2 \\ 1 & 1 & 3 & 1 \\ 1 & 2 & 1 & 4 \end{pmatrix}. At the top level a1=(1,1)\mathbf a_1 = (1, 1), a2=(2,0)\mathbf a_2 = (2, 0), b1=(2,0)\mathbf b_1 = (2, 0), b2=(1,1)\mathbf b_2 = (1, 1), ap =(3,1)= (3, 1), bp =(3,1)= (3, 1) (4 operations). The three recursive calls (each on length 22, cost 77):

call a1,a2a_1, a_2 b1,b2b_1, b_2 UU VV Z=(a1+a2)(b1+b2)Z = (a_1 + a_2)(b_1 + b_2) result (U+V∣Z−U)(U + V \mid Z - U)
UU 1,11, 1 2,02, 0 22 00 2⋅2=42 \cdot 2 = 4 (2∣2)(2 \mid 2)
VV 2,02, 0 1,11, 1 22 00 2⋅2=42 \cdot 2 = 4 (2∣2)(2 \mid 2)
ZZ 3,13, 1 3,13, 1 99 11 4⋅4=164 \cdot 4 = 16 (10∣7)(10 \mid 7)

Conquer: E=U+V=(2,2)+(2,2)=(4,4)E = U + V = (2, 2) + (2, 2) = (4, 4) and F=Z−U=(10,7)−(2,2)=(8,5)F = Z - U = (10, 7) - (2, 2) = (8, 5). The first row of the product is (4,4,8,5)(4, 4, 8, 5). Check with the matrices: the first row of MNMN is (1⋅2+1⋅0+2⋅1+0⋅1, … )=(4,4,8,5)(1 \cdot 2 + 1 \cdot 0 + 2 \cdot 1 + 0 \cdot 1,\ \dots) = (4, 4, 8, 5) and the whole product is MN=(44854851385129513921),MN = \begin{pmatrix} 4 & 4 & 8 & 5 \\ 4 & 8 & 5 & 13 \\ 8 & 5 & 12 & 9 \\ 5 & 13 & 9 & 21 \end{pmatrix}, again an FBM (blocks E=(4448)E = \begin{pmatrix} 4 & 4 \\ 4 & 8 \end{pmatrix}, F=(85513)F = \begin{pmatrix} 8 & 5 \\ 5 & 13 \end{pmatrix} and E+F=(129921)E + F = \begin{pmatrix} 12 & 9 \\ 9 & 21 \end{pmatrix}). Operations: 3⋅7+8=29=T(4)3 \cdot 7 + 8 = 29 = T(4).

Four recursive calls. The algorithm that computes ACAC, BDBD, ADAD, BCBC and then E=AC+BDE = AC + BD (n2\frac n2 operations) and F=(AD+BC)+BDF = (AD + BC) + BD (2⋅n22 \cdot \frac n2 operations) has T4(1)=1T_4(1) = 1 and T4(n)=4 T4(n2)+3n2T_4(n) = 4\,T_4(\frac n2) + \frac{3n}{2}, so T4(n)=5n2−3n2T_4(n) = \frac{5n^2 - 3n}{2} (Case 1 with threshold n2n^2). Comparison:

nn 11 22 44 88 1616 3232
T(n)T(n), three calls 11 77 2929 103103 341341 10871087
T4(n)T_4(n), four calls 11 77 3434 148148 616616 25122512

For n=4n = 4: 3434 operations against 2929. The three-call algorithm is never worse and wins from n=4n = 4 on; the gap grows as n2n^2 against n1.585n^{1.585}.

Exercise 2 (points 11)

Scheduling with deadlines. A machine must execute n≥1n \ge 1 jobs J1,…,JnJ_1, \dots, J_n one after the other, without interruptions and without idle time, starting at time 00. The job JiJ_i needs a time tit_i (a positive integer) and has a deadline did_i (an integer, not necessarily positive). A schedule is a bijection π:{1,…,n}→{1,…,n}\pi : \{1, \dots, n\} \to \{1, \dots, n\}: the jobs are executed in the order ⟨Jπ(1),…,Jπ(n)⟩\langle J_{\pi(1)}, \dots, J_{\pi(n)} \rangle. Under π\pi the completion time of the ii-th executed job is Ciπ=∑j=1itπ(j),C_i^\pi = \sum_{j=1}^{i} t_{\pi(j)}, its lateness is Ciπ−dπ(i)C_i^\pi - d_{\pi(i)} (negative if the job finishes early), and the cost of the schedule is the maximum lateness Lπ=max⁡1≤i≤n(Ciπ−dπ(i)).L^\pi = \max_{1 \le i \le n} \left( C_i^\pi - d_{\pi(i)} \right). We look for a schedule that minimizes LπL^\pi. Example: for t=(1,3)t = (1, 3) and d=(10,3)d = (10, 3), the order ⟨J1,J2⟩\langle J_1, J_2 \rangle has completion times 1,41, 4 and latenesses −9,1-9, 1, so L=1L = 1.

  • (a) [3 points] Give a greedy choice and the clean-up, and write the pseudocode of the resulting algorithm. Show with a counterexample that the rule "execute first the job with the shortest duration" is not optimal.
  • (b) [4 points] Prove the greedy choice property: there is an optimal schedule that starts with the greedy choice.
  • (c) [3 points] Prove the optimal substructure property and, by induction on nn, the correctness of the algorithm.
  • (d) [1 point] Give the cost of the algorithm, and apply it to t=(3,2,4,1,2,5)t = (3, 2, 4, 1, 2, 5) and d=(9,4,14,3,7,12)d = (9, 4, 14, 3, 7, 12), giving the schedule and its maximum lateness.
Show the result

(a) Greedy choice: a job of minimum deadline (earliest deadline first). Clean-up: none; the residual instance is formed by the other n−1n - 1 jobs, with deadlines dj−ted_j - t_e (where ee is the chosen job). The algorithm sorts the jobs by non-decreasing deadline. Counterexample for the shortest job first: t=(1,3)t = (1, 3), d=(10,3)d = (10, 3): shortest first gives L=1L = 1, earliest deadline first gives L=0L = 0.

(b) If ee has minimum deadline and is in position kk of an optimal schedule, moving it to the front gives a schedule that is not worse. (There is an optimal schedule starting with ee; not every optimal schedule does.)

(c) opt(J)=max⁡(te−de, opt(J′))\mathrm{opt}(J) = \max\bigl( t_e - d_e,\ \mathrm{opt}(J') \bigr), with J′J' the residual instance.

(d) O(nlog⁡n)O(n \log n) (sorting) plus nn steps of scan. For the example: order J4,J2,J5,J1,J6,J3J_4, J_2, J_5, J_1, J_6, J_3, completion times 1,3,5,8,13,171, 3, 5, 8, 13, 17, latenesses −2,−1,−2,−1,1,3-2, -1, -2, -1, 1, 3, maximum lateness 33 (shortest first would give 55).

Show the full solution

(a) Greedy choice, clean-up, algorithm. The method is the one of the greedy exercises (Greedy Paradigm and Activity Selectionthe greedy paradigm makes the locally best choice immediately, cleans up the instance and solves the single residual subinstance (tail recursion, hence a loop); the code is easy and correctness is not, requiring the greedy choice property and optimal substructure; case study activity selection with the earliest-finish rule, full proof, Theta(n) after sorting, and counterexamples for wrong greedy rules.Greedy Paradigm and Activity Selection →, 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 →): make the first choice immediately, clean up, and solve the residual instance.

  • Greedy choice: execute first a job JeJ_e whose deadline ded_e is minimum (ties: any).
  • Clean-up: none, because no job becomes incompatible. The residual instance J′J' consists of the other n−1n - 1 jobs; since they start at time tet_e and not at time 00, their deadlines are shifted: dj′=dj−ted'_j = d_j - t_e (this is why the deadlines are allowed to be negative: the residual has the same form as the original problem).
REC_EDF(J)                      { J: jobs with times t and deadlines d }
  if J = {} then return <>
  e <- a job of J with minimum deadline                    // greedy choice
  J' <- { (t_j, d_j - t_e) : J_j in J, j != e }            // residual instance
  return <J_e> followed by REC_EDF(J')

The recursion is a tail recursion. Since the shift dj−ted_j - t_e is the same for all the remaining jobs, it does not change their order, so all the choices follow the order of the original deadlines and the algorithm is a sort:

EDF_SCHEDULE(t, d)
  n <- t.len
  sort the indices 1..n by non-decreasing d              // O(n log n)
  pi[1..n] <- the sorted indices
  C <- 0;  L <- -infinity                                // L = maximum lateness so far
  for i <- 1 to n do
    C <- C + t[pi[i]]                                    // completion time of the i-th job
    L <- max(L, C - d[pi[i]])
  return (pi, L)

The shortest job first is not optimal. Take t=(1,3)t = (1, 3), d=(10,3)d = (10, 3). Shortest first executes J1J_1 and then J2J_2: completion times 1,41, 4, latenesses 1−10=−91 - 10 = -9 and 4−3=14 - 3 = 1, so L=1L = 1. Earliest deadline first executes J2J_2 and then J1J_1: completion times 3,43, 4, latenesses 3−3=03 - 3 = 0 and 4−10=−64 - 10 = -6, so L=0<1L = 0 < 1. One instance is enough to disprove a greedy rule. (Also "least slack di−tid_i - t_i first" is wrong: with t=(1,10)t = (1, 10), d=(2,10)d = (2, 10) it executes J2J_2 first, finishing at time 1010 with lateness 00, and then J1J_1 at time 1111 with lateness 99, while the order J1,J2J_1, J_2 has latenesses −1-1 and 11.)

(b) Greedy choice property. Claim: if JeJ_e has minimum deadline, there is an optimal schedule whose first job is JeJ_e.

Proof (exchange). Let π∗\pi^\ast be an optimal schedule, with completion times C∗C^\ast, and suppose that JeJ_e is in position k>1k > 1 (if k=1k = 1 there is nothing to prove). Build π^\hat\pi by moving JeJ_e to the front, leaving the others in the same relative order: π^=⟨Je, Jπ∗(1),…,Jπ∗(k−1), Jπ∗(k+1),…,Jπ∗(n)⟩\hat\pi = \langle J_e,\ J_{\pi^\ast(1)}, \dots, J_{\pi^\ast(k-1)},\ J_{\pi^\ast(k+1)}, \dots, J_{\pi^\ast(n)} \rangle. Compare the latenesses, calling Λ=Ck∗−de\Lambda = C^\ast_k - d_e the lateness of JeJ_e in π∗\pi^\ast (so Λ≤Lπ∗\Lambda \le L^{\pi^\ast}):

  1. JeJ_e. It now completes at te≤Ck∗t_e \le C^\ast_k (in π∗\pi^\ast it completes after the jobs before it and its own time). Its lateness is te−de≤Ck∗−de=Λ≤Lπ∗t_e - d_e \le C^\ast_k - d_e = \Lambda \le L^{\pi^\ast}.
  2. The jobs JjJ_j in positions 1,…,k−11, \dots, k-1 of π∗\pi^\ast. They are delayed by tet_e: the new completion time is Ci∗+teC^\ast_i + t_e (for the job in position ii). Since Ck∗=Ck−1∗+te≥Ci∗+teC^\ast_k = C^\ast_{k-1} + t_e \ge C^\ast_i + t_e for i≤k−1i \le k - 1, the new completion time is at most Ck∗C^\ast_k. Together with dj≥ded_j \ge d_e (minimality of ded_e): (Ci∗+te)−dj≤Ck∗−de=Λ≤Lπ∗.(C^\ast_i + t_e) - d_j \le C^\ast_k - d_e = \Lambda \le L^{\pi^\ast}.
  3. The jobs in positions k+1,…,nk+1, \dots, n. Before them are executed the same set of jobs as in π∗\pi^\ast, so their completion times, and latenesses, are unchanged.

Every lateness in π^\hat\pi is at most Lπ∗L^{\pi^\ast}, hence Lπ^≤Lπ∗L^{\hat\pi} \le L^{\pi^\ast}; by the optimality of π∗\pi^\ast, Lπ^=Lπ∗L^{\hat\pi} = L^{\pi^\ast}: π^\hat\pi is optimal and starts with JeJ_e. □\square

Remark. The claim says "there is", not "every". In the example of (d) there are 1212 optimal schedules, and some of them start with J2J_2 (deadline 4>34 > 3): for instance J2,J4,J5,J1,J6,J3J_2, J_4, J_5, J_1, J_6, J_3 has the same maximum lateness 33. This is different from the shortest-job-first property of the scheduling that minimizes the waiting time, which holds for every optimum.

Typical mistakes: checking only JeJ_e and forgetting that the jobs it overtakes are delayed; not using dj≥ded_j \ge d_e to bound the latenesses of the delayed jobs; swapping JeJ_e with the first job instead of moving it (a swap also changes the completion times of all the jobs in between, and the argument does not close).

(c) Optimal substructure. Let JeJ_e be a job of minimum deadline, J′J' the residual instance (the other jobs with deadlines dj−ted_j - t_e) and opt(J)\mathrm{opt}(J) the minimum maximum lateness. Claim: opt(J)=max⁡(te−de, opt(J′)),opt(J′)=−∞ if J′=∅.\mathrm{opt}(J) = \max\bigl( t_e - d_e,\ \mathrm{opt}(J') \bigr), \qquad \mathrm{opt}(J') = -\infty \text{ if } J' = \emptyset.

Proof. Take any schedule π\pi of JJ that starts with JeJ_e and let π′\pi' be the schedule of the other jobs, in the same order (a schedule of J′J'). The job in position i≥2i \ge 2 of π\pi completes at Ciπ=te+Ci−1π′C_i^\pi = t_e + C_{i-1}^{\pi'}, where Cπ′C^{\pi'} are the completion times in J′J' (which starts at time 00). Its lateness is therefore Ciπ−dπ(i)=Ci−1π′−(dπ(i)−te)=lateness of the same job in J′.C_i^\pi - d_{\pi(i)} = C_{i-1}^{\pi'} - (d_{\pi(i)} - t_e) = \text{lateness of the same job in } J'. So, exactly, Lπ=max⁡(te−de, Lπ′(J′)),L^\pi = \max\bigl( t_e - d_e,\ L^{\pi'}(J') \bigr), where the first term is the lateness of JeJ_e (completing at tet_e). The function x↦max⁡(te−de,x)x \mapsto \max(t_e - d_e, x) is non-decreasing, so among the schedules starting with JeJ_e the minimum is obtained with an optimal π′\pi' for J′J' (cut-and-paste: if π′\pi' were not optimal for J′J', replacing it with an optimal one would not increase LπL^\pi). Hence the best schedule starting with JeJ_e costs max⁡(te−de,opt(J′))\max(t_e - d_e, \mathrm{opt}(J')). By (b) some optimal schedule of JJ starts with JeJ_e, so this value is opt(J)\mathrm{opt}(J). □\square

(The cost is combined with a maximum, not with a sum as in the waiting-time problem; this is why the substructure is an equality of the form max⁡(⋅,⋅)\max(\cdot, \cdot).)

Correctness of the algorithm (Greedy Paradigm and Activity Selectionthe greedy paradigm makes the locally best choice immediately, cleans up the instance and solves the single residual subinstance (tail recursion, hence a loop); the code is easy and correctness is not, requiring the greedy choice property and optimal substructure; case study activity selection with the earliest-finish rule, full proof, Theta(n) after sorting, and counterexamples for wrong greedy rules.Greedy Paradigm and Activity Selection →): induction on the number nn of jobs. Base n=1n = 1: the only schedule is optimal. Step n≥2n \ge 2: assume that the algorithm is correct on instances with n−1n - 1 jobs, in particular on J′J'. REC_EDF places JeJ_e first and then the schedule π′\pi' of J′J', optimal by induction. By the identity above its maximum lateness is max⁡(te−de,opt(J′))=opt(J)\max(t_e - d_e, \mathrm{opt}(J')) = \mathrm{opt}(J) by the substructure property. The iterative algorithm makes the same choices, since the shift of the deadlines does not change their order. □\square

(d) Cost and example. Sorting by deadline costs O(nlog⁡n)O(n \log n) comparisons (for instance with merge sort, at most n⌈log⁡2n⌉n \lceil \log_2 n \rceil); the loop does nn iterations, each with 11 sum, 11 difference and 11 comparison, i.e. Θ(n)\Theta(n). In total O(nlog⁡n)O(n \log n), dominated by the sort.

Example t=(3,2,4,1,2,5)t = (3, 2, 4, 1, 2, 5), d=(9,4,14,3,7,12)d = (9, 4, 14, 3, 7, 12). Order by deadline: d4=3<d2=4<d5=7<d1=9<d6=12<d3=14d_4 = 3 < d_2 = 4 < d_5 = 7 < d_1 = 9 < d_6 = 12 < d_3 = 14.

position job tt dd completion time lateness
1 J4J_4 11 33 11 −2-2
2 J2J_2 22 44 33 −1-1
3 J5J_5 22 77 55 −2-2
4 J1J_1 33 99 88 −1-1
5 J6J_6 55 1212 1313 11
6 J3J_3 44 1414 1717 33

The maximum lateness is 33 (job J3J_3). The same schedule satisfies the substructure formula: after J4J_4 the residual has times (3,2,4,2,5)(3, 2, 4, 2, 5) for J1,J2,J3,J5,J6J_1, J_2, J_3, J_5, J_6 and deadlines shifted by 11: (8,3,13,6,11)(8, 3, 13, 6, 11); its best order J2,J5,J1,J6,J3J_2, J_5, J_1, J_6, J_3 has completion times 2,4,7,12,162, 4, 7, 12, 16 and latenesses −1,−2,−1,1,3-1, -2, -1, 1, 3, so opt(J′)=3\mathrm{opt}(J') = 3 and opt(J)=max⁡(1−3,3)=3\mathrm{opt}(J) = \max(1 - 3, 3) = 3. For comparison, the shortest job first (order J4,J2,J5,J1,J3,J6J_4, J_2, J_5, J_1, J_3, J_6, completion times 1,3,5,8,12,171, 3, 5, 8, 12, 17) gives latenesses −2,−1,−2,−1,−2,5-2, -1, -2, -1, -2, 5, so L=5>3L = 5 > 3.

Self-assessment grid
Exercise Points What must be there for full marks Typical mistakes
Quiz 1-10 10 (1 each) Quiz 1: size is the number of bits, so Θ(2b)\Theta(2^b). Quiz 2: 8484 with the correct bounds. Quiz 3: 292292 with the leaves counted. Quiz 4: the balanced case, 4T(n/2)+n24T(n/2) + n^2. Quiz 5: 6520065200 with leaves and internal levels. Quiz 6: 247247. Quiz 7: −i-i after reducing the exponent. Quiz 8: (4,2,0,2)(4, 2, 0, 2). Quiz 9: 2nlog⁡2n=7682n \log_2 n = 768. Quiz 10: the Master Theorem does not apply, Θ(n2log⁡2n)\Theta(n^2 \log^2 n) value taken as size; k=jk = j not included in a loop; leaves forgotten; Case 3 with a logarithmic gap; exponent not reduced modulo nn; sign errors in the DFT
1 11 (a) induction with base, closure of sum and product (the bottom-right block is E+FE + F), symmetry and commutativity; (b) first row, nn free parameters, cost nn; (c) UU, VV, ZZ, E=U+VE = U + V, F=Z−UF = Z - U, pseudocode on first rows, correctness by induction; (d) T(n)=3T(n/2)+2nT(n) = 3T(n/2) + 2n, exact 5nlog⁡23−4n5n^{\log_2 3} - 4n checked on n=1,2,4n = 1, 2, 4, Case 1; (e) (4,4,8,5)(4, 4, 8, 5), 2929 operations against 3434 checking only the sum; counting n2n^2 parameters; returning the full matrix (the block E+FE + F costs a sum at each level); w(n)=4nw(n) = 4n; T(1)=0T(1) = 0; Case 2 instead of Case 1
2 11 (a) earliest deadline first, shifted residual deadlines, pseudocode, counterexample for the shortest job first; (b) exchange by moving JeJ_e to the front, with the three groups of jobs checked and dj≥ded_j \ge d_e used; (c) opt(J)=max⁡(te−de,opt(J′))\mathrm{opt}(J) = \max(t_e - d_e, \mathrm{opt}(J')) proved with the identity on the completion times, induction; (d) O(nlog⁡n)O(n \log n) and the schedule with L=3L = 3 swapping instead of moving; forgetting the delay of the overtaken jobs; claiming the property for every optimum; additive substructure instead of the maximum; not shifting the deadlines of the residual

For extra practice on the same topics see 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 →, Strassen's AlgorithmStrassen computes the n x n product with 7 recursive calls instead of 8, using 10 matrix additions in Divide and 8 in Conquer: T(n) = 7T(n/2) + (9/2)n^2, Theta(n^(log2 7)) with exact solution 7n^(log2 7) - 6n^2. Its leading constant 7 is more than three times that of the iterative algorithm (2), so Strassen beats it only from n = 655 (from 1024 for powers of two).Strassen's Algorithm →, 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 →, 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 →, 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 → and 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 →.