假设 a=[A, B, C, D],每个元素都有一个权重 w,如果选择则设置为 1,否则为 0。我想按以下顺序生成排列。
1,1,1,1
1,1,1,0
1,1,0,1
1,1,0,0
1,0,1,1
1,0,1,0
1,0,0,1
1,0,0,0
0,1,1,1
0,1,1,0
0,1,0,1
0,1,0,0
0,0,1,1
0,0,1,0
0,0,0,1
0,0,0,0
让我们使用w=[1,2,3,4]代表物品A、B、C、D ... 并且max_weight=4。对于每个排列,如果累计重量超过了max_weight,则停止该排列的计算并转移到下一个排列。例如:
1,1,1 --> 6 > 4, exceeded, stop, move to next
1,1,1 --> 6 > 4, exceeded, stop, move to next
1,1,0,1 --> 7 > 4 finished, move to next
1,1,0,0 --> 3 finished, move to next
1,0,1,1 --> 8 > 4, finished, move to next
1,0,1,0 --> 4 finished, move to next
1,0,0,1 --> 5 > 4 finished, move to next
1,0,0,0 --> 1 finished, move to next
etc calculation continue
到目前为止,[1,0,1,0] 是最佳组合,没有超过最大重量 4
我的问题是:
- 生成所需排列的算法是什么?或者有什么建议可以生成排列?
- 由于元素数量可能高达 10000,并且如果分支的累积重量超过 max_weight,则计算会停止,因此在计算之前不必先生成所有排列。如何使第一步中的算法即时生成排列?