views:

134

answers:

2

I've been able to solve the following problem using std::next_permutation (c++) etc, but I'm now thinking about it in a more general and would very much like to form an expression as this type of problem seems to lend itself - though I'm not having any luck as of yet.

Here is the question:

Given a running race with N contestants, what is the probability that exactly M contestants will finish in a position that is the same as the number on their shirt. Where M <= N.

What I've done so far:

  1. There will be N! ways the race can end,

  2. I've tried fiddling with a small variant of the problem consisting of either 3 or 4 contestants with the required number of people meeting the condition as being 2. in both cases for 2 people finishing in the particular order the probability is 1/2

I'd like to know if there's already some kind of expression that handles all the cases?

Some code:

#include <cstdio>
#include <algorithm>
#include <vector>

int main(int argc, char* argv[]) {
   if (argc != 3) return 1;

   int n = atoi(argv[1]);
   int m = atoi(argv[2]);

   if (m > n) return 1;

   std::vector<int> lst(n);

   for (int i = 0; i < n; ++i) lst[i] = i;

   unsigned int total = 0;
   unsigned int perm_count = 0;

   do {
      int cnt = 0;
      for (int i = 0; i < n; ++i) if (lst[i] == i) ++cnt;
      if (cnt == m) 
         ++total;
      ++perm_count;
   }
   while (std::next_permutation(lst.begin(),lst.end()));

   printf("Probability of (%d,%d) = %8.7f\n",n,m,(1.0 * total / perm_count));

   return 0;
}

Update: The expression is called a Partial Derangement:

http://mathworld.wolfram.com/PartialDerangement.html

Note1: The formula is correct, if one assumes that the fully ordered permutation does not count.

Note2: I've changed the question slightly to make it more clear, hence also changed to code - this should reconsile with comments made by ShreevatsaR.

A: 

There are two definitions you need to solve this in closed form:

  1. The number of ways to permute N people is N! (N factorial), or N * N-1 * N-2 * ... * 1. These are called permutations.

  2. The number of ways to choose M people from N is called (N choose M), and it equals N! / (M! (N-M)!) - these are called combinations. (If this is new to you, do a Google search for "permutations and combinations".)

I'm working on a closed-form solution...

dmazzoni
+4  A: 

The number of permutations of a set with n elements containing m fixed points is

D(n,m) = \frac{n!}{m!}\sum_{k=0}^{n-m}\frac{(-1)^k}{k!}

(see http://en.wikipedia.org/wiki/Random_permutation_statistics#Number_of_permutations_that_are_derangements)

Therefore, the probability is D(n,m)/n!, i.e.

d(n,m) = \frac{1}{m!}\sum_{k=0}^{n-m}\frac{(-1)^k}{k!}

grep
aaah excellent, so the key mathematical term is "fixed points" or "fixed terms"
dman
... or "derangements"?
Zach Scrivena
A more helpful wikipedia reference might be [Rencontres numbers](http://en.wikipedia.org/wiki/Rencontres_numbers) if you are only interested in the results
grddev
@grep is the formula on wiki correct? I'm talking about n!/m!sum(..) formula. I simplify n! and for the case of 5 contestants where 3 is required, I get a different result to those that I see empirically.
dman
@dman: Yes, the formula is correct. For n=5 and m=3, you have D(5,3) = (5!/3!)(1/0! - 1/1! + 1/2!) = (20)(1-1+1/2)=10. And there are indeed 10 permutations — 01243,01324,01432,02134,03214,04231,10234,21034,31204,41230— of 5 elements that have 3 fixed points. So the probability is 10/5! = 10/120 = 1/12 = 0.83333, and not 0.333 as you say in the question.
ShreevatsaR
@ShreevatsaR: (5,3) is a bit small, lets assume (10,4) there might be a permutation that has 5, 6, 7, 8, 9 or even 10 fixed points. how are the various combinations in those situations counted? not counted? for the scenario of 5 fixed points there a total of 20 combinations of 4 fixed points, are there not?
dman
@dman: No, they are not counted. The "M" in your question is interpreted as "exactly M". If you mean "at least M", then count the "exactly M" for each M. That is, if you count the answers to (10,4), (10,5), (10,6) and so on (given by the formula for D(n,m)), you'll get the number of permutations with at least 4 fixed points.
ShreevatsaR
@ShreevarsaR: So if i were to reword the question to say exactly M fixed points then the partial derangement approach would be correct.
dman