OAN*_*OAN 11 c# algorithm combinations
如何在不重复的情况下有效地生成数字组合的集合,其中所有集合在彼此之间具有特定的独特数字.
*注意:范围编号始终从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种有效组合
我目前的解决方案效率很低(或者你可以称之为暴力).
*首先我为每个组合循环.==> 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;
}
}
Run Code Online (Sandbox Code Playgroud)
我从这里得到了基本代码来生成组合.
这是有效的,但对于更大范围的数字,此解决方案将花费大量时间来完成.我知道因为所涉及的组合算法,但必须有某种捷径或模式来简化它(我的小脑子没能弄明白).
非常感谢你.
#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;
}
Run Code Online (Sandbox Code Playgroud)