In the previous article on permutations, the special case of arrangements of all the “n” distinct items, was dealt with. Now, with that and the logic of combinations (different ways of selecting “k” items from “n” distinct items), we are all set for the general permutations of first selecting “k” items from “n” distinct items, and then arranging those “k” items (in different permutations or possible ways). In mathematical terms, it is denoted by ^{n}P_{k} (read n P k), permutations of “k” items from “n” (distinct) items.

As mentioned in the previous article, the total number of such different permutations or arrangements possible can be readily computed using the following recursive relation and the terminating condition:

^{n}P_{k} = k * ^{n-1}P_{k-1} + ^{n-1}P_{k}, n > k > 0 (recursive relation)

^{n}P_{k} = k * ^{n-1}P_{k-1}, n = k > 0 (recursive relation)

^{n}P_{k} = 1, for k = 0 (terminating condition)

Note that, the implicit condition on which ^{n}P_{k} is defined is n >= k >= 0.

A straight away functional recursive implementation would look like this:

permutations(n, k) { if (k == 0) return 1; else if (k == n) return k * permutations(n - 1, k - 1); else return k * permutations(n - 1, k - 1) + permutations(n - 1, k); }

And now with the earlier two logics of printing (different possible selections of “k” items from “n” (distinct) items, and all different possible arrangements of “n” (distinct) items), it is just a matter of few tweaks to be able to invoke the above two logics to solve the general permutations printing logic.

Note that the general permutations is a two step process of first selecting “k” items from “n” distinct items, and then arranging those “k” items (in different permutations or possible ways). Hence, it can be implemented recursively, using a recursive logic similar to that of the selection logic, and then applying the arrangement logic, once selected.

Assuming the print_arrangements(n, basket[]) available from the previous article, and invoking & renaming the selections() logic from its previous article, we have the following:

selections_arrangements(n, k, basket[], plen, prefix[]) { if (k == 0) { print_arrangements(plen, prefix); } else if (k == n) { for (i = 1; i < k; i++) { prefix[plen + i] = basket[i]; } print_arrangements(plen + k, prefix); } else { // Following two recursive calls of selections_arrangements // have been swapped, compared to the original recursive calls // of selections, to print them from left to right prefix[plen] = basket[0]; selections_arrangements(n - 1, k - 1, basket + 1, plen+1, prefix); selections_arrangements(n - 1, k, basket + 1, plen, prefix); } }

Note the change to the invocation of print_arrangements() logic, instead of just the print() logic, in the cases of k == 0 and k == n, where the selecting of “k” items from “n” distinct items is already complete and now their arrangements need to be printed.

Moreover, as the cases k == 0 and k == n, take similar action, they can be further merged as follows:

selections_arrangements(n, k, basket[], plen, prefix[]) { if ((k == 0) || (k == n)) { for (i = 1; i < k; i++) { prefix[plen + i] = basket[i]; } print_arrangements(plen + k, prefix); } else { prefix[plen] = basket[0]; selections_arrangements(n - 1, k - 1, basket + 1, plen+1, prefix); selections_arrangements(n - 1, k, basket + 1, plen, prefix); } }

And again, as in the previous articles, to make the toplevel call look beautiful, a wrapper print_selections_arrangements() can be provided as follows:

print_selections_arrangements(n, k, basket[]) { plen = 0; prefix[] = {}; // prefix being capable of supporting max "k" items selections_arrangements(n, k, basket, plen, prefix); }

Pingback: Permutations | Playing with Systems

Pingback: Naturally Recursive Trees | Playing with Systems