| home | contents | send comment | send link | add bookmark |

Permutation generator

by Stephen R. Schmitt

Number - n   
Select - k   
Permutations   

Contents

  1. About
  2. The source code
  3. Discussion

1. About

This is a Java Script calculator that counts the number of permutations of k objects in a set of n objects and can list those permutations.

To operate the calculator, enter the total number of objects in the set and the size of the subset of objects. Press the Count button to compute the number of permutations. Press the Listing button to compute the result and generate the listing of permutations.

On invalid entries, the Permutations windows will display:

Error

Return to Contents


2. The source code

The Java Script source code for this program can be viewed by using the View|Source command of your web browser.

You may use or modify this source code in any way you find useful, provided that you agree that the author has no warranty, obligations or liability. You must determine the suitability of this source code for your use.

Return to Contents


3. Discussion

number of permutations

The formula for finding the number of permutations of k objects you can choose from a set of n objects is:

         n!
nPk = ---------
      (n - k)!
For selecting 2 objects out of 3 we get
       3!
3P2 = ---- = 6
       1!
For selecting 4 objects out of 10 we get
       10!     10·9·8·7·6!
10P4 = ----- = ------------- = 5040
        6!          6!

listing the permutations

An algorithm to generate permutations is shown below. Starting from an initial a combination of objects, it computes subsequent permutations in lexicographic order. The algorithm will generate all permutations for a given combination of objects if the initial combination is the first in lexicographic order.

valuei <-- ai

loop

    i <-- k - 1                         % find place to start
    while valuei-1 >= valuei 
        decr i
    end while

    exit when i < 1                     % all in reverse order

    j <-- k                             % do next permutation
    while valuej-1 <= valuei-1 
        decr j
    end while

    swap valuei-1, valuej-1

    incr i 
    j <-- k

    while i < j                         % need more swaps
        swap valuei-1, valuej-1
        incr i
        decr j
    end while

end loop
To generate all k! permutations of each combination, insert the above algorithm into an algorithm that generates combinations such that the objects in each combination are ordered from lowest to highest. The modified combination generator, shown below, will accomplish this.
a0 <-- 1, a1 <-- 2, ... ak-1 <-- k

loop

    permute a0, a1, ... ak-1

    i <-- k - 1                         % start at last item
    while ai = (n - k + i + 1)          % find next item to increment
        decr i

    exit when (i < 0)                   % all done

    incr ai                             % increment

    for j <-- i + 1 ... k - 1 do        % reset elements to the right
        aj <-- ai + j - i
    end for

end loop

See Also

Permutation generator 2 that counts the number of permutations in a set of n objects with duplicate objects.

Permutation generator Zeno source code to generate permutations of k objects in a set of n objects.

Return to Contents



Copyright © 2004, Stephen R. Schmitt