# Lexicographic Orders

The lexicographic order is an order on the Cartesian product of two or more partially ordered sets. The lexicographic order is also known as lexicographical order, alphabetical order, or dictionary order.

## Order Involving Two Sets

Let \({\left( {{A},{\preccurlyeq}_1} \right)}\) and \(\left( {{B},{\preccurlyeq}_2} \right)\) be two partially ordered sets. The lexicographic order \({\preccurlyeq}\) on the Cartesian product \({{A} \times {B}}\) is defined by

if and only if

## Order Involving \(N\) Sets

The above definition can be generalized to the Cartesian product of \(n\) posets. Given partially ordered sets \(\left( {{A_1},{\preccurlyeq}_1} \right),\) \(\left( {{A_2},{\preccurlyeq}_2} \right),\ldots\) \(\left( {{A_n},{\preccurlyeq}_n} \right),\) we define the lexicographic order on the Cartesian product \({{A_1} \times {A_2} \times \cdots \times {A_n}}\) as

where

where

It can be shown that if the sets \({{A_1}, {A_2},\ldots {A_n}}\) are partially ordered, then the lexicographic order is also a partial order. Similarly, the lexicographic order is a total order (well order), if all these sets are totally ordered (well ordered).

## Ordering of Words

Suppose now the set \(A\) represents a certain alphabet. The letters \({a_1},{a_2},\ldots,{a_k}\) of the alphabet \(A\) are assumed to be strictly totally ordered:

The elements of the \(n\text{th}\) Cartesian power \(A^n\) are treated as words or strings of length \(n.\) The order of two words depends on the order of the symbols in the first place where the two words differ (counting from the beginning of the words).

If two words have different lengths, the shorter word is padded with trailing blank characters to have the same length as the longer word. The blank symbol is supposed to come before any other symbol in the alphabet. Then the words can be compared lexicographically.

So, using the basic Latin alphabet, we can sort city names in the following order:

There is also another convention called shortlex order which is frequently used for comparison of words of different lengths. Under the shortlex word order, a shorter word or string always occurs earlier than a longer word. The shortlex ordering of the city list above looks as follows:

## Ordering of Numeric Tuples

Let \(A = \mathbb{N}.\) The set \(\mathbb{N}^n\) represents \(n-\)tuples of natural numbers - for example, \(\left( {3,1,7} \right)\) for \(n = 3,\) or \(\left( {5,2,4,11,15} \right)\) for \(n = 5.\)

The lexicographic order allows to compare \(n-\)tuples of natural numbers. With the usual "less than" order \(\lt\) on the set of natural numbers \(\mathbb{N},\) we define that

if there is \(i \le n\) such that \({a_j} = {b_j}\) for all \(j \lt i\) and \({a_i} \le {b_i}.\)

#### Examples

Similarly we can define lexicographic orders on sets of other numerical objects such as \(\mathbb{Z}^n\) of \(n-\)tuples of integers or \(\mathbb{R}^n\) of \(n-\)tuples of real numbers.

## Ordering of Subsets

We can use lexicographic order to sort any objects if they are represented by lists with a partial ordering. Suppose we want to order subsets of a set \(A\) consisting of \(n\) elements. These might be, say, all subsets of the power set of \(A\) or subsets of size \(k\) (\(k-\)subsets).

Recall that the order of elements within a subset does not matter. Therefore, first we should arrange them in increasing order based on the given or implied ordering rule. So, the numbers can be arranged with the standard "less than" ordering, and the letters can be sorted in alphabetical order. Then we can apply lexicographic ordering to the subsets.

For example, the subsets of \(A = \left\{ {a,b,c,d} \right\}\) are represented in lexicographic order as follows: