TExcelTAD
Home › Discrete Mathematics › Permutations and the Generalized Product Rule

Permutations and the Generalized Product Rule

The product rule assumes each choice comes from a fixed set. Often the options shrink as you go: once a runner has won gold, she cannot also win silver. The generalized product rule handles that, and it counts permutations: arrangements where order matters and nothing repeats.

The generalized product rule

If a sequence is built in kk steps and, whatever the earlier choices were, step ii always has exactly nin_i options, then there are n1⋅n2⋯nkn_1 \cdot n_2 \cdots n_k sequences.

Medals. 8 runners, gold/silver/bronze: 8⋅7⋅6=3368 \cdot 7 \cdot 6 = 336 podiums. The set of options at step 2 depends on who won gold, but its size is always 7.

Permutations

A permutation of nn distinct objects is an ordering of all of them. There are

n!=n⋅(n−1)⋯2⋅1n! = n \cdot (n - 1) \cdots 2 \cdot 1

of them, with 0!=10! = 1 (one way to order nothing). An ordering of kk of the nn objects (a kk-permutation) can be done in

P(n,k)=n(n−1)⋯(n−k+1)=n!(n−k)!P(n, k) = n (n - 1) \cdots (n - k + 1) = \frac{n!}{(n - k)!}

ways.

python · try it yourself
from itertools import permutations
from math import factorial, perm

print("orderings of 'abc':", ["".join(p) for p in permutations("abc")])
print("4! =", factorial(4), " P(8, 3) =", perm(8, 3), " by listing:", len(list(permutations(range(8), 3))))
print("52! =", factorial(52))
orderings of 'abc': ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
4! = 24  P(8, 3) = 336  by listing: 336
52! = 80658175170943878571660636856403766975289505440883277824000000000000

52!≈8×106752! \approx 8 \times 10^{67}: every well-shuffled deck is almost certainly in an order that has never existed before.

Permutations as functions

A permutation of {0,…,n−1}\{0, \ldots, n-1\} is a bijection from the set to itself. Composing two permutations gives a permutation, and every permutation has an inverse. Every permutation splits into cycles: follow i→π(i)→π(π(i))→⋯i \to \pi(i) \to \pi(\pi(i)) \to \cdots until you return. The cycle structure tells how many times you must apply a shuffle before the cards return to their starting order: the lcm of the cycle lengths.

python · try it yourself
from math import lcm

def cycles(perm):
    seen, result = set(), []
    for start in range(len(perm)):
        if start not in seen:
            c, x = [], start
            while x not in seen:
                seen.add(x); c.append(x); x = perm[x]
            result.append(c)
    return result

# a "perfect out-shuffle" of 8 cards: interleave the two halves, top card stays on top
n = 8
out_shuffle = [0] * n
for i in range(n // 2):
    out_shuffle[2 * i] = i
    out_shuffle[2 * i + 1] = i + n // 2
pos = [out_shuffle.index(i) for i in range(n)]       # where card i goes
cs = cycles(pos)
print("cycles:", cs, " order:", lcm(*map(len, cs)))

deck = list(range(n))
for k in range(1, 4):
    deck = [deck[j] for j in out_shuffle]
    print(f"after {k} shuffles:", deck)
cycles: [[0], [1, 2, 4], [3, 6, 5], [7]]  order: 3
after 1 shuffles: [0, 4, 1, 5, 2, 6, 3, 7]
after 2 shuffles: [0, 2, 4, 6, 1, 3, 5, 7]
after 3 shuffles: [0, 1, 2, 3, 4, 5, 6, 7]

Three perfect out-shuffles restore an 8-card deck. For 52 cards, 8 perfect out-shuffles do it, a classic magician's trick.

In software

Permutations measure brute-force cost: the travelling salesman problem has (n−1)!/2(n-1)!/2 different tours on nn cities, already 1.8×10161.8 \times 10^{16} for 20 cities, which is why exact algorithms use dynamic programming or branch and bound. random.shuffle implements the Fisher–Yates shuffle, which picks each of the n!n! orders with equal probability by choosing step ii uniformly from the n−in - i remaining positions, exactly the generalized product rule.

Exercises

Exercise 1: Seating

In how many ways can 6 people sit in a row of 6 chairs? In a row of 10 chairs (4 stay empty)?

Answer

6!=7206! = 720; P(10,6)=151200P(10, 6) = 151200.

Solution

In the second case, assign chairs to people in turn: the first person has 10 chairs, the next 9, and so on: 10⋅9⋅8⋅7⋅6⋅5=15120010 \cdot 9 \cdot 8 \cdot 7 \cdot 6 \cdot 5 = 151200.

Exercise 2: No two adjacent

How many orderings of the letters A, B, C, D, E have A and B not next to each other?

Answer
Solution

Total orderings 5!=1205! = 120. Orderings with A and B adjacent: glue them into one block (4 objects: 4!=244! = 24 orders) times 2 orders inside the block, giving 48. Complement: 120−48=72120 - 48 = 72.

python · try it yourself
from itertools import permutations

print(sum(1 for p in permutations("ABCDE") if abs(p.index("A") - p.index("B")) != 1))
72

Exercise 3: Order of a permutation (code)

The permutation π=(1,2,0,4,5,6,3)\pi = (1, 2, 0, 4, 5, 6, 3) (meaning 0→1→2→00 \to 1 \to 2 \to 0, and so on) is applied repeatedly. After how many applications does every element return?

Answer
Solution

Cycles (0 1 2)(0\,1\,2) and (3 4 5 6)(3\,4\,5\,6) have lengths 3 and 4; lcm⁡(3,4)=12\operatorname{lcm}(3, 4) = 12.

python · try it yourself
p = [1, 2, 0, 4, 5, 6, 3]
x, k = list(range(7)), 0
while True:
    x = [p[i] for i in x]; k += 1
    if x == list(range(7)):
        break
print(k)
12

Test yourself

How many ways can 5 different books be arranged on a shelf?

  1. 5
  2. 25
  3. 120
  4. 3125

What is P(10,2)P(10, 2)?

  1. 20
  2. 45
  3. 90
  4. 100

What is 0!0!?

  1. 0
  2. 1
  3. undefined
  4. infinity