I have a game that one player X wants to pass a ball to player Y, but he can be playing with more than one player and the others players can pass the ball to Y.

I want to know how many different paths can the ball take from X to Y?

for example if he is playing with 3 players there are 5 different paths, 4 players 16 paths, if he is playing with 20 players there are 330665665962404000 paths, and 40 players 55447192200369381342665835466328897344361743780 that the ball can take.
the number max. of players that he can play with is 500.

I was thinking in using Catalan Numbers? do you think is a correct approach to solve this?
Can you give me some tips.

If there are 4 players in total and assuming he wants to throm from player A to player D the routes the ball can take are A->D, A->B->D, A->C->D, A->B->C->D or A->C->B->D.
–
deanWombourneMay 28 '10 at 13:25

@Larry: I think it is clear from the writeup what phimuemue meant by n over i. Besides, n choose i need not be standard terminology. The mistake is that the sum must be from 0 to n.
–
AryabhattaMay 28 '10 at 13:40

It might not be, but it's correct and precise, free from context. Honestly I thought it was n / i for a few seconds.
–
LarryMay 28 '10 at 13:59

I dismissed your answer as wrong, but when I calculated mine I saw they were the same :) +1
–
UnreasonMay 28 '10 at 14:45

Mark the number of different paths from player X to player Y as F(n), where n is the number of players including Y but not X.
Now, how many different paths are there? Player X can either pass the ball straight to Y (1 option), or pass it to one of the other players (n-1 options). If X passes to another player, we can pretend that player is the new X, where there are n-1 players in the field (since the 'old' X is no longer in the game). That's why
F(n) = 1 + (n-1)F(n-1)
and
F(1) = 1

I'm pretty sure you can reach phimuemue's answer from this one. The question is if you prefer a recursive solution or one with summation.

this solution works well for small amount of players, but when i put 40 players, for example it doesn't work. the result should be 24034400959142450300587879489790 and i get 4386910660075210148 as result. I am using this code: long F(int n){ if(n==1) return 1; return 1+(n-1)*F(n-1); what am i doing wrong?
–
PeiskaMay 28 '10 at 15:48

I'm somewhat of a noob at this kind of searching, but a quick run through the numbers demonstrates the more you can trim, cut out, filter out, the faster you can do it. The numbers you cite are BIG.

First thing that comes to mind is "Is it practical to limit your search depth?" If you can limit your search depth to say 4 (an arbitrary number), your worst case number of possibilities comes out to ...

Which gives a total of 5 different paths, similarly for 3 extra players you have 16 different paths

First try to reduce the problem to something known, and for this I would eliminate X-Y, they are common to all of the above translates to question: what is the sum of k-permutations for k from 0 to n, where n is the number of P.

This can be given as

f(n):=sum(n!/(n-i)!,i,0,n);

and I can confirm your findings for 19 and 39 (20 and 40 in your notation).

EDIT: After more clarification from the comments of the question, my answer is absolutely useless :) he definitely wants the number of possible routes, not the best one!

My first thought is why do you want to know these numbers? You're certainly never going to iterate through all the paths available to 500 people (would take far too long) and it's too big to display on a ui in any meaningful way.

I'm assuming that you're going to try to find the best route that the ball can take in which case I would consider looking into algorithms that don't care about the number of nodes in a route.

The question makes it quite clear that it's not the best route that he wants (after all, the best route is always 'pass from X to Y').
–
Adriano Varoli PiazzaMay 28 '10 at 13:33

Not necessarily X->Y every time - the ball might have a max distance to travel in his game or some players might be better at throwing or there might be an opposition player in the way! Though I have just noticed the 'homework' tag in his question so my answer probably isn't that helpful!
–
deanWombourneMay 28 '10 at 13:56