## 5.371. sort

Origin
Constraint

$\mathrm{\pi \pi \pi \pi }\left(\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1},\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}\right)$

Synonyms

$\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi \pi }$, $\mathrm{\pi \pi \pi \pi \pi \pi }$, $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$.

Arguments
 $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}$ $\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi \pi }\left(\mathrm{\pi \pi \pi }-\mathrm{\pi \pi \pi \pi }\right)$ $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}$ $\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi \pi }\left(\mathrm{\pi \pi \pi }-\mathrm{\pi \pi \pi \pi }\right)$
Restrictions
 $|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}|=|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}|$ $\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi }$$\left(\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1},\mathrm{\pi \pi \pi }\right)$ $\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi }$$\left(\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2},\mathrm{\pi \pi \pi }\right)$
Purpose

First, the variables of the collection $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}$ correspond to a permutation of the variables of $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}$. Second, the variables of $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}$ are sorted in increasing order.

Example
$\left(β©1,9,1,5,2,1βͺ,β©1,1,1,2,5,9βͺ\right)$

The $\mathrm{\pi \pi \pi \pi }$ constraint holds since:

• Values 1, 2, 5 and 9 have the same number of occurrences within both collections $\beta ©1,9,1,5,2,1\beta ͺ$ and $\beta ©1,1,1,2,5,9\beta ͺ$. FigureΒ 5.371.1 illustrates this correspondence.

• The items of collection $\beta ©1,1,1,2,5,9\beta ͺ$ are sorted in increasing order.

All solutions

FigureΒ 5.371.2 gives all solutions to the following non ground instance of the $\mathrm{\pi \pi \pi \pi }$ constraint: ${V}_{1}\beta \left[2,3\right]$, ${V}_{2}\beta \left[2,3\right]$, ${V}_{3}\beta \left[1,2\right]$, ${V}_{4}\beta \left[4,5\right]$, ${V}_{5}\beta \left[2,4\right]$, ${S}_{1}\beta \left[2,3\right]$, ${S}_{2}\beta \left[2,3\right]$, ${S}_{3}\beta \left[1,3\right]$, ${S}_{4}\beta \left[4,5\right]$, ${S}_{5}\beta \left[2,5\right]$, $\mathrm{\pi \pi \pi \pi }$$\left(\beta ©{V}_{1},{V}_{2},{V}_{3},{V}_{4},{V}_{5}\beta ͺ,\beta ©{S}_{1},{S}_{2},{S}_{3},{S}_{4},{S}_{5}\beta ͺ\right)$.

Typical
 $|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}|>1$ $\mathrm{\pi \pi \pi \pi \pi }$$\left(\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}.\mathrm{\pi \pi \pi }\right)>1$
Symmetries
• Items of $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}$ are permutable.

• One and the same constant can be added to the $\mathrm{\pi \pi \pi }$ attributes of all items of $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}$ and $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}$.

Arg. properties

Functional dependency: $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}$ determined by $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}$.

Usage

The main usage of the $\mathrm{\pi \pi \pi \pi }$ constraint, that was not foreseen when the $\mathrm{\pi \pi \pi \pi }$ constraint was invented, is its use in many reformulations. Many constraints involving one or several collections of variables become much simpler to express when the variables of these collections are sorted. In addition these reformulations typically have a size that is linear in the number of variables of the original constraint. This justifies why the $\mathrm{\pi \pi \pi \pi }$ constraint is considered to be a core constraint. As illustrative examples of these types of reformulations we successively consider the $\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi \pi \pi \pi }$ and the $\mathrm{\pi \pi \pi \pi }$ constraints:

Remark

A variant of this constraint called $\mathrm{\pi \pi \pi \pi }_\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi \pi \pi }$ was introduced inΒ [Zhou97]. In this variant an additional list of domain variables represents the permutation that allows to go from $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}$ to $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}$.

Algorithm
Systems

generalisation: $\mathrm{\pi \pi \pi \pi }_\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi \pi \pi }$Β ($\mathrm{\pi Ώ\pi ΄\pi \pi Ό\pi \pi \pi °\pi \pi Έ\pi Ύ\pi ½}$ parameter added).

Keywords
Arc input(s)

$\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}$ $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}$

Arc generator
$\mathrm{\pi \pi  \pi \pi ·\pi \pi Ά\pi }$$\beta ¦\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi \pi }\left(\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi }\mathtt{1},\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi }\mathtt{2}\right)$

Arc arity
Arc constraint(s)
$\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi }\mathtt{1}.\mathrm{\pi \pi \pi }=\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi }\mathtt{2}.\mathrm{\pi \pi \pi }$
Graph property(ies)
 $\beta ’\text{for}\text{all}\text{connected}\text{components:}$$\mathrm{\pi \pi \pi \pi \pi \pi \pi }$$=$$\mathrm{\pi \pi \pi \pi \pi }$ $\beta ’$$\mathrm{\pi \pi \pi \pi \pi \pi \pi }$$=|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}|$ $\beta ’$$\mathrm{\pi \pi \pi \pi \pi }$$=|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}|$

Arc input(s)

$\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}$

Arc generator
$\mathrm{\pi \pi ΄\pi \pi »}$$\beta ¦\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi \pi }\left(\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi }\mathtt{1},\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi }\mathtt{2}\right)$

Arc arity
Arc constraint(s)
$\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi }\mathtt{1}.\mathrm{\pi \pi \pi }\beta €\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi }\mathtt{2}.\mathrm{\pi \pi \pi }$
Graph property(ies)
$\mathrm{\pi \pi \pi \pi }$$=|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}|-1$

Graph model

PartsΒ (A) andΒ (B) of FigureΒ 5.371.3 respectively show the initial and final graph associated with the first graph constraint of the Example slot. Since it uses the $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ and $\mathrm{\pi \pi \pi \pi \pi }$ graph properties, the source and sink vertices of this final graph are stressed with a double circle. Since there is a constraint on each connected component of the final graph we also show the different connected components. The $\mathrm{\pi \pi \pi \pi }$ constraint holds since:

• Each connected component of the final graph of the first graph constraint has the same number of sources and of sinks.

• The number of sources of the final graph of the first graph constraint is equal to $|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}|$.

• The number of sinks of the final graph of the first graph constraint is equal to $|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}|$.

• Finally the second graph constraint holds also since its corresponding final graph contains exactly $|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}-1|$ arcs: all the inequalities constraints between consecutive variables of $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}$ holds.

Signature

Consider the first graph constraint. Since the initial graph contains only sources and sinks, and since isolated vertices are eliminated from the final graph, we make the following observations:

• Sources of the initial graph cannot become sinks of the final graph,

• Sinks of the initial graph cannot become sources of the final graph.

From the previous observations and since we use the $\mathrm{\pi \pi  \pi \pi ·\pi \pi Ά\pi }$ arc generator on the collections $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}$ and $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}$, we have that the maximum number of sources and sinks of the final graph is respectively equal to $|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}|$ and $|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}|$. Therefore we can rewrite $\mathrm{\pi \pi \pi \pi \pi \pi \pi }=|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}|$ to $\mathrm{\pi \pi \pi \pi \pi \pi \pi }\beta ₯|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{1}|$ and simplify $\underset{Μ²}{\stackrel{Β―}{\mathrm{\pi \pi \pi \pi \pi \pi \pi }}}$ to $\stackrel{Β―}{\mathrm{\pi \pi \pi \pi \pi \pi \pi }}$. In a similar way, we can rewrite $\mathrm{\pi \pi \pi \pi \pi }=|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}|$ to $\mathrm{\pi \pi \pi \pi \pi }\beta ₯|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}|$ and simplify $\underset{Μ²}{\stackrel{Β―}{\mathrm{\pi \pi \pi \pi \pi }}}$ to $\stackrel{Β―}{\mathrm{\pi \pi \pi \pi \pi }}$.

Consider now the second graph constraint. Since we use the $\mathrm{\pi \pi ΄\pi \pi »}$ arc generator with an arity of 2 on the $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}$ collection, the maximum number of arcs of the final graph is equal to $|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}|-1$. Therefore we can rewrite the graph property $\mathrm{\pi \pi \pi \pi }=|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}|-1$ to $\mathrm{\pi \pi \pi \pi }\beta ₯|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\mathtt{2}|-1$ and simplify $\underset{Μ²}{\stackrel{Β―}{\mathrm{\pi \pi \pi \pi }}}$ to $\stackrel{Β―}{\mathrm{\pi \pi \pi \pi }}$.

Quiz

Β Β

$\mathrm{\pi \pi \pi \pi }$: checking whether a ground instance holds or not Β

$\mathrm{\pi \pi \pi \pi }$: finding all solutions

Β