Exercise · Volume 1 · 1.1-7
Exercise 1.1-7
not stated yet in Lean solution written
| Rating | M21 |
|---|---|
| Markers | M (mathematical), recommended |
| Kind | K7, analyze: find the mean, the variance or the distribution of a cost from the written solution |
| Section | Section 1.1 of Volume 1. Companion chapter: Algorithms |
The task
Fix and let range over all positive integers. Show that the average number of runs of step E1 is well defined, and find how it is related to .
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 average over infinitely many needs a definition. Take the average over and ask for its limit as .
Hint 2
For , the first pass of Algorithm E only swaps the two numbers.
Hint 3
After the swap, the pair is an input of the kind that gives .
Solution
Show the solution
Let be the number of runs of step E1 on the input . The average over is and is the limit of as .
Step 1: the count for large . For the first run of E1 gives , since . Step E3 then sets the pair to . So for . Here is the count with second argument .
Step 2: depends on . If divides , then . Otherwise the first division gives the remainder with , and the pair is . So is a function of , where and for . (The latter is true since as in step 1.) Also . So since the value in the first sum gives the same count as .
Step 3: the limit. The number is fixed. For , The first sum has terms and is a constant . In the second sum each block of consecutive values of gives each residue once, so a full block contributes . Write with . The second sum is plus a part of at most terms, and each term is at most . Divide by and let : the constant and the partial block vanish in the limit, and . So The limit exists, so is well defined, and it is plus one.
Check
For , , so . The counts for are . From on they repeat with period five, with values , whose average is . Computed averages: , and , which approach .
Remarks
The extra is the wasted swap, which is the cost that the swap step E0 of the C1 chapter removes. The same argument works for any quantity that depends on only through and has a bounded change at small .
This solution is checked, 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.