article

In mathematics, the lexicographical order, (a.k.a. dictionary order or alphabetic order), 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 bb′.
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'.

Case of multiple products


Suppose

\{ A_1, A_2, \dots, A_n \} is a collection of sets, with respective to total orderings
\{ <_1, <_2, \cdots, <_n \}

The dictionary ordering

\ \ <^d of
A_1 \times A_2 \times \cdots \times A_n is then
(a_1, a_2, \dots, a_n) <^d (b_1,b_2, \dots, b_n) \iff (\exists\ m > 0) \ (\forall\ i < m) (a_i = b_i) \land (a_m <_m b_m)

That is, if one of the terms

\ \ a_m <_m b_m and all the preceding terms are equal.

Informally,

\ \ a_1 represents the first letter,
\ \ a_2 the second and so on when looking up a word in a dictionary, hence the name.

This could be more elegantly defined recursively by defining the ordering of any set

\ \ C= A_j \times A_{j+1} \times \cdots \times A_k

represented by

\ \ <^d (C)

This will satisfy

a <^d (A_i) a' \iff (a <_i a')

(a,b) <^d (A_i \times B) (a',b') \iff a <^d (A) a' \lor ( a=a' \ \land \ b <^d (B) b')

where B = A_{i+1} \times A_{i+2} \times \cdots \times A_n.

Or, more simply put, compare the first terms if they are equal compare the second — and so on.

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.

C/C++ function for case insensitive string comparison


bool lexComp(std::string str_1, std::string str_2 ){
  
  /* In ASCII, codes of Upper and Lower case letters have always a 
     difference of 32 places as they spread sequentialy, 65-90 for upper 
     case letters and 97-122 for lower case ones.
     E.g. 'A' has code 65, 'a' has code 97 and 97-65=32. So we subtract 32
     to turn from lower to upper case.     
  */  
  //turn the first string to upper case 
  for(int i=0; i&& str_1[i<123)
      str_1*-=32; 

//turn the second string to upper case for(int i=0; i&& str_2[i<123) str_2*-=32; //check which string has the smallest length, and keep it //we use it as an "exit condition" in the loop below int smallest_len; (str_1.size()**) return true; if(str_1*>str_2*) return false; } //if the two strings were found equal inside "for" decide the precedence //by checking their size if(str_1.size()

See also


Order theory

Lexikographische Ordnung | Orden lexicográfico | Ordine lessicografico | Porządek leksykograficzny | Lexikografisk ordning

 

This article is licensed under the GNU Free Documentation License. It uses material from the "Lexicographical order".

Home Pageartsbusinesscomputersgameshealthhospitalshomekids & teensnewsphysiciansrecreationreferenceregionalscienceshoppingsocietysportsworld