生成所有“独特"的集合的子集(不是幂集)

编程入门 行业动态 更新时间:2024-10-22 10:38:10
本文介绍了生成所有“独特"的集合的子集(不是幂集)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我们有一个集合 S,其中包含一些子集:

Let's say we have a Set S which contains a few subsets:

- [a,b,c] - [a,b] - [c] - [d,e,f] - [d,f] - [e]

假设 S 包含六个唯一元素:a、b、c、d、e 和 f.

Let's also say that S contains six unique elements: a, b, c, d, e and f.

我们如何才能找到所有可能的 S 子集,其中包含 S 的每个唯一元素恰好一次?

How can we find all possible subsets of S that contain each of the unique elements of S exactly once?

函数/方法的结果应该是这样的:

The result of the function/method should be something like that:

  • [[a,b,c], [d,e,f]];
  • [[a,b,c], [d,f], [e]];
  • [[a,b], [c], [d,e,f]];
  • [[a,b], [c], [d,f], [e]].
  • 是否有任何最佳实践或任何标准方法来实现这一目标?

    Is there any best practice or any standard way to achieve that?

    如果有伪代码、Ruby 或 Erlang 示例,我将不胜感激.

    I would be grateful for a Pseudo-code, Ruby or Erlang example.

    推荐答案

    听起来你正在寻找的是 分区 集合/数组.

    It sounds like what you are looking for are the partitions of a set/array.

    这样做的一种方法是递归:

    One way of doing this is recursively:

    • 一个 1 元素数组 [x] 恰好有一个分区
    • 如果 x 是 x = [head] + tail 形式的数组,则 x 的分区是通过获取 tail 的每个分区并将 head 添加到每个可能的来生成的.例如,如果我们生成 [3,2,1] 的分区,那么从 [2,1] 的分区 [[2,1]] 我们可以将 3 添加到 [2,1] 或作为单独的元素,这为我们提供了 [1,2,3] 的 5 个分区中的 2 个分区 [[3,2,1] 或 [[2,1], [3]]

    ruby 实现有点像

    def partitions(x) if x.length == 1 [[x]] else head, tail = x[0], x[1, x.length-1] partitions(tail).inject([]) do |result, tail_partition| result + partitions_by_adding_element(tail_partition, head) end end end def partitions_by_adding_element(partition, element) (0..partition.length).collect do |index_to_add_at| new_partition = partition.dup new_partition[index_to_add_at] = (new_partition[index_to_add_at] || []) + [element] new_partition end end

    更多推荐

    生成所有“独特"的集合的子集(不是幂集)

    本文发布于:2023-11-29 00:39:20,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1644602.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:子集   独特   quot

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!