Lexicographical order
From Wikipedia, the free encyclopedia
In mathematics, the lexicographic or lexicographical order, (a.k.a. dictionary order, alphabetic order or lexicographic(al) product), is a natural order structure of the cartesian product of two ordered sets. Given A and B, two ordered sets, the lexicographical order in the cartesian product A × B is defined as
- (a,b) ≤ (a′,b′) if and only if a < a′, or a = a′ and b ≤ b′.
The name comes from its generalizing the order given to words in a dictionary: a sequence of letters (i.e. a word)
- a1a2 ... ak
appears in a dictionary before a sequence
- b1b2 ... bk
if and only if the first ai which is different from bi comes before bi in the alphabet. That assumes both have the same length; what is usually done is to pad out the shorter word for symbols for 'blanks', and to consider that a blank is a new minimum ('bottom') element.
For the purpose of dictionaries, etc., one may assume that all words have the same length, by adding blank spaces at the end, and considering the blank space as a special character which comes before any other letter in the alphabet. This also allows ordering of phrases. See alphabetical order.
An important property of the lexicographical order is that it preserves well-orders, that is, if A and B are well-ordered sets, then the product set A × B with the lexicographical order is also well-ordered.
An important exploitation of lexicographical ordering is expressed in the ISO 8601 date formatting scheme, which expresses a date as YYYY-MM-DD. This date ordering lends itself to straightforward computerized sorting of dates such that the sorting algorithm does not need to treat the numeric parts of the date string any differently from a string of non-numeric characters, and the dates will be sorted into chronological order. Note, however, that for this to work, there must always be four digits for the year, two for the month, and two for the day, so for example single-digit days must be padded with a zero yielding '01', '02', ... , '09'.
Contents |
[edit] Case of multiple products
Suppose
is a collection of sets, with respective to total orderings
The dictionary ordering
of
is then
That is, if one of the terms
and all the preceding terms are equal.
Informally,
represents the first letter,
the second and so on when looking up a word in a dictionary, hence the name.
This could be more elegantly stated by recursively defining the ordering of any set
represented by
This will satisfy
where
To put it even more simply, compare the first terms. If they are equal, compare the second terms — and so on. The relationship between the first corresponding terms that are not equal determines the relationship between the entire elements.
[edit] Ordering of strings
If Σ is a finite and totally ordered alphabet, the above considerations allow to define naturally a lexicographical order < d over the free monoid Σ * (i.e., the set of all words over Σ), as follows:
- u < dv if
- u is a prefix of v, or
- u = wau' and v = wbv', where w is the longest common prefix of u and v, a and b are letters of Σ such that a < b, and u',v' are words.
However, in general this is not a well-order; for instance, if Σ = {a,b}, the language has no least element. A well-order for strings, based on the lexicographical order, is the shortlex order.
[edit] Monomials
In algebra it is traditional to order terms in a polynomial, by ordering the monomials in the indeterminates. This is fundamental, in order to have a normal form. Such matters are typically left implicit in discussion between humans, but must of course be dealt with exactly in computer algebra. In practice one has an alphabet of indeterminates X, Y, ... and orders all monomials formed from them by a variant of lexicographical order. For example if one decides to order the alphabet by
- X > Y > ...
and also to look at higher terms first, that means ordering
- ... > X3 > X2 > X
and also
- X > Yk for all k.
There is some flexibility in ordering monomials, and this can be exploited in Gröbner basis theory.
[edit] Variations
[edit] Reverse lexicographic order
In a common variation of lexicographic order, one compares elements by reading from the right instead of from the left. For example, in reverse lexicographic order on monomials in three variables,
- xy3z2 < yz2.
This is not simply the order opposite to lexicographic. For example, xyz < xy2 in both lexicographic and reverse lexicographic orders. A list of words so arranged is called a reverse lexicon, often confused with a rhyming dictionary.