I’m trying to write an algorithm for finding out the number of ways n numbers can be ordered. For example, two number say a and b can be ordered in 3 ways..
Similarly, 3 numbers can be arranged in 13 ways.
I found out that I can solve the problem using dynamic programming. And here’s is what I’m thinking to have layers which represent different ordering. Ex. a > b have two layers and a = b has a single layer and so on. So that I can use it for later purposes as done in dynamic programming. But I’m not able to write a recurrence relation for the same. Can someone suggest me how can I write that?
Assume f(n,k) = number of possible ways by having k inequality (and so n-k-1 equality), so:
assume you have n-1 number, now you want to add another number and calculate f(n,k), then we have two possibility:
1) There are (k-1) inequalities in those (n-1) numbers, and there are (k+1)(f(n-1,k-1)) ways to add n’th number so that new inequality added.
2) There are k inequalities in those (n-1) numbers, and there are (k+1)(f(n-1,k)) ways to add n’th number with no additional inequality.
and what you want is sum of them (from zero to n-1), Bellow code is in c# (just tested for simple cases), in fact We just calculate number of possible ways not generating all ways..