 Iterator/Combination - Help

Iterator

 Combination
 generate t-combinations of a set

 Calling Sequence Combination(n, t, opts)

Parameters

 n - posint; size of set t - nonnegint; size of combinations opts - (optional) equation(s) of the form option = value; specify options for the Combination command

Options

 • compile = truefalse
 True means compile the iterator. The default is true.
 • rank = nonnegint
 Specify the starting rank of the iterator. The default is one. The starting rank reverts to one when the iterator is reset, reused, or copied.

Description

 • The Combination command returns an iterator that generates all t-combinations of the integers $0..n-1$.
 • The combinations, when read from right to left, appear in lexicographic order.
 • The n parameter is the size of the set.
 • The t parameter is the number of elements in each combination; it must be less than or equal to n.

Methods

The iterator object has the following methods. The self parameter is the iterator object.

 • Number(self): return the number of iterations required to step through the iterator, assuming it started at rank one.
 • Rank(self,L): return the rank of the current iteration. Optionally pass L, a list or one-dimensional rtable, and return its rank.
 • Unrank(self,rnk): return a one-dimensional Array corresponding to the iterator output with rank rnk.

Examples

 > $\mathrm{with}\left(\mathrm{Iterator}\right):$

Construct an iterator that returns all 3-combinations of the integers from 0 to 4.

 > $n,t≔5,3:$
 > $M≔\mathrm{Combination}\left(n,t\right):$
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}m\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}M\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{printf}\left("%d\n",m\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}$
 0 1 2 0 1 3 0 2 3 1 2 3 0 1 4 0 2 4 1 2 4 0 3 4 1 3 4 2 3 4

Compute the number of iterations.

 > $\mathrm{Number}\left(M\right)$
 ${10}$ (1)

Return the element with rank equal to 4.

 > $\mathrm{Unrank}\left(M,4\right)$
 $\left[\begin{array}{ccc}{1}& {2}& {3}\end{array}\right]$ (2)

 > $N≔\mathrm{Object}\left(M,\mathrm{rank}=4\right):$
 > $\mathrm{seq}\left(v\left[\right],v=N\right)$
 $\left[\begin{array}{ccc}{1}& {2}& {3}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {1}& {4}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {2}& {4}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {2}& {4}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {3}& {4}\end{array}\right]{,}\left[\begin{array}{ccc}{1}& {3}& {4}\end{array}\right]{,}\left[\begin{array}{ccc}{2}& {3}& {4}\end{array}\right]$ (3)

References

 Knuth, Donald Ervin. The Art of Computer Programming, volume 4, fascicle 3; generating all combinations and partitions, sec. 7.2.1.3, algorithm T (Lexicographic combinations), p. 5.

Compatibility

 • The Iterator[Combination] command was introduced in Maple 2016.