5.371. sort

DESCRIPTIONLINKSGRAPH
Origin

[OlderSwinkelsEmden95]

Constraint

πšœπš˜πš›πš(πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1,πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2)

Synonyms

πšœπš˜πš›πšπšŽπšπš—πšŽπšœπšœ, πšœπš˜πš›πšπšŽπš, πšœπš˜πš›πšπš’πš—πš.

Arguments
πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1πšŒπš˜πš•πš•πšŽπšŒπšπš’πš˜πš—(πšŸπšŠπš›-πšπšŸπšŠπš›)
πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2πšŒπš˜πš•πš•πšŽπšŒπšπš’πš˜πš—(πšŸπšŠπš›-πšπšŸπšŠπš›)
Restrictions
|πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1|=|πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2|
πš›πšŽπššπšžπš’πš›πšŽπš(πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1,πšŸπšŠπš›)
πš›πšŽπššπšžπš’πš›πšŽπš(πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2,πšŸπšŠπš›)
Purpose

First, the variables of the collection πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2 correspond to a permutation of the variables of πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1. Second, the variables of πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2 are sorted in increasing order.

Example
(1,9,1,5,2,1,1,1,1,2,5,9)

The πšœπš˜πš›πš constraint holds since:

  • Values 1, 2, 5 and 9 have the same number of occurrences within both collections 〈1,9,1,5,2,1βŒͺ and 〈1,1,1,2,5,9βŒͺ. FigureΒ 5.371.1 illustrates this correspondence.

  • The items of collection 〈1,1,1,2,5,9βŒͺ are sorted in increasing order.

Figure 5.371.1. Illustration of the correspondence between the items of the πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1 and of the πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2 collections of the Example slot (note that the items of the πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2 are sorted in increasing order)
ctrs/sort-1-tikz
All solutions

FigureΒ 5.371.2 gives all solutions to the following non ground instance of the πšœπš˜πš›πš constraint: V 1 ∈[2,3], V 2 ∈[2,3], V 3 ∈[1,2], V 4 ∈[4,5], V 5 ∈[2,4], S 1 ∈[2,3], S 2 ∈[2,3], S 3 ∈[1,3], S 4 ∈[4,5], S 5 ∈[2,5], πšœπš˜πš›πš(〈V 1 ,V 2 ,V 3 ,V 4 ,V 5 βŒͺ,〈S 1 ,S 2 ,S 3 ,S 4 ,S 5 βŒͺ).

Figure 5.371.2. All solutions corresponding to the non ground example of the πšœπš˜πš›πš constraint of the All solutions slot
ctrs/sort-2-tikz
Typical
|πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1|>1
πš›πšŠπš—πšπšŽ(πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1.πšŸπšŠπš›)>1
Symmetries
  • Items of πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1 are permutable.

  • One and the same constant can be added to the πšŸπšŠπš› attributes of all items of πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1 and πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2.

Arg. properties

Functional dependency: πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2 determined by πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1.

Usage

The main usage of the πšœπš˜πš›πš constraint, that was not foreseen when the πšœπš˜πš›πš 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 πšœπš˜πš›πš constraint is considered to be a core constraint. As illustrative examples of these types of reformulations we successively consider the πšŠπš•πš•πšπš’πšπšπšŽπš›πšŽπš—πš and the πšœπšŠπš–πšŽ constraints:

Remark

A variant of this constraint called πšœπš˜πš›πš_πš™πšŽπš›πš–πšžπšπšŠπšπš’πš˜πš— was introduced inΒ [Zhou97]. In this variant an additional list of domain variables represents the permutation that allows to go from πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1 to πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2.

Algorithm

[GuernalecColmerauer97], [MehlhornThiel00].

Systems

sorting in Choco, sorted in Gecode, sort in MiniZinc, sorting in SICStus.

See also

generalisation: πšœπš˜πš›πš_πš™πšŽπš›πš–πšžπšπšŠπšπš’πš˜πš—Β (π™Ώπ™΄πšπ™Όπš„πšƒπ™°πšƒπ™Έπ™Ύπ™½ parameter added).

implies: πš•πšŽπš‘_πšπš›πšŽπšŠπšπšŽπš›πšŽπšš, πšœπšŠπš–πšŽ.

uses in its reformulation: πšŠπš•πš•πšπš’πšπšπšŽπš›πšŽπš—πš, πšœπšŠπš–πšŽ.

Keywords

characteristic of a constraint: core, sort.

combinatorial object: permutation.

constraint arguments: constraint between two collections of variables, pure functional dependency.

filtering: bound-consistency.

modelling: functional dependency.

Arc input(s)

πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1 πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2

Arc generator
π‘ƒπ‘…π‘‚π·π‘ˆπΆπ‘‡β†¦πšŒπš˜πš•πš•πšŽπšŒπšπš’πš˜πš—(πšŸπšŠπš›πš’πšŠπš‹πš•πšŽπšœ1,πšŸπšŠπš›πš’πšŠπš‹πš•πšŽπšœ2)

Arc arity
Arc constraint(s)
πšŸπšŠπš›πš’πšŠπš‹πš•πšŽπšœ1.πšŸπšŠπš›=πšŸπšŠπš›πš’πšŠπš‹πš•πšŽπšœ2.πšŸπšŠπš›
Graph property(ies)
β€’ for all connected components: ππ’πŽπ”π‘π‚π„=ππ’πˆππŠ
β€’ ππ’πŽπ”π‘π‚π„=|πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1|
β€’ ππ’πˆππŠ=|πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2|

Arc input(s)

πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2

Arc generator
π‘ƒπ΄π‘‡π»β†¦πšŒπš˜πš•πš•πšŽπšŒπšπš’πš˜πš—(πšŸπšŠπš›πš’πšŠπš‹πš•πšŽπšœ1,πšŸπšŠπš›πš’πšŠπš‹πš•πšŽπšœ2)

Arc arity
Arc constraint(s)
πšŸπšŠπš›πš’πšŠπš‹πš•πšŽπšœ1.πšŸπšŠπš›β‰€πšŸπšŠπš›πš’πšŠπš‹πš•πšŽπšœ2.πšŸπšŠπš›
Graph property(ies)
𝐍𝐀𝐑𝐂=|πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚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 ππ’πŽπ”π‘π‚π„ and ππ’πˆππŠ 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 πšœπš˜πš›πš 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 |πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1|.

  • The number of sinks of the final graph of the first graph constraint is equal to |πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2|.

  • Finally the second graph constraint holds also since its corresponding final graph contains exactly |πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1-1| arcs: all the inequalities constraints between consecutive variables of πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2 holds.

Figure 5.371.3. Initial and final graph of the πšœπš˜πš›πš constraint
ctrs/sortA
(a)
ctrs/sortB
(b)
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 π‘ƒπ‘…π‘‚π·π‘ˆπΆπ‘‡ arc generator on the collections πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1 and πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2, we have that the maximum number of sources and sinks of the final graph is respectively equal to |πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1| and |πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2|. Therefore we can rewrite ππ’πŽπ”π‘π‚π„=|πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1| to ππ’πŽπ”π‘π‚π„β‰₯|πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚1| and simplify ππ’πŽπ”π‘π‚π„ Β― Μ² to ππ’πŽπ”π‘π‚π„ Β―. In a similar way, we can rewrite ππ’πˆππŠ=|πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2| to ππ’πˆππŠβ‰₯|πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2| and simplify ππ’πˆππŠ Β― Μ² to ππ’πˆππŠ Β―.

Consider now the second graph constraint. Since we use the 𝑃𝐴𝑇𝐻 arc generator with an arity of 2 on the πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2 collection, the maximum number of arcs of the final graph is equal to |πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2|-1. Therefore we can rewrite the graph property 𝐍𝐀𝐑𝐂=|πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2|-1 to 𝐍𝐀𝐑𝐂β‰₯|πš…π™°πšπ™Έπ™°π™±π™»π™΄πš‚2|-1 and simplify 𝐍𝐀𝐑𝐂 Β― Μ² to 𝐍𝐀𝐑𝐂 Β―.

Quiz

Β Β 

πšœπš˜πš›πš: checking whether a ground instance holds or not ctrs/sort-3-tikz Β 

πšœπš˜πš›πš: finding all solutions ctrs/sort-4-tikz

ctrs/sort-5-tikz Β 

ctrs/sort-6-tikz