I have an array with a large number of elements in it, and would like to group them as many ways as possible, making each group an element in a new array. However, certain elements are incompatible, example:
objects = {a, b, c, d}
incompatible = {{a,b}, {a,d}, {c,d}}
I would like to generate an array containing all compatible arrangements of objects, with no duplicates. An element of this array can contain any and all number of objects, even singltons, as long as it's contents are all compatible.
in the case of the example above, it would look like this:
compatible_groups = {{},{a},{b},{c},{d},{a,c},{b,c},{b,d}}
One possible solution i considered was a function which takes an arbitrary number of arguments, comparing them all against one another such as:
function generate_groups(...)
local arg={...}
for i=1, #arg-(n+0) do -- only looping partially through array to prevent repeats
for j=i+1, #arg-(n+1) do
for k=j+1, #arg-(n+2) do
...
but for that to work, i would need to have as many nested for loops as the function has arguments. No idea how to do that.
Once that's settled, it would be fairly trivial to check to see if two of the arguments form one of the elements of the incompatible array.
for i=1, #incompatible
if arg[a][1] == incompatible[a][1] and arg[a][2] == incompatible[a][2]...
So i think anyway. Is there a simpler approach I'm missing here?