Volume C1 · Basic concepts · TAOCP Section 1.2.10
Analysis of an algorithm
Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.
Read it next to Section 1.2.10 of the book. The nodes and the exercises of the section.
Contents
Analysis of an algorithm
Overview
Section 1.2.10 of TAOCP studies one small algorithm, Algorithm M, which scans a table from its end and finds the largest entry. Its running time is a sum of step counts, and all counts are fixed by except one: , the number of times that the current maximum changes. The section finds the least value, the greatest value, the average and the standard deviation of when the input is a random order of distinct numbers. For this it sets up a recursion for the probabilities, turns it into a recursion for a generating function, and reads the mean and the variance from the derivatives of the generating function at . It then proves that the mean and the variance of a product of generating functions are sums, uses this on two more distributions (coin tossing and the uniform distribution), and relates the method to the classical language of probability: characteristic functions, convolution, cumulants and the tail inequalities. The section uses the sums and the harmonic numbers of Sections 1.2.3 and 1.2.7, the Stirling numbers of Section 1.2.5, and the generating functions of Section 1.2.9.
The ideas
The cost of a program is a list of step counts. Take each step of the algorithm and count how often it runs. Multiply each count by the time of the step on a given machine, and add. Four of the five counts of Algorithm M are fixed by . The fifth, , depends on the data. The analysis of the algorithm is the analysis of .
Four numbers describe a data-dependent count. The least value says what happens in the best case. The greatest value says what happens in the worst case. The average says what happens in the typical case, and it needs a model of the input: here, all orders of distinct numbers are equally likely. The standard deviation says how far a single run can be from the average. The four numbers together are the quadruple of the book.
Probabilities by recursion, then by generating function. Let be the probability that . The first element of the input either is the largest or it is not, and this gives a recursion in . A recursion on a table of numbers becomes a recursion on one polynomial . For Algorithm M the polynomial recursion is a product with a linear factor, so itself is a product of linear factors.
The mean and the variance are derivatives at . If with , then , the mean is and the variance is . These are cheap to compute even when the probabilities themselves have no simple form. For the uniform distribution the derivatives at come from the Taylor series of , with no limit of a ratio.
A product is a sum for these two numbers. The mean and the variance of are the sums of the means and of the variances. The reason is that turns the product into a sum, and the mean and the variance are the first two derivatives of at . This is the property that makes the generating function method work: a random quantity that is a sum of independent parts has a generating function that is a product.
Tail inequalities bound the chance of a large deviation. The generating function also bounds and by , for a free parameter . The idea is the same as in Chebyshev's inequality: a rare event must have a small share of a sum of nonnegative terms.
C1 follows the order of the section. It proves the equations by the shortest route, and it checks each result against an exact count of all permutations of elements. Where the section defers a proof (Theorem A), C1 gives the proof at the place of the theorem. For eq. 1.2.10(13) and (21) C1 gives both the algebraic proof and the check by a small case.
Definitions, results and proofs
Definition C1.1.2.10.1 (the notation for a random quantity, TAOCP Section 1.2.10). Let be a quantity that takes real values with a probability. The notation states four facts: the least value that takes is , the average is , the greatest is , and the standard deviation is . The standard deviation is the square root of the variance, and the variance is the average of .
Node: TA0BFM039A · status: not stated yet
Explanation. The notation puts the whole analysis of one quantity in one line. The book uses it for the rest of the volume. A quantity such as the number of comparisons of a sort gets its line, and a reader can compare two algorithms by comparing two lines. The four numbers are not independent: always , and (a variable that stays in an interval of length has a standard deviation at most ).
Example. For a fair die, the faces are equally likely, and the quadruple is , which is about . The variance is . For a quantity that is always , the quadruple is .
Proof of the bound . Let the values lie in with mean . Then , so . The variance is . The product of two numbers with a fixed sum is at most . Take square roots.
Definition C1.1.2.10.2 (the probabilities of the count , TAOCP eq. 1.2.10(1)). Let be the probability that is when the entries are distinct and each of the orders is equally likely:
Node: TA3YXY6HR1 · Lean: TAOCP.S1_2_10.prob, TAOCP.S1_2_10.permutations'_range · status: not stated yet
Explanation. Algorithm M uses the entries only by comparing them. So only the relative order of the entries matters, and the entries can be taken to be in some order. The probability of an event is then the share of the permutations that belong to the event. The model assumes distinct entries and a uniform order. Exercises 1.2.10-7 to 10 and 18 change the model.
Example. For the permutations of give in cases. So , , and . The sum is . For instance the order gives : the scan starts with , the entry is larger and becomes , and then and are smaller than . A direct count over all permutations of objects, done by machine, agrees with the numbers of eq. 1.2.10(9) below.
Definition C1.1.2.10.3 (the average of , TAOCP eq. 1.2.10(2)). The average, also the mean or the expected value, is
Node: TA6Q6S7X2X · Lean: TAOCP.S1_2_10.eq2_M · status: not stated yet
Explanation. It is the sum of the values weighted by their probabilities. Since , the average lies between and . The sum has finitely many nonzero terms for each .
Example. For : . For the three nonzero probabilities are for , so .
Definition C1.1.2.10.4 (the variance of , TAOCP eq. 1.2.10(3)). The variance is the average of , and it has a second form. The standard deviation is .
Node: TAWKTCXWTM · Lean: TAOCP.S1_2_10.eq3, TAOCP.S1_2_10.eq3_M · status: proved
Explanation. The variance measures the spread around the average. The second form is the one for calculation, since it needs only the sum of .
Example. For : , and . So , and .
Proof of the second form. Expand the square: . Sum with the weights . The first part is . The second is by eq. 1.2.10(2). The third is , since the probabilities add to . The total is .
Lemma C1.1.2.10.5 (Chebyshev's inequality, a fact that TAOCP Section 1.2.10 states in the text). Let have mean and standard deviation . For each real , the probability that is less than .
Node: none · Lean: ProbabilityTheory.meas_ge_le_variance_div_sq, from Mathlib
Explanation. The standard deviation is a true unit of distance for a random quantity: more than units from the mean has a chance below , for each distribution. It is crude. For the normal distribution the chance of more than two units is , but the inequality says only less than . Its value is that it holds for each distribution with a variance.
Example. For the count of Algorithm M has and . Take . The inequality says . Since and , the event is . By eq. 1.2.10(9) its chance is .
Proof. Let be the probability that . If , there is nothing to prove. If , then the variance, which is the average of , is at least the part of the average that comes from the outcomes with . On these outcomes , so the part is more than . Hence , and .
Theorem C1.1.2.10.6 (the recursion of the probabilities, TAOCP eq. 1.2.10(4)). For and all integers ,
Node: TAS16HR5JE · Lean: TAOCP.S1_2_10.eq4_succ, TAOCP.S1_2_10.eq4_zero, TAOCP.S1_2_10.N_cons_succ, TAOCP.S1_2_10.N_cons_zero (partial) · status: not stated yet
Explanation. Look at the first entry of the order. With probability it is the largest entry. In this case it is a new maximum when the scan reaches it, so is one more than it is on the other entries. With probability it is not the largest. Then some later entry is larger and the scan never changes at , so equals its value on the other entries. The other entries are in random order again, so their has the probabilities .
Example. For and : . This is the number counted above.
Proof. Count permutations instead of probabilities. Let be the number of permutations of with . Split them by . Case : the tail is any permutation of . The scan reaches last, and is larger than all that it has seen, so it counts as a change of . Hence , and there are such permutations with . Case : there are choices of . Since is in the tail, is not larger than the maximum of the tail, so the scan does not change at and . The count of the tail depends only on the relative order of the tail, so for each choice of , exactly tails have . The total is . Divide by .
Theorem C1.1.2.10.7 (the start of the recursion, TAOCP eq. 1.2.10(5)). , and for .
Node: TA17B4ZV4M · Lean: TAOCP.S1_2_10.eq5 (partial) · status: proved
Explanation. With one entry the algorithm never changes in step M4, so with probability . The count is never negative. These two facts and the recursion of eq. 1.2.10(4) fix all , by induction on . The recursion gives, for example, (since ), so , which is exercise 1.2.10-1: exactly when the last entry is the largest, and this has probability .
Example. From : , since and . Then , and .
Proof. With the scan has no step M3 to M5, and M4 never runs. So . The count counts runs of a step, so it is not negative.
Definition C1.1.2.10.8 (the generating function of the probabilities, TAOCP eq. 1.2.10(6)).
Node: TAMMRQ99SY · Lean: TAOCP.S1_2_10.pgf, TAOCP.S1_2_10.coeff_pgf · status: not stated yet
Explanation. This is the ordinary generating function of Section 1.2.9, with the probabilities as coefficients. Since for , is a polynomial of degree at most , and the infinite sum is only a convenient way to write it.
Example. , , and .
Theorem C1.1.2.10.9 (the recursion of the generating functions, TAOCP eq. 1.2.10(7)). For ,
Node: TAH26XMEDX · Lean: TAOCP.S1_2_10.eq7 · status: proved
Explanation. The recursion of eq. 1.2.10(4) has a shift in the index in one term. A shift is a multiplication by in the generating function. This turns the two-term recursion of numbers into a product with a linear factor.
Example. . The coefficients are the .
Proof. Multiply eq. 1.2.10(4) by and sum over . The left side gives . The first term on the right gives . The second term gives . Add. The start comes from eq. 1.2.10(5): .
Theorem C1.1.2.10.10 (the product form, TAOCP eq. 1.2.10(8)).
Node: TAYW2YGGYP · Lean: TAOCP.S1_2_10.eq8 · status: proved
Explanation. The generating function of is, up to the factor , a rising power. It is also a binomial coefficient with a real upper index (Section 1.2.6), divided by . The form as a product is the useful one: a product of the linear polynomials for . Each is the generating function of a quantity that is with probability and otherwise.
Example. For : , as in the example of eq. 1.2.10(6). For the product is , as it must be.
Proof. Apply eq. 1.2.10(7) again and again, down to : . The denominators multiply to . For the second form, by the definition of the binomial coefficient in Section 1.2.6, and division by removes the first factor.
Theorem C1.1.2.10.11 (the probabilities by Stirling numbers, TAOCP eq. 1.2.10(9)). For and ,
Node: TAT2XETC9Z · Lean: TAOCP.S1_2_10.eq9 · status: proved
Explanation. The number of permutations of objects with exactly cycles is the Stirling number of the first kind (Section 1.2.6). The theorem says that the permutations with are equinumerous with the permutations with cycles. This is a hint for a bijection. In fact the number of records of a permutation has the same distribution as the number of cycles, by a standard bijection between the two (see Section 1.3.3 on the cycle form). It also explains why the scan from the right counts records: one record is the entry at the start, and records are the changes.
Example. For : , , , , , and the sum is . So for . A direct count of all permutations by a program gives the same five numbers.
Proof. Put in eq. 1.2.6(44), the expansion of by Stirling numbers, and multiply by . This gives . Divide by . By eq. 1.2.10(8), , and the term with is zero for . Compare the coefficient of : it is .
Definition C1.1.2.10.12 (the mean and the variance of a generating function, TAOCP eq. 1.2.10(10)). For with and ,
Node: TAGZ4M2YDD · Lean: TAOCP.S1_2_10.mean, TAOCP.S1_2_10.var, TAOCP.S1_2_10.eq10_mean (partial) · status: not stated yet
Explanation. This is eq. 1.2.10(2) and (3) with a general table of probabilities in place of . The sums are finite for a polynomial. For an infinite series they can be infinite, and then the mean or the variance does not exist.
Example. For (the values equally likely), , , and .
Theorem C1.1.2.10.13 (the value at , TAOCP eq. 1.2.10(11)). If is a generating function of probabilities, then .
Node: TAAYG65D26 · Lean: TAOCP.S1_2_10.eq11, TAOCP.S1_2_10.eval_one_sum (partial) · status: proved
Explanation. All outcomes together have probability . This is the test that a guess of a generating function must pass, and it is the one fact that Theorem A needs.
Example. , and by eq. 1.2.10(8).
Proof. , which is the sum of the probabilities of all values, and this is .
Theorem C1.1.2.10.14 (the mean is the derivative at , TAOCP eq. 1.2.10(12)). .
Node: TAD1TRG25A · Lean: TAOCP.S1_2_10.mean, TAOCP.S1_2_10.eq10_mean (partial) · status: not stated yet
Explanation. Differentiation brings down the exponent. When the series is infinite, the derivative at is read as the limit from the left, and it exists exactly when the mean exists.
Example. For : , so , the mean found from the definition.
Proof. For a polynomial, , and at this is .
Theorem C1.1.2.10.15 (the variance by the first two derivatives, TAOCP eq. 1.2.10(13)). .
Node: TAGKJ1H01A · Lean: TAOCP.S1_2_10.var, TAOCP.S1_2_10.eq13 (partial) · status: proved
Explanation. The second derivative at is the average of , a factorial moment. Adding gives the average of .
Example. For : , so . Then , the value of eq. 1.2.10(3). For : , , and .
Proof. (Exercise 1.2.10-2.) , so . Hence . Subtract in eq. 1.2.10(10).
Theorem C1.1.2.10.16 (the average of , TAOCP eq. 1.2.10(14)). , where .
Node: TA89E5J489 · Lean: TAOCP.S1_2_10.eq14, TAOCP.S1_2_10.eq2_M · status: proved
Explanation. The current maximum changes about times on a random input of entries, not about or times. So on the average the work of step M4 is small next to the comparisons of step M3. Since (Section 1.2.11.2), , with .
Example. , , , so . For , , so the current maximum changes about times in entries.
Proof. Differentiate eq. 1.2.10(7) with the product rule: . Put . Since and at , the result is . With , induction on gives .
Theorem C1.1.2.10.17 (mean and variance of a product, TAOCP Theorem 1.2.10A). Let and be generating functions with and with two derivatives at . Then and . The variance is the one of eq. 1.2.10(13).
Node: TAZGCK2RXE · Lean: TAOCP.S1_2_10.theoremA_mean, TAOCP.S1_2_10.theoremA_var (partial) · status: not stated yet
Explanation. If two independent random quantities have the generating functions and , then their sum has the generating function . The theorem says that the mean and the variance of the sum are the sums of the means and of the variances, which is the familiar fact about independent quantities. The proof here works with the formulas alone: it needs only .
Example. Take and . Then , , , and (from : ). The product is , with mean and variance . The check: .
Proof of the mean. By the product rule, , since . The variance is proved under eq. 1.2.10(15).
Theorem C1.1.2.10.18 (the variance of a product, TAOCP eq. 1.2.10(15)). Under the assumptions of Theorem 1.2.10A, .
Node: TAVHTPH4AY · Lean: TAOCP.S1_2_10.theoremA_var (partial) · status: not stated yet
Explanation. The proof of C1 differentiates the product twice and uses eq. 1.2.10(13). The book gives the same result through the logarithm, as shown under eq. 1.2.10(23): the variance is the second derivative of at , and the logarithm of a product is a sum, so both the mean and the variance, and all the cumulants, add.
Example. The variance of with : each has , , so . For the sum is . This is .
Proof. By the product rule twice, . At , with : . Also . By eq. 1.2.10(13), . Expand the square: . The cross terms cancel, and the rest is .
Analysis C1.1.2.10.19 (the min, the average, the maximum and the deviation of , TAOCP eq. 1.2.10(16)).
Node: TAFP8Q7YC6 · Lean: TAOCP.S1_2_10.eq14, TAOCP.S1_2_10.eq16_var, TAOCP.S1_2_10.eq3_M, TAOCP.S1_2_10.changes_eq_zero_iff, TAOCP.S1_2_10.changes_lt, TAOCP.S1_2_10.changes_of_pairwise (partial) · status: not stated yet
Explanation. The least value happens when is the largest entry. The greatest value happens when : then each step finds a larger entry than the last. The mean is eq. 1.2.10(14). The variance is a sum over the factors : , and the term is zero, so the sum from can be written from : , with of Section 1.2.7. For large , , so the variance is . The deviation grows like , so is concentrated near its mean.
Example. For : , and . For : and , so the deviation is . For the mean is , the variance is , and the deviation is . Exact counts over all permutations for agree with and , for example gives the mean and the variance .
Proof. The mean is eq. 1.2.10(14). For the variance, apply eq. 1.2.10(15) to the product with , which satisfies , and . Then by eq. 1.2.10(13). Sum over , add the zero term for , and split the sum. The extreme values were shown above.
Theorem C1.1.2.10.20 (the recursion of the binomial distribution, TAOCP eq. 1.2.10(17)). Let a coin show heads with probability , let , and let be the probability of heads in independent tosses. Then .
Node: TAP1SR9VC2 · Lean: TAOCP.S1_2_10.eq17, TAOCP.S1_2_10.eq17_zero · status: proved
Explanation. Look at the last toss. With probability it is a head, and then the first tosses have heads. With probability it is a tail, and they have heads. The tosses are independent, so the probabilities multiply. The form is the same as that of eq. 1.2.10(4), where and take the place of and .
Example. Take and . , , . By the recursion, .
Proof. The event " heads in tosses" is the disjoint union of "head at toss and heads in the first " and "tail at toss and heads in the first ". By independence, the probabilities of the two parts are and .
Theorem C1.1.2.10.21 (the generating function of the binomial distribution, TAOCP eq. 1.2.10(18)). .
Node: TA9C24E238 · Lean: TAOCP.S1_2_10.coin, TAOCP.S1_2_10.eval_coin · status: not stated yet
Explanation. This is the binomial theorem in the form of a generating function, and it shows that (exercise 1.2.10-4). The number of heads is the sum of independent counts, each or with the generating function .
Example. For and , . This is the most likely value, and is also the mean .
Proof. As in the proof of eq. 1.2.10(7), multiply eq. 1.2.10(17) by and sum over : . With (from , ), induction gives .
Theorem C1.1.2.10.22 (the statistics of the number of heads, TAOCP eq. 1.2.10(19)). The number of heads in tosses has .
Node: TAB875092J · Lean: TAOCP.S1_2_10.eq19 (partial) · status: proved
Explanation. The mean is times the chance of a head. The deviation grows like while the mean grows like , so the relative spread shrinks: the number of heads is concentrated near , which is the law of large numbers in its elementary form. The least value and the greatest value are reached (they have the probabilities and ) when .
Example. For and : mean , variance , deviation . The relative spread is .
Proof. The generating function has and , so and . By Theorem 1.2.10A applied times to , and .
Theorem C1.1.2.10.23 (the generating function of the uniform distribution, TAOCP eq. 1.2.10(20)). If the values are equally likely, the generating function is
Node: TA25H78F6R · Lean: TAOCP.S1_2_10.unif, TAOCP.S1_2_10.eq20 (partial) · status: proved
Explanation. The second form is the sum of a finite geometric series. It has the factor in the denominator, so , and come out as the ratio and need a limit. The next theorem avoids this.
Example. For : , and , the same polynomial.
Proof. Each of the values has the probability , so . The geometric sum is for .
Theorem C1.1.2.10.24 (the Taylor series at , TAOCP eq. 1.2.10(21)). If is a polynomial or a convergent power series near , then
Node: TA48NMJNPG · status: not stated yet
Explanation. This is Taylor's theorem at the point . It gives the use of : replace by in the formula for , expand it, and read the first coefficients. The coefficient of is . The coefficient of is . No limit is needed.
Example. For : . So and .
Proof. For a polynomial of degree , the Taylor expansion at ends at and is exact. It follows from the binomial theorem: , so the coefficient of in is . For this is . For it is , and for it is .
Theorem C1.1.2.10.25 (the statistics of the uniform distribution, TAOCP eq. 1.2.10(22)). For equally likely values ,
Node: TA65GVJ49X · Lean: TAOCP.S1_2_10.eq22_derivs, TAOCP.S1_2_10.eq22 (partial) · status: proved
Explanation. The mean is the middle of the range. The deviation is about for large , since . The spread is a fixed share of the range, so a single sample says little about the next. The book calls this an unstable situation.
Example. For : mean , variance , deviation . For a die () the deviation is , as in the first example of this section.
Proof. By eq. 1.2.10(20), , since is the denominator after the shift. Expand . Subtract and divide by : . The first coefficient is . The next two are and . By eq. 1.2.10(21), and . Then .
Definition C1.1.2.10.26 (the cumulants, TAOCP eq. 1.2.10(23)). The cumulants of a generating function with are defined by the series
Node: TARFKMZAQD · status: not stated yet
Explanation. The cumulants (also called semi-invariants) are the derivatives of at . The first is the mean, the second is the variance, and the third is a measure of the skew. They have two properties. They add under the product of generating functions, since the logarithm turns a product into a sum: this holds for all the , and Theorem 1.2.10A is the case . A distribution is normal when all its cumulants after the second are zero.
Example. For the Bernoulli factor the third cumulant is , the third cumulant of a coin with (exercise 1.2.10-6). By additivity, the third cumulant of for is . This agrees with the formula from the moments , , of the distribution of for , computed exactly.
Proof of and . Let . By the chain rule, , and at this is . Differentiating again: , and at this is . So and (this is a second proof of eq. 1.2.10(13)). For a product, , so each cumulant of is the sum of the cumulants of and .
Theorem C1.1.2.10.27 (a tail bound from below, TAOCP eq. 1.2.10(24)). If the nonnegative integer quantity has the generating function , then for every real and every with ,
Node: TAGHCHPQPW · Lean: TAOCP.S1_2_10.eq24 (partial) · status: proved
Explanation. The inequality holds for each in the range, so the best bound is the smallest value of the right side. An exact minimum is often hard to find, and a value of near the minimum gives a good bound. The idea is that for , the power is large for small , so the terms have a big share of the sum .
Example. Let be the number of heads in tosses with , so . The true value of is . The bound equals at , at , and at . The derivative of its logarithm, , is zero at , where the bound is . The best bound is about times the true value, but it is simple and it is valid for each .
Proof. For and , . So . The terms with are nonnegative, so .
Theorem C1.1.2.10.28 (a tail bound from above, TAOCP eq. 1.2.10(25)). Under the same assumptions, for every real and every ,
Node: TAHY3MSH7A · Lean: TAOCP.S1_2_10.eq25 (partial) · status: proved
Explanation. This is the mirror of eq. 1.2.10(24). For the power is large for large . The method is the one that gives the Chernoff bounds. Exercise 1.2.10-21 uses it to prove for the binomial distribution.
Example. Take the same and . The true value of is . The bound is at , at , at , and at . The best is , so the best bound is , about times the true value.
Proof. For and , . So , since the added terms are nonnegative.
Algorithms and programs
Algorithm C1.1.2.10.29 (find the maximum, TAOCP Algorithm 1.2.10M). The input is a table with . The output is a pair with . When the maximum occurs more than once, is its last position.
Node: TA2D92P953 · Lean: TAOCP.S1_2_10.runM, TAOCP.S1_2_10.runM_eq, TAOCP.S1_2_10.changes, TAOCP.S1_2_10.chgAux (partial) · status: not stated yet
The idea. Scan the table from the end. Keep the largest entry seen so far and its index. Replace them only when a strictly larger entry comes. Because the scan goes from the end and the test is strict, the kept index is the largest among the positions of the maximum.
The steps.
FindMax(X, n): -- n >= 1
j := n; k := n - 1; m := X[n]
while k > 0:
if X[k] > m:
j := k; m := X[k] -- a new current maximum
k := k - 1
return (m, j)
A worked run. Take and . This input has a repeated entry, so it also shows the rule for ties.
| State at the test | Action | ||||
|---|---|---|---|---|---|
| start | 5 | 5 | 7 | 6 | , keep |
| 2nd | 4 | 9 | 7 | 6 | , so , |
| 3rd | 3 | 2 | 9 | 4 | keep |
| 4th | 2 | 9 | 9 | 4 | , keep (the tie does not move ) |
| 5th | 1 | 4 | 9 | 4 | keep |
| end | 0 | 9 | 4 | return |
The result is with . The project has no Lean definition of Algorithm 1.2.10M yet, so there is no #eval to show.
Correctness. Let be the statement: , , for , and for . holds at the first test of the loop, with and : the range is , and the strict range is empty. Suppose that holds and . If , the body sets and . Now is larger than for , so the strict range holds with the new and the weak range holds. After , the statement holds again with the new . If , then and do not change, and now holds for . The strict range does not contain , since . After , holds. When the loop ends, , and says that for all , with strict inequality after . So is the maximum, and is the largest index at which the maximum occurs.
Termination. The variable is an integer that falls by at each pass of the loop and the loop ends when . So there are exactly passes.
Cost. See the next block.
Analysis C1.1.2.10.30 (the running time of Algorithm M, TAOCP Section 1.2.10, Fig. 9). The table of the counts of the steps of Algorithm M is: M1 once, M2 times, M3 times, M4 times, and M5 times.
Node: TAV36Q7KCT · status: not stated yet
The idea. Each step of the algorithm has a count. If the time of a step on a given machine is , the total time is the sum of the counts times the times. All counts except the count of M4 are fixed by , so only must be analyzed. The counts are tied by Kirchhoff's first law: at each box, the flow in equals the flow out.
Proof of the counts. The five steps of Algorithm M, in order, are the set-up (M1), the test for the end (M2), the comparison (M3), the change of (M4) and the decrement of (M5). Step M2 is reached once from M1, and once from M5 after each pass, and the loop makes passes (the termination argument above, with from to ). So M2 runs times, M3 runs times, M5 runs times (it follows each M3 either directly or after M4), and M4 runs times by the definition of . Kirchhoff: the flow out of M3 is (to M4, and past it to M5); the flow into M5 is the same .
Cost. The total time is . For a machine where each step takes one unit, . The minimum is at , the maximum is at , and the average is by eq. 1.2.10(14). The deviation of is the deviation of , since is fixed, and so the time of the algorithm is . In the worked run above (, ), the counts are and the total is . For the average is units, and the deviation is : nearly all of the time is the fixed part. Program 1.3.2M is Algorithm M in MIX, and Section 1.3.2 repeats this analysis with the times of the MIX instructions.
The exercises
The table has the exercises of Section 1.2.10. The kind is the class of spec 06.3, and the indexer of S0 fixes it in the map.
| Exercise | Rating | Markers | Kind | Lean | Solution |
|---|---|---|---|---|---|
| 1.2.10-1 | 10 | K3 | not yet stated | X1, 1.2.10-1 | |
| 1.2.10-2 | 16 | HM | K1 | not yet stated | X1, 1.2.10-2 |
| 1.2.10-3 | 15 | M | K3 | not yet stated | X1, 1.2.10-3 |
| 1.2.10-4 | 10 | M | K3 | not yet stated | X1, 1.2.10-4 |
| 1.2.10-5 | 13 | M | K3 | not yet stated | X1, 1.2.10-5 |
| 1.2.10-6 | 27 | HM | K3 | not yet stated | X1, 1.2.10-6 |
| 1.2.10-7 | 27 | M | K7 | not yet stated | X1, 1.2.10-7 |
| 1.2.10-8 | 20 | M | K3 | not yet stated | X1, 1.2.10-8 |
| 1.2.10-9 | 25 | M | K3 | not yet stated | X1, 1.2.10-9 |
| 1.2.10-10 | 20 | M | K7 | not yet stated | X1, 1.2.10-10 |
| 1.2.10-11 | 15 | M | K3 | not yet stated | X1, 1.2.10-11 |
| 1.2.10-12 | 21 | HM | K1 | not yet stated | X1, 1.2.10-12 |
| 1.2.10-13 | 38 | HM | K1 | not yet stated | X1, 1.2.10-13 |
| 1.2.10-14 | 30 | HM | K1 | not yet stated | X1, 1.2.10-14 |
| 1.2.10-15 | 23 | HM | K1 | not yet stated | X1, 1.2.10-15 |
| 1.2.10-16 | 25 | M | K3 | not yet stated | X1, 1.2.10-16 |
| 1.2.10-17 | 27 | M | K1 | not yet stated | X1, 1.2.10-17 |
| 1.2.10-18 | 28 | M | K1 | not yet stated | X1, 1.2.10-18 |
| 1.2.10-19 | 21 | M | K1 | not yet stated | X1, 1.2.10-19 |
| 1.2.10-20 | 22 | M | K1 | not yet stated | X1, 1.2.10-20 |
| 1.2.10-21 | 21 | HM | K1 | not yet stated | X1, 1.2.10-21 |
| 1.2.10-22 | 22 | HM | K1 | not yet stated | X1, 1.2.10-22 |
The order to do them: start with 1.2.10-1 and 3, which use the formulas of the section directly. Then do 1.2.10-4, 5 and 2, the coin and the formula for the variance. Exercises 1.2.10-7 to 10 change the model of the input in steps, from repeated values to a random choice from a set: do them in this order, since each uses the one before. Exercise 1.2.10-11 and 16 to 17 are short exercises on generating functions. Exercise 1.2.10-6 and 12 are about the cumulants of eq. 1.2.10(23). Exercises 1.2.10-13 to 15 prove that distributions approach the normal distribution and need Fourier analysis. Exercises 1.2.10-21 and 22 use the tail bounds of eqs. (24) and (25). Exercises 1.2.10-19 and 20 are about records of permutations.
Lean
The Lean code of tamnd/taocp-lean at main has the file TAOCP/V1/S1_2_10.lean, in the namespace TAOCP.S1_2_10. It states eqs. (1) to (20), (22), (24) and (25), Theorem A, the statistics of Algorithm M and a model of the algorithm. The Node lines name the declarations. Where the Lean statement is weaker, the Node line says "partial". Chebyshev's inequality is ProbabilityTheory.meas_ge_le_variance_div_sq of Mathlib, and its statement bounds the chance with a weak inequality, so it is a little weaker than the strict form of the section.
Not yet stated: the notation for a random quantity, the Taylor series at (eq. (21)), the cumulants (eq. (23)) and the running time of Algorithm M with its step counts.
The model of Lean is a finite exact model, so the points where it differs from the book are these.
- The probabilities are rational numbers.
TAOCP.S1_2_10.prob n kis the number of permutations of with , divided by . The generating functions are polynomials overℚ,TAOCP.S1_2_10.pgf, not power series, and the sum ofpgf nstops at . - Eq. (4) is stated for with in place of , in two parts:
TAOCP.S1_2_10.eq4_zerofor andTAOCP.S1_2_10.eq4_succfor . The case of a negative is not stated.TAOCP.S1_2_10.N_cons_zeroandTAOCP.S1_2_10.N_cons_succare the same step as a count of permutations. - Eq. (5) is stated for natural , and it does not say that for .
TAOCP.S1_2_10.prob_eq_zerosays that for . - Eq. (7) is
TAOCP.S1_2_10.eq7, with the factorTAOCP.S1_2_10.Q. - Eqs. (10) to (13) are for polynomials over
ℚ:TAOCP.S1_2_10.meanis andTAOCP.S1_2_10.varis . Eq. (11) is stated forpgf n, andTAOCP.S1_2_10.eval_one_sumstates it for a polynomial with the coefficients . - Theorem A and eq. (15) are for two polynomials with . The book allows each function that has two derivatives at .
- Eq. (16): the mean is
TAOCP.S1_2_10.eq14and the variance isTAOCP.S1_2_10.eq16_var, with . The minimum and the maximum areTAOCP.S1_2_10.changes_eq_zero_iff,TAOCP.S1_2_10.changes_ltandTAOCP.S1_2_10.changes_of_pairwise. The deviation as a square root is not stated. - Eqs. (19) and (22) state the mean and the variance only. Eq. (20) has no division: .
- Eqs. (24) and (25) are for with and a natural bound . The book has a real .
- Algorithm M is
TAOCP.S1_2_10.runM, on a list of natural numbers. The list is in the order of the scan. It returns the maximum and the count , and not the position .TAOCP.S1_2_10.runM_eqshows that the result is the fold ofmaxandTAOCP.S1_2_10.chgAux.
The Mathlib names that the statements use are harmonic for , Polynomial.derivative for the derivatives at and TAOCP.stirling1 for the Stirling numbers of the first kind. The statements of TAOCP.S1_2_10.eq3_M and TAOCP.S1_2_10.eq2_M use the finite uniform model, and a bridge to ProbabilityTheory.variance is not stated.
Notes and history
The inequality in the section is named after P. L. Chebyshev, but J. Bienaymé found it earlier (1853), and the book gives this credit. The cumulants (semi-invariants) were introduced by T. N. Thiele (1889). The approach of the normal distribution by the binomial distribution is the theorem of A. de Moivre (1733), later improved by P. S. Laplace. The generating function of the number of left-to-right maxima, a product of linear factors, is the simplest case of the family of Stirling-number distributions that also count cycles of permutations.
The number of Algorithm M is the number of records of a random permutation, and its mean is one of the standard examples of the harmonic numbers in the analysis of algorithms. The same distribution counts the cycles of a random permutation (eq. 1.2.10(9)) and the changes in a random scan for a minimum.
The tail bounds (24) and (25) are the method of S. N. Bernstein and of H. Chernoff: choose the parameter to make the bound small. Exercise 1.2.10-22 gives the form that applies to a sum of independent unequal coins.
The project has no errata of Section 1.2.10 to report.
Section 1.2.10 of TAOCP shows how to study the running time of a small algorithm in full: Algorithm M, which finds the largest of numbers. The time depends on one count , and the section finds the least, the average, the greatest and the spread of . To do this it uses probability generating functions, and it builds the first tools of probability that the later chapters use. This chapter of C1 explains each item of the section, proves each equation, and adds checks by exact counting for small .
The source of this page. Something is wrong? Report it.