TExcelTAD
Home › Discrete Mathematics › Sequences and Cartesian Products

Sequences and Cartesian Products

Sets ignore order and repetition. Often we need both: a password is a sequence of characters, a point is an ordered pair of coordinates, a database row is a tuple of field values. This lesson introduces sequences, tuples and the Cartesian product, the set of all tuples.

Sequences and tuples

A sequence (or tuple) lists elements in order and may repeat them. Write it with parentheses: (a,b,a)(a, b, a). Two sequences are equal when they have the same length and the same element at each position, so (1,2)≠(2,1)(1, 2) \ne (2, 1) although {1,2}={2,1}\{1, 2\} = \{2, 1\}.

Set Sequence
notation {a,b}\{a, b\} (a,b)(a, b)
order matters no yes
repetition ignored counts
Python set, frozenset tuple, list, str

A sequence of length 2 is an ordered pair, of length 3 a triple, of length nn an nn-tuple. The empty sequence is λ\lambda (or the empty string "" in code).

The Cartesian product

The Cartesian product of sets AA and BB is the set of all ordered pairs with first element from AA and second from BB:

A×B={(a,b)∣a∈A, b∈B}A \times B = \{(a, b) \mid a \in A,\ b \in B\}

More generally A1×⋯×AnA_1 \times \cdots \times A_n is the set of nn-tuples, and An=A×⋯×AA^n = A \times \cdots \times A (nn times). Sizes multiply:

∣A×B∣=∣A∣⋅∣B∣,∣An∣=∣A∣n\lvert A \times B \rvert = \lvert A \rvert \cdot \lvert B \rvert, \qquad \lvert A^n \rvert = \lvert A \rvert^n
python · try it yourself
from itertools import product

ranks = ["A", "K", "Q"]
suits = ["S", "H"]
cards = list(product(ranks, suits))
print(len(cards), cards)

bits3 = ["".join(t) for t in product("01", repeat=3)]
print("{0,1}^3:", bits3, len(bits3))
6 [('A', 'S'), ('A', 'H'), ('K', 'S'), ('K', 'H'), ('Q', 'S'), ('Q', 'H')]
{0,1}^3: ['000', '001', '010', '011', '100', '101', '110', '111'] 8

The plane R2=R×R\mathbb{R}^2 = \mathbb{R} \times \mathbb{R} is a Cartesian product, which is where the name comes from (René Descartes). Note that A×B≠B×AA \times B \ne B \times A in general, since (a,b)≠(b,a)(a, b) \ne (b, a).

Strings are sequences

A string over an alphabet Σ\Sigma is a finite sequence of symbols from Σ\Sigma. Σn\Sigma^n is the set of strings of length nn, and Σ∗\Sigma^* is the set of all finite strings. For a bit alphabet Σ={0,1}\Sigma = \{0, 1\}, ∣Σn∣=2n\lvert \Sigma^n \rvert = 2^n. This is the basic counting fact behind password strength and brute-force attack costs:

python · try it yourself
import math

for name, size in [("digits", 10), ("lowercase", 26), ("letters+digits", 62), ("printable ASCII", 95)]:
    for length in (8, 12):
        count = size ** length
        print(f"{name:16s} length {length:2d}: {count:.2e} strings = {math.log2(count):5.1f} bits")
digits           length  8: 1.00e+08 strings =  26.6 bits
digits           length 12: 1.00e+12 strings =  39.9 bits
lowercase        length  8: 2.09e+11 strings =  37.6 bits
lowercase        length 12: 9.54e+16 strings =  56.4 bits
letters+digits   length  8: 2.18e+14 strings =  47.6 bits
letters+digits   length 12: 3.23e+21 strings =  71.5 bits
printable ASCII  length  8: 6.63e+15 strings =  52.6 bits
printable ASCII  length 12: 5.40e+23 strings =  78.8 bits

Twelve characters from the 95 printable ASCII symbols give about 79 bits of entropy (if chosen uniformly at random), far beyond brute force.

In software

Tuples are records: a row of a relational table with columns of types T1,…,TnT_1, \ldots, T_n is an element of T1×⋯×TnT_1 \times \cdots \times T_n. A SQL CROSS JOIN computes a Cartesian product, which is why forgetting the join condition makes a query return ∣A∣⋅∣B∣\lvert A \rvert \cdot \lvert B \rvert rows. Nested loops for a in A: for b in B: visit A×BA \times B, and their running time is ∣A∣⋅∣B∣\lvert A \rvert \cdot \lvert B \rvert iterations.

Exercises

Exercise 1: List a product

List {1,2}×{x,y,z}\{1, 2\} \times \{x, y, z\} and give its size.

Answer

{(1,x),(1,y),(1,z),(2,x),(2,y),(2,z)}\{(1, x), (1, y), (1, z), (2, x), (2, y), (2, z)\}; size 6.

Solution

Each of the 2 first elements pairs with each of the 3 second elements, giving 2⋅3=62 \cdot 3 = 6 pairs.

Exercise 2: Sets versus tuples

Which are true? (a) {1,2,2}={2,1}\{1, 2, 2\} = \{2, 1\}; (b) (1,2,2)=(2,1)(1, 2, 2) = (2, 1); (c) (1,2)∈{1,2}×{1,2}(1, 2) \in \{1, 2\} \times \{1, 2\}; (d) ∣∅×A∣=0\lvert \emptyset \times A \rvert = 0.

Answer

(a), (c) and (d) are true; (b) is false.

Solution

(a) both sets are {1,2}\{1, 2\}. (b) the tuples have different lengths. (c) 1∈{1,2}1 \in \{1, 2\} and 2∈{1,2}2 \in \{1, 2\}. (d) there is no first element to choose, so the product is empty: 0⋅∣A∣=00 \cdot \lvert A \rvert = 0.

Exercise 3: License plates (code)

A plate has 3 uppercase letters followed by 4 digits. How many plates are there? How many if the letters must be different?

Answer

263⋅104=175,760,00026^3 \cdot 10^4 = 175{,}760{,}000; with distinct letters 26⋅25⋅24⋅104=156,000,00026 \cdot 25 \cdot 24 \cdot 10^4 = 156{,}000{,}000.

Solution

A plate is an element of L3×D4L^3 \times D^4, and sizes multiply. With distinct letters, the second letter has 25 choices and the third 24.

python · try it yourself
print(26 ** 3 * 10 ** 4)
print(26 * 25 * 24 * 10 ** 4)
175760000
156000000

Test yourself

If ∣A∣=4\lvert A \rvert = 4 and ∣B∣=5\lvert B \rvert = 5, what is ∣A×B∣\lvert A \times B \rvert?

  1. 9
  2. 20
  3. 454^5
  4. 545^4

How many binary strings of length 10 are there?

  1. 10
  2. 20
  3. 100
  4. 1024

Which SQL operation returns a Cartesian product?

  1. UNION
  2. CROSS JOIN
  3. INTERSECT
  4. GROUP BY