k组合的分布

时间:2019-07-08 12:14:07

标签: clojure combinations combinatorics

我正在从Clojure的列表中生成所有可能的子集/ 3组合(不考虑组合元素的顺序-即子集ABC与CBA等)。

对于列表(A B C D E F G),我的组合结果总计为35,如下所示:

("A" "B" "C")
("A" "B" "D")
("A" "B" "E")
("A" "B" "F")
("A" "B" "G")
("A" "C" "D")
("A" "C" "E")
("A" "C" "F")
("A" "C" "G")
("A" "D" "E")
("A" "D" "F")
("A" "D" "G")
("A" "E" "F")
("A" "E" "G")
("A" "F" "G")
("B" "C" "D")
("B" "C" "E")
("B" "C" "F")
("B" "C" "G")
("B" "D" "E")
("B" "D" "F")
("B" "D" "G")
("B" "E" "F")
("B" "E" "G")
("B" "F" "G")
("C" "D" "E")
("C" "D" "F")
("C" "D" "G")
("C" "E" "F")
("C" "E" "G")
("C" "F" "G")
("D" "E" "F")
("D" "E" "G")
("D" "F" "G")
("E" "F" "G")

这一切都很好,除了我需要使组合对“均匀”分布,即,仅当列表中的所有其他项已组合使用时,才应重复包含“ A”的组合对。如下:

("A" "B" "C")
("D" "E" "F")
("A" "B" "G")
("C" "D" "E")
("A" "F" "G")
... and onwards

我当然希望这是一个已经解决了很多次的普遍问题,但是我很难确定我对该问题进行进一步研究的方向。对算法有何建议或需要进一步研究?

我生成如下所示的组合。

(defn k-combinations [n items]
  (concat (map
            #(cons (first items) %)                         
            (subsets (dec n) (rest items)))
          (subsets n (rest items))))

(defn subsets [n items]
  (cond
    (= n 0) '(())                                           
    (empty? items) '()                                      
    :else (k-combinations n items)))

(subsets 3 '("A" "B" "C" "D" "E" "F" "G"))

0 个答案:

没有答案