如何有效地生成 数字组合的集合而不重复 其中所有集合之间具有特定的区别数。
*注意:范围编号始终从0开始。
范围编号(数字[]) = 0,1,2,3,4,5,6,7 ==>总共8个数字(n)。
组合(k) = 5个数字。
特殊数字(nD) = 2个数字。
结果:
0 1 2 3 4
0 1 2 5 6
0 1 3 5 7
0 1 4 6 7
0 2 3 6 7
0 2 4 5 7
0 3 4 5 6
有7种有效组合
我目前的解决方案效率很低(或者你可以称之为暴力)。
*首先为每个组合循环i。 ==> k C n
*然后我为有效组合创建一个temp。
*然后对于每个组合我验证我的临时,如果它有效然后将其存储在临时,否则忽略它。
那就是它。
以下是我在Console App中的代码:
class Program
{
static List<int[]> ValidCombinations;
static void Main()
{
ValidCombinations = new List<int[]>();
int[] numbers = Enumerable.Range(0, 8).ToArray();
int n = numbers.Length;
const int k = 5;
const int nD = 2;
int maxIntersect = k - nD;
int iCombination = 0;
int iValidCombination = 0;
int[] _temp = new int[k];
foreach (int[] c in FindCombinations(k, n))
{
// #Print out
for (int i = 0; i < n; i++)
{
if (c.Contains(i))
Console.Write(c[Array.IndexOf(c, i)] + " ");
else
Console.Write("_ ");
}
// Save to List
if (IsValidSet(c, maxIntersect))
{
_temp = new int[k];
for (int i = 0; i < c.Length; i++)
{
_temp[i] = c[i];
}
ValidCombinations.Add(_temp);
iValidCombination++;
Console.Write(" ### --> {0}", string.Join(" ", c));
}
Console.WriteLine();
iCombination++;
}
Console.WriteLine("\nTotal Combination = {0}", iCombination);
Console.WriteLine("Valid Combination Found = {0}", iValidCombination);
}
public static IEnumerable<int[]> FindCombosRec(int[] buffer, int done, int begin, int end)
{
for (int i = begin; i < end; i++)
{
buffer[done] = i;
if (done == buffer.Length - 1)
yield return buffer;
else
foreach (int[] child in FindCombosRec(buffer, done + 1, i + 1, end))
yield return child;
}
}
public static IEnumerable<int[]> FindCombinations(int m, int n)
{
return FindCombosRec(new int[m], 0, 0, n);
}
private static bool IsValidSet(int[] set, int maxIntersect)
{
foreach (var item in ValidCombinations)
{
if (set.Intersect(item).Count() > maxIntersect)
return false;
}
return true;
}
}
我从here获得基本代码以生成组合。
这是有效的,但是对于更大范围的数字,此解决方案将花费大量时间来完成。我知道因为所涉及的组合算法,但必须有某种捷径或模式来简化它(我的小脑子未能弄明白)。
非常感谢。
答案 0 :(得分:2)
你的矩阵表示显示这个问题是同源的,或者至少非常类似于在任何一对中找到一组固定大小,常数Hamming weight和常数Hamming distance的不同二进制单词。
以图形方式:
如this question所述,这个问题不一定是微不足道的。特别是,建议的解决方案解释了如何构造Hadamard matrix,哪些行是您正在寻找的二进制单词。
这与您的矩阵非常相似。无论如何,你需要的是更一般的。与此案例不同,您不希望每对行的距离都为n/2
,但距离为d < n/2
。
底线
可以轻松生成具有恒定大小(由您的numbers
数组的长度确定),恒定权重(由您的k
确定)和恒定距离(由你的nD
)在很大程度上取决于这些参数。鉴于some techniques for generating those sets依赖于对这些参数的一些假设,我的猜测是对于一般情况没有有效的算法。
无论如何,如果您重新提问并在MathOverflow上提问,可能会有用,可能会将此问题与我链接的问题相关联。
算法建议
至于算法(就像你的算法一样,赢得了大数字),你可以尝试以下方法:
k
个,后跟(numbers.Length - nD)
个零组成的二进制单词,并将其存储在列表中2*nD
位的每个单词。2*nD
距离时,才尝试将其存储在列表中。与你的方法没有什么不同,但我认为这可以更好一点,你。
答案 1 :(得分:0)
#include<iostream>
#include<vector>
#define N 8
#define K 5
#define D 2
using namespace std;
vector<vector<int>> vv;
vector<int> v;
int intersection(const vector<int>& a, const vector<int>& b)
{//count elements of intersection of two sorted vectors
int count = 0;
auto a_it = a.begin();
auto b_it = b.begin();
while(a_it != a.end() && b_it != b.end()) {
if(*a_it == *b_it) count++, a_it++, b_it++;
else if(*a_it < *b_it) a_it++;
else b_it++;
}
return count;
}
void select_num(int n)
{//might reduce some unnecessary iteration of nCk combination
for(auto& a : vv) if(intersection(a, v) > K - D) return;
//above line will cut off the chain when the intersection is already over
//limit. You can add some more conditions to cut off unnecessary calculation.
if(v.size() == K) {
bool ok = true;
for(auto& a : vv) {
if(intersection(a, v) != K - D) {
ok = false;
break;
}
}
if(ok) vv.push_back(v);
return;
}
if(n == N) return;
//case : select n
v.push_back(n);
select_num(n+1);
v.pop_back();
//case : do not select n
select_num(n+1);
}
int main()
{
select_num(0);
for(auto& a : vv) {
for(auto& b : a) cout << b << ' ';
cout << endl;
}
cout << endl << vv.size() << endl;
}