在不确定的值集中创建可能的对,而不管顺序如何

时间:2021-04-08 22:49:34

What i am trying to figure out is an algorithm that will create possible pairs irrespective of order in a indefinite set of values.

我想弄清楚的是一种算法,它将创建可能的对,而不管无限组值中的顺序。

for example let's say the set is A,B,C,D,E

例如,假设该集合是A,B,C,D,E

then possible sets are

然后可能的集合

AB AC AD AE BC CD DE

AB AC AD AE BC CD DE

but... i also want pairs of more than 2 values.

但是......我也想要超过2个值的对。

for example

ABC ABD ABE BCD BCE

ABC ABD ABE BCD BCE

but also ABCD or ABCE. The problem here is that i want to make a method with input an array of Strings STring[] and the output would be a list of Strings in pair of 2,3.... up to number of values -1.

还有ABCD或ABCE。这里的问题是我想创建一个输入一个字符串STring []数组的方法,输出将是一对2,3的字符串列表,最多值为-1。

If anyone has a thought of a solution please help. :)

如果有人想到解决方案请帮忙。 :)

4 个解决方案

#1


It seems you want to construct a power set. This question is essentially the same, look there for answers.

看来你想构建一个电源组。这个问题基本相同,请寻找答案。

#2


This is one of the most computation-intensive things you could do. This will not scale well at all with an even remotely large set of data.

这是你可以做的计算密集度最高的事情之一。即使是远程大量的数据,这也无法很好地扩展。

It's really not practical for indefinite sets. You could limit the pairs which can be generated, and therefore make this scale better.

这对于无限期集合来说真的不切实际。您可以限制可生成的对,从而使此比例更好。

#3


What you want to create is some kind of power set of your input's permutations.

您想要创建的是输入排列的某种功率集。

Java's iterator concept theoretically allows infinite sequences.

Java的迭代器概念理论上允许无限序列。

But what has your question to do with comparing?

但是你的问题与比较有什么关系?

#4


Not the most efficient, but a solution:

不是最有效的,而是一个解决方案:

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class PowersetTest {

public static void main(String [] args){
    Set<Set<String>> sets =  permute(Arrays.asList("a","b","c","d","e"));
    for (Set<String> item : sets){
        System.out.printf("Set: %s%n", item.toString());
    }
}
    static public <T> Set<Set<T>> permute (Collection<T> items){
      Set<Set<T>> result = new HashSet<Set<T>>();
      for (final T item : items){
        Set<T> subsetElements = filter(items, new Predicate<T>(){public boolean apply(T f){ return (f!=item);}});
        if (subsetElements.size() > 0) {
          result.addAll(permute(subsetElements));
        }
        Set<T> temp = new HashSet<T>();
        temp.addAll(items);
        result.add(temp);
      }
      return result;
    }

  static public <T> Set<T> filter(Collection<T> items, Predicate<T> filter){ 
    Set<T> result = new HashSet<T>();
    for (T item : items){ 
      if (filter.apply(item)) {
        result.add(item);
      }
    }
    return result;
  }

  public interface Predicate<T>{ public boolean apply(T item); }
}

#1


It seems you want to construct a power set. This question is essentially the same, look there for answers.

看来你想构建一个电源组。这个问题基本相同,请寻找答案。

#2


This is one of the most computation-intensive things you could do. This will not scale well at all with an even remotely large set of data.

这是你可以做的计算密集度最高的事情之一。即使是远程大量的数据,这也无法很好地扩展。

It's really not practical for indefinite sets. You could limit the pairs which can be generated, and therefore make this scale better.

这对于无限期集合来说真的不切实际。您可以限制可生成的对,从而使此比例更好。

#3


What you want to create is some kind of power set of your input's permutations.

您想要创建的是输入排列的某种功率集。

Java's iterator concept theoretically allows infinite sequences.

Java的迭代器概念理论上允许无限序列。

But what has your question to do with comparing?

但是你的问题与比较有什么关系?

#4


Not the most efficient, but a solution:

不是最有效的,而是一个解决方案:

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class PowersetTest {

public static void main(String [] args){
    Set<Set<String>> sets =  permute(Arrays.asList("a","b","c","d","e"));
    for (Set<String> item : sets){
        System.out.printf("Set: %s%n", item.toString());
    }
}
    static public <T> Set<Set<T>> permute (Collection<T> items){
      Set<Set<T>> result = new HashSet<Set<T>>();
      for (final T item : items){
        Set<T> subsetElements = filter(items, new Predicate<T>(){public boolean apply(T f){ return (f!=item);}});
        if (subsetElements.size() > 0) {
          result.addAll(permute(subsetElements));
        }
        Set<T> temp = new HashSet<T>();
        temp.addAll(items);
        result.add(temp);
      }
      return result;
    }

  static public <T> Set<T> filter(Collection<T> items, Predicate<T> filter){ 
    Set<T> result = new HashSet<T>();
    for (T item : items){ 
      if (filter.apply(item)) {
        result.add(item);
      }
    }
    return result;
  }

  public interface Predicate<T>{ public boolean apply(T item); }
}