taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 2.2.4-16

Exercise 2.2.4-16

not stated yet in Lean solution written

RatingM28
MarkersM (mathematical)
KindK4, find an estimate: give an asymptotic form or a bound from the written solution
SectionSection 2.2.4 of Volume 1

The task

Estimate the running time of the subroutine of Exercise 15 in terms of suitable parameters.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

The time is the time of the rr calls of Program A, plus the cost of the switch subroutines.

Hint 2

The terms of QQ that Program A passes without a match cost time, and their number grows from one call to the next.

Solution

Show the solution

Let rr be the number of terms of MM and pp the number of terms of PP. In one call of Program A with a shifted copy of PP let m′m' be the number of terms that cancel with a term of QQ, m′′m'' the number that match and do not cancel, p′p' the number of terms of PP that are not matched, and q′q' the number of terms of QQ that are not matched. Program A takes 27m′+18m′′+27p′+8q′+1327m' + 18m'' + 27p' + 8q' + 13 units, plus 22 for the call and the return.

With the code of Exercise 15 the switch subroutines add to the time of Program A. The total time, which agrees exactly with the simulator in 200 random runs, is

T=29+13r+25pr+∑(27m′+18m′′+27p′+8q′+15),T = 29 + 13r + 25pr + \sum \bigl( 27m' + 18m'' + 27p' + 8q' + 15 \bigr),

where the sum is over the rr calls of Program A. The constants 2929, 1313, 2525 belong to the code of Exercise 15, and 25pr25pr is the cost of the two switch subroutines for the p+1p + 1 visits of PP in each of the rr calls.

To see the growth, assume that no term cancels, so m′=0m' = 0, and that a fraction α\alpha of the terms of PP is new at each call, so p′=αpp' = \alpha p and m′′=(1−α)pm'' = (1 - \alpha)p. Then QQ has q0+(j−1)αpq_0 + (j - 1)\alpha p terms before the call number jj, and q′=q0+(j−1)αp−(1−α)pq' = q_0 + (j - 1)\alpha p - (1 - \alpha)p. The sum of the q′q' over the rr calls is rq0+αp r(r−1)/2−(1−α)prr q_0 + \alpha p\, r(r - 1)/2 - (1 - \alpha) p r. The total is

T=4αpr2+(35+13α) pr+8q0r+28r+29.T = 4\alpha p r^2 + (35 + 13\alpha)\, p r + 8 q_0 r + 28 r + 29.

Thus the main term is 4αpr24 \alpha p r^2. It is quadratic in the number of terms of the multiplier MM, and linear in the number of terms of PP. So one should take as multiplier the polynomial with the fewer terms, since the time spent on the terms of QQ that do not match grows with the square of rr.

Check

The value of the second formula for α=1/2\alpha = 1/2, p=10p = 10, r=7r = 7, q0=13q_0 = 13 is 48384838, and the sum of the first formula over the calls, with the same assumptions, gives 48384838.

Remarks

If rr and pp are both about nn, and QQ starts empty, the time is of order n3n^3 in the worst case, where all products are different. A faster method is known for large polynomials, but it is outside the scope of the section.

This solution is drafted, and a person has not reviewed it yet. The source.

In Lean

not stated yet The exercise is not stated in Lean yet.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.