Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools


Weak Composition


Press ▶ Play to build all C(n+k−1, k−1) weak compositions of n identical items into k bins (empty bins allowed), or Step ▶ to drop one bar at a time. A bin is the run of cells between two adjacent bars (or before the first / after the last); the count of items in bin i is xᵢ. Each outcome = a choice of k − 1 bar positions among n + k − 1 total cells.
● BallsA Letters
n =4?
k =3?
Speed
C(6, 2) = 15Press Play or Step to beginBARS TO PLACE (k − 1 = 2)STRIP (n + k − 1 = 6 cells)1234561111x₁ = ?x₂ = ?x₃ = ?composition: (?, ?, ?)bar positions: {?, ?}Choose k − 1 = 2 bar positions out of n + k − 1 = 6 cells:C(6, 2) = 6!/(2! · 4!) = 720/(2·24) = 15
Weak compositions (stars and bars)
C(n+k−1, k−1) = C(6, 2)
= 6! / (2! · 4!)
= 720 / (2 · 24)
= 15
Distribute n identical items into k distinct bins, with empty bins allowed. Count the solutions to x₁ + x₂ + … + xₖ = n in non-negative integers. Equivalently: choose k − 1 bar positions among n + k − 1 cells — the remaining cells hold the items.





Key Terms



Key Terms

Weak composition — a distribution of nn identical items into kk distinct bins where bins may be empty. Equivalently, the number of non-negative integer solutions to x1+x2++xk=nx_1 + x_2 + \dots + x_k = n. The count is (n+k1k1)\binom{n + k - 1}{k - 1}.

Stars and bars — the visual encoding of this problem. Draw nn stars in a row and insert k1k - 1 bars among them to split them into kk groups. The count of arrangements is (n+k1k1)\binom{n + k - 1}{k - 1} because there are n+k1n + k - 1 total symbols and we choose k1k - 1 positions for the bars.

Bin — the run of cells between two adjacent bars in the strip, or before the first bar, or after the last. The number of items in bin ii is xix_i.

Strip — the row of n+k1n + k - 1 cells where items (stars) and bars are placed. Each cell either holds one item or one bar.

Composition tuple $(x_1, x_2, \dots, x_k)$ — the encoded outcome: xix_i is the count of items in bin ii, with x1+x2++xk=nx_1 + x_2 + \dots + x_k = n and each xi0x_i \geq 0.

$x_1$-group — the family of weak compositions with the same first-bin count. The completed section groups results by x1x_1; group sizes vary as (nj+k2k2)\binom{n - j + k - 2}{k - 2} for x1=jx_1 = j.

Getting Started

The tool opens with n=4n = 4 identical items and k=3k = 3 bins. The scene splits into three areas:

• A holding row at the top labeled *BARS TO PLACE (k − 1 = K)* showing the k1k - 1 bars waiting to be inserted.

• A strip in the middle labeled *STRIP (n + k − 1 = N+K-1 cells)* — a row of n+k1n + k - 1 cells, numbered 11 to n+k1n + k - 1 above. The nn items (identical, rendered as red balls or letter A) are pre-placed in the cells from the start. The animation moves the bars from the holding row into specific cell positions.

• A completed section below, where every finished composition is filed under the row matching its x1x_1 value.

To run the visualization:

• Press ▶ Play to auto-build all (n+k1k1)\binom{n + k - 1}{k - 1} weak compositions.

• Press Step ▶ to drop one bar at a time.

• Press to step backward.

• Adjust the Speed slider.

The header shows the formula C(n+k1,k1)=totalC(n + k - 1, k - 1) = \text{total} alongside a live status line like *x1=jx_1 = j: kk / size*.

The Strip and Bars

The strip is where the stars-and-bars encoding plays out:

Numbered cells 11 through n+k1n + k - 1 across the top. The current cell where a bar lands highlights to mark its position.

Pre-placed items (red balls or the letter A) sit in cells that aren't currently bar positions. Items are identical — same color, same size — because the scenario treats them as indistinguishable.

Bins are highlighted with alternating color bands that cover only the cells inside each bin (not the bar cells). Bin 1 might be blue-tinted, bin 2 amber, bin 3 blue again, and so on.

Bars arrive from the holding row above with a dotted guide line tracing the trajectory.

Brackets below the strip mark each bin with a label xi=jx_i = j. Brackets are progressive: each label shows ?? until the relevant bar lands, then it reveals the actual value.

Empty bins appear as dashed mini-rectangles in the bracket row, indicating that no cells lie between adjacent bars.

When all k1k - 1 bars are placed, a flash ring pulses around the strip and the composition is filed in the completed grid below.

Adjusting n and k

Two steppers in the control bar control the size:

n sets the number of identical items. Range 33 to 55.

k sets the number of distinct bins. Range 22 to 44.

Common combinations:

n=3,k=2n = 3, k = 2: (41)=4\binom{4}{1} = 4 compositions.

n=3,k=3n = 3, k = 3: (52)=10\binom{5}{2} = 10 compositions.

n=4,k=3n = 4, k = 3: (62)=15\binom{6}{2} = 15 compositions.

n=4,k=4n = 4, k = 4: (73)=35\binom{7}{3} = 35 compositions.

n=5,k=3n = 5, k = 3: (72)=21\binom{7}{2} = 21 compositions.

n=5,k=4n = 5, k = 4: (83)=56\binom{8}{3} = 56 compositions.

Reducing nn does not constrain kk in this scenario, since even n=0n = 0 gives valid weak compositions (all xi=0x_i = 0). Changing either value resets the build, refreshes the formula in the header, and rebuilds the completed section into a new set of x1x_1-rows.

Grouping by First-Bin Count

The completed section organizes weak compositions by x1x_1 — the count of items in the first bin. There are n+1n + 1 rows total (one for each value x1=0,1,2,,nx_1 = 0, 1, 2, \dots, n), and group sizes vary.

When x1=jx_1 = j, the remaining njn - j items must be distributed among the remaining k1k - 1 bins, also as a weak composition:

group size at x1=j=(nj+k2k2)\text{group size at } x_1 = j = \binom{n - j + k - 2}{k - 2}


For example with n=4,k=3n = 4, k = 3:

x1=0x_1 = 0: distribute 4 items into 2 bins. (51)=5\binom{5}{1} = 5 compositions.

x1=1x_1 = 1: distribute 3 items into 2 bins. (41)=4\binom{4}{1} = 4 compositions.

x1=2x_1 = 2: distribute 2 items into 2 bins. (31)=3\binom{3}{1} = 3 compositions.

x1=3x_1 = 3: distribute 1 item into 2 bins. (21)=2\binom{2}{1} = 2 compositions.

x1=4x_1 = 4: distribute 0 items into 2 bins. (11)=1\binom{1}{1} = 1 composition.

Total: 5+4+3+2+1=15=(62)5 + 4 + 3 + 2 + 1 = 15 = \binom{6}{2}. The decomposition is a visual proof of the hockey-stick identity j=0n(nj+k2k2)=(n+k1k1)\sum_{j=0}^{n} \binom{n - j + k - 2}{k - 2} = \binom{n + k - 1}{k - 1}.

Transport Controls

The control bar offers four transport buttons plus a speed slider:

(Step back) — walks the animation one step backward.

Step ▶ (Step forward) — drops one bar into its cell. Stop after each step to read the partial composition; cells without bars yet show ?? in their brackets.

▶ Play / ⏸ Pause — runs continuously until all (n+k1k1)\binom{n + k - 1}{k - 1} weak compositions are built, then auto-pauses.

↺ Reset — clears the completed section and starts over.

The Speed slider controls how fast play advances. At slower speeds you can clearly see each bar travel from the holding row into its cell and watch the bin brackets update from ?? to the actual count.

Mode Switch

The Mode switch at the start of the control bar toggles how identical items are rendered:

Balls mode (default) — items appear as red circles. All circles are identical because the items themselves are indistinguishable. Best for the classical stars-and-bars look.

Letters mode — items appear as the letter A. Same identicality applies — every item shows the same letter in the same color. Best when reading compositions algebraically and emphasizing that the items are interchangeable.

The encoding is consistent across the strip, the mini cards in the completed grid, and the right-panel narration. Bars are unaffected — they always render as the same accent-colored vertical bars regardless of mode.

Right Panel and Progress

The right panel narrates the build as it unfolds, anchored by the header *Weak compositions (stars and bars)* with the full formula across multiple lines and a reminder that this counts non-negative integer solutions to x1+x2++xk=nx_1 + x_2 + \dots + x_k = n.

A StepRow is added for each x1x_1-group as soon as a composition in that group starts or completes. Each StepRow shows:

• The first bin count as a label — *First bin: x1=jx_1 = j*.

• A progress counter k/(nj+k2k2)k / \binom{n - j + k - 2}{k - 2} tracking how many compositions in this group have completed.

• A short narration of the structure: *When the first bin holds jj items (x1=jx_1 = j), the remaining njn - j items are split among the other k1k - 1 bins, giving (nj+k2k2)\binom{n - j + k - 2}{k - 2} outcomes.* Edge cases get tailored phrasing: x1=0x_1 = 0 uses *the first bin is empty*; x1=nx_1 = n uses *all items go to the first bin* with the others empty.

When all groups complete, every StepRow shows *done* with a checkmark, and the counter reaches *total / total*.

What Is a Weak Composition

A weak composition of nn into kk parts is an ordered tuple (x1,x2,,xk)(x_1, x_2, \dots, x_k) of non-negative integers summing to nn:

x1+x2++xk=n,xi0x_1 + x_2 + \dots + x_k = n, \quad x_i \geq 0


The word *weak* distinguishes this from a strong composition, which requires every xi1x_i \geq 1. Weak compositions allow zero parts, so the first bin (or any bin) is permitted to be empty.

The count of weak compositions is:

(n+k1k1)=(n+k1n)\binom{n + k - 1}{k - 1} = \binom{n + k - 1}{n}


Examples:

• Distributing 1010 identical candies among 33 children (some children may get none): (122)=66\binom{12}{2} = 66 ways.

• Number of non-negative integer solutions to a+b+c+d=7a + b + c + d = 7: (103)=120\binom{10}{3} = 120.

• Choosing 55 scoops of ice cream from 66 flavors with repetition allowed (a flavor may be skipped): (105)=252\binom{10}{5} = 252 choices.

• Distributing nn identical items into kk labeled boxes with no capacity limit: the canonical setup.

For deeper coverage of weak compositions and their connections, see the weak composition section on the combinations theory page.

The Stars-and-Bars Argument

The classical proof maps weak compositions to a simple selection problem.

Encode each composition as a row of symbols. Write x1x_1 stars, then a bar, then x2x_2 stars, then a bar, and so on through xkx_k stars. The total is nn stars plus k1k - 1 bars, giving n+k1n + k - 1 symbols in a row.

Every weak composition is determined by where the bars sit. The bars partition the row into kk groups; the iith group has xix_i stars. Different bar placements give different compositions, and every composition comes from a unique bar placement (including bars adjacent to each other, which encode xi=0x_i = 0).

Count the bar placements. We're choosing k1k - 1 positions out of n+k1n + k - 1 total to be bars (the rest are stars). The count is the binomial coefficient:

(n+k1k1)\binom{n + k - 1}{k - 1}


This is the visual setup the tool implements. Each completed strip has nn items in fixed (canonical) positions and k1k - 1 bars in specific cells; the cells without bars hold the items, and the brackets below convert the bar positions back into the tuple (x1,,xk)(x_1, \dots, x_k).

The dual reading (n+k1n)\binom{n + k - 1}{n} — choose the nn star positions instead of the bars — gives the same count by complementary counting.

Related Concepts

Strong composition — the same setup but every bin must hold at least one item. Formula (n1k1)\binom{n - 1}{k - 1}. Smaller than the weak count because zero parts are forbidden.

Simple combination — the no-repetition unordered selection. (nr)\binom{n}{r}. Weak compositions can be seen as combinations with repetition allowed: choosing nn items from kk types with reuse equals weak compositions of nn into kk parts.

Combination with repetition — exactly the weak composition count (n+k1n)\binom{n + k - 1}{n} under a different name and framing.

Distribution into cells — distributes nn distinct items (not identical) into kk labeled cells. Formula knk^n, much larger than the weak composition count because items can be distinguished.

Partition into groups — distributes nn distinct items into kk labeled boxes of fixed sizes. Multinomial coefficient n!/(n1!nk!)n! / (n_1! \cdots n_k!).

Multiset coefficient ((kn))\binom{\binom{k}{n}}{} — equivalent notation for the weak composition count, emphasizing that the outcome is a size-nn multiset over a kk-element ground set.

Hockey-stick identity — the grouping by x1x_1 proves j=0n(nj+k2k2)=(n+k1k1)\sum_{j=0}^{n} \binom{n - j + k - 2}{k - 2} = \binom{n + k - 1}{k - 1}.

Combinatorics calculator — to compute (n+k1k1)\binom{n + k - 1}{k - 1} for any nn and kk, see the weak composition calculator.