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-12

Exercise 2.2.4-12

not stated yet in Lean solution written

Rating21
KindK7, analyze: find the mean, the variance or the distribution of a cost from the written solution
SectionSection 2.2.4 of Volume 1

The task

Compare the running time of the subroutine of Exercise 11 with that of Program A when polynomial(Q)=0\mathrm{polynomial}(Q) = 0.

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

To copy by addition one first has to make a zero polynomial. Count that time too.

Solution

Show the solution

Let PP have pp terms. With Q=0Q = 0 every term of PP is a term that is not in QQ, so it is copied by Program A, and the time of Program A is 27p+1327p + 13 units, plus 22 for the call and the return. For a fair comparison the zero polynomial must be made first, and the subroutine of Exercise 14 below takes 1818 units with its call. The copy by addition costs 27p+3327p + 33 units.

The subroutine of Exercise 11 takes 22p+2822p + 28 units. The difference is 5p+55p + 5, so the subroutine of Exercise 11 is the faster one. For large pp its time is 22/2722/27, which is about 81%81\%, of the other.

Check

In the simulator, for p=4,5,6p = 4, 5, 6, a call to ZERO followed by Program A took 18+12318 + 123, 18+15018 + 150 and 18+17718 + 177 units, which is 27p+3327p + 33, and COPY took 116116, 138138 and 160160, which is 22p+2822p + 28.

Remarks

Program A spends 2727 units on a term that is added to nothing because it has to compare, find that PP is larger, and insert. COPY knows from the start that every term is new.

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.