Distributing Identical Objects
Section titled “Distributing Identical Objects”Stars and bars counts distributions of identical objects into distinct boxes. Equivalently, it counts integer solutions of
Write stars in a row and insert bars to separate the boxes. Adjacent bars mean an empty box. Choosing the positions of the bars among positions gives
If the objects are distinct, assigning each object to a box instead gives . If the boxes are indistinguishable, ordinary stars and bars does not apply.
Example. Twelve identical candies are distributed to four named students, with empty shares allowed. How many distributions are possible?
A distribution is determined by the four nonnegative candy counts. Stars and bars gives
Lower and Upper Bounds
Section titled “Lower and Upper Bounds”For lower bounds , first give each box its required minimum. Setting leaves a nonnegative distribution of objects. If that total is negative, there are no solutions.
Example. Find the number of integer solutions to with , , and .
Set , , and . Then and . Hence the count is
Upper bounds need a different correction. Count all nonnegative solutions, then exclude the cases where a variable exceeds its bound. If two violations can occur together, inclusion–exclusion restores their overlap.
Example. How many integer solutions satisfy and ?
There are unrestricted solutions. For a violation , set ; the remaining total is , giving solutions. The same count holds for and .
Two violations would require a total of at least , so none overlap. The answer is
For uniform upper bounds , the general count is
where a term is zero when . The shift is because that is the first forbidden value.
Repeated Objects and Gaps
Section titled “Repeated Objects and Gaps”An arrangement with identical objects of type has possibilities. For restrictions such as “no two adjacent,” arrange the other objects first and place the restricted objects in gaps.
Example. How many arrangements of four identical A’s and three identical B’s have no two B’s adjacent?
Place the A’s first:
There are five gaps. Choose three different gaps for the B’s, giving arrangements. A gap receives at most one B, exactly enforcing the restriction. Because all B’s are identical, no additional factor of is needed.
Lattice Paths and Waypoints
Section titled “Lattice Paths and Waypoints”A path from to using only unit right and up steps has moves. Choose which moves are up steps:
To force a waypoint, count the path before it and after it separately. To forbid a waypoint, subtract the paths that pass through it.
Example. How many right/up paths from to avoid ?
There are paths in total. A path through has choices before that point and afterward. Thus the answer is
Restricted Paths and Catalan Numbers
Section titled “Restricted Paths and Catalan Numbers”Paths from to that never go above are counted by the Catalan number
To derive it, start with all paths. For a bad path, reflect the initial segment up to its first arrival at by exchanging right and up moves. This transforms it into a path with right moves and up moves. The transformation is reversible by reflecting up to the first arrival at . Thus there are bad paths, and
Example. How many strings containing four opening and four closing parentheses are properly balanced?
Map an opening parenthesis to a right step and a closing parenthesis to an up step. A balanced prefix has at least as many opens as closes, so its path stays on or below . This is a reversible correspondence, giving
Generating Functions for Bounded Distributions
Section titled “Generating Functions for Bounded Distributions”A generating function can record the allowed size of each box separately. If box may receive amounts in a set , assign it the polynomial or series . Then
counts distributions totaling . Selecting one term from each factor selects the amount in each box, and multiplying adds those amounts through the exponent.
This method is particularly useful when the boxes have different restrictions, such as one receiving an even amount and another receiving at least two. The restrictions belong in the factors before any algebra is performed.
For an unrestricted nonnegative box, the factor is . A box receiving at least contributes , while a box receiving between zero and contributes . The numerator in the bounded case is exactly the algebraic form of excluding a first forbidden amount.
Example. Three fair six-sided dice are rolled. Find the number of ordered outcomes whose sum is ten.
Each die contributes , so the desired count is
Only the constant and numerator terms can contribute. Since , the answer is
The factors correspond to named dice, so outcomes with different orders are distinct. The method would need adjustment if only the multiset of face values were counted.
Different Upper Bounds and Capacity Checks
Section titled “Different Upper Bounds and Capacity Checks”For unequal upper bounds , declare the violation for box to be . For a chosen set of violating boxes, subtract the corresponding quantities and count the residual nonnegative solutions. Intersections with negative residual total contribute zero.
Before computing a long sum, check capacity: if there are no distributions. Likewise, lower bounds may already exceed the available total. These checks can collapse a problem before inclusion–exclusion begins.
Example. Count solutions to with , , and .
Set , leaving . The unrestricted count is . A violation of the first bound shifts four units, giving solutions. A violation of the second shifts five units, giving solutions.
Both violations together require at least nine units, more than eight, so their intersection is empty. The answer is .
An upper bound is not handled by simply replacing the total in stars and bars. It restricts individual coordinates, so the correction depends on which boxes violate it. The generating-function and inclusion–exclusion approaches are two descriptions of that same correction.
Indistinguishable Boxes and Integer Partitions
Section titled “Indistinguishable Boxes and Integer Partitions”Stars and bars assumes the boxes are labeled. If the boxes are indistinguishable, permuting their contents does not give a new distribution. Dividing a labeled count by is generally invalid because repeated box sizes have smaller symmetry orbits.
An integer partition writes a positive integer as an unordered sum of positive parts. A distribution into exactly nonempty indistinguishable boxes corresponds to a partition into exactly parts. To list them without repetition, put the parts in nondecreasing order.
Example. Eight identical objects are put into three nonempty indistinguishable boxes. Find the number of distributions.
Write the box sizes as with . If , the possibilities are . If , they are . Since , no larger smallest part is possible.
Thus there are five distributions. The ordered stars-and-bars count would be , and dividing by six would fail because the repeated-part patterns have different numbers of orderings.
Let count partitions of into exactly positive parts. Separate partitions containing a part of size one from those whose parts are all at least two. Removing one part of size one gives . Subtracting one from each of the parts in the second case gives . Therefore
The two cases are disjoint, and each construction is reversible. This recurrence is useful when a direct list becomes too long.
Several Forbidden Waypoints
Section titled “Several Forbidden Waypoints”Paths through a sequence of waypoints can be counted by multiplying the counts between consecutive points, provided the coordinates never decrease. If a later waypoint lies left or below an earlier one, no right/up path can visit them in that order.
When avoiding several points, inclusion–exclusion is often more efficient than building a recurrence across the whole grid. The intersection terms are counts of paths visiting several waypoints, so ordering and compatibility matter.
Example. How many right/up paths from to avoid both and ?
There are paths in total. Through either specified point there are
paths. A path through both points splits into three path segments, giving .
Inclusion–exclusion yields . The last term restores paths that were excluded twice.
For obstacles covering edges rather than vertices, describe the forced step as well as the endpoints. A path using a specified edge splits into a path to its starting vertex, the edge itself, and a path from its ending vertex. This avoids accidentally subtracting every path through the vertex.
Reflection and Ballot-Type Counts
Section titled “Reflection and Ballot-Type Counts”The reflection principle works because it maps every bad path to one unrestricted path with a different endpoint. The reflecting point must be uniquely chosen, usually the first time a boundary is crossed. Choosing an arbitrary crossing makes the correspondence nonunique.
For paths from to with that never go above , reflect the initial segment up to the first point with . A bad path becomes a path with right steps and up steps. Therefore the good count is
At , this reduces to the Catalan formula. The numerator records the extra horizontal room available before the path violates the diagonal constraint.
Example. A vote count contains six votes for A and four for B. In how many orders of the ten votes is A never behind B during the count?
Treat A as a right step and B as an up step. The prefix condition is exactly . The count is
If A must be strictly ahead after every nonempty prefix, the boundary condition changes and this formula cannot be used unchanged.
Catalan Decompositions and Their Generating Function
Section titled “Catalan Decompositions and Their Generating Function”Catalan objects often split into two smaller objects at a distinguished first return or matching pair. For balanced parentheses, match the first opening parenthesis to its closing partner. The substring inside is a balanced string, and the substring after it is another balanced string.
If there are pairs in total and pairs inside, there are pairs after the partner. This gives
Let . The convolution becomes . Solving the quadratic gives
The minus sign is selected because the series must have a finite constant term of one; the other branch has a negative power of . This example shows how the same sequence can be understood through reflection, decomposition, and generating functions. Each viewpoint is useful for a different kind of modification to the problem.
Counting Missing Capacity Instead of Used Capacity
Section titled “Counting Missing Capacity Instead of Used Capacity”A distribution near full capacity can be easier to count through its deficits. For labeled boxes each with capacity , put . Then
with the same bounds . This is a reversible transformation, so the counts at totals and are equal.
Example. Thirty-one identical candies are distributed to five named students, each receiving at most seven. Count the distributions.
The total capacity is thirty-five, so the five deficits sum to four. Because their total is already less than seven, no individual deficit can exceed the allowed maximum. Ordinary stars and bars gives distributions.
Directly applying inclusion–exclusion at total thirty-one would require several large alternating terms. The deficit model makes every upper bound automatic.
This complements the earlier lower-bound shift. Giving each box its minimum counts excess above a floor; subtracting each amount from capacity counts missing units below a ceiling.