Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 计数以查找子集频率_C#_Algorithm_Sorting - Fatal编程技术网

C# 计数以查找子集频率

C# 计数以查找子集频率,c#,algorithm,sorting,C#,Algorithm,Sorting,我试图找到N个集合中经常出现的数字组合(相同大小,一个集合中没有重复) 例如: {3, 5, 2, 4, 6, 11} {3, 7, 2, 11, 5, 14} {8, 2, 1, 11, 14, 6} {9, 1, 12, 8, 17, 4} {4, 10, 16, 5, 14, 3} 我使用了一种计数算法来查找集合中单个数字的出现情况 public static int[] Counting (int []A, int m ) { int n = A.Length; int

我试图找到N个集合中经常出现的数字组合(相同大小,一个集合中没有重复)

例如:

{3, 5, 2, 4, 6, 11}
{3, 7, 2, 11, 5, 14}
{8, 2, 1, 11, 14, 6}
{9, 1, 12, 8, 17, 4}
{4, 10, 16, 5, 14, 3}
我使用了一种计数算法来查找集合中单个数字的出现情况

public static int[] Counting (int []A, int m )
{
    int n = A.Length;
    int[] count = new int[m+1];
    Array.Clear(count, 0, m+1);
    for (int k = 0; k < n; k++)
        count[A[k]] += 1;
    return count;
}
公共静态int[]计数(int[]A,int m)
{
int n=A.长度;
int[]计数=新的int[m+1];
数组。清除(计数,0,m+1);
对于(int k=0;k
是否有一种算法可以对子集执行相同的操作。在上面的例子中,{2,11}、{3,2,11}、{11,14}一起出现的频率更高。输出应该有一个子集的计数,即,对于上面的示例,{2,11}频率是3

这对您有用吗

Func<IEnumerable<int>, IEnumerable<IEnumerable<int>>> getAllSubsets = null;
getAllSubsets = xs =>
    (xs == null || !xs.Any())
        ? Enumerable.Empty<IEnumerable<int>>()
        :  xs.Skip(1).Any()
            ? getAllSubsets(xs.Skip(1))
                .SelectMany(ys => new [] { ys, xs.Take(1).Concat(ys) })
            : new [] { Enumerable.Empty<int>(), xs.Take(1) };

var source = new int[][]
{
    new [] {3, 5, 2, 4, 6, 11},
    new [] {3, 7, 2, 11, 5, 14},
    new [] {8, 2, 1, 11, 14, 6},
    new [] {9, 1, 12, 8, 17, 4},
    new [] {4, 10, 16, 5, 14, 3},
};

var subsets = source.Select(x => getAllSubsets(x).Select(y => new { key = String.Join(",", y), values = y.ToArray() }).ToArray()).ToArray();

var keys = subsets.SelectMany(x => x.Select(y => y.key)).Distinct().ToArray();

var query =
    from key in keys
    let count = subsets.Where(x => x.Select(y => y.key).Contains(key)).Count()
    where count > 1
    orderby count descending
    select new { key, count, };
Func getallsubset=null;
getAllSubsets=xs=>
(xs==null | |!xs.Any())
? Enumerable.Empty()
:xs.Skip(1).Any()
? GetAllSubset(xs.Skip(1))
.SelectMany(ys=>new[]{ys,xs.Take(1).Concat(ys)})
:new[]{Enumerable.Empty(),xs.Take(1)};
变量源=新整数[][]
{
新[{3,5,2,4,6,11},
新[{3,7,2,11,5,14},
新[{8,2,1,11,14,6},
新[{9,1,12,8,17,4},
新[{4,10,16,5,14,3},
};
var subsets=source.Select(x=>getAllSubsets(x).Select(y=>new{key=String.Join(“,”,y),values=y.ToArray()}).ToArray()).ToArray();
var key=subset.SelectMany(x=>x.Select(y=>y.key)).Distinct().ToArray();
变量查询=
从钥匙插入钥匙
让count=子集。其中(x=>x.Select(y=>y.key)。包含(key)).count()
其中计数>1
orderby计数递减
选择新{键,计数,};
我得到这个结果:


5
的第一个结果是空集合,每个集合都包含空集合。

Nice,我在使用linq创建所有置换时遇到了难题。