Exercise · Volume 1 · 2.2.4-16
Exercise 2.2.4-16
not stated yet in Lean solution written
| Rating | M28 |
|---|---|
| Markers | M (mathematical) |
| Kind | K4, find an estimate: give an asymptotic form or a bound from the written solution |
| Section | Section 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 calls of Program A, plus the cost of the switch subroutines.
Hint 2
The terms of that Program A passes without a match cost time, and their number grows from one call to the next.
Solution
Show the solution
Let be the number of terms of and the number of terms of . In one call of Program A with a shifted copy of let be the number of terms that cancel with a term of , the number that match and do not cancel, the number of terms of that are not matched, and the number of terms of that are not matched. Program A takes units, plus 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
where the sum is over the calls of Program A. The constants , , belong to the code of Exercise 15, and is the cost of the two switch subroutines for the visits of in each of the calls.
To see the growth, assume that no term cancels, so , and that a fraction of the terms of is new at each call, so and . Then has terms before the call number , and . The sum of the over the calls is . The total is
Thus the main term is . It is quadratic in the number of terms of the multiplier , and linear in the number of terms of . So one should take as multiplier the polynomial with the fewer terms, since the time spent on the terms of that do not match grows with the square of .
Check
The value of the second formula for , , , is , and the sum of the first formula over the calls, with the same assumptions, gives .
Remarks
If and are both about , and starts empty, the time is of order 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.