[Common Math 1 Part 22] Permutations and Combinations

한국어 버전

In the previous post, we emphasized that counting begins by deciding what makes cases different. In this post, we focus on one important standard: order.

Learn factorials, permutations, and combinations, and distinguish between ordered selections and unordered selections.

The key ideas are:

  • n!n! counts the ways to arrange nn distinct objects in a line.
  • A permutation is a selection where order matters.
  • A combination is a selection where order does not matter.
  • The combination formula comes from dividing out repeated counting in permutations.
  • After understanding the mathematics, we can generate actual arrays with Python.

1. Factorial

Before permutations and combinations, we need factorial notation.

n!n!

means the product of all natural numbers from nn down to 1.

n!=n×(n1)×(n2)××2×1n!=n\times(n-1)\times(n-2)\times\cdots\times2\times1

For example,

5!=5×4×3×2×1=1205!=5\times4\times3\times2\times1=120

Also,

1!=1,0!=11!=1,\qquad 0!=1

We define 0!=10!=1 because it makes many counting formulas work consistently. It also means that there is exactly one way to choose or arrange nothing: do nothing.


2. Arranging all objects

Suppose we arrange A, B, C in a line.

For the first position, there are 3 choices. After one is chosen, 2 remain. For the last position, 1 remains.

So the number of arrangements is

3×2×1=3!=63\times2\times1=3!=6

The actual arrangements are:

ABC, ACB, BAC, BCA, CAB, CBAABC,\ ACB,\ BAC,\ BCA,\ CAB,\ CBA

In general, the number of ways to arrange nn distinct objects in a line is

n!n!

3. Permutations: order matters

Now suppose we choose 2 objects from A, B, C and arrange them in order.

For the first position, there are 3 choices. For the second position, there are 2 remaining choices.

So the number is

3×2=63\times2=6

The actual ordered arrays are:

AB, AC, BA, BC, CA, CBAB,\ AC,\ BA,\ BC,\ CA,\ CB

Here ABAB and BABA are different because order matters.

The number of ways to choose rr objects from nn distinct objects and arrange them in order is written as

nPr{}_nP_r

and the formula is

nPr=n(n1)(n2)(nr+1)=n!(nr)!{}_nP_r=n(n-1)(n-2)\cdots(n-r+1)=\frac{n!}{(n-r)!}

How arrays are generated

Watch items being picked one by one and appended to the current list.

Array generation
Permutation

Remove the picked item before the next step.

Completed lists: 6

Progress: 1 / 15
0%
Speed
0%
Items
A B C

밝게 표시된 원소가 지금 선택할 수 있는 원소입니다. 번호가 붙은 손가락은 현재 리스트에 들어간 순서를 나타냅니다.

Current list
[]
List being built now
A
[] [🔴 A] [A]
Completed so far

아직 완성된 리스트가 없습니다.


4. Why the permutation formula works

The formula comes directly from the product rule.

When we fill rr positions:

  • 1st position: nn choices
  • 2nd position: n1n-1 choices
  • 3rd position: n2n-2 choices
  • ...
  • rrth position: nr+1n-r+1 choices

Therefore,

nPr=n(n1)(n2)(nr+1){}_nP_r=n(n-1)(n-2)\cdots(n-r+1)

This is the same as

n!(nr)!\frac{n!}{(n-r)!}

because the unused factors cancel.

For example,

5P3=5!2!=5×4×3=60{}_5P_3=\frac{5!}{2!}=5\times4\times3=60

5. A recurrence view of permutations

A permutation can also be generated from smaller permutations.

To make an ordered array of length rr:

  1. choose the first element;
  2. generate an ordered array of length r1r-1 from the remaining elements;
  3. attach the first element in front.

Symbolically,

P(n,r)=nP(n1,r1)P(n,r)=n\cdot P(n-1,r-1)

This is not just a formula. It describes how lists are actually generated.

Recurrence: building next arrays from previous arrays

A recurrence is not only a count; it is a rule that builds larger array lists from smaller ones.

Recurrence
{}_{4}C_{2}={}_{3}C_{1}+{}_{3}C_{2}

Combine arrays that include A and arrays that do not include A.

Progress: 1 / 6
0%
0%
Previous array
A를 뽑는 경우
[ B ] [ C ] [ D ]
Rule
[A] + 선택된 이전 배열
⬇️
New array
A B
Generated so far
[ A,B ]

6. Combinations: order does not matter

Now suppose we choose 2 objects from A, B, C, but order does not matter.

Then ABAB and BABA represent the same selection. The possible selections are:

AB, AC, BCAB,\ AC,\ BC

There are 3 cases.

The number of ways to choose rr objects from nn distinct objects without considering order is written as

nCr{}_nC_r

7. Why the combination formula works

First count as if order matters. That gives

nPr{}_nP_r

But each unordered group of rr objects has been counted r!r! times, because the same selected objects can be arranged in r!r! orders.

Therefore,

nCr=nPrr!{}_nC_r=\frac{{}_nP_r}{r!}

Using the permutation formula,

nCr=n!r!(nr)!{}_nC_r=\frac{n!}{r!(n-r)!}

For example,

5C2=5!2!3!=10{}_5C_2=\frac{5!}{2!3!}=10

How arrays are generated

Watch items being picked one by one and appended to the current list.

Array generation
Permutation

Remove the picked item before the next step.

Completed lists: 6

Progress: 1 / 15
0%
Speed
0%
Items
A B C

밝게 표시된 원소가 지금 선택할 수 있는 원소입니다. 번호가 붙은 손가락은 현재 리스트에 들어간 순서를 나타냅니다.

Current list
[]
List being built now
A
[] [🔴 A] [A]
Completed so far

아직 완성된 리스트가 없습니다.


8. A recurrence view of combinations

Combinations can be built by asking whether the first element is included.

For a fixed element A, every selection falls into one of two groups.

  1. Selections that include A
  2. Selections that do not include A

If A is included, we choose the remaining r1r-1 objects from the other n1n-1 objects. If A is not included, we choose all rr objects from the other n1n-1 objects.

So

C(n,r)=C(n1,r1)+C(n1,r)C(n,r)=C(n-1,r-1)+C(n-1,r)

This is Pascal's identity.

Recurrence: building next arrays from previous arrays

A recurrence is not only a count; it is a rule that builds larger array lists from smaller ones.

Recurrence
{}_{4}C_{2}={}_{3}C_{1}+{}_{3}C_{2}

Combine arrays that include A and arrays that do not include A.

Progress: 1 / 6
0%
0%
Previous array
A를 뽑는 경우
[ B ] [ C ] [ D ]
Rule
[A] + 선택된 이전 배열
⬇️
New array
A B
Generated so far
[ A,B ]

9. Important properties of combinations

Symmetry

Choosing rr objects to include is the same as choosing nrn-r objects to exclude.

nCr=nCnr{}_nC_r={}_nC_{n-r}

For example,

7C2=7C5{}_7C_2={}_7C_5

Edge cases

There is exactly one way to choose nothing, and exactly one way to choose everything.

nC0=1,nCn=1{}_nC_0=1,\qquad {}_nC_n=1

10. Math first, code later

The code below generates actual arrays, not just counts.

Generate permutation arrays with Python
def permutations(items, r):
    if r == 0:
        return [[]]

    result = []
    for i, item in enumerate(items):
        rest = items[:i] + items[i+1:]
        for tail in permutations(rest, r - 1):
            result.append([item] + tail)
    return result

print(permutations(['A', 'B', 'C'], 2))
print(len(permutations(['A', 'B', 'C'], 2)))
Generate combination arrays with Python
def combinations(items, r):
    if r == 0:
        return [[]]
    if not items:
        return []

    first = items[0]
    rest = items[1:]

    with_first = [[first] + tail for tail in combinations(rest, r - 1)]
    without_first = combinations(rest, r)
    return with_first + without_first

print(combinations(['A', 'B', 'C'], 2))
print(len(combinations(['A', 'B', 'C'], 2)))

11. Summary

  • n!n! counts the ways to arrange all nn distinct objects.
  • nPr{}_nP_r counts ordered selections.
  • nPr=n!(nr)!{}_nP_r=\dfrac{n!}{(n-r)!}
  • nCr{}_nC_r counts unordered selections.
  • nCr=n!r!(nr)!{}_nC_r=\dfrac{n!}{r!(n-r)!}
  • Combinations can be understood by dividing ordered cases by r!r!.
  • Recurrence relations show how the arrays themselves can be generated.

Chapter 22 Check

Permutations and combinations.

QUIZ
Question 1 / 0 Completed 0 / 0
Progress 0 / 0 0%
Current question Correct Incorrect Pending
Question 1 Multiple choice Pending
Score 0pts · Correct 0/0

💬 댓글

이 글에 대한 의견을 남겨주세요