Analysis of the gift exchange problem

In the gift exchange game there are n players and n wrapped gifts. When a player's number is called, that person can either choose one of the remaining wrapped gifts, or can"steal"a gift from someone who has already unwrapped it, subject to the restriction that no gift can be stolen more than a total of sigma times. The problem is to determine the number of ways that the game can be played out, for given values of sigma and n. Formulas and asymptotic expansions are given for these numbers. This work was inspired in part by a 2005 remark by Robert A. Proctor in the On-Line Encyclopedia of Integer Sequences.


The problem
The following game is sometimes played at parties. A number σ (typically 1 or 2) is fixed in advance. Each of the n guests brings a wrapped gift, the gifts are placed on a table (this is the (1) and so the final answer is 6 · 7 = 42.
If we continue to ignore the factor of n! that arises from the order in which the gifts are selected from the pool, the numbers of scenarios for the cases σ = 1 and n = 1, 2, 3, 4, 5 are 1, 2, 7, 37, 266, respectively.
We noticed that these five terms matched the beginning of entry A001515 in [16], although indexed differently. The nth term of A001515 is defined to be y n (1), where y n (x) is a Bessel polynomial [12,13,18], and for n = 0, 1, 2, 3, 4 the values are indeed 1, 2, 7, 37, 266. Although there was no suggestion of gift-exchanging in that entry, one of the comments there (contributed by Robert A. Proctor on April 18, 2005) mentioned set partitions and restricted Stirling numbers, and this was enough of a hint to lead us to a solution of the general problem.
Outline of paper. The connection with set partitions is discussed in §2, where Theorem 1 expresses the general solution to the problem-the function G σ (n)-as a sum of restricted Stirling numbers of the second kind. We find it convenient to introduce our own notation, E σ (n, k) (see (3)), for the latter numbers. Theorem 3 gives various properties of the E σ (n, k), and Theorem 4 gives an integral representation for G σ (n) that will enable us to use the methods of [1] and [4] to find recurrences.
We will obtain two different linear recurrences for G σ (n), which we label Type C and Type D. Type C is somewhat simpler, and has leading coefficient 1, while Type D has lower order (order σ + 1, in fact, see Theorem 11). To see the difference between the two types of recurrence for G 2 (n), G 3 (n), and G 4 (n), compare (37) with (38), (44) with (46), and (45) with the expression in Table 2 (the first of each pair being the Type C recurrence, the second the Type D recurrence). Sections 3,4, and 5 deal with cases σ = 1 (where there are connections with Bessel polynomials and hypergeometric functions, see Theorem 5), σ = 2 (where we introduce the two types of recurrence), and σ ≥ 3, respectively.
Comments on the rules. (i) If σ = 1 then once a gift has been stolen it can never be stolen again.
(ii) If σ = 2, and someone steals your gift, then if you wish you may immediately steal it back (provided you hadn't stolen it to begin with), and then it cannot be stolen again. Retrieving a gift in this way, although permitted by a strict interpretation of the rules, may be prohibited at real parties. (iii) A variation of the game allows the last player to take any gift that has been unwrapped, regardless of how many times it has already been stolen, as an alternative to taking the last gift from the pool. This case only requires minor modifications of the analysis, and we will not consider it here. (iv) We also ignore the complications caused by the fact that you brought (and wrapped) one of the gifts yourself, and so are presumably unlikely to choose it when your number is called.

Connection with partitions of labeled sets
Let H σ (n) be the number of scenarios with n gifts and a limit of σ steals, for σ ≥ 0, n ≥ 1. Then H σ (n) is a multiple of n!, and we write H σ (n) = n!G σ (n − 1), where in G σ (n − 1) we assume that the gifts are taken from the pool in the order 1, 2, . . . , n. We write n − 1 rather than n as the argument of G σ because the nth gift plays a special (and less important) role. This also simplifies the statement of Theorem 1.
In words, G σ (n) is the number of scenarios when there are n + 1 gifts, with a limit of σ steals per gift, and the gifts are taken from the pool in the order 1, 2, . . . , n + 1.
We recall that the Stirling number of the second kind, S 2 (i, j), is the number of partitions of the labeled set {1, . . . , i} into j parts [10,11], while for h ≥ 1 the h-restricted Stirling number of the second kind, S (h) 2 (i, j), is the number of partitions of {1, . . . , i} into j parts of size at most h [7,8,9]. It is understood that parts are always nonempty. Theorem 1. For σ ≥ 0 and n ≥ 0, Proof. Equation (2) is an assertion about G σ (n), so we are now discussing scenarios where there are n + 1 gifts. For σ = 0, H 0 (n + 1) = (n + 1)!, so G 0 (n) = 1, in agreement with S 2 (n, n) = 1. We may assume therefore that σ ≥ 1. Let an "action" refer to a player choosing a gift γ, either by taking it from the pool or by stealing it from another player. Since we are now assuming that the gifts are taken from the pool in order, γ determines both the player and whether the action was to take a gift from the pool or to steal it from another player. So the scenario is fully specified simply by the sequence of γ values, recording which gift is chosen at each action. For example, the scenarios in (1) are represented by the sequences 123, 1213, 12123, 1223, 12213, 1123, 11223. Since the game ends as soon as the (n + 1)st gift is selected, the number of actions is at least n + 1 and at most (σ + 1)n + 1.
The sequence of γ values is therefore a sequence of integers from {1, . . . , n + 1} which begins with 1, ends with n + 1, where each number i ∈ {1, . . . , n} appears at least once and at most σ + 1 times and n + 1 appears just once, and in which the first i can appear only after i − 1 has appeared. Conversely, any sequence with these properties determines a unique scenario.
Let k denote the length of the sequence with the last entry (the unique n + 1) deleted. We map this shortened sequence to a partition of [1, . . . , k] into n parts: the first part records the positions of the 1's, the second part records the positions of the 2's, . . ., and the nth part records the positions of the n's. Continuing the example, for the seven sequences above, the values of k and the corresponding partitions are as shown in Table 1. The number of such partitions is precisely S (σ+1) 2 (k, n). Since the mapping from sequences to partitions is completely reversible, the desired result follows.
Remark. The sums B(i) := j S 2 (i, j) are the classical Bell numbers. The sums j S (h) 2 (i, j) also have a long history [14,15]. However, the sums i S (h) 2 (i, j) mentioned in (2) (where we sum on the first index rather than the second) do not seem to have studied before. Note that the limits in (2) are the natural limits on the summand k, and could be omitted.
To simplify the notation, and to put the most important variable first, let for σ ≥ 0, n ≥ 0, k ≥ 0. In words, E σ (n, k) is the number of partitions of {1, . . . , k} into exactly n parts of sizes in the range [1, . . . , σ + 1]. For n ≥ 0, E σ (n, k) is nonzero only for n ≤ k ≤ (σ + 1)n. To avoid having to worry about negative arguments, we define E σ (n, k) to be zero if either n or k is negative. Then the answer to our problem can be written as Stirling numbers of the second kind satisfy many different recurrences and generating functions [10, Chap. V], and to a lesser extent this is also true of E σ (n, k). We begin with four general properties.
Then (10) follows at once from (11) and the main theorem of [22].
From (4) and (6) we have: where the inner sum is over all (σ + 1)-tuples of nonnegative integers (ν 1 , . . . , ν σ+1 ) satisfying (7). This may be rewritten as a sum of multinomial coefficients: where i r is the size of the rth part, or, equivalently, the number of times the rth gift is chosen in an action. Equation (6) also leads to an integral representation and generating function for G σ (n).
The recurrence (5) makes it easy to compute as many values of E σ (n, k) and G σ (n) as one wishes. In the rest of the paper we will derive further formulas and recurrences, and asymptotic estimates for these numbers.

The case σ = 1
In the case σ = 1, i.e., when a gift can be stolen at most once, from Theorem 3 we have E 1 (1, k) = δ 1,k , and, for n ≥ 2, E 1 (n, k) = 0 for k < n and k > 2n, E 1 (n, n) = 1, and for n < k ≤ 2n. We also have the explicit formula for n ≤ k ≤ 2n; and the generating function It follows from (4) and (20) that Equation (22) shows that the sequence G 1 (n) is indeed given by entry A001515 in [16]. That entry states (mostly without proof) several other properties of these numbers, taken from various sources, notably Grosswald [12]. We collect some of these properties in the next theorem. We recall from [12] that the Bessel polynomial y n (z) is given by Also 2 F 0 and (later) 2 F 1 denote hypergeometric functions. (ii) for n ≥ 2, with G 1 (0) = 1, G 1 (1) = 2.

The case σ = 2
In the case when σ = 1, i.e., when a gift can be stolen at most once, the problem, as we saw in the previous section, turned out to be related to the values of Bessel polynomials, and the principal sequence, G 1 (n), had been studied before. For σ ≥ 2, we appear to be in new territory-for one thing, the sequences G 2 (n), G 3 (n), . . . were not in [16]. 2 We naturally tried to find analogs of the various parts of Theorem 5 that would hold for σ ≥ 2. Let us begin with the simplest result, the asymptotic behavior. This is directly analogous to Theorem 5(v).
Concerning Theorem 5(i), we do not know if there is a generalization of Bessel polynomials whose value gives (13) for σ ≥ 2.
As for Theorem 5(ii), there is a relationship with hypergeometric functions in the case σ = 2. From (6) we have where η = k − n.
Proof. (i) Equation (36) follows by applying the Almkvist-Zeilberger technique of "differentiating under the integral sign" from [1], starting with the integral representation for G 2 (n) given in (15).
Remarks. (i) To apply the technique of "differentiating under the integral sign" from [1], we used the Maple implementation given in [2].
(iii) A third proof may be obtained using the method of Sister Mary Celine Fasenmyer, as described in §4.1 of [17].
We discovered (37) by experiment, using Theorem 6 to suggest the leading term. (If r(n) denotes the right-hand side of (31), then r(n)/r(n − 1) = (9n 2 − 9n + 2)/2.) This is a fourth-order recurrence for G 2 (n). We also discovered (again by experimenting) a third-order recurrence for G 2 (n), although the coefficient of G 2 (n) on the left side is now n − 2 rather than 1.
In view of (35), (38) is equivalent to a complicated identity involving hypergeometric functions.
Proof. To prove (38) we apply the Almkvist-Zeilberger technique of "differencing under the integral sign" from [1, §7], again starting from the integral representation for G 2 (n) given in (15). (We again used the Maple implementation of the technique from [2].) Since (36) uses the continuous version of the algorithm in [1], and (38) the discrete version, we refer to these recurrences as Types C and D respectively. When σ = 1, both Types C and D produce (26).
Finally, if we need more terms of the asymptotic expansion than are given in Theorem 6 (or a computer-certified proof of it!), we can apply the Poincaré-Birkhoff-Trjitzinsky method as presented in [21] to the Type C recurrence (37). This works for any σ; for σ = 2, we find that (39)

The cases σ ≥ 3
In this final section we discuss the cases σ ≥ 3, mostly concentrating on G σ (n) rather than E σ (n, k). We have not found any connections between these G σ (n) and generalized Bessel polynomials or hypergeometric functions. Let us first prove that recurrences for G σ (n) and E σ (n, k) always exist. This follows from the Wilf-Zeilberger "Fundamental Theorem for multivariate sums" ([17, Theorem 4.5.1], [20]).
where the coefficients C (E) i,j (n) are polynomials in n with coefficients depending on i and j. (ii) For σ ≥ 1, there is a number δ ≥ 0 such that G σ (n) satisfies a recurrence of the form where the coefficients C Proof. (ii) As usual, Eq. (41) follows by summing (40) on k. (i) We will use the case σ = 3 to illustrate the proof, the general case being similar. We know from (6) that where the sum is over all 4-tuples of nonnegative integers (a, b, c, d) satisfying a + 2b + 3c + 4d = k .
In other words, where now the sum is over all values of c and d for which the summand is defined. This summand is a "holonomic proper-hypergeometric term", in the sense of [20], and it follows from the Fundamental Theorem in that paper that E 3 (n, k)/2 n and hence E 3 (n, k) satisfies a recurrence of the desired form.
Similarly, in the general case, we write the summand in E σ (n, k) as a function of n, k, a 3 , . . . , a σ+1 , again obtaining a holonomic proper-hypergeometric term.
The Type C recurrences for G 3 (n) and G 4 (n) (obtained as in §4) are: 3 We also found analogous recurrences for the E σ (n, k), which we omit. The Type D recurrence for G 3 (n) is given in (46), and that for G 4 (n) can be seen in Table 2.    Table 2 are linear recurrences of orders 4 and 5, respectively, and the Type D recurrences for G 5 (n) and G 6 (n) are similarly of orders 6 and 7. An analogous property holds for all σ.

Equation (46) and the expression in
Theorem 11. For σ ≥ 1, the Type D recurrence for G σ (n) has order at most σ + 1.
Proof. This follows from Theorem AZ of [4]. To see this we apply that theorem to the quantity That is, for the expression F (n, x) in the statement of Theorem AZ we take P OL(n, x) = 1, a(x) = −x, b(x) = 1, S p (x) = 1, s(x) = σ+1 j=1 x j /j!, and t(x) = 1. The conclusion of the theorem is that G σ (n) (and hence G σ (n)) satisfies a linear recurrence of order at most L = σ + 1.