Algorithm to generate spanning set

后端 未结 5 1855
粉色の甜心
粉色の甜心 2021-02-20 12:19

Given this input: [1,2,3,4]

I\'d like to generate the set of spanning sets:

[1] [2] [3] [4]
[1] [2] [3,4]
[1] [2,3] [4]
[1] [3] [2,4]
[1,2] [3] [4]
[1,3]         


        
5条回答
  •  逝去的感伤
    2021-02-20 12:50

    The result sets together with the empty set {} looks like the results of the powerset (or power set), but it is not the same thing.

    I started a post about a similar problem which has a few implementations (although in C#) and geared more for speed than clarity in some cases. The first example should be easy to translate. Maybe it will give a few ideas anyway.

    They work on the principle that emmumerating the combinations is similar to counting in binary (imagine counting from 0 to 16). You do not state if the order is important, or just generating all the combinations, so a quick tidy up may be in order afterwards.

    Have a look here (ignore the odd title, the discussion took another direction)

提交回复
热议问题