<分区>
是否有任何有效的方法可以将集合 {1, ... , 2*n}
的所有划分列成 n
对?
最简单的想法是列出所有排列,然后排列 (a1, a2, ... , a8)
表示除法 {{a1,a2}, ... , {a7 ,a8}}
。在这种情况下,每个分区都有 2^n * n!
排列。这是 O((2n)!)
。
我能找到更有效的方法吗?
<分区>
是否有任何有效的方法可以将集合 {1, ... , 2*n}
的所有划分列成 n
对?
最简单的想法是列出所有排列,然后排列 (a1, a2, ... , a8)
表示除法 {{a1,a2}, ... , {a7 ,a8}}
。在这种情况下,每个分区都有 2^n * n!
排列。这是 O((2n)!)
。
我能找到更有效的方法吗?
最佳答案
这是我的纸笔算法:
f(set,result):
if the set is empty:
return result
otherwise:
pair one item from the set with
each of the remaining items,
calling f again with the pair added to
the result and out of the set
123456
12 34 56
35 46
36 45
13 24 56
25 46
26 45
14 23 56
25 36
26 35
...
关于algorithm - 具有偶数个元素的集合的划分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33372926/