2010-11-04 96 views
3

因此,我试图从给定的N元素集合中找到所有k元素子集的问题。我知道使用公式C(n,k)= C(n-1,k-1)+ C(n-1,k)的k个子集的总数是多少,我也知道如何去做以迭代的方式,但是当我尝试去思考递归解决方案时,我陷入了困境。任何人都可以给我一个提示吗? 谢谢!如何在Java中递归地生成N元素集合中的所有k元素子集

+0

是否所有N元素都不同? – 2010-11-04 15:27:32

+0

您应该阅读格雷码。 – 2010-11-04 15:31:33

+0

@SKD,我假设是这样,它是一组 – 2010-11-04 15:31:44

回答

6

对于集合中的每个元素,取该元素,然后依次添加剩余N-1个元素集合的所有(k-1)个子集。

“这是个月黑风高的夜晚,船长说......”

+1

提及安东尼奥。 – 2010-11-04 15:40:32

+0

我真的需要一个关于如何将它转换为递归方法的想法,因为如前所述,我有一个涉及嵌套循环的迭代解决方案,但不知道如何将其转换为递归方法。 – rdplt 2010-11-04 16:18:49

+0

这是一个递归的方法!它说产生一个N集合的k子集的问题与取N的每个元素相同,然后计算剩下的N-1个大小的集合的k-1集合。当K变为1时,计算出它的子集是微不足道的。 – 2010-11-04 16:24:50

1

更好

这打破了k=0情况下,因为我认为它会返回一个包含了一组空集,这是不正确的。无论如何。这里还有一个迭代,如果目标是递归的,你可以用递归替换它。这是对wikipedia: powerset给出的算法的相当直接的修改。我会留下修复角落案件(k = 0)给读者。

这不是正确的尾递归,并不是它在大多数JVM中都很重要。 (我猜的IBM JVM做到这一点...)

class RecursivePowerKSet 
{ 
    static public <E> Set<Set<E>> computeKPowerSet(final Set<E> source, final int k) 
    { 
    if (k==0 || source.size() < k) { 
     Set<Set<E>> set = new HashSet<Set<E>>(); 
     set.add(Collections.EMPTY_SET); 
     return set; 
    } 

    if (source.size() == k) { 
     Set<Set<E>> set = new HashSet<Set<E>>(); 
     set.add(source); 
     return set; 
    } 

    Set<Set<E>> toReturn = new HashSet<Set<E>>(); 

    // distinguish an element 
    for(E element : source) { 
     // compute source - element 
     Set<E> relativeComplement = new HashSet<E>(source); 
     relativeComplement.remove(element); 

     // add the powerset of the complement 
     Set<Set<E>> completementPowerSet = computeKPowerSet(relativeComplement,k-1); 
     toReturn.addAll(withElement(completementPowerSet,element)); 
    } 

    return toReturn; 
    } 

    /** Given a set of sets S_i and element k, return the set of sets {S_i U {k}} */ 
    static private <E> Set<Set<E>> withElement(final Set<Set<E>> source, E element) 
    { 

    Set<Set<E>> toReturn = new HashSet<Set<E>>(); 
    for (Set<E> setElement : source) { 
     Set<E> withElementSet = new HashSet<E>(setElement); 
     withElementSet.add(element); 
     toReturn.add(withElementSet); 
    } 

    return toReturn; 
    } 

    public static void main(String[] args) 
    { 
    Set<String> source = new HashSet<String>(); 
    source.add("one"); 
    source.add("two"); 
    source.add("three"); 
    source.add("four"); 
    source.add("five"); 

    Set<Set<String>> powerset = computeKPowerSet(source,3); 

    for (Set<String> set : powerset) { 
     for (String item : set) { 
     System.out.print(item+" "); 
     } 
     System.out.println(); 
    } 
    } 
} 

发电机组只有 这并不可能完全得到那里,是不是真的优雅,但它递归计算全幂,然后修剪它(迭代)的大小。

class RecursivePowerSet 
{ 


    static public <E> Set<Set<E>> computeConstrainedSets(final Set<Set<E>> source, final SizeConstraint<Set<E>> constraint) 
    { 
    Set<Set<E>> constrained = new HashSet<Set<E>>(); 
    for (Set<E> candidate : source) { 
     if (constraint.meetsConstraint(candidate)) { 
     constrained.add(candidate); 
     } 
    } 
    return constrained; 
    } 

    static public <E> Set<Set<E>> computePowerSet(final Set<E> source) 
    { 

    if (source.isEmpty()) { 
     Set<Set<E>> setOfEmptySet = new HashSet<Set<E>>(); 
     setOfEmptySet.add(Collections.EMPTY_SET); 
     return setOfEmptySet; 
    } 


    Set<Set<E>> toReturn = new HashSet<Set<E>>(); 

    // distinguish an element 
    E element = source.iterator().next(); 

    // compute source - element 
    Set<E> relativeComplement = new HashSet<E>(source); 
    relativeComplement.remove(element); 

    // add the powerset of the complement 
    Set<Set<E>> completementPowerSet = computePowerSet(relativeComplement); 
    toReturn.addAll(completementPowerSet); 
    toReturn.addAll(withElement(completementPowerSet,element)); 

    return toReturn; 
    } 

    static private <E> Set<Set<E>> withElement(final Set<Set<E>> source, E element) 
    { 

    Set<Set<E>> toReturn = new HashSet<Set<E>>(); 
    for (Set<E> setElement : source) { 
     Set<E> withElementSet = new HashSet<E>(setElement); 
     withElementSet.add(element); 
     toReturn.add(withElementSet); 
    } 

    return toReturn; 
    } 

    public static void main(String[] args) 
    { 
    Set<String> source = new HashSet<String>(); 
    source.add("one"); 
    source.add("two"); 
    source.add("three"); 
    source.add("four"); 
    source.add("five"); 

    SizeConstraint<Set<String>> constraint = new SizeConstraint<Set<String>>(3); 

    Set<Set<String>> powerset = computePowerSet(source); 
    Set<Set<String>> constrained = computeConstrainedSets(powerset, constraint); 
    for (Set<String> set : constrained) { 
     for (String item : set) { 
     System.out.print(item+" "); 
     } 
     System.out.println(); 
    } 

    } 

    static class SizeConstraint<V extends Set> { 

    final int size; 
    public SizeConstraint(final int size) 
    { 
    this.size = size; 
    } 

    public boolean meetsConstraint(V set) 
    { 
     return set.size() == size; 
    } 
    } 

} 
+0

它并没有真正帮助我。 – rdplt 2010-11-05 20:15:29

+0

@ user497302:为什么不呢?它计算所有的k子集,递归...编译它,它的工作。 – andersoj 2010-11-05 20:17:20

0

这是一些伪代码。通过随时随地存储每个呼叫的值,并在递归呼叫检查之前(如果呼叫值已存在),可以剪切相同的递归调用。

以下算法将具有排除空集的所有子集。

list * subsets(string s, list * v){ 
    if(s.length() == 1){ 
     list.add(s);  
     return v; 
    } 
    else 
    { 
     list * temp = subsets(s[1 to length-1], v);  
     int length = temp->size(); 

     for(int i=0;i<length;i++){ 
      temp.add(s[0]+temp[i]); 
     } 

     list.add(s[0]); 
     return temp; 
    } 
} 
相关问题