A Fast Combination Generator

6.4.1 A Fast Combination Generator

We begin by restating the following properties of m-combinations of n items, listed in lexicographic order.

6.4 Generating Combinations in Parallel 159

Property 1. For 1 m n, the first combination in lexicographic order is

(1 2.. . m), and the last one is (n - m + 1 n - m + 2.. . n).

Property 2. Denote the last combination by ( x ,

.. . x,). If ( y , .. . y,) is

one of the other possible combinations, then

(i) y ,

... < and

for 1

i m.

(ii) If there is a subscript i, 2 i m, such that all y's from to y, equal to x,, respectively, and

then the next successive combination is given by where

m. Otherwise, the next successive combination is given by

The preceding discussion leads naturally to our first parallel combination generator. The first combination generated is (1 2.. . m). Now, if ( y , ... y,) is the combination just generated, then the next successive combination is given by property

b, c , x , y, and z, each of length m, in shared memory. The ith position of each of these arrays is denoted by

The algorithm uses five arrays

and respectively. The first of these arrays, array b, is used for broadcasting. Array c is simply an output buffer where every new combination generated is placed. The last three arrays are used to store intermediate results:

1. Array holds the last combination, namely,

for 1

2. Array y holds the current combination being generated.

3. Array keeps track of those positions in y that have reached their limiting values; thus for

if = false otherwise.

The algorithm is given in what follows as procedure PARALLEL COMBINATIONS. It uses m processors

P,,. . . , P, and invokes procedure

BROADCAST.

PARALLEL COMBINATIONS (n,

Step 1: {Initialization) for i =

1 to m do in parallel (1.1)

(1.2) i (1.3) if = then

else

false

end if (1.4)

i end for.

160 Generating Permutations and Combinations Chap. 6 Step 2: {The value of

if broadcast}

BROADCAST , b). Step 3: while

= false do

(3.1) k (3.2) {Find rightmost element of current combination that has not reached its

limiting value}

for i = 2 to do in parallel

if

= false and

= true

(i)

(ii) k i

end if end for

(3.3) BROADCAST (k, m, b) (3.4) {If no element has reached its limiting value, increment y,; otherwise update

all elements from

to

if k = then y,

y, + 1

else (i) BROADCAST

m, b)

(ii) for i = k to m do in parallel

do in parallel (ii) if = then = true

(3.5) for i = 1 to

else = false end if

end while.

Note that step 3.1 is executed by one processor, say, P,. Also, in step 3.2 a t most one processor finds

= false and = true and updates

and k. Finally in the

then part of step 3.4 only one processor, say, P,, increments y,.

Analysis. Steps 1, and 3.5 take constant time. I n steps

2, 3.3, 3.4, and

3.6 procedure BROADCAST requires m) time. Since step 3 is executed ("C, - 1) times, the overall running time of procedure PARALLEL COMBINATIONS is m), a n d its cost

log m), which is not optimal.

Example 6.4

The behavior of PARALLEL COMBINATIONS is illustrated in Fig. 6.2 for the case where n = 5 and m =

3. The figure shows the contents of each of the arrays y, z, and c as well as the value of k after each step of the procedure where they are modified by an assignment. Note that t and

represent true and false, respectively. Also, x,) = (3 4 5) throughout.

AFTER STEP k

Figure 6.2 Generating combinations of three out of five items using procedure PARALLEL COMBINATIONS.

1 62 Generating Permutations and Combinations Chap. 6 Discussion. When stating desirable properties of algorithms in chapter 2,

we said that

(i)

a parallel algorithm should be adaptive, that is, capable of modifying its behavior according to the number of processors actually available on the parallel computer being used,

(ii) its running time should vary with the number of processors used, and (iii) its cost should be optimal.

Procedure PARALLEL COMBINATIONS does not satisfy any of the preceding criteria:

(i) It requires the availability of m processors. (ii) Although quite fast, its running time does not decrease with an increasing

number of processors. (iii) Its cost exceeds the

operations sufficient to generate all m combinations of n items by procedure SEQUENTIAL COMBINATIONS.

The purpose of the next section is to exhibit an algorithm satisfying these three desirable properties.