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

Exercise 2.2.4-7

not stated yet in Lean solution written

Rating10
KindInformal: the exercise has no formal statement, so Lean does not state it from the written solution
SectionSection 2.2.4 of Volume 1

The task

Explain why it is useful to keep the ABC\mathrm{ABC} fields of a polynomial list in decreasing order.

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

Think about how Algorithm A finds, for a term of PP, the term of QQ with the same exponents.

Solution

Show the solution

With a fixed order both lists can be scanned once, side by side. For a term of PP the pointer in QQ moves forward until it reaches an ABC\mathrm{ABC} that is not larger. If it is equal, the terms are added. If it is smaller, the term is inserted before it. The pointer into QQ never goes back, so the whole addition is one pass over both lists, and the time is linear in the sum of their lengths. Without any order one would search QQ from the start for each term of PP, which takes time proportional to the product of the lengths.

The decreasing order also fits the sentinel. The sentinel has ABC=−1\mathrm{ABC} = -1, which is smaller than every real term, so it comes last in a decreasing list. It stops the scan of QQ in the same way as any smaller term. In an increasing order the smallest value would have to be at the front, and the sentinel with −1-1 would have to be at the front, where it could not serve as the stop of the loop.

Remarks

Another way to say this is that the lists are sorted lists and addition is a merge. The merge works only because both lists use the same order.

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.