Top Description Inners Fields Constructors Methods
org.apache.commons.math3.util

public Class Combinations

extends Object
implements Iterable<int[]>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable
Imports
java.util.Iterator, .Comparator, .Arrays, .NoSuchElementException, java.io.Serializable, org.apache.commons.math3.exception.MathInternalError, .DimensionMismatchException, .OutOfRangeException

Utility to create combinations (n, k) of k elements in a set of n elements.
Since
3.3

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum
Combinations.IterationOrder

Describes the type of iteration performed by the iterator.

private static class
Combinations.LexicographicComparator

Defines the lexicographic ordering of combinations, using the lexNorm(int[]) method.

private static class
Combinations.LexicographicIterator

Lexicographic combinations iterator.

private static class
Combinations.SingletonIterator

Iterator with just one element to handle degenerate cases (full array, empty array) for combination iterator.

Field Summary

Modifier and TypeField and Description
private final Combinations.IterationOrder
iterationOrder

Iteration order.

private final int
k

Number of elements in each combination.

private final int
n

Size of the set from which combinations are drawn.

Constructor Summary

AccessConstructor and Description
public
Combinations(int
Size of the set from which subsets are selected.
n
,
int
Size of the subsets to be enumerated.
k
)

Creates an instance whose range is the k-element subsets of {0, ..., n - 1} represented as int[] arrays.

private
Combinations(int
Size of the set from which subsets are selected.
n
,
int
Size of the subsets to be enumerated.
k
,
Combinations.IterationOrder
Specifies the iteration order.
iterationOrder
)

Creates an instance whose range is the k-element subsets of {0, ..., n - 1} represented as int[] arrays.

Method Summary

Modifier and TypeMethod and Description
public Comparator<int[]>

Returns:

a lexicographic comparator.
comparator
()

Defines a lexicographic ordering of combinations.

public int

Returns:

the size of the subsets to be enumerated.
getK
()

Gets the number of elements in each combination.

public int

Returns:

the size of the universe.
getN
()

Gets the size of the set from which combinations are drawn.

public Iterator<int[]>
iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

iterationOrderback to summary
private final Combinations.IterationOrder iterationOrder

Iteration order.

kback to summary
private final int k

Number of elements in each combination.

nback to summary
private final int n

Size of the set from which combinations are drawn.

Constructor Detail

Combinationsback to summary
public Combinations(int n, int k)

Creates an instance whose range is the k-element subsets of {0, ..., n - 1} represented as int[] arrays.

The iteration order is lexicographic: the arrays returned by the iterator are sorted in descending order and they are visited in lexicographic order with significance from right to left. For example, new Combinations(4, 2).iterator() returns an iterator that will generate the following sequence of arrays on successive calls to next():
[0, 1], [0, 2], [1, 2], [0, 3], [1, 3], [2, 3]

If k == 0 an iterator containing an empty array is returned; if k == n an iterator containing [0, ..., n - 1] is returned.
Parameters
n:int

Size of the set from which subsets are selected.

k:int

Size of the subsets to be enumerated.

Exceptions
NotPositiveException:
if n < 0.
NumberIsTooLargeException:
if k > n.
Combinationsback to summary
private Combinations(int n, int k, Combinations.IterationOrder iterationOrder)

Creates an instance whose range is the k-element subsets of {0, ..., n - 1} represented as int[] arrays.

If the iterationOrder argument is set to IterationOrder#LEXICOGRAPHIC, the arrays returned by the iterator are sorted in descending order and they are visited in lexicographic order with significance from right to left. For example, new Combinations(4, 2).iterator() returns an iterator that will generate the following sequence of arrays on successive calls to next():
[0, 1], [0, 2], [1, 2], [0, 3], [1, 3], [2, 3]

If k == 0 an iterator containing an empty array is returned; if k == n an iterator containing [0, ..., n - 1] is returned.
Parameters
n:int

Size of the set from which subsets are selected.

k:int

Size of the subsets to be enumerated.

iterationOrder:Combinations.IterationOrder

Specifies the iteration order.

Exceptions
NotPositiveException:
if n < 0.
NumberIsTooLargeException:
if k > n.

Method Detail

comparatorback to summary
public Comparator<int[]> comparator()

Defines a lexicographic ordering of combinations. The returned comparator allows to compare any two combinations that can be produced by this instance's iterator. Its compare(int[],int[]) method will throw exceptions if passed combinations that are inconsistent with this instance:

  • DimensionMismatchException if the array lengths are not equal to k,
  • OutOfRangeException if an element of the array is not within the interval [0, n).
Returns:Comparator<int[]>

a lexicographic comparator.

getKback to summary
public int getK()

Gets the number of elements in each combination.

Returns:int

the size of the subsets to be enumerated.

getNback to summary
public int getN()

Gets the size of the set from which combinations are drawn.

Returns:int

the size of the universe.

iteratorback to summary
public Iterator<int[]> iterator()

Implements java.lang.Iterable.iterator.

Doc from java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

Returns:Iterator<int[]>

an Iterator.

org.apache.commons.math3.util back to summary

private final Enum Combinations.IterationOrder

extends Enum<Combinations.IterationOrder>
Class Inheritance

Describes the type of iteration performed by the iterator.

Field Summary

Modifier and TypeField and Description
public static final Combinations.IterationOrder
LEXICOGRAPHIC

Lexicographic order.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Combinations.IterationOrder
public static Combinations.IterationOrder[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

LEXICOGRAPHICback to summary
public static final Combinations.IterationOrder LEXICOGRAPHIC

Lexicographic order.

Constructor Detail

IterationOrderback to summary
private IterationOrder()

Method Detail

valueOfback to summary
public static Combinations.IterationOrder valueOf(String name)
valuesback to summary
public static Combinations.IterationOrder[] values()
org.apache.commons.math3.util back to summary

private Class Combinations.LexicographicComparator

extends Object
implements Comparator<int[]>, Serializable
Class Inheritance
  • java.lang.Object
  • org.apache.commons.math3.util.Combinations.LexicographicComparator
All Implemented Interfaces
java.io.Serializable, java.util.Comparator

Defines the lexicographic ordering of combinations, using the lexNorm(int[]) method.

Field Summary

Modifier and TypeField and Description
private final int
k

Number of elements in each combination.

private final int
n

Size of the set from which combinations are drawn.

private static final long
serialVersionUID

Serializable version identifier.

Constructor Summary

AccessConstructor and Description
pack-priv
LexicographicComparator(int
Size of the set from which subsets are selected.
n
,
int
Size of the subsets to be enumerated.
k
)

Method Summary

Modifier and TypeMethod and Description
public int
compare(int[]
the first object to be compared.
c1
,
int[]
the second object to be compared.
c2
)

Implements java.util.Comparator.compare.

Compares its two arguments for order.

private long

Returns:

the lexicographic norm.
lexNorm
(int[]
Input array.
c
)

Computes the value (in base 10) represented by the digit (interpreted in base n) in the input array in reverse order.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

kback to summary
private final int k

Number of elements in each combination.

nback to summary
private final int n

Size of the set from which combinations are drawn.

serialVersionUIDback to summary
private static final long serialVersionUID

Serializable version identifier.

Constructor Detail

LexicographicComparatorback to summary
pack-priv LexicographicComparator(int n, int k)
Parameters
n:int

Size of the set from which subsets are selected.

k:int

Size of the subsets to be enumerated.

Method Detail

compareback to summary
public int compare(int[] c1, int[] c2)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Parameters
c1:int[]

the first object to be compared.

c2:int[]

the second object to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Exceptions
DimensionMismatchException:
if the array lengths are not equal to k.
OutOfRangeException:
if an element of the array is not within the interval [0, n).
lexNormback to summary
private long lexNorm(int[] c)

Computes the value (in base 10) represented by the digit (interpreted in base n) in the input array in reverse order. For example if c is {3, 2, 1}, and n is 3, the method will return 18.

Parameters
c:int[]

Input array.

Returns:long

the lexicographic norm.

Exceptions
OutOfRangeException:
if an element of the array is not within the interval [0, n).
org.apache.commons.math3.util back to summary

private Class Combinations.LexicographicIterator

extends Object
implements Iterator<int[]>
Class Inheritance
  • java.lang.Object
  • org.apache.commons.math3.util.Combinations.LexicographicIterator
All Implemented Interfaces
java.util.Iterator

Lexicographic combinations iterator.

Implementation follows Algorithm T in The Art of Computer Programming Internet Draft (PRE-FASCICLE 3A), "A Draft of Section 7.2.1.3 Generating All Combinations, D. Knuth, 2004.

The degenerate cases k == 0 and k == n are NOT handled by this implementation. If constructor arguments satisfy k == 0 or k >= n, no exception is generated, but the iterator is empty.

Field Summary

Modifier and TypeField and Description
private final int[]
c

c[1], ..., c[k] stores the next combination; c[k + 1], c[k + 2] are sentinels.

private int
j

Marker: smallest index such that c[j + 1] > j

private final int
k

Size of subsets returned by the iterator

private boolean
more

Return value for hasNext()

Constructor Summary

AccessConstructor and Description
pack-priv
LexicographicIterator(int
size of the set from which subsets are enumerated
n
,
int
size of the subsets to enumerate
k
)

Construct a CombinationIterator to enumerate k-sets from n.

Method Summary

Modifier and TypeMethod and Description
public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.

public int[]
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.

public void
remove()

Overrides default java.util.Iterator.remove.

Not supported.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cback to summary
private final int[] c

c[1], ..., c[k] stores the next combination; c[k + 1], c[k + 2] are sentinels.

Note that c[0] is "wasted" but this makes it a little easier to follow the code.

jback to summary
private int j

Marker: smallest index such that c[j + 1] > j

kback to summary
private final int k

Size of subsets returned by the iterator

moreback to summary
private boolean more

Return value for hasNext()

Constructor Detail

LexicographicIteratorback to summary
pack-priv LexicographicIterator(int n, int k)

Construct a CombinationIterator to enumerate k-sets from n.

Note

If k === 0 or k >= n, the Iterator will be empty (that is, hasNext() will return false immediately.

Parameters
n:int

size of the set from which subsets are enumerated

k:int

size of the subsets to enumerate

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

nextback to summary
public int[] next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:int[]

the next element in the iteration

removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Not supported.

org.apache.commons.math3.util back to summary

private Class Combinations.SingletonIterator

extends Object
implements Iterator<int[]>
Class Inheritance
All Implemented Interfaces
java.util.Iterator

Iterator with just one element to handle degenerate cases (full array, empty array) for combination iterator.

Field Summary

Modifier and TypeField and Description
private boolean
more

True on initialization, false after first call to next

private final int[]
singleton

Singleton array

Constructor Summary

AccessConstructor and Description
pack-priv
SingletonIterator(final int[]
array returned by the iterator
singleton
)

Create a singleton iterator providing the given array.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

True until next is called the first time, then false
hasNext
()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.

public int[]

Returns:

the singleton in first activation; throws NSEE thereafter
next
()

Implements java.util.Iterator.next.

Returns the next element in the iteration.

public void
remove()

Overrides default java.util.Iterator.remove.

Not supported

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

moreback to summary
private boolean more

True on initialization, false after first call to next

singletonback to summary
private final int[] singleton

Singleton array

Constructor Detail

SingletonIteratorback to summary
pack-priv SingletonIterator(final int[] singleton)

Create a singleton iterator providing the given array.

Parameters
singleton:int[]

array returned by the iterator

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

True until next is called the first time, then false

nextback to summary
public int[] next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:int[]

the singleton in first activation; throws NSEE thereafter

removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Not supported