Function Repository Resource:

EquivalentOrderings

Source Notebook

Generalize Ordering by giving alternative orderings if there are equal items

Contributed by: Andrew Read

ResourceFunction["EquivalentOrderings"][list]

return a list of all possible orderings of list considering the permutations of equal items.

ResourceFunction["EquivalentOrderings"][list,n]

return a list of all possible orderings of list considering the permutations of equal items, and limit each ordering to n items.

Details and Options

All elements in list must accept the default comparison operator

Examples

Basic Examples (2) 

Find the two ways of ordering this list:

In[1]:=
ResourceFunction["EquivalentOrderings"][{0.5, 0.3, 0.1, 0.5}]
Out[1]=

If the orderings are limited to the first two elements, then there is only one way of ordering the list:

In[2]:=
ResourceFunction["EquivalentOrderings"][{0.5, 0.3, 0.1, 0.5}, 2]
Out[2]=

EquivalentOrderings will work on the same non-numeric values as Ordering:

In[3]:=
ResourceFunction["EquivalentOrderings"][{c, a, a, b}]
Out[3]=

Compare with Ordering:

In[4]:=
Ordering[{c, a, a, b}]
Out[4]=

Scope (1) 

Finding all possible orderings of a list of probabilities is helpful for considering alternative Huffman codes:

In[5]:=
ResourceFunction["EquivalentOrderings"][{0.35, 0.35, 0.1, 0.1, 0.1}]
Out[5]=

Publisher

Andrew Read

Version History

  • 1.0.0 – 13 April 2020

Author Notes

Uncomment the Print statements to watch the algorithm work!

License Information