I asked a question on a couple of resources until I found the answer. I can not figure out how to optimally do the following algorithm:
Input data:
some positive integer N
some array of arrays (arrays are not repeated) containing arbitrary lists of numbers (from 1 to N), for example: {{1}, {1,2,7}, {3, 5}, ..., {1,2, 3,4,5,6}}
Output:
array of arrays of the following arrays: {{{1}, {2,3}, {4,7}, {5}, {6,8}, ...}, {{1,2}, {3, 9} , {4.7}, {5}, {6.8}, ...}, ...}
Any array of arrays from the output must contain all the numbers from the set (1, 2, ..., N) and not a single number should be repeated twice.
The returned array of arrays of arrays must contain all possible combinations of arrays of arrays that satisfy the conditions above.
Note, just in case, that the array of arrays, which is in the input data, may not contain any of the possible combinations (for example, {1, 2, 3}). Accordingly, this combination and should NOT be considered further.
Example:
Input data:
N = 4 Array of arrays:
{ {1}, {2}, {4}, {1,2}, {1,3}, {1,3,4}, {2,4}, {3,4} } Returned data:
Array of arrays of arrays:
{ { {1},{2},{3,4} }, { {1,2},{3,4} }, { {1,3}, {2}, {4} }, { {1,3}, {2,4} }, { {1,3,4}, {2} } } Update
My version:
At first, a table of the form is generated (let N = 5):
11111 (все числа в одном массиве) 11112 (четыре первых числа в одном массиве и одно во втором) ... 12131 12133 ... 12345 Then from it, arrays of arrays are generated line by line (respectively):
{1,2,3,4,5} {1,2,3,4},{5} ... {1,3,5},{2},{4} {1,3},{2},{4,5} ... {1},{2},{3},{4},{5} Then go through the list and delete all variants containing at least one inaccessible array of numbers. Well, either you can immediately remove unnecessary options, but this is still not the way of the Jedi of some kind - all options are generated and unnecessary ones are deleted.