I have a bunch of sets say S1, S2, S3, ... Each set have different elements. Say S1 = { A, B, C}, S2 = { X, Y }, S3 = { P, Q, R, T }
There exists a combination of these sets K = { S1, S2, S3 }. For example, an instance of this combination would be { A, X, P }. There are obviously 3 x 2 x 4 = 24 combinations possible. What I need is the "rank" of a particular combination, calculated using simple ordered enumeration from left to right, and the vice versa.
Obviously, I can calculate this easily by simply enumerating all the combinations and comparing it to the requested combination while keeping a counter, but I need an efficient algorithm as my sets can contain up to 20000 elements each, and number of combined sets for some case are > 10.
BTW, I am aware of the thread Compute rank of a combination? here in stack overflow. But, unfortunately, it is not applicable here as my combinations are made out of different sized sets for different positions
I would appreciate an implementation in C# but other languages or pseudo-code would also be very helpful.
Any suggestions, please
kemal
UPDATE: @spinning_plane & @aasmund. Thank you for the answers. They both provide me with the same formula for calculating the rank.
But, I also need the other way around. i.e. to get the combination for a given rank (zero based). For example, given the rank 0, the result would be {A,X,P} ,for 3 {A, X, R }, etc. Anyone with an algorithm, please?