I hope someone is able to help me with what is, at least to me, quite a tricky algorithm.
The Problem
I have a List (1 <= size <= 5
, but size unknown until run-time) of Lists (1 <= size <= 2
) that I need to combine. Here is an example of what I am looking at:-
ListOfLists = { {1}, {2,3}, {2,3}, {4}, {2,3} }
So, there are 2 stages to what I need to do:-
(1). I need to combine the inner lists in such a way that any combination has exactly ONE item from each list, that is, the possible combinations in the result set here would be:-
- 1,2,2,4,2
- 1,2,2,4,3
- 1,2,3,4,2
- 1,2,3,4,3
- 1,3,2,4,2
- 1,3,2,4,3
- 1,3,3,4,2
- 1,3,3,4,3
The Cartesian Product takes care of this, so stage 1 is done.....now, here comes the twist which I can't figure out - at least I can't figure out a LINQ way of doing it (I am still a LINQ noob).
(2). I now need to filter out any duplicate results from this Cartesian Product. A duplicate in this case constitutes any line in the result set with the same quantity of each distinct list element as another line, that is,
1,2,2,4,3 is the "same" as 1,3,2,4,2
because each distinct item within the first list occurs the same number of times in both lists (1 occurs once in each list, 2 appears twice in each list, ....
The final result set should therefore look like this...
- 1,2,2,4,2
- 1,2,2,4,3
- --
- 1,2,3,4,3
- --
- --
- --
- 1,3,3,4,3
Another example is the worst-case scenario (from a combination point of view) where the ListOfLists is {{2,3}, {2,3}, {2,3}, {2,3}, {2,3}}, i.e. a list containing inner lists of the maximum size - in this case there would obviously be 32 results in the Cartesian Product result-set, but the pruned result-set that I am trying to get at would just be:-
- 2,2,2,2,2
- 2,2,2,2,3 <-- all other results with four 2's and one 3 (in any order) are suppressed
- 2,2,2,3,3 <-- all other results with three 2's and two 3's are suppressed, etc
- 2,2,3,3,3
- 2,3,3,3,3
- 3,3,3,3,3
To any mathematically-minded folks out there - I hope you can help. I have actually got a working solution to part 2, but it is a total hack and is computationally-intensive, and I am looking for guidance in finding a more elegant, and efficient LINQ solution to the issue of pruning.
Thanks for reading.
pip
Some resources used so far (to get the Cartesian Product)
UPDATE - The Solution
Apologies for not posting this sooner...see below
See Question&Answers more detail:
os