Mathematics for Computer Science

Chapter 4
Combinatorics and Discrete Counting

Bachelors Program in Computer Science Engineering
Mathematics for Computer Science   •   Bachelor

Combinatorics and Discrete Counting

Introduction

Combinatorics studies finite structures and the principles used to count, construct, and analyze them. It is fundamental to computer science because algorithms operate on discrete objects: strings, graphs, schedules, subsets, database records, memory locations, test cases, and assignments of tasks to processors.

Counting is not merely arithmetic. A correct solution requires a precise model of what constitutes one outcome, whether order matters, whether repetition is permitted, and whether constraints cause outcomes to overlap. These questions determine whether permutations, combinations, inclusion–exclusion, generating functions, or another method is appropriate.

All counts in this chapter are dimensionless nonnegative integers. Unless stated otherwise, objects are assumed distinct and selections are finite.

Learning Objectives

After studying this chapter, students should be able to:

  • apply the addition, multiplication, division, and complement principles;
  • distinguish permutations from combinations;
  • count arrangements involving repeated objects or circular symmetry;
  • use binomial and multinomial identities;
  • solve allocation problems using stars and bars;
  • derive guarantees using the pigeonhole principle;
  • count overlapping sets using inclusion–exclusion;
  • construct combinatorial proofs of identities;
  • represent counting sequences with generating functions;
  • formulate counting algorithms and recognize combinatorial explosion;
  • apply discrete counting to scheduling, testing, security, and resource allocation.

Foundations of Counting

The addition principle

If a task can be completed using one of several mutually exclusive alternatives, and alternative i has ni outcomes, then the total number of outcomes is

n1 + n2 + ··· + nk.

Mutual exclusivity is essential. If alternatives overlap, direct addition causes double counting.

The multiplication principle

If a process has k successive stages, with ni choices at stage i for every valid history of earlier choices, then the total is

n1n2···nk.

For example, a system identifier consisting of two uppercase English letters followed by three decimal digits has

262 × 103 = 676,000

possible strings when repetition is allowed. Leading zeroes are valid because this is an identifier rather than a numerical value.

Complement and division principles

If a finite universe U contains |U| outcomes and A is a subset, then

|U − A| = |U| − |A|.

Complement counting is often easier than counting valid outcomes directly.

The division principle applies when a construction counts every distinct outcome exactly d times. The required count is then the construction count divided by d. Its use requires proof that the multiplicity d is constant.

Permutations and Arrangements

For a nonnegative integer n, the factorial is

n! = n(n−1)(n−2)···1,

with 0! = 1. The value n! counts linear arrangements of n distinct objects.

Ordered selections

The number of ordered selections of r distinct objects from n distinct objects, where 0 ≤ r ≤ n, is

P(n,r) = n!/(n−r)!.

The first position has n choices, the second has n−1, and so forth.

Worked example. From 12 software engineers, a lead, reviewer, and release manager must be assigned, with no person holding two roles. Since the roles are distinct,

P(12,3) = 12 × 11 × 10 = 1,320.

Repeated objects

Suppose n objects contain groups of identical objects with multiplicities n1,n2,…,nk, where n1+···+nk=n. The number of distinct arrangements is

n!/(n1!n2!···nk!).

For example, the word LEVEL contains two Ls, two Es, and one V. Its distinct arrangements number

5!/(2!2!) = 30.

Circular permutations

For n ≥ 1 distinct objects arranged around a circle, arrangements equivalent under rotation are counted once. Fixing one object removes rotational symmetry, giving

(n−1)!.

This formula does not identify mirror images. If reflection is also considered equivalent, further symmetry analysis is required; simply dividing by two is not universally valid for arrangements containing additional structure.

Combinations and Subsets

The number of unordered selections of r objects from n distinct objects is

C(n,r) = n!/[r!(n−r)!], 0 ≤ r ≤ n.

The notation C(n,r) is the binomial coefficient. Every r-element subset has r! orderings, so dividing P(n,r) by r! gives the formula.

The symmetry identity

C(n,r) = C(n,n−r)

follows because selecting the r included objects uniquely determines the n−r excluded objects.

Subsets and bit strings

Each subset of an n-element set corresponds to a binary string of length n: bit 1 means that an element is included, and bit 0 means that it is excluded. Therefore, an n-element set has 2n subsets.

Bijection between subsets and bit strings Subsets of {a,b,c} Bit strings ∅000 {a}100 {b}010 {c}001 {a,b}110 {a,c}101 {b,c}011 {a,b,c}111
Bijection between subsets of a three-element set and three-bit strings
Figure: Bijection between subsets of a three-element set and three-bit strings

Figure: A bijection between subsets and membership bit strings.

Binomial and Multinomial Identities

The binomial theorem

For an integer n ≥ 0,

(x+y)n = ∑r=0n C(n,r)xn−ryr.

To obtain a term containing yr, choose the factor y from exactly r of the n copies of (x+y).

Setting x=y=1 gives

∑r=0n C(n,r) = 2n,

which also counts all subsets according to their sizes.

Pascal’s identity is

C(n,r) = C(n−1,r) + C(n−1,r−1),

for 1 ≤ r ≤ n−1. Relative to a fixed element, an r-subset either excludes it or includes it.

Multinomial coefficients

If n distinct objects are divided into k labeled groups of prescribed sizes n1,…,nk, where the sizes are nonnegative and sum to n, the number of divisions is

n!/(n1!n2!···nk!).

Correspondingly,

(x1+···+xk)n = ∑ [n!/(n1!···nk!)]x1n1···xknk,

where the sum ranges over all nonnegative ni satisfying n1+···+nk=n.

Stars and Bars

Stars and bars counts distributions of identical objects among distinct containers.

The number of nonnegative integer solutions of

x1+x2+···+xk=n

is

C(n+k−1,k−1).

Here n stars represent objects and k−1 bars divide them among k labeled containers.

If every variable must be positive, let yi=xi−1. Then the number of positive solutions, when n ≥ k, is

C(n−1,k−1).

These formulas do not directly impose upper bounds. Upper-bounded problems require inclusion–exclusion, generating functions, or dynamic programming.

Worked example. Allocate 10 identical virtual-machine licenses among four departments, allowing a department to receive none. The number of allocations is

C(10+4−1,4−1) = C(13,3) = 286.

If every department must receive at least one license, the count becomes

C(10−1,4−1) = C(9,3) = 84.

The Pigeonhole Principle

The basic pigeonhole principle states that placing more than k objects into k boxes forces at least one box to contain at least two objects.

The generalized form states that if N objects are assigned to k ≥ 1 boxes, then some box contains at least

⌈N/k⌉

objects. This is the strongest universal lower bound: a distribution whose box loads differ by at most one attains it.

Worked example. If 101 requests are assigned to 10 servers, at least one server receives

⌈101/10⌉ = 11

requests. This guarantee is tight because loads of 11, 10, 10, 10, 10, 10, 10, 10, 10, and 10 sum to 101.

The principle proves existence but usually does not identify the overloaded box. Applications include hash collisions, load balancing, duplicate detection, memory allocation, and Ramsey-type arguments.

Inclusion–Exclusion

For two finite sets,

|A ∪ B| = |A| + |B| − |A ∩ B|.

The intersection is subtracted because it was counted twice. For three sets,

|A ∪ B ∪ C| = |A|+|B|+|C|−|A∩B|−|A∩C|−|B∩C|+|A∩B∩C|.

For finite sets A1,…,Am, the general inclusion–exclusion principle is

|⋃i=1m Ai| = ∑_{∅≠J⊆{1,…,m}} (−1)|J|+1|⋂j∈J Aj|.

Onto functions

Let a domain have n elements and a codomain have k labeled elements. There are kn functions in total. To count onto functions, exclude functions that omit one or more codomain elements:

∑j=0k (−1)j C(k,j)(k−j)n.

The formula remains valid when n<k, in which case the result is zero.

Derangements

A derangement is a permutation with no fixed point. Inclusion–exclusion gives

Dn = n!∑j=0n (−1)j/j!.

For example,

D4 = 24(1−1+1/2−1/6+1/24) = 9.

For n ≥ 1, Dn is the nearest integer to n!/e, but the finite sum is the exact formula.

Combinatorial Proofs

A combinatorial proof establishes an identity by showing that both sides count the same finite set, usually in different ways. Such proofs explain why an identity holds rather than merely verifying algebra.

Vandermonde’s identity

Suppose a population has m objects of one type and n of another. The number of ways to select r objects from the combined population is C(m+n,r). Alternatively, choose k from the first group and r−k from the second:

C(m+n,r) = ∑k=0r C(m,k)C(n,r−k),

where binomial coefficients with infeasible lower indices are interpreted as zero.

Counting nested subsets

The identity

C(n,r)C(r,k) = C(n,k)C(n−k,r−k)

holds for 0 ≤ k ≤ r ≤ n. The left side first chooses an r-subset and then a k-subset inside it. The right side first chooses the inner k-subset and then the remaining r−k elements of the outer subset.

A sound combinatorial proof must define the counted objects, explain each construction, and establish a bijection or equal counting multiplicity.

Generating Functions

A generating function encodes a sequence as coefficients of a formal power series. For a sequence a0,a1,…, its ordinary generating function is

A(x) = ∑n≥0 anxn.

In combinatorics, x may be treated formally; analytical convergence is unnecessary when only coefficient identities are used. The notation [xn]A(x) means the coefficient of xn in A(x).

Encoding choices

Choice patternGenerating factor
Select zero or one object of weight w1+xw
Select up to b identical objects of weight w1+xw+···+xbw
Select any nonnegative number of weight w1/(1−xw)

Multiplying generating functions combines independent choices. Coefficient extraction performs the required convolution.

Worked example: bounded allocation. Count integer solutions of

x1+x2+x3+x4=5, 0≤xi≤2.

The answer is [x5](1+x+x2)4. By stars and bars there are C(8,3)=56 unrestricted nonnegative solutions. For each index, solutions with xi≥3 correspond to a residual sum of 2, giving C(5,3)=10. No two variables can both be at least 3 because their sum would exceed 5. Therefore,

56 − 4×10 = 16.

Worked example: coin combinations. With unlimited coins of values 1, 2, and 5, the number of unordered combinations totaling 10 is

[x10] 1/[(1−x)(1−x2)(1−x5)].

Solving a+2b+5c=10 gives 6 solutions for c=0, 3 for c=1, and 1 for c=2, for a total of 10.

Recurrences and generating functions

Let F0=0, F1=1, and Fn=Fn−1+Fn−2 for n≥2. If F(x)=∑n≥0Fnxn, shifting and combining the recurrence yields

F(x) = x + xF(x) + x2F(x),

and hence

F(x) = x/(1−x−x2).

Exponential generating functions, defined by ∑n≥0anxn/n!, are particularly useful for labeled structures. They should not be interchanged with ordinary generating functions without accounting for the factorial normalization.

Counting in Algorithms

Combinatorial formulas influence both algorithm design and complexity analysis.

  • Exhaustive subset search examines 2n candidates.
  • Exhaustive permutation search examines n! orderings.
  • A dynamic program for bounded sums can multiply truncated generating polynomials instead of enumerating all assignments.
  • Inclusion–exclusion can replace direct enumeration when the number of constraints is moderate.
  • Fast polynomial multiplication accelerates large generating-function computations.

For exact computation of C(n,r), use symmetry r←min(r,n−r) and evaluate factors incrementally. Arbitrary-precision integers may be necessary. Modular division using factorial inverses is valid only when the modulus and denominators satisfy the required invertibility conditions—for example, under a prime modulus with factorial arguments smaller than the modulus.

Enumeration algorithms should be output-sensitive where possible: generating all C(n,r) subsets necessarily requires at least proportional time simply to emit them.

Illustrative Case: Planning a Software Regression Campaign

Background

A fictional software organization is preparing regression tests for a release containing 14 components: six security-critical components and eight ordinary components. Each test bundle covers five distinct components. Separately, nine distinct regression jobs must be assigned to three labeled execution windows, with every window receiving between one and four jobs.

Problem

The release manager needs the number of valid component bundles and feasible schedules. This count determines the search space for an optimization program that later considers cost and dependency constraints.

Analysis and solution

A valid bundle contains at least two security-critical components. If it contains s security components, then s can be 2, 3, 4, or 5. Thus,

C(6,2)C(8,3)+C(6,3)C(8,2)+C(6,4)C(8,1)+C(6,5)C(8,0)
= 15×56 + 20×28 + 15×8 + 6×1
= 1,526.

For scheduling, the permitted load triples sum to 9, with each load at most 4. Up to order, the possibilities are (1,4,4), (2,3,4), and (3,3,3). Because jobs are distinct and windows are labeled:

3·9!/(1!4!4!) + 6·9!/(2!3!4!) + 9!/(3!3!3!)
= 1,890 + 7,560 + 1,680
= 11,130.

The factors 3 and 6 count distinct assignments of unequal load sizes to the labeled windows.

If bundle choice and scheduling are independent, the total number of candidate plans is

1,526 × 11,130 = 16,984,380.

Outcome

The count demonstrates that brute-force evaluation is possible but potentially expensive once additional configuration choices are introduced. The organization can use dynamic programming, constraint programming, or branch-and-bound to avoid exploring infeasible candidates.

Learning points

  • Combinations count component bundles because bundle order is irrelevant.
  • Multinomial coefficients assign distinct jobs to windows with fixed loads.
  • Constraints must be modeled before applying a formula.
  • Multiplication is valid only because bundle selection and scheduling are independent stages.
  • Counting the search space informs algorithm and infrastructure choices.

Limitations, Governance, and Best Practice

Counting models are only as valid as their assumptions. Common errors include treating distinct objects as identical, ignoring symmetry, adding overlapping cases, or using stars and bars despite upper bounds. Real systems may also include dependencies that destroy the independence required by the multiplication principle.

Combinatorial explosion is a practical limitation. Exact counting problems such as model counting for Boolean formulas can be computationally intractable. Contemporary tools combine decision diagrams, SAT solvers, symbolic algebra, dynamic programming, randomized approximation, and parallel computation. AI-assisted modeling may suggest constraints or decompositions, but results should be checked against small cases and independently derived bounds.

Counting also has governance implications. Estimating password spaces must not be confused with measuring actual security: user behavior, rate limits, credential reuse, and implementation flaws matter. Enumeration of user combinations or data linkages can create privacy risks. Organizations should minimize sensitive data, enforce authorized use, document assumptions, and avoid reporting misleadingly precise counts.

Recommended practice is to define outcomes explicitly, test formulas on small instances, use complementary methods where possible, preserve exact integer arithmetic, and separate mathematical possibility from operational probability.

Chapter Summary

Combinatorics provides a rigorous language for finite decision spaces. The addition and multiplication principles form the foundation of counting. Permutations apply when order matters, while combinations count unordered selections. Multiset, circular, binomial, and multinomial formulas address repeated objects and structured arrangements. Stars and bars counts unconstrained or positive allocations of identical objects.

The pigeonhole principle supplies sharp existence guarantees without identifying a specific object. Inclusion–exclusion corrects overlapping counts and yields formulas for onto functions and derangements. Combinatorial proofs establish identities by counting the same structures in different ways. Generating functions encode counting sequences and transform constrained selection into coefficient extraction. Together, these methods support algorithm analysis, scheduling, testing, security assessment, and resource allocation while revealing the practical consequences of combinatorial explosion.

Key Terms

Addition principle: Rule for adding counts of mutually exclusive alternatives.

Multiplication principle: Rule for multiplying counts across successive independent-choice stages.

Permutation: An ordered arrangement or selection of objects.

Factorial: Product n(n−1)···1, with 0!=1.

Combination: An unordered selection of objects.

Binomial coefficient: The value C(n,r) counting r-subsets of an n-element set.

Multinomial coefficient: Count of divisions into labeled groups of prescribed sizes.

Stars and bars: Method for counting integer allocations among labeled containers.

Pigeonhole principle: Principle guaranteeing that some container has a minimum load.

Inclusion–exclusion: Alternating-sum method for correcting overlapping counts.

Derangement: A permutation with no fixed point.

Onto function: A function whose range equals its entire codomain.

Combinatorial proof: Proof that equates two expressions by counting the same set differently.

Bijection: A one-to-one and onto correspondence between two sets.

Generating function: Formal power series whose coefficients encode a sequence.

Coefficient extraction: Operation of selecting a specified power’s coefficient from a generating function.

Recurrence relation: Equation defining sequence terms from earlier terms.

Combinatorial explosion: Rapid growth of a discrete search space.

Review Questions

Short Answer Questions

  1. State the multiplication principle and its main applicability condition.
  2. Explain the difference between a permutation and a combination.
  3. State the generalized pigeonhole principle.
  4. What does [xn]A(x) mean?
  5. Why does direct addition fail for overlapping sets?

Long Answer Questions

  1. Derive the formula for permutations of a multiset and illustrate it with an example.
  2. Explain the binomial theorem and give both an algebraic and combinatorial interpretation of its coefficients.
  3. Derive the stars-and-bars formulas for nonnegative and positive integer solutions.
  4. Develop the inclusion–exclusion formula for onto functions.
  5. Explain how generating functions represent constrained selections and recurrences.

Analytical / Application Questions

  1. A data center assigns 250 distinct requests to 24 servers. Determine the strongest guaranteed minimum load of the busiest server and prove that the bound is tight.
  2. Count the eight-character strings over uppercase English letters that contain at least one A and at least one B, allowing repetition.
  3. Design a method to count solutions of x1+x2+x3+x4=20 subject to 0≤xi≤7, using either inclusion–exclusion or a generating function.

Multiple Choice Questions with Answers

Question 1

How many distinct arrangements are there of the letters in LEVEL?

A. 20

B. 30

C. 60

D. 120

Correct Answer: B. 30

Question 2

How many three-person committees can be selected from eight distinct people?

A. 24

B. 48

C. 56

D. 336

Correct Answer: C. 56

Question 3

What is the coefficient of x4 in (1+x)7?

A. 21

B. 28

C. 35

D. 70

Correct Answer: C. 35

Question 4

Exactly 101 requests are assigned to 10 servers with no other restrictions. What is the minimum possible value of the maximum server load?

A. 9

B. 10

C. 11

D. 12

Correct Answer: C. 11

Question 5

If |A|=40, |B|=28, and |A∩B|=13, what is |A∪B|?

A. 42

B. 55

C. 68

D. 81

Correct Answer: B. 55

Question 6

How many nonnegative integer solutions does x1+x2+x3=7 have?

A. 21

B. 28

C. 36

D. 45

Correct Answer: C. 36

Question 7

What is the coefficient of x5 in (1+x+x2)3?

A. 1

B. 3

C. 6

D. 9

Correct Answer: B. 3

Question 8

How many derangements are there of four distinct objects?

A. 6

B. 8

C. 9

D. 12

Correct Answer: C. 9

Question 9

How many onto functions exist from a five-element set to a two-element labeled set?

A. 16

B. 30

C. 31

D. 32

Correct Answer: B. 30

Question 10

Which identity counts all subsets of an n-element set by subset size?

A. ∑k=0n C(n,k)=2n

B. ∑k=0n C(n,k)=n!

C. ∑k=0n C(n,k)=n2

D. ∑k=0n C(n,k)=2n

Correct Answer: A. ∑k=0n C(n,k)=2n

Mathematics for Computer Science
Chapter 4 of 15