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 steps and, whatever the earlier choices were, step always has exactly options, then there are sequences.
Medals. 8 runners, gold/silver/bronze: podiums. The set of options at step 2 depends on who won gold, but its size is always 7.
Permutations
A permutation of distinct objects is an ordering of all of them. There are
of them, with (one way to order nothing). An ordering of of the objects (a -permutation) can be done in
ways.
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
: every well-shuffled deck is almost certainly in an order that has never existed before.
Permutations as functions
A permutation of 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 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.
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 different tours on cities, already 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 orders with equal probability by choosing step uniformly from the 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
; .
Solution
In the second case, assign chairs to people in turn: the first person has 10 chairs, the next 9, and so on: .
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 . Orderings with A and B adjacent: glue them into one block (4 objects: orders) times 2 orders inside the block, giving 48. Complement: .
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 (meaning , and so on) is applied repeatedly. After how many applications does every element return?
Answer
Solution
Cycles and have lengths 3 and 4; .
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?
- 5
- 25
- 120
- 3125
What is ?
- 20
- 45
- 90
- 100
What is ?
- 0
- 1
- undefined
- infinity