Computer Science and Engineering, Department of


Date of this Version



Technical Report TR-UNL-CSE-2010-0009

Constraint Systems Laboratory
Department of Computer Science & Engineering
University of Nebraska-Lincoln


In this document, we describe two tree-based algorithms for computing all k-combinations and k-compositions of a finite set.

We have developed two algorithms for solving the following combinatorial tasks:

• Given a finite set S and a natural number k, find all subsets of S of size k. In the literature, this problem is called k-subsets and k-combinations.

• Given two natural numbers k, n where k [less than or equal to] n, find all k-compositions of n where a k-composition is an ordered combination of k nonzero natural numbers whose sum is n. Note that in the literature, a k-composition of n can have null numbers. Further, some authors require that the sum of the k numbers to be less or equal to n. Both algorithms are based on building an intermediary tree data-structure. Using similar tree structures for generating various combinatorial objects under constraints is a “reasonably standard approach” [Hartke 2010]. Algorithms exist in the literature for k-combinations and k-compositions. For example, Wilf [1989] discusses combinatorial Gray codes, and attributes an algorithm for k-compositions to Knuth. Ruskey [1993] shows a bijection between the compositions of Knuth and the combinations of Eades and McKay [1984]. Algorithm pseudocode for those combinatorial problems is reported in Google1, Section 4.3 and 5.7 of [Ruskey 2010], and [Arndt 2010a; 2010b]. The goal of this document is to report the pseudocode of the algorithms implemented in our software [Karakashian 2010].