The Problem:
So I was thinking up some simple combinatorics problems, and this one stumped me.
Let N be the set of numbers $\{1 .. n\}$, or any set of cardinality $n$
Let K be the set of numbers $\{1 .. k\}$ where $k < n$, or any subset of N of cardinality $k$
How many permutations of N exist such that no two members of set k are adjacent?
Here was my basic approach:
Solutions = Permutations of N - Permutations of N that contain a pair in K
Permutations of N = nPn = n!
Every item in K will occur in each permutation of N so we loop through:
You place down one item of K Possibilities where the next is in K = K - 1 You place down an item of K possibilities where next is in k = k-2 ... You place the last item in KThe total permutations with a pair would therefore be:
$ (k-1) + (k-2) + ... + 0 $
However, many of those entries are duplicates, so we have to rule out instances where two of those pairs occurred in one set, and then again rule out instances that occurred three times, up pairs occurred k times
I think the amount with two pairs would best be found by getting the amount with one pair minus the amount with no more.
To do this I would loop as:
You place two items in K There are k-2 chances the next item is in k You place an item in K There are k-3 chances the next item is in k ... You place your last item in KTotal permutations with two pairs $= (k - 2) + (k - 3) + ... + 0$
Total permutations with three pairs $= (k - 3) + (k - 4) + ... + 0$
And so on..
And at this point I know I am incorrect, because I would get a total of:
$( (k-1) + (k-2) + ... + 0 ) - ( (k-2) + (k-3) + ... + 0 ) - ( (k-3) + (k-4) + ... + 0 ) + ... + 0$ permutations that contain pairs.
This number is waaay negative...
As I pointed out at the end of that, my solution finds a negative amount of permutations that have pairs in them, and so it is clearly wrong. If someone could explain my error, or else show a better way to approach the problem, I would greatly appreciate it.
Things that I think could be causing my answer to be wrong:
- I'm not sure if my generalization for removing the permutations with multiple pairs from the total amount of possible pairs works correctly if the pair does not occur as the first appearance of a member of set K