Combinatorics studies how to count selections, arrangements, and cases.

In algorithms, it appears when the problem asks how many ways something can happen.

Core Idea

The main distinction is between order mattering and order not mattering. Arrangements care about order. Combinations do not.

Counting can be direct through formulas, recursive through dynamic programming, or constrained by modular arithmetic.

Python Example

import math
 
ways_to_choose = math.comb(5, 2)
arrangements = math.perm(5, 2)

math.comb(5, 2) counts unordered selections. math.perm(5, 2) counts ordered arrangements.

Common Confusions

Counting all cases is not the same as listing all cases. A problem may need only the count, and listing every case may be too slow.

Be careful about duplicates. Counting arrangements of repeated values requires different reasoning from counting arrangements of distinct values.

When To Use It

Use combinatorics when a problem asks for number of ways, possible selections, arrangements, partitions, probability counts, or DP transitions based on choices.