MA 367

Spring 2003

Richard Hitt

Enumerating Permutations

Built-in Function

Mathematica does have a built-in function for enumerating permutations called (you guessed it) Permutations[].

?Permutations

Permutations[{a, b, c, d}]

Note that this function enumerates the permutations in lexicagraphic order.  We can check the runtime of the algorithm by doing a few calculations.

Table[i, {i, 1, 3}]

Table[
Timing[
Permutations[
Table[
i, {i, 1, 
                k}
]
]
]
[[1]], 
{k, 1, 
        9}
]

<< DiscreteMath`Combinatorica`

Table[
Timing[
LexicographicPermutations[
Table[
i, {i, 
                1, k}
]
]
]
[[1]], 
{k, 1, 
        9}
]

Minimum Change Permutations

We can load a package that has a few more combinatorial algorithms that Mathematica loads automatically.

<< DiscreteMath`Combinatorica`

The MinimumChangePermutation function is now defined that produces an enumeration of permutations so that each differs from its next-door neighbors by a single transposition.

MinimumChangePermutations[{a, b, c, d}]

We can also examine the runtime of this algorithm as we did on the previous one.

Table[
Timing[
MinimumChangePermutations[
Table[
i, {i, 
                1, k}
]
]
]
[[1]], 
{k, 1, 
        9}
]

Combinations

Mathematica can enuerate the r-combinations of n objects with the KSubsets function.  For example, to generate the number of 3-subsets of 5 objects, we can use

KSubsets[{a, b, c, d, e}, 3]

Partitions of Integers

The Mathematica funciton Partitions[] can be used to generate all of the partitions of an integer into one or more parts or into k or fewer parts.

?Partitions

Partitions[10, 10]

Partitions[10, 10]

Once you can enumerate partitions, you can then count partitions with particular properties.   For example, you can solve problem #5 on Quiz 2 with the following:

n = 10;
A = Partitions[n, n];
B = {};
For[
i = 1, 
i < 
        Length[A], 
If[Length[A[[i]]] ≤ 3, 
        AppendTo[B, 
          A[[i]]]]; 
i++
]
Print[B]
Length[B]

Generating Functions

Making Change for a Dollar

To introduce generating functions, lets think about the number of ways ot make change for a dollar given an abundant supply of pennies, nickels, dimes, quarters, and half-dollars.  Lets leave out the Sacagawea dollar for now.

To solve this problem, then, we need to count the number of non-negative integer solutions to the equation

(Subscript[x, 1]) + 5  (Subscript[x, 2]) + 10  (Subscript[x, 3]) + 25  (Subscript[x, 4]) + 50  (Subscript[x, 5]) = 
      100.

This is not easy to count using the previous methods in the course.  But we can note that the left-hand side of the above equation is exactly how the exponents are built we multiply and expand the following polynomials.

(Subscript[p, 1]) = Sum[t^i, {i, 0, 100}]
(Subscript[p, 5]) = Sum[t^{5 i}, {i, 0, 20}]
(Subscript[p, 10]) = Sum[t^{10 i}, {i, 0, 10}]
(Subscript[p, 25]) = Sum[t^{25 i}, {i, 0, 4}]
(Subscript[p, 50]) = Sum[t^{50 i}, {i, 0, 2}]

[Long pause] Got it yet?  Well, perhaps not.  [Another long pause]  OK, note in the above summation loops that define the polynomials that the dummy variable i measures how many coins of that particular denomination are used and the exponent measures the total value.  So the exponent on (t^23) , say, in the expansion of (Subscript[p, 1])×(Subscript[p, 2])×(Subscript[p, 3])×(Subscript[p, 4])×(Subscript[p, 5])tells us how many different ways there are to select pennies, nickels, and dimes so that they total 23 cents.  So to solve our problem, all we need to do is expand  (Subscript[p, 1])×(Subscript[p, 2])×(Subscript[p, 3])×(Subscript[p, 4])×(Subscript[p, 5])and find the coefficient of (t^100).  No problem!

Expand[(Subscript[p, 1])*(Subscript[p, 5])*(Subscript[p, 10])*(Subscript[p, 25])*(Subscript[p, 50])]

To save unnecessary output, we could just skip out the 101st term directly to get

Expand[(Subscript[p, 1])*(Subscript[p, 5])*(Subscript[p, 10])*(Subscript[p, 25])*(Subscript[p, 50])][[1, 101]]

So we see that there are 292 ways to make change for a dollar using pennies, nickels, dimes, quarters, and half-dollars.

Exercise

What change (pardon the pun) would you have to make in the above Mathematica code to determine how many ways there are to change a dollar using pennies, nickels, dimes, and quarters, but no half-dollars or Sacagawea dollars?  What is the answer?