2011-11-29 62 views
8

通過對列表進行分區,我指的是列表元素的一組子集,使得任何不同的子集對的交集都是空的,並且所有子集的並集等於原始列表。如何獲得Mathematica中列表的所有分區?

例如,如果我的輸入列表是{1,π,x}那麼我想返回

{ {{1},{π},{x}}, {{1,π},{x}}, {{1,x},{π}}, {{1},{x,π}}, {{1,π,x}} } 
+0

@yoda:該OP可能與術語混淆。我同意這些不是分區。 – Blender

+0

@Blender是的,我只是意識到他可能會得到別的東西。 – abcd

+1

@Blender,yoda:這些是[集合意義上的分區](http://en.wikipedia.org/wiki/Partition_of_a_set),不是Mathematica命令[Partition](http:// reference .wolfram.com /數學/ REF/Partition.html)。 – Simon

回答

12

使用適合代碼http://mathforum.org/advanced/robertd/bell.html

BellList[1] = {{{1}}}; 
BellList[n_Integer?Positive] := Join @@ 
    (ReplaceList[#, 
    {{b___, {S__}, a___} :> {b, {S, n}, a}, 
    {S__} :> {S, {n}}} 
    ] & /@ BellList[n - 1]) 

s = {a, b, c, d, e}; 

bell = [email protected]@s /. n_Integer :> s[[n]] 

或者,勿庸置疑,在Combinatorica包有此功能(SetPartitions)了!

Needs["Combinatorica`"] 

comb = SetPartitions[{a, b, c, d, e}] 

檢查,他們都返回相同的結果(但在不同的順序)

Complement[bell, comb] == {} 
[email protected] == [email protected] 
(* Both of the above return True *) 
+0

@Wizard先生,這將需要我一點時間精神分析,但據我可以告訴它做的工作,謝謝! – Michael

+0

@Michael,我忘了檢查標準庫中的這個函數。查看我剛纔提供的更新。 –

+0

@Simon,感謝您的編輯。 –

2

我將開始與設定的Powerset功能(使用Subsets[x]),然後濾除那些地方Union[x]的集合不是原始集合。

有點慢,但我覺得它很直觀。

+0

{{1,x},{π}}'的聯合不是原始集合;儘管OP要求它。 –

+3

@BillyONeal爲什麼你說聯合{1,x}和{π}不等於{1,π,x}?一組元素的順序與集合的定義無關... – Michael

+0

@Blender:我站得更正了。 :) –

相關問題