Algorithm to generate spanning set

后端 未结 5 1859
粉色の甜心
粉色の甜心 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:44

    What about this? I haven't tested it yet, but I'll try it later…

    I think this technique is called Dynamic Programming:

    1. Take the first element [1]
      What can you create with it? Only [1]

    2. Take the second one [2]
      Now you've got two possibilities: [1,2] and [1] [2]

    3. Take the third one [3]
      With the first of number 2 [1,2] one can create [1,2,3] and [1,2] [3]
      With the second of number 2 [1] [2] one can create [1,3] [2] and [1] [2,3] and [1] [2] [3]

    I hope it is clear enough what I tried to show. (If not, drop a comment!)

提交回复
热议问题